13cab2bb3Spatrickinclude(CMakeCheckCompilerFlagCommonPatterns) 23cab2bb3Spatrick 33cab2bb3Spatrick# Test compiler can compile simple C/C++/Objective-C program without invoking 43cab2bb3Spatrick# the linker. 53cab2bb3Spatrick# 63cab2bb3Spatrick# try_compile_only( 73cab2bb3Spatrick# OUTPUT_VAR 83cab2bb3Spatrick# [SOURCE source_text] 93cab2bb3Spatrick# [FLAGS flag_0 [ flag_1 ]] 103cab2bb3Spatrick# ) 113cab2bb3Spatrick# 123cab2bb3Spatrick# OUTPUT_VAR - The variable name to store the result. The result is a boolean 133cab2bb3Spatrick# `True` or `False`. 143cab2bb3Spatrick# 153cab2bb3Spatrick# SOURCE - Optional. If specified use source the source text string 163cab2bb3Spatrick# specified. If not specified source code will be used that is C, 173cab2bb3Spatrick# C++, and Objective-C compatible. 183cab2bb3Spatrick# 193cab2bb3Spatrick# FLAGS - Optional. If specified pass the one or more specified flags to 203cab2bb3Spatrick# the compiler. 213cab2bb3Spatrick# 223cab2bb3Spatrick# EXAMPLES: 233cab2bb3Spatrick# 243cab2bb3Spatrick# try_compile_only(HAS_F_NO_RTTI FLAGS "-fno-rtti") 253cab2bb3Spatrick# 263cab2bb3Spatrick# try_compile_only(HAS_CXX_AUTO_TYPE_DECL 273cab2bb3Spatrick# SOURCE "int foo(int x) { auto y = x + 1; return y;}" 283cab2bb3Spatrick# FLAGS "-x" "c++" "-std=c++11" "-Werror=c++11-extensions" 293cab2bb3Spatrick# ) 303cab2bb3Spatrick# 313cab2bb3Spatrickfunction(try_compile_only output) 323cab2bb3Spatrick # NOTE: `SOURCE` needs to be a multi-argument because source code 333cab2bb3Spatrick # often contains semicolons which happens to be CMake's list separator 343cab2bb3Spatrick # which confuses `cmake_parse_arguments()`. 353cab2bb3Spatrick cmake_parse_arguments(ARG "" "" "SOURCE;FLAGS" ${ARGN}) 363cab2bb3Spatrick if (ARG_UNPARSED_ARGUMENTS) 373cab2bb3Spatrick message(FATAL_ERROR "Unexpected arguments \"${ARG_UNPARSED_ARGUMENTS}\"") 383cab2bb3Spatrick endif() 393cab2bb3Spatrick if(NOT ARG_SOURCE) 403cab2bb3Spatrick set(ARG_SOURCE "int foo(int x, int y) { return x + y; }\n") 413cab2bb3Spatrick endif() 423cab2bb3Spatrick set(SIMPLE_C ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/src.c) 433cab2bb3Spatrick file(WRITE ${SIMPLE_C} "${ARG_SOURCE}\n") 443cab2bb3Spatrick string(REGEX MATCHALL "<[A-Za-z0-9_]*>" substitutions 453cab2bb3Spatrick ${CMAKE_C_COMPILE_OBJECT}) 463cab2bb3Spatrick 473cab2bb3Spatrick set(TRY_COMPILE_FLAGS "${ARG_FLAGS}") 483cab2bb3Spatrick if(CMAKE_C_COMPILER_ID MATCHES Clang AND CMAKE_C_COMPILER_TARGET) 49*810390e3Srobert list(APPEND TRY_COMPILE_FLAGS "--target=${CMAKE_C_COMPILER_TARGET}") 503cab2bb3Spatrick endif() 513cab2bb3Spatrick 523cab2bb3Spatrick string(REPLACE ";" " " extra_flags "${TRY_COMPILE_FLAGS}") 533cab2bb3Spatrick 543cab2bb3Spatrick set(test_compile_command "${CMAKE_C_COMPILE_OBJECT}") 553cab2bb3Spatrick foreach(substitution ${substitutions}) 563cab2bb3Spatrick if(substitution STREQUAL "<CMAKE_C_COMPILER>") 573cab2bb3Spatrick string(REPLACE "<CMAKE_C_COMPILER>" 583cab2bb3Spatrick "${CMAKE_C_COMPILER}" test_compile_command ${test_compile_command}) 593cab2bb3Spatrick elseif(substitution STREQUAL "<OBJECT>") 603cab2bb3Spatrick string(REPLACE "<OBJECT>" 613cab2bb3Spatrick "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/test.o" 623cab2bb3Spatrick test_compile_command ${test_compile_command}) 633cab2bb3Spatrick elseif(substitution STREQUAL "<SOURCE>") 643cab2bb3Spatrick string(REPLACE "<SOURCE>" "${SIMPLE_C}" test_compile_command 653cab2bb3Spatrick ${test_compile_command}) 663cab2bb3Spatrick elseif(substitution STREQUAL "<FLAGS>") 673cab2bb3Spatrick string(REPLACE "<FLAGS>" "${CMAKE_C_FLAGS} ${extra_flags}" 683cab2bb3Spatrick test_compile_command ${test_compile_command}) 693cab2bb3Spatrick else() 703cab2bb3Spatrick string(REPLACE "${substitution}" "" test_compile_command 713cab2bb3Spatrick ${test_compile_command}) 723cab2bb3Spatrick endif() 733cab2bb3Spatrick endforeach() 743cab2bb3Spatrick 753cab2bb3Spatrick # Strip quotes from the compile command, as the compiler is not expecting 763cab2bb3Spatrick # quoted arguments (see discussion on D62063 for when this can come up). If 77*810390e3Srobert # the quotes were there for arguments with spaces in them, the quotes were 783cab2bb3Spatrick # not going to help since the string gets split on spaces below. 793cab2bb3Spatrick string(REPLACE "\"" "" test_compile_command "${test_compile_command}") 803cab2bb3Spatrick 813cab2bb3Spatrick string(REPLACE " " ";" test_compile_command "${test_compile_command}") 823cab2bb3Spatrick 833cab2bb3Spatrick execute_process( 843cab2bb3Spatrick COMMAND ${test_compile_command} 853cab2bb3Spatrick RESULT_VARIABLE result 863cab2bb3Spatrick OUTPUT_VARIABLE TEST_OUTPUT 873cab2bb3Spatrick ERROR_VARIABLE TEST_ERROR 883cab2bb3Spatrick ) 893cab2bb3Spatrick 903cab2bb3Spatrick CHECK_COMPILER_FLAG_COMMON_PATTERNS(_CheckCCompilerFlag_COMMON_PATTERNS) 913cab2bb3Spatrick set(ERRORS_FOUND OFF) 923cab2bb3Spatrick foreach(var ${_CheckCCompilerFlag_COMMON_PATTERNS}) 933cab2bb3Spatrick if("${var}" STREQUAL "FAIL_REGEX") 943cab2bb3Spatrick continue() 953cab2bb3Spatrick endif() 963cab2bb3Spatrick if("${TEST_ERROR}" MATCHES "${var}" OR "${TEST_OUTPUT}" MATCHES "${var}") 973cab2bb3Spatrick set(ERRORS_FOUND ON) 983cab2bb3Spatrick endif() 993cab2bb3Spatrick endforeach() 1003cab2bb3Spatrick 1013cab2bb3Spatrick if(result EQUAL 0 AND NOT ERRORS_FOUND) 1023cab2bb3Spatrick set(${output} True PARENT_SCOPE) 1033cab2bb3Spatrick else() 1043cab2bb3Spatrick file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log 1053cab2bb3Spatrick "Testing compiler for supporting " ${ARGN} ":\n" 1063cab2bb3Spatrick "Command: ${test_compile_command}\n" 1073cab2bb3Spatrick "${TEST_OUTPUT}\n${TEST_ERROR}\n${result}\n") 1083cab2bb3Spatrick set(${output} False PARENT_SCOPE) 1093cab2bb3Spatrick endif() 1103cab2bb3Spatrickendfunction() 1113cab2bb3Spatrick 1123cab2bb3Spatrickfunction(builtin_check_c_compiler_flag flag output) 1133cab2bb3Spatrick if(NOT DEFINED ${output}) 1143cab2bb3Spatrick message(STATUS "Performing Test ${output}") 1153cab2bb3Spatrick try_compile_only(result FLAGS ${flag}) 1163cab2bb3Spatrick set(${output} ${result} CACHE INTERNAL "Compiler supports ${flag}") 1173cab2bb3Spatrick if(${result}) 1183cab2bb3Spatrick message(STATUS "Performing Test ${output} - Success") 1193cab2bb3Spatrick else() 1203cab2bb3Spatrick message(STATUS "Performing Test ${output} - Failed") 1213cab2bb3Spatrick endif() 1223cab2bb3Spatrick endif() 1233cab2bb3Spatrickendfunction() 1243cab2bb3Spatrick 1253cab2bb3Spatrickfunction(builtin_check_c_compiler_source output source) 1263cab2bb3Spatrick if(NOT DEFINED ${output}) 1273cab2bb3Spatrick message(STATUS "Performing Test ${output}") 1283cab2bb3Spatrick try_compile_only(result SOURCE ${source}) 1293cab2bb3Spatrick set(${output} ${result} CACHE INTERNAL "Compiler supports ${flag}") 1303cab2bb3Spatrick if(${result}) 1313cab2bb3Spatrick message(STATUS "Performing Test ${output} - Success") 1323cab2bb3Spatrick else() 1333cab2bb3Spatrick message(STATUS "Performing Test ${output} - Failed") 1343cab2bb3Spatrick endif() 1353cab2bb3Spatrick endif() 1363cab2bb3Spatrickendfunction() 137