fix(cmake): prioritize discovered zlib headers for brpc sources - #3475
fix(cmake): prioritize discovered zlib headers for brpc sources#3475zchuango wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR addresses a CMake integration issue when bRPC is consumed via add_subdirectory(): inherited include directories from a parent project can cause Protobuf’s gzip_stream.h to pick up an unrelated zlib.h (e.g., Crypto++), breaking compilation. The changes make zlib discovery explicit and attempt to prioritize the intended zlib headers during compilation of bRPC sources.
Changes:
- Add
find_package(ZLIB REQUIRED)and link againstZLIB::ZLIBinstead of the barezlibrary name. - Prepend the discovered zlib include directory(ies) for the
SOURCES_LIBobject library to avoid header collisions.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| CMakeLists.txt | Explicitly discovers zlib and links with ZLIB::ZLIB to use CMake’s standard zlib integration. |
| src/CMakeLists.txt | Adjusts include ordering for SOURCES_LIB so Protobuf’s gzip compilation resolves the intended zlib.h. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
|
|
||
| # 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}) |
There was a problem hiding this 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.
|
@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? |
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++'szlib.h, protobuf'sgzip_stream.hmay resolve that header instead of the zlib header discovered for the build. This causes protobuf compilation to fail because types such asz_streamare not defined:The issue is caused by compile-time header resolution, so linking brpc against the bare
zlibrary name does not ensure that protobuf includes the intended zlib header.What is changed and the side effects?
Changed:
find_package(ZLIB REQUIRED).ZLIB::ZLIBinstead of the bare library namez.SOURCES_LIB, where protobuf'sgzip_stream.his compiled.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:
FindZLIBresult explicitly.Verification
The issue was reproduced with a minimal parent CMake project that:
add_subdirectory;zlib.h;parent_smokeexecutable against brpc.Before this change, the reproduction failed while compiling protobuf's
gzip_stream.hwith:After this change, a fresh build of the same collision case completed successfully:
The fresh build completed all 378 steps, linked
libbrpc.aandparent_smoke, and ranparent_smokesuccessfully.Additional verification:
brpc-staticbuild: passed.parent_smokecompile, link, and execution: passed.zlib.h: passed after the change.git diff --check: passed.Check List: