How to set $PATH for try_compile runs, and during build?

I’m trying to use CUDA with clang-cl (or just plain cl.exe) on Windows, using Mark Schofield’s Windows toolchain. The toolchain finds cl.exe, but then when CMake tries to find CUDA it fails, because nvcc can’t find compiler cl.exe in $PATH.

I think the same thing happens at build time, because cmake doesn’t set $PATH before invoking ninja.

How can I set my project up so cl.exe is on $PATH both while reading the CMakefiles and when running the build?

I guess the best way on Windows is to open a developer prompt and work from there. That seems to be what cmake is expecting. Oh well.

There’s no mechanism to do so (unless cmake --build $dir is the way to run the build).

Note that CMake expects the environment to support running the compiler as needed. MSVC has this as a persistent requirement and other platforms work because the default environment “works”. However, when using module load on Unix machines or DEVELOPER_DIR= to choose an Xcode toolchain on macOS, the environment needs to be consistent between compiler detection and use. CMake cannot know what all matters from the environment nor how to persist it to the build in a local way (e.g., xcode-select can change default toolchain behaviors on macOS behind CMake’s back).