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