Skip to content

Commit 3f06ca4

Browse files
mkruskal-googlecopybara-github
authored andcommitted
Breaking change: Remove CMake submodule support in favor of fetched or installed dependencies.
This flips the default behavior to "fetch", downloading local copies of required dependencies. This can be disabled by setting `-Dprotobuf_FETCH_DEPENDENCIES=OFF`, in which case we will look for a local installation using find_package. Setting `-Dprotobuf_ABSL_PROVIDER=package` will continue to have the same behavior as before. See https://protobuf.dev/news/2024-10-02/#replace-cmake-submods for more details. #test-continuous PiperOrigin-RevId: 686649864
1 parent 66f80bb commit 3f06ca4

File tree

6 files changed

+38
-127
lines changed

6 files changed

+38
-127
lines changed

.github/workflows/test_cpp.yml

+14-40
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ jobs:
131131
sccache -z;
132132
cmake . -DWITH_PROTOC=/workspace/${{ steps.cross-compile.outputs.protoc }}
133133
-Dprotobuf_BUILD_LIBUPB=OFF -Dprotobuf_BUILD_CONFORMANCE=ON -DCMAKE_CXX_STANDARD=14
134-
-Dprotobuf_WITH_ZLIB=OFF -Dprotobuf_FETCH_DEPENDENCIES=ON ${{ env.SCCACHE_CMAKE_FLAGS }};
134+
-Dprotobuf_WITH_ZLIB=OFF ${{ env.SCCACHE_CMAKE_FLAGS }};
135135
cmake --build . --parallel 20;
136136
ctest --parallel 20;
137137
sccache -s"
@@ -141,19 +141,19 @@ jobs:
141141
fail-fast: false # Don't cancel all jobs if one fails.
142142
matrix:
143143
include:
144-
- flags: -Dprotobuf_BUILD_EXAMPLES=ON -DCMAKE_CXX_STANDARD=14 -Dprotobuf_USE_EXTERNAL_GTEST=ON -Dprotobuf_ABSL_PROVIDER=package -Dprotobuf_JSONCPP_PROVIDER=package
144+
- flags: -Dprotobuf_BUILD_EXAMPLES=ON -DCMAKE_CXX_STANDARD=14 -Dprotobuf_USE_EXTERNAL_GTEST=ON -Dprotobuf_FETCH_DEPENDENCIES=OFF
145145
- name: Ninja
146-
flags: -G Ninja -DCMAKE_CXX_STANDARD=14 -Dprotobuf_USE_EXTERNAL_GTEST=ON -Dprotobuf_ABSL_PROVIDER=package -Dprotobuf_JSONCPP_PROVIDER=package
146+
flags: -G Ninja -DCMAKE_CXX_STANDARD=14 -Dprotobuf_USE_EXTERNAL_GTEST=ON -Dprotobuf_FETCH_DEPENDENCIES=OFF
147147
continuous-only: true
148148
- name: Shared
149-
flags: -Dprotobuf_BUILD_SHARED_LIBS=ON -Dprotobuf_BUILD_EXAMPLES=ON -DCMAKE_CXX_STANDARD=14 -Dprotobuf_USE_EXTERNAL_GTEST=ON -Dprotobuf_ABSL_PROVIDER=package -Dprotobuf_JSONCPP_PROVIDER=package
149+
flags: -Dprotobuf_BUILD_SHARED_LIBS=ON -Dprotobuf_BUILD_EXAMPLES=ON -DCMAKE_CXX_STANDARD=14 -Dprotobuf_USE_EXTERNAL_GTEST=ON -Dprotobuf_FETCH_DEPENDENCIES=OFF
150150
continuous-only: true
151151
- name: C++17
152-
flags: -DCMAKE_CXX_STANDARD=17 -Dprotobuf_USE_EXTERNAL_GTEST=ON -Dprotobuf_ABSL_PROVIDER=package -Dprotobuf_JSONCPP_PROVIDER=package
152+
flags: -DCMAKE_CXX_STANDARD=17 -Dprotobuf_USE_EXTERNAL_GTEST=ON -Dprotobuf_FETCH_DEPENDENCIES=OFF
153153
- name: C++20
154-
flags: -DCMAKE_CXX_STANDARD=20 -Dprotobuf_USE_EXTERNAL_GTEST=ON -Dprotobuf_ABSL_PROVIDER=package -Dprotobuf_JSONCPP_PROVIDER=package
154+
flags: -DCMAKE_CXX_STANDARD=20 -Dprotobuf_USE_EXTERNAL_GTEST=ON -Dprotobuf_FETCH_DEPENDENCIES=OFF
155155
- name: Fetch
156-
flags: -DCMAKE_CXX_STANDARD=17 -Dprotobuf_FETCH_DEPENDENCIES=ON
156+
flags: -DCMAKE_CXX_STANDARD=17
157157

158158
name: ${{ matrix.continuous-only && inputs.continuous-prefix || '' }} Linux CMake ${{ matrix.name}}
159159
runs-on: ubuntu-latest
@@ -190,12 +190,11 @@ jobs:
190190
# Set defaults
191191
- type: package
192192
name: Install
193-
flags: -Dprotobuf_USE_EXTERNAL_GTEST=ON -Dprotobuf_ABSL_PROVIDER=package -Dprotobuf_JSONCPP_PROVIDER=package
193+
flags: -Dprotobuf_USE_EXTERNAL_GTEST=ON -Dprotobuf_FETCH_DEPENDENCIES=OFF
194194
- type: fetch
195195
name: Install (Fetch)
196-
flags: -Dprotobuf_FETCH_DEPENDENCIES=ON
197196
continuous-only: true
198-
name: ${{ matrix.continuous-only && inputs.continuous-prefix || '' }}Linux CMake ${{ matrix.name }})
197+
name: ${{ matrix.continuous-only && inputs.continuous-prefix || '' }}Linux CMake ${{ matrix.name }}
199198
runs-on: ubuntu-latest
200199
steps:
201200
- name: Checkout pending changes
@@ -255,7 +254,7 @@ jobs:
255254
credentials: ${{ secrets.GAR_SERVICE_ACCOUNT }}
256255
command: >-
257256
/install.sh -DCMAKE_CXX_STANDARD=14 ${{ env.SCCACHE_CMAKE_FLAGS }}
258-
-Dprotobuf_USE_EXTERNAL_GTEST=ON -Dprotobuf_ABSL_PROVIDER=package
257+
-Dprotobuf_USE_EXTERNAL_GTEST=ON -Dprotobuf_FETCH_DEPENDENCIES=OFF
259258
-Dprotobuf_BUILD_EXAMPLES=OFF \&\&
260259
mkdir examples/build \&\&
261260
cd examples/build \&\&
@@ -302,36 +301,11 @@ jobs:
302301
-c 'set -ex;
303302
cd /workspace;
304303
sccache -z;
305-
cmake . ${{ matrix.flags }} ${{ env.SCCACHE_CMAKE_FLAGS }} -Dprotobuf_FETCH_DEPENDENCIES=ON;
304+
cmake . ${{ matrix.flags }} ${{ env.SCCACHE_CMAKE_FLAGS }};
306305
cmake --build . --parallel 20;
307306
ctest --verbose --parallel 20;
308307
sccache -s'
309308
310-
linux-cmake-submodules:
311-
name: Linux CMake Submodules
312-
runs-on: ubuntu-latest
313-
steps:
314-
- name: Checkout pending changes
315-
uses: protocolbuffers/protobuf-ci/checkout@v3
316-
with:
317-
ref: ${{ inputs.safe-checkout }}
318-
submodules: recursive
319-
320-
- name: Setup sccache
321-
uses: protocolbuffers/protobuf-ci/sccache@v3
322-
with:
323-
cache-prefix: linux-cmake-submodules
324-
credentials: ${{ secrets.GAR_SERVICE_ACCOUNT }}
325-
326-
- name: Run tests
327-
uses: protocolbuffers/protobuf-ci/docker@v3
328-
with:
329-
image: us-docker.pkg.dev/protobuf-build/containers/test/linux/cmake:3.16.9-f39fc8b4e244fe5cd4c7138d0b6959a52b46ca48
330-
credentials: ${{ secrets.GAR_SERVICE_ACCOUNT }}
331-
command: >-
332-
/test.sh ${{ env.SCCACHE_CMAKE_FLAGS }}
333-
-Dprotobuf_BUILD_CONFORMANCE=ON -Dprotobuf_BUILD_EXAMPLES=ON -DCMAKE_CXX_STANDARD=14
334-
335309
linux-cmake-32-bit:
336310
name: Linux CMake 32-bit
337311
runs-on: ubuntu-latest
@@ -357,7 +331,7 @@ jobs:
357331
/bin/bash -cex '
358332
cd /workspace;
359333
sccache -z;
360-
cmake . -DCMAKE_CXX_STANDARD=14 -Dprotobuf_FETCH_DEPENDENCIES=ON ${{ env.SCCACHE_CMAKE_FLAGS }};
334+
cmake . -DCMAKE_CXX_STANDARD=14 ${{ env.SCCACHE_CMAKE_FLAGS }};
361335
cmake --build . --parallel 20;
362336
ctest --verbose --parallel 20;
363337
sccache -s'
@@ -511,7 +485,7 @@ jobs:
511485
uses: protocolbuffers/protobuf-ci/bash@v3
512486
with:
513487
credentials: ${{ secrets.GAR_SERVICE_ACCOUNT }}
514-
command: cmake . ${{ matrix.install-flags }} ${{ env.SCCACHE_CMAKE_FLAGS }} -Dprotobuf_ALLOW_CCACHE=ON -Dprotobuf_FETCH_DEPENDENCIES=ON
488+
command: cmake . ${{ matrix.install-flags }} ${{ env.SCCACHE_CMAKE_FLAGS }} -Dprotobuf_ALLOW_CCACHE=ON
515489
- name: Build for install
516490
if: ${{ matrix.install-flags && (!matrix.continuous-only || inputs.continuous-run) }}
517491
shell: bash
@@ -534,7 +508,7 @@ jobs:
534508
uses: protocolbuffers/protobuf-ci/bash@v3
535509
with:
536510
credentials: ${{ secrets.GAR_SERVICE_ACCOUNT }}
537-
command: cmake . ${{ matrix.flags }} ${{ env.SCCACHE_CMAKE_FLAGS }} -Dprotobuf_ALLOW_CCACHE=ON -Dprotobuf_FETCH_DEPENDENCIES=ON
511+
command: cmake . ${{ matrix.flags }} ${{ env.SCCACHE_CMAKE_FLAGS }} -Dprotobuf_ALLOW_CCACHE=ON
538512

539513
- name: Build
540514
if: ${{ !matrix.continuous-only || inputs.continuous-run }}

CMakeLists.txt

+5-5
Original file line numberDiff line numberDiff line change
@@ -281,13 +281,13 @@ include_directories(
281281
${protobuf_BINARY_DIR}/src
282282
${protobuf_SOURCE_DIR}/src)
283283

284-
set(protobuf_FETCH_DEPENDENCIES OFF CACHE BOOL "Download dependencies from GitHub. If this option is not set, the dependency must be available locally, either as a sub-module or an installed package.")
284+
set(protobuf_FETCH_DEPENDENCIES ON CACHE BOOL "Allow downloading dependencies from GitHub. If this option is not set, the dependency must be available locally as an installed package.")
285285

286-
set(protobuf_ABSL_PROVIDER "module" CACHE STRING "Provider of absl library. `module` uses sub-modules, `package` searches for a local installation, and `fetch` downloads from GitHub")
287-
set_property(CACHE protobuf_ABSL_PROVIDER PROPERTY STRINGS "module" "package" "fetch")
286+
set(protobuf_ABSL_PROVIDER "fetch" CACHE STRING "Provider of absl library. `fetch` downloads from GitHub and `package` searches for a local installation")
287+
set_property(CACHE protobuf_ABSL_PROVIDER PROPERTY STRINGS "package" "fetch")
288288

289-
set(protobuf_JSONCPP_PROVIDER "module" CACHE STRING "Provider of jsoncpp library. `module` uses sub-modules, `package` searches for a local installation, and `fetch` downloads from GitHub")
290-
set_property(CACHE protobuf_JSONCPP_PROVIDER PROPERTY STRINGS "module" "package" "fetch")
289+
set(protobuf_JSONCPP_PROVIDER "fetch" CACHE STRING "Provider of jsoncpp library. `fetch` downloads from GitHub and `package` searches for a local installation")
290+
set_property(CACHE protobuf_JSONCPP_PROVIDER PROPERTY STRINGS "package" "fetch")
291291

292292
if (protobuf_BUILD_TESTS)
293293
include(${protobuf_SOURCE_DIR}/cmake/gtest.cmake)

cmake/README.md

+2-9
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,6 @@ Go to the project folder:
8282
C:\Path\to\src> cd protobuf
8383
C:\Path\to\src\protobuf>
8484

85-
Remember to update any submodules if you are using git clone (you can skip this
86-
step if you are using a release .tar.gz or .zip package):
87-
88-
```console
89-
C:\Path\to\src\protobuf> git submodule update --init --recursive
90-
```
91-
9285
Good. Now you are ready for *CMake* configuration.
9386

9487
## CMake Configuration
@@ -156,8 +149,8 @@ It will generate *Visual Studio* solution file *protobuf.sln* in current directo
156149

157150
Unit tests are being built along with the rest of protobuf. The unit tests require Google Mock (now a part of Google Test).
158151

159-
A copy of [Google Test](https://github.com/google/googletest) is included as a Git submodule in the `third-party/googletest` folder.
160-
(You do need to initialize the Git submodules as explained above.)
152+
By default, a local copy of [Google Test](https://github.com/google/googletest)
153+
will be downloaded during CMake configuration.
161154

162155
Alternately, you may want to use protobuf in a larger set-up, you may want to use that standard CMake approach where
163156
you build and install a shared copy of Google Test.

cmake/abseil-cpp.cmake

+2-16
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ endif()
1313
if(TARGET absl::strings)
1414
# If Abseil is included already, skip including it.
1515
# (https://github.com/protocolbuffers/protobuf/issues/10435)
16-
elseif (protobuf_FETCH_DEPENDENCIES OR protobuf_ABSL_PROVIDER STREQUAL "fetch")
16+
elseif (protobuf_FETCH_DEPENDENCIES AND protobuf_ABSL_PROVIDER STREQUAL "fetch")
1717
include(${protobuf_SOURCE_DIR}/cmake/dependencies.cmake)
1818
include(FetchContent)
1919
FetchContent_Declare(
@@ -27,21 +27,7 @@ elseif (protobuf_FETCH_DEPENDENCIES OR protobuf_ABSL_PROVIDER STREQUAL "fetch")
2727
set(ABSL_ENABLE_INSTALL ON)
2828
endif()
2929
FetchContent_MakeAvailable(absl)
30-
elseif(protobuf_ABSL_PROVIDER STREQUAL "module")
31-
if(NOT ABSL_ROOT_DIR)
32-
set(ABSL_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party/abseil-cpp)
33-
endif()
34-
if(EXISTS "${ABSL_ROOT_DIR}/CMakeLists.txt")
35-
if(protobuf_INSTALL)
36-
# When protobuf_INSTALL is enabled and Abseil will be built as a module,
37-
# Abseil will be installed along with protobuf for convenience.
38-
set(ABSL_ENABLE_INSTALL ON)
39-
endif()
40-
add_subdirectory(${ABSL_ROOT_DIR} third_party/abseil-cpp)
41-
else()
42-
message(WARNING "protobuf_ABSL_PROVIDER is \"module\" but ABSL_ROOT_DIR is wrong")
43-
endif()
44-
elseif(protobuf_ABSL_PROVIDER STREQUAL "package")
30+
else ()
4531
# Use "CONFIG" as there is no built-in cmake module for absl.
4632
find_package(absl REQUIRED CONFIG)
4733
endif()

cmake/conformance.cmake

+4-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# Don't run jsoncpp tests.
22
set(JSONCPP_WITH_TESTS OFF)
33

4-
if (protobuf_FETCH_DEPENDENCIES OR protobuf_JSONCPP_PROVIDER STREQUAL "fetch")
4+
if (TARGET jsoncpp_lib)
5+
# jsoncpp is already present.
6+
elseif (protobuf_FETCH_DEPENDENCIES AND protobuf_JSONCPP_PROVIDER STREQUAL "fetch")
57
include(${protobuf_SOURCE_DIR}/cmake/dependencies.cmake)
68
include(FetchContent)
79
FetchContent_Declare(
@@ -11,17 +13,7 @@ if (protobuf_FETCH_DEPENDENCIES OR protobuf_JSONCPP_PROVIDER STREQUAL "fetch")
1113
GIT_TAG "1.9.4"
1214
)
1315
FetchContent_MakeAvailable(jsoncpp)
14-
elseif (protobuf_JSONCPP_PROVIDER STREQUAL "module")
15-
if (NOT EXISTS "${protobuf_SOURCE_DIR}/third_party/jsoncpp/CMakeLists.txt")
16-
message(FATAL_ERROR
17-
"Cannot find third_party/jsoncpp directory that's needed to "
18-
"build conformance tests. If you use git, make sure you have cloned "
19-
"submodules:\n"
20-
" git submodule update --init --recursive\n"
21-
"If instead you want to skip them, run cmake with:\n"
22-
" cmake -Dprotobuf_BUILD_CONFORMANCE=OFF\n")
23-
endif()
24-
elseif(protobuf_JSONCPP_PROVIDER STREQUAL "package")
16+
else ()
2517
find_package(jsoncpp REQUIRED)
2618
endif()
2719

cmake/gtest.cmake

+11-45
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
option(protobuf_USE_EXTERNAL_GTEST "Use external Google Test (i.e. not the one in third_party/googletest)" OFF)
22

3-
if (protobuf_USE_EXTERNAL_GTEST)
3+
if (TARGET GTest::gmock)
4+
# GTest is already present.
5+
elseif (protobuf_USE_EXTERNAL_GTEST)
46
find_package(GTest REQUIRED CONFIG)
5-
elseif (protobuf_FETCH_DEPENDENCIES)
7+
else ()
8+
if (NOT protobuf_FETCH_DEPENDENCIES)
9+
message(FATAL_ERROR
10+
"Cannot find local googletest directory that's needed to "
11+
"build tests.\n"
12+
"If instead you want to skip tests, run cmake with:\n"
13+
" cmake -Dprotobuf_BUILD_TESTS=OFF\n")
14+
endif()
615
include(${protobuf_SOURCE_DIR}/cmake/dependencies.cmake)
716
include(FetchContent)
817
FetchContent_Declare(
@@ -13,47 +22,4 @@ elseif (protobuf_FETCH_DEPENDENCIES)
1322
# Due to https://github.com/google/googletest/issues/4384, we can't name this
1423
# GTest for use with find_package until 1.15.0.
1524
FetchContent_MakeAvailable(googletest)
16-
else()
17-
if (NOT EXISTS "${protobuf_SOURCE_DIR}/third_party/googletest/CMakeLists.txt")
18-
message(FATAL_ERROR
19-
"Cannot find third_party/googletest directory that's needed to "
20-
"build tests. If you use git, make sure you have cloned submodules:\n"
21-
" git submodule update --init --recursive\n"
22-
"If instead you want to skip tests, run cmake with:\n"
23-
" cmake -Dprotobuf_BUILD_TESTS=OFF\n")
24-
endif()
25-
26-
set(googlemock_source_dir "${protobuf_SOURCE_DIR}/third_party/googletest/googlemock")
27-
set(googletest_source_dir "${protobuf_SOURCE_DIR}/third_party/googletest/googletest")
28-
include_directories(
29-
${googlemock_source_dir}
30-
${googletest_source_dir}
31-
${googletest_source_dir}/include
32-
${googlemock_source_dir}/include
33-
)
34-
35-
add_library(gmock ${protobuf_SHARED_OR_STATIC}
36-
"${googlemock_source_dir}/src/gmock-all.cc"
37-
"${googletest_source_dir}/src/gtest-all.cc"
38-
)
39-
if (protobuf_BUILD_SHARED_LIBS)
40-
set_target_properties(gmock
41-
PROPERTIES
42-
COMPILE_DEFINITIONS
43-
"GTEST_CREATE_SHARED_LIBRARY=1"
44-
)
45-
46-
endif()
47-
if (protobuf_INSTALL)
48-
set(protobuf_INSTALL_TESTS ON)
49-
endif()
50-
51-
target_link_libraries(gmock ${CMAKE_THREAD_LIBS_INIT})
52-
add_library(gmock_main STATIC "${googlemock_source_dir}/src/gmock_main.cc")
53-
target_link_libraries(gmock_main gmock)
54-
55-
add_library(GTest::gmock ALIAS gmock)
56-
add_library(GTest::gmock_main ALIAS gmock_main)
57-
add_library(GTest::gtest ALIAS gmock)
58-
add_library(GTest::gtest_main ALIAS gmock_main)
5925
endif()

0 commit comments

Comments
 (0)