CheckOBJCSourceCompiles.cmake 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. CheckOBJCSourceCompiles
  5. -----------------------
  6. Check if given Objective-C source compiles and links into an executable.
  7. .. command:: check_objc_source_compiles
  8. .. code-block:: cmake
  9. check_objc_source_compiles(<code> <resultVar>
  10. [FAIL_REGEX <regex1> [<regex2>...]])
  11. Check that the source supplied in ``<code>`` can be compiled as a Objectie-C source
  12. file and linked as an executable (so it must contain at least a ``main()``
  13. function). The result will be stored in the internal cache variable specified
  14. by ``<resultVar>``, with a boolean true value for success and boolean false
  15. for failure. If ``FAIL_REGEX`` is provided, then failure is determined by
  16. checking if anything in the output matches any of the specified regular
  17. expressions.
  18. The underlying check is performed by the :command:`try_compile` command. The
  19. compile and link commands can be influenced by setting any of the following
  20. variables prior to calling ``check_objc_source_compiles()``:
  21. ``CMAKE_REQUIRED_FLAGS``
  22. Additional flags to pass to the compiler. Note that the contents of
  23. :variable:`CMAKE_OBJC_FLAGS <CMAKE_<LANG>_FLAGS>` and its associated
  24. configuration-specific variable are automatically added to the compiler
  25. command before the contents of ``CMAKE_REQUIRED_FLAGS``.
  26. ``CMAKE_REQUIRED_DEFINITIONS``
  27. A :ref:`;-list <CMake Language Lists>` of compiler definitions of the form
  28. ``-DFOO`` or ``-DFOO=bar``. A definition for the name specified by
  29. ``<resultVar>`` will also be added automatically.
  30. ``CMAKE_REQUIRED_INCLUDES``
  31. A :ref:`;-list <CMake Language Lists>` of header search paths to pass to
  32. the compiler. These will be the only header search paths used by
  33. ``try_compile()``, i.e. the contents of the :prop_dir:`INCLUDE_DIRECTORIES`
  34. directory property will be ignored.
  35. ``CMAKE_REQUIRED_LINK_OPTIONS``
  36. A :ref:`;-list <CMake Language Lists>` of options to add to the link
  37. command (see :command:`try_compile` for further details).
  38. ``CMAKE_REQUIRED_LIBRARIES``
  39. A :ref:`;-list <CMake Language Lists>` of libraries to add to the link
  40. command. These can be the name of system libraries or they can be
  41. :ref:`Imported Targets <Imported Targets>` (see :command:`try_compile` for
  42. further details).
  43. ``CMAKE_REQUIRED_QUIET``
  44. If this variable evaluates to a boolean true value, all status messages
  45. associated with the check will be suppressed.
  46. The check is only performed once, with the result cached in the variable
  47. named by ``<resultVar>``. Every subsequent CMake run will re-use this cached
  48. value rather than performing the check again, even if the ``<code>`` changes.
  49. In order to force the check to be re-evaluated, the variable named by
  50. ``<resultVar>`` must be manually removed from the cache.
  51. #]=======================================================================]
  52. include_guard(GLOBAL)
  53. macro(CHECK_OBJC_SOURCE_COMPILES SOURCE VAR)
  54. if(NOT DEFINED "${VAR}")
  55. set(_FAIL_REGEX)
  56. set(_key)
  57. foreach(arg ${ARGN})
  58. if("${arg}" MATCHES "^(FAIL_REGEX)$")
  59. set(_key "${arg}")
  60. elseif(_key)
  61. list(APPEND _${_key} "${arg}")
  62. else()
  63. message(FATAL_ERROR "Unknown argument:\n ${arg}\n")
  64. endif()
  65. endforeach()
  66. set(MACRO_CHECK_FUNCTION_DEFINITIONS
  67. "-D${VAR} ${CMAKE_REQUIRED_FLAGS}")
  68. if(CMAKE_REQUIRED_LINK_OPTIONS)
  69. set(CHECK_OBJC_SOURCE_COMPILES_ADD_LINK_OPTIONS
  70. LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS})
  71. else()
  72. set(CHECK_OBJC_SOURCE_COMPILES_ADD_LINK_OPTIONS)
  73. endif()
  74. if(CMAKE_REQUIRED_LIBRARIES)
  75. set(CHECK_OBJC_SOURCE_COMPILES_ADD_LIBRARIES
  76. LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
  77. else()
  78. set(CHECK_OBJC_SOURCE_COMPILES_ADD_LIBRARIES)
  79. endif()
  80. if(CMAKE_REQUIRED_INCLUDES)
  81. set(CHECK_OBJC_SOURCE_COMPILES_ADD_INCLUDES
  82. "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
  83. else()
  84. set(CHECK_OBJC_SOURCE_COMPILES_ADD_INCLUDES)
  85. endif()
  86. file(WRITE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.m"
  87. "${SOURCE}\n")
  88. if(NOT CMAKE_REQUIRED_QUIET)
  89. message(STATUS "Performing Test ${VAR}")
  90. endif()
  91. try_compile(${VAR}
  92. ${CMAKE_BINARY_DIR}
  93. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.m
  94. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  95. ${CHECK_OBJC_SOURCE_COMPILES_ADD_LINK_OPTIONS}
  96. ${CHECK_OBJC_SOURCE_COMPILES_ADD_LIBRARIES}
  97. CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_FUNCTION_DEFINITIONS}
  98. "${CHECK_OBJC_SOURCE_COMPILES_ADD_INCLUDES}"
  99. OUTPUT_VARIABLE OUTPUT)
  100. foreach(_regex ${_FAIL_REGEX})
  101. if("${OUTPUT}" MATCHES "${_regex}")
  102. set(${VAR} 0)
  103. endif()
  104. endforeach()
  105. if(${VAR})
  106. set(${VAR} 1 CACHE INTERNAL "Test ${VAR}")
  107. if(NOT CMAKE_REQUIRED_QUIET)
  108. message(STATUS "Performing Test ${VAR} - Success")
  109. endif()
  110. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  111. "Performing Objective-C SOURCE FILE Test ${VAR} succeeded with the following output:\n"
  112. "${OUTPUT}\n"
  113. "Source file was:\n${SOURCE}\n")
  114. else()
  115. if(NOT CMAKE_REQUIRED_QUIET)
  116. message(STATUS "Performing Test ${VAR} - Failed")
  117. endif()
  118. set(${VAR} "" CACHE INTERNAL "Test ${VAR}")
  119. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  120. "Performing Objective-C SOURCE FILE Test ${VAR} failed with the following output:\n"
  121. "${OUTPUT}\n"
  122. "Source file was:\n${SOURCE}\n")
  123. endif()
  124. endif()
  125. endmacro()