0

Within a CMakeLists.txt file, you can write something like:

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules")

but what if I want to "prime" that variable before the invocation of CMake? Using the environment, perhaps? Is this possible?

The documentation says it is empty by default, but I was hoping to diverge from this default.

einpoklum
  • 118,144
  • 57
  • 340
  • 684

2 Answers2

0

CMAKE_MODULE_PATH is not an "cmake-env-variable". It won't read same name environment variable to initialize it automatically. As an example, CMAKE_EXPORT_COMPILE_COMMANDS can be initialized from env var (ref).

An alternative for your situation:

if(DEFINED ENV{CMAKE_MODULE_PATH})
  set(CMAKE_MODULE_PATH "$ENV{CMAKE_MODULE_PATH}")
else()
  message(WARNING "CMAKE_MODULE_PATH env var not defined, using the default empty one")
endif()
Alex Reinking
  • 16,724
  • 5
  • 52
  • 86
ChrisZZ
  • 1,521
  • 2
  • 17
  • 24
  • The whole point is that I don't want to have to make `CMakeList.txt` changes in my particular scenario. So, I take it your answer is "no you can't"? – einpoklum Jan 18 '22 at 14:01
  • I just use another variable, `ARTIFACTS_DIR`, for my own cross-project, cross-machine, cross-platform dependency(library files, header files) searching. It might be good to have a package manager. – ChrisZZ Jan 18 '22 at 14:04
0

My preferred option would be to pass this to cmake at the time of configuration.

Using command line options

cmake -D "CMAKE_MODULE_PATH:STRING=${CMAKE_MODULE_PATH}" -S ...

or

Using a cache configuration script

# initialCache.cmake

set(CMAKE_MODULE_PATH $ENV{CMAKE_MODULE_PATH} CACHE STRING "path to look for cmake modules")
cmake -C initialCache.cmake -S ...

or

Using a preset

CMakePresets.json

{
  "version": 3,
  "cmakeMinimumRequired": {
    "major": 3,
    "minor": 19,
    "patch": 0
  },
  "configurePresets": [
    {
      "name": "mypreset",
      "displayName": "My preset",
      "description": "Preset using the environment variable to set CMAKE_MODULE_PATH",
      "cacheVariables": {
        "CMAKE_MODULE_PATH": {
          "type": "STRING",
          "value": "$env{CMAKE_MODULE_PATH}"
        }
      }
    }
  ]
}
cmake --preset mypreset -S ...
fabian
  • 80,457
  • 12
  • 86
  • 114