Skip to content

Commit 9cc685e

Browse files
mkruskal-googlecopybara-github
authored andcommitted
Breaking change: Flip default behavior for handling cmake dependencies.
Instead of fetching dependencies by default, we will first look for a local installation and only fetch as a fallback. Two new options are added for forcing either of these behaviors. protobuf_FORCE_FETCH_DEPENDENCIES will always fetch dependencies, and protobuf_PREVENT_FETCH_DEPENDENCIES will never do so. #test-continuous PiperOrigin-RevId: 693898394
1 parent a59cfa4 commit 9cc685e

File tree

9 files changed

+114
-111
lines changed

9 files changed

+114
-111
lines changed

.github/workflows/test_cpp.yml

+12-10
Original file line numberDiff line numberDiff line change
@@ -141,17 +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=17 -Dprotobuf_USE_EXTERNAL_GTEST=ON -Dprotobuf_FETCH_DEPENDENCIES=OFF
144+
- flags: -Dprotobuf_BUILD_EXAMPLES=ON -DCMAKE_CXX_STANDARD=17
145145
- name: Ninja
146-
flags: -G Ninja -DCMAKE_CXX_STANDARD=17 -Dprotobuf_USE_EXTERNAL_GTEST=ON -Dprotobuf_FETCH_DEPENDENCIES=OFF
146+
flags: -G Ninja -DCMAKE_CXX_STANDARD=17
147147
continuous-only: true
148148
- name: Shared
149-
flags: -Dprotobuf_BUILD_SHARED_LIBS=ON -Dprotobuf_BUILD_EXAMPLES=ON -DCMAKE_CXX_STANDARD=17 -Dprotobuf_USE_EXTERNAL_GTEST=ON -Dprotobuf_FETCH_DEPENDENCIES=OFF
149+
flags: -Dprotobuf_BUILD_SHARED_LIBS=ON -Dprotobuf_BUILD_EXAMPLES=ON -DCMAKE_CXX_STANDARD=17
150150
continuous-only: true
151151
- name: C++20
152-
flags: -DCMAKE_CXX_STANDARD=20 -Dprotobuf_USE_EXTERNAL_GTEST=ON -Dprotobuf_FETCH_DEPENDENCIES=OFF
152+
flags: -DCMAKE_CXX_STANDARD=20
153+
- name: Package
154+
flags: -DCMAKE_CXX_STANDARD=17 -Dprotobuf_LOCAL_DEPENDENCIES_ONLY=ON
153155
- name: Fetch
154-
flags: -DCMAKE_CXX_STANDARD=17
156+
flags: -DCMAKE_CXX_STANDARD=17 -Dprotobuf_FORCE_FETCH_DEPENDENCIES=ON
155157

156158
name: ${{ matrix.continuous-only && inputs.continuous-prefix || '' }} Linux CMake ${{ matrix.name}}
157159
runs-on: ubuntu-latest
@@ -188,9 +190,10 @@ jobs:
188190
# Set defaults
189191
- type: package
190192
name: Install
191-
flags: -Dprotobuf_USE_EXTERNAL_GTEST=ON -Dprotobuf_FETCH_DEPENDENCIES=OFF
193+
flags: -Dprotobuf_LOCAL_DEPENDENCIES_ONLY=ON
192194
- type: fetch
193195
name: Install (Fetch)
196+
flags: -Dprotobuf_FORCE_FETCH_DEPENDENCIES=ON
194197
continuous-only: true
195198
name: ${{ matrix.continuous-only && inputs.continuous-prefix || '' }}Linux CMake ${{ matrix.name }}
196199
runs-on: ubuntu-latest
@@ -252,7 +255,7 @@ jobs:
252255
credentials: ${{ secrets.GAR_SERVICE_ACCOUNT }}
253256
command: >-
254257
/install.sh -DCMAKE_CXX_STANDARD=17 ${{ env.SCCACHE_CMAKE_FLAGS }}
255-
-Dprotobuf_USE_EXTERNAL_GTEST=ON -Dprotobuf_FETCH_DEPENDENCIES=OFF
258+
-Dprotobuf_LOCAL_DEPENDENCIES_ONLY=OFF
256259
-Dprotobuf_BUILD_EXAMPLES=OFF \&\&
257260
mkdir examples/build \&\&
258261
cd examples/build \&\&
@@ -320,7 +323,7 @@ jobs:
320323
- name: Run tests
321324
uses: protocolbuffers/protobuf-ci/docker@v3
322325
with:
323-
image: us-docker.pkg.dev/protobuf-build/containers/test/linux/32bit@sha256:56548bef786201330017eae685cc3d2fdb564fd2ca3b88e30e28d84572e4c5dd
326+
image: us-docker.pkg.dev/protobuf-build/containers/test/linux/32bit@sha256:d6028ab408c49932836cdc514116f06886d7f6868a4d430630aa52adc5aee2fc
324327
platform: linux/386
325328
credentials: ${{ secrets.GAR_SERVICE_ACCOUNT }}
326329
command: >-
@@ -426,8 +429,7 @@ jobs:
426429
- name: Windows CMake Install
427430
os: windows-2022
428431
install-flags: >-
429-
-G Ninja -Dprotobuf_WITH_ZLIB=OFF
430-
-Dprotobuf_BUILD_CONFORMANCE=OFF -Dprotobuf_BUILD_TESTS=OFF
432+
-G Ninja -Dprotobuf_WITH_ZLIB=OFF -Dprotobuf_BUILD_CONFORMANCE=OFF
431433
flags: >-
432434
-G Ninja -Dprotobuf_WITH_ZLIB=OFF -Dprotobuf_BUILD_CONFORMANCE=OFF
433435
-Dprotobuf_REMOVE_INSTALLED_HEADERS=ON

CMakeLists.txt

+6-8
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ option(protobuf_BUILD_LIBUPB "Build libupb" ON)
3535
option(protobuf_DISABLE_RTTI "Remove runtime type information in the binaries" OFF)
3636
option(protobuf_TEST_XML_OUTDIR "Output directory for XML logs from tests." "")
3737
option(protobuf_ALLOW_CCACHE "Adjust build flags to allow for ccache support." OFF)
38+
option(protobuf_FORCE_FETCH_DEPENDENCIES "Force all dependencies to be downloaded from GitHub. Local installations will be ignored." OFF)
39+
option(protobuf_LOCAL_DEPENDENCIES_ONLY "Prevent downloading any dependencies from GitHub. If this option is set, the dependency must be available locally as an installed package." OFF)
3840

3941
# We support Unity (Jumbo) builds best-effort.
4042
option(protobuf_USE_UNITY_BUILD "Enable Unity (Jumbo) build for" OFF)
@@ -106,6 +108,10 @@ string(REGEX REPLACE "${protobuf_VERSION_REGEX}" "\\3"
106108
string(REGEX REPLACE "${protobuf_VERSION_REGEX}" "\\5"
107109
protobuf_VERSION_PRERELEASE "${protobuf_VERSION_STRING}")
108110

111+
if (protobuf_FORCE_FETCH_DEPENDENCIES AND protobuf_LOCAL_DEPENDENCIES_ONLY)
112+
message(FATAL_ERROR "Conflicting options protobuf_FORCE_FETCH_DEPENDENCIES and protobuf_LOCAL_DEPENDENCIES_ONLY both set")
113+
endif()
114+
109115
# Package version
110116
set(protobuf_VERSION
111117
"${protobuf_VERSION_MINOR}.${protobuf_VERSION_PATCH}")
@@ -260,14 +266,6 @@ include_directories(
260266
${protobuf_BINARY_DIR}/src
261267
${protobuf_SOURCE_DIR}/src)
262268

263-
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.")
264-
265-
set(protobuf_ABSL_PROVIDER "fetch" CACHE STRING "Provider of absl library. `fetch` downloads from GitHub and `package` searches for a local installation")
266-
set_property(CACHE protobuf_ABSL_PROVIDER PROPERTY STRINGS "package" "fetch")
267-
268-
set(protobuf_JSONCPP_PROVIDER "fetch" CACHE STRING "Provider of jsoncpp library. `fetch` downloads from GitHub and `package` searches for a local installation")
269-
set_property(CACHE protobuf_JSONCPP_PROVIDER PROPERTY STRINGS "package" "fetch")
270-
271269
if (protobuf_BUILD_TESTS)
272270
include(${protobuf_SOURCE_DIR}/cmake/gtest.cmake)
273271
endif (protobuf_BUILD_TESTS)

cmake/README.md

+9-6
Original file line numberDiff line numberDiff line change
@@ -110,19 +110,23 @@ Create a temporary *build* folder and change your working directory to it:
110110
C:\Path\to\build\protobuf>
111111

112112
During configuration you will also be specifying where CMake should expect to
113-
find your Abseil installation. To do so, first set `-Dprotobuf_ABSL_PROVIDER=package`
114-
and then set `-DCMAKE_PREFIX_PATH` to the path where you installed Abseil.
113+
find your Abseil installation. To do so, set `-DCMAKE_PREFIX_PATH` to the path
114+
where you installed Abseil.
115115

116116
For example:
117117

118118
```console
119119
C:\Path\to\build\protobuf> cmake -S. -Bcmake-out \
120120
-DCMAKE_INSTALL_PREFIX=/tmp/protobuf \
121121
-DCMAKE_CXX_STANDARD=14 \
122-
-Dprotobuf_ABSL_PROVIDER=package \
123122
-DCMAKE_PREFIX_PATH=/tmp/absl # Path to where I installed Abseil
124123
```
125124

125+
If the installation of a dependency can't be found, CMake will default to
126+
downloading and building a copy from GitHub. To prevent this and make it an
127+
error condition, you can optionally set
128+
`-Dprotobuf_LOCAL_DEPENDENCIES_ONLY=ON`.
129+
126130
The *Makefile* and *Ninja* generators can build the project in only one configuration, so you need to build
127131
a separate folder for each configuration.
128132

@@ -155,9 +159,8 @@ will be downloaded during CMake configuration.
155159
Alternately, you may want to use protobuf in a larger set-up, you may want to use that standard CMake approach where
156160
you build and install a shared copy of Google Test.
157161

158-
After you've built and installed your Google Test copy, you need add the following definition to your *cmake* command line
159-
during the configuration step: `-Dprotobuf_USE_EXTERNAL_GTEST=ON`.
160-
This will cause the standard CMake `find_package(GTest REQUIRED)` to be used.
162+
After you've built and installed your Google Test copy, the standard CMake
163+
`find_package(GTest)` will use it.
161164

162165
[find_package](https://cmake.org/cmake/help/latest/command/find_package.html) will search in a default location,
163166
which on Windows is *C:\Program Files*. This is most likely not what you want. You will want instead to search for

cmake/abseil-cpp.cmake

+30-26
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,36 @@ if(protobuf_BUILD_TESTS)
1010
set(ABSL_FIND_GOOGLETEST OFF)
1111
endif()
1212

13-
if(TARGET absl::strings)
14-
# If Abseil is included already, skip including it.
15-
# (https://github.com/protocolbuffers/protobuf/issues/10435)
16-
elseif (protobuf_FETCH_DEPENDENCIES AND protobuf_ABSL_PROVIDER STREQUAL "fetch")
17-
include(${protobuf_SOURCE_DIR}/cmake/dependencies.cmake)
18-
include(FetchContent)
19-
FetchContent_Declare(
20-
absl
21-
GIT_REPOSITORY "https://github.com/abseil/abseil-cpp.git"
22-
GIT_TAG "${abseil-cpp-version}"
23-
)
24-
if(protobuf_INSTALL)
25-
# When protobuf_INSTALL is enabled and Abseil will be built as a module,
26-
# Abseil will be installed along with protobuf for convenience.
27-
set(ABSL_ENABLE_INSTALL ON)
13+
if (NOT TARGET absl::strings)
14+
if (NOT protobuf_FORCE_FETCH_DEPENDENCIES)
15+
# Use "CONFIG" as there is no built-in cmake module for absl.
16+
find_package(absl CONFIG)
17+
endif()
18+
19+
# Fallback to fetching Abseil from github if it's not found locally.
20+
if (NOT absl_FOUND AND NOT protobuf_LOCAL_DEPENDENCIES_ONLY)
21+
include(${protobuf_SOURCE_DIR}/cmake/dependencies.cmake)
22+
message(STATUS "Fallback to downloading Abseil ${abseil-cpp-version} from GitHub")
23+
24+
include(FetchContent)
25+
FetchContent_Declare(
26+
absl
27+
GIT_REPOSITORY "https://github.com/abseil/abseil-cpp.git"
28+
GIT_TAG "${abseil-cpp-version}"
29+
)
30+
if (protobuf_INSTALL)
31+
# When protobuf_INSTALL is enabled and Abseil will be built as a module,
32+
# Abseil will be installed along with protobuf for convenience.
33+
set(ABSL_ENABLE_INSTALL ON)
34+
endif()
35+
FetchContent_MakeAvailable(absl)
2836
endif()
29-
FetchContent_MakeAvailable(absl)
30-
else ()
31-
# Use "CONFIG" as there is no built-in cmake module for absl.
32-
find_package(absl REQUIRED CONFIG)
3337
endif()
38+
39+
if (NOT TARGET absl::strings)
40+
message(FATAL_ERROR "Cannot find abseil-cpp dependency that's needed to build protobuf.\n")
41+
endif()
42+
3443
set(_protobuf_FIND_ABSL "if(NOT TARGET absl::strings)\n find_package(absl CONFIG)\nendif()")
3544

3645
if (BUILD_SHARED_LIBS AND MSVC)
@@ -41,13 +50,8 @@ if (BUILD_SHARED_LIBS AND MSVC)
4150
# Once https://github.com/abseil/abseil-cpp/pull/1466 is merged and released
4251
# in the minimum version of abseil required by protobuf, it is possible to
4352
# always link absl::abseil_dll and absl::abseil_test_dll and remove the if
44-
if(protobuf_ABSL_PROVIDER STREQUAL "package")
45-
set(protobuf_ABSL_USED_TARGETS absl::abseil_dll)
46-
set(protobuf_ABSL_USED_TEST_TARGETS absl::abseil_test_dll)
47-
else()
48-
set(protobuf_ABSL_USED_TARGETS abseil_dll)
49-
set(protobuf_ABSL_USED_TEST_TARGETS abseil_test_dll)
50-
endif()
53+
set(protobuf_ABSL_USED_TARGETS absl::abseil_dll)
54+
set(protobuf_ABSL_USED_TEST_TARGETS absl::abseil_test_dll)
5155
else()
5256
set(protobuf_ABSL_USED_TARGETS
5357
absl::absl_check

cmake/conformance.cmake

+25-17
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
11
# Don't run jsoncpp tests.
22
set(JSONCPP_WITH_TESTS OFF)
33

4-
if (TARGET jsoncpp_lib)
5-
# jsoncpp is already present.
6-
elseif (protobuf_FETCH_DEPENDENCIES AND protobuf_JSONCPP_PROVIDER STREQUAL "fetch")
7-
include(${protobuf_SOURCE_DIR}/cmake/dependencies.cmake)
8-
include(FetchContent)
9-
FetchContent_Declare(
10-
jsoncpp
11-
GIT_REPOSITORY "https://github.com/open-source-parsers/jsoncpp.git"
12-
GIT_TAG "${jsoncpp-version}"
13-
)
14-
FetchContent_MakeAvailable(jsoncpp)
15-
else ()
16-
find_package(jsoncpp REQUIRED)
4+
if (NOT TARGET jsoncpp_lib)
5+
if (NOT protobuf_FORCE_FETCH_DEPENDENCIES)
6+
find_package(jsoncpp)
7+
endif()
8+
9+
# Fallback to fetching Googletest from github if it's not found locally.
10+
if (NOT jsoncpp_FOUND AND NOT protobuf_LOCAL_DEPENDENCIES_ONLY)
11+
include(${protobuf_SOURCE_DIR}/cmake/dependencies.cmake)
12+
message(STATUS "Fallback to downloading jsoncpp ${jsoncpp-version} from GitHub")
13+
14+
include(FetchContent)
15+
FetchContent_Declare(
16+
jsoncpp
17+
GIT_REPOSITORY "https://github.com/open-source-parsers/jsoncpp.git"
18+
GIT_TAG "${jsoncpp-version}"
19+
)
20+
FetchContent_MakeAvailable(jsoncpp)
21+
endif()
22+
endif()
23+
24+
if (NOT TARGET jsoncpp_lib)
25+
message(FATAL_ERROR
26+
"Cannot find jsoncpp dependency that's needed to build conformance tests.\n"
27+
"If instead you want to skip these tests, run cmake with:\n"
28+
" cmake -Dprotobuf_BUILD_CONFORMANCE=OFF\n")
1729
endif()
1830

1931
file(MAKE_DIRECTORY ${protobuf_BINARY_DIR}/conformance)
@@ -137,10 +149,6 @@ add_test(NAME conformance_cpp_test
137149
DEPENDS conformance_test_runner conformance_cpp)
138150

139151
set(JSONCPP_WITH_TESTS OFF CACHE BOOL "Disable tests")
140-
if(NOT protobuf_FETCH_DEPENDENCIES AND protobuf_JSONCPP_PROVIDER STREQUAL "module")
141-
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/third_party/jsoncpp third_party/jsoncpp)
142-
target_include_directories(conformance_test_runner PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/third_party/jsoncpp/include)
143-
endif()
144152

145153
if(BUILD_SHARED_LIBS)
146154
target_link_libraries(conformance_test_runner jsoncpp_lib)

cmake/gtest.cmake

+25-22
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
1-
option(protobuf_USE_EXTERNAL_GTEST "Use external Google Test (i.e. not the one in third_party/googletest)" OFF)
1+
if (NOT TARGET GTest::gmock)
2+
if (NOT protobuf_FORCE_FETCH_DEPENDENCIES)
3+
find_package(GTest CONFIG)
4+
endif()
5+
6+
# Fallback to fetching Googletest from github if it's not found locally.
7+
if (NOT GTest_FOUND AND NOT protobuf_LOCAL_DEPENDENCIES_ONLY)
8+
include(${protobuf_SOURCE_DIR}/cmake/dependencies.cmake)
9+
message(STATUS "Fallback to downloading GTest ${googletest-version} from GitHub")
210

3-
if (TARGET GTest::gmock)
4-
# GTest is already present.
5-
elseif (protobuf_USE_EXTERNAL_GTEST)
6-
find_package(GTest REQUIRED CONFIG)
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")
11+
include(FetchContent)
12+
FetchContent_Declare(
13+
googletest
14+
GIT_REPOSITORY "https://github.com/google/googletest.git"
15+
GIT_TAG "v${googletest-version}"
16+
)
17+
# Due to https://github.com/google/googletest/issues/4384, we can't name this
18+
# GTest for use with find_package until 1.15.0.
19+
FetchContent_MakeAvailable(googletest)
1420
endif()
15-
include(${protobuf_SOURCE_DIR}/cmake/dependencies.cmake)
16-
include(FetchContent)
17-
FetchContent_Declare(
18-
googletest
19-
GIT_REPOSITORY "https://github.com/google/googletest.git"
20-
GIT_TAG "v${googletest-version}"
21-
)
22-
# Due to https://github.com/google/googletest/issues/4384, we can't name this
23-
# GTest for use with find_package until 1.15.0.
24-
FetchContent_MakeAvailable(googletest)
21+
endif()
22+
23+
if (NOT TARGET GTest::gmock)
24+
message(FATAL_ERROR
25+
"Cannot find googletest dependency that's needed to build tests.\n"
26+
"If instead you want to skip tests, run cmake with:\n"
27+
" cmake -Dprotobuf_BUILD_TESTS=OFF\n")
2528
endif()

csharp/src/Google.Protobuf/Reflection/FeatureSetDescriptor.g.cs

-17
This file was deleted.

src/google/protobuf/io/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ cc_test(
224224
"@com_google_absl//absl/strings",
225225
"@com_google_absl//absl/strings:cord",
226226
"@com_google_absl//absl/strings:str_format",
227+
"@com_google_absl//absl/synchronization",
227228
"@com_google_absl//absl/types:optional",
228229
"@com_google_googletest//:gtest",
229230
"@com_google_googletest//:gtest_main",

src/google/protobuf/io/zero_copy_stream_unittest.cc

+6-5
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#include "absl/strings/cord_buffer.h"
5454
#include "absl/strings/str_cat.h"
5555
#include "absl/strings/string_view.h"
56+
#include "absl/synchronization/mutex.h"
5657
#include "google/protobuf/io/coded_stream.h"
5758
#include "google/protobuf/io/io_win32.h"
5859
#include "google/protobuf/io/zero_copy_stream_impl.h"
@@ -1446,14 +1447,14 @@ TEST_F(IoTest, NonBlockingFileIo) {
14461447
ASSERT_EQ(fcntl(fd[0], F_SETFL, O_NONBLOCK), 0);
14471448
ASSERT_EQ(fcntl(fd[1], F_SETFL, O_NONBLOCK), 0);
14481449

1449-
std::mutex go_write;
1450-
go_write.lock();
1450+
absl::Mutex go_write;
1451+
go_write.Lock();
14511452

14521453
bool done_reading = false;
14531454

14541455
std::thread write_thread([this, fd, &go_write, i]() {
1455-
go_write.lock();
1456-
go_write.unlock();
1456+
go_write.Lock();
1457+
go_write.Unlock();
14571458
FileOutputStream output(fd[1], kBlockSizes[i]);
14581459
WriteStuff(&output);
14591460
EXPECT_EQ(0, output.GetErrno());
@@ -1472,7 +1473,7 @@ TEST_F(IoTest, NonBlockingFileIo) {
14721473
// reading thread waits for the data to be available before returning.
14731474
std::this_thread::sleep_for(std::chrono::milliseconds(100));
14741475
EXPECT_FALSE(done_reading);
1475-
go_write.unlock();
1476+
go_write.Unlock();
14761477
write_thread.join();
14771478
read_thread.join();
14781479
EXPECT_TRUE(done_reading);

0 commit comments

Comments
 (0)