blob: a6bf8644ad5229492f77bc49566ebfb9896c1771 [file] [log] [blame]
Chris Bieneman47839232016-08-01 20:18:18 +00001include(CMakeCheckCompilerFlagCommonPatterns)
Chris Bieneman4e4d4302016-05-03 19:48:11 +00002
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.
5function(try_compile_only output)
Chris Bieneman8a229382016-08-12 01:29:26 +00006 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 Bieneman4e4d4302016-05-03 19:48:11 +000010 set(SIMPLE_C ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/src.c)
Chris Bieneman8a229382016-08-12 01:29:26 +000011 file(WRITE ${SIMPLE_C} "${ARG_SOURCE}\n")
Chris Bieneman4e4d4302016-05-03 19:48:11 +000012 string(REGEX MATCHALL "<[A-Za-z0-9_]*>" substitutions
13 ${CMAKE_C_COMPILE_OBJECT})
Francis Ricci08a86262016-08-25 16:15:45 +000014
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 Bieneman4e4d4302016-05-03 19:48:11 +000021
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 Bieneman47839232016-08-01 20:18:18 +000051
52 CHECK_COMPILER_FLAG_COMMON_PATTERNS(_CheckCCompilerFlag_COMMON_PATTERNS)
Reid Klecknerb5dd5742016-09-08 16:25:34 +000053 set(ERRORS_FOUND OFF)
Chris Bieneman47839232016-08-01 20:18:18 +000054 foreach(var ${_CheckCCompilerFlag_COMMON_PATTERNS})
55 if("${var}" STREQUAL "FAIL_REGEX")
56 continue()
57 endif()
Reid Klecknerb5dd5742016-09-08 16:25:34 +000058 if("${TEST_ERROR}" MATCHES "${var}" OR "${TEST_OUTPUT}" MATCHES "${var}")
59 set(ERRORS_FOUND ON)
Chris Bieneman47839232016-08-01 20:18:18 +000060 endif()
61 endforeach()
62
63 if(result EQUAL 0 AND NOT ERRORS_FOUND)
Chris Bieneman4e4d4302016-05-03 19:48:11 +000064 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()
72endfunction()
73
74function(builtin_check_c_compiler_flag flag output)
Chris Bieneman8512db22016-05-11 20:37:43 +000075 if(NOT DEFINED ${output})
76 message(STATUS "Performing Test ${output}")
Chris Bieneman8a229382016-08-12 01:29:26 +000077 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()
85endfunction()
86
87function(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 Bieneman8512db22016-05-11 20:37:43 +000091 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 Bieneman4e4d4302016-05-03 19:48:11 +000097 endif()
98endfunction()