Files
conan-center-index/docs/package_templates/autotools_package/all/conanfile.py
Jordan Williams 1ccd56d73b (#20856) Minor clean up of template packages
* Bump Meson version

* Use LANGUAGES keyword in CMake project commands

* Alphabetize self.settings.rm_safe in configure

* Remove keywords for pattern, src, and dst from calls to copy

This keeps things consistent and simple.

* Use _compilers_minimum_version for MSVC in CMake package

This makes it consistent with other template packages.

* Alphabetize compilers in _compilers_minimum_version

* Use _min_cppstd in Autoools package for consistency

* Add _compilers_minimum_version check to Autotools package

* Alphabetize imports

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

Co-authored-by: Uilian Ries <uilianries@gmail.com>

* Make min cppstd consistent with compiler minimum version

---------

Co-authored-by: Uilian Ries <uilianries@gmail.com>
2023-11-06 19:04:11 +01:00

200 lines
8.5 KiB
Python

from conan import ConanFile
from conan.errors import ConanInvalidConfiguration
from conan.tools.apple import fix_apple_shared_install_name
from conan.tools.build import check_min_cppstd, cross_building
from conan.tools.env import Environment, VirtualBuildEnv, VirtualRunEnv
from conan.tools.files import apply_conandata_patches, copy, export_conandata_patches, get, rm, rmdir
from conan.tools.gnu import Autotools, AutotoolsDeps, AutotoolsToolchain, PkgConfigDeps
from conan.tools.layout import basic_layout
from conan.tools.microsoft import is_msvc, unix_path
from conan.tools.scm import Version
import os
required_conan_version = ">=1.54.0"
#
# INFO: Please, remove all comments before pushing your PR!
#
class PackageConan(ConanFile):
name = "package"
description = "short description"
# Use short name only, conform to SPDX License List: https://spdx.org/licenses/
# In case not listed there, use "LicenseRef-<license-file-name>"
license = ""
url = "https://github.com/conan-io/conan-center-index"
homepage = "https://github.com/project/package"
# no "conan" and project name in topics. Use topics from the upstream listed on GH
topics = ("topic1", "topic2", "topic3")
# package_type should usually be "library" (if there is shared option)
package_type = "library"
settings = "os", "arch", "compiler", "build_type"
options = {
"shared": [True, False],
"fPIC": [True, False],
"with_foobar": [True, False],
}
default_options = {
"shared": False,
"fPIC": True,
"with_foobar": True,
}
@property
def _min_cppstd(self):
return 14
# in case the project requires C++14/17/20/... the minimum compiler version should be listed
@property
def _compilers_minimum_version(self):
return {
"apple-clang": "10",
"clang": "7",
"gcc": "7",
"msvc": "191",
"Visual Studio": "15",
}
@property
def _settings_build(self):
return getattr(self, "settings_build", self.settings)
# no exports_sources attribute, but export_sources(self) method instead
# this allows finer grain exportation of patches per version
def export_sources(self):
export_conandata_patches(self)
def config_options(self):
if self.settings.os == "Windows":
del self.options.fPIC
def configure(self):
if self.options.shared:
self.options.rm_safe("fPIC")
# for plain C projects only
self.settings.rm_safe("compiler.cppstd")
self.settings.rm_safe("compiler.libcxx")
def layout(self):
# src_folder must use the same source folder name the project
basic_layout(self, src_folder="src")
def requirements(self):
# prefer self.requires method instead of requires attribute
self.requires("dependency/0.8.1")
if self.options.with_foobar:
self.requires("foobar/0.1.0")
def validate(self):
# validate the minimum cpp standard supported. Only for C++ projects
if self.settings.compiler.get_safe("cppstd"):
check_min_cppstd(self, self._min_cppstd)
minimum_version = self._compilers_minimum_version.get(str(self.settings.compiler), False)
if minimum_version and Version(self.settings.compiler.version) < minimum_version:
raise ConanInvalidConfiguration(
f"{self.ref} requires C++{self._min_cppstd}, which your compiler does not support."
)
if self.settings.os not in ["Linux", "FreeBSD", "Macos"]:
raise ConanInvalidConfiguration(f"{self.ref} is not supported on {self.settings.os}.")
# if another tool than the compiler or autotools is required to build the project (pkgconf, bison, flex etc)
def build_requirements(self):
# only if we have to call autoreconf
self.tool_requires("libtool/x.y.z")
# only if upstream configure.ac relies on PKG_CHECK_MODULES macro
if not self.conf.get("tools.gnu:pkg_config", check_type=str):
self.tool_requires("pkgconf/x.y.z")
# required to suppport windows as a build machine
if self._settings_build.os == "Windows":
self.win_bash = True
if not self.conf.get("tools.microsoft.bash:path", check_type=str):
self.tool_requires("msys2/cci.latest")
# for msvc support to get compile & ar-lib scripts (may be avoided if shipped in source code of the library)
# not needed if libtool already in build requirements
if is_msvc(self):
self.tool_requires("automake/x.y.z")
def source(self):
get(self, **self.conan_data["sources"][self.version], strip_root=True)
def generate(self):
# inject tool_requires env vars in build scope (not needed if there is no tool_requires)
env = VirtualBuildEnv(self)
env.generate()
# inject requires env vars in build scope
# it's required in case of native build when there is AutotoolsDeps & at least one dependency which might be shared, because configure tries to run a test executable
if not cross_building(self):
env = VirtualRunEnv(self)
env.generate(scope="build")
# --fpic is automatically managed when 'fPIC'option is declared
# --enable/disable-shared is automatically managed when 'shared' option is declared
tc = AutotoolsToolchain(self)
# autotools usually uses 'yes' and 'no' to enable/disable options
yes_no = lambda v: "yes" if v else "no"
tc.configure_args.extend([
f"--with-foobar={yes_no(self.options.with_foobar)}",
"--enable-tools=no",
"--enable-manpages=no",
])
tc.generate()
# generate pkg-config files of dependencies (useless if upstream configure.ac doesn't rely on PKG_CHECK_MODULES macro)
tc = PkgConfigDeps(self)
tc.generate()
# generate dependencies for autotools
tc = AutotoolsDeps(self)
tc.generate()
# If Visual Studio is supported
if is_msvc(self):
env = Environment()
# get compile & ar-lib from automake (or eventually lib source code if available)
# it's not always required to wrap CC, CXX & AR with these scripts, it depends on how much love was put in
# upstream build files
automake_conf = self.dependencies.build["automake"].conf_info
compile_wrapper = unix_path(self, automake_conf.get("user.automake:compile-wrapper", check_type=str))
ar_wrapper = unix_path(self, automake_conf.get("user.automake:lib-wrapper", check_type=str))
env.define("CC", f"{compile_wrapper} cl -nologo")
env.define("CXX", f"{compile_wrapper} cl -nologo")
env.define("LD", "link -nologo")
env.define("AR", f"{ar_wrapper} \"lib -nologo\"")
env.define("NM", "dumpbin -symbols")
env.define("OBJDUMP", ":")
env.define("RANLIB", ":")
env.define("STRIP", ":")
env.vars(self).save_script("conanbuild_msvc")
def build(self):
# apply patches listed in conandata.yml
apply_conandata_patches(self)
autotools = Autotools(self)
# (optional) run autoreconf to regenerate configure file (libtool should be in tool_requires)
autotools.autoreconf()
# ./configure + toolchain file
autotools.configure()
autotools.make()
def package(self):
copy(self, "LICENSE", self.source_folder, os.path.join(self.package_folder, "licenses"))
autotools = Autotools(self)
autotools.install()
# some files extensions and folders are not allowed. Please, read the FAQs to get informed.
rm(self, "*.la", os.path.join(self.package_folder, "lib"))
rmdir(self, os.path.join(self.package_folder, "lib", "pkgconfig"))
rmdir(self, os.path.join(self.package_folder, "share"))
# In shared lib/executable files, autotools set install_name (macOS) to lib dir absolute path instead of @rpath, it's not relocatable, so fix it
fix_apple_shared_install_name(self)
def package_info(self):
self.cpp_info.libs = ["package_lib"]
# 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.extend(["dl", "m", "pthread"])