CMakeDetermineOBJCXXCompiler.cmake 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 "Arguments 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, use the first item as
  61. # CMAKE_OBJCXX_COMPILER and the rest as CMAKE_OBJCXX_COMPILER_ARG1
  62. set(CMAKE_OBJCXX_COMPILER_ARG1 "${CMAKE_OBJCXX_COMPILER}")
  63. list(POP_FRONT CMAKE_OBJCXX_COMPILER_ARG1 CMAKE_OBJCXX_COMPILER)
  64. list(JOIN CMAKE_OBJCXX_COMPILER_ARG1 " " CMAKE_OBJCXX_COMPILER_ARG1)
  65. # if a compiler was specified by the user but without path,
  66. # now try to find it with the full path
  67. # if it is found, force it into the cache,
  68. # if not, don't overwrite the setting (which was given by the user) with "NOTFOUND"
  69. # if the C compiler already had a path, reuse it for searching the CXX compiler
  70. get_filename_component(_CMAKE_USER_OBJCXX_COMPILER_PATH "${CMAKE_OBJCXX_COMPILER}" PATH)
  71. if(NOT _CMAKE_USER_OBJCXX_COMPILER_PATH)
  72. find_program(CMAKE_OBJCXX_COMPILER_WITH_PATH NAMES ${CMAKE_OBJCXX_COMPILER})
  73. if(CMAKE_OBJCXX_COMPILER_WITH_PATH)
  74. set(CMAKE_OBJCXX_COMPILER ${CMAKE_OBJCXX_COMPILER_WITH_PATH} CACHE STRING "Objective-C++ compiler" FORCE)
  75. endif()
  76. unset(CMAKE_OBJCXX_COMPILER_WITH_PATH CACHE)
  77. endif()
  78. endif()
  79. mark_as_advanced(CMAKE_OBJCXX_COMPILER)
  80. # Each entry in this list is a set of extra flags to try
  81. # adding to the compile line to see if it helps produce
  82. # a valid identification file.
  83. set(CMAKE_OBJCXX_COMPILER_ID_TEST_FLAGS_FIRST)
  84. set(CMAKE_OBJCXX_COMPILER_ID_TEST_FLAGS
  85. # Try compiling to an object file only.
  86. "-c"
  87. # ARMClang need target options
  88. "--target=arm-arm-none-eabi -mcpu=cortex-m3"
  89. )
  90. endif()
  91. # Build a small source file to identify the compiler.
  92. if(NOT CMAKE_OBJCXX_COMPILER_ID_RUN)
  93. set(CMAKE_OBJCXX_COMPILER_ID_RUN 1)
  94. # Try to identify the compiler.
  95. set(CMAKE_OBJCXX_COMPILER_ID)
  96. file(READ ${CMAKE_ROOT}/Modules/CMakePlatformId.h.in
  97. CMAKE_OBJCXX_COMPILER_ID_PLATFORM_CONTENT)
  98. # Match the link line from xcodebuild output of the form
  99. # Ld ...
  100. # ...
  101. # /path/to/cc ...CompilerIdOBJCXX/...
  102. # to extract the compiler front-end for the language.
  103. 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\\\"]")
  104. set(CMAKE_OBJCXX_COMPILER_ID_TOOL_MATCH_INDEX 2)
  105. include(${CMAKE_ROOT}/Modules/CMakeDetermineCompilerId.cmake)
  106. CMAKE_DETERMINE_COMPILER_ID(OBJCXX OBJCXXFLAGS CMakeOBJCXXCompilerId.mm)
  107. # Set old compiler and platform id variables.
  108. if(CMAKE_OBJCXX_COMPILER_ID MATCHES "GNU")
  109. set(CMAKE_COMPILER_IS_GNUOBJCXX 1)
  110. endif()
  111. if(CMAKE_OBJCXX_COMPILER_ID MATCHES "Clang")
  112. set(CMAKE_COMPILER_IS_CLANGOBJCXX 1)
  113. endif()
  114. endif()
  115. if (NOT _CMAKE_TOOLCHAIN_LOCATION)
  116. get_filename_component(_CMAKE_TOOLCHAIN_LOCATION "${CMAKE_OBJCXX_COMPILER}" PATH)
  117. endif ()
  118. # if we have a g++ cross compiler, they have usually some prefix, like
  119. # e.g. powerpc-linux-g++, arm-elf-g++ or i586-mingw32msvc-g++ , optionally
  120. # with a 3-component version number at the end (e.g. arm-eabi-gcc-4.5.2).
  121. # The other tools of the toolchain usually have the same prefix
  122. # NAME_WE cannot be used since then this test will fail for names like
  123. # "arm-unknown-nto-qnx6.3.0-gcc.exe", where BASENAME would be
  124. # "arm-unknown-nto-qnx6" instead of the correct "arm-unknown-nto-qnx6.3.0-"
  125. if (NOT _CMAKE_TOOLCHAIN_PREFIX)
  126. if("${CMAKE_OBJCXX_COMPILER_ID}" MATCHES "GNU|Clang|QCC")
  127. get_filename_component(COMPILER_BASENAME "${CMAKE_OBJCXX_COMPILER}" NAME)
  128. if (COMPILER_BASENAME MATCHES "^(.+-)(clan)?[gc]\\+\\+(-[0-9]+(\\.[0-9]+)*)?(-[^.]+)?(\\.exe)?$")
  129. set(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_MATCH_1})
  130. set(_CMAKE_COMPILER_SUFFIX ${CMAKE_MATCH_5})
  131. elseif("${CMAKE_OBJCXX_COMPILER_ID}" MATCHES "Clang")
  132. if(CMAKE_OBJCXX_COMPILER_TARGET)
  133. set(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_OBJCXX_COMPILER_TARGET}-)
  134. endif()
  135. elseif(COMPILER_BASENAME MATCHES "QCC(\\.exe)?$")
  136. if(CMAKE_OBJCXX_COMPILER_TARGET MATCHES "gcc_nto([a-z0-9]+_[0-9]+|[^_le]+)(le)")
  137. set(_CMAKE_TOOLCHAIN_PREFIX nto${CMAKE_MATCH_1}-)
  138. endif()
  139. endif ()
  140. # if "llvm-" is part of the prefix, remove it, since llvm doesn't have its own binutils
  141. # but uses the regular ar, objcopy, etc. (instead of llvm-objcopy etc.)
  142. if ("${_CMAKE_TOOLCHAIN_PREFIX}" MATCHES "(.+-)?llvm-$")
  143. set(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_MATCH_1})
  144. endif ()
  145. endif()
  146. endif ()
  147. set(_CMAKE_PROCESSING_LANGUAGE "OBJCXX")
  148. include(CMakeFindBinUtils)
  149. include(Compiler/${CMAKE_OBJCXX_COMPILER_ID}-FindBinUtils OPTIONAL)
  150. unset(_CMAKE_PROCESSING_LANGUAGE)
  151. if(CMAKE_OBJCXX_COMPILER_ARCHITECTURE_ID)
  152. set(_SET_CMAKE_OBJCXX_COMPILER_ARCHITECTURE_ID
  153. "set(CMAKE_OBJCXX_COMPILER_ARCHITECTURE_ID ${CMAKE_OBJCXX_COMPILER_ARCHITECTURE_ID})")
  154. else()
  155. set(_SET_CMAKE_OBJCXX_COMPILER_ARCHITECTURE_ID "")
  156. endif()
  157. if(CMAKE_OBJCXX_XCODE_ARCHS)
  158. set(SET_CMAKE_XCODE_ARCHS
  159. "set(CMAKE_XCODE_ARCHS \"${CMAKE_OBJCXX_XCODE_ARCHS}\")")
  160. endif()
  161. # configure all variables set in this file
  162. configure_file(${CMAKE_ROOT}/Modules/CMakeOBJCXXCompiler.cmake.in
  163. ${CMAKE_PLATFORM_INFO_DIR}/CMakeOBJCXXCompiler.cmake
  164. @ONLY
  165. )
  166. set(CMAKE_OBJCXX_COMPILER_ENV_VAR "OBJCXX")