CheckCSourceRuns.cmake 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file LICENSE.rst or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. CheckCSourceRuns
  5. ----------------
  6. This module provides a command to check whether a C source can be built and
  7. run.
  8. Load this module in a CMake project with:
  9. .. code-block:: cmake
  10. include(CheckCSourceRuns)
  11. Commands
  12. ^^^^^^^^
  13. This module provides the following command:
  14. .. command:: check_c_source_runs
  15. Checks once whether the given C source code compiles and links into an
  16. executable that can subsequently be run:
  17. .. code-block:: cmake
  18. check_c_source_runs(<code> <variable>)
  19. The C source supplied in ``<code>`` must contain at least a ``main()``
  20. function. The result of the check is stored in the internal cache variable
  21. specified by ``<variable>``. If the code builds and runs with exit code
  22. ``0``, success is indicated by a boolean true value. Failure to build or
  23. run is indicated by a boolean false value, such as an empty string or an
  24. error message.
  25. .. rubric:: Variables Affecting the Check
  26. The following variables may be set before calling this command to modify
  27. the way the check is run:
  28. .. include:: /module/include/CMAKE_REQUIRED_FLAGS.rst
  29. .. include:: /module/include/CMAKE_REQUIRED_DEFINITIONS.rst
  30. .. include:: /module/include/CMAKE_REQUIRED_INCLUDES.rst
  31. .. include:: /module/include/CMAKE_REQUIRED_LINK_OPTIONS.rst
  32. .. include:: /module/include/CMAKE_REQUIRED_LIBRARIES.rst
  33. .. include:: /module/include/CMAKE_REQUIRED_LINK_DIRECTORIES.rst
  34. .. include:: /module/include/CMAKE_REQUIRED_QUIET.rst
  35. Examples
  36. ^^^^^^^^
  37. The following example demonstrates how to use this module to check whether
  38. the C source code is supported and operational at runtime. The result of
  39. the check is stored in the internal cache variable ``HAVE_NORETURN``.
  40. .. code-block:: cmake
  41. include(CheckCSourceRuns)
  42. check_c_source_runs([[
  43. #include <stdlib.h>
  44. #include <stdnoreturn.h>
  45. noreturn void f(){ exit(0); }
  46. int main(void) { f(); return 1; }
  47. ]] HAVE_NORETURN)
  48. See Also
  49. ^^^^^^^^
  50. * The :module:`CheckSourceRuns` module for a more general command syntax.
  51. * The :module:`CheckSourceCompiles` module to check whether a source code
  52. can be built.
  53. #]=======================================================================]
  54. include_guard(GLOBAL)
  55. include(Internal/CheckSourceRuns)
  56. macro(CHECK_C_SOURCE_RUNS SOURCE VAR)
  57. set(_CheckSourceRuns_old_signature 1)
  58. cmake_check_source_runs(C "${SOURCE}" ${VAR} ${ARGN})
  59. unset(_CheckSourceRuns_old_signature)
  60. endmacro()