CMakeTestCXXCompiler.cmake 3.57 KB
Newer Older
1 2
# Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
# file Copyright.txt or https://cmake.org/licensing for details.
3

4

5 6 7 8 9 10 11
if(CMAKE_CXX_COMPILER_FORCED)
  # The compiler configuration was forced by the user.
  # Assume the user has configured all compiler information.
  set(CMAKE_CXX_COMPILER_WORKS TRUE)
  return()
endif()

12
include(CMakeTestCompilerCommon)
13

14 15 16 17 18 19
# work around enforced code signing and / or missing exectuable target type
set(__CMAKE_SAVED_TRY_COMPILE_TARGET_TYPE ${CMAKE_TRY_COMPILE_TARGET_TYPE})
if(_CMAKE_FEATURE_DETECTION_TARGET_TYPE)
  set(CMAKE_TRY_COMPILE_TARGET_TYPE ${_CMAKE_FEATURE_DETECTION_TARGET_TYPE})
endif()

20 21 22 23
# Remove any cached result from an older CMake version.
# We now store this in CMakeCXXCompiler.cmake.
unset(CMAKE_CXX_COMPILER_WORKS CACHE)

24
# This file is used by EnableLanguage in cmGlobalGenerator to
luz.paz's avatar
luz.paz committed
25
# determine that the selected C++ compiler can actually compile
Brad King's avatar
Brad King committed
26
# and link the most basic of programs.   If not, a fatal error
27 28
# is set and cmake stops processing commands and will not generate
# any makefiles or projects.
29
if(NOT CMAKE_CXX_COMPILER_WORKS)
30
  PrintTestCompilerStatus("CXX" "")
Fred Baksik's avatar
Fred Baksik committed
31
  __TestCompiler_setTryCompileTargetType()
32
  file(WRITE ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/testCXXCompiler.cxx
33 34 35 36
    "#ifndef __cplusplus\n"
    "# error \"The CMAKE_CXX_COMPILER is set to a C compiler\"\n"
    "#endif\n"
    "int main(){return 0;}\n")
37
  try_compile(CMAKE_CXX_COMPILER_WORKS ${CMAKE_BINARY_DIR}
38
    ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/testCXXCompiler.cxx
39
    OUTPUT_VARIABLE __CMAKE_CXX_COMPILER_OUTPUT)
40 41 42
  # Move result from cache to normal variable.
  set(CMAKE_CXX_COMPILER_WORKS ${CMAKE_CXX_COMPILER_WORKS})
  unset(CMAKE_CXX_COMPILER_WORKS CACHE)
43
  set(CXX_TEST_WAS_RUN 1)
Fred Baksik's avatar
Fred Baksik committed
44
  __TestCompiler_restoreTryCompileTargetType()
45
endif()
46

47
if(NOT CMAKE_CXX_COMPILER_WORKS)
48
  PrintTestCompilerStatus("CXX" " -- broken")
49
  file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
50
    "Determining if the CXX compiler works failed with "
51
    "the following output:\n${__CMAKE_CXX_COMPILER_OUTPUT}\n\n")
52 53
  string(REPLACE "\n" "\n  " _output "${__CMAKE_CXX_COMPILER_OUTPUT}")
  message(FATAL_ERROR "The C++ compiler\n  \"${CMAKE_CXX_COMPILER}\"\n"
Brad King's avatar
Brad King committed
54
    "is not able to compile a simple test program.\nIt fails "
55
    "with the following output:\n  ${_output}\n\n"
56
    "CMake will not be able to correctly generate this project.")
57
else()
58
  if(CXX_TEST_WAS_RUN)
59
    PrintTestCompilerStatus("CXX" " -- works")
60
    file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
61
      "Determining if the CXX compiler works passed with "
62
      "the following output:\n${__CMAKE_CXX_COMPILER_OUTPUT}\n\n")
63
  endif()
64

65 66 67
  # Try to identify the ABI and configure it into CMakeCXXCompiler.cmake
  include(${CMAKE_ROOT}/Modules/CMakeDetermineCompilerABI.cmake)
  CMAKE_DETERMINE_COMPILER_ABI(CXX ${CMAKE_ROOT}/Modules/CMakeCXXCompilerABI.cpp)
68 69 70
  # Try to identify the compiler features
  include(${CMAKE_ROOT}/Modules/CMakeDetermineCompileFeatures.cmake)
  CMAKE_DETERMINE_COMPILE_FEATURES(CXX)
71 72 73 74 75

  # Re-configure to save learned information.
  configure_file(
    ${CMAKE_ROOT}/Modules/CMakeCXXCompiler.cmake.in
    ${CMAKE_PLATFORM_INFO_DIR}/CMakeCXXCompiler.cmake
76
    @ONLY
77 78 79
    )
  include(${CMAKE_PLATFORM_INFO_DIR}/CMakeCXXCompiler.cmake)

80 81 82 83 84 85
  if(CMAKE_CXX_SIZEOF_DATA_PTR)
    foreach(f ${CMAKE_CXX_ABI_FILES})
      include(${f})
    endforeach()
    unset(CMAKE_CXX_ABI_FILES)
  endif()
86
endif()
87

88 89
set(CMAKE_TRY_COMPILE_TARGET_TYPE ${__CMAKE_SAVED_TRY_COMPILE_TARGET_TYPE})
unset(__CMAKE_SAVED_TRY_COMPILE_TARGET_TYPE)
90
unset(__CMAKE_CXX_COMPILER_OUTPUT)