CMakeDetermineCXXCompiler.cmake 7.15 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
# determine the compiler to use for C++ programs
# NOTE, a generator may set CMAKE_CXX_COMPILER before
# loading this file to force a compiler.
8
# use environment variable CXX first if defined by user, next use
9 10
# the cmake variable CMAKE_GENERATOR_CXX which can be defined by a generator
# as a default compiler
11
# If the internal cmake variable _CMAKE_TOOLCHAIN_PREFIX is set, this is used
12
# as prefix for the tools (e.g. arm-elf-g++, arm-elf-ar etc.)
13 14 15 16 17 18
#
# Sets the following variables:
#   CMAKE_CXX_COMPILER
#   CMAKE_COMPILER_IS_GNUCXX
#   CMAKE_AR
#   CMAKE_RANLIB
19 20 21
#
# If not already set before, it also sets
#   _CMAKE_TOOLCHAIN_PREFIX
22

23
include(${CMAKE_ROOT}/Modules/CMakeDetermineCompiler.cmake)
24

25
# Load system-specific compiler preferences for this language.
26
include(Platform/${CMAKE_SYSTEM_NAME}-Determine-CXX OPTIONAL)
27 28 29 30
include(Platform/${CMAKE_SYSTEM_NAME}-CXX OPTIONAL)
if(NOT CMAKE_CXX_COMPILER_NAMES)
  set(CMAKE_CXX_COMPILER_NAMES CC)
endif()
31

32
if(${CMAKE_GENERATOR} MATCHES "Visual Studio")
33
elseif("${CMAKE_GENERATOR}" MATCHES "Green Hills MULTI")
34 35
elseif("${CMAKE_GENERATOR}" MATCHES "Xcode")
  set(CMAKE_CXX_COMPILER_XCODE_TYPE sourcecode.cpp.cpp)
36
  _cmake_find_compiler_path(CXX)
37 38 39 40 41
else()
  if(NOT CMAKE_CXX_COMPILER)
    set(CMAKE_CXX_COMPILER_INIT NOTFOUND)

    # prefer the environment variable CXX
42
    if(NOT $ENV{CXX} STREQUAL "")
43 44 45 46 47 48 49
      get_filename_component(CMAKE_CXX_COMPILER_INIT $ENV{CXX} PROGRAM PROGRAM_ARGS CMAKE_CXX_FLAGS_ENV_INIT)
      if(CMAKE_CXX_FLAGS_ENV_INIT)
        set(CMAKE_CXX_COMPILER_ARG1 "${CMAKE_CXX_FLAGS_ENV_INIT}" CACHE STRING "First argument to CXX compiler")
      endif()
      if(NOT EXISTS ${CMAKE_CXX_COMPILER_INIT})
        message(FATAL_ERROR "Could not find compiler set in environment variable CXX:\n$ENV{CXX}.\n${CMAKE_CXX_COMPILER_INIT}")
      endif()
50
    endif()
51 52 53 54 55 56

    # next prefer the generator specified compiler
    if(CMAKE_GENERATOR_CXX)
      if(NOT CMAKE_CXX_COMPILER_INIT)
        set(CMAKE_CXX_COMPILER_INIT ${CMAKE_GENERATOR_CXX})
      endif()
57
    endif()
58

59
    # finally list compilers to try
60
    if(NOT CMAKE_CXX_COMPILER_INIT)
61
      set(CMAKE_CXX_COMPILER_LIST CC ${_CMAKE_TOOLCHAIN_PREFIX}c++ ${_CMAKE_TOOLCHAIN_PREFIX}g++ aCC cl bcc xlC clang++)
62
    endif()
63

64 65
    _cmake_find_compiler(CXX)
  else()
66
    _cmake_find_compiler_path(CXX)
67
  endif()
68
  mark_as_advanced(CMAKE_CXX_COMPILER)
69

70 71 72
  # Each entry in this list is a set of extra flags to try
  # adding to the compile line to see if it helps produce
  # a valid identification file.
73
  set(CMAKE_CXX_COMPILER_ID_TEST_FLAGS_FIRST)
74
  set(CMAKE_CXX_COMPILER_ID_TEST_FLAGS
75 76
    # Try compiling to an object file only.
    "-c"
77 78 79
    # IAR does not detect language automatically
    "--c++"
    "--ec++"
80
    )
81 82 83 84 85
endif()

# Build a small source file to identify the compiler.
if(NOT CMAKE_CXX_COMPILER_ID_RUN)
  set(CMAKE_CXX_COMPILER_ID_RUN 1)
86

87
  # Try to identify the compiler.
88
  set(CMAKE_CXX_COMPILER_ID)
89
  set(CMAKE_CXX_PLATFORM_ID)
90
  file(READ ${CMAKE_ROOT}/Modules/CMakePlatformId.h.in
91
    CMAKE_CXX_COMPILER_ID_PLATFORM_CONTENT)
92 93

  # The IAR compiler produces weird output.
94
  # See https://gitlab.kitware.com/cmake/cmake/issues/10176#note_153591
95 96 97 98
  list(APPEND CMAKE_CXX_COMPILER_ID_VENDORS IAR)
  set(CMAKE_CXX_COMPILER_ID_VENDOR_FLAGS_IAR )
  set(CMAKE_CXX_COMPILER_ID_VENDOR_REGEX_IAR "IAR .+ Compiler")

99 100 101 102 103
  # Match the link line from xcodebuild output of the form
  #  Ld ...
  #      ...
  #      /path/to/cc ...CompilerIdCXX/...
  # to extract the compiler front-end for the language.
104
  set(CMAKE_CXX_COMPILER_ID_TOOL_MATCH_REGEX "\nLd[^\n]*(\n[ \t]+[^\n]*)*\n[ \t]+([^ \t\r\n]+)[^\r\n]*-o[^\r\n]*CompilerIdCXX/(\\./)?(CompilerIdCXX.(framework|xctest)/)?CompilerIdCXX[ \t\n\\\"]")
105 106
  set(CMAKE_CXX_COMPILER_ID_TOOL_MATCH_INDEX 2)

107
  include(${CMAKE_ROOT}/Modules/CMakeDetermineCompilerId.cmake)
108
  CMAKE_DETERMINE_COMPILER_ID(CXX CXXFLAGS CMakeCXXCompilerId.cpp)
109 110

  # Set old compiler and platform id variables.
111
  if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
112
    set(CMAKE_COMPILER_IS_GNUCXX 1)
113
  endif()
114
  if(CMAKE_CXX_PLATFORM_ID MATCHES "MinGW")
115
    set(CMAKE_COMPILER_IS_MINGW 1)
116
  elseif(CMAKE_CXX_PLATFORM_ID MATCHES "Cygwin")
117
    set(CMAKE_COMPILER_IS_CYGWIN 1)
118 119
  endif()
endif()
120

121 122 123 124
if (NOT _CMAKE_TOOLCHAIN_LOCATION)
  get_filename_component(_CMAKE_TOOLCHAIN_LOCATION "${CMAKE_CXX_COMPILER}" PATH)
endif ()

125
# if we have a g++ cross compiler, they have usually some prefix, like
126 127 128
# e.g. powerpc-linux-g++, arm-elf-g++ or i586-mingw32msvc-g++ , optionally
# with a 3-component version number at the end (e.g. arm-eabi-gcc-4.5.2).
# The other tools of the toolchain usually have the same prefix
129
# NAME_WE cannot be used since then this test will fail for names like
130
# "arm-unknown-nto-qnx6.3.0-gcc.exe", where BASENAME would be
131
# "arm-unknown-nto-qnx6" instead of the correct "arm-unknown-nto-qnx6.3.0-"
132 133 134 135


if (CMAKE_CROSSCOMPILING  AND NOT  _CMAKE_TOOLCHAIN_PREFIX)

136
  if("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU|Clang|QCC")
137
    get_filename_component(COMPILER_BASENAME "${CMAKE_CXX_COMPILER}" NAME)
138
    if (COMPILER_BASENAME MATCHES "^(.+-)(clan)?[gc]\\+\\+(-[0-9]+(\\.[0-9]+)*)?(-[^.]+)?(\\.exe)?$")
139
      set(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_MATCH_1})
140
      set(_CMAKE_COMPILER_SUFFIX ${CMAKE_MATCH_5})
141
    elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
142 143 144
      if(CMAKE_CXX_COMPILER_TARGET)
        set(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_CXX_COMPILER_TARGET}-)
      endif()
145
    elseif(COMPILER_BASENAME MATCHES "QCC(\\.exe)?$")
146
      if(CMAKE_CXX_COMPILER_TARGET MATCHES "gcc_nto([a-z0-9]+_[0-9]+|[^_le]+)(le)")
147 148
        set(_CMAKE_TOOLCHAIN_PREFIX nto${CMAKE_MATCH_1}-)
      endif()
149 150 151 152 153 154 155
    endif ()

    # if "llvm-" is part of the prefix, remove it, since llvm doesn't have its own binutils
    # but uses the regular ar, objcopy, etc. (instead of llvm-objcopy etc.)
    if ("${_CMAKE_TOOLCHAIN_PREFIX}" MATCHES "(.+-)?llvm-$")
      set(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_MATCH_1})
    endif ()
156 157 158 159 160 161 162 163
  elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "TI")
    # TI compilers are named e.g. cl6x, cl470 or armcl.exe
    get_filename_component(COMPILER_BASENAME "${CMAKE_CXX_COMPILER}" NAME)
    if (COMPILER_BASENAME MATCHES "^(.+)?cl([^.]+)?(\\.exe)?$")
      set(_CMAKE_TOOLCHAIN_PREFIX "${CMAKE_MATCH_1}")
      set(_CMAKE_TOOLCHAIN_SUFFIX "${CMAKE_MATCH_2}")
    endif ()

164
  endif()
165

166
endif ()
167

168
set(_CMAKE_PROCESSING_LANGUAGE "CXX")
169
include(CMakeFindBinUtils)
170 171 172
include(Compiler/${CMAKE_CXX_COMPILER_ID}-FindBinUtils OPTIONAL)
unset(_CMAKE_PROCESSING_LANGUAGE)

173 174 175 176 177 178 179
if(CMAKE_CXX_COMPILER_ARCHITECTURE_ID)
  set(_SET_CMAKE_CXX_COMPILER_ARCHITECTURE_ID
    "set(CMAKE_CXX_COMPILER_ARCHITECTURE_ID ${CMAKE_CXX_COMPILER_ARCHITECTURE_ID})")
else()
  set(_SET_CMAKE_CXX_COMPILER_ARCHITECTURE_ID "")
endif()

180 181 182
if(MSVC_CXX_ARCHITECTURE_ID)
  set(SET_MSVC_CXX_ARCHITECTURE_ID
    "set(MSVC_CXX_ARCHITECTURE_ID ${MSVC_CXX_ARCHITECTURE_ID})")
183
endif()
184

185 186 187
if(CMAKE_CXX_XCODE_ARCHS)
  set(SET_CMAKE_XCODE_ARCHS
    "set(CMAKE_XCODE_ARCHS \"${CMAKE_CXX_XCODE_ARCHS}\")")
188 189
endif()

Bill Hoffman's avatar
Bill Hoffman committed
190
# configure all variables set in this file
191
configure_file(${CMAKE_ROOT}/Modules/CMakeCXXCompiler.cmake.in
192
  ${CMAKE_PLATFORM_INFO_DIR}/CMakeCXXCompiler.cmake
193
  @ONLY
194
  )
195

196
set(CMAKE_CXX_COMPILER_ENV_VAR "CXX")