Skip to content

Commit 1558cdd

Browse files
committed
Revert "[CMake][compiler-rt] Make CRT separately buildable"
This reverts commit b89b18e since it broke the sanitizer bots.
1 parent b89b18e commit 1558cdd

File tree

9 files changed

+117
-195
lines changed

9 files changed

+117
-195
lines changed

compiler-rt/cmake/Modules/AllSupportedArchDefs.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ set(ALL_SANITIZER_COMMON_SUPPORTED_ARCH ${X86} ${X86_64} ${PPC64} ${RISCV64}
2727
${HEXAGON})
2828
set(ALL_ASAN_SUPPORTED_ARCH ${X86} ${X86_64} ${ARM32} ${ARM64} ${RISCV64}
2929
${MIPS32} ${MIPS64} ${PPC64} ${S390X} ${SPARC} ${SPARCV9} ${HEXAGON})
30+
set(ALL_CRT_SUPPORTED_ARCH ${X86} ${X86_64} ${ARM32} ${ARM64} ${PPC32}
31+
${PPC64} ${RISCV32} ${RISCV64} ${VE} ${HEXAGON})
3032
set(ALL_DFSAN_SUPPORTED_ARCH ${X86_64} ${MIPS64} ${ARM64})
3133

3234
if(ANDROID)

compiler-rt/cmake/Modules/CheckSectionExists.cmake

Lines changed: 0 additions & 91 deletions
This file was deleted.

compiler-rt/cmake/Modules/CompilerRTUtils.cmake

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,10 +238,6 @@ function(get_compiler_rt_root_source_dir ROOT_DIR_VAR)
238238
# Compiler-RT Builtins standalone build.
239239
# `llvm-project/compiler-rt/lib/builtins`
240240
set(PATH_TO_COMPILER_RT_SOURCE_ROOT "${CompilerRTBuiltins_SOURCE_DIR}/../../")
241-
elseif (DEFINED CompilerRTCRT_SOURCE_DIR)
242-
# Compiler-RT CRT standalone build.
243-
# `llvm-project/compiler-rt/lib/crt`
244-
set(PATH_TO_COMPILER_RT_SOURCE_ROOT "${CompilerRTCRT_SOURCE_DIR}/../../")
245241
elseif(DEFINED CompilerRT_SOURCE_DIR)
246242
# Compiler-RT standalone build.
247243
# `llvm-project/compiler-rt`

compiler-rt/cmake/config-ix.cmake

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,7 @@ if(APPLE)
612612
SANITIZER_COMMON_SUPPORTED_ARCH)
613613

614614
else()
615+
filter_available_targets(CRT_SUPPORTED_ARCH ${ALL_CRT_SUPPORTED_ARCH})
615616
# Architectures supported by compiler-rt libraries.
616617
filter_available_targets(SANITIZER_COMMON_SUPPORTED_ARCH
617618
${ALL_SANITIZER_COMMON_SUPPORTED_ARCH})
@@ -708,6 +709,12 @@ endif()
708709

709710
# TODO: Add builtins support.
710711

712+
if (CRT_SUPPORTED_ARCH AND OS_NAME MATCHES "Linux" AND NOT LLVM_USE_SANITIZER)
713+
set(COMPILER_RT_HAS_CRT TRUE)
714+
else()
715+
set(COMPILER_RT_HAS_CRT FALSE)
716+
endif()
717+
711718
if (COMPILER_RT_HAS_SANITIZER_COMMON AND DFSAN_SUPPORTED_ARCH AND
712719
OS_NAME MATCHES "Linux")
713720
set(COMPILER_RT_HAS_DFSAN TRUE)

compiler-rt/cmake/crt-config-ix.cmake

Lines changed: 0 additions & 47 deletions
This file was deleted.

compiler-rt/lib/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ if(COMPILER_RT_BUILD_BUILTINS)
1717
add_subdirectory(builtins)
1818
endif()
1919

20-
if(COMPILER_RT_BUILD_CRT)
20+
if(COMPILER_RT_BUILD_CRT AND COMPILER_RT_HAS_CRT)
2121
add_subdirectory(crt)
2222
endif()
2323

compiler-rt/lib/crt/CMakeLists.txt

Lines changed: 106 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,120 @@
1-
if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
2-
cmake_minimum_required(VERSION 3.13.4)
1+
add_compiler_rt_component(crt)
32

4-
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
5-
project(CompilerRTCRT C)
6-
set(COMPILER_RT_STANDALONE_BUILD TRUE)
7-
set(COMPILER_RT_CRT_STANDALONE_BUILD TRUE)
3+
function(check_cxx_section_exists section output)
4+
cmake_parse_arguments(ARG "" "" "SOURCE;FLAGS" ${ARGN})
5+
if(NOT ARG_SOURCE)
6+
set(ARG_SOURCE "int main() { return 0; }\n")
7+
endif()
88

9-
set(COMPILER_RT_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../..")
9+
string(RANDOM TARGET_NAME)
10+
set(TARGET_NAME "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/cmTC_${TARGET_NAME}.dir")
11+
file(MAKE_DIRECTORY ${TARGET_NAME})
1012

11-
set(LLVM_COMMON_CMAKE_UTILS "${COMPILER_RT_SOURCE_DIR}/../cmake")
13+
file(WRITE "${TARGET_NAME}/CheckSectionExists.c" "${ARG_SOURCE}\n")
1214

13-
# Add path for custom modules
14-
list(INSERT CMAKE_MODULE_PATH 0
15-
"${COMPILER_RT_SOURCE_DIR}/cmake"
16-
"${COMPILER_RT_SOURCE_DIR}/cmake/Modules"
17-
"${LLVM_COMMON_CMAKE_UTILS}"
18-
"${LLVM_COMMON_CMAKE_UTILS}/Modules"
19-
)
15+
string(REGEX MATCHALL "<[A-Za-z0-9_]*>" substitutions
16+
${CMAKE_C_COMPILE_OBJECT})
2017

21-
include(base-config-ix)
22-
include(CompilerRTUtils)
18+
set(try_compile_flags "${ARG_FLAGS}")
19+
if(CMAKE_C_COMPILER_ID MATCHES Clang AND CMAKE_C_COMPILER_TARGET)
20+
list(APPEND try_compile_flags "-target ${CMAKE_C_COMPILER_TARGET}")
21+
endif()
22+
append_list_if(COMPILER_RT_HAS_FNO_LTO_FLAG -fno-lto try_compile_flags)
23+
if(NOT COMPILER_RT_ENABLE_PGO)
24+
if(LLVM_PROFDATA_FILE AND COMPILER_RT_HAS_FNO_PROFILE_INSTR_USE_FLAG)
25+
list(APPEND try_compile_flags "-fno-profile-instr-use")
26+
endif()
27+
if(LLVM_BUILD_INSTRUMENTED MATCHES IR AND COMPILER_RT_HAS_FNO_PROFILE_GENERATE_FLAG)
28+
list(APPEND try_compile_flags "-fno-profile-generate")
29+
elseif((LLVM_BUILD_INSTRUMENTED OR LLVM_BUILD_INSTRUMENTED_COVERAGE) AND COMPILER_RT_HAS_FNO_PROFILE_INSTR_GENERATE_FLAG)
30+
list(APPEND try_compile_flags "-fno-profile-instr-generate")
31+
if(LLVM_BUILD_INSTRUMENTED_COVERAGE AND COMPILER_RT_HAS_FNO_COVERAGE_MAPPING_FLAG)
32+
list(APPEND try_compile_flags "-fno-coverage-mapping")
33+
endif()
34+
endif()
35+
endif()
2336

24-
load_llvm_config()
25-
construct_compiler_rt_default_triple()
37+
string(REPLACE ";" " " extra_flags "${try_compile_flags}")
2638

27-
include(SetPlatformToolchainTools)
28-
include(AddCompilerRT)
29-
endif()
39+
set(test_compile_command "${CMAKE_C_COMPILE_OBJECT}")
40+
foreach(substitution ${substitutions})
41+
if(substitution STREQUAL "<CMAKE_C_COMPILER>")
42+
string(REPLACE "<CMAKE_C_COMPILER>" "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}"
43+
test_compile_command ${test_compile_command})
44+
elseif(substitution STREQUAL "<OBJECT>")
45+
string(REPLACE "<OBJECT>" "${TARGET_NAME}/CheckSectionExists.o"
46+
test_compile_command ${test_compile_command})
47+
elseif(substitution STREQUAL "<SOURCE>")
48+
string(REPLACE "<SOURCE>" "${TARGET_NAME}/CheckSectionExists.c"
49+
test_compile_command ${test_compile_command})
50+
elseif(substitution STREQUAL "<FLAGS>")
51+
string(REPLACE "<FLAGS>" "${CMAKE_C_FLAGS} ${extra_flags}"
52+
test_compile_command ${test_compile_command})
53+
else()
54+
string(REPLACE "${substitution}" "" test_compile_command
55+
${test_compile_command})
56+
endif()
57+
endforeach()
3058

31-
include(crt-config-ix)
59+
# Strip quotes from the compile command, as the compiler is not expecting
60+
# quoted arguments (potential quotes added from D62063).
61+
string(REPLACE "\"" "" test_compile_command "${test_compile_command}")
3262

33-
if(COMPILER_RT_HAS_CRT)
34-
add_compiler_rt_component(crt)
63+
string(REPLACE " " ";" test_compile_command "${test_compile_command}")
3564

36-
include(CheckSectionExists)
37-
check_section_exists(".init_array" COMPILER_RT_HAS_INITFINI_ARRAY
38-
SOURCE "volatile int x;\n__attribute__((constructor)) void f() {x = 0;}\nint main() { return 0; }\n")
65+
execute_process(
66+
COMMAND ${test_compile_command}
67+
RESULT_VARIABLE TEST_RESULT
68+
OUTPUT_VARIABLE TEST_OUTPUT
69+
ERROR_VARIABLE TEST_ERROR
70+
)
3971

40-
append_list_if(COMPILER_RT_HAS_STD_C11_FLAG -std=c11 CRT_CFLAGS)
41-
append_list_if(COMPILER_RT_HAS_INITFINI_ARRAY -DCRT_HAS_INITFINI_ARRAY CRT_CFLAGS)
42-
append_list_if(COMPILER_RT_CRT_USE_EH_FRAME_REGISTRY -DEH_USE_FRAME_REGISTRY CRT_CFLAGS)
43-
append_list_if(COMPILER_RT_HAS_FPIC_FLAG -fPIC CRT_CFLAGS)
44-
append_list_if(COMPILER_RT_HAS_WNO_PEDANTIC -Wno-pedantic CRT_CFLAGS)
45-
if (COMPILER_RT_HAS_FCF_PROTECTION_FLAG)
46-
append_list_if(COMPILER_RT_ENABLE_CET -fcf-protection=full CRT_CFLAGS)
72+
# Explicitly throw a fatal error message if test_compile_command fails.
73+
if(TEST_RESULT)
74+
message(FATAL_ERROR "${TEST_ERROR}")
75+
return()
4776
endif()
4877

49-
foreach(arch ${CRT_SUPPORTED_ARCH})
50-
add_compiler_rt_runtime(clang_rt.crtbegin
51-
OBJECT
52-
ARCHS ${arch}
53-
SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/crtbegin.c
54-
CFLAGS ${CRT_CFLAGS}
55-
PARENT_TARGET crt)
56-
add_compiler_rt_runtime(clang_rt.crtend
57-
OBJECT
58-
ARCHS ${arch}
59-
SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/crtend.c
60-
CFLAGS ${CRT_CFLAGS}
61-
PARENT_TARGET crt)
62-
endforeach()
78+
execute_process(
79+
COMMAND ${CMAKE_OBJDUMP} -h "${TARGET_NAME}/CheckSectionExists.o"
80+
RESULT_VARIABLE CHECK_RESULT
81+
OUTPUT_VARIABLE CHECK_OUTPUT
82+
ERROR_VARIABLE CHECK_ERROR
83+
)
84+
string(FIND "${CHECK_OUTPUT}" "${section}" SECTION_FOUND)
85+
86+
if(NOT SECTION_FOUND EQUAL -1)
87+
set(${output} TRUE PARENT_SCOPE)
88+
else()
89+
set(${output} FALSE PARENT_SCOPE)
90+
endif()
91+
92+
file(REMOVE_RECURSE ${TARGET_NAME})
93+
endfunction()
94+
95+
check_cxx_section_exists(".init_array" COMPILER_RT_HAS_INITFINI_ARRAY
96+
SOURCE "volatile int x;\n__attribute__((constructor)) void f() {x = 0;}\nint main() { return 0; }\n")
97+
98+
append_list_if(COMPILER_RT_HAS_STD_C11_FLAG -std=c11 CRT_CFLAGS)
99+
append_list_if(COMPILER_RT_HAS_INITFINI_ARRAY -DCRT_HAS_INITFINI_ARRAY CRT_CFLAGS)
100+
append_list_if(COMPILER_RT_CRT_USE_EH_FRAME_REGISTRY -DEH_USE_FRAME_REGISTRY CRT_CFLAGS)
101+
append_list_if(COMPILER_RT_HAS_FPIC_FLAG -fPIC CRT_CFLAGS)
102+
append_list_if(COMPILER_RT_HAS_WNO_PEDANTIC -Wno-pedantic CRT_CFLAGS)
103+
if (COMPILER_RT_HAS_FCF_PROTECTION_FLAG)
104+
append_list_if(COMPILER_RT_ENABLE_CET -fcf-protection=full CRT_CFLAGS)
63105
endif()
106+
107+
foreach(arch ${CRT_SUPPORTED_ARCH})
108+
add_compiler_rt_runtime(clang_rt.crtbegin
109+
OBJECT
110+
ARCHS ${arch}
111+
SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/crtbegin.c
112+
CFLAGS ${CRT_CFLAGS}
113+
PARENT_TARGET crt)
114+
add_compiler_rt_runtime(clang_rt.crtend
115+
OBJECT
116+
ARCHS ${arch}
117+
SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/crtend.c
118+
CFLAGS ${CRT_CFLAGS}
119+
PARENT_TARGET crt)
120+
endforeach()

compiler-rt/test/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ if(COMPILER_RT_CAN_EXECUTE_TESTS)
9292
if(COMPILER_RT_BUILD_ORC)
9393
compiler_rt_Test_runtime(orc)
9494
endif()
95-
if(COMPILER_RT_BUILD_CRT)
95+
if(COMPILER_RT_BUILD_CRT AND COMPILER_RT_HAS_CRT)
9696
add_subdirectory(crt)
9797
endif()
9898
# ShadowCallStack does not yet provide a runtime with compiler-rt, the tests

compiler-rt/test/crt/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
include(crt-config-ix)
2-
31
set(CRT_LIT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
42

53
set(CRT_TESTSUITES)

0 commit comments

Comments
 (0)