|
| 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