|
| 1 | +# Copyright 2021 Whitley Software Services, LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# |
| 16 | +# Add a gtest with all found test dependencies. |
| 17 | +# |
| 18 | +# Call add_executable(target ARGN), link it against the gtest libraries |
| 19 | +# and all found test dependencies, and then register the executable as a test. |
| 20 | +# |
| 21 | +# If gtest is not available the specified target is not being created and |
| 22 | +# therefore the target existence should be checked before being used. |
| 23 | +# |
| 24 | +# :param target: the target name which will also be used as the test name |
| 25 | +# :type target: string |
| 26 | +# :param ARGN: the list of source files |
| 27 | +# :type ARGN: list of strings |
| 28 | +# :param RUNNER: the path to the test runner script (default: |
| 29 | +# see ament_add_test). |
| 30 | +# :type RUNNER: string |
| 31 | +# :param TIMEOUT: the test timeout in seconds, |
| 32 | +# default defined by ``ament_add_test()`` |
| 33 | +# :type TIMEOUT: integer |
| 34 | +# :param WORKING_DIRECTORY: the working directory for invoking the |
| 35 | +# executable in, default defined by ``ament_add_test()`` |
| 36 | +# :type WORKING_DIRECTORY: string |
| 37 | +# :param SKIP_LINKING_MAIN_LIBRARIES: if set skip linking against the gtest |
| 38 | +# main libraries |
| 39 | +# :type SKIP_LINKING_MAIN_LIBRARIES: option |
| 40 | +# :param SKIP_TEST: if set mark the test as being skipped |
| 41 | +# :type SKIP_TEST: option |
| 42 | +# :param ENV: list of env vars to set; listed as ``VAR=value`` |
| 43 | +# :type ENV: list of strings |
| 44 | +# :param APPEND_ENV: list of env vars to append if already set, otherwise set; |
| 45 | +# listed as ``VAR=value`` |
| 46 | +# :type APPEND_ENV: list of strings |
| 47 | +# :param APPEND_LIBRARY_DIRS: list of library dirs to append to the appropriate |
| 48 | +# OS specific env var, a la LD_LIBRARY_PATH |
| 49 | +# :type APPEND_LIBRARY_DIRS: list of strings |
| 50 | +# |
| 51 | +# @public |
| 52 | +# |
| 53 | +macro(ament_auto_add_gtest target) |
| 54 | + cmake_parse_arguments(_ARGN |
| 55 | + "SKIP_LINKING_MAIN_LIBRARIES;SKIP_TEST" |
| 56 | + "RUNNER;TIMEOUT;WORKING_DIRECTORY" |
| 57 | + "APPEND_ENV;APPEND_LIBRARY_DIRS;ENV" |
| 58 | + ${ARGN}) |
| 59 | + if(NOT _ARGN_UNPARSED_ARGUMENTS) |
| 60 | + message(FATAL_ERROR |
| 61 | + "ament_auto_add_gtest() must be invoked with at least one source file") |
| 62 | + endif() |
| 63 | + |
| 64 | + # add executable |
| 65 | + set(_argn_executable ${_ARGN_UNPARSED_ARGUMENTS}) |
| 66 | + if(_ARG_SKIP_LINKING_MAIN_LIBRARIES) |
| 67 | + list(APPEND _argn_executable "SKIP_LINKING_MAIN_LIBRARIES") |
| 68 | + endif() |
| 69 | + ament_add_gtest_executable("${target}" ${_argn_executable}) |
| 70 | + |
| 71 | + # add include directory of this package if it exists |
| 72 | + if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/include") |
| 73 | + target_include_directories("${target}" PUBLIC |
| 74 | + "${CMAKE_CURRENT_SOURCE_DIR}/include") |
| 75 | + endif() |
| 76 | + |
| 77 | + # link against other libraries of this package |
| 78 | + if(NOT ${PROJECT_NAME}_LIBRARIES STREQUAL "") |
| 79 | + target_link_libraries("${target}" ${${PROJECT_NAME}_LIBRARIES}) |
| 80 | + endif() |
| 81 | + |
| 82 | + # add exported information from found dependencies |
| 83 | + ament_target_dependencies(${target} |
| 84 | + ${${PROJECT_NAME}_FOUND_BUILD_DEPENDS} |
| 85 | + ${${PROJECT_NAME}_FOUND_TEST_DEPENDS} |
| 86 | + ) |
| 87 | + |
| 88 | + # add test |
| 89 | + set(_argn_test "") |
| 90 | + if(_ARG_RUNNER) |
| 91 | + list(APPEND _argn_test "RUNNER" "${_ARG_RUNNER}") |
| 92 | + endif() |
| 93 | + if(_ARG_TIMEOUT) |
| 94 | + list(APPEND _argn_test "TIMEOUT" "${_ARG_TIMEOUT}") |
| 95 | + endif() |
| 96 | + if(_ARG_WORKING_DIRECTORY) |
| 97 | + list(APPEND _argn_test "WORKING_DIRECTORY" "${_ARG_WORKING_DIRECTORY}") |
| 98 | + endif() |
| 99 | + if(_ARG_SKIP_TEST) |
| 100 | + list(APPEND _argn_test "SKIP_TEST") |
| 101 | + endif() |
| 102 | + if(_ARG_ENV) |
| 103 | + list(APPEND _argn_test "ENV" ${_ARG_ENV}) |
| 104 | + endif() |
| 105 | + if(_ARG_APPEND_ENV) |
| 106 | + list(APPEND _argn_test "APPEND_ENV" ${_ARG_APPEND_ENV}) |
| 107 | + endif() |
| 108 | + if(_ARG_APPEND_LIBRARY_DIRS) |
| 109 | + list(APPEND _argn_test "APPEND_LIBRARY_DIRS" ${_ARG_APPEND_LIBRARY_DIRS}) |
| 110 | + endif() |
| 111 | + ament_add_gtest_test("${target}" ${_argn_test}) |
| 112 | +endmacro() |
0 commit comments