Skip to content

Commit da0c063

Browse files
authored
Consuming system requirements only when building a package (#3980)
* Add consuming system packages Signed-off-by: Uilian Ries <[email protected]> * Reorganize system package manager structure Signed-off-by: Uilian Ries <[email protected]> * Simplify system reqs only when building example Signed-off-by: Uilian Ries <[email protected]> --------- Signed-off-by: Uilian Ries <[email protected]>
1 parent 3bf23aa commit da0c063

File tree

4 files changed

+79
-1
lines changed

4 files changed

+79
-1
lines changed

Diff for: examples/tools.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ Conan recipe tools examples
1313
tools/autotools/autotools
1414
tools/scm/git/capture_scm/git_capture_scm
1515
tools/microsoft/msbuild
16-
tools/system/package_manager
16+
tools/system/system

Diff for: examples/tools/system/system.rst

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.. _examples_system:
2+
3+
4+
System Packages
5+
===============
6+
7+
8+
.. toctree::
9+
:maxdepth: 2
10+
11+
12+
system_package/package_manager
13+
system_package/consuming_system_packages
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
.. _examples_tools_system_consuming_system_packages:
2+
3+
Consuming system requirements only when building a package
4+
==========================================================
5+
6+
In some cases, you may want to consume system requirements only when building a package, but not when installing it.
7+
It can be useful when you want to build a package in a CI/CD pipeline, but you don't want to run the system package
8+
manager when installing the Conan package in a different environment.
9+
For those cases, there are few approaches that can be used to achieve this goal.
10+
11+
12+
Consume a Conan package wrapper for a system package as build requirement
13+
-------------------------------------------------------------------------
14+
15+
In this approach, you can use a Conan package for a :ref:`wrapped system package<examples_tools_system_package_manager>`.
16+
Then, the package can be consumed regularly by the method
17+
:ref:`build_requirements()<reference_conanfile_methods_build_requirements>`.
18+
19+
.. code-block:: python
20+
21+
from conan import ConanFile
22+
23+
class MyPackage(ConanFile):
24+
name = "mypackage"
25+
settings = "os", "compiler", "build_type", "arch"
26+
27+
def build_requirements(self):
28+
self.tool_requires("ncurses/system")
29+
30+
...
31+
32+
This ensures that downstream consumers of the package *mypackage* will not directly invoke the system
33+
package manager (e.g., apt-get). Only the direct package consumer of the system wrap package for ``ncurses``
34+
will execute the system package manager when building the package.
35+
36+
Centralizing and wrapping ``ncurses`` in a separated recipe makes it reusable across multiple cases and is
37+
good practice to avoid code duplication.
38+
39+
40+
Consume the system package directly in the build() method
41+
---------------------------------------------------------
42+
43+
In case wanting to run the system package manager only when building the package, but not having a Conan package to
44+
wrap the system library information, it's possible to run the system package manager in the **build()** method:
45+
46+
.. code-block:: python
47+
48+
from conan import ConanFile
49+
from conan.tools.system import package_manager
50+
51+
class MyPackage(ConanFile):
52+
settings = "os", "compiler", "build_type", "arch"
53+
...
54+
55+
def build(self):
56+
if self.settings.os == "Linux":
57+
apt = package_manager.Apt(self)
58+
apt.install(["libncurses-dev"], update=True, check=True)
59+
60+
This way, the system package manager will be called only when building the package, not when installing it.
61+
There is the advantage of not needed to create a separated Conan package to wrap the system library information,
62+
this is a much simpler case, when only a single recipe need to install the system package.
63+
64+
Still, this approach may lead to code duplication if multiple recipes consume the same system package.
65+
It is recommended to use this method sparingly and only for well-contained cases.

0 commit comments

Comments
 (0)