In CMake, is there a way to make built-in Find scripts to prioritize a custom directory that we specify? Because especially in windows, module finding scripts usually can't detect the module in, for example visual studio directories. Therefore I usually have to manually set the paths for the external libraries which is pretty tiring. Instead, I want those scripts to look in a custom directory, let's say 'dependencies' folder in the main project first so that I can directly put those externals in that folder which is much easier than putting them into the VC folder or manually setting the paths.
1 Answers
Setting CMAKE_PREFIX_PATH variable serves exactly these purposes: hinting find_*
function about location of searched item.
While description of this variable doesn't note about find_package
function, the variable affects it indirectly: the most of Find<name>.cmake
scripts use find_library and find_path functions. Note, that all find_*
functions have precise algorithm for search items, and paths constructed with CMAKE_PREFIX_PATH
are checked before system ones.
Moreover, CMAKE_PREFIX_PATH
affects some other search procedures. E.g., if 3rd party package provides <name>Config.cmake
script instead of Find<name>.cmake
one, this script is also searched for using this variable. pkg_check_modules also uses CMAKE_PREFIX_PATH
to search for .pc
files describing the package.
CMAKE_PREFIX_PATH
variable can be set as environment one (in platform-depending and usage-specific way), as parameter to cmake
call:
cmake -DCMAKE_PREFIX_PATH=<additional-path> <other-parameters>
or within CMakeLists.txt
file. In the last case it is better to append search directories, so user of you package is able to set the variable too for search packages not shipped with your project:
list(APPEND CMAKE_PREFIX_PATH "${CMAKE_SOURCE_DIR}/dependencies")
Note, that variable CMAKE_PREFIX_PATH
doesn't affect searching for FindXXX.cmake
script itself. For specifying the directory where the script is located, use CMAKE_MODULE_PATH
variable.
-
Thanks, it worked great. It's strange that such an important option is not noted to help Find scripts. Does it have any side effects that would "break" any script? – deniz Jan 15 '16 at 04:24
-
Until directory, listed in `CMAKE_PREFIX_PATH`, has some partial or broken installation, or has unrelated files under standard search suffixes (lib/, lib64/, include/, etc.), setting this variable shoudn't harm any `find` script. And if it does, this is a problem in the `find` script itself. The worst thing which could happen is that script may ignore directory listed in `CMAKE_PREFIX_PATH`. In that case one should check concrete `find` script. – Tsyvarev Jan 15 '16 at 08:51
-
1I would add, that for **only** searching `Find
.cmake` you should use `CMAKE_MODULE_PATH` instead... – Mizux May 02 '19 at 06:07 -
I would like to add, for more information about `Find
.cmake` scripts, I would recommend reading the [find-modules](https://cmake.org/cmake/help/v3.16/manual/cmake-developer.7.html#find-modules) page in the cmake manual. – jrh Jan 24 '20 at 20:47