CMakeDetermineOBJCCompiler.cmake 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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_OBJC_COMPILER before
  5. # loading this file to force a compiler.
  6. # use environment variable OBJC first if defined by user, next use
  7. # the cmake variable CMAKE_GENERATOR_OBJC which can be defined by a generator
  8. # as a default compiler
  9. #
  10. # Sets the following variables:
  11. # CMAKE_OBJC_COMPILER
  12. # CMAKE_AR
  13. # CMAKE_RANLIB
  14. # CMAKE_COMPILER_IS_GNUOBJC
  15. # CMAKE_COMPILER_IS_CLANGOBJC
  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-OBJC OPTIONAL)
  22. include(Platform/${CMAKE_SYSTEM_NAME}-OBJC OPTIONAL)
  23. if(NOT CMAKE_OBJC_COMPILER_NAMES)
  24. set(CMAKE_OBJC_COMPILER_NAMES clang)
  25. endif()
  26. if("${CMAKE_GENERATOR}" MATCHES "Xcode")
  27. set(CMAKE_OBJC_COMPILER_XCODE_TYPE sourcecode.c.objc)
  28. else()
  29. if(NOT CMAKE_OBJC_COMPILER)
  30. set(CMAKE_OBJC_COMPILER_INIT NOTFOUND)
  31. # prefer the environment variable OBJC
  32. if($ENV{OBJC} MATCHES ".+")
  33. get_filename_component(CMAKE_OBJC_COMPILER_INIT $ENV{OBJC} PROGRAM PROGRAM_ARGS CMAKE_OBJC_FLAGS_ENV_INIT)
  34. if(CMAKE_OBJC_FLAGS_ENV_INIT)
  35. set(CMAKE_OBJC_COMPILER_ARG1 "${CMAKE_OBJC_FLAGS_ENV_INIT}" CACHE STRING "First argument to Objective-C compiler")
  36. endif()
  37. if(NOT EXISTS ${CMAKE_OBJC_COMPILER_INIT})
  38. message(FATAL_ERROR "Could not find compiler set in environment variable OBJC:\n$ENV{OBJC}.")
  39. endif()
  40. endif()
  41. # next try prefer the compiler specified by the generator
  42. if(CMAKE_GENERATOR_OBJC)
  43. if(NOT CMAKE_OBJC_COMPILER_INIT)
  44. set(CMAKE_OBJC_COMPILER_INIT ${CMAKE_GENERATOR_OBJC})
  45. endif()
  46. endif()
  47. # finally list compilers to try
  48. if(NOT CMAKE_OBJC_COMPILER_INIT)
  49. set(CMAKE_OBJC_COMPILER_LIST ${_CMAKE_TOOLCHAIN_PREFIX}cc ${_CMAKE_TOOLCHAIN_PREFIX}gcc clang)
  50. endif()
  51. _cmake_find_compiler(OBJC)
  52. else()
  53. # we only get here if CMAKE_OBJC_COMPILER was specified using -D or a pre-made CMakeCache.txt
  54. # (e.g. via ctest) or set in CMAKE_TOOLCHAIN_FILE
  55. # if CMAKE_OBJC_COMPILER is a list of length 2, use the first item as
  56. # CMAKE_OBJC_COMPILER and the 2nd one as CMAKE_OBJC_COMPILER_ARG1
  57. list(LENGTH CMAKE_OBJC_COMPILER _CMAKE_OBJC_COMPILER_LIST_LENGTH)
  58. if("${_CMAKE_OBJC_COMPILER_LIST_LENGTH}" EQUAL 2)
  59. list(GET CMAKE_OBJC_COMPILER 1 CMAKE_OBJC_COMPILER_ARG1)
  60. list(GET CMAKE_OBJC_COMPILER 0 CMAKE_OBJC_COMPILER)
  61. endif()
  62. # if a compiler was specified by the user but without path,
  63. # now try to find it with the full path
  64. # if it is found, force it into the cache,
  65. # if not, don't overwrite the setting (which was given by the user) with "NOTFOUND"
  66. # if the C compiler already had a path, reuse it for searching the CXX compiler
  67. get_filename_component(_CMAKE_USER_OBJC_COMPILER_PATH "${CMAKE_OBJC_COMPILER}" PATH)
  68. if(NOT _CMAKE_USER_OBJC_COMPILER_PATH)
  69. find_program(CMAKE_OBJC_COMPILER_WITH_PATH NAMES ${CMAKE_OBJC_COMPILER})
  70. if(CMAKE_OBJC_COMPILER_WITH_PATH)
  71. set(CMAKE_OBJC_COMPILER ${CMAKE_OBJC_COMPILER_WITH_PATH} CACHE STRING "Objective-C compiler" FORCE)
  72. endif()
  73. unset(CMAKE_OBJC_COMPILER_WITH_PATH CACHE)
  74. endif()
  75. endif()
  76. mark_as_advanced(CMAKE_OBJC_COMPILER)
  77. # Each entry in this list is a set of extra flags to try
  78. # adding to the compile line to see if it helps produce
  79. # a valid identification file.
  80. set(CMAKE_OBJC_COMPILER_ID_TEST_FLAGS_FIRST)
  81. set(CMAKE_OBJC_COMPILER_ID_TEST_FLAGS
  82. # Try compiling to an object file only.
  83. "-c"
  84. )
  85. endif()
  86. # Build a small source file to identify the compiler.
  87. if(NOT CMAKE_OBJC_COMPILER_ID_RUN)
  88. set(CMAKE_OBJC_COMPILER_ID_RUN 1)
  89. # Try to identify the compiler.
  90. set(CMAKE_OBJC_COMPILER_ID)
  91. file(READ ${CMAKE_ROOT}/Modules/CMakePlatformId.h.in
  92. CMAKE_OBJC_COMPILER_ID_PLATFORM_CONTENT)
  93. # Match the link line from xcodebuild output of the form
  94. # Ld ...
  95. # ...
  96. # /path/to/cc ...CompilerIdOBJC/...
  97. # to extract the compiler front-end for the language.
  98. set(CMAKE_OBJC_COMPILER_ID_TOOL_MATCH_REGEX "\nLd[^\n]*(\n[ \t]+[^\n]*)*\n[ \t]+([^ \t\r\n]+)[^\r\n]*-o[^\r\n]*CompilerIdOBJC/(\\./)?(CompilerIdOBJC.(framework|xctest)/)?CompilerIdOBJC[ \t\n\\\"]")
  99. set(CMAKE_OBJC_COMPILER_ID_TOOL_MATCH_INDEX 2)
  100. include(${CMAKE_ROOT}/Modules/CMakeDetermineCompilerId.cmake)
  101. CMAKE_DETERMINE_COMPILER_ID(OBJC OBJCCFLAGS CMakeOBJCCompilerId.m)
  102. # Set old compiler and platform id variables.
  103. if(CMAKE_OBJC_COMPILER_ID STREQUAL "GNU")
  104. set(CMAKE_COMPILER_IS_GNUOBJC 1)
  105. endif()
  106. if(CMAKE_OBJC_COMPILER_ID STREQUAL "Clang")
  107. set(CMAKE_COMPILER_IS_CLANGOBJC 1)
  108. endif()
  109. endif()
  110. if (NOT _CMAKE_TOOLCHAIN_LOCATION)
  111. get_filename_component(_CMAKE_TOOLCHAIN_LOCATION "${CMAKE_OBJC_COMPILER}" PATH)
  112. endif ()
  113. # If we have a gcc cross compiler, they have usually some prefix, like
  114. # e.g. powerpc-linux-gcc, arm-elf-gcc or i586-mingw32msvc-gcc, optionally
  115. # with a 3-component version number at the end (e.g. arm-eabi-gcc-4.5.2).
  116. # The other tools of the toolchain usually have the same prefix
  117. # NAME_WE cannot be used since then this test will fail for names like
  118. # "arm-unknown-nto-qnx6.3.0-gcc.exe", where BASENAME would be
  119. # "arm-unknown-nto-qnx6" instead of the correct "arm-unknown-nto-qnx6.3.0-"
  120. if (CMAKE_CROSSCOMPILING AND NOT _CMAKE_TOOLCHAIN_PREFIX)
  121. if(CMAKE_OBJC_COMPILER_ID MATCHES "GNU|Clang|QCC")
  122. get_filename_component(COMPILER_BASENAME "${CMAKE_OBJC_COMPILER}" NAME)
  123. if (COMPILER_BASENAME MATCHES "^(.+-)(clang|g?cc)(-[0-9]+(\\.[0-9]+)*)?(-[^.]+)?(\\.exe)?$")
  124. set(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_MATCH_1})
  125. set(_CMAKE_COMPILER_SUFFIX ${CMAKE_MATCH_5})
  126. elseif(CMAKE_OBJC_COMPILER_ID MATCHES "Clang")
  127. if(CMAKE_OBJC_COMPILER_TARGET)
  128. set(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_OBJC_COMPILER_TARGET}-)
  129. endif()
  130. elseif(COMPILER_BASENAME MATCHES "qcc(\\.exe)?$")
  131. if(CMAKE_OBJC_COMPILER_TARGET MATCHES "gcc_nto([a-z0-9]+_[0-9]+|[^_le]+)(le)?")
  132. set(_CMAKE_TOOLCHAIN_PREFIX nto${CMAKE_MATCH_1}-)
  133. endif()
  134. endif ()
  135. # if "llvm-" is part of the prefix, remove it, since llvm doesn't have its own binutils
  136. # but uses the regular ar, objcopy, etc. (instead of llvm-objcopy etc.)
  137. if ("${_CMAKE_TOOLCHAIN_PREFIX}" MATCHES "(.+-)?llvm-$")
  138. set(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_MATCH_1})
  139. endif ()
  140. endif()
  141. endif ()
  142. set(_CMAKE_PROCESSING_LANGUAGE "OBJC")
  143. include(CMakeFindBinUtils)
  144. include(Compiler/${CMAKE_OBJC_COMPILER_ID}-FindBinUtils OPTIONAL)
  145. unset(_CMAKE_PROCESSING_LANGUAGE)
  146. if(CMAKE_OBJC_COMPILER_ARCHITECTURE_ID)
  147. set(_SET_CMAKE_OBJC_COMPILER_ARCHITECTURE_ID
  148. "set(CMAKE_OBJC_COMPILER_ARCHITECTURE_ID ${CMAKE_OBJC_COMPILER_ARCHITECTURE_ID})")
  149. else()
  150. set(_SET_CMAKE_OBJC_COMPILER_ARCHITECTURE_ID "")
  151. endif()
  152. if(CMAKE_OBJC_XCODE_ARCHS)
  153. set(SET_CMAKE_XCODE_ARCHS
  154. "set(CMAKE_XCODE_ARCHS \"${CMAKE_OBJC_XCODE_ARCHS}\")")
  155. endif()
  156. # configure variables set in this file for fast reload later on
  157. configure_file(${CMAKE_ROOT}/Modules/CMakeOBJCCompiler.cmake.in
  158. ${CMAKE_PLATFORM_INFO_DIR}/CMakeOBJCCompiler.cmake
  159. @ONLY
  160. )
  161. set(CMAKE_OBJC_COMPILER_ENV_VAR "OBJC")