|
| 1 | +# Copyright 2014 Open Source Robotics Foundation, Inc. |
| 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, automatically linking and including dependencies. |
| 17 | +# |
| 18 | +# Call add_executable(target ARGN), link it against the gtest libraries |
| 19 | +# and 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 | +# |
| 25 | +# All arguments of the CMake function ``add_executable()`` can be |
| 26 | +# used beside the custom arguments ``DIRECTORY`` and |
| 27 | +# ``NO_TARGET_LINK_LIBRARIES``. |
| 28 | +# |
| 29 | +# :param target: the name of the executable target |
| 30 | +# :type target: string |
| 31 | +# :param DIRECTORY: the directory to recursively glob for source |
| 32 | +# files with the following extensions: c, cc, cpp, cxx |
| 33 | +# :type DIRECTORY: string |
| 34 | +# :param NO_TARGET_LINK_LIBRARIES: if set skip linking against |
| 35 | +# ``${PROJECT_NAME}_LIBRARIES`` |
| 36 | +# :type NO_TARGET_LINK_LIBRARIES: option |
| 37 | +# :param ARGN: the list of source files |
| 38 | +# :type ARGN: list of strings |
| 39 | +# :param RUNNER: the path to the test runner script |
| 40 | +# (default: see ament_add_test). |
| 41 | +# :type RUNNER: string |
| 42 | +# :param TIMEOUT: the test timeout in seconds, |
| 43 | +# default defined by ``ament_add_test()`` |
| 44 | +# :type TIMEOUT: integer |
| 45 | +# :param WORKING_DIRECTORY: the working directory for invoking the |
| 46 | +# executable in, default defined by ``ament_add_test()`` |
| 47 | +# :type WORKING_DIRECTORY: string |
| 48 | +# :param SKIP_LINKING_MAIN_LIBRARIES: if set skip linking against the gtest |
| 49 | +# main libraries |
| 50 | +# :type SKIP_LINKING_MAIN_LIBRARIES: option |
| 51 | +# :param SKIP_TEST: if set mark the test as being skipped |
| 52 | +# :type SKIP_TEST: option |
| 53 | +# :param ENV: list of env vars to set; listed as ``VAR=value`` |
| 54 | +# :type ENV: list of strings |
| 55 | +# :param APPEND_ENV: list of env vars to append if already set, otherwise set; |
| 56 | +# listed as ``VAR=value`` |
| 57 | +# :type APPEND_ENV: list of strings |
| 58 | +# :param APPEND_LIBRARY_DIRS: list of library dirs to append to the appropriate |
| 59 | +# OS specific env var, a la LD_LIBRARY_PATH |
| 60 | +# :type APPEND_LIBRARY_DIRS: list of strings |
| 61 | +# |
| 62 | +# @public |
| 63 | +# |
| 64 | +macro(ament_auto_add_gtest target) |
| 65 | + cmake_parse_arguments(ARG |
| 66 | + "WIN32;MACOSX_BUNDLE;EXCLUDE_FROM_ALL;NO_TARGET_LINK_LIBRARIES;SKIP_LINKING_MAIN_LIBRARIES;SKIP_TEST" |
| 67 | + "RUNNER;TIMEOUT;WORKING_DIRECTORY;DIRECTORY" |
| 68 | + "APPEND_ENV;APPEND_LIBRARY_DIRS;ENV" |
| 69 | + ${ARGN}) |
| 70 | + if(NOT ARG_DIRECTORY AND NOT ARG_UNPARSED_ARGUMENTS) |
| 71 | + message(FATAL_ERROR "ament_auto_add_executable() called without any " |
| 72 | + "source files and without a DIRECTORY argument") |
| 73 | + endif() |
| 74 | + |
| 75 | + set(_source_files "") |
| 76 | + if(ARG_DIRECTORY) |
| 77 | + # glob all source files |
| 78 | + file( |
| 79 | + GLOB_RECURSE |
| 80 | + _source_files |
| 81 | + RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" |
| 82 | + "${ARG_DIRECTORY}/*.c" |
| 83 | + "${ARG_DIRECTORY}/*.cc" |
| 84 | + "${ARG_DIRECTORY}/*.cpp" |
| 85 | + "${ARG_DIRECTORY}/*.cxx" |
| 86 | + ) |
| 87 | + if(NOT _source_files) |
| 88 | + message(FATAL_ERROR "ament_auto_add_executable() no source files found " |
| 89 | + "in directory '${CMAKE_CURRENT_SOURCE_DIR}/${ARG_DIRECTORY}'") |
| 90 | + endif() |
| 91 | + endif() |
| 92 | + |
| 93 | + # parse again to "remove" custom arguments |
| 94 | + cmake_parse_arguments(ARG "NO_TARGET_LINK_LIBRARIES" "DIRECTORY" "" ${ARGN}) |
| 95 | + ament_add_gtest("${target}" ${ARG_UNPARSED_ARGUMENTS} ${_source_files}) |
| 96 | + |
| 97 | + # add include directory of this package if it exists |
| 98 | + if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/include") |
| 99 | + target_include_directories("${target}" PUBLIC |
| 100 | + "${CMAKE_CURRENT_SOURCE_DIR}/include") |
| 101 | + endif() |
| 102 | + # link against other libraries of this package |
| 103 | + if(NOT ${PROJECT_NAME}_LIBRARIES STREQUAL "" AND |
| 104 | + NOT ARG_NO_TARGET_LINK_LIBRARIES) |
| 105 | + target_link_libraries("${target}" ${${PROJECT_NAME}_LIBRARIES}) |
| 106 | + endif() |
| 107 | + |
| 108 | + # add exported information from found build dependencies |
| 109 | + ament_target_dependencies(${target} ${${PROJECT_NAME}_FOUND_BUILD_DEPENDS}) |
| 110 | +endmacro() |
0 commit comments