From 4d1223a57d92258e3b0974ae6bfb69d12cf1797e Mon Sep 17 00:00:00 2001 From: songgaoye Date: Thu, 15 Oct 2026 13:54:23 +0800 Subject: [PATCH 1/6] Fix CMake protobuf include order --- CMakeLists.txt | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4d00c90b1e..94db9d7065 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -418,7 +418,6 @@ endif() list(APPEND BRPC_COMMON_INCLUDE_DIRS ${GFLAGS_INCLUDE_PATH} - ${PROTOBUF_INCLUDE_DIRS} ${LEVELDB_INCLUDE_PATH} ) if(WITH_URMA) @@ -511,7 +510,13 @@ add_library(brpc_common_config INTERFACE) # Generated protobuf headers live in the build directory. Search it before src/ # so leftover in-source *.pb.h (gitignored, produced by Make) cannot shadow # updated .proto files such as errno.proto. -target_include_directories(brpc_common_config BEFORE INTERFACE ${CMAKE_CURRENT_BINARY_DIR}) +# Keep protobuf headers ahead of generic dependency include paths. On Homebrew, +# a dependency may contribute the common prefix containing a different protobuf +# installation. +target_include_directories(brpc_common_config BEFORE INTERFACE + ${CMAKE_CURRENT_BINARY_DIR} + ${PROTOBUF_INCLUDE_DIRS} +) target_include_directories(brpc_common_config INTERFACE ${BRPC_COMMON_INCLUDE_DIRS}) target_compile_definitions(brpc_common_config INTERFACE ${BRPC_COMMON_DEFINITIONS}) target_compile_options(brpc_common_config INTERFACE ${BRPC_COMMON_COMPILE_OPTIONS}) From 500c13a0552a00953f9673e863cd8b788c00150b Mon Sep 17 00:00:00 2001 From: songgaoye Date: Fri, 16 Oct 2026 17:24:47 +0800 Subject: [PATCH 2/6] Fix CMake include path ordering --- CMakeLists.txt | 13 ++++--------- test/CMakeLists.txt | 6 +++++- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 94db9d7065..0590389dab 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -70,9 +70,7 @@ endif() set(BRPC_COMMON_COMPILE_OPTIONS) set(BRPC_COMMON_DEFINITIONS) -set(BRPC_COMMON_INCLUDE_DIRS - ${PROJECT_SOURCE_DIR}/src -) +set(BRPC_COMMON_INCLUDE_DIRS) set(BRPC_COMMON_LINK_OPTIONS) set(BRPC_CXX_STANDARD 14) @@ -507,14 +505,11 @@ endif() list(REMOVE_DUPLICATES BRPC_COMMON_INCLUDE_DIRS) add_library(brpc_common_config INTERFACE) -# Generated protobuf headers live in the build directory. Search it before src/ -# so leftover in-source *.pb.h (gitignored, produced by Make) cannot shadow -# updated .proto files such as errno.proto. -# Keep protobuf headers ahead of generic dependency include paths. On Homebrew, -# a dependency may contribute the common prefix containing a different protobuf -# installation. +# Keep generated, in-tree, and selected protobuf headers ahead of generic +# dependency paths to avoid header shadowing. target_include_directories(brpc_common_config BEFORE INTERFACE ${CMAKE_CURRENT_BINARY_DIR} + ${PROJECT_SOURCE_DIR}/src ${PROTOBUF_INCLUDE_DIRS} ) target_include_directories(brpc_common_config INTERFACE ${BRPC_COMMON_INCLUDE_DIRS}) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 71baf85df4..843eaf9603 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -19,7 +19,11 @@ find_package(Gperftools) add_library(brpc_test_config INTERFACE) target_link_libraries(brpc_test_config INTERFACE brpc_common_config) -target_include_directories(brpc_test_config INTERFACE ${CMAKE_CURRENT_BINARY_DIR}) +target_include_directories(brpc_test_config BEFORE INTERFACE + ${CMAKE_CURRENT_BINARY_DIR} + ${PROJECT_SOURCE_DIR}/src + ${PROTOBUF_INCLUDE_DIRS} +) target_compile_definitions(brpc_test_config INTERFACE UNIT_TEST BVAR_NOT_LINK_DEFAULT_VARIABLES From 91caebbe6ab8b07ec880ef52bee0448b92a86dc6 Mon Sep 17 00:00:00 2001 From: songgaoye Date: Sun, 18 Oct 2026 12:17:01 +0800 Subject: [PATCH 3/6] Fix CMake test include path ordering --- test/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 843eaf9603..66cd98992a 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -21,7 +21,7 @@ add_library(brpc_test_config INTERFACE) target_link_libraries(brpc_test_config INTERFACE brpc_common_config) target_include_directories(brpc_test_config BEFORE INTERFACE ${CMAKE_CURRENT_BINARY_DIR} - ${PROJECT_SOURCE_DIR}/src + ${PROJECT_BINARY_DIR} ${PROTOBUF_INCLUDE_DIRS} ) target_compile_definitions(brpc_test_config INTERFACE From 7adf0143edf03394ce5c314e631d51f7957439ce Mon Sep 17 00:00:00 2001 From: songgaoye Date: Wed, 21 Oct 2026 22:29:05 +0800 Subject: [PATCH 4/6] cmake: prioritize selected Protobuf include directories --- src/butil/CMakeLists.txt | 15 +++++++-- test/CMakeLists.txt | 11 +++++++ test/check_protobuf_include_order.cmake | 41 +++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 test/check_protobuf_include_order.cmake diff --git a/src/butil/CMakeLists.txt b/src/butil/CMakeLists.txt index 1e57710a19..5b7c3c85a9 100644 --- a/src/butil/CMakeLists.txt +++ b/src/butil/CMakeLists.txt @@ -46,7 +46,9 @@ configure_file(${CMAKE_CURRENT_SOURCE_DIR}/config.h.in get_filename_component(butil_generated_include_dir "${CMAKE_CURRENT_BINARY_DIR}/.." ABSOLUTE) -set(BUTIL_INCLUDE_DIRS ${GFLAGS_INCLUDE_PATH} ${PROTOBUF_INCLUDE_DIRS}) +# Keep generic dependency prefixes after the selected Protobuf installation. +# Otherwise a shared prefix can supply incompatible google/protobuf headers. +set(BUTIL_INCLUDE_DIRS ${GFLAGS_INCLUDE_PATH}) if(WITH_GLOG) find_path(GLOG_INCLUDE_PATH NAMES glog/logging.h) find_library(GLOG_LIB NAMES glog) @@ -85,9 +87,18 @@ add_library(butil_build_config INTERFACE) target_link_libraries(butil_build_config INTERFACE brpc_common_config) target_compile_definitions(butil_build_config INTERFACE ${BUTIL_DEFINITIONS}) target_include_directories(butil_build_config BEFORE INTERFACE - ${butil_generated_include_dir}) + ${butil_generated_include_dir} + ${Protobuf_INCLUDE_DIRS}) target_include_directories(butil_build_config INTERFACE ${BUTIL_INCLUDE_DIRS}) +get_target_property(butil_build_config_include_dirs + butil_build_config INTERFACE_INCLUDE_DIRECTORIES) +string(REPLACE ";" "\n" butil_build_config_include_dirs + "${butil_build_config_include_dirs}") +file(GENERATE + OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/butil_build_config_include_dirs.txt" + CONTENT "${butil_build_config_include_dirs}\n") + set(butil_crc32c_options) if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") if(CMAKE_SYSTEM_PROCESSOR MATCHES "(x86)|(X86)|(amd64)|(AMD64)") diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 1e9b4489d2..a2c870469a 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -254,6 +254,17 @@ target_link_libraries(test_butil PRIVATE add_test(NAME test_butil COMMAND test_butil) +string(REPLACE ";" "|" protobuf_include_dirs_for_test + "${Protobuf_INCLUDE_DIRS}") +string(REPLACE ";" "|" gflags_include_dirs_for_test + "${GFLAGS_INCLUDE_PATH}") +add_test(NAME cmake_protobuf_include_order + COMMAND ${CMAKE_COMMAND} + "-DINCLUDE_DIRS_FILE=${PROJECT_BINARY_DIR}/src/butil/butil_build_config_include_dirs.txt" + "-DPROTOBUF_INCLUDE_DIRS=${protobuf_include_dirs_for_test}" + "-DGENERIC_INCLUDE_DIRS=${gflags_include_dirs_for_test}" + -P ${CMAKE_CURRENT_SOURCE_DIR}/check_protobuf_include_order.cmake) + # test_bvar # -DBVAR_NOT_LINK_DEFAULT_VARIABLES not work for gcc >= 5.0, just remove the file to prevent linking into unit tests get_filename_component(bvar_default_variables diff --git a/test/check_protobuf_include_order.cmake b/test/check_protobuf_include_order.cmake new file mode 100644 index 0000000000..42121afe92 --- /dev/null +++ b/test/check_protobuf_include_order.cmake @@ -0,0 +1,41 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +if(NOT EXISTS "${INCLUDE_DIRS_FILE}") + message(FATAL_ERROR "Missing include directory list: ${INCLUDE_DIRS_FILE}") +endif() + +string(REPLACE "|" ";" PROTOBUF_INCLUDE_DIRS "${PROTOBUF_INCLUDE_DIRS}") +string(REPLACE "|" ";" GENERIC_INCLUDE_DIRS "${GENERIC_INCLUDE_DIRS}") +file(STRINGS "${INCLUDE_DIRS_FILE}" INCLUDE_DIRS) + +foreach(protobuf_include_dir IN LISTS PROTOBUF_INCLUDE_DIRS) + list(FIND INCLUDE_DIRS "${protobuf_include_dir}" protobuf_index) + if(protobuf_index EQUAL -1) + message(FATAL_ERROR + "Selected Protobuf include directory is missing: ${protobuf_include_dir}") + endif() + foreach(generic_include_dir IN LISTS GENERIC_INCLUDE_DIRS) + if(generic_include_dir STREQUAL protobuf_include_dir) + continue() + endif() + list(FIND INCLUDE_DIRS "${generic_include_dir}" generic_index) + if(NOT generic_index EQUAL -1 AND protobuf_index GREATER generic_index) + message(FATAL_ERROR + "Selected Protobuf include directory must precede generic " + "include directory: ${protobuf_include_dir}") + endif() + endforeach() +endforeach() From 9474b068ecb8d2248b1f0f2398c59538db31f787 Mon Sep 17 00:00:00 2001 From: songgaoye Date: Thu, 22 Oct 2026 23:23:34 +0800 Subject: [PATCH 5/6] cmake: prioritize selected Protobuf headers across targets --- CMakeLists.txt | 33 ++++++++++++++++++++ src/brpc/CMakeLists.txt | 4 ++- src/butil/CMakeLists.txt | 9 +----- test/CMakeLists.txt | 17 +++------- test/check_protobuf_include_order.cmake | 41 ------------------------- 5 files changed, 41 insertions(+), 63 deletions(-) delete mode 100644 test/check_protobuf_include_order.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 59acdfe3bc..7190c63f8a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -150,6 +150,39 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") endif() find_package(Protobuf REQUIRED) + +# A dependency found in a generic prefix (for example /opt/homebrew/include) +# can expose google/protobuf headers from a different installation than the +# selected Protobuf package. Keep the selected package ahead of such prefixes +# on every configuration target that compiles BRPC sources. +function(brpc_assert_protobuf_include_order target) + get_target_property(target_include_dirs ${target} + INTERFACE_INCLUDE_DIRECTORIES) + if(NOT target_include_dirs) + message(FATAL_ERROR "${target} has no interface include directories") + endif() + + foreach(protobuf_include_dir IN LISTS Protobuf_INCLUDE_DIRS) + list(FIND target_include_dirs "${protobuf_include_dir}" protobuf_index) + if(protobuf_index EQUAL -1) + message(FATAL_ERROR + "${target} is missing selected Protobuf include directory: " + "${protobuf_include_dir}") + endif() + foreach(generic_include_dir IN LISTS ARGN) + if(generic_include_dir STREQUAL protobuf_include_dir) + continue() + endif() + list(FIND target_include_dirs "${generic_include_dir}" generic_index) + if(NOT generic_index EQUAL -1 AND protobuf_index GREATER generic_index) + message(FATAL_ERROR + "${target} must search selected Protobuf headers before: " + "${generic_include_dir}") + endif() + endforeach() + endforeach() +endfunction() + foreach(protobuf_include_dir IN LISTS Protobuf_INCLUDE_DIRS) list(APPEND PROTOC_FLAGS -I${protobuf_include_dir}) endforeach() diff --git a/src/brpc/CMakeLists.txt b/src/brpc/CMakeLists.txt index 4efd059bfd..b3ff607d7e 100644 --- a/src/brpc/CMakeLists.txt +++ b/src/brpc/CMakeLists.txt @@ -208,9 +208,11 @@ if(WITH_URMA AND URMA_USE_MOCK_ENABLED) target_compile_features(brpc_build_config INTERFACE cxx_std_17) endif() target_include_directories(brpc_build_config BEFORE INTERFACE - ${CMAKE_CURRENT_BINARY_DIR}) + ${CMAKE_CURRENT_BINARY_DIR} + ${Protobuf_INCLUDE_DIRS}) target_compile_definitions(brpc_build_config INTERFACE ${BRPC_DEFINITIONS}) target_include_directories(brpc_build_config INTERFACE ${BRPC_INCLUDE_DIRS}) +brpc_assert_protobuf_include_order(brpc_build_config ${BRPC_INCLUDE_DIRS}) function(check_thrift_version target_arg) #use thrift command to get version diff --git a/src/butil/CMakeLists.txt b/src/butil/CMakeLists.txt index 5b7c3c85a9..0ddfd216d5 100644 --- a/src/butil/CMakeLists.txt +++ b/src/butil/CMakeLists.txt @@ -90,14 +90,7 @@ target_include_directories(butil_build_config BEFORE INTERFACE ${butil_generated_include_dir} ${Protobuf_INCLUDE_DIRS}) target_include_directories(butil_build_config INTERFACE ${BUTIL_INCLUDE_DIRS}) - -get_target_property(butil_build_config_include_dirs - butil_build_config INTERFACE_INCLUDE_DIRECTORIES) -string(REPLACE ";" "\n" butil_build_config_include_dirs - "${butil_build_config_include_dirs}") -file(GENERATE - OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/butil_build_config_include_dirs.txt" - CONTENT "${butil_build_config_include_dirs}\n") +brpc_assert_protobuf_include_order(butil_build_config ${BUTIL_INCLUDE_DIRS}) set(butil_crc32c_options) if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a2c870469a..8ac0e71aad 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -23,7 +23,9 @@ find_package(Gperftools) add_library(brpc_test_config INTERFACE) target_link_libraries(brpc_test_config INTERFACE brpc_build_config) -target_include_directories(brpc_test_config INTERFACE ${CMAKE_CURRENT_BINARY_DIR}) +target_include_directories(brpc_test_config BEFORE INTERFACE + ${CMAKE_CURRENT_BINARY_DIR} + ${Protobuf_INCLUDE_DIRS}) target_compile_definitions(brpc_test_config INTERFACE UNIT_TEST BVAR_NOT_LINK_DEFAULT_VARIABLES @@ -32,6 +34,7 @@ target_compile_options(brpc_test_config INTERFACE -fno-access-control) if(GPERFTOOLS_INCLUDE_DIR) target_include_directories(brpc_test_config INTERFACE ${GPERFTOOLS_INCLUDE_DIR}) endif() +brpc_assert_protobuf_include_order(brpc_test_config ${GPERFTOOLS_INCLUDE_DIR}) include(CompileProto) set(TEST_PROTO_FILES addressbook.proto @@ -128,7 +131,6 @@ SET(TEST_BUTIL_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/non_thread_safe_unittest.cc ${CMAKE_CURRENT_SOURCE_DIR}/nullable_string16_unittest.cc ${CMAKE_CURRENT_SOURCE_DIR}/object_pool_unittest.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/resource_pool_unittest.cpp ${CMAKE_CURRENT_SOURCE_DIR}/observer_list_unittest.cc ${CMAKE_CURRENT_SOURCE_DIR}/optional_unittest.cpp ${CMAKE_CURRENT_SOURCE_DIR}/platform_thread_unittest.cc @@ -254,17 +256,6 @@ target_link_libraries(test_butil PRIVATE add_test(NAME test_butil COMMAND test_butil) -string(REPLACE ";" "|" protobuf_include_dirs_for_test - "${Protobuf_INCLUDE_DIRS}") -string(REPLACE ";" "|" gflags_include_dirs_for_test - "${GFLAGS_INCLUDE_PATH}") -add_test(NAME cmake_protobuf_include_order - COMMAND ${CMAKE_COMMAND} - "-DINCLUDE_DIRS_FILE=${PROJECT_BINARY_DIR}/src/butil/butil_build_config_include_dirs.txt" - "-DPROTOBUF_INCLUDE_DIRS=${protobuf_include_dirs_for_test}" - "-DGENERIC_INCLUDE_DIRS=${gflags_include_dirs_for_test}" - -P ${CMAKE_CURRENT_SOURCE_DIR}/check_protobuf_include_order.cmake) - # test_bvar # -DBVAR_NOT_LINK_DEFAULT_VARIABLES not work for gcc >= 5.0, just remove the file to prevent linking into unit tests get_filename_component(bvar_default_variables diff --git a/test/check_protobuf_include_order.cmake b/test/check_protobuf_include_order.cmake deleted file mode 100644 index 42121afe92..0000000000 --- a/test/check_protobuf_include_order.cmake +++ /dev/null @@ -1,41 +0,0 @@ -# Licensed to the Apache Software Foundation (ASF) under one or more -# contributor license agreements. See the NOTICE file distributed with -# this work for additional information regarding copyright ownership. -# The ASF licenses this file to You under the Apache License, Version 2.0 -# (the "License"); you may not use this file except in compliance with -# the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -if(NOT EXISTS "${INCLUDE_DIRS_FILE}") - message(FATAL_ERROR "Missing include directory list: ${INCLUDE_DIRS_FILE}") -endif() - -string(REPLACE "|" ";" PROTOBUF_INCLUDE_DIRS "${PROTOBUF_INCLUDE_DIRS}") -string(REPLACE "|" ";" GENERIC_INCLUDE_DIRS "${GENERIC_INCLUDE_DIRS}") -file(STRINGS "${INCLUDE_DIRS_FILE}" INCLUDE_DIRS) - -foreach(protobuf_include_dir IN LISTS PROTOBUF_INCLUDE_DIRS) - list(FIND INCLUDE_DIRS "${protobuf_include_dir}" protobuf_index) - if(protobuf_index EQUAL -1) - message(FATAL_ERROR - "Selected Protobuf include directory is missing: ${protobuf_include_dir}") - endif() - foreach(generic_include_dir IN LISTS GENERIC_INCLUDE_DIRS) - if(generic_include_dir STREQUAL protobuf_include_dir) - continue() - endif() - list(FIND INCLUDE_DIRS "${generic_include_dir}" generic_index) - if(NOT generic_index EQUAL -1 AND protobuf_index GREATER generic_index) - message(FATAL_ERROR - "Selected Protobuf include directory must precede generic " - "include directory: ${protobuf_include_dir}") - endif() - endforeach() -endforeach() From 675a85196f54e827343cdbf8de487656a167fad8 Mon Sep 17 00:00:00 2001 From: songgaoye Date: Sun, 25 Oct 2026 20:11:07 +0800 Subject: [PATCH 6/6] cmake: test Protobuf include precedence with conflicting headers --- CMakeLists.txt | 33 ------------------- src/brpc/CMakeLists.txt | 1 - src/butil/CMakeLists.txt | 1 - test/CMakeLists.txt | 14 +++++++- .../google/protobuf/stubs/common.h | 3 ++ .../protobuf_include_order.cpp | 24 ++++++++++++++ 6 files changed, 40 insertions(+), 36 deletions(-) create mode 100644 test/protobuf_include_order/generic_include/google/protobuf/stubs/common.h create mode 100644 test/protobuf_include_order/protobuf_include_order.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 7190c63f8a..59acdfe3bc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -150,39 +150,6 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") endif() find_package(Protobuf REQUIRED) - -# A dependency found in a generic prefix (for example /opt/homebrew/include) -# can expose google/protobuf headers from a different installation than the -# selected Protobuf package. Keep the selected package ahead of such prefixes -# on every configuration target that compiles BRPC sources. -function(brpc_assert_protobuf_include_order target) - get_target_property(target_include_dirs ${target} - INTERFACE_INCLUDE_DIRECTORIES) - if(NOT target_include_dirs) - message(FATAL_ERROR "${target} has no interface include directories") - endif() - - foreach(protobuf_include_dir IN LISTS Protobuf_INCLUDE_DIRS) - list(FIND target_include_dirs "${protobuf_include_dir}" protobuf_index) - if(protobuf_index EQUAL -1) - message(FATAL_ERROR - "${target} is missing selected Protobuf include directory: " - "${protobuf_include_dir}") - endif() - foreach(generic_include_dir IN LISTS ARGN) - if(generic_include_dir STREQUAL protobuf_include_dir) - continue() - endif() - list(FIND target_include_dirs "${generic_include_dir}" generic_index) - if(NOT generic_index EQUAL -1 AND protobuf_index GREATER generic_index) - message(FATAL_ERROR - "${target} must search selected Protobuf headers before: " - "${generic_include_dir}") - endif() - endforeach() - endforeach() -endfunction() - foreach(protobuf_include_dir IN LISTS Protobuf_INCLUDE_DIRS) list(APPEND PROTOC_FLAGS -I${protobuf_include_dir}) endforeach() diff --git a/src/brpc/CMakeLists.txt b/src/brpc/CMakeLists.txt index b3ff607d7e..3f1ed29960 100644 --- a/src/brpc/CMakeLists.txt +++ b/src/brpc/CMakeLists.txt @@ -212,7 +212,6 @@ target_include_directories(brpc_build_config BEFORE INTERFACE ${Protobuf_INCLUDE_DIRS}) target_compile_definitions(brpc_build_config INTERFACE ${BRPC_DEFINITIONS}) target_include_directories(brpc_build_config INTERFACE ${BRPC_INCLUDE_DIRS}) -brpc_assert_protobuf_include_order(brpc_build_config ${BRPC_INCLUDE_DIRS}) function(check_thrift_version target_arg) #use thrift command to get version diff --git a/src/butil/CMakeLists.txt b/src/butil/CMakeLists.txt index 0ddfd216d5..eb19c0d008 100644 --- a/src/butil/CMakeLists.txt +++ b/src/butil/CMakeLists.txt @@ -90,7 +90,6 @@ target_include_directories(butil_build_config BEFORE INTERFACE ${butil_generated_include_dir} ${Protobuf_INCLUDE_DIRS}) target_include_directories(butil_build_config INTERFACE ${BUTIL_INCLUDE_DIRS}) -brpc_assert_protobuf_include_order(butil_build_config ${BUTIL_INCLUDE_DIRS}) set(butil_crc32c_options) if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 8ac0e71aad..c3334965fd 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -34,7 +34,6 @@ target_compile_options(brpc_test_config INTERFACE -fno-access-control) if(GPERFTOOLS_INCLUDE_DIR) target_include_directories(brpc_test_config INTERFACE ${GPERFTOOLS_INCLUDE_DIR}) endif() -brpc_assert_protobuf_include_order(brpc_test_config ${GPERFTOOLS_INCLUDE_DIR}) include(CompileProto) set(TEST_PROTO_FILES addressbook.proto @@ -256,6 +255,19 @@ target_link_libraries(test_butil PRIVATE add_test(NAME test_butil COMMAND test_butil) +# The generic dependency intentionally contains an incompatible header at the +# same path as a Protobuf header. Compilation verifies that the include order +# seen by the compiler selects the Protobuf package found for this build. +add_library(protobuf_include_order_generic INTERFACE) +target_include_directories(protobuf_include_order_generic INTERFACE + ${CMAKE_CURRENT_SOURCE_DIR}/protobuf_include_order/generic_include) +add_executable(cmake_protobuf_include_order + ${CMAKE_CURRENT_SOURCE_DIR}/protobuf_include_order/protobuf_include_order.cpp) +target_link_libraries(cmake_protobuf_include_order PRIVATE + brpc_test_config + protobuf_include_order_generic) +add_test(NAME cmake_protobuf_include_order COMMAND cmake_protobuf_include_order) + # test_bvar # -DBVAR_NOT_LINK_DEFAULT_VARIABLES not work for gcc >= 5.0, just remove the file to prevent linking into unit tests get_filename_component(bvar_default_variables diff --git a/test/protobuf_include_order/generic_include/google/protobuf/stubs/common.h b/test/protobuf_include_order/generic_include/google/protobuf/stubs/common.h new file mode 100644 index 0000000000..a4ca30df6a --- /dev/null +++ b/test/protobuf_include_order/generic_include/google/protobuf/stubs/common.h @@ -0,0 +1,3 @@ +// This deliberately conflicts with the selected Protobuf installation. The +// compile probe must never see this header. +#define GOOGLE_PROTOBUF_VERSION 1 diff --git a/test/protobuf_include_order/protobuf_include_order.cpp b/test/protobuf_include_order/protobuf_include_order.cpp new file mode 100644 index 0000000000..b131db3ed5 --- /dev/null +++ b/test/protobuf_include_order/protobuf_include_order.cpp @@ -0,0 +1,24 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information regarding copyright +// ownership. The ASF licenses this file to you under the Apache License, +// Version 2.0 (the "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. + +#include + +#if GOOGLE_PROTOBUF_VERSION == 1 +#error "A generic dependency's Protobuf header was selected" +#endif + +int main() { + return 0; +}