Skip to content
This repository was archived by the owner on Dec 8, 2021. It is now read-only.

Commit a8d3b61

Browse files
authored
feat: cleanup CMake configs to work in -cpp (#1476)
Fixes: googleapis/google-cloud-cpp#3975 Fixes: googleapis/google-cloud-cpp#3974 This PR mainly adds a `cmake/GoogleCloudCppCommon.cmake` file was copied from `-cpp`. It also removes spanner-specific CMake variable names in favor of generic `GOOGLE_CLOUD_CPP_*` naming to match what will be used in `-cpp`.
1 parent 07c56e9 commit a8d3b61

File tree

11 files changed

+147
-53
lines changed

11 files changed

+147
-53
lines changed

CMakeLists.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ include(SelectMSVCRuntime)
5757
include(EnableCxxExceptions)
5858

5959
# This should be included from the top level CMakeLists file
60-
set(SPANNER_CLIENT_VERSION_MAJOR 1)
61-
set(SPANNER_CLIENT_VERSION_MINOR 2)
62-
set(SPANNER_CLIENT_VERSION_PATCH 0)
63-
string(CONCAT SPANNER_CLIENT_VERSION "${SPANNER_CLIENT_VERSION_MAJOR}" "."
64-
"${SPANNER_CLIENT_VERSION_MINOR}" "."
65-
"${SPANNER_CLIENT_VERSION_PATCH}")
60+
set(GOOGLE_CLOUD_CPP_VERSION_MAJOR 1)
61+
set(GOOGLE_CLOUD_CPP_VERSION_MINOR 2)
62+
set(GOOGLE_CLOUD_CPP_VERSION_PATCH 0)
63+
string(CONCAT GOOGLE_CLOUD_CPP_VERSION "${GOOGLE_CLOUD_CPP_VERSION_MAJOR}" "."
64+
"${GOOGLE_CLOUD_CPP_VERSION_MINOR}" "."
65+
"${GOOGLE_CLOUD_CPP_VERSION_PATCH}")
6666

6767
include(CTest)
6868

ci/kokoro/docker/build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ elif [[ "${BUILD_NAME}" = "tsan" ]]; then
120120
elif [[ "${BUILD_NAME}" = "noex" ]]; then
121121
export DISTRO=fedora-install
122122
export DISTRO_VERSION=30
123-
export CMAKE_FLAGS="-DGOOGLE_CLOUD_CPP_SPANNER_ENABLE_CXX_EXCEPTIONS=no"
123+
export CMAKE_FLAGS="-DGOOGLE_CLOUD_CPP_ENABLE_CXX_EXCEPTIONS=no"
124124
in_docker_script="ci/kokoro/docker/build-in-docker-cmake.sh"
125125
elif [[ "${BUILD_NAME}" = "msan" ]]; then
126126
# We use Fedora for this build because (1) I was able to find instructions on

cmake/EnableCxxExceptions.cmake

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,22 @@
1414
# limitations under the License.
1515
# ~~~
1616

17-
option(GOOGLE_CLOUD_CPP_SPANNER_ENABLE_CXX_EXCEPTIONS
18-
"Enable C++ Exception Support" ON)
17+
option(GOOGLE_CLOUD_CPP_ENABLE_CXX_EXCEPTIONS "Enable C++ Exception Support" ON)
1918

2019
# If the user disabled C++ exceptions we should give them a heads up about the
2120
# consequences.
22-
if (NOT GOOGLE_CLOUD_CPP_SPANNER_ENABLE_CXX_EXCEPTIONS)
21+
if (NOT GOOGLE_CLOUD_CPP_ENABLE_CXX_EXCEPTIONS)
2322
message(
2423
WARNING
2524
"C++ Exceptions disabled, any operation that normally"
2625
" raises exceptions will instead log the error and call"
2726
" std::abort(). In addition, some examples and tests will not be"
2827
" compiled.")
2928
if (MSVC)
30-
set(GOOGLE_CLOUD_CPP_SPANNER_EXCEPTIONS_FLAG "/EHs-c-")
29+
set(GOOGLE_CLOUD_CPP_EXCEPTIONS_FLAG "/EHs-c-")
3130
else ()
32-
set(GOOGLE_CLOUD_CPP_SPANNER_EXCEPTIONS_FLAG "-fno-exceptions")
31+
set(GOOGLE_CLOUD_CPP_EXCEPTIONS_FLAG "-fno-exceptions")
3332
endif ()
3433
else ()
35-
set(GOOGLE_CLOUD_CPP_SPANNER_EXCEPTIONS_FLAG "")
34+
set(GOOGLE_CLOUD_CPP_EXCEPTIONS_FLAG "")
3635
endif ()

cmake/EnableDoxygen.cmake

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,6 @@ else ()
6161
set(DOXYGEN_SOURCE_BROWSER YES)
6262
set(DOXYGEN_GENERATE_TAGFILE
6363
"${CMAKE_CURRENT_BINARY_DIR}/${GOOGLE_CLOUD_CPP_SUBPROJECT}.tag")
64-
set(DOXYGEN_EXCLUDE_SYMBOLS "internal" "spanner_testing")
65-
set(DOXYGEN_PREDEFINED
66-
"SPANNER_CLIENT_NS=v${SPANNER_CLIENT_VERSION_MAJOR}")
67-
set(DOXYGEN_EXCLUDE_PATTERNS
68-
"*/google/cloud/spanner/README.md"
69-
"*/google/cloud/spanner/internal/*"
70-
"*/google/cloud/spanner/benchmarks/*"
71-
"*/google/cloud/spanner/testing/*"
72-
"*/google/cloud/spanner/*_test.cc")
7364
set(GOOGLE_CLOUD_CPP_COMMON_TAG_URL
7465
"https://googleapis.dev/cpp/google-cloud-common/")
7566
string(APPEND GOOGLE_CLOUD_CPP_COMMON_TAG_URL

cmake/GoogleCloudCppCommon.cmake

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# ~~~
2+
# Copyright 2018 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
# ~~~
16+
17+
# Find out the name of the subproject.
18+
get_filename_component(GOOGLE_CLOUD_CPP_SUBPROJECT
19+
"${CMAKE_CURRENT_SOURCE_DIR}" NAME)
20+
21+
# Find out what flags turn on all available warnings and turn those warnings
22+
# into errors.
23+
include(CheckCXXCompilerFlag)
24+
if (NOT MSVC)
25+
check_cxx_compiler_flag(-Wall GOOGLE_CLOUD_CPP_COMPILER_SUPPORTS_WALL)
26+
check_cxx_compiler_flag(-Wextra GOOGLE_CLOUD_CPP_COMPILER_SUPPORTS_WEXTRA)
27+
check_cxx_compiler_flag(-Werror GOOGLE_CLOUD_CPP_COMPILER_SUPPORTS_WERROR)
28+
else ()
29+
check_cxx_compiler_flag("/std:c++latest"
30+
GOOGLE_CLOUD_CPP_COMPILER_SUPPORTS_CPP_LATEST)
31+
endif ()
32+
33+
# If possible, enable a code coverage build type.
34+
include(EnableCoverage)
35+
36+
# Include support for clang-tidy if available
37+
include(EnableClangTidy)
38+
39+
# C++ Exceptions are enabled by default, but allow the user to turn them off.
40+
include(EnableCxxExceptions)
41+
42+
# Get the destination directories based on the GNU recommendations.
43+
include(GNUInstallDirs)
44+
45+
# Pick the right MSVC runtime libraries.
46+
include(SelectMSVCRuntime)
47+
48+
# Enable doxygen
49+
include(EnableDoxygen)
50+
51+
function (google_cloud_cpp_add_common_options target)
52+
if (GOOGLE_CLOUD_CPP_COMPILER_SUPPORTS_CPP_LATEST)
53+
target_compile_options(${target} INTERFACE "/std:c++latest")
54+
endif ()
55+
if (GOOGLE_CLOUD_CPP_COMPILER_SUPPORTS_WALL)
56+
target_compile_options(${target} INTERFACE "-Wall")
57+
endif ()
58+
if (GOOGLE_CLOUD_CPP_COMPILER_SUPPORTS_WEXTRA)
59+
target_compile_options(${target} INTERFACE "-Wextra")
60+
endif ()
61+
if (GOOGLE_CLOUD_CPP_COMPILER_SUPPORTS_WERROR)
62+
target_compile_options(${target} INTERFACE "-Werror")
63+
endif ()
64+
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU"
65+
AND "${CMAKE_CXX_COMPILER_VERSION}" VERSION_LESS 5.0)
66+
# With GCC 4.x this warning is too noisy to be useful.
67+
target_compile_options(${target}
68+
INTERFACE "-Wno-missing-field-initializers")
69+
endif ()
70+
endfunction ()
71+
72+
function (google_cloud_cpp_add_clang_tidy target)
73+
if (GOOGLE_CLOUD_CPP_CLANG_TIDY_PROGRAM AND GOOGLE_CLOUD_CPP_CLANG_TIDY)
74+
set_target_properties(
75+
${target} PROPERTIES CXX_CLANG_TIDY
76+
"${GOOGLE_CLOUD_CPP_CLANG_TIDY_PROGRAM}")
77+
endif ()
78+
endfunction ()
79+
80+
#
81+
# google_cloud_cpp_install_headers : install all the headers in a target
82+
#
83+
# Find all the headers in @p target and install them at @p destination,
84+
# preserving the directory structure.
85+
#
86+
# * target the name of the target.
87+
# * destination the destination directory, relative to <PREFIX>. Typically this
88+
# starts with `include/google/cloud`, the function requires the full
89+
# destination in case some headers get installed elsewhere in the future.
90+
#
91+
function (google_cloud_cpp_install_headers target destination)
92+
get_target_property(target_sources ${target} SOURCES)
93+
foreach (header ${target_sources})
94+
if (NOT "${header}" MATCHES "\\.h$" AND NOT "${header}" MATCHES
95+
"\\.inc$")
96+
continue()
97+
endif ()
98+
string(REPLACE "${CMAKE_CURRENT_BINARY_DIR}/" "" relative "${header}")
99+
get_filename_component(dir "${relative}" DIRECTORY)
100+
install(FILES "${header}" DESTINATION "${destination}/${dir}")
101+
endforeach ()
102+
endfunction ()

google/cloud/spanner/CMakeLists.txt

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,19 @@
1717
find_package(google_cloud_cpp_common CONFIG REQUIRED)
1818
find_package(google_cloud_cpp_grpc_utils CONFIG REQUIRED)
1919

20-
include(EnableClangTidy)
21-
include(EnableWerror)
2220
set(DOXYGEN_PROJECT_NAME "Google Cloud Spanner C++ Client")
2321
set(DOXYGEN_PROJECT_BRIEF "A C++ Client Library for Google Cloud Spanner")
24-
set(DOXYGEN_PROJECT_NUMBER "${SPANNER_CLIENT_VERSION}")
22+
set(DOXYGEN_PROJECT_NUMBER "${GOOGLE_CLOUD_CPP_CLIENT_VERSION}")
2523
set(DOXYGEN_EXAMPLE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/samples
2624
${CMAKE_CURRENT_SOURCE_DIR}/../../../quickstart)
27-
include(EnableDoxygen)
25+
set(DOXYGEN_EXCLUDE_SYMBOLS "internal" "spanner_testing")
26+
set(DOXYGEN_PREDEFINED "SPANNER_CLIENT_NS=v${GOOGLE_CLOUD_CPP_VERSION_MAJOR}")
27+
set(DOXYGEN_EXCLUDE_PATTERNS
28+
"*/google/cloud/spanner/README.md" "*/google/cloud/spanner/internal/*"
29+
"*/google/cloud/spanner/benchmarks/*" "*/google/cloud/spanner/testing/*"
30+
"*/google/cloud/spanner/*_test.cc")
31+
32+
include(GoogleCloudCppCommon)
2833

2934
# Define a function to fetch the current git revision. Using a function creates
3035
# a new scope, so the CMake variables do not leak to the global namespace.
@@ -224,10 +229,10 @@ target_link_libraries(
224229
spanner_client PUBLIC google_cloud_cpp_grpc_utils google_cloud_cpp_common
225230
googleapis-c++::spanner_protos)
226231
set_target_properties(
227-
spanner_client PROPERTIES VERSION "${SPANNER_CLIENT_VERSION}"
228-
SOVERSION "${SPANNER_CLIENT_VERSION_MAJOR}")
232+
spanner_client PROPERTIES VERSION "${GOOGLE_CLOUD_CPP_VERSION}"
233+
SOVERSION "${GOOGLE_CLOUD_CPP_VERSION_MAJOR}")
229234
target_compile_options(spanner_client
230-
PUBLIC ${GOOGLE_CLOUD_CPP_SPANNER_EXCEPTIONS_FLAG})
235+
PUBLIC ${GOOGLE_CLOUD_CPP_EXCEPTIONS_FLAG})
231236

232237
google_cloud_cpp_add_clang_tidy(spanner_client)
233238
google_cloud_cpp_add_common_options(spanner_client)
@@ -260,7 +265,7 @@ target_include_directories(
260265
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>
261266
$<INSTALL_INTERFACE:include>)
262267
target_compile_options(spanner_client_mocks
263-
INTERFACE ${GOOGLE_CLOUD_CPP_SPANNER_EXCEPTIONS_FLAG})
268+
INTERFACE ${GOOGLE_CLOUD_CPP_EXCEPTIONS_FLAG})
264269

265270
# Define the tests in a function so we have a new scope for variable names.
266271
function (spanner_client_define_tests)
@@ -311,7 +316,7 @@ function (spanner_client_define_tests)
311316
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>
312317
$<INSTALL_INTERFACE:include>)
313318
target_compile_options(spanner_client_testing
314-
PUBLIC ${GOOGLE_CLOUD_CPP_SPANNER_EXCEPTIONS_FLAG})
319+
PUBLIC ${GOOGLE_CLOUD_CPP_EXCEPTIONS_FLAG})
315320

316321
set(spanner_client_unit_tests
317322
# cmake-format: sortable
@@ -454,12 +459,9 @@ add_subdirectory(benchmarks) # macro benchmarks
454459

455460
# Only compile the samples if we're building with exceptions enabled. They
456461
# require exceptions to keep them simple and idiomatic.
457-
if (GOOGLE_CLOUD_CPP_SPANNER_ENABLE_CXX_EXCEPTIONS)
462+
if (GOOGLE_CLOUD_CPP_ENABLE_CXX_EXCEPTIONS)
458463
add_subdirectory(samples)
459-
endif (GOOGLE_CLOUD_CPP_SPANNER_ENABLE_CXX_EXCEPTIONS)
460-
461-
# Get the destination directories based on the GNU recommendations.
462-
include(GNUInstallDirs)
464+
endif (GOOGLE_CLOUD_CPP_ENABLE_CXX_EXCEPTIONS)
463465

464466
# Export the CMake targets to make it easy to create configuration files.
465467
install(EXPORT spanner-targets
@@ -493,15 +495,15 @@ google_cloud_cpp_install_headers("spanner_client_mocks"
493495
"include/google/cloud/spanner")
494496

495497
# Setup global variables used in the following *.in files.
496-
set(GOOGLE_CLOUD_SPANNER_CONFIG_VERSION_MAJOR ${SPANNER_CLIENT_VERSION_MAJOR})
497-
set(GOOGLE_CLOUD_SPANNER_CONFIG_VERSION_MINOR ${SPANNER_CLIENT_VERSION_MINOR})
498-
set(GOOGLE_CLOUD_SPANNER_CONFIG_VERSION_PATCH ${SPANNER_CLIENT_VERSION_PATCH})
499-
set(GOOGLE_CLOUD_SPANNER_PC_NAME "The Google Cloud Spanner C++ Client Library")
500-
set(GOOGLE_CLOUD_SPANNER_PC_DESCRIPTION
498+
set(GOOGLE_CLOUD_CPP_CONFIG_VERSION_MAJOR ${GOOGLE_CLOUD_CPP_VERSION_MAJOR})
499+
set(GOOGLE_CLOUD_CPP_CONFIG_VERSION_MINOR ${GOOGLE_CLOUD_CPP_VERSION_MINOR})
500+
set(GOOGLE_CLOUD_CPP_CONFIG_VERSION_PATCH ${GOOGLE_CLOUD_CPP_VERSION_PATCH})
501+
set(GOOGLE_CLOUD_CPP_PC_NAME "The Google Cloud Spanner C++ Client Library")
502+
set(GOOGLE_CLOUD_CPP_PC_DESCRIPTION
501503
"Provides C++ APIs to access Google Cloud Spanner.")
502-
set(GOOGLE_CLOUD_SPANNER_PC_REQUIRES
504+
set(GOOGLE_CLOUD_CPP_PC_REQUIRES
503505
"google_cloud_cpp_grpc_utils google_cloud_cpp_common")
504-
set(GOOGLE_CLOUD_SPANNER_PC_LIBS "-lspanner_client")
506+
set(GOOGLE_CLOUD_CPP_PC_LIBS "-lspanner_client")
505507

506508
# Create and install the pkg-config files.
507509
configure_file("${PROJECT_SOURCE_DIR}/google/cloud/spanner/config.pc.in"

google/cloud/spanner/benchmarks/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function (spanner_client_define_benchmarks)
5454
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>
5555
$<INSTALL_INTERFACE:include>)
5656
target_compile_options(spanner_client_benchmarks
57-
PUBLIC ${GOOGLE_CLOUD_CPP_SPANNER_EXCEPTIONS_FLAG})
57+
PUBLIC ${GOOGLE_CLOUD_CPP_EXCEPTIONS_FLAG})
5858

5959
set(spanner_client_benchmark_programs
6060
# cmake-format: sortable

google/cloud/spanner/config-version.cmake.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ set(PACKAGE_VERSION @DOXYGEN_PROJECT_NUMBER@)
1616

1717
# This package has not reached 1.0, there are no compatibility guarantees
1818
# before then.
19-
if (@SPANNER_CLIENT_VERSION@ EQUAL 0)
19+
if (@GOOGLE_CLOUD_CPP_VERSION@ EQUAL 0)
2020
if ("${PACKAGE_FIND_VERSION}" STREQUAL "")
2121
set(PACKAGE_VERSION_COMPATIBLE TRUE)
2222
elseif ("${PACKAGE_VERSION}" VERSION_EQUAL "${PACKAGE_FIND_VERSION}")

google/cloud/spanner/config.pc.in

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ exec_prefix=${prefix}/@CMAKE_INSTALL_BINDIR@
1717
libdir=${prefix}/@CMAKE_INSTALL_LIBDIR@
1818
includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@
1919

20-
Name: @GOOGLE_CLOUD_SPANNER_PC_NAME@
21-
Description: @GOOGLE_CLOUD_SPANNER_PC_DESCRIPTION@
22-
Requires: @GOOGLE_CLOUD_SPANNER_PC_REQUIRES@
20+
Name: @GOOGLE_CLOUD_CPP_PC_NAME@
21+
Description: @GOOGLE_CLOUD_CPP_PC_DESCRIPTION@
22+
Requires: @GOOGLE_CLOUD_CPP_PC_REQUIRES@
2323
Version: @DOXYGEN_PROJECT_NUMBER@
2424

25-
Libs: -L${libdir} @GOOGLE_CLOUD_SPANNER_PC_LIBS@
25+
Libs: -L${libdir} @GOOGLE_CLOUD_CPP_PC_LIBS@
2626
Cflags: -I${includedir}

google/cloud/spanner/integration_tests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ function (spanner_client_define_integration_tests)
8080
PROPERTY COMPILE_DEFINITIONS "_CRT_SECURE_NO_WARNINGS")
8181
endif ()
8282

83-
if (NOT GOOGLE_CLOUD_CPP_SPANNER_ENABLE_CXX_EXCEPTIONS)
83+
if (NOT GOOGLE_CLOUD_CPP_ENABLE_CXX_EXCEPTIONS)
8484
return()
8585
endif ()
8686

0 commit comments

Comments
 (0)