[CMake] Can I use a *Targets-someName.cmake file to get access to _IMPORT_PREFIX ?

Daniel Oertwig musteresel at gmail.com
Tue Oct 15 07:26:11 EDT 2019


TL;DR:

 - For a project thing I get thingTargets.cmake when I install EXPORT it
 - thingTargets.cmake sets _IMPORT_PREFIX
 - thingTargets.cmake loads files thingTargets-*.cmake

Can I create and install thingTargets-someName.cmake which uses
_IMPORT_PREFIX to "hook" into above mechanism? Or is this likely to break /
change in the future?

Longer version:

I really like that you can use the exported targets of cmake even when
still in a staging directory and not installed. Example:

# in thing project
mkdir thing-build; cd thing-build
cmake .. -DCMAKE_INSTALL_PREFIX=""
make
# creates /path/to/staging/lib/cmake/thing/lib{Config,Targets}.cmake and
friends
make install DESTDIR=/path/to/staging

# in user project
mkdir user-build
cmake .. -Dthing_DIR=/path/to/staging/lib/cmake/thing
make # correctly uses libraries and headers from /path/to/staging/

Looking at /path/to/staging/lib/cmake/thing/libTargets.cmake I can see that
it calculates an _IMPORT_PREFIX variable (in this case to /path/to/staging/
while not yet installed). Also it loads files named libTargets-*.cmake (of
which there's only one for me, namely libTargets-noconfig.cmake).

Can I install a file libTargets-someName.cmake? In this file I'd have
access to the _IMPORT_PREFIX variable to e.g. set paths:

target_link_options(thing::lib
  INTERFACE
  -T ${_IMPORT_PREFIX}/share/link.ld)

Larger example at
https://github.com/musteresel/cmake-multiple-example-research

Best,
Daniel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20191015/28d8fa70/attachment.html>


More information about the CMake mailing list