@@ -64,6 +64,8 @@ translated from the current ``settings``:
64
64
- Definition of ``CMAKE_VS_DEBUGGER_ENVIRONMENT `` when on Windows with Visual Studio.
65
65
This sets up the ``PATH `` environment variable to point to directories containing DLLs,
66
66
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.
67
69
68
70
- **conanvcvars.bat **: In some cases, the Visual Studio environment needs to be defined correctly for building,
69
71
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``:
141
143
with CMake>=3.23) and the schema for the ``CMakePresets.json `` is 3 (compatible with
142
144
CMake>=3.21).
143
145
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
+
144
176
Customization
145
177
-------------
146
178
147
-
148
179
preprocessor_definitions
149
180
^^^^^^^^^^^^^^^^^^^^^^^^
150
181
@@ -312,34 +343,6 @@ By default it is ``"conan"``, and it will generate CMake presets named "conan-xx
312
343
This is done to avoid potential name clashes with users own presets.
313
344
314
345
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
-
343
346
absolute_paths
344
347
^^^^^^^^^^^^^^
345
348
@@ -364,7 +367,11 @@ Using a custom toolchain file
364
367
There are two ways of providing custom CMake toolchain files:
365
368
366
369
- 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.
368
375
- A custom user toolchain file can be added (included from) to the ``conan_toolchain.cmake `` one, by using the
369
376
``user_toolchain `` block described below, and defining the ``tools.cmake.cmaketoolchain:user_toolchain=["<filepath>"] ``
370
377
configuration value.
@@ -416,6 +423,20 @@ There are two ways of providing custom CMake toolchain files:
416
423
cmake.configure()
417
424
418
425
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
+
419
440
Extending and advanced customization
420
441
------------------------------------
421
442
@@ -469,8 +490,13 @@ and added in this order:
469
490
# For CMAKE_INSTALL_DATAROOTDIR, takes the first value:
470
491
self .cpp.package.resdirs = [" myres" ]
471
492
493
+
472
494
.. 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
474
500
475
501
476
502
Customizing the content blocks
@@ -493,18 +519,31 @@ Every block can be customized in different ways (recall to call ``tc.generate()`
493
519
tc = CMakeToolchain(self )
494
520
tc.blocks.remove(" generic_system" , " cmake_flags_init" )
495
521
496
- # keep one block, remove all the others
522
+ # LEGACY: keep one block, remove all the others
497
523
# If you want to generate conan_toolchain.cmake with only that
498
- # block
524
+ # block. Use "tc.blocks.enabled()" instead
499
525
def generate (self ):
500
526
tc = CMakeToolchain(self )
527
+ # this still leaves blocks "variables" and "preprocessor"
528
+ # use "tc.blocks.enabled()"" instead
501
529
tc.blocks.select(" generic_system" )
502
530
503
- # keep several blocks, remove the other blocks
531
+ # LEGACY: keep several blocks, remove the other blocks
532
+ # Use "tc.blocks.enabled()" instead
504
533
def generate (self ):
505
534
tc = CMakeToolchain(self )
535
+ # this still leaves blocks "variables" and "preprocessor"
536
+ # use "tc.blocks.enabled()" instead
506
537
tc.blocks.select(" generic_system" , " cmake_flags_init" )
507
538
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
+
508
547
# iterate blocks
509
548
def generate (self ):
510
549
tc = CMakeToolchain(self )
@@ -572,6 +611,27 @@ Every block can be customized in different ways (recall to call ``tc.generate()`
572
611
tc.blocks[" mynewblock" ] = MyBlock
573
612
574
613
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
+
575
635
For more information about these blocks, please have a look at the source code.
576
636
577
637
@@ -653,6 +713,7 @@ CMakeToolchain is affected by these ``[conf]`` variables:
653
713
- **tools.cmake.cmaketoolchain:system_name ** is not necessary in most cases and is only used to force-define ``CMAKE_SYSTEM_NAME ``.
654
714
- **tools.cmake.cmaketoolchain:system_version ** is not necessary in most cases and is only used to force-define ``CMAKE_SYSTEM_VERSION ``.
655
715
- **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.
656
717
- **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.
657
718
658
719
.. code-block :: text
0 commit comments