I want to design some cmake cache entry as alias from the automake/autoconf like CC
to CMAKE_C_COMPILER
, CXX
to CMAKE_CXX_COMPILER
, CFLAGS
to CMAKE_C_FLAGS
etc, but cmake always ignore my passed -D
entrys on the compiler names insert to the first configuration.
First of all, I’m working on Windows. So the command line given below is in VS2022 pwsh.
I’ve add this to the top-level CMakeLists.txt:
cmake_minimum_required(VERSION 3.5)
if(DEFINED CC)
set(CMAKE_C_COMPILER ${CC} CACHE STRING "C Compiler" Force)
endif()
if(DEFINED CXX)
set(CMAKE_CXX_COMPILER ${CXX} CACHE STRING "C++ Compiler" Force)
endif()
.
.
.
.
When I pass cmake command for using clang-cl
or gcc
/g++
:
cmake -S . -B build -G Ninja -DCC=clang-cl -DCXX=clang-cl
cmake -S . -B build -G Ninja -DCC=gcc -DCXX=g++
It always shows identification is cl.exe
. This means cmake didn’t get my force entry C/CXX compiler:
-- The C compiler identification is MSVC 19.40.33811.0
-- The CXX compiler identification is MSVC 19.40.33811.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Developer/Microsoft/VisualStudio/2022/Community/VC/Tools/MSVC/14.40.33807/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Developer/Microsoft/VisualStudio/2022/Community/VC/Tools/MSVC/14.40.33807/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for sys/types.h
.
.
.
I need to re-run the same cmake command and cmake changes the output:
You have changed variables that require your cache to be deleted.
Configure will be re-run and you may have to reset some variables.
The following variables have changed:
CMAKE_C_COMPILER= gcc
CMAKE_CXX_COMPILER= g++
-- The C compiler identification is GNU 13.1.0
-- The CXX compiler identification is GNU 13.1.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Developer/Strawberry/c/bin/gcc.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Developer/Strawberry/c/bin/g++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
I’ve checked the variable_name-variable_value relation(VAR -> ${VAR}), checked the variable is cached entry define from cmake command, even I tried on Linux has the same result.
How can I let cmake get my alias into the first configuration?