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