Chris Bieneman | 4783923 | 2016-08-01 20:18:18 +0000 | [diff] [blame] | 1 | include(CMakeCheckCompilerFlagCommonPatterns) |
Chris Bieneman | 4e4d430 | 2016-05-03 19:48:11 +0000 | [diff] [blame] | 2 | |
| 3 | # This function takes an OS and a list of architectures and identifies the |
| 4 | # subset of the architectures list that the installed toolchain can target. |
| 5 | function(try_compile_only output) |
Chris Bieneman | 8a22938 | 2016-08-12 01:29:26 +0000 | [diff] [blame] | 6 | cmake_parse_arguments(ARG "" "" "SOURCE;FLAGS" ${ARGN}) |
| 7 | if(NOT ARG_SOURCE) |
| 8 | set(ARG_SOURCE "int foo(int x, int y) { return x + y; }\n") |
| 9 | endif() |
Chris Bieneman | 4e4d430 | 2016-05-03 19:48:11 +0000 | [diff] [blame] | 10 | set(SIMPLE_C ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/src.c) |
Chris Bieneman | 8a22938 | 2016-08-12 01:29:26 +0000 | [diff] [blame] | 11 | file(WRITE ${SIMPLE_C} "${ARG_SOURCE}\n") |
Chris Bieneman | 4e4d430 | 2016-05-03 19:48:11 +0000 | [diff] [blame] | 12 | string(REGEX MATCHALL "<[A-Za-z0-9_]*>" substitutions |
| 13 | ${CMAKE_C_COMPILE_OBJECT}) |
Francis Ricci | 08a8626 | 2016-08-25 16:15:45 +0000 | [diff] [blame] | 14 | |
| 15 | set(TRY_COMPILE_FLAGS "${ARG_FLAGS}") |
| 16 | if(CMAKE_C_COMPILER_ID MATCHES Clang AND CMAKE_C_COMPILER_TARGET) |
| 17 | list(APPEND TRY_COMPILE_FLAGS "-target ${CMAKE_C_COMPILER_TARGET}") |
| 18 | endif() |
| 19 | |
| 20 | string(REPLACE ";" " " extra_flags "${TRY_COMPILE_FLAGS}") |
Chris Bieneman | 4e4d430 | 2016-05-03 19:48:11 +0000 | [diff] [blame] | 21 | |
| 22 | set(test_compile_command "${CMAKE_C_COMPILE_OBJECT}") |
| 23 | foreach(substitution ${substitutions}) |
| 24 | if(substitution STREQUAL "<CMAKE_C_COMPILER>") |
| 25 | string(REPLACE "<CMAKE_C_COMPILER>" |
| 26 | "${CMAKE_C_COMPILER}" test_compile_command ${test_compile_command}) |
| 27 | elseif(substitution STREQUAL "<OBJECT>") |
| 28 | string(REPLACE "<OBJECT>" |
| 29 | "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/test.o" |
| 30 | test_compile_command ${test_compile_command}) |
| 31 | elseif(substitution STREQUAL "<SOURCE>") |
| 32 | string(REPLACE "<SOURCE>" "${SIMPLE_C}" test_compile_command |
| 33 | ${test_compile_command}) |
| 34 | elseif(substitution STREQUAL "<FLAGS>") |
| 35 | string(REPLACE "<FLAGS>" "${CMAKE_C_FLAGS} ${extra_flags}" |
| 36 | test_compile_command ${test_compile_command}) |
| 37 | else() |
| 38 | string(REPLACE "${substitution}" "" test_compile_command |
| 39 | ${test_compile_command}) |
| 40 | endif() |
| 41 | endforeach() |
| 42 | |
| 43 | string(REPLACE " " ";" test_compile_command "${test_compile_command}") |
| 44 | |
| 45 | execute_process( |
| 46 | COMMAND ${test_compile_command} |
| 47 | RESULT_VARIABLE result |
| 48 | OUTPUT_VARIABLE TEST_OUTPUT |
| 49 | ERROR_VARIABLE TEST_ERROR |
| 50 | ) |
Chris Bieneman | 4783923 | 2016-08-01 20:18:18 +0000 | [diff] [blame] | 51 | |
| 52 | CHECK_COMPILER_FLAG_COMMON_PATTERNS(_CheckCCompilerFlag_COMMON_PATTERNS) |
Reid Kleckner | b5dd574 | 2016-09-08 16:25:34 +0000 | [diff] [blame] | 53 | set(ERRORS_FOUND OFF) |
Chris Bieneman | 4783923 | 2016-08-01 20:18:18 +0000 | [diff] [blame] | 54 | foreach(var ${_CheckCCompilerFlag_COMMON_PATTERNS}) |
| 55 | if("${var}" STREQUAL "FAIL_REGEX") |
| 56 | continue() |
| 57 | endif() |
Reid Kleckner | b5dd574 | 2016-09-08 16:25:34 +0000 | [diff] [blame] | 58 | if("${TEST_ERROR}" MATCHES "${var}" OR "${TEST_OUTPUT}" MATCHES "${var}") |
| 59 | set(ERRORS_FOUND ON) |
Chris Bieneman | 4783923 | 2016-08-01 20:18:18 +0000 | [diff] [blame] | 60 | endif() |
| 61 | endforeach() |
| 62 | |
| 63 | if(result EQUAL 0 AND NOT ERRORS_FOUND) |
Chris Bieneman | 4e4d430 | 2016-05-03 19:48:11 +0000 | [diff] [blame] | 64 | set(${output} True PARENT_SCOPE) |
| 65 | else() |
| 66 | file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log |
| 67 | "Testing compiler for supporting " ${ARGN} ":\n" |
| 68 | "Command: ${test_compile_command}\n" |
| 69 | "${TEST_OUTPUT}\n${TEST_ERROR}\n${result}\n") |
| 70 | set(${output} False PARENT_SCOPE) |
| 71 | endif() |
| 72 | endfunction() |
| 73 | |
| 74 | function(builtin_check_c_compiler_flag flag output) |
Chris Bieneman | 8512db2 | 2016-05-11 20:37:43 +0000 | [diff] [blame] | 75 | if(NOT DEFINED ${output}) |
| 76 | message(STATUS "Performing Test ${output}") |
Chris Bieneman | 8a22938 | 2016-08-12 01:29:26 +0000 | [diff] [blame] | 77 | try_compile_only(result FLAGS ${flag}) |
| 78 | set(${output} ${result} CACHE INTERNAL "Compiler supports ${flag}") |
| 79 | if(${result}) |
| 80 | message(STATUS "Performing Test ${output} - Success") |
| 81 | else() |
| 82 | message(STATUS "Performing Test ${output} - Failed") |
| 83 | endif() |
| 84 | endif() |
| 85 | endfunction() |
| 86 | |
| 87 | function(builtin_check_c_compiler_source output source) |
| 88 | if(NOT DEFINED ${output}) |
| 89 | message(STATUS "Performing Test ${output}") |
| 90 | try_compile_only(result SOURCE ${source}) |
Chris Bieneman | 8512db2 | 2016-05-11 20:37:43 +0000 | [diff] [blame] | 91 | set(${output} ${result} CACHE INTERNAL "Compiler supports ${flag}") |
| 92 | if(${result}) |
| 93 | message(STATUS "Performing Test ${output} - Success") |
| 94 | else() |
| 95 | message(STATUS "Performing Test ${output} - Failed") |
| 96 | endif() |
Chris Bieneman | 4e4d430 | 2016-05-03 19:48:11 +0000 | [diff] [blame] | 97 | endif() |
| 98 | endfunction() |