1 include(CMakeCheckCompilerFlagCommonPatterns)
3 # Test compiler can compile simple C/C++/Objective-C program without invoking
9 # [FLAGS flag_0 [ flag_1 ]]
12 # OUTPUT_VAR - The variable name to store the result. The result is a boolean
15 # SOURCE - Optional. If specified use source the source text string
16 # specified. If not specified source code will be used that is C,
17 # C++, and Objective-C compatible.
19 # FLAGS - Optional. If specified pass the one or more specified flags to
24 # try_compile_only(HAS_F_NO_RTTI FLAGS "-fno-rtti")
26 # try_compile_only(HAS_CXX_AUTO_TYPE_DECL
27 # SOURCE "int foo(int x) { auto y = x + 1; return y;}"
28 # FLAGS "-x" "c++" "-std=c++11" "-Werror=c++11-extensions"
31 function(try_compile_only output)
32 # NOTE: `SOURCE` needs to be a multi-argument because source code
33 # often contains semicolons which happens to be CMake's list separator
34 # which confuses `cmake_parse_arguments()`.
35 cmake_parse_arguments(ARG "" "" "SOURCE;FLAGS" ${ARGN})
36 if (ARG_UNPARSED_ARGUMENTS)
37 message(FATAL_ERROR "Unexpected arguments \"${ARG_UNPARSED_ARGUMENTS}\"")
40 set(ARG_SOURCE "int foo(int x, int y) { return x + y; }\n")
42 set(SIMPLE_C ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/src.c)
43 file(WRITE ${SIMPLE_C} "${ARG_SOURCE}\n")
44 string(REGEX MATCHALL "<[A-Za-z0-9_]*>" substitutions
45 ${CMAKE_C_COMPILE_OBJECT})
47 set(TRY_COMPILE_FLAGS "${ARG_FLAGS}")
48 if(CMAKE_C_COMPILER_ID MATCHES Clang AND CMAKE_C_COMPILER_TARGET)
49 list(APPEND TRY_COMPILE_FLAGS "--target=${CMAKE_C_COMPILER_TARGET}")
52 string(REPLACE ";" " " extra_flags "${TRY_COMPILE_FLAGS}")
54 set(test_compile_command "${CMAKE_C_COMPILE_OBJECT}")
55 foreach(substitution ${substitutions})
56 if(substitution STREQUAL "<CMAKE_C_COMPILER>")
57 string(REPLACE "<CMAKE_C_COMPILER>"
58 "${CMAKE_C_COMPILER}" test_compile_command ${test_compile_command})
59 elseif(substitution STREQUAL "<OBJECT>")
60 string(REPLACE "<OBJECT>"
61 "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/test.o"
62 test_compile_command ${test_compile_command})
63 elseif(substitution STREQUAL "<SOURCE>")
64 string(REPLACE "<SOURCE>" "${SIMPLE_C}" test_compile_command
65 ${test_compile_command})
66 elseif(substitution STREQUAL "<FLAGS>")
67 string(REPLACE "<FLAGS>" "${CMAKE_C_FLAGS} ${extra_flags}"
68 test_compile_command ${test_compile_command})
70 string(REPLACE "${substitution}" "" test_compile_command
71 ${test_compile_command})
75 # Strip quotes from the compile command, as the compiler is not expecting
76 # quoted arguments (see discussion on D62063 for when this can come up). If
77 # the quotes were there for arguments with spaces in them, the quotes were
78 # not going to help since the string gets split on spaces below.
79 string(REPLACE "\"" "" test_compile_command "${test_compile_command}")
81 string(REPLACE " " ";" test_compile_command "${test_compile_command}")
84 COMMAND ${test_compile_command}
85 RESULT_VARIABLE result
86 OUTPUT_VARIABLE TEST_OUTPUT
87 ERROR_VARIABLE TEST_ERROR
90 CHECK_COMPILER_FLAG_COMMON_PATTERNS(_CheckCCompilerFlag_COMMON_PATTERNS)
92 foreach(var ${_CheckCCompilerFlag_COMMON_PATTERNS})
93 if("${var}" STREQUAL "FAIL_REGEX")
96 if("${TEST_ERROR}" MATCHES "${var}" OR "${TEST_OUTPUT}" MATCHES "${var}")
101 if(result EQUAL 0 AND NOT ERRORS_FOUND)
102 set(${output} True PARENT_SCOPE)
104 file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
105 "Testing compiler for supporting " ${ARGN} ":\n"
106 "Command: ${test_compile_command}\n"
107 "${TEST_OUTPUT}\n${TEST_ERROR}\n${result}\n")
108 set(${output} False PARENT_SCOPE)
112 function(builtin_check_c_compiler_flag flag output)
113 if(NOT DEFINED ${output})
114 message(STATUS "Performing Test ${output}")
115 try_compile_only(result FLAGS ${flag})
116 set(${output} ${result} CACHE INTERNAL "Compiler supports ${flag}")
118 message(STATUS "Performing Test ${output} - Success")
120 message(STATUS "Performing Test ${output} - Failed")
125 function(builtin_check_c_compiler_source output source)
126 if(NOT DEFINED ${output})
127 message(STATUS "Performing Test ${output}")
128 try_compile_only(result SOURCE ${source})
129 set(${output} ${result} CACHE INTERNAL "Compiler supports ${flag}")
131 message(STATUS "Performing Test ${output} - Success")
133 message(STATUS "Performing Test ${output} - Failed")