1CMAKE-GENERATOR-EXPRESSIONS(7) CMake CMAKE-GENERATOR-EXPRESSIONS(7)
2
3
4
6 cmake-generator-expressions - CMake Generator Expressions
7
9 Generator expressions are evaluated during build system generation to
10 produce information specific to each build configuration.
11
12 Generator expressions are allowed in the context of many target proper‐
13 ties, such as LINK_LIBRARIES, INCLUDE_DIRECTORIES, COMPILE_DEFINITIONS
14 and others. They may also be used when using commands to populate
15 those properties, such as target_link_libraries(), target_include_di‐
16 rectories(), target_compile_definitions() and others.
17
18 They enable conditional linking, conditional definitions used when com‐
19 piling, conditional include directories, and more. The conditions may
20 be based on the build configuration, target properties, platform infor‐
21 mation or any other queryable information.
22
23 Generator expressions have the form $<...>. To avoid confusion, this
24 page deviates from most of the CMake documentation in that it omits an‐
25 gular brackets <...> around placeholders like condition, string, tar‐
26 get, among others.
27
28 Generator expressions can be nested, as shown in most of the examples
29 below.
30
32 Boolean expressions evaluate to either 0 or 1. They are typically used
33 to construct the condition in a conditional generator expression.
34
35 Available boolean expressions are:
36
37 Logical Operators
38 $<BOOL:string>
39 Converts string to 0 or 1. Evaluates to 0 if any of the follow‐
40 ing is true:
41
42 • string is empty,
43
44 • string is a case-insensitive equal of 0, FALSE, OFF, N, NO,
45 IGNORE, or NOTFOUND, or
46
47 • string ends in the suffix -NOTFOUND (case-sensitive).
48
49 Otherwise evaluates to 1.
50
51 $<AND:conditions>
52 where conditions is a comma-separated list of boolean expres‐
53 sions. Evaluates to 1 if all conditions are 1. Otherwise eval‐
54 uates to 0.
55
56 $<OR:conditions>
57 where conditions is a comma-separated list of boolean expres‐
58 sions. Evaluates to 1 if at least one of the conditions is 1.
59 Otherwise evaluates to 0.
60
61 $<NOT:condition>
62 0 if condition is 1, else 1.
63
64 String Comparisons
65 $<STREQUAL:string1,string2>
66 1 if string1 and string2 are equal, else 0. The comparison is
67 case-sensitive. For a case-insensitive comparison, combine with
68 a string transforming generator expression,
69
70 $<STREQUAL:$<UPPER_CASE:${foo}>,"BAR"> # "1" if ${foo} is any of "BAR", "Bar", "bar", ...
71
72 $<EQUAL:value1,value2>
73 1 if value1 and value2 are numerically equal, else 0.
74
75 $<IN_LIST:string,list>
76 1 if string is member of the semicolon-separated list, else 0.
77 Uses case-sensitive comparisons.
78
79 $<VERSION_LESS:v1,v2>
80 1 if v1 is a version less than v2, else 0.
81
82 $<VERSION_GREATER:v1,v2>
83 1 if v1 is a version greater than v2, else 0.
84
85 $<VERSION_EQUAL:v1,v2>
86 1 if v1 is the same version as v2, else 0.
87
88 $<VERSION_LESS_EQUAL:v1,v2>
89 1 if v1 is a version less than or equal to v2, else 0.
90
91 $<VERSION_GREATER_EQUAL:v1,v2>
92 1 if v1 is a version greater than or equal to v2, else 0.
93
94 Variable Queries
95 $<TARGET_EXISTS:target>
96 1 if target exists, else 0.
97
98 $<CONFIG:cfgs>
99 1 if config is any one of the entries in cfgs, else 0. This is a
100 case-insensitive comparison. The mapping in MAP_IMPORTED_CON‐
101 FIG_<CONFIG> is also considered by this expression when it is
102 evaluated on a property on an IMPORTED target.
103
104 $<PLATFORM_ID:platform_ids>
105 where platform_ids is a comma-separated list. 1 if the CMake's
106 platform id matches any one of the entries in platform_ids, oth‐
107 erwise 0. See also the CMAKE_SYSTEM_NAME variable.
108
109 $<C_COMPILER_ID:compiler_ids>
110 where compiler_ids is a comma-separated list. 1 if the CMake's
111 compiler id of the C compiler matches any one of the entries in
112 compiler_ids, otherwise 0. See also the CMAKE_<LANG>_COM‐
113 PILER_ID variable.
114
115 $<CXX_COMPILER_ID:compiler_ids>
116 where compiler_ids is a comma-separated list. 1 if the CMake's
117 compiler id of the CXX compiler matches any one of the entries
118 in compiler_ids, otherwise 0. See also the CMAKE_<LANG>_COM‐
119 PILER_ID variable.
120
121 $<CUDA_COMPILER_ID:compiler_ids>
122 where compiler_ids is a comma-separated list. 1 if the CMake's
123 compiler id of the CUDA compiler matches any one of the entries
124 in compiler_ids, otherwise 0. See also the CMAKE_<LANG>_COM‐
125 PILER_ID variable.
126
127 $<OBJC_COMPILER_ID:compiler_ids>
128 where compiler_ids is a comma-separated list. 1 if the CMake's
129 compiler id of the Objective-C compiler matches any one of the
130 entries in compiler_ids, otherwise 0. See also the
131 CMAKE_<LANG>_COMPILER_ID variable.
132
133 $<OBJCXX_COMPILER_ID:compiler_ids>
134 where compiler_ids is a comma-separated list. 1 if the CMake's
135 compiler id of the Objective-C++ compiler matches any one of the
136 entries in compiler_ids, otherwise 0. See also the
137 CMAKE_<LANG>_COMPILER_ID variable.
138
139 $<Fortran_COMPILER_ID:compiler_ids>
140 where compiler_ids is a comma-separated list. 1 if the CMake's
141 compiler id of the Fortran compiler matches any one of the en‐
142 tries in compiler_ids, otherwise 0. See also the
143 CMAKE_<LANG>_COMPILER_ID variable.
144
145 $<ISPC_COMPILER_ID:compiler_ids>
146 where compiler_ids is a comma-separated list. 1 if the CMake's
147 compiler id of the ISPC compiler matches any one of the entries
148 in compiler_ids, otherwise 0. See also the CMAKE_<LANG>_COM‐
149 PILER_ID variable.
150
151 $<C_COMPILER_VERSION:version>
152 1 if the version of the C compiler matches version, otherwise 0.
153 See also the CMAKE_<LANG>_COMPILER_VERSION variable.
154
155 $<CXX_COMPILER_VERSION:version>
156 1 if the version of the CXX compiler matches version, otherwise
157 0. See also the CMAKE_<LANG>_COMPILER_VERSION variable.
158
159 $<CUDA_COMPILER_VERSION:version>
160 1 if the version of the CXX compiler matches version, otherwise
161 0. See also the CMAKE_<LANG>_COMPILER_VERSION variable.
162
163 $<OBJC_COMPILER_VERSION:version>
164 1 if the version of the OBJC compiler matches version, otherwise
165 0. See also the CMAKE_<LANG>_COMPILER_VERSION variable.
166
167 $<OBJCXX_COMPILER_VERSION:version>
168 1 if the version of the OBJCXX compiler matches version, other‐
169 wise 0. See also the CMAKE_<LANG>_COMPILER_VERSION variable.
170
171 $<Fortran_COMPILER_VERSION:version>
172 1 if the version of the Fortran compiler matches version, other‐
173 wise 0. See also the CMAKE_<LANG>_COMPILER_VERSION variable.
174
175 $<ISPC_COMPILER_VERSION:version>
176 1 if the version of the ISPC compiler matches version, otherwise
177 0. See also the CMAKE_<LANG>_COMPILER_VERSION variable.
178
179 $<TARGET_POLICY:policy>
180 1 if the policy was NEW when the 'head' target was created, else
181 0. If the policy was not set, the warning message for the pol‐
182 icy will be emitted. This generator expression only works for a
183 subset of policies.
184
185 $<COMPILE_FEATURES:features>
186 where features is a comma-spearated list. Evaluates to 1 if all
187 of the features are available for the 'head' target, and 0 oth‐
188 erwise. If this expression is used while evaluating the link im‐
189 plementation of a target and if any dependency transitively in‐
190 creases the required C_STANDARD or CXX_STANDARD for the 'head'
191 target, an error is reported. See the cmake-compile-features(7)
192 manual for information on compile features and a list of sup‐
193 ported compilers.
194
195 $<COMPILE_LANG_AND_ID:language,compiler_ids>
196 1 when the language used for compilation unit matches language
197 and the CMake's compiler id of the language compiler matches any
198 one of the entries in compiler_ids, otherwise 0. This expression
199 is a short form for the combination of $<COMPILE_LANGUAGE:lan‐
200 guage> and $<LANG_COMPILER_ID:compiler_ids>. This expression may
201 be used to specify compile options, compile definitions, and in‐
202 clude directories for source files of a particular language and
203 compiler combination in a target. For example:
204
205 add_executable(myapp main.cpp foo.c bar.cpp zot.cu)
206 target_compile_definitions(myapp
207 PRIVATE $<$<COMPILE_LANG_AND_ID:CXX,AppleClang,Clang>:COMPILING_CXX_WITH_CLANG>
208 $<$<COMPILE_LANG_AND_ID:CXX,Intel>:COMPILING_CXX_WITH_INTEL>
209 $<$<COMPILE_LANG_AND_ID:C,Clang>:COMPILING_C_WITH_CLANG>
210 )
211
212 This specifies the use of different compile definitions based on
213 both the compiler id and compilation language. This example will
214 have a COMPILING_CXX_WITH_CLANG compile definition when Clang is
215 the CXX compiler, and COMPILING_CXX_WITH_INTEL when Intel is the
216 CXX compiler. Likewise when the C compiler is Clang it will
217 only see the COMPILING_C_WITH_CLANG definition.
218
219 Without the COMPILE_LANG_AND_ID generator expression the same
220 logic would be expressed as:
221
222 target_compile_definitions(myapp
223 PRIVATE $<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CXX_COMPILER_ID:AppleClang,Clang>>:COMPILING_CXX_WITH_CLANG>
224 $<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CXX_COMPILER_ID:Intel>>:COMPILING_CXX_WITH_INTEL>
225 $<$<AND:$<COMPILE_LANGUAGE:C>,$<C_COMPILER_ID:Clang>>:COMPILING_C_WITH_CLANG>
226 )
227
228 $<COMPILE_LANGUAGE:languages>
229 1 when the language used for compilation unit matches any of the
230 entries in languages, otherwise 0. This expression may be used
231 to specify compile options, compile definitions, and include di‐
232 rectories for source files of a particular language in a target.
233 For example:
234
235 add_executable(myapp main.cpp foo.c bar.cpp zot.cu)
236 target_compile_options(myapp
237 PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions>
238 )
239 target_compile_definitions(myapp
240 PRIVATE $<$<COMPILE_LANGUAGE:CXX>:COMPILING_CXX>
241 $<$<COMPILE_LANGUAGE:CUDA>:COMPILING_CUDA>
242 )
243 target_include_directories(myapp
244 PRIVATE $<$<COMPILE_LANGUAGE:CXX,CUDA>:/opt/foo/headers>
245 )
246
247 This specifies the use of the -fno-exceptions compile option,
248 COMPILING_CXX compile definition, and cxx_headers include direc‐
249 tory for C++ only (compiler id checks elided). It also speci‐
250 fies a COMPILING_CUDA compile definition for CUDA.
251
252 Note that with Visual Studio Generators and Xcode there is no
253 way to represent target-wide compile definitions or include di‐
254 rectories separately for C and CXX languages. Also, with Visual
255 Studio Generators there is no way to represent target-wide flags
256 separately for C and CXX languages. Under these generators, ex‐
257 pressions for both C and C++ sources will be evaluated using CXX
258 if there are any C++ sources and otherwise using C. A work‐
259 around is to create separate libraries for each source file lan‐
260 guage instead:
261
262 add_library(myapp_c foo.c)
263 add_library(myapp_cxx bar.cpp)
264 target_compile_options(myapp_cxx PUBLIC -fno-exceptions)
265 add_executable(myapp main.cpp)
266 target_link_libraries(myapp myapp_c myapp_cxx)
267
268 $<LINK_LANG_AND_ID:language,compiler_ids>
269 1 when the language used for link step matches language and the
270 CMake's compiler id of the language linker matches any one of
271 the entries in compiler_ids, otherwise 0. This expression is a
272 short form for the combination of $<LINK_LANGUAGE:language> and
273 $<LANG_COMPILER_ID:compiler_ids>. This expression may be used to
274 specify link libraries, link options, link directories and link
275 dependencies of a particular language and linker combination in
276 a target. For example:
277
278 add_library(libC_Clang ...)
279 add_library(libCXX_Clang ...)
280 add_library(libC_Intel ...)
281 add_library(libCXX_Intel ...)
282
283 add_executable(myapp main.c)
284 if (CXX_CONFIG)
285 target_sources(myapp PRIVATE file.cxx)
286 endif()
287 target_link_libraries(myapp
288 PRIVATE $<$<LINK_LANG_AND_ID:CXX,Clang,AppleClang>:libCXX_Clang>
289 $<$<LINK_LANG_AND_ID:C,Clang,AppleClang>:libC_Clang>
290 $<$<LINK_LANG_AND_ID:CXX,Intel>:libCXX_Intel>
291 $<$<LINK_LANG_AND_ID:C,Intel>:libC_Intel>)
292
293 This specifies the use of different link libraries based on both
294 the compiler id and link language. This example will have target
295 libCXX_Clang as link dependency when Clang or AppleClang is the
296 CXX linker, and libCXX_Intel when Intel is the CXX linker.
297 Likewise when the C linker is Clang or AppleClang, target
298 libC_Clang will be added as link dependency and libC_Intel when
299 Intel is the C linker.
300
301 See the note related to $<LINK_LANGUAGE:language> for con‐
302 straints about the usage of this generator expression.
303
304 $<LINK_LANGUAGE:languages>
305 1 when the language used for link step matches any of the en‐
306 tries in languages, otherwise 0. This expression may be used to
307 specify link libraries, link options, link directories and link
308 dependencies of a particular language in a target. For example:
309
310 add_library(api_C ...)
311 add_library(api_CXX ...)
312 add_library(api INTERFACE)
313 target_link_options(api INTERFACE $<$<LINK_LANGUAGE:C>:-opt_c>
314 $<$<LINK_LANGUAGE:CXX>:-opt_cxx>)
315 target_link_libraries(api INTERFACE $<$<LINK_LANGUAGE:C>:api_C>
316 $<$<LINK_LANGUAGE:CXX>:api_CXX>)
317
318 add_executable(myapp1 main.c)
319 target_link_options(myapp1 PRIVATE api)
320
321 add_executable(myapp2 main.cpp)
322 target_link_options(myapp2 PRIVATE api)
323
324 This specifies to use the api target for linking targets myapp1
325 and myapp2. In practice, myapp1 will link with target api_C and
326 option -opt_c because it will use C as link language. And myapp2
327 will link with api_CXX and option -opt_cxx because CXX will be
328 the link language.
329
330 NOTE:
331 To determine the link language of a target, it is required to
332 collect, transitively, all the targets which will be linked
333 to it. So, for link libraries properties, a double evaluation
334 will be done. During the first evaluation, $<LINK_LAN‐
335 GUAGE:..> expressions will always return 0. The link lan‐
336 guage computed after this first pass will be used to do the
337 second pass. To avoid inconsistency, it is required that the
338 second pass do not change the link language. Moreover, to
339 avoid unexpected side-effects, it is required to specify com‐
340 plete entities as part of the $<LINK_LANGUAGE:..> expression.
341 For example:
342
343 add_library(lib STATIC file.cxx)
344 add_library(libother STATIC file.c)
345
346 # bad usage
347 add_executable(myapp1 main.c)
348 target_link_libraries(myapp1 PRIVATE lib$<$<LINK_LANGUAGE:C>:other>)
349
350 # correct usage
351 add_executable(myapp2 main.c)
352 target_link_libraries(myapp2 PRIVATE $<$<LINK_LANGUAGE:C>:libother>)
353
354 In this example, for myapp1, the first pass will, unexpect‐
355 edly, determine that the link language is CXX because the
356 evaluation of the generator expression will be an empty
357 string so myapp1 will depends on target lib which is C++. On
358 the contrary, for myapp2, the first evaluation will give C as
359 link language, so the second pass will correctly add target
360 libother as link dependency.
361
362 $<DEVICE_LINK:list>
363 Returns the list if it is the device link step, an empty list
364 otherwise. The device link step is controlled by CUDA_SEPARA‐
365 BLE_COMPILATION and CUDA_RESOLVE_DEVICE_SYMBOLS properties and
366 policy CMP0105. This expression can only be used to specify link
367 options.
368
369 $<HOST_LINK:list>
370 Returns the list if it is the normal link step, an empty list
371 otherwise. This expression is mainly useful when a device link
372 step is also involved (see $<DEVICE_LINK:list> generator expres‐
373 sion). This expression can only be used to specify link options.
374
376 These expressions expand to some string. For example,
377
378 include_directories(/usr/include/$<CXX_COMPILER_ID>/)
379
380 expands to /usr/include/GNU/ or /usr/include/Clang/ etc, depending on
381 the compiler identifier.
382
383 String-valued expressions may also be combined with other expressions.
384 Here an example for a string-valued expression within a boolean expres‐
385 sions within a conditional expression:
386
387 $<$<VERSION_LESS:$<CXX_COMPILER_VERSION>,4.2.0>:OLD_COMPILER>
388
389 expands to OLD_COMPILER if the CMAKE_CXX_COMPILER_VERSION is less than
390 4.2.0.
391
392 And here two nested string-valued expressions:
393
394 -I$<JOIN:$<TARGET_PROPERTY:INCLUDE_DIRECTORIES>, -I>
395
396 generates a string of the entries in the INCLUDE_DIRECTORIES target
397 property with each entry preceded by -I.
398
399 Expanding on the previous example, if one first wants to check if the
400 INCLUDE_DIRECTORIES property is non-empty, then it is advisable to in‐
401 troduce a helper variable to keep the code readable:
402
403 set(prop "$<TARGET_PROPERTY:INCLUDE_DIRECTORIES>") # helper variable
404 $<$<BOOL:${prop}>:-I$<JOIN:${prop}, -I>>
405
406 The following string-valued generator expressions are available:
407
408 Escaped Characters
409 String literals to escape the special meaning a character would other‐
410 wise have:
411
412 $<ANGLE-R>
413 A literal >. Used for example to compare strings that contain a
414 >.
415
416 $<COMMA>
417 A literal ,. Used for example to compare strings which contain a
418 ,.
419
420 $<SEMICOLON>
421 A literal ;. Used to prevent list expansion on an argument with
422 ;.
423
424 Conditional Expressions
425 Conditional generator expressions depend on a boolean condition that
426 must be 0 or 1.
427
428 $<condition:true_string>
429 Evaluates to true_string if condition is 1. Otherwise evaluates
430 to the empty string.
431
432 $<IF:condition,true_string,false_string>
433 Evaluates to true_string if condition is 1. Otherwise evaluates
434 to false_string.
435
436 Typically, the condition is a boolean generator expression. For in‐
437 stance,
438
439 $<$<CONFIG:Debug>:DEBUG_MODE>
440
441 expands to DEBUG_MODE when the Debug configuration is used, and other‐
442 wise expands to the empty string.
443
444 String Transformations
445 $<JOIN:list,string>
446 Joins the list with the content of string.
447
448 $<REMOVE_DUPLICATES:list>
449 Removes duplicated items in the given list.
450
451 $<FILTER:list,INCLUDE|EXCLUDE,regex>
452 Includes or removes items from list that match the regular ex‐
453 pression regex.
454
455 $<LOWER_CASE:string>
456 Content of string converted to lower case.
457
458 $<UPPER_CASE:string>
459 Content of string converted to upper case.
460
461 $<GENEX_EVAL:expr>
462 Content of expr evaluated as a generator expression in the cur‐
463 rent context. This enables consumption of generator expressions
464 whose evaluation results itself in generator expressions.
465
466 $<TARGET_GENEX_EVAL:tgt,expr>
467 Content of expr evaluated as a generator expression in the con‐
468 text of tgt target. This enables consumption of custom target
469 properties that themselves contain generator expressions.
470
471 Having the capability to evaluate generator expressions is very
472 useful when you want to manage custom properties supporting gen‐
473 erator expressions. For example:
474
475 add_library(foo ...)
476
477 set_property(TARGET foo PROPERTY
478 CUSTOM_KEYS $<$<CONFIG:DEBUG>:FOO_EXTRA_THINGS>
479 )
480
481 add_custom_target(printFooKeys
482 COMMAND ${CMAKE_COMMAND} -E echo $<TARGET_PROPERTY:foo,CUSTOM_KEYS>
483 )
484
485 This naive implementation of the printFooKeys custom command is
486 wrong because CUSTOM_KEYS target property is not evaluated and
487 the content is passed as is (i.e. $<$<CONFIG:DEBUG>:FOO_EX‐
488 TRA_THINGS>).
489
490 To have the expected result (i.e. FOO_EXTRA_THINGS if config is
491 Debug), it is required to evaluate the output of $<TARGET_PROP‐
492 ERTY:foo,CUSTOM_KEYS>:
493
494 add_custom_target(printFooKeys
495 COMMAND ${CMAKE_COMMAND} -E
496 echo $<TARGET_GENEX_EVAL:foo,$<TARGET_PROPERTY:foo,CUSTOM_KEYS>>
497 )
498
499 Variable Queries
500 $<CONFIG>
501 Configuration name.
502
503 $<CONFIGURATION>
504 Configuration name. Deprecated since CMake 3.0. Use CONFIG in‐
505 stead.
506
507 $<PLATFORM_ID>
508 The current system's CMake platform id. See also the CMAKE_SYS‐
509 TEM_NAME variable.
510
511 $<C_COMPILER_ID>
512 The CMake's compiler id of the C compiler used. See also the
513 CMAKE_<LANG>_COMPILER_ID variable.
514
515 $<CXX_COMPILER_ID>
516 The CMake's compiler id of the CXX compiler used. See also the
517 CMAKE_<LANG>_COMPILER_ID variable.
518
519 $<CUDA_COMPILER_ID>
520 The CMake's compiler id of the CUDA compiler used. See also the
521 CMAKE_<LANG>_COMPILER_ID variable.
522
523 $<OBJC_COMPILER_ID>
524 The CMake's compiler id of the OBJC compiler used. See also the
525 CMAKE_<LANG>_COMPILER_ID variable.
526
527 $<OBJCXX_COMPILER_ID>
528 The CMake's compiler id of the OBJCXX compiler used. See also
529 the CMAKE_<LANG>_COMPILER_ID variable.
530
531 $<Fortran_COMPILER_ID>
532 The CMake's compiler id of the Fortran compiler used. See also
533 the CMAKE_<LANG>_COMPILER_ID variable.
534
535 $<ISPC_COMPILER_ID>
536 The CMake's compiler id of the ISPC compiler used. See also the
537 CMAKE_<LANG>_COMPILER_ID variable.
538
539 $<C_COMPILER_VERSION>
540 The version of the C compiler used. See also the
541 CMAKE_<LANG>_COMPILER_VERSION variable.
542
543 $<CXX_COMPILER_VERSION>
544 The version of the CXX compiler used. See also the
545 CMAKE_<LANG>_COMPILER_VERSION variable.
546
547 $<CUDA_COMPILER_VERSION>
548 The version of the CUDA compiler used. See also the
549 CMAKE_<LANG>_COMPILER_VERSION variable.
550
551 $<OBJC_COMPILER_VERSION>
552 The version of the OBJC compiler used. See also the
553 CMAKE_<LANG>_COMPILER_VERSION variable.
554
555 $<OBJCXX_COMPILER_VERSION>
556 The version of the OBJCXX compiler used. See also the
557 CMAKE_<LANG>_COMPILER_VERSION variable.
558
559 $<Fortran_COMPILER_VERSION>
560 The version of the Fortran compiler used. See also the
561 CMAKE_<LANG>_COMPILER_VERSION variable.
562
563 $<ISPC_COMPILER_VERSION>
564 The version of the ISPC compiler used. See also the
565 CMAKE_<LANG>_COMPILER_VERSION variable.
566
567 $<COMPILE_LANGUAGE>
568 The compile language of source files when evaluating compile op‐
569 tions. See the related boolean expression $<COMPILE_LAN‐
570 GUAGE:language> for notes about the portability of this genera‐
571 tor expression.
572
573 $<LINK_LANGUAGE>
574 The link language of target when evaluating link options. See
575 the related boolean expression $<LINK_LANGUAGE:language> for
576 notes about the portability of this generator expression.
577
578 NOTE:
579 This generator expression is not supported by the link li‐
580 braries properties to avoid side-effects due to the double
581 evaluation of these properties.
582
583 Target-Dependent Queries
584 These queries refer to a target tgt. This can be any runtime artifact,
585 namely:
586
587 • an executable target created by add_executable()
588
589 • a shared library target (.so, .dll but not their .lib import library)
590 created by add_library()
591
592 • a static library target created by add_library()
593
594 In the following, "the tgt filename" means the name of the tgt binary
595 file. This has to be distinguished from "the target name", which is
596 just the string tgt.
597
598 $<TARGET_NAME_IF_EXISTS:tgt>
599 The target name tgt if the target exists, an empty string other‐
600 wise.
601
602 Note that tgt is not added as a dependency of the target this
603 expression is evaluated on.
604
605 $<TARGET_FILE:tgt>
606 Full path to the tgt binary file.
607
608 $<TARGET_FILE_BASE_NAME:tgt>
609 Base name of tgt, i.e. $<TARGET_FILE_NAME:tgt> without prefix
610 and suffix. For example, if the tgt filename is libbase.so, the
611 base name is base.
612
613 See also the OUTPUT_NAME, ARCHIVE_OUTPUT_NAME, LIBRARY_OUT‐
614 PUT_NAME and RUNTIME_OUTPUT_NAME target properties and their
615 configuration specific variants OUTPUT_NAME_<CONFIG>, AR‐
616 CHIVE_OUTPUT_NAME_<CONFIG>, LIBRARY_OUTPUT_NAME_<CONFIG> and
617 RUNTIME_OUTPUT_NAME_<CONFIG>.
618
619 The <CONFIG>_POSTFIX and DEBUG_POSTFIX target properties can
620 also be considered.
621
622 Note that tgt is not added as a dependency of the target this
623 expression is evaluated on.
624
625 $<TARGET_FILE_PREFIX:tgt>
626 Prefix of the tgt filename (such as lib).
627
628 See also the PREFIX target property.
629
630 Note that tgt is not added as a dependency of the target this
631 expression is evaluated on.
632
633 $<TARGET_FILE_SUFFIX:tgt>
634 Suffix of the tgt filename (extension such as .so or .exe).
635
636 See also the SUFFIX target property.
637
638 Note that tgt is not added as a dependency of the target this
639 expression is evaluated on.
640
641 $<TARGET_FILE_NAME:tgt>
642 The tgt filename.
643
644 Note that tgt is not added as a dependency of the target this
645 expression is evaluated on (see policy CMP0112).
646
647 $<TARGET_FILE_DIR:tgt>
648 Directory of the tgt binary file.
649
650 Note that tgt is not added as a dependency of the target this
651 expression is evaluated on (see policy CMP0112).
652
653 $<TARGET_LINKER_FILE:tgt>
654 File used when linking to the tgt target. This will usually be
655 the library that tgt represents (.a, .lib, .so), but for a
656 shared library on DLL platforms, it would be the .lib import li‐
657 brary associated with the DLL.
658
659 $<TARGET_LINKER_FILE_BASE_NAME:tgt>
660 Base name of file used to link the target tgt, i.e. $<TAR‐
661 GET_LINKER_FILE_NAME:tgt> without prefix and suffix. For exam‐
662 ple, if target file name is libbase.a, the base name is base.
663
664 See also the OUTPUT_NAME, ARCHIVE_OUTPUT_NAME, and LIBRARY_OUT‐
665 PUT_NAME target properties and their configuration specific
666 variants OUTPUT_NAME_<CONFIG>, ARCHIVE_OUTPUT_NAME_<CONFIG> and
667 LIBRARY_OUTPUT_NAME_<CONFIG>.
668
669 The <CONFIG>_POSTFIX and DEBUG_POSTFIX target properties can
670 also be considered.
671
672 Note that tgt is not added as a dependency of the target this
673 expression is evaluated on.
674
675 $<TARGET_LINKER_FILE_PREFIX:tgt>
676 Prefix of file used to link target tgt.
677
678 See also the PREFIX and IMPORT_PREFIX target properties.
679
680 Note that tgt is not added as a dependency of the target this
681 expression is evaluated on.
682
683 $<TARGET_LINKER_FILE_SUFFIX:tgt>
684 Suffix of file used to link where tgt is the name of a target.
685
686 The suffix corresponds to the file extension (such as ".so" or
687 ".lib").
688
689 See also the SUFFIX and IMPORT_SUFFIX target properties.
690
691 Note that tgt is not added as a dependency of the target this
692 expression is evaluated on.
693
694 $<TARGET_LINKER_FILE_NAME:tgt>
695 Name of file used to link target tgt.
696
697 Note that tgt is not added as a dependency of the target this
698 expression is evaluated on (see policy CMP0112).
699
700 $<TARGET_LINKER_FILE_DIR:tgt>
701 Directory of file used to link target tgt.
702
703 Note that tgt is not added as a dependency of the target this
704 expression is evaluated on (see policy CMP0112).
705
706 $<TARGET_SONAME_FILE:tgt>
707 File with soname (.so.3) where tgt is the name of a target.
708
709 $<TARGET_SONAME_FILE_NAME:tgt>
710 Name of file with soname (.so.3).
711
712 Note that tgt is not added as a dependency of the target this
713 expression is evaluated on (see policy CMP0112).
714
715 $<TARGET_SONAME_FILE_DIR:tgt>
716 Directory of with soname (.so.3).
717
718 Note that tgt is not added as a dependency of the target this
719 expression is evaluated on (see policy CMP0112).
720
721 $<TARGET_PDB_FILE:tgt>
722 Full path to the linker generated program database file (.pdb)
723 where tgt is the name of a target.
724
725 See also the PDB_NAME and PDB_OUTPUT_DIRECTORY target properties
726 and their configuration specific variants PDB_NAME_<CONFIG> and
727 PDB_OUTPUT_DIRECTORY_<CONFIG>.
728
729 $<TARGET_PDB_FILE_BASE_NAME:tgt>
730 Base name of the linker generated program database file (.pdb)
731 where tgt is the name of a target.
732
733 The base name corresponds to the target PDB file name (see
734 $<TARGET_PDB_FILE_NAME:tgt>) without prefix and suffix. For ex‐
735 ample, if target file name is base.pdb, the base name is base.
736
737 See also the PDB_NAME target property and its configuration spe‐
738 cific variant PDB_NAME_<CONFIG>.
739
740 The <CONFIG>_POSTFIX and DEBUG_POSTFIX target properties can
741 also be considered.
742
743 Note that tgt is not added as a dependency of the target this
744 expression is evaluated on.
745
746 $<TARGET_PDB_FILE_NAME:tgt>
747 Name of the linker generated program database file (.pdb).
748
749 Note that tgt is not added as a dependency of the target this
750 expression is evaluated on (see policy CMP0112).
751
752 $<TARGET_PDB_FILE_DIR:tgt>
753 Directory of the linker generated program database file (.pdb).
754
755 Note that tgt is not added as a dependency of the target this
756 expression is evaluated on (see policy CMP0112).
757
758 $<TARGET_BUNDLE_DIR:tgt>
759 Full path to the bundle directory (my.app, my.framework, or
760 my.bundle) where tgt is the name of a target.
761
762 Note that tgt is not added as a dependency of the target this
763 expression is evaluated on (see policy CMP0112).
764
765 $<TARGET_BUNDLE_CONTENT_DIR:tgt>
766 Full path to the bundle content directory where tgt is the name
767 of a target. For the macOS SDK it leads to my.app/Contents,
768 my.framework, or my.bundle/Contents. For all other SDKs (e.g.
769 iOS) it leads to my.app, my.framework, or my.bundle due to the
770 flat bundle structure.
771
772 Note that tgt is not added as a dependency of the target this
773 expression is evaluated on (see policy CMP0112).
774
775 $<TARGET_PROPERTY:tgt,prop>
776 Value of the property prop on the target tgt.
777
778 Note that tgt is not added as a dependency of the target this
779 expression is evaluated on.
780
781 $<TARGET_PROPERTY:prop>
782 Value of the property prop on the target for which the expres‐
783 sion is being evaluated. Note that for generator expressions in
784 Target Usage Requirements this is the consuming target rather
785 than the target specifying the requirement.
786
787 $<INSTALL_PREFIX>
788 Content of the install prefix when the target is exported via
789 install(EXPORT), or when evaluated in INSTALL_NAME_DIR, and
790 empty otherwise.
791
792 Output-Related Expressions
793 $<TARGET_NAME:...>
794 Marks ... as being the name of a target. This is required if
795 exporting targets to multiple dependent export sets. The ...
796 must be a literal name of a target- it may not contain generator
797 expressions.
798
799 $<LINK_ONLY:...>
800 Content of ... except when evaluated in a link interface while
801 propagating Target Usage Requirements, in which case it is the
802 empty string. Intended for use only in an INTERFACE_LINK_LI‐
803 BRARIES target property, perhaps via the target_link_libraries()
804 command, to specify private link dependencies without other us‐
805 age requirements.
806
807 $<INSTALL_INTERFACE:...>
808 Content of ... when the property is exported using install(EX‐
809 PORT), and empty otherwise.
810
811 $<BUILD_INTERFACE:...>
812 Content of ... when the property is exported using export(), or
813 when the target is used by another target in the same buildsys‐
814 tem. Expands to the empty string otherwise.
815
816 $<MAKE_C_IDENTIFIER:...>
817 Content of ... converted to a C identifier. The conversion fol‐
818 lows the same behavior as string(MAKE_C_IDENTIFIER).
819
820 $<TARGET_OBJECTS:objLib>
821 List of objects resulting from build of objLib.
822
823 $<SHELL_PATH:...>
824 Content of ... converted to shell path style. For example,
825 slashes are converted to backslashes in Windows shells and drive
826 letters are converted to posix paths in MSYS shells. The ...
827 must be an absolute path. The ... may be a semicolon-separated
828 list of paths, in which case each path is converted individually
829 and a result list is generated using the shell path separator (:
830 on POSIX and ; on Windows). Be sure to enclose the argument
831 containing this genex in double quotes in CMake source code so
832 that ; does not split arguments.
833
834 $<OUTPUT_CONFIG:...>
835 New in version 3.20.
836
837
838 Only valid in add_custom_command() and add_custom_target() as
839 the outer-most generator expression in an argument. With the
840 Ninja Multi-Config generator, generator expressions in ... are
841 evaluated using the custom command's "output config". With
842 other generators, the content of ... is evaluated normally.
843
844 $<COMMAND_CONFIG:...>
845 New in version 3.20.
846
847
848 Only valid in add_custom_command() and add_custom_target() as
849 the outer-most generator expression in an argument. With the
850 Ninja Multi-Config generator, generator expressions in ... are
851 evaluated using the custom command's "command config". With
852 other generators, the content of ... is evaluated normally.
853
855 Since generator expressions are evaluated during generation of the
856 buildsystem, and not during processing of CMakeLists.txt files, it is
857 not possible to inspect their result with the message() command.
858
859 One possible way to generate debug messages is to add a custom target,
860
861 add_custom_target(genexdebug COMMAND ${CMAKE_COMMAND} -E echo "$<...>")
862
863 The shell command make genexdebug (invoked after execution of cmake)
864 would then print the result of $<...>.
865
866 Another way is to write debug messages to a file:
867
868 file(GENERATE OUTPUT filename CONTENT "$<...>")
869
871 2000-2021 Kitware, Inc. and Contributors
872
873
874
875
8763.20.3 May 30, 2021 CMAKE-GENERATOR-EXPRESSIONS(7)