1add_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() 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 AND COMPILER_RT_HAS_FNO_PROFILE_INSTR_GENERATE_FLAG) 30 list(APPEND try_compile_flags "-fno-profile-instr-generate") 31 endif() 32 endif() 33 34 string(REPLACE ";" " " extra_flags "${try_compile_flags}") 35 36 set(test_compile_command "${CMAKE_C_COMPILE_OBJECT}") 37 foreach(substitution ${substitutions}) 38 if(substitution STREQUAL "<CMAKE_C_COMPILER>") 39 string(REPLACE "<CMAKE_C_COMPILER>" "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}" 40 test_compile_command ${test_compile_command}) 41 elseif(substitution STREQUAL "<OBJECT>") 42 string(REPLACE "<OBJECT>" "${TARGET_NAME}/CheckSectionExists.o" 43 test_compile_command ${test_compile_command}) 44 elseif(substitution STREQUAL "<SOURCE>") 45 string(REPLACE "<SOURCE>" "${TARGET_NAME}/CheckSectionExists.c" 46 test_compile_command ${test_compile_command}) 47 elseif(substitution STREQUAL "<FLAGS>") 48 string(REPLACE "<FLAGS>" "${CMAKE_C_FLAGS} ${extra_flags}" 49 test_compile_command ${test_compile_command}) 50 else() 51 string(REPLACE "${substitution}" "" test_compile_command 52 ${test_compile_command}) 53 endif() 54 endforeach() 55 56 # Strip quotes from the compile command, as the compiler is not expecting 57 # quoted arguments (potential quotes added from D62063). 58 string(REPLACE "\"" "" test_compile_command "${test_compile_command}") 59 60 string(REPLACE " " ";" test_compile_command "${test_compile_command}") 61 62 execute_process( 63 COMMAND ${test_compile_command} 64 RESULT_VARIABLE TEST_RESULT 65 OUTPUT_VARIABLE TEST_OUTPUT 66 ERROR_VARIABLE TEST_ERROR 67 ) 68 69 # Explicitly throw a fatal error message if test_compile_command fails. 70 if(TEST_RESULT) 71 message(FATAL_ERROR "${TEST_ERROR}") 72 return() 73 endif() 74 75 execute_process( 76 COMMAND ${CMAKE_OBJDUMP} -h "${TARGET_NAME}/CheckSectionExists.o" 77 RESULT_VARIABLE CHECK_RESULT 78 OUTPUT_VARIABLE CHECK_OUTPUT 79 ERROR_VARIABLE CHECK_ERROR 80 ) 81 string(FIND "${CHECK_OUTPUT}" "${section}" SECTION_FOUND) 82 83 if(NOT SECTION_FOUND EQUAL -1) 84 set(${output} TRUE PARENT_SCOPE) 85 else() 86 set(${output} FALSE PARENT_SCOPE) 87 endif() 88 89 file(REMOVE_RECURSE ${TARGET_NAME}) 90endfunction() 91 92check_cxx_section_exists(".init_array" COMPILER_RT_HAS_INITFINI_ARRAY 93 SOURCE "volatile int x;\n__attribute__((constructor)) void f() {x = 0;}\nint main() { return 0; }\n") 94 95append_list_if(COMPILER_RT_HAS_STD_C11_FLAG -std=c11 CRT_CFLAGS) 96append_list_if(COMPILER_RT_HAS_INITFINI_ARRAY -DCRT_HAS_INITFINI_ARRAY CRT_CFLAGS) 97append_list_if(COMPILER_RT_CRT_USE_EH_FRAME_REGISTRY -DEH_USE_FRAME_REGISTRY CRT_CFLAGS) 98append_list_if(COMPILER_RT_HAS_FPIC_FLAG -fPIC CRT_CFLAGS) 99append_list_if(COMPILER_RT_HAS_WNO_PEDANTIC -Wno-pedantic CRT_CFLAGS) 100 101foreach(arch ${CRT_SUPPORTED_ARCH}) 102 add_compiler_rt_runtime(clang_rt.crtbegin 103 OBJECT 104 ARCHS ${arch} 105 SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/crtbegin.c 106 CFLAGS ${CRT_CFLAGS} 107 PARENT_TARGET crt) 108 add_compiler_rt_runtime(clang_rt.crtend 109 OBJECT 110 ARCHS ${arch} 111 SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/crtend.c 112 CFLAGS ${CRT_CFLAGS} 113 PARENT_TARGET crt) 114endforeach() 115