|
1 |
| -if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR) |
2 |
| - cmake_minimum_required(VERSION 3.13.4) |
| 1 | +add_compiler_rt_component(crt) |
3 | 2 |
|
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() |
8 | 8 |
|
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}) |
10 | 12 |
|
11 |
| - set(LLVM_COMMON_CMAKE_UTILS "${COMPILER_RT_SOURCE_DIR}/../cmake") |
| 13 | + file(WRITE "${TARGET_NAME}/CheckSectionExists.c" "${ARG_SOURCE}\n") |
12 | 14 |
|
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}) |
20 | 17 |
|
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() |
23 | 36 |
|
24 |
| - load_llvm_config() |
25 |
| - construct_compiler_rt_default_triple() |
| 37 | + string(REPLACE ";" " " extra_flags "${try_compile_flags}") |
26 | 38 |
|
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() |
30 | 58 |
|
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}") |
32 | 62 |
|
33 |
| -if(COMPILER_RT_HAS_CRT) |
34 |
| - add_compiler_rt_component(crt) |
| 63 | + string(REPLACE " " ";" test_compile_command "${test_compile_command}") |
35 | 64 |
|
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 | + ) |
39 | 71 |
|
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() |
47 | 76 | endif()
|
48 | 77 |
|
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) |
63 | 105 | 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() |
0 commit comments