Why does -D on command line work, but -C with file does not?

As part of my ongoing flailing and sanity-eroding attempts to compile clang and libc++ for myself, I tried something like this:

% cmake -G Ninja -C phase2.x86.cmake -S runtimes -B build_phase2_x86

Where that phase2.x86.cmake file contains:

set(PHASE1_BUILD_DIR /Users/rob/Dev/llvm-project/build_phase1)
set(CMAKE_C_COMPILER ${PHASE1_BUILD_DIR}/bin/clang)
set(CMAKE_CXX_COMPILER ${PHASE1_BUILD_DIR}/bin/clang++)
set(CMAKE_ASM_COMPILER ${PHASE1_BUILD_DIR}/bin/llvm-as)
...

But when it runs it ignores that and prints:

-- The C compiler identification is AppleClang 14.0.3.14030022
-- The CXX compiler identification is AppleClang 14.0.3.14030022
...

But if I put them on the command line, it seems to work. Well, it errors later, but that’s another problem.

% cmake -G Ninja -C ~/Documents/KnowledgeBase/llvm/phase2.x86.cmake -S runtimes -B build_phase2_x86 -DCMAKE_CXX_COMPILER=${CL_BIN}/clang++ -DCMAKE_C_COMPILER=${CL_BIN}/clang -DCMAKE_ASM_COMPILER=${CL_BIN}/llvm-as
-- The C compiler identification is Clang 17.0.0
-- The CXX compiler identification is Clang 17.0.0
-- The ASM compiler identification is unknown
-- Found assembler: /Users/rob/Dev/llvm-project/build_phase1/bin/llvm-as
...

Can someone explain what’s going on there?

As specified in the cmake documentation, the set() commands must have the CACHE option…

1 Like