Skip to content

Commit

Permalink
(#12081) bzip2: cross-compatibility conan v1 & v2
Browse files Browse the repository at this point in the history
* conan v2 support

* fix injection of bzip2 major version

* use conan.tools.scm.Version again

* no conan.tools.scm.Version to get major bzip2 version

wait conan-io/conan#11823
  • Loading branch information
SpaceIm authored Aug 16, 2022
1 parent 5c807f6 commit 58c6c59
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 59 deletions.
5 changes: 1 addition & 4 deletions recipes/bzip2/all/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@ project(bzip2 C)

include(GNUInstallDirs)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(KEEP_RPATHS)

if(MSVC OR MSVC90 OR MSVC10)
set(MSVC ON)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
endif()

set(SOURCE_SUBFOLDER ${CMAKE_CURRENT_SOURCE_DIR}/source_subfolder)
set(SOURCE_SUBFOLDER ${CMAKE_CURRENT_SOURCE_DIR}/src)
set(BZ2_LIBRARY bz2)

option(BZ2_BUILD_EXE ON)
Expand Down
1 change: 0 additions & 1 deletion recipes/bzip2/all/conandata.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@ sources:
patches:
"1.0.6":
- patch_file: "patches/0001-fix-sys-stat-include.patch"
base_path: "source_subfolder"
83 changes: 39 additions & 44 deletions recipes/bzip2/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
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, copy, get, save
import os
import textwrap

required_conan_version = ">=1.43.0"
required_conan_version = ">=1.46.0"


class Bzip2Conan(ConanFile):
Expand All @@ -25,17 +27,10 @@ class Bzip2Conan(ConanFile):
"build_executable": True,
}

generators = "cmake"
_cmake = None

@property
def _source_subfolder(self):
return "source_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"])
copy(self, "CMakeLists.txt", self.recipe_folder, self.export_sources_folder)
for p in self.conan_data.get("patches", {}).get(self.version, []):
copy(self, p["patch_file"], self.recipe_folder, self.export_sources_folder)

def config_options(self):
if self.settings.os == "Windows":
Expand All @@ -45,38 +40,44 @@ def config_options(self):
def configure(self):
if self.options.shared:
del self.options.fPIC
del self.settings.compiler.libcxx
del self.settings.compiler.cppstd
try:
del self.settings.compiler.libcxx
except Exception:
pass
try:
del self.settings.compiler.cppstd
except Exception:
pass

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)

def _configure_cmake(self):
if self._cmake:
return self._cmake
self._cmake = CMake(self)
self._cmake.definitions["BZ2_VERSION_STRING"] = self.version
self._cmake.definitions["BZ2_VERSION_MAJOR"] = tools.Version(self.version).major
self._cmake.definitions["BZ2_BUILD_EXE"] = self.options.build_executable
self._cmake.configure()
return self._cmake
get(self, **self.conan_data["sources"][self.version],
destination=self.source_folder, strip_root=True)

def generate(self):
tc = CMakeToolchain(self)
tc.variables["BZ2_VERSION_STRING"] = self.version
tc.variables["BZ2_VERSION_MAJOR"] = str(self.version).split(".")[0]
tc.variables["BZ2_BUILD_EXE"] = self.options.build_executable
tc.generate()

def build(self):
for patch in self.conan_data.get("patches", {}).get(self.version, []):
tools.patch(**patch)
cmake = self._configure_cmake()
apply_conandata_patches(self)
cmake = CMake(self)
cmake.configure(build_script_folder=os.path.join(self.source_folder, os.pardir))
cmake.build()

def package(self):
self.copy("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()
self._create_cmake_module_variables(
os.path.join(self.package_folder, self._module_file)
os.path.join(self.package_folder, self._module_file_rel_path)
)

@staticmethod
def _create_cmake_module_variables(module_file):
def _create_cmake_module_variables(self, module_file):
content = textwrap.dedent("""\
set(BZIP2_NEED_PREFIX TRUE)
if(NOT DEFINED BZIP2_FOUND AND DEFINED BZip2_FOUND)
Expand All @@ -95,28 +96,22 @@ def _create_cmake_module_variables(module_file):
set(BZIP2_VERSION_STRING ${BZip2_VERSION})
endif()
""")
tools.save(module_file, content)

@property
def _module_subfolder(self):
return os.path.join("lib", "cmake")
save(self, module_file, content)

@property
def _module_file(self):
return os.path.join(self._module_subfolder,
"conan-official-{}-variables.cmake".format(self.name))
def _module_file_rel_path(self):
return os.path.join("lib", "cmake", "conan-official-{}-variables.cmake".format(self.name))

def package_info(self):
self.cpp_info.set_property("cmake_find_mode", "both")
self.cpp_info.set_property("cmake_file_name", "BZip2")
self.cpp_info.set_property("cmake_target_name", "BZip2::BZip2")
self.cpp_info.builddirs.append(self._module_subfolder)
self.cpp_info.set_property("cmake_build_modules", [self._module_file])
self.cpp_info.set_property("cmake_build_modules", [self._module_file_rel_path])
self.cpp_info.libs = ["bz2"]

self.cpp_info.names["cmake_find_package"] = "BZip2"
self.cpp_info.names["cmake_find_package_multi"] = "BZip2"
self.cpp_info.build_modules["cmake_find_package"] = [self._module_file]
self.cpp_info.build_modules["cmake_find_package"] = [self._module_file_rel_path]

if self.options.build_executable:
bin_path = os.path.join(self.package_folder, "bin")
Expand Down
7 changes: 2 additions & 5 deletions recipes/bzip2/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
cmake_minimum_required(VERSION 3.1)
project(test_package C)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)
project(test_package LANGUAGES C)

find_package(BZip2 REQUIRED)
message("BZIP2_FOUND: ${BZIP2_FOUND}")
Expand All @@ -13,4 +10,4 @@ message("BZIP2_LIBRARIES: ${BZIP2_LIBRARIES}")
message("BZIP2_VERSION_STRING: ${BZIP2_VERSION_STRING}")

add_executable(test_package test_package.c)
target_link_libraries(test_package BZip2::BZip2)
target_link_libraries(test_package PRIVATE BZip2::BZip2)
18 changes: 13 additions & 5 deletions recipes/bzip2/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
from conans import ConanFile, CMake, tools
from conan import ConanFile
from conan.tools.build import cross_building
from conan.tools.cmake import CMake, cmake_layout
import os


class TestPackageConan(ConanFile):
settings = "os", "arch", "compiler", "build_type"
generators = "cmake", "cmake_find_package"
generators = "CMakeToolchain", "CMakeDeps", "VirtualRunEnv"

def requirements(self):
self.requires(self.tested_reference_str)

def layout(self):
cmake_layout(self)

def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()

def test(self):
if not tools.cross_building(self):
bin_path = os.path.join("bin", "test_package")
self.run("%s --help" % bin_path, run_environment=True)
if not cross_building(self):
bin_path = os.path.join(self.cpp.build.bindirs[0], "test_package")
self.run(bin_path, env="conanrun")
16 changes: 16 additions & 0 deletions recipes/bzip2/all/test_v1_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 3.1)
project(test_package LANGUAGES C)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)

find_package(BZip2 REQUIRED)
message("BZIP2_FOUND: ${BZIP2_FOUND}")
message("BZIP2_NEED_PREFIX: ${BZIP2_NEED_PREFIX}")
message("BZIP2_INCLUDE_DIRS: ${BZIP2_INCLUDE_DIRS}")
message("BZIP2_INCLUDE_DIR: ${BZIP2_INCLUDE_DIR}")
message("BZIP2_LIBRARIES: ${BZIP2_LIBRARIES}")
message("BZIP2_VERSION_STRING: ${BZIP2_VERSION_STRING}")

add_executable(test_package ../test_package/test_package.c)
target_link_libraries(test_package PRIVATE BZip2::BZip2)
18 changes: 18 additions & 0 deletions recipes/bzip2/all/test_v1_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# pylint: skip-file
from conans import ConanFile, CMake, tools
import os


class TestPackageConan(ConanFile):
settings = "os", "arch", "compiler", "build_type"
generators = "cmake", "cmake_find_package"

def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()

def test(self):
if not tools.cross_building(self):
bin_path = os.path.join("bin", "test_package")
self.run("%s --help" % bin_path, run_environment=True)

0 comments on commit 58c6c59

Please sign in to comment.