Skip to content

Commit

Permalink
feat: cmake option to skip building mock libraries (#13673)
Browse files Browse the repository at this point in the history
To skip building mocks, applications can `cmake -DBUILD_TESTING=OFF -DGOOGLE_CLOUD_CPP_WITH_MOCKS=OFF`
dbolduc authored Feb 29, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 33be7d2 commit 7a8daed
Showing 11 changed files with 695 additions and 648 deletions.
30 changes: 22 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -165,6 +165,28 @@ This option is required to support package maintainers that split
OFF)
mark_as_advanced(GOOGLE_CLOUD_CPP_USE_INSTALLED_COMMON)

include(CMakeDependentOption)
cmake_dependent_option(
GOOGLE_CLOUD_CPP_WITH_MOCKS
[==[Build the google-cloud-cpp mocking libraries.

google-cloud-cpp offers mocking libraries with mock classes, to facilitate unit
testing of Cloud C++ clients. Consumers of this library that do not use the
provided mocks to test code involving the Cloud C++ clients may wish to turn
this flag off.]==]
ON
"NOT BUILD_TESTING"
ON)
mark_as_advanced(GOOGLE_CLOUD_CPP_WITH_MOCKS)

# The examples use exception handling to simplify the code. Therefore they
# cannot be compiled when exceptions are disabled, and applications cannot force
# the flag.
cmake_dependent_option(
GOOGLE_CLOUD_CPP_ENABLE_EXAMPLES "Compile the google-cloud-cpp examples."
ON "GOOGLE_CLOUD_CPP_ENABLE_CXX_EXCEPTIONS;BUILD_TESTING" OFF)
mark_as_advanced(GOOGLE_CLOUD_CPP_ENABLE_EXAMPLES)

include(GoogleCloudCppFeatures)

# Controls the list of features compiled by `google-cloud-cpp`.
@@ -231,14 +253,6 @@ if (BUILD_TESTING)
include(FindGMockWithTargets)
endif ()

# The examples use exception handling to simplify the code. Therefore they
# cannot be compiled when exceptions are disabled, and applications cannot force
# the flag.
cmake_dependent_option(
GOOGLE_CLOUD_CPP_ENABLE_EXAMPLES "Compile the google-cloud-cpp examples."
ON "GOOGLE_CLOUD_CPP_ENABLE_CXX_EXCEPTIONS;BUILD_TESTING" OFF)
mark_as_advanced(GOOGLE_CLOUD_CPP_ENABLE_EXAMPLES)

# Add any subdirectories configured in the application.

# `docfx/` must be included before any library directories. The tool is used to
66 changes: 34 additions & 32 deletions cmake/GoogleCloudCppLibrary.cmake
Original file line number Diff line number Diff line change
@@ -253,37 +253,6 @@ function (google_cloud_cpp_add_gapic_library library display_name)

add_library(${library_alias} ALIAS ${library_target})

# Create a header-only library for the mocks. We use a CMake `INTERFACE`
# library for these, a regular library would not work on macOS (where the
# library needs at least one .o file). Unfortunately INTERFACE libraries are
# a bit weird in that they need absolute paths for their sources.
file(
GLOB relative_mock_files
RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}"
${mocks_globs})
list(SORT relative_mock_files)
set(mock_files)
foreach (file IN LISTS relative_mock_files)
# We use a generator expression per the recommendation in:
# https://stackoverflow.com/a/62465051
list(APPEND mock_files
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/${file}>")
endforeach ()
add_library(${mocks_target} INTERFACE)
target_sources(${mocks_target} INTERFACE ${mock_files})
target_link_libraries(
${mocks_target} INTERFACE ${library_alias} GTest::gmock_main
GTest::gmock GTest::gtest)
set_target_properties(${mocks_target} PROPERTIES EXPORT_NAME
${library_alias}_mocks)
target_include_directories(
${mocks_target}
INTERFACE $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>
$<INSTALL_INTERFACE:include>)
target_compile_options(${mocks_target}
INTERFACE ${GOOGLE_CLOUD_CPP_EXCEPTIONS_FLAG})

# Get the destination directories based on the GNU recommendations.
include(GNUInstallDirs)

@@ -349,7 +318,40 @@ function (google_cloud_cpp_add_gapic_library library display_name)
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${library_target}"
COMPONENT google_cloud_cpp_development)

google_cloud_cpp_install_mocks("${library}" "${display_name}")
if (GOOGLE_CLOUD_CPP_WITH_MOCKS)
# Create a header-only library for the mocks. We use a CMake `INTERFACE`
# library for these, a regular library would not work on macOS (where
# the library needs at least one .o file). Unfortunately INTERFACE
# libraries are a bit weird in that they need absolute paths for their
# sources.
file(
GLOB relative_mock_files
RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}"
${mocks_globs})
list(SORT relative_mock_files)
set(mock_files)
foreach (file IN LISTS relative_mock_files)
# We use a generator expression per the recommendation in:
# https://stackoverflow.com/a/62465051
list(APPEND mock_files
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/${file}>")
endforeach ()
add_library(${mocks_target} INTERFACE)
target_sources(${mocks_target} INTERFACE ${mock_files})
target_link_libraries(
${mocks_target} INTERFACE ${library_alias} GTest::gmock_main
GTest::gmock GTest::gtest)
set_target_properties(${mocks_target} PROPERTIES EXPORT_NAME
${library_alias}_mocks)
target_include_directories(
${mocks_target}
INTERFACE $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>
$<INSTALL_INTERFACE:include>)
target_compile_options(${mocks_target}
INTERFACE ${GOOGLE_CLOUD_CPP_EXCEPTIONS_FLAG})
google_cloud_cpp_install_mocks("${library}" "${display_name}")
endif ()

# ${library_alias} must be defined before we can add the samples.
if (BUILD_TESTING AND GOOGLE_CLOUD_CPP_ENABLE_CXX_EXCEPTIONS)
153 changes: 79 additions & 74 deletions google/cloud/bigquery/bigquery_rest.cmake
Original file line number Diff line number Diff line change
@@ -162,37 +162,90 @@ target_compile_options(google_cloud_cpp_bigquery_rest
add_library(google-cloud-cpp::experimental-bigquery_rest ALIAS
google_cloud_cpp_bigquery_rest)

# Create a header-only library for the mocks.
add_library(google_cloud_cpp_bigquery_rest_mocks INTERFACE)
target_sources(
google_cloud_cpp_bigquery_rest_mocks
INTERFACE
${CMAKE_CURRENT_SOURCE_DIR}/v2/minimal/mocks/mock_dataset_connection.h
${CMAKE_CURRENT_SOURCE_DIR}/v2/minimal/mocks/mock_job_connection.h
${CMAKE_CURRENT_SOURCE_DIR}/v2/minimal/mocks/mock_project_connection.h
${CMAKE_CURRENT_SOURCE_DIR}/v2/minimal/mocks/mock_table_connection.h)
target_link_libraries(
google_cloud_cpp_bigquery_rest_mocks
INTERFACE google-cloud-cpp::experimental-bigquery_rest GTest::gmock_main
GTest::gmock GTest::gtest)
set_target_properties(
google_cloud_cpp_bigquery_rest_mocks
PROPERTIES EXPORT_NAME google-cloud-cpp::experimental-bigquery_rest_mocks)
target_include_directories(
google_cloud_cpp_bigquery_rest_mocks
INTERFACE $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>
$<INSTALL_INTERFACE:include>)
target_compile_options(google_cloud_cpp_bigquery_rest_mocks
INTERFACE ${GOOGLE_CLOUD_CPP_EXCEPTIONS_FLAG})
add_library(google-cloud-cpp::experimental-bigquery_rest_mocks ALIAS
google_cloud_cpp_bigquery_rest_mocks)
# Get the destination directories based on the GNU recommendations.
include(GNUInstallDirs)

# Export the CMake targets to make it easy to create configuration files.
install(
EXPORT google_cloud_cpp_bigquery_rest-targets
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/google_cloud_cpp_bigquery_rest"
COMPONENT google_cloud_cpp_development)

# Install the libraries and headers in the locations determined by
# GNUInstallDirs
install(
TARGETS google_cloud_cpp_bigquery_rest
EXPORT google_cloud_cpp_bigquery_rest-targets
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
COMPONENT google_cloud_cpp_runtime
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
COMPONENT google_cloud_cpp_runtime
NAMELINK_COMPONENT google_cloud_cpp_development
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
COMPONENT google_cloud_cpp_development)

google_cloud_cpp_install_headers("google_cloud_cpp_bigquery_rest"
"include/google/cloud/bigquery")

google_cloud_cpp_add_pkgconfig(
bigquery_rest "Experimental BigQuery REST client library"
"An experimental BigQuery C++ client library using REST for transport."
"google_cloud_cpp_rest_internal")

# Create and install the CMake configuration files.
include(CMakePackageConfigHelpers)
configure_file("config-rest.cmake.in"
"google_cloud_cpp_bigquery_rest-config.cmake" @ONLY)
write_basic_package_version_file(
"google_cloud_cpp_bigquery_rest-config-version.cmake"
VERSION ${PROJECT_VERSION}
COMPATIBILITY ExactVersion)

install(
FILES
"${CMAKE_CURRENT_BINARY_DIR}/google_cloud_cpp_bigquery_rest-config.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/google_cloud_cpp_bigquery_rest-config-version.cmake"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/google_cloud_cpp_bigquery_rest"
COMPONENT google_cloud_cpp_development)

if (GOOGLE_CLOUD_CPP_WITH_MOCKS)
# Create a header-only library for the mocks.
add_library(google_cloud_cpp_bigquery_rest_mocks INTERFACE)
target_sources(
google_cloud_cpp_bigquery_rest_mocks
INTERFACE
${CMAKE_CURRENT_SOURCE_DIR}/v2/minimal/mocks/mock_dataset_connection.h
${CMAKE_CURRENT_SOURCE_DIR}/v2/minimal/mocks/mock_job_connection.h
${CMAKE_CURRENT_SOURCE_DIR}/v2/minimal/mocks/mock_project_connection.h
${CMAKE_CURRENT_SOURCE_DIR}/v2/minimal/mocks/mock_table_connection.h
)
target_link_libraries(
google_cloud_cpp_bigquery_rest_mocks
INTERFACE google-cloud-cpp::experimental-bigquery_rest
GTest::gmock_main GTest::gmock GTest::gtest)
set_target_properties(
google_cloud_cpp_bigquery_rest_mocks
PROPERTIES EXPORT_NAME
google-cloud-cpp::experimental-bigquery_rest_mocks)
target_include_directories(
google_cloud_cpp_bigquery_rest_mocks
INTERFACE $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>
$<INSTALL_INTERFACE:include>)
target_compile_options(google_cloud_cpp_bigquery_rest_mocks
INTERFACE ${GOOGLE_CLOUD_CPP_EXCEPTIONS_FLAG})
add_library(google-cloud-cpp::experimental-bigquery_rest_mocks ALIAS
google_cloud_cpp_bigquery_rest_mocks)
create_bazel_config(google_cloud_cpp_bigquery_rest_mocks YEAR "2023")

google_cloud_cpp_install_headers("google_cloud_cpp_bigquery_rest_mocks"
"include/google/cloud/bigquery")
endif ()

# To avoid maintaining the list of files for the library, export them to a .bzl
# file.
include(CreateBazelConfig)
create_bazel_config(google_cloud_cpp_bigquery_rest YEAR "2023")
create_bazel_config(google_cloud_cpp_bigquery_rest_mocks YEAR "2023")

# Define the tests in a function so we have a new scope for variable names.
function (bigquery_rest_define_tests)
@@ -316,54 +369,6 @@ function (bigquery_rest_define_tests)
endforeach ()
endfunction ()

# Get the destination directories based on the GNU recommendations.
include(GNUInstallDirs)

# Export the CMake targets to make it easy to create configuration files.
install(
EXPORT google_cloud_cpp_bigquery_rest-targets
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/google_cloud_cpp_bigquery_rest"
COMPONENT google_cloud_cpp_development)

# Install the libraries and headers in the locations determined by
# GNUInstallDirs
install(
TARGETS google_cloud_cpp_bigquery_rest
EXPORT google_cloud_cpp_bigquery_rest-targets
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
COMPONENT google_cloud_cpp_runtime
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
COMPONENT google_cloud_cpp_runtime
NAMELINK_COMPONENT google_cloud_cpp_development
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
COMPONENT google_cloud_cpp_development)

google_cloud_cpp_install_headers("google_cloud_cpp_bigquery_rest"
"include/google/cloud/bigquery")
google_cloud_cpp_install_headers("google_cloud_cpp_bigquery_rest_mocks"
"include/google/cloud/bigquery")

google_cloud_cpp_add_pkgconfig(
bigquery_rest "Experimental BigQuery REST client library"
"An experimental BigQuery C++ client library using REST for transport."
"google_cloud_cpp_rest_internal")

# Create and install the CMake configuration files.
include(CMakePackageConfigHelpers)
configure_file("config-rest.cmake.in"
"google_cloud_cpp_bigquery_rest-config.cmake" @ONLY)
write_basic_package_version_file(
"google_cloud_cpp_bigquery_rest-config-version.cmake"
VERSION ${PROJECT_VERSION}
COMPATIBILITY ExactVersion)

install(
FILES
"${CMAKE_CURRENT_BINARY_DIR}/google_cloud_cpp_bigquery_rest-config.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/google_cloud_cpp_bigquery_rest-config-version.cmake"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/google_cloud_cpp_bigquery_rest"
COMPONENT google_cloud_cpp_development)

if (BUILD_TESTING)
bigquery_rest_define_tests()
endif ()
Loading

0 comments on commit 7a8daed

Please sign in to comment.