CMakeDetermineOBJCXXCompiler.cmake 7.3 KB

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