blob: 90e94b93db4774d2cfe089711c1fe9a442fbbc96 [file] [log] [blame]
Petr Hosek999bb5a2019-04-30 18:13:22 +00001add_compiler_rt_component(crt)
2
3function(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 Wennborg9e8b0112019-08-20 07:41:14 +000022 append_list_if(COMPILER_RT_HAS_FNO_LTO_FLAG -fno-lto try_compile_flags)
Petr Hosek999bb5a2019-04-30 18:13:22 +000023
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 Hosekcd782092019-05-19 03:29:15 +000029 string(REPLACE "<CMAKE_C_COMPILER>" "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}"
30 test_compile_command ${test_compile_command})
Petr Hosek999bb5a2019-04-30 18:13:22 +000031 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})
70endfunction()
71
72check_cxx_section_exists(".init_array" COMPILER_RT_HAS_INITFINI_ARRAY
Jian Cai155a43e2019-09-16 21:47:47 +000073 SOURCE "volatile int x;\n__attribute__((constructor)) void f() {x = 0;}\nint main() { return 0; }\n")
Petr Hosek999bb5a2019-04-30 18:13:22 +000074
Petr Hosekfc86c7f2019-05-10 19:23:56 +000075append_list_if(COMPILER_RT_HAS_STD_C11_FLAG -std=c11 CRT_CFLAGS)
Petr Hosek999bb5a2019-04-30 18:13:22 +000076append_list_if(COMPILER_RT_HAS_INITFINI_ARRAY -DCRT_HAS_INITFINI_ARRAY CRT_CFLAGS)
77append_list_if(COMPILER_RT_HAS_FPIC_FLAG -fPIC CRT_CFLAGS)
Petr Hosek3e280752019-05-01 07:00:27 +000078append_list_if(COMPILER_RT_HAS_WNO_PEDANTIC -Wno-pedantic CRT_CFLAGS)
Petr Hosek999bb5a2019-04-30 18:13:22 +000079
80foreach(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)
93endforeach()