Skip to content

Commit 4be802d

Browse files
authored
Link privately to protobuf static libraries (onnx#6881)
### Description Link privately to protobuf static builds so that it doesn't need to find protobuf. Signed-off-by: cyy <cyyever@outlook.com>
1 parent 58ddc8d commit 4be802d

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

CMakeLists.txt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ if(NOT ONNX_PROTOC_EXECUTABLE)
260260
set(ABSL_PROPAGATE_CXX_STD 1)
261261
set(ONNX_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS})
262262
set(ONNX_CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
263-
# Use this setting to build thirdparty libs.
263+
# Use this setting to build third-party libs.
264264
set(BUILD_SHARED_LIBS ${ONNX_USE_PROTOBUF_SHARED_LIBS})
265265
set(ProtobufURL https://github.com/protocolbuffers/protobuf/releases/download/v30.2/protobuf-30.2.tar.gz)
266266
set(ProtobufSHA1 f846d1f047255d09142361ba6d1985336db5e407)
@@ -474,7 +474,14 @@ if(ONNX_USE_LITE_PROTO)
474474
set(LINKED_PROTOBUF_TARGET protobuf::libprotobuf-lite)
475475
endif()
476476
endif()
477-
target_link_libraries(onnx_proto PUBLIC ${LINKED_PROTOBUF_TARGET})
477+
if((NOT ONNX_USE_PROTOBUF_SHARED_LIBS) AND Build_Protobuf)
478+
target_link_libraries(onnx_proto PRIVATE ${LINKED_PROTOBUF_TARGET})
479+
target_include_directories(onnx_proto PUBLIC
480+
$<BUILD_INTERFACE:$<TARGET_PROPERTY:${LINKED_PROTOBUF_TARGET},INCLUDE_DIRECTORIES>>
481+
)
482+
else()
483+
target_link_libraries(onnx_proto PUBLIC ${LINKED_PROTOBUF_TARGET})
484+
endif()
478485
foreach(ABSL_USED_TARGET IN LISTS protobuf_ABSL_USED_TARGETS)
479486
if(TARGET ${ABSL_USED_TARGET})
480487
target_link_libraries(onnx_proto PRIVATE ${ABSL_USED_TARGET})

cmake/ONNXConfig.cmake.in

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
# library version information
77
set(ONNX_VERSION "@ONNX_VERSION@")
88

9-
find_package(Protobuf REQUIRED CONFIG)
9+
if((NOT @@ONNX_USE_PROTOBUF_SHARED_LIBS@@) AND @@Build_Protobuf@@)
10+
find_package(Protobuf REQUIRED CONFIG)
11+
endif()
1012

1113
# import targets
1214
include ("${CMAKE_CURRENT_LIST_DIR}/ONNXTargets.cmake")

0 commit comments

Comments
 (0)