[CMake] Creating a relocatable ProjectConfig.cmake when build uses absolute paths

Lucas Soltic lucas.soltic at orange.fr
Wed Jan 10 18:09:49 EST 2018


Hello,

I'm trying to create a relocatable package configuration file but I'm having a hard time with absolute paths that are used during the build.
Note that I use CMake 3.10.0.

First of all for the include path I'm using this:
target_include_directories(MyStaticTarget PUBLIC
                           $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
                           $<INSTALL_INTERFACE:include>)

This does work but according to the doc it should not : https://cmake.org/cmake/help/latest/manual/cmake-generator-expressions.7.html?highlight=build_interface#output-expressions <https://cmake.org/cmake/help/latest/manual/cmake-generator-expressions.7.html?highlight=build_interface#output-expressions>
The doc of BUILD_INTERFACE says: "Content of ... when the property is exported using export(), or when the target is used by another target in the same buildsystem. Expands to the empty string otherwise."
So according to the doc, when compiling the target MyTarget, the include dir I gave should not be used. Which seems consistent with the other uses of INTERFACE wording in CMake language. But it actually behaves like PUBLIC rather than INTERFACE. This is fine to me but inconsistent… :)
When exporting the target, the include dir uses what's given in for INSTALL_INTERFACE so it works fine. No problem on that part.


The second point is about libraries to link when using my project. My project exports a static library that depends on other libraries. These libraries are found (when generating my project through cmake) through calls like find_package(OpenGL). This provides a variable that contains an absolute non-relocatable path.

I've tried doing things like this:
target_link_libraries(MyStaticTarget PRIVATE
                      $<BUILD_INTERFACE:${OpenGL_absolute_path}>
                      $<INSTALL_INTERFACE:${OpenGL_relocatable_link_flag}>)

According to https://cmake.org/pipermail/cmake/2016-May/063400.html <https://cmake.org/pipermail/cmake/2016-May/063400.html>, for static libraries, PRIVATE link dependencies become "PUBLIC", which is ok. And I could see that the link flag for this "private" dependency got exported in the generated config. But the problem is that whatever I put for INSTALL_INTERFACE, it is ignored and not written to the generated cmake config file. The generated CMake config file just takes into account what I put for BUILD_INTERFACE. Contrary to what happened with include directories. And I don't want to use only ${OpenGL_relocatable_link_flag} because MyStaticTarget can be configured to be a dynamic library, in which case the library is really used during link and really want to link against the exact library that's referenced.

How can I have ${OpenGL_relocatable_link_flag} be used in the generated cmake config file?
Any other solution that would make the generated config relocatable is also fine.

Best regards,
Lucas 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20180111/e09b1ec6/attachment.html>


More information about the CMake mailing list