Warning, /frameworks/extra-cmake-modules/modules/ECMAddTests.cmake is written in an unsupported language. File is not indexed.

0001 # SPDX-FileCopyrightText: 2013 Alexander Richardson <arichardson.kde@gmail.com>
0002 # SPDX-FileCopyrightText: 2015 Alex Merry <alex.merry@kde.org>
0003 #
0004 # SPDX-License-Identifier: BSD-3-Clause
0005 
0006 #[=======================================================================[.rst:
0007 ECMAddTests
0008 -----------
0009 
0010 Convenience functions for adding tests.
0011 
0012 ::
0013 
0014   ecm_add_tests(<sources>
0015       LINK_LIBRARIES <library> [<library> [...]]
0016       [NAME_PREFIX <prefix>]
0017       [GUI]
0018       [TARGET_NAMES_VAR <target_names_var>]
0019       [TEST_NAMES_VAR <test_names_var>]
0020       [WORKING_DIRECTORY <dir>] #  Since 5.111
0021   )
0022 
0023 A convenience function for adding multiple tests, each consisting of a
0024 single source file. For each file in <sources>, an executable target will be
0025 created (the name of which will be the basename of the source file). This
0026 will be linked against the libraries given with ``LINK_LIBRARIES``. Each
0027 executable will be added as a test with the same name.
0028 
0029 If ``NAME_PREFIX`` is given, this prefix will be prepended to the test names, but
0030 not the target names. As a result, it will not prevent clashes between tests
0031 with the same name in different parts of the project, but it can be used to
0032 give an indication of where to look for a failing test.
0033 
0034 If the flag ``GUI`` is passed the test binaries will be GUI executables, otherwise
0035 the resulting binaries will be console applications (regardless of the value
0036 of ``CMAKE_WIN32_EXECUTABLE`` or ``CMAKE_MACOSX_BUNDLE``). Be aware that this changes
0037 the executable entry point on Windows (although some frameworks, such as Qt,
0038 abstract this difference away).
0039 
0040 The tests will be build with ``-DQT_FORCE_ASSERTS`` to enable assertions in the
0041 test executable even for release builds.
0042 
0043 The ``TARGET_NAMES_VAR`` and ``TEST_NAMES_VAR`` arguments, if given, should specify a
0044 variable name to receive the list of generated target and test names,
0045 respectively. This makes it convenient to apply properties to them as a
0046 whole, for example, using ``set_target_properties()`` or  ``set_tests_properties()``.
0047 
0048 The generated target executables will have the effects of ``ecm_mark_as_test()``
0049 (from the :module:`ECMMarkAsTest` module) applied to it.
0050 
0051 ``WORKING_DIRECTORY`` sets the test property `WORKING_DIRECTORY
0052 <https://cmake.org/cmake/help/latest/prop_test/WORKING_DIRECTORY.html>`_
0053 in which to execute the test. By default the test will be run in
0054 ``${CMAKE_CURRENT_BINARY_DIR}``. The working directory can be specified using
0055 generator expressions. Since 5.111.
0056 
0057 ::
0058 
0059   ecm_add_test(
0060       <sources>
0061       LINK_LIBRARIES <library> [<library> [...]]
0062       [TEST_NAME <name>]
0063       [NAME_PREFIX <prefix>]
0064       [GUI]
0065       [WORKING_DIRECTORY <dir>] #  Since 5.111
0066   )
0067 
0068 This is a single-test form of ``ecm_add_tests`` that allows multiple source files
0069 to be used for a single test. If using multiple source files, ``TEST_NAME`` must
0070 be given; this will be used for both the target and test names (and, as with
0071 ``ecm_add_tests()``, the ``NAME_PREFIX`` argument will be prepended to the test name).
0072 
0073 ``WORKING_DIRECTORY`` sets the test property `WORKING_DIRECTORY
0074 <https://cmake.org/cmake/help/latest/prop_test/WORKING_DIRECTORY.html>`_
0075 in which to execute the test. By default the test will be run in
0076 ``${CMAKE_CURRENT_BINARY_DIR}``. The working directory can be specified using
0077 generator expressions. Since 5.111.
0078 
0079 Since pre-1.0.0.
0080 #]=======================================================================]
0081 
0082 include(ECMMarkAsTest)
0083 include(ECMMarkNonGuiExecutable)
0084 
0085 function(ecm_add_test)
0086   set(options GUI)
0087   # TARGET_NAME_VAR and TEST_NAME_VAR are undocumented args used by
0088   # ecm_add_tests
0089   set(oneValueArgs TEST_NAME NAME_PREFIX TARGET_NAME_VAR TEST_NAME_VAR WORKING_DIRECTORY)
0090   set(multiValueArgs LINK_LIBRARIES)
0091   cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
0092   set(_sources ${ARG_UNPARSED_ARGUMENTS})
0093   list(LENGTH _sources _sourceCount)
0094   if(ARG_TEST_NAME)
0095     set(_targetname ${ARG_TEST_NAME})
0096   elseif(${_sourceCount} EQUAL "1")
0097     #use the source file name without extension as the testname
0098     get_filename_component(_targetname ${_sources} NAME_WE)
0099   else()
0100     #more than one source file passed, but no test name given -> error
0101     message(FATAL_ERROR "ecm_add_test() called with multiple source files but without setting \"TEST_NAME\"")
0102   endif()
0103 
0104   set(_testname ${ARG_NAME_PREFIX}${_targetname})
0105   set(gui_args)
0106   if(ARG_GUI)
0107       set(gui_args WIN32 MACOSX_BUNDLE)
0108   endif()
0109   add_executable(${_targetname} ${gui_args} ${_sources})
0110   if(NOT ARG_GUI)
0111     ecm_mark_nongui_executable(${_targetname})
0112   endif()
0113   set(test_args)
0114   if(DEFINED ARG_WORKING_DIRECTORY)
0115       list(APPEND test_args WORKING_DIRECTORY ${ARG_WORKING_DIRECTORY})
0116   endif()
0117   add_test(NAME ${_testname} COMMAND ${_targetname} ${test_args})
0118   target_link_libraries(${_targetname} ${ARG_LINK_LIBRARIES})
0119   target_compile_definitions(${_targetname} PRIVATE -DQT_FORCE_ASSERTS)
0120   ecm_mark_as_test(${_targetname})
0121   if (CMAKE_LIBRARY_OUTPUT_DIRECTORY)
0122     if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
0123        # https://stackoverflow.com/questions/59862894/how-do-i-make-a-list-in-cmake-with-the-semicolon-value
0124        set(PATHSEP "\\\;") # Don't want cmake to treat it like a list
0125     else() # e.g. Linux
0126       set(PATHSEP ":")
0127     endif()
0128     set(_plugin_path "$ENV{QT_PLUGIN_PATH}")
0129     set_property(TEST ${_testname} PROPERTY ENVIRONMENT "QT_PLUGIN_PATH=${CMAKE_LIBRARY_OUTPUT_DIRECTORY}${PATHSEP}${_plugin_path}")
0130   endif()
0131   if (ARG_TARGET_NAME_VAR)
0132     set(${ARG_TARGET_NAME_VAR} "${_targetname}" PARENT_SCOPE)
0133   endif()
0134   if (ARG_TEST_NAME_VAR)
0135     set(${ARG_TEST_NAME_VAR} "${_testname}" PARENT_SCOPE)
0136   endif()
0137 endfunction()
0138 
0139 function(ecm_add_tests)
0140   set(options GUI)
0141   set(oneValueArgs NAME_PREFIX TARGET_NAMES_VAR TEST_NAMES_VAR WORKING_DIRECTORY)
0142   set(multiValueArgs LINK_LIBRARIES)
0143   cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
0144   if(ARG_GUI)
0145     set(_exe_type GUI)
0146   else()
0147     set(_exe_type "")
0148   endif()
0149   set(test_args)
0150   if(DEFINED ARG_WORKING_DIRECTORY)
0151       list(APPEND test_args WORKING_DIRECTORY ${ARG_WORKING_DIRECTORY})
0152   endif()
0153   set(test_names)
0154   set(target_names)
0155   foreach(_test_source ${ARG_UNPARSED_ARGUMENTS})
0156     ecm_add_test(${_test_source}
0157       NAME_PREFIX ${ARG_NAME_PREFIX}
0158       LINK_LIBRARIES ${ARG_LINK_LIBRARIES}
0159       TARGET_NAME_VAR target_name
0160       TEST_NAME_VAR test_name
0161       ${_exe_type}
0162       ${test_args}
0163     )
0164     list(APPEND _test_names "${test_name}")
0165     list(APPEND _target_names "${target_name}")
0166   endforeach()
0167   if (ARG_TARGET_NAMES_VAR)
0168     set(${ARG_TARGET_NAMES_VAR} "${_target_names}" PARENT_SCOPE)
0169   endif()
0170   if (ARG_TEST_NAMES_VAR)
0171     set(${ARG_TEST_NAMES_VAR} "${_test_names}" PARENT_SCOPE)
0172   endif()
0173 endfunction()