[CMake] Interface Libraries allow include directories but not link directories.. Why?

Eric Noulard eric.noulard at gmail.com
Wed Aug 23 08:51:55 EDT 2017


2017-08-23 14:10 GMT+02:00 Jean-Michaël Celerier <
jeanmichael.celerier at gmail.com>:

> > - Says that custom functions such as add_{project}_library shouldn't be
> used and function definitions should be used as little as possible. Except
> this just leads to extremely verbose CMakeLists where repeated properties
> are defined again and again and again.
>
> I also never understood how to handle this.
>
> I have a project where I want to define, say, -fsanitize=address,
> -DFOO_BAR and the SUFFIX property on a specific set of 25 targets amongst
> ~100 targets. What am I to do ?
>

I have some similar need.


>
> * set(CMAKE_CXX_FLAGS "-fsanitize=address") is "not modern CMake" (and
> also would be harder to set / unset on specific targets).
> * calling target_compile_options(...) 25 times ... well I mean, everyone
> knows it's bad to duplicate code. Especially if the change is meant to be
> only when a specific option() is enabled, or for debugging purposes
> * creating a function that would set the correct flags, etc and then call
> this function for each target is apparently "not modern CMake" either
>

Why is it not "modern" ? You can leverage meta-informations offered by
CMake in DIRECTORY and TARGET properties.

My solution is close to this one, I have written one macro (let's call it
myproj_sanitize) which takes as input
an inclusion REGEX for exe, an inclusion REGEX for lib, an exclusion REGEX
for exe/lib.
So that you only have a single call (near the end of the main
CMakeLists.txt where I know every targets have been defined)

myproj_sanitize(
    EXCLUDE_EXE_REGEX ""
    INCLUDE_EXE_REGEX ""
    EXCLUDE_LIB_REGEX ""
    INCLUDE_LIB_REGEX ""
              )

The algorihtm of this macros is simple:

S1) collect all SHARED and STATUS LIBRARY and EXECUTABLES targets
      by
      examining the BUILDSYSTEM_TARGETS directory property
      and looping over all directories from the main one.
      At the end of this step I have 2 lists one for executable one for
library

S2) apply exclusion and inclusion regexes for the one which are not empty.

S3) Call add_sanitizer (from https://github.com/arsenm/sanitizers-cmake)
      on the remaining targets.

I can can easily share the code of the step S1 of the macro. Once we have a
mean
to get the list of concerned targets the remaining part is easy.

I was afraid of the collect processing time because the concerned project
has 500+ libraries
and 800+ executables but  this is done in a couple of seconds and this is
only done
when compiling in "sanitize" mode which is not ON by default.

.
> * creating and linking to "dummy" INTERFACE targets with the flags and
> properties I want have an awful lot of limitations
>
> So what is the right course of actions here ?
>
> Ideally I'd like to add "groups" to targets; e.g. "target Foo is a plugin
> for $software", "target Bar is an integration test" and set per-group
> options, flags, properties, etc. Like
>
>     add_group(PluginGroup)
>     target_compile_definitions(PluginGroup -DBLAH)
>     set_property(GROUP PluginGroup PROPERTIES /* whatever in
> cmake-properties*/)
>     set_group(myTarget PluginGroup) // applies everything to the target
>

I think this approach is already possible with not so much CMake scripting:

group_add(PluginGroup) --> create a bunch of global properties for internal
group machinery.

G_PluginGroup_targets --> the list of targets belonging to the group
G_PluginGroup_compile_definitions
G_PluginGroup_properties --> the list of (property,value) pairs for the
group

group_compile_definitions(PluginGroup -DBLAH) append
to G_PluginGroup_compile_definitions
group_set_properties(PluginGroup PROPERTIES /* whatever in
cmake-properties*/) append to G_PluginGroup_properties

group_set(TARGETS myTarget1 myTarget2) // applies previously defined group
things to the concerned targets.

The "only" limitation would be that you have to define the
meta-informations for the groups before using group_set on any
targets OR you need group_process macro called neat the end of the main
CMakeLists.txt

-- 
Eric
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20170823/903156a1/attachment.html>


More information about the CMake mailing list