Skip to content

Commit 45344cf

Browse files
committed
[CMake][compiler-rt][libunwind] Compile assembly files as ASM not C, unify workarounds
It isn't very wise to pass an assembly file to the compiler and tell it to compile as a C file and hope that the compiler recognizes it as assembly instead. Simply don't mark the file as C and CMake will recognize the rest. This was attempted earlier in https://reviews.llvm.org/D85706, but reverted due to architecture issues on Apple. Subsequent digging revealed a similar change was done earlier for libunwind in https://reviews.llvm.org/rGb780df052dd2b246a760d00e00f7de9ebdab9d09. Afterwards workarounds were added for MinGW and Apple: * https://reviews.llvm.org/rGb780df052dd2b246a760d00e00f7de9ebdab9d09 * https://reviews.llvm.org/rGd4ded05ba851304b26a437896bc3962ef56f62cb The workarounds in libunwind and compiler-rt are unified and comments added pointing to each other. The workaround is updated to only be used for MinGW for CMake versions before 3.17, which fixed the issue (https://gitlab.kitware.com/cmake/cmake/-/merge_requests/4287). Additionally fixed Clang not being passed as the assembly compiler for compiler-rt runtime build. Example error: [525/634] Building C object lib/tsan/CMakeFiles/clang_rt.tsan-aarch64.dir/rtl/tsan_rtl_aarch64.S.o FAILED: lib/tsan/CMakeFiles/clang_rt.tsan-aarch64.dir/rtl/tsan_rtl_aarch64.S.o /opt/tooling/drive/host/bin/clang --target=aarch64-linux-gnu -I/opt/tooling/drive/llvm/compiler-rt/lib/tsan/.. -isystem /opt/tooling/drive/toolchain/opt/drive/toolchain/include -x c -Wall -Wno-unused-parameter -fno-lto -fPIC -fno-builtin -fno-exceptions -fomit-frame-pointer -funwind-tables -fno-stack-protector -fno-sanitize=safe-stack -fvisibility=hidden -fno-lto -O3 -gline-tables-only -Wno-gnu -Wno-variadic-macros -Wno-c99-extensions -Wno-non-virtual-dtor -fPIE -fno-rtti -Wframe-larger-than=530 -Wglobal-constructors --sysroot=. -MD -MT lib/tsan/CMakeFiles/clang_rt.tsan-aarch64.dir/rtl/tsan_rtl_aarch64.S.o -MF lib/tsan/CMakeFiles/clang_rt.tsan-aarch64.dir/rtl/tsan_rtl_aarch64.S.o.d -o lib/tsan/CMakeFiles/clang_rt.tsan-aarch64.dir/rtl/tsan_rtl_aarch64.S.o -c /opt/tooling/drive/llvm/compiler-rt/lib/tsan/rtl/tsan_rtl_aarch64.S /opt/tooling/drive/llvm/compiler-rt/lib/tsan/rtl/tsan_rtl_aarch64.S:29:1: error: expected identifier or '(' .section .text ^ 1 error generated. Differential Revision: https://reviews.llvm.org/D86308
1 parent 45eeb8c commit 45344cf

File tree

3 files changed

+10
-13
lines changed

3 files changed

+10
-13
lines changed

clang/runtime/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ if(LLVM_BUILD_EXTERNAL_COMPILER_RT AND EXISTS ${COMPILER_RT_SRC_ROOT}/)
7575
CMAKE_ARGS ${CLANG_COMPILER_RT_CMAKE_ARGS}
7676
-DCMAKE_C_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang
7777
-DCMAKE_CXX_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang++
78+
-DCMAKE_ASM_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang
7879
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
7980
-DCMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM}
8081
-DLLVM_CONFIG_PATH=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-config

compiler-rt/cmake/Modules/AddCompilerRT.cmake

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,11 @@ endfunction()
109109

110110
function(add_asm_sources output)
111111
set(${output} ${ARGN} PARENT_SCOPE)
112-
# Xcode will try to compile asm files as C ('clang -x c'), and that will fail.
113-
if (${CMAKE_GENERATOR} STREQUAL "Xcode")
114-
enable_language(ASM)
115-
else()
116-
# Pass ASM file directly to the C++ compiler.
112+
# CMake doesn't pass the correct architecture for Apple prior to CMake 3.19. https://gitlab.kitware.com/cmake/cmake/-/issues/20771
113+
# MinGW didn't work correctly with assembly prior to CMake 3.17. https://gitlab.kitware.com/cmake/cmake/-/merge_requests/4287 and https://reviews.llvm.org/rGb780df052dd2b246a760d00e00f7de9ebdab9d09
114+
# Workaround these two issues by compiling as C.
115+
# Same workaround used in libunwind. Also update there if changed here.
116+
if((APPLE AND CMAKE_VERSION VERSION_LESS 3.19) OR (MINGW AND CMAKE_VERSION VERSION_LESS 3.17))
117117
set_source_files_properties(${ARGN} PROPERTIES LANGUAGE C)
118118
endif()
119119
endfunction()

libunwind/src/CMakeLists.txt

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,10 @@ set(LIBUNWIND_ASM_SOURCES
2424
UnwindRegistersRestore.S
2525
UnwindRegistersSave.S
2626
)
27-
if (MINGW OR APPLE)
28-
# CMake doesn't build assembly sources for windows/gnu targets properly
29-
# (up to current CMake, 3.16), so treat them as C files.
30-
# Additionally, CMake ignores OSX_ARCHITECTURE for ASM files when targeting
31-
# Apple platforms.
32-
set_source_files_properties(${LIBUNWIND_ASM_SOURCES}
33-
PROPERTIES
34-
LANGUAGE C)
27+
28+
# See add_asm_sources() in compiler-rt for explanation of this workaround.
29+
if((APPLE AND CMAKE_VERSION VERSION_LESS 3.19) OR (MINGW AND CMAKE_VERSION VERSION_LESS 3.17))
30+
set_source_files_properties(${LIBUNWIND_ASM_SOURCES} PROPERTIES LANGUAGE C)
3531
endif()
3632

3733
set(LIBUNWIND_HEADERS

0 commit comments

Comments
 (0)