Skip to content

Commit 41f9b6f

Browse files
uilianriesMartin Valgurdanimtb
authored
[docs] Recipe templates with Conan 2.x support only (#25849)
* [docs] package_templates: demo non-list conandata URLs as well * [docs] autotools: do not use `-nologo` for AR with MSVC It keeps the logs slightly cleaner for a very infrequent command but has caused issues with "ar interface detection" in ./configure for several recipes. * [docs] autotools: provide tool_requires() versions These change very infrequently (except for pkgconf, somewhat). I think these can be included for convenience. * [docs] autotools: mention a common workaround for MSVC * [docs] cmake: improve recipe example * [docs] package_templates: update the conventional LicenseRef format * [docs] package_templates: improve package_type comment * [docs] package_templates: various minor improvements * [docs] package_templates: remove outdated test_package comment * [docs] meson: bump versions in template * [docs] package_templates: add a version range example * [docs] package_templates: improve transitive_headers=True examples * [docs] autotools: improve AutotoolsDeps workaround comment * [docs] autotools: revert to x.y.z tool versions * [docs] package_templates: remove outdated CMakeLists.txt comment * [docs] package_templates: add layout to prebuilt_tool_package test_package The test_package directory gets gunked up with generated VirtualBuildEnv files otherwise. * [docs] package_templates: fix some typos * Move CMake and and PkgConfig properties to the top, drop legacy generator props The CMake and PkgConfig info is the most relevant part to any consumers and should come first, imo. New recipes should not bother with the legacy generators, especially after test_v1_package is no longer included. * Simplify Cmake template Signed-off-by: Uilian Ries <[email protected]> * Simplify header-only template Signed-off-by: Uilian Ries <[email protected]> * Simplify header-only template Signed-off-by: Uilian Ries <[email protected]> * Move patches to source Signed-off-by: Uilian Ries <[email protected]> * Simplify autotools template Signed-off-by: Uilian Ries <[email protected]> * Simplify meson template Signed-off-by: Uilian Ries <[email protected]> * Simplify msbuild template Signed-off-by: Uilian Ries <[email protected]> * Simplify prebuilt template Signed-off-by: Uilian Ries <[email protected]> * Grammar typo Signed-off-by: Uilian Ries <[email protected]> * Use cxx language by default on test package Signed-off-by: Uilian Ries <[email protected]> * Remove VirtuanEnvs from test package Signed-off-by: Uilian Ries <[email protected]> * Update test package comment Signed-off-by: Uilian Ries <[email protected]> * Remove VirtuanEnvs - They are automatic Signed-off-by: Uilian Ries <[email protected]> * Comment update Co-authored-by: Daniel <[email protected]> * Comment update Co-authored-by: Daniel <[email protected]> * Comment update Co-authored-by: Daniel <[email protected]> * Comment update Co-authored-by: Daniel <[email protected]> * Comment how to install --------- Signed-off-by: Uilian Ries <[email protected]> Co-authored-by: Martin Valgur <[email protected]> Co-authored-by: Daniel <[email protected]>
1 parent 4ced948 commit 41f9b6f

File tree

33 files changed

+224
-399
lines changed

33 files changed

+224
-399
lines changed

docs/package_templates/autotools_package/all/conandata.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
sources:
2-
# Newer versions at the top
32
"1.2.0":
43
url:
54
- "https://mirror1.net/package-1.2.0.tar.gz"
65
- "https://mirror2.net/package-1.2.0.tar.gz"
76
sha256: "________________________________________________________________"
87
"1.1.0":
9-
url:
10-
- "https://mirror1.net/package-1.1.0.tar.gz"
11-
- "https://mirror2.net/package-1.1.0.tar.gz"
8+
url: "https://mirror2.net/package-1.1.0.tar.gz"
129
sha256: "________________________________________________________________"
1310
patches:
14-
# Newer versions at the top
1511
"1.1.0":
1612
- patch_file: "patches/0001-fix-cmake.patch"
1713
patch_description: "correct the order of cmake min and project"

docs/package_templates/autotools_package/all/conanfile.py

Lines changed: 31 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from conan.errors import ConanInvalidConfiguration
33
from conan.tools.apple import fix_apple_shared_install_name
44
from conan.tools.build import check_min_cppstd, cross_building
5-
from conan.tools.env import Environment, VirtualBuildEnv, VirtualRunEnv
5+
from conan.tools.env import Environment, VirtualRunEnv
66
from conan.tools.files import apply_conandata_patches, copy, export_conandata_patches, get, rm, rmdir
77
from conan.tools.gnu import Autotools, AutotoolsDeps, AutotoolsToolchain, PkgConfigDeps
88
from conan.tools.layout import basic_layout
@@ -11,7 +11,7 @@
1111
import os
1212

1313

14-
required_conan_version = ">=1.54.0"
14+
required_conan_version = ">=2.0.9"
1515

1616
#
1717
# INFO: Please, remove all comments before pushing your PR!
@@ -22,13 +22,13 @@ class PackageConan(ConanFile):
2222
name = "package"
2323
description = "short description"
2424
# Use short name only, conform to SPDX License List: https://spdx.org/licenses/
25-
# In case not listed there, use "LicenseRef-<license-file-name>"
25+
# In case not listed there, use "DocumentRef-<license-file-name>:LicenseRef-<package-name>"
2626
license = ""
2727
url = "https://github.com/conan-io/conan-center-index"
2828
homepage = "https://github.com/project/package"
2929
# no "conan" and project name in topics. Use topics from the upstream listed on GH
3030
topics = ("topic1", "topic2", "topic3")
31-
# package_type should usually be "library" (if there is shared option)
31+
# package_type should usually be "library", "shared-library" or "static-library"
3232
package_type = "library"
3333
settings = "os", "arch", "compiler", "build_type"
3434
options = {
@@ -41,65 +41,44 @@ class PackageConan(ConanFile):
4141
"fPIC": True,
4242
"with_foobar": True,
4343
}
44-
45-
@property
46-
def _min_cppstd(self):
47-
return 14
48-
49-
# in case the project requires C++14/17/20/... the minimum compiler version should be listed
50-
@property
51-
def _compilers_minimum_version(self):
52-
return {
53-
"apple-clang": "10",
54-
"clang": "7",
55-
"gcc": "7",
56-
"msvc": "191",
57-
"Visual Studio": "15",
58-
}
44+
implements = ["auto_shared_fpic"]
5945

6046
@property
6147
def _settings_build(self):
6248
return getattr(self, "settings_build", self.settings)
6349

6450
# no exports_sources attribute, but export_sources(self) method instead
65-
# this allows finer grain exportation of patches per version
6651
def export_sources(self):
6752
export_conandata_patches(self)
6853

69-
def config_options(self):
70-
if self.settings.os == "Windows":
71-
del self.options.fPIC
72-
7354
def configure(self):
74-
if self.options.shared:
75-
self.options.rm_safe("fPIC")
76-
# for plain C projects only
55+
# for plain C projects only. Otherwise, remove this method.
7756
self.settings.rm_safe("compiler.cppstd")
7857
self.settings.rm_safe("compiler.libcxx")
7958

8059
def layout(self):
81-
# src_folder must use the same source folder name the project
8260
basic_layout(self, src_folder="src")
8361

8462
def requirements(self):
85-
# prefer self.requires method instead of requires attribute
63+
# Prefer self.requirements() method instead of self.requires attribute.
8664
self.requires("dependency/0.8.1")
8765
if self.options.with_foobar:
66+
# INFO: used in foo/baz.hpp:34
8867
self.requires("foobar/0.1.0")
68+
# Some dependencies on CCI are allowed to use version ranges.
69+
# See https://github.com/conan-io/conan-center-index/blob/master/docs/adding_packages/dependencies.md#version-ranges
70+
self.requires("openssl/[>=1.1 <4]")
8971

9072
def validate(self):
9173
# validate the minimum cpp standard supported. Only for C++ projects
92-
if self.settings.compiler.get_safe("cppstd"):
93-
check_min_cppstd(self, self._min_cppstd)
94-
minimum_version = self._compilers_minimum_version.get(str(self.settings.compiler), False)
95-
if minimum_version and Version(self.settings.compiler.version) < minimum_version:
96-
raise ConanInvalidConfiguration(
97-
f"{self.ref} requires C++{self._min_cppstd}, which your compiler does not support."
98-
)
74+
check_min_cppstd(self, 14)
75+
76+
# Always comment the reason including the upstream issue.
77+
# INFO: Upstream only support Unix systems. See <URL>
9978
if self.settings.os not in ["Linux", "FreeBSD", "Macos"]:
10079
raise ConanInvalidConfiguration(f"{self.ref} is not supported on {self.settings.os}.")
10180

102-
# if another tool than the compiler or autotools is required to build the project (pkgconf, bison, flex etc)
81+
# if a tool other than the compiler or autotools is required to build the project (pkgconf, bison, flex etc)
10382
def build_requirements(self):
10483
# only if we have to call autoreconf
10584
self.tool_requires("libtool/x.y.z")
@@ -118,17 +97,16 @@ def build_requirements(self):
11897

11998
def source(self):
12099
get(self, **self.conan_data["sources"][self.version], strip_root=True)
100+
# apply patches listed in conandata.yml
101+
# Using patches is always the last resort to fix issues. If possible, try to fix the issue in the upstream project.
102+
apply_conandata_patches(self)
121103

122104
def generate(self):
123-
# inject tool_requires env vars in build scope (not needed if there is no tool_requires)
124-
env = VirtualBuildEnv(self)
125-
env.generate()
126-
# inject requires env vars in build scope
105+
# inject required env vars into the build scope
127106
# 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
128107
if not cross_building(self):
129-
env = VirtualRunEnv(self)
130-
env.generate(scope="build")
131-
# --fpic is automatically managed when 'fPIC'option is declared
108+
VirtualRunEnv(self).generate(scope="build")
109+
# --fpic is automatically managed when 'fPIC' option is declared
132110
# --enable/disable-shared is automatically managed when 'shared' option is declared
133111
tc = AutotoolsToolchain(self)
134112
# autotools usually uses 'yes' and 'no' to enable/disable options
@@ -143,8 +121,10 @@ def yes_no(v): return "yes" if v else "no"
143121
tc = PkgConfigDeps(self)
144122
tc.generate()
145123
# generate dependencies for autotools
146-
tc = AutotoolsDeps(self)
147-
tc.generate()
124+
# some recipes might require a workaround for MSVC (https://github.com/conan-io/conan/issues/12784):
125+
# https://github.com/conan-io/conan-center-index/blob/00ce907b910d0d772f1c73bb699971c141c423c1/recipes/xapian-core/all/conanfile.py#L106-L135
126+
deps = AutotoolsDeps(self)
127+
deps.generate()
148128

149129
# If Visual Studio is supported
150130
if is_msvc(self):
@@ -158,16 +138,15 @@ def yes_no(v): return "yes" if v else "no"
158138
env.define("CC", f"{compile_wrapper} cl -nologo")
159139
env.define("CXX", f"{compile_wrapper} cl -nologo")
160140
env.define("LD", "link -nologo")
161-
env.define("AR", f"{ar_wrapper} \"lib -nologo\"")
141+
env.define("AR", f"{ar_wrapper} lib")
162142
env.define("NM", "dumpbin -symbols")
163143
env.define("OBJDUMP", ":")
164144
env.define("RANLIB", ":")
165145
env.define("STRIP", ":")
166146
env.vars(self).save_script("conanbuild_msvc")
167147

168148
def build(self):
169-
# apply patches listed in conandata.yml
170-
apply_conandata_patches(self)
149+
171150
autotools = Autotools(self)
172151
# (optional) run autoreconf to regenerate configure file (libtool should be in tool_requires)
173152
autotools.autoreconf()
@@ -180,8 +159,9 @@ def package(self):
180159
autotools = Autotools(self)
181160
autotools.install()
182161

183-
# some files extensions and folders are not allowed. Please, read the FAQs to get informed.
162+
# Some files extensions and folders are not allowed. Please, read the FAQs to get informed.
184163
rm(self, "*.la", os.path.join(self.package_folder, "lib"))
164+
# Consider disabling these at first to verify that the package_info() output matches the info exported by the project.
185165
rmdir(self, os.path.join(self.package_folder, "lib", "pkgconfig"))
186166
rmdir(self, os.path.join(self.package_folder, "share"))
187167

@@ -191,7 +171,7 @@ def package(self):
191171
def package_info(self):
192172
self.cpp_info.libs = ["package_lib"]
193173

194-
# if package provides a pkgconfig file (package.pc, usually installed in <prefix>/lib/pkgconfig/)
174+
# if the package provides a pkgconfig file (package.pc, usually installed in <prefix>/lib/pkgconfig/)
195175
self.cpp_info.set_property("pkg_config_name", "package")
196176

197177
# If they are needed on Linux, m, pthread and dl are usually needed on FreeBSD too
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
cmake_minimum_required(VERSION 3.15)
2-
project(test_package LANGUAGES C) # if the project is pure C
3-
# project(test_package LANGUAGES CXX) # if the project uses c++
2+
project(test_package LANGUAGES CXX)
43

54
find_package(package REQUIRED CONFIG)
65

7-
add_executable(${PROJECT_NAME} test_package.c)
8-
# don't link to ${CONAN_LIBS} or CONAN_PKG::package
6+
add_executable(${PROJECT_NAME} test_package.cpp)
97
target_link_libraries(${PROJECT_NAME} PRIVATE package::package)
10-
# In case the target project need a specific C++ standard
11-
# target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_11)

docs/package_templates/autotools_package/all/test_package/conanfile.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44
import os
55

66

7-
# It will become the standard on Conan 2.x
87
class TestPackageConan(ConanFile):
98
settings = "os", "arch", "compiler", "build_type"
10-
generators = "CMakeDeps", "CMakeToolchain", "VirtualRunEnv"
11-
test_type = "explicit"
9+
generators = "CMakeDeps", "CMakeToolchain"
1210

1311
def layout(self):
1412
cmake_layout(self)

docs/package_templates/autotools_package/all/test_package/test_package.c

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <cstdlib>
2+
#include "package/foobar.h"
3+
4+
5+
int main(void) {
6+
/*
7+
* TODO: Remove this comment before pushing the testing code;
8+
*
9+
* Create a minimal usage for the target project here;
10+
* Avoid upstream full examples, or code bigger than 15 lines;
11+
* Avoid networking connections;
12+
* Avoid background apps or servers;
13+
* Avoid GUI apps;
14+
* Avoid extra files like images, sounds and other binaries;
15+
* The propose is testing the generated artifacts ONLY;
16+
*/
17+
18+
foobar_print_version(); // Make sure to call something that will require linkage for compiled libraries
19+
20+
return EXIT_SUCCESS;
21+
}

docs/package_templates/cmake_package/all/conandata.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
sources:
2-
# Newer versions at the top
32
"1.2.0":
43
url:
54
- "https://mirror1.net/package-1.2.0.tar.gz"
65
- "https://mirror2.net/package-1.2.0.tar.gz"
76
sha256: "________________________________________________________________"
87
"1.1.0":
9-
url:
10-
- "https://mirror1.net/package-1.1.0.tar.gz"
11-
- "https://mirror2.net/package-1.1.0.tar.gz"
8+
url: "https://mirror1.net/package-1.1.0.tar.gz"
129
sha256: "________________________________________________________________"
1310
patches:
14-
# Newer versions at the top
1511
"1.1.0":
1612
- patch_file: "patches/0001-fix-cmake.patch"
1713
patch_description: "correct the order of cmake min and project"

0 commit comments

Comments
 (0)