|
| 1 | +from conan import ConanFile |
| 2 | +from conan.tools.cmake import CMake, CMakeToolchain, cmake_layout |
| 3 | +from conan.tools.files import apply_conandata_patches, export_conandata_patches, get, load, replace_in_file, save |
| 4 | +from conan.tools.scm import Version |
| 5 | +import os |
| 6 | + |
| 7 | +required_conan_version = ">=1.53.0" |
| 8 | + |
| 9 | + |
| 10 | +class ZlibConan(ConanFile): |
| 11 | + name = "zlib" |
| 12 | + package_type = "library" |
| 13 | + url = "https://github.com/conan-io/conan-center-index" |
| 14 | + homepage = "https://zlib.net" |
| 15 | + license = "Zlib" |
| 16 | + description = ("A Massively Spiffy Yet Delicately Unobtrusive Compression Library " |
| 17 | + "(Also Free, Not to Mention Unencumbered by Patents)") |
| 18 | + topics = ("zlib", "compression") |
| 19 | + |
| 20 | + settings = "os", "arch", "compiler", "build_type" |
| 21 | + options = { |
| 22 | + "shared": [True, False], |
| 23 | + "fPIC": [True, False], |
| 24 | + } |
| 25 | + default_options = { |
| 26 | + "shared": False, |
| 27 | + "fPIC": True, |
| 28 | + } |
| 29 | + |
| 30 | + def export_sources(self): |
| 31 | + export_conandata_patches(self) |
| 32 | + |
| 33 | + def config_options(self): |
| 34 | + if self.settings.os == "Windows": |
| 35 | + del self.options.fPIC |
| 36 | + |
| 37 | + def configure(self): |
| 38 | + if self.options.shared: |
| 39 | + self.options.rm_safe("fPIC") |
| 40 | + self.settings.rm_safe("compiler.libcxx") |
| 41 | + self.settings.rm_safe("compiler.cppstd") |
| 42 | + |
| 43 | + def layout(self): |
| 44 | + cmake_layout(self, src_folder="src") |
| 45 | + |
| 46 | + def source(self): |
| 47 | + get(self, **self.conan_data["sources"][self.version], |
| 48 | + destination=self.source_folder, strip_root=True) |
| 49 | + |
| 50 | + def generate(self): |
| 51 | + tc = CMakeToolchain(self) |
| 52 | + tc.variables["SKIP_INSTALL_ALL"] = False |
| 53 | + tc.variables["SKIP_INSTALL_LIBRARIES"] = False |
| 54 | + tc.variables["SKIP_INSTALL_HEADERS"] = False |
| 55 | + tc.variables["SKIP_INSTALL_FILES"] = True |
| 56 | + # Correct for misuse of "${CMAKE_INSTALL_PREFIX}/" in CMakeLists.txt |
| 57 | + tc.variables["INSTALL_LIB_DIR"] = "lib" |
| 58 | + tc.variables["INSTALL_INC_DIR"] = "include" |
| 59 | + tc.variables["ZLIB_BUILD_EXAMPLES"] = False |
| 60 | + tc.generate() |
| 61 | + |
| 62 | + def _patch_sources(self): |
| 63 | + apply_conandata_patches(self) |
| 64 | + |
| 65 | + is_apple_clang12 = self.settings.compiler == "apple-clang" and Version(self.settings.compiler.version) >= "12.0" |
| 66 | + if not is_apple_clang12: |
| 67 | + for filename in ['zconf.h', 'zconf.h.cmakein', 'zconf.h.in']: |
| 68 | + filepath = os.path.join(self.source_folder, filename) |
| 69 | + replace_in_file(self, filepath, |
| 70 | + '#ifdef HAVE_UNISTD_H ' |
| 71 | + '/* may be set to #if 1 by ./configure */', |
| 72 | + '#if defined(HAVE_UNISTD_H) && (1-HAVE_UNISTD_H-1 != 0)') |
| 73 | + replace_in_file(self, filepath, |
| 74 | + '#ifdef HAVE_STDARG_H ' |
| 75 | + '/* may be set to #if 1 by ./configure */', |
| 76 | + '#if defined(HAVE_STDARG_H) && (1-HAVE_STDARG_H-1 != 0)') |
| 77 | + |
| 78 | + def build(self): |
| 79 | + self._patch_sources() |
| 80 | + cmake = CMake(self) |
| 81 | + cmake.configure() |
| 82 | + cmake.build() |
| 83 | + |
| 84 | + def _extract_license(self): |
| 85 | + tmp = load(self, os.path.join(self.source_folder, "zlib.h")) |
| 86 | + license_contents = tmp[2:tmp.find("*/", 1)] |
| 87 | + return license_contents |
| 88 | + |
| 89 | + def package(self): |
| 90 | + save(self, os.path.join(self.package_folder, "licenses", "LICENSE"), self._extract_license()) |
| 91 | + cmake = CMake(self) |
| 92 | + cmake.install() |
| 93 | + |
| 94 | + def package_info(self): |
| 95 | + self.cpp_info.set_property("cmake_find_mode", "both") |
| 96 | + self.cpp_info.set_property("cmake_file_name", "ZLIB") |
| 97 | + self.cpp_info.set_property("cmake_target_name", "ZLIB::ZLIB") |
| 98 | + self.cpp_info.set_property("pkg_config_name", "zlib") |
| 99 | + |
| 100 | + if self.settings.os == "Windows": |
| 101 | + # The recipe patches the CMakeLists.txt to generate different filenames when CMake |
| 102 | + # detects MINGW (clang, gcc with compiler.runtime undefined and compiler.libcxx defined) |
| 103 | + libname = "zdll" if self.options.shared else "zlib" |
| 104 | + else: |
| 105 | + libname = "z" |
| 106 | + self.cpp_info.libs = [libname] |
| 107 | + |
| 108 | + self.cpp_info.names["cmake_find_package"] = "ZLIB" |
| 109 | + self.cpp_info.names["cmake_find_package_multi"] = "ZLIB" |
0 commit comments