[CMake] Not hardcoding install dir in Config.cmake.

Chris Dembia chris530d at gmail.com
Sun Feb 8 12:30:31 EST 2015


Hey all:

I work on a project that is used heavily on Windows. Our project also
creates a Config.cmake file. During build time, we do not know where the
project will actually be installed on the user's system. However, the
Config.file needs to know the installation directory of the package.
Previously we were hardcoding this, and of course this doesn't let users
choose their own install directory if they are only given our binary
package.

I see that the Targets.cmake file deals with this by computing the
installation directory by using the knowledge of *where* the Targets.cmake
file is within the project. This made me so happy to see! Anyway, this is
how I'm thinking of doing the same thing:

Given CONFIG_INSTALL_DIR (full path location of the Config.cmake file in
the installation), and CMAKE_INSTALL_PREFIX:

# within the project's CMakeLists.txt
set(tmp "${CONFIG_INSTALL_DIR}")
set(counter)
while(NOT "${CMAKE_INSTALL_PREFIX}" STREQUAL "${tmp}")
    get_filename_component(tmp "${tmp}" PATH)
    math(EXPR counter "${counter} + 1")
endwhile()

# within the Config.cmake
set(install_dir "${CMAKE_CURRENT_LIST_FILE}")
foreach(i RANGE @counter@)
    get_filename_component(install_dir "${install_dir}" PATH)
endforeach()

I think this will work. My question is if anyone thinks they have
suggestions for improvements on this. Is there a method to just "subtract"
two paths or to count the number of directories in a relative path? Thank
you!

I also think it would be good if there were instructions in the
documentation about Config.cmake files that showed people how they don't
need to hard-code the install directory if they just know where the
Config.cmake exists within the installation.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20150208/e56b2965/attachment.html>


More information about the CMake mailing list