File: osrf_testing_tools_cpp_require_googletest.cmake

package info (click to toggle)
ros2-osrf-testing-tools-cpp 1.5.2%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 572 kB
  • sloc: cpp: 3,049; xml: 31; makefile: 5
file content (152 lines) | stat: -rw-r--r-- 5,862 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# Copyright 2018 Open Source Robotics Foundation, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

if(OSRF_TESTING_TOOLS_CPP_REQUIRE_GOOGLETEST_CMAKE__INCLUDED)
  return()
endif()
set(OSRF_TESTING_TOOLS_CPP_REQUIRE_GOOGLETEST_CMAKE__INCLUDED TRUE)

include(${CMAKE_CURRENT_LIST_DIR}/osrf_testing_tools_cpp_get_googletest_versions.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/osrf_testing_tools_cpp_extract_and_build_googletest.cmake)
set(OSRF_TESTING_TOOLS_CPP_REQUIRE_GOOGLETEST_VERSION_SETUP)

#
# Provides and sets up googletest at the requested version.
#
# This package provides several versions of googletest, the best of which will
# be selected, built, and setup based on the version constraints given.
# This macro should only be called once, as two googletests being built
# simultaneously is not supported.
#
# :param VERSION_EQ: exact version to match, mutually exclusive with all other options
# :type VERSION_EQ: string
# :param VERSION_LT: version must be less than, mutually exclusive with VERSION_LTE
# :type VERSION_LT: string
# :param VERSION_LTE: version must be less than or equal, mutually exclusive with VERSION_LT
# :type VERSION_LTE: string
# :param VERSION_GT: version must be greater than, mutually exclusive with VERSION_GTE
# :type VERSION_GT: string
# :param VERSION_GTE: version must be greater than or equal, mutually exclusive with VERSION_GT
# :type VERSION_GTE: string
# :param VERSION_SELECTED_OUTPUT: name of variable to output selected version into, optional
# :type VERSION_SELECTED_OUTPUT: string
#
# @public
#
macro(osrf_testing_tools_cpp_require_googletest)
  # Make sure this was only called once.
  if(OSRF_TESTING_TOOLS_CPP_REQUIRE_GOOGLETEST_VERSION_SETUP)
    set(error_msg "osrf_testing_tools_cpp_require_googletest():")
    set(error_msg "${error_msg} googletest already setup for version")
    set(error_msg "${error_msg}  '${OSRF_TESTING_TOOLS_CPP_REQUIRE_GOOGLETEST_VERSION_SETUP}'")
    message(FATAL_ERROR ${error_msg})
  endif()

  # Check if any requested versions match constraints
  _osrf_testing_tools_cpp_require_googletest(__return_variable ${ARGN})
endmacro()

function(_osrf_testing_tools_cpp_filter_versions
  valid_version_indexes_var_name
  version_list
)
  # Arguments are validated in the calling function, _osrf_testing_tools_cpp_require_googletest()
  cmake_parse_arguments(ARG
    ""
    "VERSION_EQ;VERSION_LT;VERSION_LTE;VERSION_GT;VERSION_GTE;VERSION_SELECTED_OUTPUT"
    ""
    ${ARGN})

  set(valid_version_indexes)
  set(current_index 0)
  foreach(version IN LISTS ${version_list})
    set(version_valid FALSE)
    if(ARG_VERSION_EQ)
      if(version VERSION_EQUAL ARG_VERSION_EQ)
        set(version_valid TRUE)
      endif()
    else()
      if(ARG_VERSION_LT)
        if(version VERSION_LESS ARG_VERSION_LT)
          set(version_valid TRUE)
        endif()
      endif()
      if(ARG_VERSION_LTE)
        if(version VERSION_LESS ARG_VERSION_LTE OR version EQUAL ARG_VERSION_LTE)
          set(version_valid TRUE)
        endif()
      endif()
      if(ARG_VERSION_GT)
        if(version VERSION_GREATER ARG_VERSION_GT)
          set(version_valid TRUE)
        endif()
      endif()
      if(ARG_VERSION_GTE)
        if(version VERSION_GREATER ARG_VERSION_GTE OR version EQUAL ARG_VERSION_GTE)
          set(version_valid TRUE)
        endif()
      endif()
    endif()
    if(version_valid)
      list(APPEND valid_version_indexes ${current_index})
    endif()
    math(EXPR current_index "${current_index} + 1")
  endforeach()
  set(${valid_version_indexes_var_name} ${valid_version_indexes} PARENT_SCOPE)
endfunction()

function(_osrf_testing_tools_cpp_require_googletest return_variable)
  cmake_parse_arguments(ARG
    ""
    "VERSION_EQ;VERSION_LT;VERSION_LTE;VERSION_GT;VERSION_GTE;VERSION_SELECTED_OUTPUT;VENDOR_DIR"
    ""
    ${ARGN})
  if(ARG_UNPARSED_ARGUMENTS)
    message(FATAL_ERROR
      "osrf_testing_tools_cpp_require_googletest(): unknown args: ${ARG_UNPARSED_ARGUMENTS}")
  endif()

  # Check for use of mutually exclusive options
  if(ARG_VERSION_EQ AND (ARG_VERSION_LT OR ARG_VERSION_LTE OR ARG_VERSION_GT OR ARG_VERSION_GTE))
    message(FATAL_ERROR
      "osrf_testing_tools_cpp_require_googletest(): VERSION_EQ is a mutually exclusive option")
  endif()
  if(ARG_VERSION_GT AND ARG_VERSION_GTE)
    message(FATAL_ERROR
      "osrf_testing_tools_cpp_require_googletest(): VERSION_GT and VERSION_GTE used together")
  endif()
  if(ARG_VERSION_LT AND ARG_VERSION_LTE)
    message(FATAL_ERROR
      "osrf_testing_tools_cpp_require_googletest(): VERSION_LT and VERSION_LTE used together")
  endif()

  find_package(GTest REQUIRED)

  # First generate a list of versions which fit the constraints.
  _osrf_testing_tools_cpp_filter_versions(valid_version_indexes GTest_VERSION ${ARGN})
  list(LENGTH valid_version_indexes valid_version_indexes_length)
  if(valid_version_indexes_length EQUAL 0)
    message(FATAL_ERROR
      "No valid googletest version found in provided versions: ${versions}")
  endif()
  set(OSRF_TESTING_TOOLS_CPP_REQUIRE_GOOGLETEST_VERSION_SETUP ${GTest_VERSION})
  if(NOT TARGET gtest)
    add_library(gtest INTERFACE)
    target_link_libraries(gtest INTERFACE GTest::gtest)
  endif()
  if(NOT TARGET gtest_main)
    add_library(gtest_main INTERFACE)
    target_link_libraries(gtest_main INTERFACE GTest::gtest_main)
  endif()
endfunction()