Petr Hosek | 999bb5a | 2019-04-30 18:13:22 +0000 | [diff] [blame] | 1 | add_compiler_rt_component(crt) |
| 2 | |
| 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 | |
| 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}) |
| 12 | |
| 13 | file(WRITE "${TARGET_NAME}/CheckSectionExists.c" "${ARG_SOURCE}\n") |
| 14 | |
| 15 | string(REGEX MATCHALL "<[A-Za-z0-9_]*>" substitutions |
| 16 | ${CMAKE_C_COMPILE_OBJECT}) |
| 17 | |
| 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() |
Hans Wennborg | 9e8b011 | 2019-08-20 07:41:14 +0000 | [diff] [blame] | 22 | append_list_if(COMPILER_RT_HAS_FNO_LTO_FLAG -fno-lto try_compile_flags) |
Petr Hosek | 999bb5a | 2019-04-30 18:13:22 +0000 | [diff] [blame] | 23 | |
| 24 | string(REPLACE ";" " " extra_flags "${try_compile_flags}") |
| 25 | |
| 26 | set(test_compile_command "${CMAKE_C_COMPILE_OBJECT}") |
| 27 | foreach(substitution ${substitutions}) |
| 28 | if(substitution STREQUAL "<CMAKE_C_COMPILER>") |
Petr Hosek | cd78209 | 2019-05-19 03:29:15 +0000 | [diff] [blame] | 29 | string(REPLACE "<CMAKE_C_COMPILER>" "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}" |
| 30 | test_compile_command ${test_compile_command}) |
Petr Hosek | 999bb5a | 2019-04-30 18:13:22 +0000 | [diff] [blame] | 31 | elseif(substitution STREQUAL "<OBJECT>") |
| 32 | string(REPLACE "<OBJECT>" "${TARGET_NAME}/CheckSectionExists.o" |
| 33 | test_compile_command ${test_compile_command}) |
| 34 | elseif(substitution STREQUAL "<SOURCE>") |
| 35 | string(REPLACE "<SOURCE>" "${TARGET_NAME}/CheckSectionExists.c" |
| 36 | test_compile_command ${test_compile_command}) |
| 37 | elseif(substitution STREQUAL "<FLAGS>") |
| 38 | string(REPLACE "<FLAGS>" "${CMAKE_C_FLAGS} ${extra_flags}" |
| 39 | test_compile_command ${test_compile_command}) |
| 40 | else() |
| 41 | string(REPLACE "${substitution}" "" test_compile_command |
| 42 | ${test_compile_command}) |
| 43 | endif() |
| 44 | endforeach() |
| 45 | |
| 46 | string(REPLACE " " ";" test_compile_command "${test_compile_command}") |
| 47 | |
| 48 | execute_process( |
| 49 | COMMAND ${test_compile_command} |
| 50 | RESULT_VARIABLE TEST_RESULT |
| 51 | OUTPUT_VARIABLE TEST_OUTPUT |
| 52 | ERROR_VARIABLE TEST_ERROR |
| 53 | ) |
| 54 | |
| 55 | execute_process( |
| 56 | COMMAND ${CMAKE_OBJDUMP} -h "${TARGET_NAME}/CheckSectionExists.o" |
| 57 | RESULT_VARIABLE CHECK_RESULT |
| 58 | OUTPUT_VARIABLE CHECK_OUTPUT |
| 59 | ERROR_VARIABLE CHECK_ERROR |
| 60 | ) |
| 61 | string(FIND "${CHECK_OUTPUT}" "${section}" SECTION_FOUND) |
| 62 | |
| 63 | if(NOT SECTION_FOUND EQUAL -1) |
| 64 | set(${output} TRUE PARENT_SCOPE) |
| 65 | else() |
| 66 | set(${output} FALSE PARENT_SCOPE) |
| 67 | endif() |
| 68 | |
| 69 | file(REMOVE_RECURSE ${TARGET_NAME}) |
| 70 | endfunction() |
| 71 | |
| 72 | check_cxx_section_exists(".init_array" COMPILER_RT_HAS_INITFINI_ARRAY |
Jian Cai | 155a43e | 2019-09-16 21:47:47 +0000 | [diff] [blame] | 73 | SOURCE "volatile int x;\n__attribute__((constructor)) void f() {x = 0;}\nint main() { return 0; }\n") |
Petr Hosek | 999bb5a | 2019-04-30 18:13:22 +0000 | [diff] [blame] | 74 | |
Petr Hosek | fc86c7f | 2019-05-10 19:23:56 +0000 | [diff] [blame] | 75 | append_list_if(COMPILER_RT_HAS_STD_C11_FLAG -std=c11 CRT_CFLAGS) |
Petr Hosek | 999bb5a | 2019-04-30 18:13:22 +0000 | [diff] [blame] | 76 | append_list_if(COMPILER_RT_HAS_INITFINI_ARRAY -DCRT_HAS_INITFINI_ARRAY CRT_CFLAGS) |
| 77 | append_list_if(COMPILER_RT_HAS_FPIC_FLAG -fPIC CRT_CFLAGS) |
Petr Hosek | 3e28075 | 2019-05-01 07:00:27 +0000 | [diff] [blame] | 78 | append_list_if(COMPILER_RT_HAS_WNO_PEDANTIC -Wno-pedantic CRT_CFLAGS) |
Petr Hosek | 999bb5a | 2019-04-30 18:13:22 +0000 | [diff] [blame] | 79 | |
| 80 | foreach(arch ${CRT_SUPPORTED_ARCH}) |
| 81 | add_compiler_rt_runtime(clang_rt.crtbegin |
| 82 | OBJECT |
| 83 | ARCHS ${arch} |
| 84 | SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/crtbegin.c |
| 85 | CFLAGS ${CRT_CFLAGS} |
| 86 | PARENT_TARGET crt) |
| 87 | add_compiler_rt_runtime(clang_rt.crtend |
| 88 | OBJECT |
| 89 | ARCHS ${arch} |
| 90 | SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/crtend.c |
| 91 | CFLAGS ${CRT_CFLAGS} |
| 92 | PARENT_TARGET crt) |
| 93 | endforeach() |