(#12849) [docs] CMake package template

* Adapt template package to V1 and V2

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* inform about each test package need

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* Fix test_v1_package path

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* add autotools example

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* add msbuild template

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* Move package to cmake_package

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* Pure C for autotools

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* Add more comments

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* use static tools

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* Remove unused imports for msbuild example

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* patch can be applied under build() for header-only

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* Add pre-built binaries example

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* Remove duplicated package_id from header-only

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* Apply SpaceIm suggestion

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* safe cppstd

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* Update cmake package example order

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* Update autotools package example order

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* Update msbuild package example order

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* Update prebuilt package example order

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* includedirs not used by pre-built

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* add test_type to all test_package folders

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* Update docs/package_templates/header_only/all/conanfile.py

Co-authored-by: Chris Mc <prince.chrismc@gmail.com>

* Declared layout

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* remove other templates

Signed-off-by: Uilian Ries <uilianries@gmail.com>

* Update docs/package_templates/cmake_package/all/conanfile.py

Co-authored-by: Javier G. Sogo <jgsogo@gmail.com>

Signed-off-by: Uilian Ries <uilianries@gmail.com>
Co-authored-by: Chris Mc <prince.chrismc@gmail.com>
Co-authored-by: Javier G. Sogo <jgsogo@gmail.com>
This commit is contained in:
Uilian Ries
2022-09-07 13:44:19 +02:00
committed by GitHub
parent 1ca7e5ddef
commit d348c438f1
8 changed files with 26 additions and 36 deletions

View File

@@ -0,0 +1,167 @@
from conan import ConanFile
from conan.errors import ConanInvalidConfiguration
from conan.tools.microsoft import is_msvc_static_runtime, is_msvc
from conan.tools.files import apply_conandata_patches, get, copy, rm, rmdir, replace_in_file
from conan.tools.build import check_min_cppstd
from conan.tools.scm import Version
from conan.tools.cmake import CMake, CMakeDeps, CMakeToolchain, cmake_layout
from conan.tools.env import VirtualBuildEnv
import os
required_conan_version = ">=1.51.3"
class PackageConan(ConanFile):
name = "package"
description = "short description"
license = "" # Use short name only, conform to SPDX License List: https://spdx.org/licenses/
url = "https://github.com/conan-io/conan-center-index"
homepage = "https://github.com/project/package"
topics = ("topic1", "topic2", "topic3") # no "conan" and project name in topics
settings = "os", "arch", "compiler", "build_type"
options = {
"shared": [True, False],
"fPIC": [True, False],
}
default_options = {
"shared": False,
"fPIC": True,
}
@property
def _minimum_cpp_standard(self):
return 17
# in case the project requires C++14/17/20/... the minimum compiler version should be listed
@property
def _compilers_minimum_version(self):
return {
"gcc": "7",
"Visual Studio": "15.7",
"clang": "7",
"apple-clang": "10",
}
# no exports_sources attribute, but export_sources(self) method instead
# this allows finer grain exportation of patches per version
def export_sources(self):
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":
del self.options.fPIC
def configure(self):
if self.options.shared:
try:
del self.options.fPIC # once removed by config_options, need try..except for a second del
except Exception:
pass
try:
del self.settings.compiler.libcxx # for plain C projects only
except Exception:
pass
try:
del self.settings.compiler.cppstd # for plain C projects only
except Exception:
pass
def layout(self):
cmake_layout(self, src_folder="src") # src_folder must use the same source folder name the project
def requirements(self):
self.requires("dependency/0.8.1") # prefer self.requires method instead of requires attribute
def validate(self):
# validate the minimum cpp standard supported. For C++ projects only
if self.info.settings.compiler.cppstd:
check_min_cppstd(self, self._minimum_cpp_standard)
minimum_version = self._compilers_minimum_version.get(str(self.info.settings.compiler), False)
if minimum_version and Version(self.info.settings.compiler.version) < minimum_version:
raise ConanInvalidConfiguration(f"{self.ref} requires C++{self._minimum_cpp_standard}, which your compiler does not support.")
# in case it does not work in another configuration, it should validated here too
if is_msvc(self) and self.info.options.shared:
raise ConanInvalidConfiguration(f"{self.ref} can not be built as shared on Visual Studio and msvc.")
# if another tool than the compiler or CMake is required to build the project (pkgconf, bison, flex etc)
def build_requirements(self):
self.tool_requires("tool/x.y.z")
def source(self):
get(self, **self.conan_data["sources"][self.version],
destination=self.source_folder, strip_root=True)
def generate(self):
# BUILD_SHARED_LIBS and POSITION_INDEPENDENT_CODE are automatically parsed when self.options.shared or self.options.fPIC exist
tc = CMakeToolchain(self)
# Boolean values are preferred instead of "ON"/"OFF"
tc.cache_variables["PACKAGE_CUSTOM_DEFINITION"] = True
if is_msvc(self):
# don't use self.settings.compiler.runtime
tc.cache_variables.definitions["USE_MSVC_RUNTIME_LIBRARY_DLL"] = not is_msvc_static_runtime(self)
# deps_cpp_info, deps_env_info and deps_user_info are no longer used
if self.dependencies["dependency"].options.foobar:
tc.cache_variables["DEPENDENCY_LIBPATH"] = self.dependencies["dependency"].cpp_info.libdirs
tc.generate()
# In case there are dependencies listed on requirements, CMakeDeps should be used
tc = CMakeDeps(self)
tc.generate()
# In case there are dependencies listed on build_requirements, VirtualBuildEnv should be used
tc = VirtualBuildEnv(self)
tc.generate(scope="build")
def _patch_sources(self):
apply_conandata_patches(self)
# remove bundled xxhash
rm(self, "whateer.*", os.path.join(self.source_folder, "lib"))
replace_in_file(
self,
os.path.join(self.source_folder, "CMakeLists.txt"),
"...",
"",
)
def build(self):
self._patch_sources() # It can be apply_conandata_patches(self) only in case no more patches are needed
cmake = CMake(self)
cmake.build()
def package(self):
copy(self, pattern="LICENSE", dst=os.path.join(self.package_folder, "licenses"), src=self.source_folder)
cmake = CMake(self)
cmake.install()
# some files extensions and folders are not allowed. Please, read the FAQs to get informed.
rmdir(self, 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, "share"))
rm(self, "*.pdb", self, os.path.join(self.package_folder, "lib"))
rm(self, "*.la", self, os.path.join(self.package_folder, "lib"))
rm(self, "*.pdb", os.path.join(self.package_folder, "bin"))
def package_info(self):
self.cpp_info.libs = ["package_lib"]
# if package has an official FindPACKAGE.cmake listed in https://cmake.org/cmake/help/latest/manual/cmake-modules.7.html#find-modules
# examples: bzip2, freetype, gdal, icu, libcurl, libjpeg, libpng, libtiff, openssl, sqlite3, zlib...
self.cpp_info.set_property("cmake_module_file_name", "PACKAGE")
self.cpp_info.set_property("cmake_module_target_name", "PACKAGE::PACKAGE")
# if package provides a CMake config file (package-config.cmake or packageConfig.cmake, with package::package target, usually installed in <prefix>/lib/cmake/<package>/)
self.cpp_info.set_property("cmake_file_name", "package")
self.cpp_info.set_property("cmake_target_name", "package::package")
# if package provides a pkgconfig file (package.pc, usually installed in <prefix>/lib/pkgconfig/)
self.cpp_info.set_property("pkg_config_name", "package")
# If they are needed on Linux, m, pthread and dl are usually needed on FreeBSD too
if self.settings.os in ["Linux", "FreeBSD"]:
self.cpp_info.system_libs.append("m")
self.cpp_info.system_libs.append("pthread")
self.cpp_info.system_libs.append("dl")
# TODO: to remove in conan v2 once cmake_find_package_* generators removed
self.cpp_info.filenames["cmake_find_package"] = "PACKAGE"
self.cpp_info.filenames["cmake_find_package_multi"] = "package"
self.cpp_info.names["cmake_find_package"] = "PACKAGE"
self.cpp_info.names["cmake_find_package_multi"] = "package"