C compiler tests ignore CMAKE_EXE_LINKER_FLAGS unless set in toolchain file
Tested with CMake 3.12.1.
I am attempting to use a C compiler that does not produce valid binaries without custom linker flags.
If these flags are set in a toolchain file, these flags will be passed to the initial C compiler test and will pass successfully.
If these flags are set in the top level CMakeLists.txt, these flags will not be passed to the initial C compiler test.
An example of this can be tested as following:
toolchain.cmake
set(CMAKE_SYSTEM_NAME "Generic")
set(CMAKE_SYSTEM_PROCESSOR "Arm")
set(CMAKE_C_COMPILER "/usr/bin/arm-none-eabi-gcc")
set(CMAKE_CXX_COMPILER "/usr/bin/arm-none-eabi-g++")
CMakeLists.txt
project(LinkTest C)
cmake_minimum_required(VERSION 3.5)
set(CMAKE_EXE_LINKER_FLAGS "-Wl,-nostdlib")
Invoking CMake on the above example will fail with the following error:
$ cmake . -DCMAKE_TOOLCHAIN_FILE=toolchain.cmake --debug-trycompile
debug trycompile on
-- The C compiler identification is GNU 6.3.1
-- Check for working C compiler: /usr/bin/arm-none-eabi-gcc
-- Check for working C compiler: /usr/bin/arm-none-eabi-gcc -- broken
CMake Error at /usr/share/cmake-3.12/Modules/CMakeTestCCompiler.cmake:52 (message):
The C compiler
"/usr/bin/arm-none-eabi-gcc"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: /tmp/cmake/test/CMakeFiles/CMakeTmp
Run Build Command:"/usr/bin/make" "cmTC_ccd73/fast"
/usr/bin/make -f CMakeFiles/cmTC_ccd73.dir/build.make CMakeFiles/cmTC_ccd73.dir/build
make[1]: Entering directory '/tmp/cmake/test/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_ccd73.dir/testCCompiler.c.o
/usr/bin/arm-none-eabi-gcc -o CMakeFiles/cmTC_ccd73.dir/testCCompiler.c.o -c /tmp/cmake/test/CMakeFiles/CMakeTmp/testCCompiler.c
Linking C executable cmTC_ccd73
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_ccd73.dir/link.txt --verbose=1
/usr/bin/arm-none-eabi-gcc -rdynamic CMakeFiles/cmTC_ccd73.dir/testCCompiler.c.o -o cmTC_ccd73
arm-none-eabi-gcc: error: unrecognized command line option '-rdynamic'
make[1]: *** [CMakeFiles/cmTC_ccd73.dir/build.make:87: cmTC_ccd73] Error 1
make[1]: Leaving directory '/tmp/cmake/test/CMakeFiles/CMakeTmp'
make: *** [Makefile:121: cmTC_ccd73/fast] Error 2
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:1 (project)
-- Configuring incomplete, errors occurred!
See also "/tmp/cmake/test/CMakeFiles/CMakeOutput.log".
See also "/tmp/cmake/test/CMakeFiles/CMakeError.log".
However, if I modify the above example to add the following line: toolchain.cmake
set(CMAKE_SYSTEM_NAME "Generic")
set(CMAKE_SYSTEM_PROCESSOR "Arm")
set(CMAKE_C_COMPILER "/usr/bin/arm-none-eabi-gcc")
set(CMAKE_CXX_COMPILER "/usr/bin/arm-none-eabi-g++")
set(CMAKE_EXE_LINKER_FLAGS "-Wl,-nostdlib")
I get the following output:
$ cmake . -DCMAKE_TOOLCHAIN_FILE=toolchain.cmake --debug-trycompile
debug trycompile on
-- The C compiler identification is GNU 6.3.1
-- Check for working C compiler: /usr/bin/arm-none-eabi-gcc
-- Check for working C compiler: /usr/bin/arm-none-eabi-gcc -- broken
CMake Error at /usr/share/cmake-3.12/Modules/CMakeTestCCompiler.cmake:52 (message):
The C compiler
"/usr/bin/arm-none-eabi-gcc"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: /tmp/cmake/test/CMakeFiles/CMakeTmp
Run Build Command:"/usr/bin/make" "cmTC_7bca3/fast"
/usr/bin/make -f CMakeFiles/cmTC_7bca3.dir/build.make CMakeFiles/cmTC_7bca3.dir/build
make[1]: Entering directory '/tmp/cmake/test/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_7bca3.dir/testCCompiler.c.o
/usr/bin/arm-none-eabi-gcc -o CMakeFiles/cmTC_7bca3.dir/testCCompiler.c.o -c /tmp/cmake/test/CMakeFiles/CMakeTmp/testCCompiler.c
Linking C executable cmTC_7bca3
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_7bca3.dir/link.txt --verbose=1
/usr/bin/arm-none-eabi-gcc -Wl,-nostdlib -rdynamic CMakeFiles/cmTC_7bca3.dir/testCCompiler.c.o -o cmTC_7bca3
arm-none-eabi-gcc: error: unrecognized command line option '-rdynamic'
make[1]: *** [CMakeFiles/cmTC_7bca3.dir/build.make:87: cmTC_7bca3] Error 1
make[1]: Leaving directory '/tmp/cmake/test/CMakeFiles/CMakeTmp'
make: *** [Makefile:121: cmTC_7bca3/fast] Error 2
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:1 (project)
-- Configuring incomplete, errors occurred!
See also "/tmp/cmake/test/CMakeFiles/CMakeOutput.log".
See also "/tmp/cmake/test/CMakeFiles/CMakeError.log".
In this example, my linker flag -Wl,-nostdlib
have successfully been passed down to the compiler test.
Judging from previous experience with CMake and cached variables, I would expect that flags set in toolchain files may not be picked up on the initial processing of CMake, but this appears to be the inverse.
Whilst I know that the command CMAKE_TRY_COMPILE_TARGET_TYPE
is available, this does not solve the problem as I want to ensure that binaries can be produced (including linking step).