brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.9 KiB · 63c5f47 Raw
137 lines · plain
1include(CMakeCheckCompilerFlagCommonPatterns)2 3# Test compiler can compile simple C/C++/Objective-C program without invoking4# the linker.5#6# try_compile_only(7#   OUTPUT_VAR8#   [SOURCE source_text]9#   [FLAGS flag_0 [ flag_1 ]]10# )11#12# OUTPUT_VAR - The variable name to store the result. The result is a boolean13#              `True` or `False`.14#15# SOURCE     - Optional. If specified use source the source text string16#              specified. If not specified source code will be used that is C,17#              C++, and Objective-C compatible.18#19# FLAGS      - Optional. If specified pass the one or more specified flags to20#              the compiler.21#22# EXAMPLES:23#24# try_compile_only(HAS_F_NO_RTTI FLAGS "-fno-rtti")25#26# try_compile_only(HAS_CXX_AUTO_TYPE_DECL27#   SOURCE "int foo(int x) { auto y = x + 1; return y;}"28#   FLAGS "-x" "c++" "-std=c++11" "-Werror=c++11-extensions"29# )30#31function(try_compile_only output)32  # NOTE: `SOURCE` needs to be a multi-argument because source code33  # often contains semicolons which happens to be CMake's list separator34  # 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}\"")38  endif()39  if(NOT ARG_SOURCE)40    set(ARG_SOURCE "int foo(int x, int y) { return x + y; }\n")41  endif()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_]*>" substitutions45         ${CMAKE_C_COMPILE_OBJECT})46 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}")50  endif()51 52  string(REPLACE ";" " " extra_flags "${TRY_COMPILE_FLAGS}")53 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_command65             ${test_compile_command})66    elseif(substitution STREQUAL "<FLAGS>")67      string(REPLACE "<FLAGS>" "${CMAKE_C_FLAGS} ${extra_flags}"68             test_compile_command ${test_compile_command})69    else()70      string(REPLACE "${substitution}" "" test_compile_command71             ${test_compile_command})72    endif()73  endforeach()74 75  # Strip quotes from the compile command, as the compiler is not expecting76  # quoted arguments (see discussion on D62063 for when this can come up). If77  # the quotes were there for arguments with spaces in them, the quotes were78  # not going to help since the string gets split on spaces below.79  string(REPLACE "\"" "" test_compile_command "${test_compile_command}")80 81  string(REPLACE " " ";" test_compile_command "${test_compile_command}")82 83  execute_process(84    COMMAND ${test_compile_command}85    RESULT_VARIABLE result86    OUTPUT_VARIABLE TEST_OUTPUT87    ERROR_VARIABLE TEST_ERROR88  )89 90  CHECK_COMPILER_FLAG_COMMON_PATTERNS(_CheckCCompilerFlag_COMMON_PATTERNS)91  set(ERRORS_FOUND OFF)92  foreach(var ${_CheckCCompilerFlag_COMMON_PATTERNS})93    if("${var}" STREQUAL "FAIL_REGEX")94      continue()95    endif()96    if("${TEST_ERROR}" MATCHES "${var}" OR "${TEST_OUTPUT}" MATCHES "${var}")97      set(ERRORS_FOUND ON)98    endif()99  endforeach()100 101  if(result EQUAL 0 AND NOT ERRORS_FOUND)102    set(${output} True PARENT_SCOPE)103  else()104    file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log105        "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)109  endif()110endfunction()111 112function(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} ${CMAKE_REQUIRED_FLAGS})116    set(${output} ${result} CACHE INTERNAL "Compiler supports ${flag}")117    if(${result})118      message(STATUS "Performing Test ${output} - Success")119    else()120      message(STATUS "Performing Test ${output} - Failed")121    endif()122  endif()123endfunction()124 125function(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}")130    if(${result})131      message(STATUS "Performing Test ${output} - Success")132    else()133      message(STATUS "Performing Test ${output} - Failed")134    endif()135  endif()136endfunction()137