Skip to content

Commit 15b37da

Browse files
authored
refactor: scaffolding knows about cmake library helper (#12470)
1 parent 313bd53 commit 15b37da

4 files changed

Lines changed: 32 additions & 203 deletions

File tree

cmake/GoogleCloudCppLibrary.cmake

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,23 @@
3333
# `asset` sets this.
3434
#
3535
function (google_cloud_cpp_add_ga_grpc_library library display_name)
36-
cmake_parse_arguments(_opt "" "" "ADDITIONAL_PROTO_LISTS" ${ARGN})
36+
cmake_parse_arguments(_opt "EXPERIMENTAL" "" "ADDITIONAL_PROTO_LISTS"
37+
${ARGN})
3738
set(library_target "google_cloud_cpp_${library}")
3839
set(mocks_target "google_cloud_cpp_${library}_mocks")
3940
set(protos_target "google_cloud_cpp_${library}_protos")
41+
set(library_alias "google-cloud-cpp::${library}")
42+
if (_opt_EXPERIMENTAL)
43+
set(library_alias "google-cloud-cpp::experimental-${library}")
44+
endif ()
4045

4146
include(GoogleapisConfig)
4247
set(DOXYGEN_PROJECT_NAME "${display_name} C++ Client")
4348
set(DOXYGEN_PROJECT_BRIEF "A C++ Client Library for the ${display_name}")
4449
set(DOXYGEN_PROJECT_NUMBER "${PROJECT_VERSION}")
50+
if (_opt_EXPERIMENTAL)
51+
set(DOXYGEN_PROJECT_NUMBER "${PROJECT_VERSION} (Experimental)")
52+
endif ()
4553
set(DOXYGEN_EXCLUDE_SYMBOLS "internal")
4654
set(DOXYGEN_EXAMPLE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/quickstart")
4755

@@ -103,13 +111,13 @@ function (google_cloud_cpp_add_ga_grpc_library library display_name)
103111
google_cloud_cpp_add_common_options(${library_target})
104112
set_target_properties(
105113
${library_target}
106-
PROPERTIES EXPORT_NAME google-cloud-cpp::${library}
114+
PROPERTIES EXPORT_NAME ${library_alias}
107115
VERSION "${PROJECT_VERSION}"
108116
SOVERSION "${PROJECT_VERSION_MAJOR}")
109117
target_compile_options(${library_target}
110118
PUBLIC ${GOOGLE_CLOUD_CPP_EXCEPTIONS_FLAG})
111119

112-
add_library(google-cloud-cpp::${library} ALIAS ${library_target})
120+
add_library(${library_alias} ALIAS ${library_target})
113121

114122
# Create a header-only library for the mocks. We use a CMake `INTERFACE`
115123
# library for these, a regular library would not work on macOS (where the
@@ -127,11 +135,10 @@ function (google_cloud_cpp_add_ga_grpc_library library display_name)
127135
add_library(${mocks_target} INTERFACE)
128136
target_sources(${mocks_target} INTERFACE ${mock_files})
129137
target_link_libraries(
130-
${mocks_target} INTERFACE google-cloud-cpp::${library}
131-
GTest::gmock_main GTest::gmock GTest::gtest)
132-
set_target_properties(
133-
${mocks_target} PROPERTIES EXPORT_NAME
134-
google-cloud-cpp::${library}_mocks)
138+
${mocks_target} INTERFACE ${library_alias} GTest::gmock_main
139+
GTest::gmock GTest::gtest)
140+
set_target_properties(${mocks_target} PROPERTIES EXPORT_NAME
141+
${library_alias}_mocks)
135142
target_include_directories(
136143
${mocks_target}
137144
INTERFACE $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>
@@ -192,8 +199,7 @@ function (google_cloud_cpp_add_ga_grpc_library library display_name)
192199

193200
external_googleapis_install_pc("${protos_target}")
194201

195-
# google-cloud-cpp::${library} must be defined before we can add the
196-
# samples.
202+
# ${library_alias} must be defined before we can add the samples.
197203
if (BUILD_TESTING AND GOOGLE_CLOUD_CPP_ENABLE_CXX_EXCEPTIONS)
198204
foreach (dir IN LISTS GOOGLE_CLOUD_CPP_SERVICE_DIRS)
199205
if ("${dir}" STREQUAL "__EMPTY__")

doc/contributor/howto-guide-declare-a-library-ga.md

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,10 @@ for lib in "${ga[@]}"; do sed -i 's/^Please note that the Google Cloud C/While t
8686

8787
### `google/cloud/${library}/CMakeLists.txt`:
8888

89-
Change the definition of the `DOXYGEN_PROJECT_NUMBER` variable from
90-
`${PROJECT_VERSION} (Experimental)` to `${PROJECT_VERSION}`.
89+
Remove the EXPERIMENTAL keyword from the library definition.
9190

9291
```shell
93-
for lib in "${ga[@]}"; do sed -i 's;"\${PROJECT_VERSION} (Experimental)";"${PROJECT_VERSION}";' google/cloud/${lib}/CMakeLists.txt; done
94-
```
95-
96-
Change the target name from `google-cloud-cpp::experimental-${library}`:
97-
98-
```shell
99-
for lib in "${ga[@]}"; do sed -i 's/google-cloud-cpp::experimental-/google-cloud-cpp::/' google/cloud/${lib}/CMakeLists.txt; done
92+
for lib in "${ga[@]}"; do sed -i 's/EXPERIMENTAL//' google/cloud/${lib}/CMakeLists.txt; done
10093
```
10194

10295
### `google/cloud/${library}/config.cmake.in`:

generator/internal/scaffold_generator.cc

Lines changed: 6 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ std::map<std::string, std::string> ScaffoldVars(
139139
vars["product_options_page"] = OptionsGroup(service.product_path());
140140
vars["service_subdirectory"] = ServiceSubdirectory(service.product_path());
141141
vars["site_root"] = SiteRoot(service);
142+
vars["experimental"] = experimental ? " EXPERIMENTAL" : "";
142143
vars["library_prefix"] = experimental ? "experimental-" : "";
143-
vars["doxygen_version_suffix"] = experimental ? " (Experimental)" : "";
144144
vars["construction"] = experimental ? "\n:construction:\n" : "";
145145
vars["status"] =
146146
experimental ? "This library is **experimental**. Its APIs are subject "
@@ -487,100 +487,11 @@ void GenerateCMakeLists(std::ostream& os,
487487
# limitations under the License.
488488
# ~~~
489489
490-
include(GoogleapisConfig)
491-
set(DOXYGEN_PROJECT_NAME "$title$ C++ Client")
492-
set(DOXYGEN_PROJECT_BRIEF "A C++ Client Library for the $title$")
493-
set(DOXYGEN_PROJECT_NUMBER "$${PROJECT_VERSION}$doxygen_version_suffix$")
494-
set(DOXYGEN_EXCLUDE_SYMBOLS "internal")
495-
set(DOXYGEN_EXAMPLE_PATH $${CMAKE_CURRENT_SOURCE_DIR}/quickstart)
496-
497-
unset(mocks_globs)
498-
unset(source_globs)
499-
set(service_dirs "$service_subdirectory$")
500-
foreach (dir IN LISTS service_dirs)
501-
string(REPLACE "/" "_" ns "$${dir}")
502-
list(APPEND source_globs "$${dir}*.h" "$${dir}*.cc" "$${dir}internal/*")
503-
list(APPEND mocks_globs "$${dir}mocks/*.h")
504-
list(APPEND DOXYGEN_EXAMPLE_PATH
505-
"$${CMAKE_CURRENT_SOURCE_DIR}/$${dir}samples")
506-
list(APPEND DOXYGEN_EXCLUDE_SYMBOLS "$library$_$${ns}internal")
507-
endforeach ()
508-
509-
include(GoogleCloudCppDoxygen)
510-
google_cloud_cpp_doxygen_targets("$library$" DEPENDS cloud-docs
511-
google-cloud-cpp::$library$_protos)
512-
513-
include(GoogleCloudCppCommon)
514-
515-
include(CompileProtos)
516-
google_cloud_cpp_find_proto_include_dir(PROTO_INCLUDE_DIR)
517-
google_cloud_cpp_load_protolist(
518-
proto_list
519-
"$${PROJECT_SOURCE_DIR}/external/googleapis/protolists/$library$.list")
520-
google_cloud_cpp_load_protodeps(
521-
proto_deps
522-
"$${PROJECT_SOURCE_DIR}/external/googleapis/protodeps/$library$.deps")
523-
google_cloud_cpp_grpcpp_library(
524-
google_cloud_cpp_$library$_protos # cmake-format: sort
525-
$${proto_list}
526-
PROTO_PATH_DIRECTORIES
527-
"$${EXTERNAL_GOOGLEAPIS_SOURCE}" "$${PROTO_INCLUDE_DIR}")
528-
external_googleapis_set_version_and_alias($library$_protos)
529-
target_link_libraries(google_cloud_cpp_$library$_protos PUBLIC $${proto_deps})
530-
531-
file(GLOB source_files
532-
RELATIVE "$${CMAKE_CURRENT_SOURCE_DIR}"
533-
$${source_globs})
534-
list(SORT source_files)
535-
add_library(google_cloud_cpp_$library$ $${source_files})
536-
target_include_directories(
537-
google_cloud_cpp_$library$
538-
PUBLIC $$<BUILD_INTERFACE:$${PROJECT_SOURCE_DIR}>
539-
$$<BUILD_INTERFACE:$${PROJECT_BINARY_DIR}>
540-
$$<INSTALL_INTERFACE:include>)
541-
target_link_libraries(
542-
google_cloud_cpp_$library$
543-
PUBLIC google-cloud-cpp::grpc_utils google-cloud-cpp::common
544-
google-cloud-cpp::$library$_protos)
545-
google_cloud_cpp_add_common_options(google_cloud_cpp_$library$)
546-
set_target_properties(
547-
google_cloud_cpp_$library$
548-
PROPERTIES EXPORT_NAME google-cloud-cpp::$library_prefix$$library$
549-
VERSION "$${PROJECT_VERSION}"
550-
SOVERSION "$${PROJECT_VERSION_MAJOR}")
551-
target_compile_options(google_cloud_cpp_$library$
552-
PUBLIC $${GOOGLE_CLOUD_CPP_EXCEPTIONS_FLAG})
553-
554-
add_library(google-cloud-cpp::$library_prefix$$library$ ALIAS google_cloud_cpp_$library$)
555-
556-
# Create a header-only library for the mocks. We use a CMake `INTERFACE` library
557-
# for these, a regular library would not work on macOS (where the library needs
558-
# at least one .o file). Unfortunately INTERFACE libraries are a bit weird in
559-
# that they need absolute paths for their sources.
560-
file(GLOB relative_mock_files
561-
RELATIVE "$${CMAKE_CURRENT_SOURCE_DIR}"
562-
$${mocks_globs})
563-
list(SORT relative_mock_files)
564-
set(mock_files)
565-
foreach (file IN LISTS relative_mock_files)
566-
list(APPEND mock_files "$${CMAKE_CURRENT_SOURCE_DIR}/$${file}")
567-
endforeach ()
568-
add_library(google_cloud_cpp_$library$_mocks INTERFACE)
569-
target_sources(google_cloud_cpp_$library$_mocks INTERFACE $${mock_files})
570-
target_link_libraries(
571-
google_cloud_cpp_$library$_mocks
572-
INTERFACE google-cloud-cpp::$library_prefix$$library$ GTest::gmock_main
573-
GTest::gmock GTest::gtest)
574-
set_target_properties(
575-
google_cloud_cpp_$library$_mocks
576-
PROPERTIES EXPORT_NAME google-cloud-cpp::$library_prefix$$library$_mocks)
577-
target_include_directories(
578-
google_cloud_cpp_$library$_mocks
579-
INTERFACE $$<BUILD_INTERFACE:$${PROJECT_SOURCE_DIR}>
580-
$$<BUILD_INTERFACE:$${PROJECT_BINARY_DIR}>
581-
$$<INSTALL_INTERFACE:include>)
582-
target_compile_options(google_cloud_cpp_$library$_mocks
583-
INTERFACE $${GOOGLE_CLOUD_CPP_EXCEPTIONS_FLAG})
490+
include(GoogleCloudCppLibrary)
491+
492+
set(GOOGLE_CLOUD_CPP_SERVICE_DIRS "$service_subdirectory$")
493+
494+
google_cloud_cpp_add_ga_grpc_library($library$ "$title$"$experimental$)
584495
585496
if (BUILD_TESTING AND GOOGLE_CLOUD_CPP_ENABLE_CXX_EXCEPTIONS)
586497
add_executable($library$_quickstart "quickstart/quickstart.cc")
@@ -596,77 +507,6 @@ if (BUILD_TESTING AND GOOGLE_CLOUD_CPP_ENABLE_CXX_EXCEPTIONS)
596507
set_tests_properties($library$_quickstart
597508
PROPERTIES LABELS "integration-test;quickstart")
598509
endif ()
599-
600-
# Get the destination directories based on the GNU recommendations.
601-
include(GNUInstallDirs)
602-
603-
# Export the CMake targets to make it easy to create configuration files.
604-
install(
605-
EXPORT google_cloud_cpp_$library$-targets
606-
DESTINATION "$${CMAKE_INSTALL_LIBDIR}/cmake/google_cloud_cpp_$library$"
607-
COMPONENT google_cloud_cpp_development)
608-
609-
# Install the libraries and headers in the locations determined by
610-
# GNUInstallDirs
611-
install(
612-
TARGETS google_cloud_cpp_$library$ google_cloud_cpp_$library$_protos
613-
EXPORT google_cloud_cpp_$library$-targets
614-
RUNTIME DESTINATION $${CMAKE_INSTALL_BINDIR}
615-
COMPONENT google_cloud_cpp_runtime
616-
LIBRARY DESTINATION $${CMAKE_INSTALL_LIBDIR}
617-
COMPONENT google_cloud_cpp_runtime
618-
NAMELINK_SKIP
619-
ARCHIVE DESTINATION $${CMAKE_INSTALL_LIBDIR}
620-
COMPONENT google_cloud_cpp_development)
621-
# With CMake-3.12 and higher we could avoid this separate command (and the
622-
# duplication).
623-
install(
624-
TARGETS google_cloud_cpp_$library$ google_cloud_cpp_$library$_protos
625-
LIBRARY DESTINATION $${CMAKE_INSTALL_LIBDIR}
626-
COMPONENT google_cloud_cpp_development
627-
NAMELINK_ONLY
628-
ARCHIVE DESTINATION $${CMAKE_INSTALL_LIBDIR}
629-
COMPONENT google_cloud_cpp_development)
630-
631-
google_cloud_cpp_install_proto_library_protos("google_cloud_cpp_$library$_protos"
632-
"$${EXTERNAL_GOOGLEAPIS_SOURCE}")
633-
google_cloud_cpp_install_proto_library_headers("google_cloud_cpp_$library$_protos")
634-
google_cloud_cpp_install_headers("google_cloud_cpp_$library$"
635-
"include/google/cloud/$library$")
636-
google_cloud_cpp_install_headers("google_cloud_cpp_$library$_mocks"
637-
"include/google/cloud/$library$")
638-
639-
google_cloud_cpp_add_pkgconfig(
640-
$library$
641-
"The $title$ C++ Client Library"
642-
"Provides C++ APIs to use the $title$."
643-
"google_cloud_cpp_grpc_utils"
644-
"google_cloud_cpp_common"
645-
"google_cloud_cpp_$library$_protos")
646-
647-
# Create and install the CMake configuration files.
648-
include(CMakePackageConfigHelpers)
649-
configure_file("config.cmake.in" "google_cloud_cpp_$library$-config.cmake" @ONLY)
650-
write_basic_package_version_file(
651-
"google_cloud_cpp_$library$-config-version.cmake"
652-
VERSION $${PROJECT_VERSION}
653-
COMPATIBILITY ExactVersion)
654-
655-
install(
656-
FILES
657-
"$${CMAKE_CURRENT_BINARY_DIR}/google_cloud_cpp_$library$-config.cmake"
658-
"$${CMAKE_CURRENT_BINARY_DIR}/google_cloud_cpp_$library$-config-version.cmake"
659-
DESTINATION "$${CMAKE_INSTALL_LIBDIR}/cmake/google_cloud_cpp_$library$"
660-
COMPONENT google_cloud_cpp_development)
661-
662-
external_googleapis_install_pc("google_cloud_cpp_$library$_protos")
663-
664-
# google-cloud-cpp::$library$ must be defined before we can add the samples.
665-
foreach (dir IN LISTS service_dirs)
666-
if (BUILD_TESTING AND GOOGLE_CLOUD_CPP_ENABLE_CXX_EXCEPTIONS)
667-
google_cloud_cpp_add_samples_relative("$library$" "$${dir}samples/")
668-
endif ()
669-
endforeach ()
670510
)""";
671511
google::protobuf::io::OstreamOutputStream output(&os);
672512
google::protobuf::io::Printer printer(&output, '$');

generator/internal/scaffold_generator_test.cc

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ TEST_F(ScaffoldGenerator, Vars) {
169169
Contains(Pair("product_options_page", "google-cloud-test-options")),
170170
Contains(Pair("copyright_year", "2034")),
171171
Contains(Pair("library_prefix", "")),
172-
Contains(Pair("doxygen_version_suffix", "")),
172+
Contains(Pair("experimental", "")),
173173
Contains(Pair("construction", "")),
174174
Contains(Pair("status", HasSubstr("**GA**")))));
175175

@@ -184,7 +184,7 @@ TEST_F(ScaffoldGenerator, Vars) {
184184
Contains(Pair("product_options_page", "google-cloud-test-options")),
185185
Contains(Pair("copyright_year", "2034")),
186186
Contains(Pair("library_prefix", "experimental-")),
187-
Contains(Pair("doxygen_version_suffix", " (Experimental)")),
187+
Contains(Pair("experimental", " EXPERIMENTAL")),
188188
Contains(Pair("construction", "\n:construction:\n")),
189189
Contains(Pair("status", HasSubstr("**experimental**")))));
190190
}
@@ -239,22 +239,12 @@ TEST_F(ScaffoldGenerator, CMakeLists) {
239239
EXPECT_THAT(actual, HasSubstr("2034"));
240240
EXPECT_THAT(actual, Not(HasSubstr("$copyright_year$")));
241241
EXPECT_THAT(actual, Not(HasSubstr("$library_prefix$")));
242-
EXPECT_THAT(actual, Not(HasSubstr("$doxygen_version_suffix$")));
243-
EXPECT_THAT(actual, HasSubstr(R"""(include(CompileProtos)
244-
google_cloud_cpp_find_proto_include_dir(PROTO_INCLUDE_DIR)
245-
google_cloud_cpp_load_protolist(
246-
proto_list
247-
"${PROJECT_SOURCE_DIR}/external/googleapis/protolists/test.list")
248-
google_cloud_cpp_load_protodeps(
249-
proto_deps
250-
"${PROJECT_SOURCE_DIR}/external/googleapis/protodeps/test.deps")
251-
google_cloud_cpp_grpcpp_library(
252-
google_cloud_cpp_test_protos # cmake-format: sort
253-
${proto_list}
254-
PROTO_PATH_DIRECTORIES
255-
"${EXTERNAL_GOOGLEAPIS_SOURCE}" "${PROTO_INCLUDE_DIR}")
256-
external_googleapis_set_version_and_alias(test_protos)
257-
target_link_libraries(google_cloud_cpp_test_protos PUBLIC ${proto_deps})
242+
EXPECT_THAT(actual, Not(HasSubstr("$experimental$")));
243+
EXPECT_THAT(actual, HasSubstr(R"""(include(GoogleCloudCppLibrary)
244+
245+
set(GOOGLE_CLOUD_CPP_SERVICE_DIRS "v1/")
246+
247+
google_cloud_cpp_add_ga_grpc_library(test "Test Only API")
258248
)"""));
259249

260250
EXPECT_THAT(actual, HasSubstr(R"""(add_executable(test_quickstart)"""));

0 commit comments

Comments
 (0)