<div dir="ltr">Hey all:<div><br></div><div>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.</div><div><br></div><div>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:</div><div><br></div><div>Given CONFIG_INSTALL_DIR (full path location of the Config.cmake file in the installation), and CMAKE_INSTALL_PREFIX:</div><div><br></div><div># within the project's CMakeLists.txt</div><div>set(tmp "${CONFIG_INSTALL_DIR}")</div><div>set(counter)</div><div>while(NOT "${CMAKE_INSTALL_PREFIX}" STREQUAL "${tmp}")</div><div>    get_filename_component(tmp "${tmp}" PATH)</div><div>    math(EXPR counter "${counter} + 1")</div><div>endwhile()</div><div><br></div><div># within the Config.cmake</div><div>set(install_dir "${CMAKE_CURRENT_LIST_FILE}")</div><div>foreach(i RANGE @counter@)</div><div>    get_filename_component(install_dir "${install_dir}" PATH)<br></div><div>endforeach()</div><div><br></div><div>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!</div><div><br></div><div>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.</div><div><br></div></div>