Enabling CUDA fails in the presence of certain environment variables
Enabling CUDA fails with an unhelpful error message "Couldn't find CUDA library root." in the presence of environment variables with names ending with TOP
. The culprit is the inspection of nvcc
verbose output which searches for the regex TOP=([^\r\n]*)
to determine the variable CMAKE_CUDA_COMPILER_TOOLKIT_ROOT
. nvcc
verbose output apparently outputs the environment some time before the sought-after TOP=
line. I'm running CMake 3.20.2 but this method of CMAKE_CUDA_COMPILER_TOOLKIT_ROOT
determination is present in all 3.20.x versions. I'm not sure if older versions of nvcc
also output the environment. On my Windows 10.0.19041 system, nvcc --version
reports:
Built on Mon_May__3_19:41:42_Pacific_Daylight_Time_2021
Cuda compilation tools, release 11.3, V11.3.109
Build cuda_11.3.r11.3/compiler.29920130_0
In my case, I was running CMake from a terminal in VSCode. This apparently sets the environment variable ORIGINAL_XDG_CURRENT_DESKTOP
which causes the clash. So this issue could be quite common. A temporary workaround is to remove all environment variables ending in TOP
and hoping you'll be alright.
I'm not sure about nvcc
output stability guarantees and whether inspecting verbose output is the best approach in the long run. Changing the regex to \<TOP=([^\r\n]*)
(added word boundary at the beginning) should work for now for most cases but would still fail if there was an environment variable called exactly TOP
. In the future, maybe drop the verbose output inspection altogether or allow it to fail and fall back to inferring CMAKE_CUDA_COMPILER_TOOLKIT_ROOT
from the path to the nvcc
executable.