Skip to content

Commit eea7c8e

Browse files
committed
Add CMake build
- Add CMakeLists.txt to build the native and wrapped proto caster libraries - Use FetchContent to retrieve dependencies. - Base dependencies on version from google/or-tools. - Declare all fetch content dependencies before calling make available . - Use CACHE INTERNAL to disable protobuf tests. - Set dependency versions to match those in google/or-tools. Signed-off-by: Rhys Mainwaring <[email protected]>
1 parent 80f3440 commit eea7c8e

File tree

4 files changed

+300
-0
lines changed

4 files changed

+300
-0
lines changed

CMakeLists.txt

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

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)