IC_
IC_

Reputation: 1839

How to limit find_package() search to CMAKE_MODULE_PATH by default?

I want to modularize my project and use find_package to control 3rdparty inclusions and i also have a strict requirement to control all my packages. I don't want cmake to include something on the side and debug what is going on, i need to limit its search path to CMAKE_MODULE_PATH. The single explicit approach i found is NO_DEFAULT_PATH in find_package calls but i don't want to repeat these arguments over and over again. Is there a variable that can control it for me? I found a family of variables CMAKE_FIND_USE_*_PATH but there is no clear documentation on them

Upvotes: 1

Views: 1246

Answers (2)

Tsyvarev
Tsyvarev

Reputation: 66061

CMake documentation describes the algorithm of constructing installation prefixes. By following that algorithm it should be easy to collect all CMAKE_FIND_USE_*_PATH variables, setting which to FALSE will disable corresponding prefixes:

  1. Search paths specified in the <PackageName>_ROOT CMake and environment variables could be omitted by setting CMAKE_FIND_USE_PACKAGE_ROOT_PATH variable.
  2. Search paths specified in cmake-specific cache variables could be omitted by setting CMAKE_FIND_USE_CMAKE_PATH variable.
  3. Search paths specified in cmake-specific environment variables could be omitted by setting CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH variable.
  4. Search paths specified by the HINTS option cannot be disabled.
  5. Search the standard system environment variables could be omitted by setting CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH variable.
  6. Search paths stored in the CMake User Package Registry could be omitted by setting CMAKE_FIND_PACKAGE_NO_PACKAGE_REGISTRY variable.
  7. Search cmake variables defined in the Platform files for the current system could be omitted by setting CMAKE_FIND_USE_CMAKE_SYSTEM_PATH variable.
  8. Search paths stored in the CMake System Package Registry could be omitted by setting CMAKE_FIND_PACKAGE_NO_SYSTEM_PACKAGE_REGISTRY variable.
  9. Search paths specified by the PATHS option cannot be disabled.

In total, setting

set(CMAKE_FIND_USE_PACKAGE_ROOT_PATH FALSE)
set(CMAKE_FIND_USE_CMAKE_PATH FALSE)
set(CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH FALSE)
set(CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH FALSE)
set(CMAKE_FIND_PACKAGE_NO_PACKAGE_REGISTRY FALSE)
set(CMAKE_FIND_USE_CMAKE_SYSTEM_PATH FALSE)
set(CMAKE_FIND_PACKAGE_NO_SYSTEM_PACKAGE_REGISTRY FALSE)

is equivalent to NO_DEFAULT_PATH option in every find_package call.

Upvotes: 3

ixSci
ixSci

Reputation: 13708

When you need to modify some standard behavior of the command you might use functions or macros to hide some details. In this case you need a macro which might look like this:

macro(find_package_restricted)
    find_package(${ARGN} NO_DEFAULT_PATH)
endmacro()

And you use it just like you would use find_package:

find_package_restricted(Boost REQUIRED)

Upvotes: 1

Related Questions