Skip to content

Commit 1c2db61

Browse files
rwgkcopybara-github
authored andcommitted
Import #73
Manual import. The Google-internal repo is the source of truth for pybind11_protobuf. Sorry we didn't get to automating imports from GitHub PRs. PiperOrigin-RevId: 512739514
1 parent 80f3440 commit 1c2db61

File tree

4 files changed

+306
-0
lines changed

4 files changed

+306
-0
lines changed

CMakeLists.txt

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
cmake_minimum_required(VERSION 3.18)
2+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
3+
4+
project(pybind11_protobuf )
5+
6+
if(MSVC)
7+
set(CMAKE_CXX_STANDARD 20)
8+
else()
9+
set(CMAKE_CXX_STANDARD 17)
10+
endif()
11+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
12+
set(CMAKE_CXX_EXTENSIONS OFF)
13+
14+
15+
#============================================================================
16+
# Warnings
17+
18+
message(WARNING "CI testing for pybind11_protobuf cmake is currently MISSING! (https://github.com/pybind/pybind11_protobuf/issues/104)")
19+
20+
#============================================================================
21+
# Options
22+
23+
option(BUILD_absl "Build the abseil-cpp dependency Library" ON)
24+
message(STATUS "Build abseil-cpp: ${BUILD_absl}")
25+
26+
option(BUILD_protobuf "Build the Protobuf dependency Library" ON)
27+
message(STATUS "Build protobuf: ${BUILD_protobuf}")
28+
29+
option(BUILD_pybind11 "Build the pybind11 dependency Library" ON)
30+
message(STATUS "Python: Build pybind11: ${BUILD_pybind11}")
31+
32+
#============================================================================
33+
# Find Python
34+
35+
find_package(Python COMPONENTS Interpreter Development)
36+
37+
#============================================================================
38+
# Build dependencies
39+
40+
add_subdirectory(cmake/dependencies dependencies)
41+
42+
include(deps)
43+
44+
include(CMakeFindDependencyMacro)
45+
include(FindProtobuf)
46+
if (NOT protobuf_FOUND AND NOT PROTOBUF_FOUND AND NOT TARGET protobuf::libprotobuf)
47+
if (BUILD_protobuf)
48+
find_dependency(protobuf CONFIG REQUIRED)
49+
endif()
50+
endif()
51+
52+
#============================================================================
53+
# Debug messages for find_package
54+
55+
# message("pybind11_FOUND: ${pybind11_FOUND}")
56+
# message("pybind11_VERSION: ${pybind11_VERSION}")
57+
# message("pybind11_VERSION_TYPE: ${pybind11_VERSION_TYPE}")
58+
# message("pybind11_INCLUDE_DIRS: ${pybind11_INCLUDE_DIRS}")
59+
# message("pybind11_INCLUDE_DIR: ${pybind11_INCLUDE_DIR}")
60+
# message("pybind11_DEFINITIONS: ${pybind11_DEFINITIONS}")
61+
# message("pybind11_LIBRARIES: ${pybind11_LIBRARIES}")
62+
63+
# message("Python_FOUND: ${Python_FOUND}")
64+
# message("Python_VERSION: ${Python_VERSION}")
65+
# message("Python_INCLUDE_DIRS: ${Python_INCLUDE_DIRS}")
66+
# message("Python_LIBRARIES: ${Python_LIBRARIES}")
67+
68+
# message("protobuf_FOUND: ${protobuf_FOUND}")
69+
# message("protobuf_VERSION: ${protobuf_VERSION}")
70+
# message("protobuf_INCLUDE_DIRS: ${protobuf_INCLUDE_DIRS}")
71+
# message("protobuf_SOURCE_DIR: ${protobuf_SOURCE_DIR}")
72+
# message("protobuf_LIBRARIES: ${protobuf_LIBRARIES}")
73+
74+
#============================================================================
75+
# pybind11_proto_utils pybind11 extension module
76+
pybind11_add_module(pybind11_proto_utils MODULE
77+
pybind11_protobuf/proto_utils.cc
78+
pybind11_protobuf/proto_utils.h
79+
)
80+
81+
target_link_libraries(pybind11_proto_utils
82+
PRIVATE
83+
absl::strings
84+
protobuf::libprotobuf
85+
${Python_LIBRARIES}
86+
)
87+
88+
target_include_directories(pybind11_proto_utils
89+
PRIVATE
90+
${PROJECT_SOURCE_DIR}
91+
${protobuf_INCLUDE_DIRS}
92+
${protobuf_SOURCE_DIR}
93+
${pybind11_INCLUDE_DIRS}
94+
)
95+
96+
#============================================================================
97+
# pybind11_native_proto_caster shared library
98+
add_library(pybind11_native_proto_caster SHARED
99+
# bazel: pybind_library: native_proto_caster
100+
pybind11_protobuf/native_proto_caster.h
101+
102+
# bazel: pybind_library: enum_type_caster
103+
pybind11_protobuf/enum_type_caster.h
104+
105+
# bazel: pybind_library: proto_cast_util
106+
pybind11_protobuf/proto_cast_util.cc
107+
pybind11_protobuf/proto_cast_util.h
108+
pybind11_protobuf/proto_caster_impl.h
109+
110+
# bazel: cc_library::check_unknown_fields
111+
pybind11_protobuf/check_unknown_fields.cc
112+
pybind11_protobuf/check_unknown_fields.h
113+
)
114+
115+
target_link_libraries(pybind11_native_proto_caster
116+
absl::flat_hash_map
117+
absl::flat_hash_set
118+
absl::hash
119+
absl::strings
120+
absl::optional
121+
protobuf::libprotobuf
122+
pybind11::pybind11
123+
${Python_LIBRARIES}
124+
)
125+
126+
target_include_directories(pybind11_native_proto_caster
127+
PRIVATE
128+
${PROJECT_SOURCE_DIR}
129+
${protobuf_INCLUDE_DIRS}
130+
${protobuf_SOURCE_DIR}
131+
${pybind11_INCLUDE_DIRS}
132+
)
133+
134+
#============================================================================
135+
# pybind11_wrapped_proto_caster shared library
136+
add_library(pybind11_wrapped_proto_caster SHARED
137+
# bazel: pybind_library: wrapped_proto_caster
138+
pybind11_protobuf/wrapped_proto_caster.h
139+
140+
# bazel: pybind_library: proto_cast_util
141+
pybind11_protobuf/proto_cast_util.cc
142+
pybind11_protobuf/proto_cast_util.h
143+
pybind11_protobuf/proto_caster_impl.h
144+
145+
# bazel: cc_library: check_unknown_fields
146+
pybind11_protobuf/check_unknown_fields.cc
147+
pybind11_protobuf/check_unknown_fields.h
148+
)
149+
150+
target_link_libraries(pybind11_wrapped_proto_caster
151+
absl::flat_hash_map
152+
absl::flat_hash_set
153+
absl::hash
154+
absl::strings
155+
absl::optional
156+
protobuf::libprotobuf
157+
pybind11::pybind11
158+
${Python_LIBRARIES}
159+
)
160+
161+
target_include_directories(pybind11_wrapped_proto_caster
162+
PRIVATE
163+
${PROJECT_SOURCE_DIR}
164+
${protobuf_INCLUDE_DIRS}
165+
${protobuf_SOURCE_DIR}
166+
${pybind11_INCLUDE_DIRS}
167+
)
168+
169+
# TODO set defines
170+
# PYBIND11_PROTOBUF_ENABLE_PYPROTO_API
171+
# see: bazel: pybind_library: proto_cast_util
172+
173+
# bazel equivs. checklist
174+
#
175+
# bazel: pybind_library: enum_type_caster
176+
# - enum_type_caster.h
177+
#
178+
# bazel: pybind_library: native_proto_caster
179+
# - native_proto_caster.h
180+
#
181+
# check_unknown_fields
182+
# enum_type_caster
183+
# proto_cast_util
184+
#
185+
# bazel: pybind_library: proto_cast_util
186+
# - proto_cast_util.cc
187+
# - proto_cast_util.h
188+
# - proto_caster_impl.h
189+
#
190+
# check_unknown_fields
191+
#
192+
# bazel: pybind_library: wrapped_proto_caster
193+
# - wrapped_proto_caster.h
194+
#
195+
# proto_cast_util
196+
#

cmake/dependencies/CMakeLists.txt

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
include(FetchContent)
2+
3+
#============================================================================
4+
# Declare all dependencies first
5+
6+
if(BUILD_absl)
7+
FetchContent_Declare(
8+
absl
9+
GIT_REPOSITORY "https://github.com/abseil/abseil-cpp.git"
10+
GIT_TAG "20220623.1"
11+
)
12+
endif()
13+
14+
# https://stackoverflow.com/questions/63309544/cmake-protobuf-external-to-application-code
15+
# https://cmake.org/cmake/help/latest/policy/CMP0077.html
16+
# https://gitlab.kitware.com/cmake/cmake/-/merge_requests/7565/diffs
17+
if(BUILD_protobuf)
18+
set(protobuf_BUILD_TESTS OFF CACHE INTERNAL "")
19+
FetchContent_Declare(
20+
protobuf
21+
GIT_REPOSITORY "https://github.com/protocolbuffers/protobuf.git"
22+
GIT_TAG "v21.5"
23+
GIT_SUBMODULES ""
24+
)
25+
endif()
26+
27+
if(BUILD_pybind11)
28+
set(PYBIND11_TEST OFF)
29+
FetchContent_Declare(
30+
pybind11
31+
GIT_REPOSITORY "https://github.com/pybind/pybind11.git"
32+
GIT_TAG "v2.10.1"
33+
)
34+
endif()
35+
36+
#============================================================================
37+
# Make dependencies avaialble
38+
39+
if(BUILD_absl)
40+
message(CHECK_START "Fetching Abseil-cpp")
41+
list(APPEND CMAKE_MESSAGE_INDENT " ")
42+
FetchContent_MakeAvailable(absl)
43+
list(POP_BACK CMAKE_MESSAGE_INDENT)
44+
message(CHECK_PASS "fetched")
45+
endif()
46+
47+
if(BUILD_protobuf)
48+
message(CHECK_START "Fetching protobuf")
49+
list(APPEND CMAKE_MESSAGE_INDENT " ")
50+
FetchContent_MakeAvailable(protobuf)
51+
list(POP_BACK CMAKE_MESSAGE_INDENT)
52+
message(CHECK_PASS "fetched")
53+
endif()
54+
55+
if(BUILD_pybind11)
56+
message(CHECK_START "Fetching pybind11")
57+
list(APPEND CMAKE_MESSAGE_INDENT " ")
58+
FetchContent_MakeAvailable(pybind11)
59+
list(POP_BACK CMAKE_MESSAGE_INDENT)
60+
message(CHECK_PASS "fetched")
61+
endif()
62+

cmake/deps.cmake

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
if(NOT BUILD_absl)
2+
find_package(absl REQUIRED)
3+
endif()
4+
5+
if(NOT BUILD_protobuf)
6+
find_package(protobuf REQUIRED)
7+
endif()
8+
9+
if(NOT BUILD_pybind11)
10+
find_package(pybind11 REQUIRED)
11+
endif()
12+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#============================================================================
2+
# tests
3+
#============================================================================
4+
5+
#============================================================================
6+
# generate protobuf bindings
7+
8+
set(Protobuf_IMPORT_DIRS
9+
"${CMAKE_CURRENT_SOURCE_DIR}/../../..;${Protobuf_IMPORT_DIRS}"
10+
)
11+
12+
protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS
13+
# extension.proto
14+
test.proto
15+
)
16+
17+
protobuf_generate_python(PROTO_PYS
18+
# extension.proto
19+
test.proto
20+
)
21+
22+
#============================================================================
23+
# adding a custom target forces CMake to generate the bindings
24+
25+
add_custom_target(pybind11_test_proto_cpp ALL DEPENDS ${PROTO_SRCS} ${PROTO_HDRS})
26+
add_custom_target(pybind11_test_proto_python ALL DEPENDS ${PROTO_PYS})
27+
28+
#============================================================================
29+
# generate protobuf library
30+
31+
add_library(pybind11_test_proto ${PROTO_SRCS} ${PROTO_HDR})
32+
33+
target_include_directories(pybind11_test_proto
34+
PUBLIC
35+
${Protobuf_INCLUDE_DIR}
36+
)

0 commit comments

Comments
 (0)