Skip to content

Commit a550351

Browse files
danimtbmemsharded
authored andcommitted
Constrain settings and options section (conan-io#698)
* Constrain settings and options section * review
1 parent 1b2a9a2 commit a550351

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

‎mastering/conditional.rst‎

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,59 @@ Here is an example of what we could do in our **configure method**:
4646
else:
4747
self.requires("OpenSSL/1.0.2d@lasote/stable")
4848
49+
Constrain settings and options
50+
------------------------------
4951

50-
.. seealso:: Check the section :ref:`Reference/conanfile.py/configure(), config_options() <method_configure_config_options>` to find out more.
52+
Sometimes there are libraries that are not compatible with specific settings like libraries
53+
that are not compatible with an architecture or options that only make sense for an operating system. It can be also useful when there are
54+
settings under development.
55+
56+
There are two approaches for this situation:
57+
58+
- **Use** ``configure()`` **to raise an error for non-supported configurations**:
59+
60+
This approach is the first one evaluated when Conan loads the recipe so it is quite handy to perform checks of the input settings. It
61+
relies on the set of possible settings inside your *settings.yml* file so it can be used to constrain any recipe.
62+
63+
.. code-block:: python
64+
65+
def configure(self):
66+
if self.settings.os == "Windows":
67+
raise ConanException("This library is not compatible with Windows")
68+
69+
This same method is also valid for ``options`` and ``config_options()`` method and it is commonly used to remove options for one setting:
70+
71+
.. code-block:: python
72+
73+
def config_options(self):
74+
if self.settings.os == "Windows":
75+
del self.options.fPIC
76+
77+
- **Constrain settings inside a recipe**:
78+
79+
This approach constrains the settings inside a recipe to a subset of them and it is normally used in recipes that are never supposed to
80+
work out of the restricted settings.
81+
82+
.. code-block:: python
83+
84+
from conans import ConanFile
85+
86+
class MyConan(ConanFile):
87+
name = "myconanlibrary"
88+
version = "1.0.0"
89+
settings = {"os": None, "build_type": None, "compiler": None, "arch": ["x86_64"]}
90+
91+
The disadvantage of this is that possible settings are hardcoded in the recipe and in case new values are used in the future, it will
92+
require the recipe to be modified explicitly.
93+
94+
.. important::
95+
96+
Note the use of ``None`` value in the ``os``, ``compiler`` and ``build_type`` settings described above will allow them to take the values
97+
from *settings.yml* file
98+
99+
We strongly recommend the use if the first approach whenever it is possible and use the second one only for those cases where a stronger
100+
constrain is needed for a particular recipe.
101+
102+
.. seealso::
103+
104+
Check the reference section :ref:`configure(), config_options() <method_configure_config_options>` to find out more.

0 commit comments

Comments
 (0)