Skip to content

Commit ae45608

Browse files
memshardedAbrilRBS
andauthored
CMake enabled blocks (#3786)
* CMake enabled blocks * wip * Update reference/tools/cmake/cmaketoolchain.rst Co-authored-by: Abril Rincón Blanco <[email protected]> * Update reference/tools/cmake/cmaketoolchain.rst Co-authored-by: Abril Rincón Blanco <[email protected]> --------- Co-authored-by: Abril Rincón Blanco <[email protected]>
1 parent 4553ac7 commit ae45608

File tree

1 file changed

+95
-34
lines changed

1 file changed

+95
-34
lines changed

reference/tools/cmake/cmaketoolchain.rst

Lines changed: 95 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ translated from the current ``settings``:
6464
- Definition of ``CMAKE_VS_DEBUGGER_ENVIRONMENT`` when on Windows with Visual Studio.
6565
This sets up the ``PATH`` environment variable to point to directories containing DLLs,
6666
to allow debugging directly from the Visual Studio IDE without copying DLLs (requires CMake 3.27).
67+
- Definition of ``CONAN_RUNTIME_LIB_DIRS`` to allow collecting runtime dependencies (shared libraries),
68+
see below for details.
6769

6870
- **conanvcvars.bat**: In some cases, the Visual Studio environment needs to be defined correctly for building,
6971
like when using the Ninja or NMake generators. If necessary, the ``CMakeToolchain`` will generate this script,
@@ -141,10 +143,39 @@ translated from the current ``settings``:
141143
with CMake>=3.23) and the schema for the ``CMakePresets.json`` is 3 (compatible with
142144
CMake>=3.21).
143145

146+
147+
CONAN_RUNTIME_LIB_DIRS
148+
^^^^^^^^^^^^^^^^^^^^^^
149+
150+
This variable in the generated ``conan_toolchain.cmake`` file contains a list of directories
151+
that contain runtime libraries (like DLLs)
152+
from all dependencies in the host context. This is intended to be used when relying on
153+
CMake functionality to collect shared libraries to create a relocatable bundle, as
154+
per the example below.
155+
156+
157+
Just pass the ``CONAN_RUNTIME_LIB_DIRS`` variable to the ``DIRECTORIES`` argument
158+
in the ``install(RUNTIME_DEPENDENCY_SET ...)``` invocation.
159+
160+
.. code:: cmake
161+
162+
install(RUNTIME_DEPENDENCY_SET my_app_deps
163+
PRE_EXCLUDE_REGEXES
164+
[[api-ms-win-.*]]
165+
[[ext-ms-.*]]
166+
[[kernel32\.dll]]
167+
[[libc\.so\..*]] [[libgcc_s\.so\..*]] [[libm\.so\..*]] [[libstdc\+\+\.so\..*]]
168+
POST_EXCLUDE_REGEXES
169+
[[.*/system32/.*\.dll]]
170+
[[^/lib.*]]
171+
[[^/usr/lib.*]]
172+
DIRECTORIES ${CONAN_RUNTIME_LIB_DIRS}
173+
)
174+
175+
144176
Customization
145177
-------------
146178

147-
148179
preprocessor_definitions
149180
^^^^^^^^^^^^^^^^^^^^^^^^
150181

@@ -312,34 +343,6 @@ By default it is ``"conan"``, and it will generate CMake presets named "conan-xx
312343
This is done to avoid potential name clashes with users own presets.
313344

314345

315-
316-
CONAN_RUNTIME_LIB_DIRS
317-
^^^^^^^^^^^^^^^^^^^^^^
318-
319-
This variable contains a list of directories that contain runtime libraries (like DLLs)
320-
from all dependencies in the host context. This is intended to be used when relying on
321-
CMake functionality to collect shared libraries to create a relocatable bundle, as
322-
per the example below.
323-
324-
325-
Just pass the ``CONAN_RUNTIME_LIB_DIRS`` variable to the ``DIRECTORIES`` argument
326-
in the ``install(RUNTIME_DEPENDENCY_SET ...)``` invocation.
327-
328-
.. code:: cmake
329-
330-
install(RUNTIME_DEPENDENCY_SET my_app_deps
331-
PRE_EXCLUDE_REGEXES
332-
[[api-ms-win-.*]]
333-
[[ext-ms-.*]]
334-
[[kernel32\.dll]]
335-
[[libc\.so\..*]] [[libgcc_s\.so\..*]] [[libm\.so\..*]] [[libstdc\+\+\.so\..*]]
336-
POST_EXCLUDE_REGEXES
337-
[[.*/system32/.*\.dll]]
338-
[[^/lib.*]]
339-
[[^/usr/lib.*]]
340-
DIRECTORIES ${CONAN_RUNTIME_LIB_DIRS}
341-
)
342-
343346
absolute_paths
344347
^^^^^^^^^^^^^^
345348

@@ -364,7 +367,11 @@ Using a custom toolchain file
364367
There are two ways of providing custom CMake toolchain files:
365368

366369
- The ``conan_toolchain.cmake`` file can be completely skipped and replaced by a user one, defining the
367-
``tools.cmake.cmaketoolchain:toolchain_file=<filepath>`` configuration value.
370+
``tools.cmake.cmaketoolchain:toolchain_file=<filepath>`` configuration value. Note this approach will translate
371+
all the toolchain responsibility to the user provided toolchain, but things like locating the necessary ``xxx-config.cmake``
372+
files from dependencies can be challenging without some help. For this reason, using the following
373+
``tools.cmake.cmaketoolchain:user_toolchain`` is recommended in most cases, and if necessary,
374+
using ``tools.cmake.cmaketoolchain:enabled_blocks`` can be used.
368375
- A custom user toolchain file can be added (included from) to the ``conan_toolchain.cmake`` one, by using the
369376
``user_toolchain`` block described below, and defining the ``tools.cmake.cmaketoolchain:user_toolchain=["<filepath>"]``
370377
configuration value.
@@ -416,6 +423,20 @@ There are two ways of providing custom CMake toolchain files:
416423
cmake.configure()
417424
418425
426+
.. note::
427+
428+
**Important notes**
429+
430+
- In most cases, ``tools.cmake.cmaketoolchain:user_toolchain`` will be preferred over ``tools.cmake.cmaketoolchain:toolchain_file``
431+
- The definition of a ``tools.cmake.cmaketoolchain:user_toolchain`` inhibits the automatic definition of
432+
``CMAKE_SYSTEM_NAME``, ``CMAKE_SYSTEM_VERSION`` and ``CMAKE_SYSTEM_PROCESSOR`` that Conan would try to auto-detect
433+
in cases of cross-building, and the user toolchains should provide these values or use other confs such as
434+
``tools.cmake.cmaketoolchain:system_name``.
435+
- The usage of ``tools.cmake.cmaketoolchain:enabled_blocks`` can be used together with ``tools.cmake.cmaketoolchain:user_toolchain``
436+
to enable only certain blocks but avoid CMakeToolchain to override CMake values defined in the user toolchain
437+
file.
438+
439+
419440
Extending and advanced customization
420441
------------------------------------
421442

@@ -469,8 +490,13 @@ and added in this order:
469490
# For CMAKE_INSTALL_DATAROOTDIR, takes the first value:
470491
self.cpp.package.resdirs = ["myres"]
471492
493+
472494
.. note::
473-
It is **not valid** to change the self.cpp_info at the ``package_info()`` method.
495+
It is **not valid** to change the self.cpp_info at the ``package_info()`` method, the ``self.cpp.package``
496+
needs to be defined instead.
497+
498+
- **variables**: Define CMake variables from the ``CMakeToolchain.variables`` attribute.
499+
- **preprocessor**: Define preprocessor directives from ``CMakeToolchain.preprocessor_definitions`` attribute
474500

475501

476502
Customizing the content blocks
@@ -493,18 +519,31 @@ Every block can be customized in different ways (recall to call ``tc.generate()`
493519
tc = CMakeToolchain(self)
494520
tc.blocks.remove("generic_system", "cmake_flags_init")
495521
496-
# keep one block, remove all the others
522+
# LEGACY: keep one block, remove all the others
497523
# If you want to generate conan_toolchain.cmake with only that
498-
# block
524+
# block. Use "tc.blocks.enabled()" instead
499525
def generate(self):
500526
tc = CMakeToolchain(self)
527+
# this still leaves blocks "variables" and "preprocessor"
528+
# use "tc.blocks.enabled()"" instead
501529
tc.blocks.select("generic_system")
502530
503-
# keep several blocks, remove the other blocks
531+
# LEGACY: keep several blocks, remove the other blocks
532+
# Use "tc.blocks.enabled()" instead
504533
def generate(self):
505534
tc = CMakeToolchain(self)
535+
# this still leaves blocks "variables" and "preprocessor"
536+
# use "tc.blocks.enabled()" instead
506537
tc.blocks.select("generic_system", "cmake_flags_init")
507538
539+
# keep several blocks, remove the other blocks
540+
# This can be done from configuration with
541+
# tools.cmake.cmaketoolchain:enabled_blocs
542+
def generate(self):
543+
tc = CMakeToolchain(self)
544+
# Discard all the other blocks except ``generic_system``
545+
tc.blocks.enabled("generic_system")
546+
508547
# iterate blocks
509548
def generate(self):
510549
tc = CMakeToolchain(self)
@@ -572,6 +611,27 @@ Every block can be customized in different ways (recall to call ``tc.generate()`
572611
tc.blocks["mynewblock"] = MyBlock
573612
574613
614+
It is possible to select which blocks are active from configuration in profiles, using the
615+
``tools.cmake.cmaketoolchain:enabled_blocks`` configuration. This is a list of blocks, so doing:
616+
617+
.. code-block::
618+
619+
[conf]
620+
tools.cmake.cmaketoolchain:enabled_blocks=["generic_system"]
621+
622+
Will leave only the ``generic_system`` block, and discard all others.
623+
This feature can be used for example when users are providing their own toolchain files, and they don't
624+
need Conan ``CMakeToolchain`` to define any flags or CMake variables, except for the necessary paths so
625+
dependencies can be found. For this case, it should be possible to do something like:
626+
627+
628+
.. code-block::
629+
630+
[conf]
631+
tools.cmake.cmaketoolchain:user_toolchain+=my_user_toolchain.cmake
632+
tools.cmake.cmaketoolchain:enabled_blocks=["find_paths"]
633+
634+
575635
For more information about these blocks, please have a look at the source code.
576636

577637

@@ -653,6 +713,7 @@ CMakeToolchain is affected by these ``[conf]`` variables:
653713
- **tools.cmake.cmaketoolchain:system_name** is not necessary in most cases and is only used to force-define ``CMAKE_SYSTEM_NAME``.
654714
- **tools.cmake.cmaketoolchain:system_version** is not necessary in most cases and is only used to force-define ``CMAKE_SYSTEM_VERSION``.
655715
- **tools.cmake.cmaketoolchain:system_processor** is not necessary in most cases and is only used to force-define ``CMAKE_SYSTEM_PROCESSOR``.
716+
- **tools.cmake.cmaketoolchain:enabled_blocks** define which blocks are enabled and discard the others.
656717
- **tools.cmake.cmaketoolchain:extra_variables**: dict-like python object which specifies the CMake variable name and value. The value can be a plain string, a number or a dict-like python object which must specify the ``value`` (string/number) , ``cache`` (boolean), ``type`` (CMake cache type) and optionally, ``docstring`` (string: defaulted to variable name) and ``force`` (boolean) keys. It can override CMakeToolchain defined variables, for which users are at their own risk. E.g.
657718

658719
.. code-block:: text

0 commit comments

Comments
 (0)