Skip to content

build(cmake): Modernize target-scoped CMake configuration#3377

Open
darion-yaphet wants to merge 2 commits into
apache:masterfrom
darion-yaphet:update-cmake-minimum
Open

build(cmake): Modernize target-scoped CMake configuration#3377
darion-yaphet wants to merge 2 commits into
apache:masterfrom
darion-yaphet:update-cmake-minimum

Conversation

@darion-yaphet

Copy link
Copy Markdown
Contributor

What problem does this PR solve?

Issue Number: N/A

Problem Summary:
The CMake build files still rely on legacy global include directories, compile flags, and duplicated per-example configuration. This makes target behavior harder to
reason about and causes standalone examples/tests to drift from the main build configuration.

What is changed and the side effects?

Changed:

  • Modernize top-level, src, tools, and test CMake targets with target-scoped configuration.
  • Add shared brpc_common_config for common compile definitions, include paths, options, and features.
  • Refactor standalone example CMake files to use example/cmake/BrpcExample.cmake.
  • Update the gtest download helper to use CMake 3.16...3.28.
  • Ensure downloaded gtest headers take priority over Homebrew's C++17-only gtest headers during unit-test builds.

Side effects:

  • Standalone examples now consistently require CMake 3.16 or newer, matching the main project requirement.

  • DOWNLOAD_GTEST=ON creates a copied gtest include directory in the test build tree to avoid header conflicts with system-installed gtest.

  • Performance effects:
    No runtime performance impact expected. This is a build-system-only change.

  • Breaking backward compatibility:
    No source/API compatibility break expected. Users building standalone examples with CMake older than 3.16 will need to upgrade CMake.


Check List:

Verification

Passed:

  • cmake -S . -B build -DOPENSSL_ROOT_DIR=/opt/homebrew/opt/openssl@3
  • cmake --build build -j6
  • cmake -S . -B build-tests -DOPENSSL_ROOT_DIR=/opt/homebrew/opt/openssl@3 -DBUILD_UNIT_TESTS=ON -DDOWNLOAD_GTEST=ON
  • cmake --build build-tests -j6
  • Standalone example smoke builds for echo_c++, http_c++, and memcache_c++

Known macOS arm64 test issues:

  • test_butil fails in StackTraceTest.find_symbol
  • bthread_fd_unittest fails in FDTest.ping_pong
  • rdma_performance standalone example was not tested because ibverbs is unavailable on macOS

Raise the minimum CMake version range to 3.16...3.28 so CMake 4.x no longer treats the project as relying on removed pre-3.5 policy compatibility. The range preserves a conservative 3.16 runtime floor while documenting policy validation through 3.28.
Move shared compile definitions, include paths, options, and link dependencies onto interface targets so top-level, src, tools, tests, and standalone examples consume consistent build settings without relying on global directory state.

Examples now share a small CMake helper, and the gtest download path is updated to configure with modern CMake while keeping downloaded gtest headers ahead of Homebrew's C++17-only installation.
Comment thread CMakeLists.txt
# specific language governing permissions and limitations
# under the License.

cmake_minimum_required(VERSION 2.8.12)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The upgraded version seems too high, is it necessary?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The upgraded version seems too high, is it necessary?

I understand the concern about compatibility, but CMake 3.16 is actually not very radical. CMake 2.x was quite an old era. The main line has moved to 4.x now, and CMake 3 is the mainstream baseline for many years past.

The main benefits of upgrading to CMake 3 are not "keeping up with the latest trends", but rather the ability to use modern CMake syntax: target-based configuration, target_compile_features, target_link_options, generator expressions, better dependency discovery and cross-platform behavior. These features can avoid the pollution of global CFLAGS/LDFLAGS, allowing includes, macro definitions, and link parameters to be propagated by target. This makes the project easier to maintain and less likely to fail due to differences in the local environment.

Overall, 3.16 represents a relatively conservative modern baseline, not a radical upgrade.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants