diff --git a/Help/guide/tutorial/Adding System Introspection.rst b/Help/guide/tutorial/Adding System Introspection.rst index 117b2306d9..87070eddd4 100644 --- a/Help/guide/tutorial/Adding System Introspection.rst +++ b/Help/guide/tutorial/Adding System Introspection.rst @@ -35,7 +35,7 @@ exercise, complete ``TODO 1`` through ``TODO 5``. Start by editing ``MathFunctions/CMakeLists.txt``. Include the :module:`CheckCXXSourceCompiles` module. Then, use -``check_cxx_source_compiles`` to determine whether ``log`` and ``exp`` are +``check_cxx_source_compiles()`` to determine whether ``log`` and ``exp`` are available from ``cmath``. If they are available, use :command:`target_compile_definitions` to specify ``HAVE_LOG`` and ``HAVE_EXP`` as compile definitions. diff --git a/Help/module/include/CMAKE_TRY_COMPILE_TARGET_TYPE.rst b/Help/module/include/CMAKE_TRY_COMPILE_TARGET_TYPE.rst new file mode 100644 index 0000000000..af855f8f28 --- /dev/null +++ b/Help/module/include/CMAKE_TRY_COMPILE_TARGET_TYPE.rst @@ -0,0 +1,6 @@ +:variable:`CMAKE_TRY_COMPILE_TARGET_TYPE` + Internally, the :command:`try_compile` command is used to perform the + check, and this variable controls the type of target it creates. If this + variable is set to ``EXECUTABLE`` (the default), the check compiles and + links the test source code as an executable program. If set to + ``STATIC_LIBRARY``, the test source code is compiled but not linked. diff --git a/Help/release/3.1.rst b/Help/release/3.1.rst index fa70367c3a..9d7cddfac4 100644 --- a/Help/release/3.1.rst +++ b/Help/release/3.1.rst @@ -191,7 +191,7 @@ Modules meant for a prefix other than :variable:`CMAKE_INSTALL_PREFIX`. * The :module:`CheckFortranSourceCompiles` module was added to - provide a ``CHECK_Fortran_SOURCE_COMPILES`` macro. + provide a ``check_fortran_source_compiles()`` command. * The :module:`ExternalData` module learned to tolerate a ``DATA{}`` reference to a missing source file with a warning instead of diff --git a/Help/release/3.7.rst b/Help/release/3.7.rst index ceac100b2b..e52f361187 100644 --- a/Help/release/3.7.rst +++ b/Help/release/3.7.rst @@ -158,8 +158,8 @@ Modules * An :module:`AndroidTestUtilities` module was added to manage transfer of test data to an Android device. -* The :module:`CheckFortranSourceCompiles` module macro - ``CHECK_Fortran_SOURCE_COMPILES`` gained a ``SRC_EXT`` option +* The :module:`CheckFortranSourceCompiles` module command + ``check_fortran_source_compiles()`` gained a ``SRC_EXT`` option to specify a custom test Fortran source file extension. * The :module:`ExternalProject` module gained ``HTTP_USERNAME`` and diff --git a/Modules/CheckCSourceCompiles.cmake b/Modules/CheckCSourceCompiles.cmake index c7a281d4b0..83672060ea 100644 --- a/Modules/CheckCSourceCompiles.cmake +++ b/Modules/CheckCSourceCompiles.cmake @@ -5,35 +5,51 @@ CheckCSourceCompiles -------------------- -Check once if C source code can be built. +This module provides a command to check whether a C source can be built. + +Load this module in a CMake project with: + +.. code-block:: cmake + + include(CheckCSourceCompiles) + +Commands +^^^^^^^^ + +This module provides the following command: .. command:: check_c_source_compiles + Checks once whether the given C source code can be built: + .. code-block:: cmake - check_c_source_compiles( - [FAIL_REGEX [...]]) + check_c_source_compiles( [FAIL_REGEX ...]) - Check once that the source supplied in ```` can be built. The result is - stored in the internal cache variable specified by ````, with - boolean ``true`` for success and boolean ``false`` for failure. + This command checks once that the source supplied in ```` can be + compiled (and linked into an executable). The result of the check is + stored in the internal cache variable specified by ````. - If ``FAIL_REGEX`` is provided, then failure is determined by checking - if anything in the compiler output matches any of the specified regular - expressions. + The arguments are: - Internally, :command:`try_compile` is used to compile the source. If - :variable:`CMAKE_TRY_COMPILE_TARGET_TYPE` is set to ``EXECUTABLE`` (default), - the source is compiled and linked as an executable program. If set to - ``STATIC_LIBRARY``, the source is compiled but not linked. In any case, all - functions must be declared as usual. + ```` + C source code to check. This must be an entire program, as written in + a file containing the body block. All symbols used in the source code + are expected to be declared as usual in their corresponding headers. - See also :command:`check_source_compiles` for a more general command syntax. + ```` + Variable name of an internal cache variable to store the result of the + check, with boolean true for success and boolean false for failure. - See also :command:`check_source_runs` to run compiled source. + ``FAIL_REGEX ...`` + If one or more regular expression patterns are provided, then failure is + determined by checking if anything in the compiler output matches any of + the specified regular expressions. - The compile and link commands can be influenced by setting any of the - following variables prior to calling ``check_c_source_compiles()``: + .. rubric:: Variables Affecting the Check + + The following variables may be set before calling this command to modify + the way the check is run: .. include:: /module/include/CMAKE_REQUIRED_FLAGS.rst @@ -49,6 +65,35 @@ Check once if C source code can be built. .. include:: /module/include/CMAKE_REQUIRED_QUIET.rst + .. include:: /module/include/CMAKE_TRY_COMPILE_TARGET_TYPE.rst + +Examples +^^^^^^^^ + +Checking whether C source code containing SSE2 intrinsic can be compiled +and linked: + +.. code-block:: cmake + + include(CheckCSourceCompiles) + + check_c_source_compiles(" + #include + int main(void) + { + __m128d a = _mm_setzero_pd(); + (void)a; + return 0; + } + " PROJECT_HAVE_SSE2_INTRINSICS) + +See Also +^^^^^^^^ + +* The :module:`CheckSourceCompiles` module for a more general command to + check whether source can be built. +* The :module:`CheckSourceRuns` module to check whether source can be built + and run. #]=======================================================================] include_guard(GLOBAL) diff --git a/Modules/CheckCXXSourceCompiles.cmake b/Modules/CheckCXXSourceCompiles.cmake index 46ec942f2e..2da6ef2a2b 100644 --- a/Modules/CheckCXXSourceCompiles.cmake +++ b/Modules/CheckCXXSourceCompiles.cmake @@ -5,35 +5,51 @@ CheckCXXSourceCompiles ---------------------- -Check once if C++ source code can be built. +This module provides a command to check whether a C++ source can be built. + +Load this module in a CMake project with: + +.. code-block:: cmake + + include(CheckCXXSourceCompiles) + +Commands +^^^^^^^^ + +This module provides the following command: .. command:: check_cxx_source_compiles + Checks once whether the given C++ source code can be built: + .. code-block:: cmake - check_cxx_source_compiles( - [FAIL_REGEX [...]]) + check_cxx_source_compiles( [FAIL_REGEX ...]) - Check once that the source supplied in ```` can be built. The result is - stored in the internal cache variable specified by ````, with - boolean ``true`` for success and boolean ``false`` for failure. + This command checks once that the source supplied in ```` can be + compiled (and linked into an executable). The result of the check is + stored in the internal cache variable specified by ````. - If ``FAIL_REGEX`` is provided, then failure is determined by checking - if anything in the compiler output matches any of the specified regular - expressions. + The arguments are: - Internally, :command:`try_compile` is used to compile the source. If - :variable:`CMAKE_TRY_COMPILE_TARGET_TYPE` is set to ``EXECUTABLE`` (default), - the source is compiled and linked as an executable program. If set to - ``STATIC_LIBRARY``, the source is compiled but not linked. In any case, all - functions must be declared as usual. + ```` + C++ source code to check. This must be an entire program, as written + in a file containing the body block. All symbols used in the source code + are expected to be declared as usual in their corresponding headers. - See also :command:`check_source_compiles` for a more general command syntax. + ```` + Variable name of an internal cache variable to store the result of the + check, with boolean true for success and boolean false for failure. - See also :command:`check_source_runs` to run compiled source. + ``FAIL_REGEX ...`` + If one or more regular expression patterns are provided, then failure is + determined by checking if anything in the compiler output matches any of + the specified regular expressions. - The compile and link commands can be influenced by setting any of the - following variables prior to calling ``check_cxx_source_compiles()``: + .. rubric:: Variables Affecting the Check + + The following variables may be set before calling this command to modify + the way the check is run: .. include:: /module/include/CMAKE_REQUIRED_FLAGS.rst @@ -49,6 +65,35 @@ Check once if C++ source code can be built. .. include:: /module/include/CMAKE_REQUIRED_QUIET.rst + .. include:: /module/include/CMAKE_TRY_COMPILE_TARGET_TYPE.rst + +Examples +^^^^^^^^ + +The following example demonstrates how to check whether the C++ compiler +supports a specific language feature. In this case, the check verifies if +the compiler supports ``C++11`` lambda expressions. The result is stored +in the internal cache variable ``HAVE_CXX11_LAMBDAS``: + +.. code-block:: cmake + + include(CheckCXXSourceCompiles) + + check_cxx_source_compiles(" + int main() + { + auto lambda = []() { return 42; }; + return lambda(); + } + " HAVE_CXX11_LAMBDAS) + +See Also +^^^^^^^^ + +* The :module:`CheckSourceCompiles` module for a more general command to + check whether source can be built. +* The :module:`CheckSourceRuns` module to check whether source can be built + and run. #]=======================================================================] include_guard(GLOBAL) diff --git a/Modules/CheckFortranSourceCompiles.cmake b/Modules/CheckFortranSourceCompiles.cmake index 33585635f1..ea02cd5acc 100644 --- a/Modules/CheckFortranSourceCompiles.cmake +++ b/Modules/CheckFortranSourceCompiles.cmake @@ -7,41 +7,65 @@ CheckFortranSourceCompiles .. versionadded:: 3.1 -Check once if Fortran source code can be built. +This module provides a command to check whether a Fortran source can be +built. + +Load this module in a CMake project with: + +.. code-block:: cmake + + include(CheckFortranSourceCompiles) + +Commands +^^^^^^^^ + +This module provides the following command: .. command:: check_fortran_source_compiles + Checks once whether the given Fortran source code can be built: + .. code-block:: cmake - check_fortran_source_compiles( - [FAIL_REGEX ...] - [SRC_EXT ] + check_fortran_source_compiles( + + + [FAIL_REGEX ...] + [SRC_EXT ] ) - Check once that the source supplied in ```` can be built. The result is - stored in the internal cache variable specified by ````, with - boolean ``true`` for success and boolean ``false`` for failure. + This command checks once that the source supplied in ```` can be + compiled (and linked into an executable). The result of the check is + stored in the internal cache variable specified by ````. - If ``FAIL_REGEX`` is provided, then failure is determined by checking - if anything in the compiler output matches any of the specified regular - expressions. + The arguments are: - By default, the test source file will be given a ``.F`` file extension. The - ``SRC_EXT`` option can be used to override this with ``.`` instead-- - ``.F90`` is a typical choice. + ```` + Fortran source code to check. This must be an entire program, as + written in a file containing the body block. All symbols used in the + source code are expected to be declared as usual in their corresponding + headers. - See also :command:`check_source_compiles` for a more general command syntax. + ```` + Variable name of an internal cache variable to store the result of the + check, with boolean true for success and boolean false for failure. - See also :command:`check_source_runs` to run compiled source. + ``FAIL_REGEX ...`` + If this option is provided with one or more regular expressions, then + failure is determined by checking if anything in the compiler output + matches any of the specified regular expressions. - Internally, :command:`try_compile` is used to compile the source. If - :variable:`CMAKE_TRY_COMPILE_TARGET_TYPE` is set to ``EXECUTABLE`` (default), - the source is compiled and linked as an executable program. If set to - ``STATIC_LIBRARY``, the source is compiled but not linked. In any case, all - functions must be declared as usual. + ``SRC_EXT `` + .. versionadded:: 3.7 - The compile and link commands can be influenced by setting any of the - following variables prior to calling ``check_fortran_source_compiles()``: + By default, the test source file used for the check will be given a + ``.F`` file extension. This option can be used to override this with + ``.`` instead - ``.F90`` is a typical choice. + + .. rubric:: Variables Affecting the Check + + The following variables may be set before calling this command to modify + the way the check is run: .. include:: /module/include/CMAKE_REQUIRED_FLAGS.rst @@ -57,6 +81,33 @@ Check once if Fortran source code can be built. .. include:: /module/include/CMAKE_REQUIRED_QUIET.rst + .. include:: /module/include/CMAKE_TRY_COMPILE_TARGET_TYPE.rst + +Examples +^^^^^^^^ + +Checking whether the Fortran compiler supports the ``pure`` procedure +attribute: + +.. code-block:: cmake + + include(CheckFortranSourceCompiles) + + check_fortran_source_compiles(" + pure subroutine foo() + end subroutine + program test + call foo() + end + " HAVE_PURE SRC_EXT "F90") + +See Also +^^^^^^^^ + +* The :module:`CheckSourceCompiles` module for a more general command to + check whether source can be built. +* The :module:`CheckSourceRuns` module to check whether source can be built + and run. #]=======================================================================] include_guard(GLOBAL) diff --git a/Modules/CheckOBJCSourceCompiles.cmake b/Modules/CheckOBJCSourceCompiles.cmake index 74b1ac6ca5..94d37f3093 100644 --- a/Modules/CheckOBJCSourceCompiles.cmake +++ b/Modules/CheckOBJCSourceCompiles.cmake @@ -7,35 +7,52 @@ CheckOBJCSourceCompiles .. versionadded:: 3.16 -Check once if Objective-C source can be built. +This module provides a command to check whether an Objective-C source can +be built. + +Load this module in a CMake project with: + +.. code-block:: cmake + + include(CheckOBJCSourceCompiles) + +Commands +^^^^^^^^ + +This module provides the following command: .. command:: check_objc_source_compiles + Checks once whether the given Objective-C source code can be built: + .. code-block:: cmake - check_objc_source_compiles( - [FAIL_REGEX [...]]) + check_objc_source_compiles( [FAIL_REGEX ...]) - Check once that the source supplied in ```` can be built. The result is - stored in the internal cache variable specified by ````, with - boolean ``true`` for success and boolean ``false`` for failure. + This command checks once that the source supplied in ```` can be + compiled (and linked into an executable). The result of the check is + stored in the internal cache variable specified by ````. - If ``FAIL_REGEX`` is provided, then failure is determined by checking - if anything in the compiler output matches any of the specified regular - expressions. + The arguments are: - Internally, :command:`try_compile` is used to compile the source. If - :variable:`CMAKE_TRY_COMPILE_TARGET_TYPE` is set to ``EXECUTABLE`` (default), - the source is compiled and linked as an executable program. If set to - ``STATIC_LIBRARY``, the source is compiled but not linked. In any case, all - functions must be declared as usual. + ```` + Source code to check. This must be an entire program, as written in a + file containing the body block. All symbols used in the source code + are expected to be declared as usual in their corresponding headers. - See also :command:`check_source_compiles` for a more general command syntax. + ```` + Variable name of an internal cache variable to store the result of the + check, with boolean true for success and boolean false for failure. - See also :command:`check_source_runs` to run compiled source. + ``FAIL_REGEX ...`` + If this option is provided with one or more regular expressions, then + failure is determined by checking if anything in the compiler output + matches any of the specified regular expressions. - The compile and link commands can be influenced by setting any of the - following variables prior to calling ``check_objc_source_compiles()`` + .. rubric:: Variables Affecting the Check + + The following variables may be set before calling this command to modify + the way the check is run: .. include:: /module/include/CMAKE_REQUIRED_FLAGS.rst @@ -51,6 +68,35 @@ Check once if Objective-C source can be built. .. include:: /module/include/CMAKE_REQUIRED_QUIET.rst + .. include:: /module/include/CMAKE_TRY_COMPILE_TARGET_TYPE.rst + +Examples +^^^^^^^^ + +In the following example, this module is used to check whether the provided +Objective-C source code compiles and links. Result of the check is stored in +the internal cache variable ``HAVE_WORKING_CODE``. + +.. code-block:: cmake + + include(CheckOBJCSourceCompiles) + + check_objc_source_compiles(" + #import + int main() + { + NSObject *foo; + return 0; + } + " HAVE_WORKING_CODE) + +See Also +^^^^^^^^ + +* The :module:`CheckSourceCompiles` module for a more general command to + check whether source can be built. +* The :module:`CheckSourceRuns` module to check whether source can be built + and run. #]=======================================================================] include_guard(GLOBAL) diff --git a/Modules/CheckOBJCXXSourceCompiles.cmake b/Modules/CheckOBJCXXSourceCompiles.cmake index e74a6e100b..7d76f7b669 100644 --- a/Modules/CheckOBJCXXSourceCompiles.cmake +++ b/Modules/CheckOBJCXXSourceCompiles.cmake @@ -7,35 +7,53 @@ CheckOBJCXXSourceCompiles .. versionadded:: 3.16 -Check once if Objective-C++ source can be built. +This module provides a command to check whether an Objective-C++ source can +be built. + +Load this module in a CMake project with: + +.. code-block:: cmake + + include(CheckOBJCXXSourceCompiles) + +Commands +^^^^^^^^ + +This module provides the following command: .. command:: check_objcxx_source_compiles + Checks once whether the given Objective-C++ source code can be built: + .. code-block:: cmake - check_objcxx_source_compiles( - [FAIL_REGEX [...]]) + check_objcxx_source_compiles( [FAIL_REGEX ...]) - Check once that the source supplied in ```` can be built. The result is - stored in the internal cache variable specified by ````, with - boolean ``true`` for success and boolean ``false`` for failure. + This command checks once that the source supplied in ```` can be + compiled (and linked into an executable). The result of the check is + stored in the internal cache variable specified by ````. - If ``FAIL_REGEX`` is provided, then failure is determined by checking - if anything in the compiler output matches any of the specified regular - expressions. + The arguments are: - Internally, :command:`try_compile` is used to compile the source. If - :variable:`CMAKE_TRY_COMPILE_TARGET_TYPE` is set to ``EXECUTABLE`` (default), - the source is compiled and linked as an executable program. If set to - ``STATIC_LIBRARY``, the source is compiled but not linked. In any case, all - functions must be declared as usual. + ```` + Objective-C++ source code to check. This must be an entire program, as + written in a file containing the body block. All symbols used in the + source code are expected to be declared as usual in their corresponding + headers. - See also :command:`check_source_compiles` for a more general command syntax. + ```` + Variable name of an internal cache variable to store the result of the + check, with boolean true for success and boolean false for failure. - See also :command:`check_source_runs` to run compiled source. + ``FAIL_REGEX ...`` + If this option is provided with one or more regular expressions, then + failure is determined by checking if anything in the compiler output + matches any of the specified regular expressions. - The compile and link commands can be influenced by setting any of the - following variables prior to calling ``check_objcxx_source_compiles()`` + .. rubric:: Variables Affecting the Check + + The following variables may be set before calling this command to modify + the way the check is run: .. include:: /module/include/CMAKE_REQUIRED_FLAGS.rst @@ -51,6 +69,37 @@ Check once if Objective-C++ source can be built. .. include:: /module/include/CMAKE_REQUIRED_QUIET.rst + .. include:: /module/include/CMAKE_TRY_COMPILE_TARGET_TYPE.rst + +Examples +^^^^^^^^ + +In the following example, this module is used to check whether the provided +Objective-C++ source code can be compiled and linked. Result of the check +is stored in the internal cache variable ``HAVE_WORKING_CODE``. + +.. code-block:: cmake + + include(CheckOBJCXXSourceCompiles) + + check_objcxx_source_compiles(" + #include + #import + int main() + { + std::vector v; + NSObject *foo; + return 0; + } + " HAVE_WORKING_CODE) + +See Also +^^^^^^^^ + +* The :module:`CheckSourceCompiles` module for a more general command to + check whether source can be built. +* The :module:`CheckSourceRuns` module to check whether source can be built + and run. #]=======================================================================] include_guard(GLOBAL) diff --git a/Modules/CheckSourceCompiles.cmake b/Modules/CheckSourceCompiles.cmake index f225516a61..96246da37d 100644 --- a/Modules/CheckSourceCompiles.cmake +++ b/Modules/CheckSourceCompiles.cmake @@ -8,63 +8,77 @@ CheckSourceCompiles .. versionadded:: 3.19 -Check once if source code can be built for a given language. +This module provides a command that checks whether a source code can be +built for a given language. + +Load this module in a CMake project with: + +.. code-block:: cmake + + include(CheckSourceCompiles) + +Commands +^^^^^^^^ + +This module provides the following command: .. command:: check_source_compiles - .. code-block:: cmake - - check_source_compiles( - [FAIL_REGEX [...]] - [SRC_EXT ]) - - Check once that the source supplied in ```` can be built for code - language ````. The result is stored in the internal cache variable - specified by ````, with boolean ``true`` for success and - boolean ``false`` for failure. - - If ``FAIL_REGEX`` is provided, then failure is determined by checking - if anything in the compiler output matches any of the specified regular - expressions. - - By default, the test source file will be given a file extension that matches - the requested language. The ``SRC_EXT`` option can be used to override this - with ``.`` instead. - - The C example checks if the compiler supports the ``noreturn`` attribute: + Checks once whether the given source code can be built for the given + language: .. code-block:: cmake - set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY") + check_source_compiles( + + + + [FAIL_REGEX ...] + [SRC_EXT ] + ) - check_source_compiles(C - "#if !__has_c_attribute(noreturn) - #error \"No noreturn attribute\" - #endif" - HAVE_NORETURN) + This command checks once that the source supplied in ```` can be + compiled (and linked into an executable) for code language ````. + The result of the check is stored in the internal cache variable specified + by ````. - The Fortran example checks if the compiler supports the ``pure`` procedure - attribute: + The arguments are: - .. code-block:: cmake + ```` + Language of the source code to check. Supported languages are: + ``C``, ``CXX``, ``CUDA``, ``Fortran``, ``HIP``, ``ISPC``, ``OBJC``, + ``OBJCXX``, and ``Swift``. - set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY") + .. versionadded:: 3.21 + Support for ``HIP`` language. - check_source_compiles(Fortran - "pure subroutine foo() - end subroutine" - HAVE_PURE) + .. versionadded:: 3.26 + Support for ``Swift`` language. - Internally, :command:`try_compile` is used to compile the source. If - :variable:`CMAKE_TRY_COMPILE_TARGET_TYPE` is set to ``EXECUTABLE`` (default), - the source is compiled and linked as an executable program. If set to - ``STATIC_LIBRARY``, the source is compiled but not linked. In any case, all - functions must be declared as usual. + ```` + The source code to check. This must be an entire program, as written + in a file containing the body block. All symbols used in the source code + are expected to be declared as usual in their corresponding headers. - See also :command:`check_source_runs` to run compiled source. + ```` + Variable name of an internal cache variable to store the result of the + check, with boolean true for success and boolean false for failure. - The compile and link commands can be influenced by setting any of the - following variables prior to calling ``check_source_compiles()``: + ``FAIL_REGEX ...`` + If one or more regular expression patterns are provided, then failure is + determined by checking if anything in the compiler output matches any of + the specified regular expressions. + + ``SRC_EXT `` + By default, the internal test source file used for the check will be + given a file extension that matches the requested language (e.g., ``.c`` + for C, ``.cxx`` for C++, ``.F90`` for Fortran, etc.). This option can + be used to override this with the ``.`` instead. + + .. rubric:: Variables Affecting the Check + + The following variables may be set before calling this command to modify + the way the check is run: .. include:: /module/include/CMAKE_REQUIRED_FLAGS.rst @@ -80,6 +94,108 @@ Check once if source code can be built for a given language. .. include:: /module/include/CMAKE_REQUIRED_QUIET.rst + .. include:: /module/include/CMAKE_TRY_COMPILE_TARGET_TYPE.rst + +Examples +^^^^^^^^ + +Example: Basic Usage +"""""""""""""""""""" + +The following example demonstrates how to check whether the C++ compiler +supports a specific language feature using this module. In this case, the +check verifies if the compiler supports ``C++11`` lambda expressions. The +result is stored in the internal cache variable ``HAVE_CXX11_LAMBDAS``: + +.. code-block:: cmake + + include(CheckSourceCompiles) + + check_source_compiles(CXX " + int main() + { + auto lambda = []() { return 42; }; + return lambda(); + } + " HAVE_CXX11_LAMBDAS) + +Example: Checking Code With Bracket Argument +"""""""""""""""""""""""""""""""""""""""""""" + +The following example shows how to check whether the C compiler supports the +``noreturn`` attribute. Code is supplied using the :ref:`Bracket Argument` +for easier embedded quotes handling: + +.. code-block:: cmake + :force: + + include(CheckSourceCompiles) + + check_source_compiles(C [[ + #if !__has_c_attribute(noreturn) + # error "No noreturn attribute" + #endif + int main(void) { return 0; } + ]] HAVE_NORETURN) + +Example: Performing a Check Without Linking +""""""""""""""""""""""""""""""""""""""""""" + +In the following example, this module is used to perform a compile-only +check of Fortran source code, whether the compiler supports the ``pure`` +procedure attribute: + +.. code-block:: cmake + + include(CheckSourceCompiles) + + block() + set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY") + + check_source_compiles( + Fortran + "pure subroutine foo() + end subroutine" + HAVE_PURE + ) + endblock() + +Example: Isolated Check +""""""""""""""""""""""" + +In the following example, this module is used in combination with the +:module:`CMakePushCheckState` module to modify required libraries when +checking whether the PostgreSQL ``PGVerbosity`` enum contains +``PQERRORS_SQLSTATE`` (available as of PostgreSQL version 12): + +.. code-block:: cmake + + include(CheckSourceCompiles) + include(CMakePushCheckState) + + find_package(PostgreSQL) + + if(TARGET PostgreSQL::PostgreSQL) + cmake_push_check_state(RESET) + set(CMAKE_REQUIRED_LIBRARIES PostgreSQL::PostgreSQL) + + check_source_compiles(C " + #include + int main(void) + { + PGVerbosity e = PQERRORS_SQLSTATE; + (void)e; + return 0; + } + " HAVE_PQERRORS_SQLSTATE) + cmake_pop_check_state() + endif() + +See Also +^^^^^^^^ + +* The :module:`CheckSourceRuns` module to check whether the source code can + be built and also run. #]=======================================================================] include_guard(GLOBAL) diff --git a/Tests/FortranModules/CMakeLists.txt b/Tests/FortranModules/CMakeLists.txt index ecb456bde3..8dd0b13de7 100644 --- a/Tests/FortranModules/CMakeLists.txt +++ b/Tests/FortranModules/CMakeLists.txt @@ -19,7 +19,7 @@ endif() if(NOT DEFINED CMake_TEST_Fortran_SUBMODULES) include(CheckFortranSourceCompiles) - CHECK_Fortran_SOURCE_COMPILES([[ + check_fortran_source_compiles([[ module parent interface module function id(x) diff --git a/Tests/FortranOnly/CMakeLists.txt b/Tests/FortranOnly/CMakeLists.txt index 4f2724deb1..6917cef402 100644 --- a/Tests/FortranOnly/CMakeLists.txt +++ b/Tests/FortranOnly/CMakeLists.txt @@ -63,7 +63,7 @@ add_dependencies(checksayhello sayhello) include(CheckFortranSourceCompiles) unset(HAVE_PRINT CACHE) -CHECK_Fortran_SOURCE_COMPILES([[ +check_fortran_source_compiles([[ PROGRAM TEST_HAVE_PRINT PRINT *, 'Hello' END