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

Commit e932202

Browse files
authored
cleanup: formatting updates for CMake and Bazel (#157)
Update the formatting tools for CMake and Bazel. Use the newer tools to keep file lists in CMake sorted. Lots of trivial changes due to the tool version updates.
1 parent 1882ff9 commit e932202

13 files changed

+74
-114
lines changed

.cmake-format.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
tab_size = 4
1616
separate_ctrl_name_with_space = True
17+
enable_sort = True
18+
autosort = True
1719

1820
additional_commands = {
1921
"externalproject_add": {

ci/kokoro/docker/Dockerfile.ubuntu

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -44,28 +44,6 @@ RUN apt update && \
4444
wget \
4545
zlib1g-dev
4646

47-
# By default, Ubuntu 18.04 does not install the alternatives for clang-format
48-
# and clang-tidy, so we need to manually install those.
49-
RUN if grep -q 18.04 /etc/lsb-release; then \
50-
apt update && apt install -y clang-tidy clang-format-7 clang-tools; \
51-
update-alternatives --install /usr/bin/clang-tidy clang-tidy /usr/bin/clang-tidy-6.0 100; \
52-
update-alternatives --install /usr/bin/clang-format clang-format /usr/bin/clang-format-7 100; \
53-
update-alternatives --install /usr/bin/scan-build scan-build /usr/bin/scan-build-6.0 100; \
54-
fi
55-
56-
# Install the the buildifier tool, which does not compile with the default
57-
# golang compiler for Ubuntu 16.04 and Ubuntu 18.04.
58-
RUN wget -q -O /usr/bin/buildifier https://github.com/bazelbuild/buildtools/releases/download/0.17.2/buildifier
59-
RUN chmod 755 /usr/bin/buildifier
60-
61-
# Install cmake_format to automatically format the CMake list files.
62-
# https://github.com/cheshirekow/cmake_format
63-
# Pin this to an specific version because the formatting changes when the
64-
# "latest" version is updated, and we do not want the builds to break just
65-
# because some third party changed something.
66-
RUN pip install --upgrade pip
67-
RUN pip install cmake_format==0.6.0
68-
6947
# Install Bazel because some of the builds need it.
7048
WORKDIR /var/tmp/ci
7149
COPY . /var/tmp/ci

ci/kokoro/docker/Dockerfile.ubuntu-install

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ RUN if grep -q 18.04 /etc/lsb-release; then \
5757

5858
# Install the the buildifier tool, which does not compile with the default
5959
# golang compiler for Ubuntu 16.04 and Ubuntu 18.04.
60-
RUN wget -q -O /usr/bin/buildifier https://github.com/bazelbuild/buildtools/releases/download/0.17.2/buildifier
60+
RUN wget -q -O /usr/bin/buildifier https://github.com/bazelbuild/buildtools/releases/download/0.29.0/buildifier
6161
RUN chmod 755 /usr/bin/buildifier
6262

6363
# Install cmake_format to automatically format the CMake list files.
@@ -66,7 +66,7 @@ RUN chmod 755 /usr/bin/buildifier
6666
# "latest" version is updated, and we do not want the builds to break just
6767
# because some third party changed something.
6868
RUN pip install --upgrade pip
69-
RUN pip install cmake_format==0.6.0
69+
RUN pip install cmake_format==0.6.8
7070

7171
# Install protobuf using CMake. Some distributions include protobuf, but gRPC
7272
# requires 3.4.x or newer, and many of those distribution use older versions.

cmake/CompileProtos.cmake

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ function (google_cloud_cpp_generate_proto SRCS)
6666
foreach (dir ${_opt_PROTO_PATH_DIRECTORIES})
6767
get_filename_component(absolute_path ${dir} ABSOLUTE)
6868
list(FIND protobuf_include_path "${absolute_path}"
69-
already_in_search_path)
69+
already_in_search_path)
7070
if (${already_in_search_path} EQUAL -1)
7171
list(APPEND protobuf_include_path "--proto_path" "${absolute_path}")
7272
endif ()
@@ -93,9 +93,10 @@ function (google_cloud_cpp_generate_proto SRCS)
9393
list(APPEND ${SRCS} "${pb_cc}" "${pb_h}")
9494
add_custom_command(
9595
OUTPUT "${pb_cc}" "${pb_h}"
96-
COMMAND $<TARGET_FILE:protobuf::protoc> ARGS --cpp_out
97-
"${CMAKE_CURRENT_BINARY_DIR}" ${protobuf_include_path}
98-
"${filename}"
96+
COMMAND
97+
$<TARGET_FILE:protobuf::protoc> ARGS --cpp_out
98+
"${CMAKE_CURRENT_BINARY_DIR}" ${protobuf_include_path}
99+
"${filename}"
99100
DEPENDS "${filename}" protobuf::protoc
100101
COMMENT "Running C++ protocol buffer compiler on ${filename}"
101102
VERBATIM)
@@ -148,7 +149,7 @@ function (google_cloud_cpp_generate_grpcpp SRCS)
148149
foreach (dir ${_opt_PROTO_PATH_DIRECTORIES})
149150
get_filename_component(absolute_path ${dir} ABSOLUTE)
150151
list(FIND protobuf_include_path "${absolute_path}"
151-
already_in_search_path)
152+
already_in_search_path)
152153
if (${already_in_search_path} EQUAL -1)
153154
list(APPEND protobuf_include_path "--proto_path" "${absolute_path}")
154155
endif ()

cmake/CreateBazelConfig.cmake

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
# ~~~
1616

1717
function (write_bazel_copyright FILENAME YEAR)
18-
file(
19-
WRITE "${FILENAME}"
20-
[=[
18+
file(WRITE "${FILENAME}" [=[
2119
# Copyright ]=])
2220
file(APPEND "${FILENAME}" ${YEAR})
23-
file(APPEND "${FILENAME}" [=[ Google LLC
21+
file(
22+
APPEND "${FILENAME}"
23+
[=[ Google LLC
2424
#
2525
# Licensed under the Apache License, Version 2.0 (the "License");
2626
# you may not use this file except in compliance with the License.
@@ -57,31 +57,31 @@ function (create_bazel_config TARGET)
5757
get_target_property(target_type ${TARGET} TYPE)
5858
if (${target_type} STREQUAL "INTERFACE_LIBRARY")
5959
get_target_property(sources ${TARGET} INTERFACE_SOURCES)
60-
else()
60+
else ()
6161
get_target_property(sources ${TARGET} SOURCES)
6262
endif ()
6363
foreach (src ${sources})
6464
string(FIND "${src}" "${CMAKE_CURRENT_BINARY_DIR}" in_binary_dir)
6565
if ("${in_binary_dir}" EQUAL 0)
6666
# Skip files in the binary directory, they are generated and handled
6767
# differently by our Bazel BUILD files.
68-
elseif("${src}" MATCHES "\\.inc$")
68+
elseif ("${src}" MATCHES "\\.inc$")
6969
list(APPEND H ${src})
70-
elseif("${src}" MATCHES "\\.h$")
70+
elseif ("${src}" MATCHES "\\.h$")
7171
list(APPEND H ${src})
72-
elseif("${src}" MATCHES "\\.cc$")
72+
elseif ("${src}" MATCHES "\\.cc$")
7373
list(APPEND CC ${src})
7474
endif ()
7575
endforeach ()
7676
write_bazel_copyright(${filename} ${_CREATE_BAZEL_CONFIG_OPT_YEAR})
7777
file(APPEND "${filename}" [=[
78-
"""Automatically generated source lists for ]=]
79-
)
78+
"""Automatically generated source lists for ]=])
8079
file(APPEND "${filename}" ${TARGET})
81-
file(APPEND "${filename}" [=[ - DO NOT EDIT."""
80+
file(
81+
APPEND "${filename}"
82+
[=[ - DO NOT EDIT."""
8283

83-
]=]
84-
)
84+
]=])
8585
file(APPEND "${filename}" "${TARGET}_hdrs = [\n")
8686
foreach (src ${H})
8787
file(APPEND "${filename}" " \"${src}\",\n")
@@ -102,13 +102,11 @@ function (export_list_to_bazel filename VAR)
102102
endif ()
103103
write_bazel_copyright(${filename} ${_EXPORT_LIST_TO_BAZEL_OPT_YEAR})
104104
file(
105-
APPEND
106-
"${filename}"
107-
[=[
105+
APPEND "${filename}"
106+
[=[
108107
"""Automatically generated unit tests list - DO NOT EDIT."""
109108

110-
]=]
111-
)
109+
]=])
112110
file(APPEND "${filename}" "${VAR} = [\n")
113111
foreach (item ${${VAR}})
114112
file(APPEND "${filename}" " \"${item}\",\n")
@@ -125,9 +123,8 @@ function (export_variables_to_bazel filename)
125123
endif ()
126124
write_bazel_copyright(${filename} ${_EXPORT_VARIABLES_TO_BAZEL_OPT_YEAR})
127125
file(
128-
APPEND
129-
"${filename}"
130-
[=[
126+
APPEND "${filename}"
127+
[=[
131128
"""Automatically generated version numbers - DO NOT EDIT."""
132129

133130
]=])

cmake/DownloadNlohmannJson.cmake

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,11 @@ inline nlohmann::json operator "" _json(const char* s, std::size_t n)
7171
return nlohmann::json::parse(s, s + n);
7272
}
7373
]==]
74-
"" JSON_HPP_CONTENT "${JSON_HPP_CONTENT}")
75-
string(REPLACE
74+
""
75+
JSON_HPP_CONTENT
76+
"${JSON_HPP_CONTENT}")
77+
string(
78+
REPLACE
7679
[==[
7780
inline nlohmann::json::json_pointer operator "" _json_pointer(const char* s, std::size_t n)
7881
{

cmake/FindProtobufWithTargets.cmake

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,7 @@ if (Protobuf_FOUND)
105105
TARGET protobuf::libprotobuf PROPERTY INTERFACE_INCLUDE_DIRECTORIES
106106
${Protobuf_INCLUDE_DIR})
107107
set_property(
108-
TARGET protobuf::libprotobuf
109-
APPEND
108+
TARGET protobuf::libprotobuf APPEND
110109
PROPERTY INTERFACE_LINK_LIBRARIES ${Protobuf_LIBRARY}
111110
Threads::Threads)
112111
endif ()
@@ -117,8 +116,7 @@ if (Protobuf_FOUND)
117116
TARGET protobuf::libprotobuf-lite
118117
PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${Protobuf_INCLUDE_DIR})
119118
set_property(
120-
TARGET protobuf::libprotobuf-lite
121-
APPEND
119+
TARGET protobuf::libprotobuf-lite APPEND
122120
PROPERTY INTERFACE_LINK_LIBRARIES ${Protobuf_LITE_LIBRARY}
123121
Threads::Threads)
124122
endif ()
@@ -129,8 +127,7 @@ if (Protobuf_FOUND)
129127
TARGET protobuf::libprotoc PROPERTY INTERFACE_INCLUDE_DIRECTORIES
130128
${Protobuf_INCLUDE_DIR})
131129
set_property(
132-
TARGET protobuf::libprotoc
133-
APPEND
130+
TARGET protobuf::libprotoc APPEND
134131
PROPERTY INTERFACE_LINK_LIBRARIES ${Protobuf_PROTOC_LIBRARY}
135132
Threads::Threads)
136133
endif ()
@@ -186,7 +183,7 @@ if (protobuf_DEBUG)
186183
" ProtobufWithTargets_VERSION = ${ProtobufWithTargets_VERSION}")
187184
if (ProtobufWithTargets_FOUND)
188185
foreach (_target protobuf::libprotobuf protobuf::libprotobuf-lite
189-
protobuf::libprotoc)
186+
protobuf::libprotoc)
190187
if (NOT TARGET ${_target})
191188
message(
192189
STATUS

cmake/FindgRPC.cmake

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,10 @@ endfunction ()
106106
function (_grpc_fix_grpc_target_definitions)
107107
# Including gRPC headers without this definition results in a build error.
108108
if (WIN32)
109-
set_property(
110-
TARGET gRPC::grpc
111-
APPEND
112-
PROPERTY INTERFACE_COMPILE_DEFINITIONS _WIN32_WINNT=0x600)
113-
set_property(
114-
TARGET gRPC::grpc++
115-
APPEND
116-
PROPERTY INTERFACE_COMPILE_DEFINITIONS _WIN32_WINNT=0x600)
109+
set_property(TARGET gRPC::grpc APPEND
110+
PROPERTY INTERFACE_COMPILE_DEFINITIONS _WIN32_WINNT=0x600)
111+
set_property(TARGET gRPC::grpc++ APPEND
112+
PROPERTY INTERFACE_COMPILE_DEFINITIONS _WIN32_WINNT=0x600)
117113
endif ()
118114
endfunction ()
119115

@@ -208,26 +204,21 @@ if (_gRPC_grpc_LIBRARY)
208204
"${_gRPC_grpc_LIBRARY}")
209205
endif ()
210206
if (EXISTS "${_gRPC_grpc_LIBRARY_RELEASE}")
211-
set_property(
212-
TARGET gRPC::grpc
213-
APPEND
214-
PROPERTY IMPORTED_CONFIGURATIONS RELEASE)
207+
set_property(TARGET gRPC::grpc APPEND
208+
PROPERTY IMPORTED_CONFIGURATIONS RELEASE)
215209
set_target_properties(
216210
gRPC::grpc PROPERTIES IMPORTED_LOCATION_RELEASE
217211
"${_gRPC_grpc_LIBRARY_RELEASE}")
218212
endif ()
219213
if (EXISTS "${_gRPC_grpc_LIBRARY_DEBUG}")
220-
set_property(
221-
TARGET gRPC::grpc
222-
APPEND
223-
PROPERTY IMPORTED_CONFIGURATIONS DEBUG)
214+
set_property(TARGET gRPC::grpc APPEND
215+
PROPERTY IMPORTED_CONFIGURATIONS DEBUG)
224216
set_target_properties(
225217
gRPC::grpc PROPERTIES IMPORTED_LOCATION_DEBUG
226218
"${_gRPC_grpc_LIBRARY_DEBUG}")
227219
endif ()
228220
set_property(
229-
TARGET gRPC::grpc
230-
APPEND
221+
TARGET gRPC::grpc APPEND
231222
PROPERTY INTERFACE_LINK_LIBRARIES protobuf::libprotobuf
232223
Threads::Threads)
233224
endif ()
@@ -245,47 +236,36 @@ if (_gRPC_grpc++_LIBRARY)
245236
"${_gRPC_grpc++_LIBRARY}")
246237
endif ()
247238
if (EXISTS "${_gRPC_grpc++_LIBRARY_RELEASE}")
248-
set_property(
249-
TARGET gRPC::grpc++
250-
APPEND
251-
PROPERTY IMPORTED_CONFIGURATIONS RELEASE)
239+
set_property(TARGET gRPC::grpc++ APPEND
240+
PROPERTY IMPORTED_CONFIGURATIONS RELEASE)
252241
set_target_properties(
253242
gRPC::grpc++ PROPERTIES IMPORTED_LOCATION_RELEASE
254243
"${_gRPC_grpc++_LIBRARY_RELEASE}")
255244
endif ()
256245
if (EXISTS "${_gRPC_grpc++_LIBRARY_DEBUG}")
257-
set_property(
258-
TARGET gRPC::grpc++
259-
APPEND
260-
PROPERTY IMPORTED_CONFIGURATIONS DEBUG)
246+
set_property(TARGET gRPC::grpc++ APPEND
247+
PROPERTY IMPORTED_CONFIGURATIONS DEBUG)
261248
set_target_properties(
262249
gRPC::grpc++ PROPERTIES IMPORTED_LOCATION_DEBUG
263250
"${_gRPC_grpc++_LIBRARY_DEBUG}")
264251
endif ()
265252
set_property(
266-
TARGET gRPC::grpc++
267-
APPEND
253+
TARGET gRPC::grpc++ APPEND
268254
PROPERTY INTERFACE_LINK_LIBRARIES gRPC::grpc protobuf::libprotobuf
269255
Threads::Threads)
270256
if (CMAKE_VERSION VERSION_GREATER 3.8)
271257
# gRPC++ requires C++11, but only CMake-3.8 introduced a target
272258
# compiler feature to meet that requirement.
273-
set_property(
274-
TARGET gRPC::grpc++
275-
APPEND
276-
PROPERTY INTERFACE_COMPILE_FEATURES cxx_std_11)
259+
set_property(TARGET gRPC::grpc++ APPEND
260+
PROPERTY INTERFACE_COMPILE_FEATURES cxx_std_11)
277261
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
278262
# CMake 3.5 is still alive and kicking in some older distros, use
279263
# the compiler-specific versions in these cases.
280-
set_property(
281-
TARGET gRPC::grpc++
282-
APPEND
283-
PROPERTY INTERFACE_COMPILE_OPTIONS "-std=c++11")
264+
set_property(TARGET gRPC::grpc++ APPEND
265+
PROPERTY INTERFACE_COMPILE_OPTIONS "-std=c++11")
284266
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
285-
set_property(
286-
TARGET gRPC::grpc++
287-
APPEND
288-
PROPERTY INTERFACE_COMPILE_OPTIONS "-std=c++11")
267+
set_property(TARGET gRPC::grpc++ APPEND
268+
PROPERTY INTERFACE_COMPILE_OPTIONS "-std=c++11")
289269
else ()
290270
message(
291271
WARNING

cmake/SelectMSVCRuntime.cmake

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
# to determine this, but this is a good start.
2929
#
3030
if (MSVC AND VCPKG_TARGET_TRIPLET MATCHES "-static$")
31-
foreach (flag_var CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG
32-
CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_MINSIZEREL
33-
CMAKE_CXX_FLAGS_RELWITHDEBINFO)
31+
foreach (flag_var
32+
CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
33+
CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
3434
if (${flag_var} MATCHES "/MD")
3535
string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
3636
endif ()

doc/setup-development-environment.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ sudo wget -q -O /usr/bin/buildifier https://github.com/bazelbuild/buildtools/rel
3939
sudo chmod 755 /usr/bin/buildifier
4040
```
4141

42-
Install cmake_format to automatically format the CMake list files. We pin this
42+
Install `cmake_format` to automatically format the CMake list files. We pin this
4343
tool to a specific version because the formatting changes when the "latest"
4444
version is updated, and we do not want the builds to break just
4545
because some third party changed something.
4646

4747
```console
4848
sudo apt install -y python python-pip
4949
pip install --upgrade pip
50-
pip install numpy cmake_format==0.4.0
50+
pip install numpy cmake_format==0.6.8
5151
```
5252

5353
Install the Python modules used in the integration tests:

0 commit comments

Comments
 (0)