From c5b414f7a7a43f77e956fec4dafbddc9cd068b4e Mon Sep 17 00:00:00 2001 From: dbolduc Date: Tue, 27 Feb 2024 12:30:45 -0500 Subject: [PATCH] feat: ability to skip installing mocks --- CMakeLists.txt | 4 ++ cmake/GoogleCloudCppCommon.cmake | 14 ++++- google/cloud/google_cloud_cpp_common.cmake | 57 +++++++++--------- .../google_cloud_cpp_storage_grpc.cmake | 60 ++++++++++--------- 4 files changed, 76 insertions(+), 59 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4d3dc276fcf99..b0fea9afe382c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -239,6 +239,10 @@ cmake_dependent_option( ON "GOOGLE_CLOUD_CPP_ENABLE_CXX_EXCEPTIONS;BUILD_TESTING" OFF) mark_as_advanced(GOOGLE_CLOUD_CPP_ENABLE_EXAMPLES) +option(GOOGLE_CLOUD_CPP_WITH_MOCKS + "Install the google-cloud-cpp mocking libraries." ON) +mark_as_advanced(GOOGLE_CLOUD_CPP_WITH_MOCKS) + # Add any subdirectories configured in the application. # `docfx/` must be included before any library directories. The tool is used to diff --git a/cmake/GoogleCloudCppCommon.cmake b/cmake/GoogleCloudCppCommon.cmake index 9ad0c3144ab66..e8d84d78241cc 100644 --- a/cmake/GoogleCloudCppCommon.cmake +++ b/cmake/GoogleCloudCppCommon.cmake @@ -164,6 +164,17 @@ endfunction () function (google_cloud_cpp_install_mocks library display_name) set(library_target "google_cloud_cpp_${library}") set(mocks_target "google_cloud_cpp_${library}_mocks") + + # Always install the mock headers. These are harmless, in the sense that + # they will not cause build failures in environments without GoogleTest. The + # same is not true about the mocking libraries which link GoogleTest. + google_cloud_cpp_install_headers("${mocks_target}" + "include/google/cloud/${library}") + + if (NOT GOOGLE_CLOUD_CPP_WITH_MOCKS) + # Only install modules for the mock libraries if they are requested. + return() + endif () install( EXPORT ${mocks_target}-targets DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${mocks_target}" @@ -173,9 +184,6 @@ function (google_cloud_cpp_install_mocks library display_name) EXPORT ${mocks_target}-targets COMPONENT google_cloud_cpp_development) - google_cloud_cpp_install_headers("${mocks_target}" - "include/google/cloud/${library}") - google_cloud_cpp_add_pkgconfig( ${library}_mocks "${display_name} Mocks" "Mocks for the ${display_name} C++ Client Library" "${library_target}" diff --git a/google/cloud/google_cloud_cpp_common.cmake b/google/cloud/google_cloud_cpp_common.cmake index 1966ed0b981bb..7634c4165c7e9 100644 --- a/google/cloud/google_cloud_cpp_common.cmake +++ b/google/cloud/google_cloud_cpp_common.cmake @@ -290,40 +290,43 @@ target_compile_options(google_cloud_cpp_mocks INTERFACE ${GOOGLE_CLOUD_CPP_EXCEPTIONS_FLAG}) add_library(google-cloud-cpp::mocks ALIAS google_cloud_cpp_mocks) -# Export the CMake targets to make it easy to create configuration files. -install( - EXPORT google_cloud_cpp_mocks-targets - DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/google_cloud_cpp_mocks" - COMPONENT google_cloud_cpp_development) - -install( - TARGETS google_cloud_cpp_mocks - EXPORT google_cloud_cpp_mocks-targets - COMPONENT google_cloud_cpp_development) install( FILES ${google_cloud_cpp_mocks_hdrs} DESTINATION "include/google/cloud/mocks" COMPONENT google_cloud_cpp_development) -google_cloud_cpp_add_pkgconfig( - "mocks" "Google Cloud C++ Testing Library" - "Helpers for testing the Google Cloud C++ Client Libraries" - "google_cloud_cpp_common" "gmock_main") +if (GOOGLE_CLOUD_CPP_WITH_MOCKS) + # Export the CMake targets to make it easy to create configuration files. + install( + EXPORT google_cloud_cpp_mocks-targets + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/google_cloud_cpp_mocks" + COMPONENT google_cloud_cpp_development) -# Create and install the CMake configuration files. -configure_file("mocks-config.cmake.in" "google_cloud_cpp_mocks-config.cmake" - @ONLY) -write_basic_package_version_file( - "google_cloud_cpp_mocks-config-version.cmake" - VERSION ${PROJECT_VERSION} - COMPATIBILITY ExactVersion) + install( + TARGETS google_cloud_cpp_mocks + EXPORT google_cloud_cpp_mocks-targets + COMPONENT google_cloud_cpp_development) -install( - FILES - "${CMAKE_CURRENT_BINARY_DIR}/google_cloud_cpp_mocks-config.cmake" - "${CMAKE_CURRENT_BINARY_DIR}/google_cloud_cpp_mocks-config-version.cmake" - DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/google_cloud_cpp_mocks" - COMPONENT google_cloud_cpp_development) + google_cloud_cpp_add_pkgconfig( + "mocks" "Google Cloud C++ Testing Library" + "Helpers for testing the Google Cloud C++ Client Libraries" + "google_cloud_cpp_common" "gmock_main") + + # Create and install the CMake configuration files. + configure_file("mocks-config.cmake.in" + "google_cloud_cpp_mocks-config.cmake" @ONLY) + write_basic_package_version_file( + "google_cloud_cpp_mocks-config-version.cmake" + VERSION ${PROJECT_VERSION} + COMPATIBILITY ExactVersion) + + install( + FILES + "${CMAKE_CURRENT_BINARY_DIR}/google_cloud_cpp_mocks-config.cmake" + "${CMAKE_CURRENT_BINARY_DIR}/google_cloud_cpp_mocks-config-version.cmake" + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/google_cloud_cpp_mocks" + COMPONENT google_cloud_cpp_development) +endif () if (BUILD_TESTING) include(FindBenchmarkWithWorkarounds) diff --git a/google/cloud/storage/google_cloud_cpp_storage_grpc.cmake b/google/cloud/storage/google_cloud_cpp_storage_grpc.cmake index 20b3722fdbb56..5536dda461649 100644 --- a/google/cloud/storage/google_cloud_cpp_storage_grpc.cmake +++ b/google/cloud/storage/google_cloud_cpp_storage_grpc.cmake @@ -246,42 +246,44 @@ target_compile_options(google_cloud_cpp_storage_grpc_mocks add_library(google-cloud-cpp::experimental-storage_grpc_mocks ALIAS google_cloud_cpp_storage_grpc_mocks) -install( - EXPORT storage_grpc_mocks-targets - DESTINATION - "${CMAKE_INSTALL_LIBDIR}/cmake/google_cloud_cpp_storage_grpc_mocks" - COMPONENT google_cloud_cpp_development) - -install( - TARGETS google_cloud_cpp_storage_grpc_mocks - EXPORT storage_grpc_mocks-targets - COMPONENT google_cloud_cpp_development) - install( FILES ${google_cloud_cpp_storage_grpc_mocks_hdrs} DESTINATION "include/google/cloud/storage/mocks" COMPONENT google_cloud_cpp_development) -google_cloud_cpp_add_pkgconfig( - storage_grpc_mocks "Google Cloud Storage (gRPC) Mocks" - "Mocks for the Google Cloud Storage (gRPC) C++ Client Library" - "google_cloud_cpp_storage" " gmock_main") +if (GOOGLE_CLOUD_CPP_WITH_MOCKS) + install( + EXPORT storage_grpc_mocks-targets + DESTINATION + "${CMAKE_INSTALL_LIBDIR}/cmake/google_cloud_cpp_storage_grpc_mocks" + COMPONENT google_cloud_cpp_development) -# Create and install the CMake configuration files. -configure_file("mocks-config.cmake.in" - "google_cloud_cpp_storage_grpc_mocks-config.cmake" @ONLY) -write_basic_package_version_file( - "google_cloud_cpp_storage_grpc_mocks-config-version.cmake" - VERSION ${PROJECT_VERSION} - COMPATIBILITY ExactVersion) + install( + TARGETS google_cloud_cpp_storage_grpc_mocks + EXPORT storage_grpc_mocks-targets + COMPONENT google_cloud_cpp_development) -install( - FILES - "${CMAKE_CURRENT_BINARY_DIR}/google_cloud_cpp_storage_grpc_mocks-config.cmake" - "${CMAKE_CURRENT_BINARY_DIR}/google_cloud_cpp_storage_grpc_mocks-config-version.cmake" - DESTINATION - "${CMAKE_INSTALL_LIBDIR}/cmake/google_cloud_cpp_storage_grpc_mocks" - COMPONENT google_cloud_cpp_development) + google_cloud_cpp_add_pkgconfig( + storage_grpc_mocks "Google Cloud Storage (gRPC) Mocks" + "Mocks for the Google Cloud Storage (gRPC) C++ Client Library" + "google_cloud_cpp_storage" " gmock_main") + + # Create and install the CMake configuration files. + configure_file("mocks-config.cmake.in" + "google_cloud_cpp_storage_grpc_mocks-config.cmake" @ONLY) + write_basic_package_version_file( + "google_cloud_cpp_storage_grpc_mocks-config-version.cmake" + VERSION ${PROJECT_VERSION} + COMPATIBILITY ExactVersion) + + install( + FILES + "${CMAKE_CURRENT_BINARY_DIR}/google_cloud_cpp_storage_grpc_mocks-config.cmake" + "${CMAKE_CURRENT_BINARY_DIR}/google_cloud_cpp_storage_grpc_mocks-config-version.cmake" + DESTINATION + "${CMAKE_INSTALL_LIBDIR}/cmake/google_cloud_cpp_storage_grpc_mocks" + COMPONENT google_cloud_cpp_development) +endif () if (BUILD_TESTING AND GOOGLE_CLOUD_CPP_STORAGE_ENABLE_GRPC) # This is a bit weird, we add an additional link library to