You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@brpc.apache.org by "zchuango (via GitHub)" <gi...@apache.org> on 2026/08/22 08:16:44 UTC
[PR] fix(cmake): prioritize discovered zlib headers for brpc sources (#23) (brpc)
zchuango opened a new pull request, #3475:
URL: https://github.com/apache/brpc/pull/3475
### What problem does this PR solve?
Issue Number: resolve #2593
Problem Summary:
When brpc is integrated into a parent CMake project with `add_subdirectory`, include directories added by the parent project may be inherited by brpc targets.
If one of those directories contains an unrelated header named `zlib.h`, such as Crypto++'s `zlib.h`, protobuf's `gzip_stream.h` may resolve that header instead of the zlib header discovered for the build. This causes protobuf compilation to fail because types such as `z_stream` are not defined:
```text
/usr/include/google/protobuf/io/gzip_stream.h:93:3: error: 'z_stream' does not name a type
```
The issue is caused by compile-time header resolution, so linking brpc against the bare `z` library name does not ensure that protobuf includes the intended zlib header.
### What is changed and the side effects?
Changed:
- Find zlib explicitly with `find_package(ZLIB REQUIRED)`.
- Link brpc with the standard CMake target `ZLIB::ZLIB` instead of the bare library name `z`.
- Prioritize the discovered zlib include directories for `SOURCES_LIB`, where protobuf's `gzip_stream.h` is compiled.
- Keep the include-path adjustment target-scoped so that it does not modify global include directories or affect unrelated parent-project targets.
This allows brpc to use the intended zlib header even when a parent project supplies an inherited include directory containing another `zlib.h`.
Side effects:
- Performance effects: None expected.
- Breaking backward compatibility: None expected.
- Dependency changes: None. zlib was already required by brpc; this change uses CMake's standard `FindZLIB` result explicitly.
### Verification
The issue was reproduced with a minimal parent CMake project that:
- integrates brpc using `add_subdirectory`;
- adds an inherited include directory containing Crypto++'s `zlib.h`;
- builds and links a `parent_smoke` executable against brpc.
Before this change, the reproduction failed while compiling protobuf's `gzip_stream.h` with:
```text
error: 'z_stream' does not name a type
```
After this change, a fresh build of the same collision case completed successfully:
```powershell
docker exec brpc-2593-dev bash -lc "cmake --build /workspace/screenshot-collision-build --target parent_smoke -j2 && /workspace/screenshot-collision-build/parent_smoke && echo PATCHED_COLLISION_CLEAN_BUILD_OK"
```
<img width="1096" height="119" alt="1" src="https://github.com/user-attachments/assets/e697a5a8-9437-4eb4-a4f3-25c217e07352" />
<img width="881" height="82" alt="2" src="https://github.com/user-attachments/assets/619dfa78-48a9-4458-bcf2-0bb3ee978001" />
The fresh build completed all 378 steps, linked `libbrpc.a` and `parent_smoke`, and ran `parent_smoke` successfully.
Additional verification:
- Standalone CMake configuration: passed.
- Standalone `brpc-static` build: passed.
- Parent-project CMake configuration: passed.
- Parent-project `parent_smoke` compile, link, and execution: passed.
- Collision reproduction with Crypto++'s `zlib.h`: passed after the change.
- ARM64 cross-compilation and linking: passed.
- ARM64 runtime execution: not performed because QEMU/binfmt was unavailable in the test environment.
- `git diff --check`: passed.
### Check List:
- Please make sure your changes are compilable.
- When providing us with a new feature, it is best to add related tests.
- Please follow [[Contributor Covenant Code of Conduct](https://github.com/apache/brpc/blob/master/CODE_OF_CONDUCT.md)](https://github.com/apache/brpc/blob/master/CODE_OF_CONDUCT.md).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] fix(cmake): prioritize discovered zlib headers for brpc sources (brpc)
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #3475:
URL: https://github.com/apache/brpc/pull/3475#discussion_r3835737153
##########
CMakeLists.txt:
##########
@@ -351,7 +352,7 @@ set(DYNAMIC_LIB
${CMAKE_THREAD_LIBS_INIT}
${THRIFT_LIB}
dl
- z)
+ ZLIB::ZLIB)
Review Comment:
Switching the link dependency from the bare library name `z` to `ZLIB::ZLIB` can make the actual zlib link flag/library name platform- or toolchain-specific (e.g. static variants). The generated pkg-config metadata still hard-codes `-lz` via BRPC_PRIVATE_LIBS, which may no longer match what CMake uses and can break consumers that rely on `brpc.pc` for static linking.
##########
src/CMakeLists.txt:
##########
@@ -21,6 +21,10 @@ add_dependencies(SOURCES_LIB PROTO_LIB)
target_link_libraries(BUTIL_LIB PRIVATE brpc_common_config)
target_link_libraries(SOURCES_LIB PRIVATE brpc_common_config)
+# protobuf/io/gzip_stream.h includes <zlib.h>. Prioritize the discovered
+# zlib headers over include directories inherited from parent projects.
+target_include_directories(SOURCES_LIB BEFORE PRIVATE ${ZLIB_INCLUDE_DIRS})
Review Comment:
Using ${ZLIB_INCLUDE_DIRS} duplicates FindZLIB state and can diverge from the imported target you now link (ZLIB::ZLIB). Prefer sourcing the include dirs from ZLIB::ZLIB so there’s a single authoritative discovery result (and it stays correct if ZLIB_INCLUDE_DIRS is not populated as expected).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] fix(cmake): prioritize discovered zlib headers for brpc sources (brpc)
Posted by "zchuango (via GitHub)" <gi...@apache.org>.
zchuango commented on PR #3475:
URL: https://github.com/apache/brpc/pull/3475#issuecomment-5392412464
@wasphin I’m not sure I follow. Are you suggesting that this should be fixed entirely in the parent project, with no changes needed in bRPC?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] fix(cmake): prioritize discovered zlib headers for brpc sources (brpc)
Posted by "wasphin (via GitHub)" <gi...@apache.org>.
wasphin commented on PR #3475:
URL: https://github.com/apache/brpc/pull/3475#issuecomment-5396364867
@zchuango Adding find_package(ZLIB REQUIRED) and linking against ZLIB::ZLIB is reasonable because zlib is a direct dependency of bRPC.
My concern is specifically about changing SOURCES_LIB include ordering to work around include directories leaked from the parent project. bRPC cannot guarantee that its dependency headers will never conflict with unrelated headers exposed by a parent project, and prioritizing the zlib include directory only addresses this particular collision rather than the underlying dependency-scope issue.
The parent project should scope each module dependency with target-level CMake commands, such as target_include_directories(), instead of exposing unrelated include directories through directory-level settings. This prevents unnecessary dependencies from leaking into bRPC compilation scope. Therefore, I do not think bRPC should work around the parent project dependency leakage by changing its internal include order.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] fix(cmake): prioritize discovered zlib headers for brpc sources (brpc)
Posted by "wasphin (via GitHub)" <gi...@apache.org>.
wasphin merged PR #3475:
URL: https://github.com/apache/brpc/pull/3475
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] fix(cmake): prioritize discovered zlib headers for brpc sources (brpc)
Posted by "zchuango (via GitHub)" <gi...@apache.org>.
zchuango commented on code in PR #3475:
URL: https://github.com/apache/brpc/pull/3475#discussion_r3835825641
##########
src/CMakeLists.txt:
##########
@@ -21,6 +21,10 @@ add_dependencies(SOURCES_LIB PROTO_LIB)
target_link_libraries(BUTIL_LIB PRIVATE brpc_common_config)
target_link_libraries(SOURCES_LIB PRIVATE brpc_common_config)
+# protobuf/io/gzip_stream.h includes <zlib.h>. Prioritize the discovered
+# zlib headers over include directories inherited from parent projects.
+target_include_directories(SOURCES_LIB BEFORE PRIVATE ${ZLIB_INCLUDE_DIRS})
Review Comment:
ZLIB_INCLUDE_DIRS is a documented result variable provided by the same FindZLIB invocation that creates ZLIB::ZLIB. It is used explicitly here because BEFORE ordering is required to prevent inherited include directories from shadowing the intended zlib.h.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] fix(cmake): prioritize discovered zlib headers for brpc sources (brpc)
Posted by "zchuango (via GitHub)" <gi...@apache.org>.
zchuango commented on code in PR #3475:
URL: https://github.com/apache/brpc/pull/3475#discussion_r3835824825
##########
CMakeLists.txt:
##########
@@ -351,7 +352,7 @@ set(DYNAMIC_LIB
${CMAKE_THREAD_LIBS_INIT}
${THRIFT_LIB}
dl
- z)
+ ZLIB::ZLIB)
Review Comment:
brpc.pc has historically represented zlib as the portable linker name -lz, together with the other manually maintained private dependencies. Translating a CMake imported target into relocatable pkg-config metadata is outside the scope of this header-resolution fix; using an absolute library path would also make the generated .pc file non-relocatable. We can address pkg-config dependency modeling separately if a concrete static-linking failure is identified.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] fix(cmake): prioritize discovered zlib headers for brpc sources (brpc)
Posted by "wasphin (via GitHub)" <gi...@apache.org>.
wasphin commented on code in PR #3475:
URL: https://github.com/apache/brpc/pull/3475#discussion_r3839089242
##########
src/CMakeLists.txt:
##########
@@ -21,6 +21,10 @@ add_dependencies(SOURCES_LIB PROTO_LIB)
target_link_libraries(BUTIL_LIB PRIVATE brpc_common_config)
target_link_libraries(SOURCES_LIB PRIVATE brpc_common_config)
+# protobuf/io/gzip_stream.h includes <zlib.h>. Prioritize the discovered
+# zlib headers over include directories inherited from parent projects.
+target_include_directories(SOURCES_LIB BEFORE PRIVATE ${ZLIB_INCLUDE_DIRS})
Review Comment:
This should be addressed at the parent project level by scoping the include paths to the intended modules, rather than modifying brpc's internal header order.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] fix(cmake): prioritize discovered zlib headers for brpc sources (brpc)
Posted by "zchuango (via GitHub)" <gi...@apache.org>.
zchuango commented on PR #3475:
URL: https://github.com/apache/brpc/pull/3475#issuecomment-5412504859
> @zchuango Adding find_package(ZLIB REQUIRED) and linking against ZLIB::ZLIB is reasonable because zlib is a direct dependency of bRPC.
>
> My concern is specifically about changing SOURCES_LIB include ordering to work around include directories leaked from the parent project. bRPC cannot guarantee that its dependency headers will never conflict with unrelated headers exposed by a parent project, and prioritizing the zlib include directory only addresses this particular collision rather than the underlying dependency-scope issue.
>
> The parent project should scope each module dependency with target-level CMake commands, such as target_include_directories(), instead of exposing unrelated include directories through directory-level settings. This prevents unnecessary dependencies from leaking into bRPC compilation scope. Therefore, I do not think bRPC should work around the parent project dependency leakage by changing its internal include order.
I agree that bRPC should not adjust its internal include ordering to compensate for include directories leaked by a parent project.
I have removed the target_include_directories(SOURCES_LIB BEFORE ...) workaround. The PR now only discovers zlib explicitly with find_package(ZLIB REQUIRED) and links it through ZLIB::ZLIB, since zlib is a direct dependency of bRPC.
I will also update the PR description to clarify that parent-project include leakage is not addressed by this change.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org