|
| 1 | +import glob |
| 2 | +import os |
| 3 | + |
| 4 | +from conans import ConanFile, CMake, tools |
| 5 | +from conans.errors import ConanInvalidConfiguration |
| 6 | + |
| 7 | +class NetcdfConan(ConanFile): |
| 8 | + name = "netcdf-c" |
| 9 | + description = "The Unidata network Common Data Form (netCDF) is an interface " \ |
| 10 | + "for scientific data access and a freely-distributed software " \ |
| 11 | + "library that provides an implementation of the interface." |
| 12 | + license = "BSD-3-Clause" |
| 13 | + topics = ("conan", "netcdf-c", "netcdf", "array", "dataset", "scientific") |
| 14 | + homepage = "https://www.unidata.ucar.edu/software/netcdf" |
| 15 | + url = "https://github.com/conan-io/conan-center-index" |
| 16 | + exports_sources = ["CMakeLists.txt", "patches/**"] |
| 17 | + generators = "cmake", "cmake_find_package" |
| 18 | + settings = "os", "arch", "compiler", "build_type" |
| 19 | + options = { |
| 20 | + "shared": [True, False], |
| 21 | + "fPIC": [True, False], |
| 22 | + "build_utilities": [True, False], |
| 23 | + "netcdf4": [True, False], |
| 24 | + "with_hdf4": [True, False], |
| 25 | + "with_hdf5": [True, False], |
| 26 | + "dap": [True, False], |
| 27 | + "parallel": [True, False], |
| 28 | + } |
| 29 | + default_options = { |
| 30 | + "shared": False, |
| 31 | + "fPIC": True, |
| 32 | + "build_utilities": True, |
| 33 | + "netcdf4": True, |
| 34 | + "with_hdf4": False, |
| 35 | + "with_hdf5": True, |
| 36 | + "dap": True, |
| 37 | + "parallel": False, |
| 38 | + } |
| 39 | + |
| 40 | + _cmake = None |
| 41 | + |
| 42 | + @property |
| 43 | + def _source_subfolder(self): |
| 44 | + return "source_subfolder" |
| 45 | + |
| 46 | + @property |
| 47 | + def _build_subfolder(self): |
| 48 | + return "build_subfolder" |
| 49 | + |
| 50 | + def config_options(self): |
| 51 | + if self.settings.os == "Windows": |
| 52 | + del self.options.fPIC |
| 53 | + |
| 54 | + def configure(self): |
| 55 | + if self.options.shared: |
| 56 | + del self.options.fPIC |
| 57 | + del self.settings.compiler.cppstd |
| 58 | + del self.settings.compiler.libcxx |
| 59 | + |
| 60 | + if self.options.netcdf4 and not self.options.with_hdf5: |
| 61 | + raise ConanInvalidConfiguration("netcdf4 requires hdf5") |
| 62 | + if not self.options.netcdf4: |
| 63 | + if self.options.with_hdf4: |
| 64 | + raise ConanInvalidConfiguration("netcdf4 is required for hdf4 features") |
| 65 | + if self.options.parallel: |
| 66 | + raise ConanInvalidConfiguration("netcdf4 is required for parallel IO") |
| 67 | + self._strict_options_requirements() |
| 68 | + |
| 69 | + def _strict_options_requirements(self): |
| 70 | + if self.options.with_hdf5: |
| 71 | + self.options["hdf5"].with_zlib = True |
| 72 | + self.options["hdf5"].hl = True |
| 73 | + if self.options.parallel: |
| 74 | + raise ConanInvalidConfiguration("parallel option requires openmpi which is not yet available in CCI") |
| 75 | + self.options["hdf5"].parallel = True # TODO: option not yet available in hdf5 recipe, requires openmpi in CCI |
| 76 | + |
| 77 | + def requirements(self): |
| 78 | + if self.options.with_hdf4: |
| 79 | + self.requires("hdf4/4.2.15") |
| 80 | + if self.options.with_hdf5: |
| 81 | + self.requires("hdf5/1.12.0") |
| 82 | + if self.options.dap: |
| 83 | + self.requires("libcurl/7.72.0") |
| 84 | + if self.options.parallel: |
| 85 | + self.requires("openmpi/4.0.3") |
| 86 | + |
| 87 | + def _validate_dependency_graph(self): |
| 88 | + if self.options.with_hdf5: |
| 89 | + if not (self.options["hdf5"].with_zlib and self.options["hdf5"].hl): |
| 90 | + raise ConanInvalidConfiguration("netcdf-c requires hdf5 with zlib and hl") |
| 91 | + if self.options.parallel and not self.options["hdf5"].parallel: |
| 92 | + raise ConanInvalidConfiguration("netcdf-c parallel requires hdf5 parallel") |
| 93 | + |
| 94 | + def source(self): |
| 95 | + tools.get(**self.conan_data["sources"][self.version]) |
| 96 | + os.rename(self.name + "-" + self.version, self._source_subfolder) |
| 97 | + |
| 98 | + def build(self): |
| 99 | + self._validate_dependency_graph() |
| 100 | + for patch in self.conan_data.get("patches", {}).get(self.version, []): |
| 101 | + tools.patch(**patch) |
| 102 | + cmake = self._configure_cmake() |
| 103 | + cmake.build() |
| 104 | + |
| 105 | + def _configure_cmake(self): |
| 106 | + if self._cmake: |
| 107 | + return self._cmake |
| 108 | + cmake = CMake(self) |
| 109 | + if self.settings.compiler == "Visual Studio": |
| 110 | + cmake.definitions["NC_USE_STATIC_CRT"] = str(self.settings.compiler.runtime).startswith("MT") |
| 111 | + cmake.definitions["ENABLE_V2_API"] = True |
| 112 | + cmake.definitions["BUILD_UTILITIES"] = self.options.build_utilities |
| 113 | + cmake.definitions["ENABLE_MMAP"] = True |
| 114 | + cmake.definitions["ENABLE_EXAMPLES"] = False |
| 115 | + cmake.definitions["ENABLE_HDF4"] = self.options.with_hdf4 |
| 116 | + if self.options.with_hdf4: |
| 117 | + cmake.definitions["ENABLE_HDF4_FILE_TESTS"] = False |
| 118 | + cmake.definitions["ENABLE_NETCDF_4"] = self.options.netcdf4 |
| 119 | + cmake.definitions["ENABLE_LOGGING"] = False |
| 120 | + cmake.definitions["ENABLE_SET_LOG_LEVEL_FUNC"] = True |
| 121 | + cmake.definitions["ENABLE_STRICT_NULL_BYTE_HEADER_PADDING"] = False |
| 122 | + cmake.definitions["ENABLE_RPC"] = False |
| 123 | + cmake.definitions["USE_HDF5"] = self.options.with_hdf5 |
| 124 | + if self.options.with_hdf5: |
| 125 | + cmake.definitions["NC_ENABLE_HDF_16_API"] = True |
| 126 | + cmake.definitions["ENABLE_DAP"] = self.options.dap |
| 127 | + cmake.definitions["ENABLE_BYTERANGE"] = False |
| 128 | + cmake.definitions["ENABLE_DAP_LONG_TESTS"] = False |
| 129 | + cmake.definitions["ENABLE_DAP_REMOTE_TESTS"] = False |
| 130 | + cmake.definitions["ENABLE_EXTRA_TESTS"] = False |
| 131 | + if self.settings.compiler == "Visual Studio": |
| 132 | + cmake.definitions["ENABLE_XGETOPT"] = True |
| 133 | + if self.settings.os != "Windows": |
| 134 | + cmake.definitions["ENABLE_STDIO"] = False |
| 135 | + cmake.definitions["ENABLE_FFIO"] = False |
| 136 | + cmake.definitions["ENABLE_TESTS"] = False |
| 137 | + cmake.definitions["ENABLE_EXTREME_NUMBERS"] = False |
| 138 | + cmake.definitions["ENABLE_METADATA_PERF_TESTS"] = False |
| 139 | + cmake.definitions["ENABLE_FSYNC"] = False # Option? |
| 140 | + cmake.definitions["ENABLE_JNA"] = False |
| 141 | + cmake.definitions["ENABLE_LARGE_FILE_SUPPORT"] = True # Option? |
| 142 | + cmake.definitions["ENABLE_EXAMPLE_TESTS"] = False |
| 143 | + cmake.definitions["ENABLE_PARALLEL4"] = self.options.parallel |
| 144 | + cmake.definitions["ENABLE_PNETCDF"] = False |
| 145 | + cmake.definitions["ENABLE_ERANGE_FILL"] = False |
| 146 | + cmake.definitions["ENABLE_PARALLEL_TESTS"] = False |
| 147 | + cmake.definitions["ENABLE_FILTER_TESTING"] = False |
| 148 | + cmake.definitions["ENABLE_CLIENTSIDE_FILTERS"] = False |
| 149 | + cmake.definitions["ENABLE_DOXYGEN"] = False |
| 150 | + cmake.definitions["ENABLE_DISKLESS"] = True |
| 151 | + if self.settings.compiler == "Visual Studio": |
| 152 | + cmake.definitions["NC_MSVC_STACK_SIZE"] = 40000000 # Option? |
| 153 | + cmake.definitions["ENABLE_CDF5"] = True |
| 154 | + cmake.definitions["ENABLE_BASH_SCRIPT_TESTING"] = False |
| 155 | + cmake.configure(build_folder=self._build_subfolder) |
| 156 | + self._cmake = cmake |
| 157 | + return self._cmake |
| 158 | + |
| 159 | + def package(self): |
| 160 | + self.copy("COPYRIGHT", dst="licenses", src=self._source_subfolder) |
| 161 | + cmake = self._configure_cmake() |
| 162 | + cmake.install() |
| 163 | + if self.options.build_utilities or (self.options.shared and self.settings.os == "Windows"): |
| 164 | + os.remove(os.path.join(self.package_folder, "bin", "nc-config")) |
| 165 | + else: |
| 166 | + tools.rmdir(os.path.join(self.package_folder, "bin")) |
| 167 | + tools.rmdir(os.path.join(self.package_folder, "lib", "cmake")) |
| 168 | + tools.rmdir(os.path.join(self.package_folder, "lib", "pkgconfig")) |
| 169 | + os.remove(os.path.join(self.package_folder, "lib", "libnetcdf.settings")) |
| 170 | + tools.rmdir(os.path.join(self.package_folder, "share")) |
| 171 | + if self.options.shared and self.settings.compiler == "Visual Studio": |
| 172 | + for dll_pattern_to_remove in ["concrt*.dll", "msvcp*.dll", "vcruntime*.dll"]: |
| 173 | + for dll_to_remove in glob.glob(os.path.join(self.package_folder, "bin", dll_pattern_to_remove)): |
| 174 | + os.remove(dll_to_remove) |
| 175 | + |
| 176 | + def package_info(self): |
| 177 | + self.cpp_info.names["cmake_find_package"] = "netCDF" |
| 178 | + self.cpp_info.names["cmake_find_package_multi"] = "netCDF" |
| 179 | + self.cpp_info.names["pkg_config"] = "netcdf" |
| 180 | + self.cpp_info.components["netcdf"].names["cmake_find_package"] = "netcdf" |
| 181 | + self.cpp_info.components["netcdf"].names["cmake_find_package_multi"] = "netcdf" |
| 182 | + self.cpp_info.components["netcdf"].names["pkg_config"] = "netcdf" |
| 183 | + self.cpp_info.components["netcdf"].libs = ["netcdf"] |
| 184 | + if self.options.with_hdf4: |
| 185 | + self.cpp_info.components["netcdf"].requires.append("hdf4::hdf4") |
| 186 | + if self.options.with_hdf5: |
| 187 | + self.cpp_info.components["netcdf"].requires.append("hdf5::hdf5") # TODO: when components available in hdf5 recipe => requires.extend([hdf5::hl, hdf5::c]) |
| 188 | + if self.options.dap: |
| 189 | + self.cpp_info.components["netcdf"].requires.append("libcurl::libcurl") |
| 190 | + if self.options.parallel: |
| 191 | + self.cpp_info.components["netcdf"].requires.append("openmpi::openmpi") |
| 192 | + if self.settings.os == "Linux": |
| 193 | + self.cpp_info.components["netcdf"].system_libs = ["m"] |
| 194 | + if self.settings.os == "Windows" and self.options.shared: |
| 195 | + self.cpp_info.components["netcdf"].defines.append("DLL_NETCDF") |
| 196 | + |
| 197 | + if self.options.build_utilities: |
| 198 | + bin_path = os.path.join(self.package_folder, "bin") |
| 199 | + self.output.info("Appending PATH environment variable: {}".format(bin_path)) |
| 200 | + self.env_info.PATH.append(bin_path) |
0 commit comments