CMakeDetermineOBJCXXCompiler.cmake 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. # determine the compiler to use for Objective-C++ programs
  4. # NOTE, a generator may set CMAKE_OBJCXX_COMPILER before
  5. # loading this file to force a compiler.
  6. # use environment variable OBJCXX first if defined by user, next use
  7. # the cmake variable CMAKE_GENERATOR_OBJCXX which can be defined by a generator
  8. # as a default compiler
  9. # If the internal cmake variable _CMAKE_TOOLCHAIN_PREFIX is set, this is used
  10. # as prefix for the tools (e.g. arm-elf-g++, arm-elf-ar etc.)
  11. #
  12. # Sets the following variables:
  13. # CMAKE_OBJCXX_COMPILER
  14. # CMAKE_COMPILER_IS_GNUOBJCXX
  15. # CMAKE_COMPILER_IS_CLANGOBJCXX
  16. # CMAKE_AR
  17. # CMAKE_RANLIB
  18. #
  19. # If not already set before, it also sets
  20. # _CMAKE_TOOLCHAIN_PREFIX
  21. include(${CMAKE_ROOT}/Modules/CMakeDetermineCompiler.cmake)
  22. # Load system-specific compiler preferences for this language.
  23. include(Platform/${CMAKE_SYSTEM_NAME}-Determine-OBJCXX OPTIONAL)
  24. include(Platform/${CMAKE_SYSTEM_NAME}-OBJCXX OPTIONAL)
  25. if(NOT CMAKE_OBJCXX_COMPILER_NAMES)
  26. set(CMAKE_OBJCXX_COMPILER_NAMES clang++)
  27. endif()
  28. if("${CMAKE_GENERATOR}" MATCHES "Xcode")
  29. set(CMAKE_OBJCXX_COMPILER_XCODE_TYPE sourcecode.cpp.objcpp)
  30. else()
  31. if(NOT CMAKE_OBJCXX_COMPILER)
  32. set(CMAKE_OBJCXX_COMPILER_INIT NOTFOUND)
  33. # prefer the environment variable OBJCXX or CXX
  34. foreach(var OBJCXX CXX)
  35. if($ENV{${var}} MATCHES ".+")
  36. get_filename_component(CMAKE_OBJCXX_COMPILER_INIT $ENV{${var}} PROGRAM PROGRAM_ARGS CMAKE_OBJCXX_FLAGS_ENV_INIT)
  37. if(CMAKE_OBJCXX_FLAGS_ENV_INIT)
  38. set(CMAKE_OBJCXX_COMPILER_ARG1 "${CMAKE_OBJCXX_FLAGS_ENV_INIT}" CACHE STRING "First argument to Objective-C++ compiler")
  39. endif()
  40. if(NOT EXISTS ${CMAKE_OBJCXX_COMPILER_INIT})
  41. message(FATAL_ERROR "Could not find compiler set in environment variable ${var}:\n $ENV{${var}}")
  42. endif()
  43. break()
  44. endif()
  45. endforeach()
  46. # next prefer the generator specified compiler
  47. if(CMAKE_GENERATOR_OBJCXX)
  48. if(NOT CMAKE_OBJCXX_COMPILER_INIT)
  49. set(CMAKE_OBJCXX_COMPILER_INIT ${CMAKE_GENERATOR_OBJCXX})
  50. endif()
  51. endif()
  52. # finally list compilers to try
  53. if(NOT CMAKE_OBJCXX_COMPILER_INIT)
  54. set(CMAKE_OBJCXX_COMPILER_LIST ${_CMAKE_TOOLCHAIN_PREFIX}c++ ${_CMAKE_TOOLCHAIN_PREFIX}g++ clang++)
  55. endif()
  56. _cmake_find_compiler(OBJCXX)
  57. else()
  58. # we only get here if CMAKE_OBJCXX_COMPILER was specified using -D or a pre-made CMakeCache.txt
  59. # (e.g. via ctest) or set in CMAKE_TOOLCHAIN_FILE
  60. # if CMAKE_OBJCXX_COMPILER is a list of length 2, use the first item as
  61. # CMAKE_OBJCXX_COMPILER and the 2nd one as CMAKE_OBJCXX_COMPILER_ARG1
  62. list(LENGTH CMAKE_OBJCXX_COMPILER _CMAKE_OBJCXX_COMPILER_LIST_LENGTH)
  63. if("${_CMAKE_OBJCXX_COMPILER_LIST_LENGTH}" EQUAL 2)
  64. list(GET CMAKE_OBJCXX_COMPILER 1 CMAKE_OBJCXX_COMPILER_ARG1)
  65. list(GET CMAKE_OBJCXX_COMPILER 0 CMAKE_OBJCXX_COMPILER)
  66. endif()
  67. # if a compiler was specified by the user but without path,
  68. # now try to find it with the full path
  69. # if it is found, force it into the cache,
  70. # if not, don't overwrite the setting (which was given by the user) with "NOTFOUND"
  71. # if the C compiler already had a path, reuse it for searching the CXX compiler
  72. get_filename_component(_CMAKE_USER_OBJCXX_COMPILER_PATH "${CMAKE_OBJCXX_COMPILER}" PATH)
  73. if(NOT _CMAKE_USER_OBJCXX_COMPILER_PATH)
  74. find_program(CMAKE_OBJCXX_COMPILER_WITH_PATH NAMES ${CMAKE_OBJCXX_COMPILER})
  75. if(CMAKE_OBJCXX_COMPILER_WITH_PATH)
  76. set(CMAKE_OBJCXX_COMPILER ${CMAKE_OBJCXX_COMPILER_WITH_PATH} CACHE STRING "Objective-C++ compiler" FORCE)
  77. endif()
  78. unset(CMAKE_OBJCXX_COMPILER_WITH_PATH CACHE)
  79. endif()
  80. endif()
  81. mark_as_advanced(CMAKE_OBJCXX_COMPILER)
  82. # Each entry in this list is a set of extra flags to try
  83. # adding to the compile line to see if it helps produce
  84. # a valid identification file.
  85. set(CMAKE_OBJCXX_COMPILER_ID_TEST_FLAGS_FIRST)
  86. set(CMAKE_OBJCXX_COMPILER_ID_TEST_FLAGS
  87. # Try compiling to an object file only.
  88. "-c"
  89. # ARMClang need target options
  90. "--target=arm-arm-none-eabi -mcpu=cortex-m3"
  91. )
  92. endif()
  93. # Build a small source file to identify the compiler.
  94. if(NOT CMAKE_OBJCXX_COMPILER_ID_RUN)
  95. set(CMAKE_OBJCXX_COMPILER_ID_RUN 1)
  96. # Try to identify the compiler.
  97. set(CMAKE_OBJCXX_COMPILER_ID)
  98. file(READ ${CMAKE_ROOT}/Modules/CMakePlatformId.h.in
  99. CMAKE_OBJCXX_COMPILER_ID_PLATFORM_CONTENT)
  100. # Match the link line from xcodebuild output of the form
  101. # Ld ...
  102. # ...
  103. # /path/to/cc ...CompilerIdOBJCXX/...
  104. # to extract the compiler front-end for the language.
  105. set(CMAKE_OBJCXX_COMPILER_ID_TOOL_MATCH_REGEX "\nLd[^\n]*(\n[ \t]+[^\n]*)*\n[ \t]+([^ \t\r\n]+)[^\r\n]*-o[^\r\n]*CompilerIdOBJCXX/(\\./)?(CompilerIdOBJCXX.(framework|xctest)/)?CompilerIdOBJCXX[ \t\n\\\"]")
  106. set(CMAKE_OBJCXX_COMPILER_ID_TOOL_MATCH_INDEX 2)
  107. include(${CMAKE_ROOT}/Modules/CMakeDetermineCompilerId.cmake)
  108. CMAKE_DETERMINE_COMPILER_ID(OBJCXX OBJCXXFLAGS CMakeOBJCXXCompilerId.mm)
  109. # Set old compiler and platform id variables.
  110. if(CMAKE_OBJCXX_COMPILER_ID MATCHES "GNU")
  111. set(CMAKE_COMPILER_IS_GNUOBJCXX 1)
  112. endif()
  113. if(CMAKE_OBJCXX_COMPILER_ID MATCHES "Clang")
  114. set(CMAKE_COMPILER_IS_CLANGOBJCXX 1)
  115. endif()
  116. endif()
  117. if (NOT _CMAKE_TOOLCHAIN_LOCATION)
  118. get_filename_component(_CMAKE_TOOLCHAIN_LOCATION "${CMAKE_OBJCXX_COMPILER}" PATH)
  119. endif ()
  120. # if we have a g++ cross compiler, they have usually some prefix, like
  121. # e.g. powerpc-linux-g++, arm-elf-g++ or i586-mingw32msvc-g++ , optionally
  122. # with a 3-component version number at the end (e.g. arm-eabi-gcc-4.5.2).
  123. # The other tools of the toolchain usually have the same prefix
  124. # NAME_WE cannot be used since then this test will fail for names like
  125. # "arm-unknown-nto-qnx6.3.0-gcc.exe", where BASENAME would be
  126. # "arm-unknown-nto-qnx6" instead of the correct "arm-unknown-nto-qnx6.3.0-"
  127. if (CMAKE_CROSSCOMPILING AND NOT _CMAKE_TOOLCHAIN_PREFIX)
  128. if("${CMAKE_OBJCXX_COMPILER_ID}" MATCHES "GNU|Clang|QCC")
  129. get_filename_component(COMPILER_BASENAME "${CMAKE_OBJCXX_COMPILER}" NAME)
  130. if (COMPILER_BASENAME MATCHES "^(.+-)(clan)?[gc]\\+\\+(-[0-9]+(\\.[0-9]+)*)?(-[^.]+)?(\\.exe)?$")
  131. set(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_MATCH_1})
  132. set(_CMAKE_COMPILER_SUFFIX ${CMAKE_MATCH_5})
  133. elseif("${CMAKE_OBJCXX_COMPILER_ID}" MATCHES "Clang")
  134. if(CMAKE_OBJCXX_COMPILER_TARGET)
  135. set(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_OBJCXX_COMPILER_TARGET}-)
  136. endif()
  137. elseif(COMPILER_BASENAME MATCHES "QCC(\\.exe)?$")
  138. if(CMAKE_OBJCXX_COMPILER_TARGET MATCHES "gcc_nto([a-z0-9]+_[0-9]+|[^_le]+)(le)")
  139. set(_CMAKE_TOOLCHAIN_PREFIX nto${CMAKE_MATCH_1}-)
  140. endif()
  141. endif ()
  142. # if "llvm-" is part of the prefix, remove it, since llvm doesn't have its own binutils
  143. # but uses the regular ar, objcopy, etc. (instead of llvm-objcopy etc.)
  144. if ("${_CMAKE_TOOLCHAIN_PREFIX}" MATCHES "(.+-)?llvm-$")
  145. set(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_MATCH_1})
  146. endif ()
  147. endif()
  148. endif ()
  149. set(_CMAKE_PROCESSING_LANGUAGE "OBJCXX")
  150. include(CMakeFindBinUtils)
  151. include(Compiler/${CMAKE_OBJCXX_COMPILER_ID}-FindBinUtils OPTIONAL)
  152. unset(_CMAKE_PROCESSING_LANGUAGE)
  153. if(CMAKE_OBJCXX_COMPILER_ARCHITECTURE_ID)
  154. set(_SET_CMAKE_OBJCXX_COMPILER_ARCHITECTURE_ID
  155. "set(CMAKE_OBJCXX_COMPILER_ARCHITECTURE_ID ${CMAKE_OBJCXX_COMPILER_ARCHITECTURE_ID})")
  156. else()
  157. set(_SET_CMAKE_OBJCXX_COMPILER_ARCHITECTURE_ID "")
  158. endif()
  159. if(CMAKE_OBJCXX_XCODE_ARCHS)
  160. set(SET_CMAKE_XCODE_ARCHS
  161. "set(CMAKE_XCODE_ARCHS \"${CMAKE_OBJCXX_XCODE_ARCHS}\")")
  162. endif()
  163. # configure all variables set in this file
  164. configure_file(${CMAKE_ROOT}/Modules/CMakeOBJCXXCompiler.cmake.in
  165. ${CMAKE_PLATFORM_INFO_DIR}/CMakeOBJCXXCompiler.cmake
  166. @ONLY
  167. )
  168. set(CMAKE_OBJCXX_COMPILER_ENV_VAR "OBJCXX")