Setting toolchain on command line and compiler in file, causes loop and failure?

I think I’ve found a way around this for now, but I’m curious why this happens. Maybe it’s a bug?

I tried to use vcpkg, and their docs say to invoke cmake like this, specifying the toolchain file:

cmake -B build-debug -S . -DCMAKE_TOOLCHAIN_FILE=/MyStuff/vcpkg/scripts/buildsystems/vcpkg.cmake

I’m trying to use clang 15, so in my root CMakeLists.txt I did this:

set(CMAKE_CXX_COMPILER /Path/to/clang+llvm-15.0.1-x86_64-apple-darwin/bin/clang++)

This causes the cmake run to repeat itself and then barf an error where it can’t find a dependency:

CMake Error at CMakeLists.txt:29 (find_package):  
  By not providing "Findrange-v3.cmake" in CMAKE_MODULE_PATH this project has  
  asked CMake to find a package configuration file provided by "range-v3",
  but CMake did not find one.

  Could not find a package configuration file provided by "range-v3" with any
  of the following names:

    range-v3Config.cmake
    range-v3-config.cmake

But if I specify everything on command line, it all seems to work fine.

cmake -B build-debug -S . 
   -DCMAKE_TOOLCHAIN_FILE=/MyStuff/vcpkg/scripts/buildsystems/vcpkg.cmake 
   -DCMAKE_CXX_COMPILER=/path/to/clang/bin/clang++ 
   -DCMAKE_C_COMPILER=/path/to/clang/bin/clang

Setting the compiler in the project file doesn’t usually go well. If you really want to do it, before any project() call would be better (and is why the command line works).

This is because CMake already found a compiler by this point and your setting just confuses things because introspection has already happened.