Skip to content

Commit

Permalink
Add a CMake build target for Propeller tests
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 665393981
  • Loading branch information
dhoekwater authored and copybara-github committed Aug 20, 2024
1 parent b7a1440 commit 37fbf99
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
64 changes: 64 additions & 0 deletions CMake/GenerateTests.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#
# Copyright 2024 The Propeller Authors.
#
# 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
#
# https://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.
#

include(CMakeParseArguments)
include(GoogleTest)

# propeller_generate_tests()
#
# CMake function to generate test executables for Propeller tests.
#
# Parameters:
# SRCS: List of the source files for the test binary.
# DEPS: List of the dependencies for the test binary.
#
# Note: propeller_generate_tests will generate a distinct test executable for
# each source file and add it to ctest.
#
# Usage:
# propeller_generate_tests(
# SRCS
# a_test.cc
# b_test.cc
# DEPS
# propeller_test_utils
# GTest::gmock
# GTest::gtest_main
# )
function (propeller_generate_tests)
if(NOT BUILD_TESTING)
return()
endif()

cmake_parse_arguments(PROPELLER_GENERATE_TESTS
""
""
"SRCS;DEPS"
${ARGN}
)

foreach(_TEST ${PROPELLER_GENERATE_TESTS_SRCS})
# Create the executable and add dependencies.
get_filename_component(TName ${_TEST} NAME_WE)
add_executable(${TName} "")
target_sources(${TName} PRIVATE ${_TEST})
target_link_libraries(${TName} ${PROPELLER_GENERATE_TESTS_DEPS})

# Add the test to ctest. If we want each test to be a separate ctest target,
# we could instead use gtest_discover_tests(${TName}).
add_test(NAME ${TName} COMMAND ${TName})
endforeach()
endfunction()
15 changes: 14 additions & 1 deletion propeller/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,17 @@
add_library(propeller_lib OBJECT
branch_aggregation.cc
)
target_link_libraries(propeller_lib absl::base)
target_link_libraries(propeller_lib absl::base)

# Build all CXX tests into a unified test target.
include(${CMAKE_HOME_DIRECTORY}/CMake/GenerateTests.cmake)
propeller_generate_tests(
SRCS
branch_aggregation_test.cc
DEPS
propeller_lib
absl::base
absl::hash
absl::flat_hash_set
GTest::gmock_main
)

0 comments on commit 37fbf99

Please sign in to comment.