Compiler arguments during first CMake invocation
It was noted in #19743 that there are inconsistencies and missing documentation for setting compiler flags that get used during the first invocation of CMake and the compiler detection and tests.
When setting the environment CC
or setting CMAKE_<LANG>_COMPILER
additional flags get stored in CMAKE_${lang}_COMPILER_ARG1
and will be used during first invocation compiler detection and tests. This is stored as a cache variable but also in CMakeFiles/version/CMake<LANG>Compiler.cmake
in the build directory. It is always used subsequent builds.
The environment CC
can be given multiple flags like gcc -flag1 -flag2
and store -flag1 -flag2
. CMAKE_<LANG>_COMPILER
only accepts a single flag for this purpose. It checks to see if it is a list size of 2.
CMAKE_<LANG>_FLAGS_INIT
specifically states that it is meant to be set by a toolchain file
. But it is ignored during the compiler detection and tests but is used in the Check for working
step.
CMAKE_<LANG>_FLAGS
can be used in a toolchain file or ENV{CFLAGS}
can be used and it will be used during the compiler detection and tests.
I'd suggest the following:
- Update the documentation about using
ENV{CC}
andCMAKE_<LANG>_COMPILER
as a list but clarify it's use is meant for mandatory arguments that the compiler needs to function properly. - Remove the list size check for
CMAKE_<LANG>_COMPILER
and have it behave the same way asENV{CC}
. - Allow
CMAKE_<LANG>_FLAGS_INIT
to be considered during the compiler detection and tests steps.
When using a compiler like ccintppc
from GHS that requires a couple of mandatory flags before it will compile anything (or even accepts --help
) not being able to use CMAKE_<LANG>_FLAGS_INIT
leads to a confusing situation.