LINK_OPTIONS and INTERFACE_LINK_OPTIONS properties
For the compiler there are per-target
INTERFACE_COMPILE_OPTIONS and (deprecated)
COMPILE_OPTIONS can be also used per-directory. All of them support generator expressions and they are really powerful and great to use.
For the linker there's just per-target
LINK_FLAGS behaving like the deprecated
COMPILE_FLAGS and nothing else.
I have two use cases:
Findmodule that creates an imported target for some static library. The library needs some interface compiler options, which I'm happily adding through
INTERFACE_COMPILE_OPTIONS, but it also needs some interface linker options and for those currently have to modify the global
CMAKE_*_LINKER_FLAGS, which is really ugly and completely against the modern CMake workflow.
A set of pedantic options passed to compiler/linker in order to harden the builds, which gets enabled only if the user sets a particular property on a target/directory. Currently I'm achieving this through per-directory
COMPILE_DEFINITIONSproperties that get inherited by the targets and uses a generator expression for the conditional enablement, as shown below. Unfortunately there is no per-directory
LINK_OPTIONSproperty that would behave the same way as
COMPILE_OPTIONSso I can't do similar thing for the linker.
set_property(DIRECTORY APPEND PROPERTY COMPILE_OPTIONS "$<$<BOOL:$<TARGET_PROPERTY:USE_PEDANTIC_FLAGS>>:-Wall;-Werror;-Wpedantic; ...>") set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS "$<$<BOOL:$<TARGET_PROPERTY:USE_PEDANTIC_FLAGS>>:...>") add_executable(myapp app.cpp) set_target_properties(myapp PRIVATE USE_PEDANTIC_FLAGS ON) # Enables the pedantic flags and defines
Would it be possible to add per-target and per-directory
LINK_OPTIONS property and per-target
INTERFACE_LINK_OPTIONS property? I tried to find anything about this subject and found just this old e-mail thread, is there any reason that prevents those properties to exist?
EDIT: Oh, and one more:
target_link_options() to make the feature set equivalent to compile options.