Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / cmake / Modules / CheckSectionExists.cmake
blobcb32276b5aee136477ebeee47375ce0dba516892
1 function(check_section_exists section output)
2   cmake_parse_arguments(ARG "" "" "SOURCE;FLAGS" ${ARGN})
3   if(NOT ARG_SOURCE)
4     set(ARG_SOURCE "int main(void) { return 0; }\n")
5   endif()
7   string(RANDOM TARGET_NAME)
8   set(TARGET_NAME "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/cmTC_${TARGET_NAME}.dir")
9   file(MAKE_DIRECTORY ${TARGET_NAME})
11   file(WRITE "${TARGET_NAME}/CheckSectionExists.c" "${ARG_SOURCE}\n")
13   string(REGEX MATCHALL "<[A-Za-z0-9_]*>" substitutions
14          ${CMAKE_C_COMPILE_OBJECT})
16   set(try_compile_flags "${ARG_FLAGS}")
17   if(CMAKE_C_COMPILER_ID MATCHES Clang AND CMAKE_C_COMPILER_TARGET)
18     list(APPEND try_compile_flags "-target ${CMAKE_C_COMPILER_TARGET}")
19   endif()
20   append_list_if(COMPILER_RT_HAS_FNO_LTO_FLAG -fno-lto try_compile_flags)
21   if(NOT COMPILER_RT_ENABLE_PGO)
22     if(LLVM_PROFDATA_FILE AND COMPILER_RT_HAS_FNO_PROFILE_INSTR_USE_FLAG)
23       list(APPEND try_compile_flags "-fno-profile-instr-use")
24     endif()
25     if(LLVM_BUILD_INSTRUMENTED MATCHES IR AND COMPILER_RT_HAS_FNO_PROFILE_GENERATE_FLAG)
26       list(APPEND try_compile_flags "-fno-profile-generate")
27     elseif((LLVM_BUILD_INSTRUMENTED OR LLVM_BUILD_INSTRUMENTED_COVERAGE) AND COMPILER_RT_HAS_FNO_PROFILE_INSTR_GENERATE_FLAG)
28       list(APPEND try_compile_flags "-fno-profile-instr-generate")
29       if(LLVM_BUILD_INSTRUMENTED_COVERAGE AND COMPILER_RT_HAS_FNO_COVERAGE_MAPPING_FLAG)
30         list(APPEND try_compile_flags "-fno-coverage-mapping")
31       endif()
32     endif()
33   endif()
35   string(REPLACE ";" " " extra_flags "${try_compile_flags}")
37   set(test_compile_command "${CMAKE_C_COMPILE_OBJECT}")
38   foreach(substitution ${substitutions})
39     if(substitution STREQUAL "<CMAKE_C_COMPILER>")
40       string(REPLACE "<CMAKE_C_COMPILER>" "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}"
41              test_compile_command ${test_compile_command})
42     elseif(substitution STREQUAL "<OBJECT>")
43       string(REPLACE "<OBJECT>" "${TARGET_NAME}/CheckSectionExists.o"
44              test_compile_command ${test_compile_command})
45     elseif(substitution STREQUAL "<SOURCE>")
46       string(REPLACE "<SOURCE>" "${TARGET_NAME}/CheckSectionExists.c"
47              test_compile_command ${test_compile_command})
48     elseif(substitution STREQUAL "<FLAGS>")
49       string(REPLACE "<FLAGS>" "${CMAKE_C_FLAGS} ${extra_flags}"
50              test_compile_command ${test_compile_command})
51     else()
52       string(REPLACE "${substitution}" "" test_compile_command
53              ${test_compile_command})
54     endif()
55   endforeach()
57   # Strip quotes from the compile command, as the compiler is not expecting
58   # quoted arguments (potential quotes added from D62063).
59   string(REPLACE "\"" "" test_compile_command "${test_compile_command}")
61   string(REPLACE " " ";" test_compile_command "${test_compile_command}")
63   execute_process(
64     COMMAND ${test_compile_command}
65     RESULT_VARIABLE TEST_RESULT
66     OUTPUT_VARIABLE TEST_OUTPUT
67     ERROR_VARIABLE TEST_ERROR
68   )
70   # Explicitly throw a fatal error message if test_compile_command fails.
71   if(TEST_RESULT)
72     message(FATAL_ERROR "${TEST_ERROR}")
73     return()
74   endif()
76   execute_process(
77     COMMAND ${CMAKE_OBJDUMP} -h "${TARGET_NAME}/CheckSectionExists.o"
78     RESULT_VARIABLE CHECK_RESULT
79     OUTPUT_VARIABLE CHECK_OUTPUT
80     ERROR_VARIABLE CHECK_ERROR
81   )
82   string(FIND "${CHECK_OUTPUT}" "${section}" SECTION_FOUND)
84   if(NOT SECTION_FOUND EQUAL -1)
85     set(${output} TRUE PARENT_SCOPE)
86   else()
87     set(${output} FALSE PARENT_SCOPE)
88   endif()
90   file(REMOVE_RECURSE ${TARGET_NAME})
91 endfunction()