xref: /aosp_15_r20/external/compiler-rt/cmake/Modules/BuiltinTests.cmake (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot
2*7c3d14c8STreehugger Robot# This function takes an OS and a list of architectures and identifies the
3*7c3d14c8STreehugger Robot# subset of the architectures list that the installed toolchain can target.
4*7c3d14c8STreehugger Robotfunction(try_compile_only output)
5*7c3d14c8STreehugger Robot  set(SIMPLE_C ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/src.c)
6*7c3d14c8STreehugger Robot  file(WRITE ${SIMPLE_C} "int foo(int x, int y) { return x + y; }\n")
7*7c3d14c8STreehugger Robot  string(REGEX MATCHALL "<[A-Za-z0-9_]*>" substitutions
8*7c3d14c8STreehugger Robot         ${CMAKE_C_COMPILE_OBJECT})
9*7c3d14c8STreehugger Robot  string(REPLACE ";" " " extra_flags "${ARGN}")
10*7c3d14c8STreehugger Robot
11*7c3d14c8STreehugger Robot  set(test_compile_command "${CMAKE_C_COMPILE_OBJECT}")
12*7c3d14c8STreehugger Robot  foreach(substitution ${substitutions})
13*7c3d14c8STreehugger Robot    if(substitution STREQUAL "<CMAKE_C_COMPILER>")
14*7c3d14c8STreehugger Robot      string(REPLACE "<CMAKE_C_COMPILER>"
15*7c3d14c8STreehugger Robot             "${CMAKE_C_COMPILER}" test_compile_command ${test_compile_command})
16*7c3d14c8STreehugger Robot    elseif(substitution STREQUAL "<OBJECT>")
17*7c3d14c8STreehugger Robot      string(REPLACE "<OBJECT>"
18*7c3d14c8STreehugger Robot             "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/test.o"
19*7c3d14c8STreehugger Robot             test_compile_command ${test_compile_command})
20*7c3d14c8STreehugger Robot    elseif(substitution STREQUAL "<SOURCE>")
21*7c3d14c8STreehugger Robot      string(REPLACE "<SOURCE>" "${SIMPLE_C}" test_compile_command
22*7c3d14c8STreehugger Robot             ${test_compile_command})
23*7c3d14c8STreehugger Robot    elseif(substitution STREQUAL "<FLAGS>")
24*7c3d14c8STreehugger Robot      string(REPLACE "<FLAGS>" "${CMAKE_C_FLAGS} ${extra_flags}"
25*7c3d14c8STreehugger Robot             test_compile_command ${test_compile_command})
26*7c3d14c8STreehugger Robot    else()
27*7c3d14c8STreehugger Robot      string(REPLACE "${substitution}" "" test_compile_command
28*7c3d14c8STreehugger Robot             ${test_compile_command})
29*7c3d14c8STreehugger Robot    endif()
30*7c3d14c8STreehugger Robot  endforeach()
31*7c3d14c8STreehugger Robot
32*7c3d14c8STreehugger Robot  string(REPLACE " " ";" test_compile_command "${test_compile_command}")
33*7c3d14c8STreehugger Robot
34*7c3d14c8STreehugger Robot  execute_process(
35*7c3d14c8STreehugger Robot    COMMAND ${test_compile_command}
36*7c3d14c8STreehugger Robot    RESULT_VARIABLE result
37*7c3d14c8STreehugger Robot    OUTPUT_VARIABLE TEST_OUTPUT
38*7c3d14c8STreehugger Robot    ERROR_VARIABLE TEST_ERROR
39*7c3d14c8STreehugger Robot  )
40*7c3d14c8STreehugger Robot  if(result EQUAL 0)
41*7c3d14c8STreehugger Robot    set(${output} True PARENT_SCOPE)
42*7c3d14c8STreehugger Robot  else()
43*7c3d14c8STreehugger Robot    file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
44*7c3d14c8STreehugger Robot        "Testing compiler for supporting " ${ARGN} ":\n"
45*7c3d14c8STreehugger Robot        "Command: ${test_compile_command}\n"
46*7c3d14c8STreehugger Robot        "${TEST_OUTPUT}\n${TEST_ERROR}\n${result}\n")
47*7c3d14c8STreehugger Robot    set(${output} False PARENT_SCOPE)
48*7c3d14c8STreehugger Robot  endif()
49*7c3d14c8STreehugger Robotendfunction()
50*7c3d14c8STreehugger Robot
51*7c3d14c8STreehugger Robotfunction(builtin_check_c_compiler_flag flag output)
52*7c3d14c8STreehugger Robot  if(NOT DEFINED ${output})
53*7c3d14c8STreehugger Robot    message(STATUS "Performing Test ${output}")
54*7c3d14c8STreehugger Robot    try_compile_only(result ${flag})
55*7c3d14c8STreehugger Robot    set(${output} ${result} CACHE INTERNAL "Compiler supports ${flag}")
56*7c3d14c8STreehugger Robot    if(${result})
57*7c3d14c8STreehugger Robot      message(STATUS "Performing Test ${output} - Success")
58*7c3d14c8STreehugger Robot    else()
59*7c3d14c8STreehugger Robot      message(STATUS "Performing Test ${output} - Failed")
60*7c3d14c8STreehugger Robot    endif()
61*7c3d14c8STreehugger Robot  endif()
62*7c3d14c8STreehugger Robotendfunction()
63