CheckOBJCXXSourceRuns.cmake 5.9 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. CheckOBJCXXSourceRuns
  5. ---------------------
  6. Check if given Objective-C++ source compiles and links into an executable and can
  7. subsequently be run.
  8. .. command:: check_objcxx_source_runs
  9. .. code-block:: cmake
  10. check_objcxx_source_runs(<code> <resultVar>)
  11. Check that the source supplied in ``<code>`` can be compiled as a Objective-C++ source
  12. file, linked as an executable and then run. The ``<code>`` must contain at
  13. least a ``main()`` function. If the ``<code>`` could be built and run
  14. successfully, the internal cache variable specified by ``<resultVar>`` will
  15. be set to 1, otherwise it will be set to an value that evaluates to boolean
  16. false (e.g. an empty string or an error message).
  17. The underlying check is performed by the :command:`try_run` command. The
  18. compile and link commands can be influenced by setting any of the following
  19. variables prior to calling ``check_objcxx_source_runs()``:
  20. ``CMAKE_REQUIRED_FLAGS``
  21. Additional flags to pass to the compiler. Note that the contents of
  22. :variable:`CMAKE_OBJCXX_FLAGS <CMAKE_<LANG>_FLAGS>` and its associated
  23. configuration-specific variable are automatically added to the compiler
  24. command before the contents of ``CMAKE_REQUIRED_FLAGS``.
  25. ``CMAKE_REQUIRED_DEFINITIONS``
  26. A :ref:`;-list <CMake Language Lists>` of compiler definitions of the form
  27. ``-DFOO`` or ``-DFOO=bar``. A definition for the name specified by
  28. ``<resultVar>`` will also be added automatically.
  29. ``CMAKE_REQUIRED_INCLUDES``
  30. A :ref:`;-list <CMake Language Lists>` of header search paths to pass to
  31. the compiler. These will be the only header search paths used by
  32. ``try_run()``, i.e. the contents of the :prop_dir:`INCLUDE_DIRECTORIES`
  33. directory property will be ignored.
  34. ``CMAKE_REQUIRED_LINK_OPTIONS``
  35. A :ref:`;-list <CMake Language Lists>` of options to add to the link
  36. command (see :command:`try_run` for further details).
  37. ``CMAKE_REQUIRED_LIBRARIES``
  38. A :ref:`;-list <CMake Language Lists>` of libraries to add to the link
  39. command. These can be the name of system libraries or they can be
  40. :ref:`Imported Targets <Imported Targets>` (see :command:`try_run` for
  41. further details).
  42. ``CMAKE_REQUIRED_QUIET``
  43. If this variable evaluates to a boolean true value, all status messages
  44. associated with the check will be suppressed.
  45. The check is only performed once, with the result cached in the variable
  46. named by ``<resultVar>``. Every subsequent CMake run will re-use this cached
  47. value rather than performing the check again, even if the ``<code>`` changes.
  48. In order to force the check to be re-evaluated, the variable named by
  49. ``<resultVar>`` must be manually removed from the cache.
  50. #]=======================================================================]
  51. include_guard(GLOBAL)
  52. macro(CHECK_OBJCXX_SOURCE_RUNS SOURCE VAR)
  53. if(NOT DEFINED "${VAR}")
  54. set(MACRO_CHECK_FUNCTION_DEFINITIONS
  55. "-D${VAR} ${CMAKE_REQUIRED_FLAGS}")
  56. if(CMAKE_REQUIRED_LINK_OPTIONS)
  57. set(CHECK_OBJCXX_SOURCE_COMPILES_ADD_LINK_OPTIONS
  58. LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS})
  59. else()
  60. set(CHECK_OBJCXX_SOURCE_COMPILES_ADD_LINK_OPTIONS)
  61. endif()
  62. if(CMAKE_REQUIRED_LIBRARIES)
  63. set(CHECK_OBJCXX_SOURCE_COMPILES_ADD_LIBRARIES
  64. LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
  65. else()
  66. set(CHECK_OBJCXX_SOURCE_COMPILES_ADD_LIBRARIES)
  67. endif()
  68. if(CMAKE_REQUIRED_INCLUDES)
  69. set(CHECK_OBJCXX_SOURCE_COMPILES_ADD_INCLUDES
  70. "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
  71. else()
  72. set(CHECK_OBJCXX_SOURCE_COMPILES_ADD_INCLUDES)
  73. endif()
  74. file(WRITE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.mm"
  75. "${SOURCE}\n")
  76. if(NOT CMAKE_REQUIRED_QUIET)
  77. message(STATUS "Performing Test ${VAR}")
  78. endif()
  79. try_run(${VAR}_EXITCODE ${VAR}_COMPILED
  80. ${CMAKE_BINARY_DIR}
  81. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.mm
  82. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  83. ${CHECK_OBJCXX_SOURCE_COMPILES_ADD_LINK_OPTIONS}
  84. ${CHECK_OBJCXX_SOURCE_COMPILES_ADD_LIBRARIES}
  85. CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_FUNCTION_DEFINITIONS}
  86. -DCMAKE_SKIP_RPATH:BOOL=${CMAKE_SKIP_RPATH}
  87. "${CHECK_OBJCXX_SOURCE_COMPILES_ADD_INCLUDES}"
  88. COMPILE_OUTPUT_VARIABLE OUTPUT
  89. RUN_OUTPUT_VARIABLE RUN_OUTPUT)
  90. # if it did not compile make the return value fail code of 1
  91. if(NOT ${VAR}_COMPILED)
  92. set(${VAR}_EXITCODE 1)
  93. endif()
  94. # if the return value was 0 then it worked
  95. if("${${VAR}_EXITCODE}" EQUAL 0)
  96. set(${VAR} 1 CACHE INTERNAL "Test ${VAR}")
  97. if(NOT CMAKE_REQUIRED_QUIET)
  98. message(STATUS "Performing Test ${VAR} - Success")
  99. endif()
  100. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  101. "Performing Objective-C++ SOURCE FILE Test ${VAR} succeeded with the following output:\n"
  102. "${OUTPUT}\n"
  103. "...and run output:\n"
  104. "${RUN_OUTPUT}\n"
  105. "Return value: ${${VAR}}\n"
  106. "Source file was:\n${SOURCE}\n")
  107. else()
  108. if(CMAKE_CROSSCOMPILING AND "${${VAR}_EXITCODE}" MATCHES "FAILED_TO_RUN")
  109. set(${VAR} "${${VAR}_EXITCODE}")
  110. else()
  111. set(${VAR} "" CACHE INTERNAL "Test ${VAR}")
  112. endif()
  113. if(NOT CMAKE_REQUIRED_QUIET)
  114. message(STATUS "Performing Test ${VAR} - Failed")
  115. endif()
  116. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  117. "Performing Objective-C++ SOURCE FILE Test ${VAR} failed with the following output:\n"
  118. "${OUTPUT}\n"
  119. "...and run output:\n"
  120. "${RUN_OUTPUT}\n"
  121. "Return value: ${${VAR}_EXITCODE}\n"
  122. "Source file was:\n${SOURCE}\n")
  123. endif()
  124. endif()
  125. endmacro()