Skip to content

Commit 8a1110e

Browse files
committed
cmake: Fix pybind11_protobuf integration
1 parent 3d0d015 commit 8a1110e

File tree

2 files changed

+108
-5
lines changed

2 files changed

+108
-5
lines changed

cmake/check_deps.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ endif()
9999

100100
# Check language Dependencies
101101
if(BUILD_PYTHON)
102+
if(NOT TARGET pybind11::pybind11_headers)
103+
message(FATAL_ERROR "Target pybind11::pybind11_headers not available.")
104+
endif()
105+
102106
if(NOT TARGET pybind11_abseil::absl_casters)
103107
message(FATAL_ERROR "Target pybind11_abseil::absl_casters not available.")
104108
endif()

patches/pybind11_protobuf.patch

Lines changed: 104 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
diff --git a/CMakeLists.txt b/CMakeLists.txt
2-
index 2139dc0..1942ad0 100644
2+
index 2139dc0..df3f30a 100644
33
--- a/CMakeLists.txt
44
+++ b/CMakeLists.txt
5-
@@ -27,7 +27,7 @@ include(CTest)
5+
@@ -27,58 +27,64 @@ include(CTest)
66
# ============================================================================
77
# Find Python
88

@@ -11,7 +11,106 @@ index 2139dc0..1942ad0 100644
1111

1212
# ============================================================================
1313
# Build dependencies
14-
@@ -87,8 +87,10 @@ pybind11_add_module(
14+
+include(FetchContent)
15+
16+
-if(USE_SYSTEM_ABSEIL)
17+
- # Version omitted, as absl only allows EXACT version matches
18+
- set(_absl_package_args REQUIRED)
19+
-else()
20+
- set(_absl_package_args 20230125)
21+
-endif()
22+
-if(USE_SYSTEM_PROTOBUF)
23+
- set(_protobuf_package_args 4.23.3 REQUIRED)
24+
-else()
25+
- set(_protobuf_package_args 4.23.3)
26+
-endif()
27+
-if(USE_SYSTEM_PYBIND)
28+
- set(_pybind11_package_args 2.11.1 REQUIRED)
29+
-else()
30+
- set(_pybind11_package_args 2.11.1)
31+
+message(CHECK_START "Checking for external dependencies")
32+
+list(APPEND CMAKE_MESSAGE_INDENT " ")
33+
+
34+
+if(NOT TARGET absl::base)
35+
+ if(USE_SYSTEM_ABSEIL)
36+
+ # Version omitted, as absl only allows EXACT version matches
37+
+ set(_absl_package_args REQUIRED)
38+
+ else()
39+
+ set(_absl_package_args 20230125)
40+
+ endif()
41+
+ FetchContent_Declare(
42+
+ absl
43+
+ GIT_REPOSITORY "https://github.com/abseil/abseil-cpp.git"
44+
+ GIT_TAG 20230125.3
45+
+ FIND_PACKAGE_ARGS ${_absl_package_args} NAMES absl)
46+
+ set(ABSL_PROPAGATE_CXX_STD ON)
47+
+ set(ABSL_ENABLE_INSTALL ON)
48+
+ FetchContent_MakeAvailable(absl)
49+
endif()
50+
51+
-set(ABSL_PROPAGATE_CXX_STD ON)
52+
-set(ABSL_ENABLE_INSTALL ON)
53+
+if(NOT TARGET protobuf::libprotobuf)
54+
+ if(USE_SYSTEM_PROTOBUF)
55+
+ set(_protobuf_package_args 4.23.3 REQUIRED)
56+
+ else()
57+
+ set(_protobuf_package_args 4.23.3)
58+
+ endif()
59+
+ FetchContent_Declare(
60+
+ Protobuf
61+
+ GIT_REPOSITORY "https://github.com/protocolbuffers/protobuf.git"
62+
+ GIT_TAG v23.3
63+
+ GIT_SUBMODULES ""
64+
+ FIND_PACKAGE_ARGS ${_protobuf_package_args} NAMES protobuf)
65+
+ set(protobuf_BUILD_TESTS OFF CACHE INTERNAL "")
66+
+ FetchContent_MakeAvailable(Protobuf)
67+
+endif()
68+
69+
-include(FetchContent)
70+
-FetchContent_Declare(
71+
- absl
72+
- GIT_REPOSITORY "https://github.com/abseil/abseil-cpp.git"
73+
- GIT_TAG 20230125.3
74+
- FIND_PACKAGE_ARGS ${_absl_package_args} NAMES absl)
75+
-
76+
-# cmake-format: off
77+
-FetchContent_Declare(
78+
- Protobuf
79+
- GIT_REPOSITORY "https://github.com/protocolbuffers/protobuf.git"
80+
- GIT_TAG v23.3
81+
- GIT_SUBMODULES ""
82+
- FIND_PACKAGE_ARGS ${_protobuf_package_args} NAMES protobuf)
83+
-set(protobuf_BUILD_TESTS OFF CACHE INTERNAL "")
84+
-# cmake-format: on
85+
-
86+
-FetchContent_Declare(
87+
- pybind11
88+
- GIT_REPOSITORY "https://github.com/pybind/pybind11.git"
89+
- GIT_TAG v2.11.1
90+
- FIND_PACKAGE_ARGS ${_pybind11_package_args} NAMES pybind11)
91+
+if(NOT TARGET pybind11::pybind11_headers)
92+
+ if(USE_SYSTEM_PYBIND)
93+
+ set(_pybind11_package_args 2.11.1 REQUIRED)
94+
+ else()
95+
+ set(_pybind11_package_args 2.11.1)
96+
+ endif()
97+
+ FetchContent_Declare(
98+
+ pybind11
99+
+ GIT_REPOSITORY "https://github.com/pybind/pybind11.git"
100+
+ GIT_TAG v2.11.1
101+
+ FIND_PACKAGE_ARGS ${_pybind11_package_args} NAMES pybind11)
102+
+ FetchContent_MakeAvailable(pybind11)
103+
+endif()
104+
105+
-message(CHECK_START "Checking for external dependencies")
106+
-list(APPEND CMAKE_MESSAGE_INDENT " ")
107+
-FetchContent_MakeAvailable(absl Protobuf pybind11)
108+
list(POP_BACK CMAKE_MESSAGE_INDENT)
109+
+message(CHECK_PASS "found")
110+
111+
# ============================================================================
112+
# pybind11_proto_utils pybind11 extension module
113+
@@ -87,8 +93,10 @@ pybind11_add_module(
15114
pybind11_protobuf/proto_utils.h)
16115

17116
target_link_libraries(
@@ -24,7 +123,7 @@ index 2139dc0..1942ad0 100644
24123

25124
target_include_directories(
26125
pybind11_proto_utils PRIVATE ${PROJECT_SOURCE_DIR} ${protobuf_INCLUDE_DIRS}
27-
@@ -116,10 +118,11 @@ target_link_libraries(
126+
@@ -116,10 +124,11 @@ target_link_libraries(
28127
absl::optional
29128
protobuf::libprotobuf
30129
pybind11::pybind11
@@ -37,7 +136,7 @@ index 2139dc0..1942ad0 100644
37136
PRIVATE ${PROJECT_SOURCE_DIR} ${protobuf_INCLUDE_DIRS} ${protobuf_SOURCE_DIR}
38137
${pybind11_INCLUDE_DIRS})
39138

40-
@@ -143,7 +146,7 @@ target_link_libraries(
139+
@@ -143,7 +152,7 @@ target_link_libraries(
41140
absl::optional
42141
protobuf::libprotobuf
43142
pybind11::pybind11

0 commit comments

Comments
 (0)