Skip to content

Instantly share code, notes, and snippets.

@theirix
Created June 21, 2025 19:50
Show Gist options
  • Select an option

  • Save theirix/744cd76273fd0058bc14ca6d1279f01e to your computer and use it in GitHub Desktop.

Select an option

Save theirix/744cd76273fd0058bc14ca6d1279f01e to your computer and use it in GitHub Desktop.
Conan1 vs Conan2 for zts recipe
diff --git a/recipes/zstd/all/conanfile.py b/recipes/zstd/all/conanfile.py
index eb822dc49..d85b80636 100644
--- a/recipes/zstd/all/conanfile.py
+++ b/recipes/zstd/all/conanfile.py
@@ -1,44 +1,37 @@
-from conans import ConanFile, CMake, tools
+from conan import ConanFile
+from conan.tools.cmake import CMake, CMakeToolchain, cmake_layout
+from conan.tools.files import apply_conandata_patches, collect_libs, copy, export_conandata_patches, get, replace_in_file, rmdir, rm
+from conan.tools.scm import Version
+import glob
import os
-required_conan_version = ">=1.43.0"
-
+required_conan_version = ">=2.1"
class ZstdConan(ConanFile):
name = "zstd"
url = "https://github.com/conan-io/conan-center-index"
homepage = "https://github.com/facebook/zstd"
description = "Zstandard - Fast real-time compression algorithm"
- topics = ("zstd", "compression", "algorithm", "decoder")
+ topics = ("zstandard", "compression", "algorithm", "decoder")
license = "BSD-3-Clause"
+ package_type = "library"
settings = "os", "arch", "compiler", "build_type"
options = {
"shared": [True, False],
"fPIC": [True, False],
"threading": [True, False],
+ "build_programs": [True, False],
}
default_options = {
"shared": False,
"fPIC": True,
"threading": True,
+ "build_programs": True,
}
- generators = "cmake"
- _cmake = None
-
- @property
- def _source_subfolder(self):
- return "source_subfolder"
-
- @property
- def _build_subfolder(self):
- return "build_subfolder"
-
def export_sources(self):
- self.copy("CMakeLists.txt")
- for patch in self.conan_data.get("patches", {}).get(self.version, []):
- self.copy(patch["patch_file"])
+ export_conandata_patches(self)
def config_options(self):
if self.settings.os == "Windows":
@@ -46,56 +39,63 @@ class ZstdConan(ConanFile):
def configure(self):
if self.options.shared:
- del self.options.fPIC
- del self.settings.compiler.libcxx
- del self.settings.compiler.cppstd
+ self.options.rm_safe("fPIC")
+ self.settings.rm_safe("compiler.cppstd")
+ self.settings.rm_safe("compiler.libcxx")
+
+ def layout(self):
+ cmake_layout(self, src_folder="src")
def source(self):
- tools.get(**self.conan_data["sources"][self.version], destination=self._source_subfolder, strip_root=True)
+ get(self, **self.conan_data["sources"][self.version], strip_root=True)
- def _configure_cmake(self):
- if self._cmake:
- return self._cmake
- self._cmake = CMake(self)
- self._cmake.definitions["ZSTD_BUILD_PROGRAMS"] = False
- self._cmake.definitions["ZSTD_BUILD_STATIC"] = not self.options.shared
- self._cmake.definitions["ZSTD_BUILD_SHARED"] = self.options.shared
- self._cmake.definitions["ZSTD_MULTITHREAD_SUPPORT"] = self.options.threading
- if tools.Version(self.version) < "1.4.3":
- # Generate a relocatable shared lib on Macos
- self._cmake.definitions["CMAKE_POLICY_DEFAULT_CMP0042"] = "NEW"
- self._cmake.configure(build_folder=self._build_subfolder)
- return self._cmake
+ def generate(self):
+ tc = CMakeToolchain(self)
+ tc.variables["ZSTD_BUILD_PROGRAMS"] = self.options.build_programs
+ tc.variables["ZSTD_BUILD_STATIC"] = not self.options.shared or self.options.build_programs
+ tc.variables["ZSTD_BUILD_SHARED"] = self.options.shared
+ tc.variables["ZSTD_MULTITHREAD_SUPPORT"] = self.options.threading
+ if Version(self.version) < "1.5.6":
+ tc.cache_variables["CMAKE_POLICY_VERSION_MINIMUM"] = "3.5" # CMake 4 support
+ tc.generate()
def _patch_sources(self):
- for patch in self.conan_data.get("patches", {}).get(self.version, []):
- tools.patch(**patch)
+ apply_conandata_patches(self)
# Don't force PIC
- if tools.Version(self.version) >= "1.4.5":
- tools.replace_in_file(os.path.join(self._source_subfolder, "build", "cmake", "lib", "CMakeLists.txt"),
- "POSITION_INDEPENDENT_CODE On", "")
+ replace_in_file(self, os.path.join(self.source_folder, "build", "cmake", "lib", "CMakeLists.txt"),
+ "POSITION_INDEPENDENT_CODE On", "")
def build(self):
self._patch_sources()
- cmake = self._configure_cmake()
+ cmake = CMake(self)
+ cmake.configure(build_script_folder=os.path.join(self.source_folder, "build", "cmake"))
cmake.build()
def package(self):
- self.copy(pattern="LICENSE", dst="licenses", src=self._source_subfolder)
- cmake = self._configure_cmake()
+ copy(self, "LICENSE", src=self.source_folder, dst=os.path.join(self.package_folder, "licenses"))
+ cmake = CMake(self)
cmake.install()
- tools.rmdir(os.path.join(self.package_folder, "lib", "cmake"))
- tools.rmdir(os.path.join(self.package_folder, "lib", "pkgconfig"))
+ rmdir(self, os.path.join(self.package_folder, "lib", "cmake"))
+ rmdir(self, os.path.join(self.package_folder, "lib", "pkgconfig"))
+ rmdir(self, os.path.join(self.package_folder, "share"))
+
+ if self.options.shared and self.options.build_programs:
+ # If we build programs we have to build static libs (see logic in generate()),
+ # but if shared is True, we only want shared lib in package folder.
+ rm(self, "*_static.*", os.path.join(self.package_folder, "lib"))
+ for lib in glob.glob(os.path.join(self.package_folder, "lib", "*.a")):
+ if not lib.endswith(".dll.a"):
+ os.remove(lib)
def package_info(self):
zstd_cmake = "libzstd_shared" if self.options.shared else "libzstd_static"
self.cpp_info.set_property("cmake_file_name", "zstd")
- self.cpp_info.set_property("cmake_target_name", "zstd::{}".format(zstd_cmake))
+ self.cpp_info.set_property("cmake_target_name", f"zstd::{zstd_cmake}")
self.cpp_info.set_property("pkg_config_name", "libzstd")
- self.cpp_info.components["zstdlib"].set_property("pkg_config_name", "libzstd")
- self.cpp_info.components["zstdlib"].names["cmake_find_package"] = zstd_cmake
- self.cpp_info.components["zstdlib"].names["cmake_find_package_multi"] = zstd_cmake
- self.cpp_info.components["zstdlib"].set_property("cmake_target_name", "zstd::{}".format(zstd_cmake))
- self.cpp_info.components["zstdlib"].libs = tools.collect_libs(self)
+ self.cpp_info.components["zstdlib"].libs = collect_libs(self)
if self.settings.os in ["Linux", "FreeBSD"]:
self.cpp_info.components["zstdlib"].system_libs.append("pthread")
+
+ # TODO: Remove after dropping Conan 1.x from ConanCenterIndex
+ self.cpp_info.components["zstdlib"].set_property("cmake_target_name", f"zstd::{zstd_cmake}")
+ self.cpp_info.components["zstdlib"].set_property("pkg_config_name", "libzstd")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment