CMAKE_ASM_COMPILE_OPTIONS_EXTERNAL_TOOLCHAIN set incorrectly with Clang
The CMAKE_${LANG}_COMPILE_OPTIONS_EXTERNAL_TOOLCHAIN
flags are set to --gcc-toolchain=
for current versions of Clang and -gcc-toolchain
for versions older than Clang 3.4.0.
Clang doesn't have an program with a GAS-like CLI syntax and instead reuses the C/C++ compiler driver to assemble files.
When CMake uses Clang to assemble, it sets CMAKE_ASM_COMPILE_OPTIONS_EXTERNAL_TOOLCHAIN
to -gcc-toolchain
regardless of the Clang version. This is a problem because new versions of Clang removed support for -gcc-toolchain
in favor of --gcc-toolchain=
. https://llvm.org/D108494
CMAKE_ASM_COMPILER_VERSION
is left blank rather than set to match the C/C++ variables.
A test case:
CMakeLists.txt
cmake_minimum_required(VERSION 3.21.0)
project(test C CXX ASM)
message(STATUS "CMAKE_ASM_COMPILER_VERSION = ${CMAKE_ASM_COMPILER_VERSION}")
message(STATUS "CMAKE_C_COMPILER_VERSION = ${CMAKE_C_COMPILER_VERSION}")
message(STATUS "CMAKE_CXX_COMPILER_VERSION = ${CMAKE_CXX_COMPILER_VERSION}")
message(STATUS "CMAKE_ASM_COMPILE_OPTIONS_EXTERNAL_TOOLCHAIN = ${CMAKE_ASM_COMPILE_OPTIONS_EXTERNAL_TOOLCHAIN}")
message(STATUS "CMAKE_C_COMPILE_OPTIONS_EXTERNAL_TOOLCHAIN = ${CMAKE_C_COMPILE_OPTIONS_EXTERNAL_TOOLCHAIN}")
message(STATUS "CMAKE_CXX_COMPILE_OPTIONS_EXTERNAL_TOOLCHAIN = ${CMAKE_CXX_COMPILE_OPTIONS_EXTERNAL_TOOLCHAIN}")
message(STATUS "CMAKE_ASM_COMPILER_EXTERNAL_TOOLCHAIN = ${CMAKE_ASM_COMPILER_EXTERNAL_TOOLCHAIN}")
message(STATUS "CMAKE_C_COMPILER_EXTERNAL_TOOLCHAIN = ${CMAKE_C_COMPILER_EXTERNAL_TOOLCHAIN}")
message(STATUS "CMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN = ${CMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN}")
Output:
$ CC=clang CXX=clang++ cmake -GNinja .
-- The C compiler identification is Clang 11.1.0
-- The CXX compiler identification is Clang 11.1.0
-- The ASM compiler identification is Clang
-- Found assembler: /usr/bin/clang
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/clang - 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: /usr/bin/clang++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- CMAKE_ASM_COMPILER_VERSION =
-- CMAKE_C_COMPILER_VERSION = 11.1.0
-- CMAKE_CXX_COMPILER_VERSION = 11.1.0
-- CMAKE_ASM_COMPILE_OPTIONS_EXTERNAL_TOOLCHAIN = -gcc-toolchain
-- CMAKE_C_COMPILE_OPTIONS_EXTERNAL_TOOLCHAIN = --gcc-toolchain=
-- CMAKE_CXX_COMPILE_OPTIONS_EXTERNAL_TOOLCHAIN = --gcc-toolchain=
-- CMAKE_ASM_COMPILER_EXTERNAL_TOOLCHAIN =
-- CMAKE_C_COMPILER_EXTERNAL_TOOLCHAIN =
-- CMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN =
-- Configuring done
-- Generating done
-- Build files have been written to: /x/mess/A
I'm using CMake from the cmake-3.21.4-1 debian package. I've also noticed it with a local build of CMake 3.22.1.
This issue currently breaks Android's "legacy toolchain file" (as opposed to its newer toolchain file). The legacy file happens to set the CMAKE_{ASM,C,CXX}_COMPILER_EXTERNAL_TOOLCHAIN
variables, so it's hitting this general CMake+Clang issue. I don't know if this toolchain file needs to set those variables, so maybe removing that setting is an easy fix in future NDK releases.
References: