1# Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2# file Copyright.txt or https://cmake.org/licensing for details.
3
4
5if(CMAKE_C_COMPILER_FORCED)
6  # The compiler configuration was forced by the user.
7  # Assume the user has configured all compiler information.
8  set(CMAKE_C_COMPILER_WORKS TRUE)
9  return()
10endif()
11
12include(CMakeTestCompilerCommon)
13
14# work around enforced code signing and / or missing executable target type
15set(__CMAKE_SAVED_TRY_COMPILE_TARGET_TYPE ${CMAKE_TRY_COMPILE_TARGET_TYPE})
16if(_CMAKE_FEATURE_DETECTION_TARGET_TYPE)
17  set(CMAKE_TRY_COMPILE_TARGET_TYPE ${_CMAKE_FEATURE_DETECTION_TARGET_TYPE})
18endif()
19
20# Remove any cached result from an older CMake version.
21# We now store this in CMakeCCompiler.cmake.
22unset(CMAKE_C_COMPILER_WORKS CACHE)
23
24# Try to identify the ABI and configure it into CMakeCCompiler.cmake
25include(${CMAKE_ROOT}/Modules/CMakeDetermineCompilerABI.cmake)
26CMAKE_DETERMINE_COMPILER_ABI(C ${CMAKE_ROOT}/Modules/CMakeCCompilerABI.c)
27if(CMAKE_C_ABI_COMPILED)
28  # The compiler worked so skip dedicated test below.
29  set(CMAKE_C_COMPILER_WORKS TRUE)
30  message(STATUS "Check for working C compiler: ${CMAKE_C_COMPILER} - skipped")
31endif()
32
33# This file is used by EnableLanguage in cmGlobalGenerator to
34# determine that that selected C compiler can actually compile
35# and link the most basic of programs.   If not, a fatal error
36# is set and cmake stops processing commands and will not generate
37# any makefiles or projects.
38if(NOT CMAKE_C_COMPILER_WORKS)
39  PrintTestCompilerStatus("C")
40  __TestCompiler_setTryCompileTargetType()
41  file(WRITE ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/testCCompiler.c
42    "#ifdef __cplusplus\n"
43    "# error \"The CMAKE_C_COMPILER is set to a C++ compiler\"\n"
44    "#endif\n"
45    "#if defined(__CLASSIC_C__)\n"
46    "int main(argc, argv)\n"
47    "  int argc;\n"
48    "  char* argv[];\n"
49    "#else\n"
50    "int main(int argc, char* argv[])\n"
51    "#endif\n"
52    "{ (void)argv; return argc-1;}\n")
53  # Clear result from normal variable.
54  unset(CMAKE_C_COMPILER_WORKS)
55  # Puts test result in cache variable.
56  try_compile(CMAKE_C_COMPILER_WORKS ${CMAKE_BINARY_DIR}
57    ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/testCCompiler.c
58    OUTPUT_VARIABLE __CMAKE_C_COMPILER_OUTPUT)
59  # Move result from cache to normal variable.
60  set(CMAKE_C_COMPILER_WORKS ${CMAKE_C_COMPILER_WORKS})
61  unset(CMAKE_C_COMPILER_WORKS CACHE)
62  __TestCompiler_restoreTryCompileTargetType()
63  if(NOT CMAKE_C_COMPILER_WORKS)
64    PrintTestCompilerResult(CHECK_FAIL "broken")
65    file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
66      "Determining if the C compiler works failed with "
67      "the following output:\n${__CMAKE_C_COMPILER_OUTPUT}\n\n")
68    string(REPLACE "\n" "\n  " _output "${__CMAKE_C_COMPILER_OUTPUT}")
69    message(FATAL_ERROR "The C compiler\n  \"${CMAKE_C_COMPILER}\"\n"
70      "is not able to compile a simple test program.\nIt fails "
71      "with the following output:\n  ${_output}\n\n"
72      "CMake will not be able to correctly generate this project.")
73  endif()
74  PrintTestCompilerResult(CHECK_PASS "works")
75  file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
76    "Determining if the C compiler works passed with "
77    "the following output:\n${__CMAKE_C_COMPILER_OUTPUT}\n\n")
78endif()
79
80# Try to identify the compiler features
81include(${CMAKE_ROOT}/Modules/CMakeDetermineCompileFeatures.cmake)
82CMAKE_DETERMINE_COMPILE_FEATURES(C)
83
84# Re-configure to save learned information.
85configure_file(
86  ${CMAKE_ROOT}/Modules/CMakeCCompiler.cmake.in
87  ${CMAKE_PLATFORM_INFO_DIR}/CMakeCCompiler.cmake
88  @ONLY
89  )
90include(${CMAKE_PLATFORM_INFO_DIR}/CMakeCCompiler.cmake)
91
92if(CMAKE_C_SIZEOF_DATA_PTR)
93  foreach(f ${CMAKE_C_ABI_FILES})
94    include(${f})
95  endforeach()
96  unset(CMAKE_C_ABI_FILES)
97endif()
98
99set(CMAKE_TRY_COMPILE_TARGET_TYPE ${__CMAKE_SAVED_TRY_COMPILE_TARGET_TYPE})
100unset(__CMAKE_SAVED_TRY_COMPILE_TARGET_TYPE)
101unset(__CMAKE_C_COMPILER_OUTPUT)
102