Skip to content

Commit 84653a5

Browse files
StefanBruenscopybara-github
authored andcommitted
Import #162:
* Add the missing definitions for building the tests when using the CMake build. Actually running the tests is left for another PR (see #162 for difficulties encountered): * #162 (comment) 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: 647802705
1 parent e90f33e commit 84653a5

File tree

4 files changed

+154
-95
lines changed

4 files changed

+154
-95
lines changed

CMakeLists.txt

Lines changed: 59 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@ set(CMAKE_POSITION_INDEPENDENT_CODE ON)
1616
# ============================================================================
1717
# Options
1818

19-
option(BUILD_TESTS "Build tests." OFF)
19+
option(USE_SYSTEM_ABSEIL "Force usage of system provided abseil-cpp" OFF)
20+
option(USE_SYSTEM_PROTOBUF "Force usage of system provided Protobuf" OFF)
21+
option(USE_SYSTEM_PYBIND "Force usage of system provided pybind11" OFF)
22+
23+
# ============================================================================
24+
# Testing
25+
include(CTest)
2026

2127
# ============================================================================
2228
# Find Python
@@ -26,22 +32,53 @@ find_package(Python COMPONENTS Interpreter Development)
2632
# ============================================================================
2733
# Build dependencies
2834

29-
set(_absl_repository "https://github.com/abseil/abseil-cpp.git")
30-
set(_absl_version 20230125)
31-
set(_absl_tag 20230125.3)
32-
find_package(absl ${_absl_version} QUIET)
33-
34-
set(_protobuf_repository "https://github.com/protocolbuffers/protobuf.git")
35-
set(_protobuf_version 3.23.3)
36-
set(_protobuf_tag v23.3)
37-
find_package(Protobuf ${_protobuf_version} QUIET)
38-
39-
set(_pybind11_repository "https://github.com/pybind/pybind11.git")
40-
set(_pybind11_version 2.11.1)
41-
set(_pybind11_tag v2.11.1)
42-
find_package(pybind11 ${_pybind11_version} QUIET)
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+
if(USE_SYSTEM_PROTOBUF)
42+
set(_protobuf_package_args 4.23.3 REQUIRED)
43+
else()
44+
set(_protobuf_package_args 4.23.3)
45+
endif()
46+
if(USE_SYSTEM_PYBIND)
47+
set(_pybind11_package_args 2.11.1 REQUIRED)
48+
else()
49+
set(_pybind11_package_args 2.11.1)
50+
endif()
4351

44-
add_subdirectory(cmake/dependencies dependencies)
52+
set(ABSL_PROPAGATE_CXX_STD ON)
53+
set(ABSL_ENABLE_INSTALL ON)
54+
55+
include(FetchContent)
56+
FetchContent_Declare(
57+
absl
58+
GIT_REPOSITORY "https://github.com/abseil/abseil-cpp.git"
59+
GIT_TAG 20230125.3
60+
FIND_PACKAGE_ARGS ${_absl_package_args} NAMES absl)
61+
62+
# cmake-format: off
63+
FetchContent_Declare(
64+
Protobuf
65+
GIT_REPOSITORY "https://github.com/protocolbuffers/protobuf.git"
66+
GIT_TAG v23.3
67+
GIT_SUBMODULES ""
68+
FIND_PACKAGE_ARGS ${_protobuf_package_args} NAMES protobuf)
69+
set(protobuf_BUILD_TESTS OFF CACHE INTERNAL "")
70+
# cmake-format: on
71+
72+
FetchContent_Declare(
73+
pybind11
74+
GIT_REPOSITORY "https://github.com/pybind/pybind11.git"
75+
GIT_TAG v2.11.1
76+
FIND_PACKAGE_ARGS ${_pybind11_package_args} NAMES pybind11)
77+
78+
message(CHECK_START "Checking for external dependencies")
79+
list(APPEND CMAKE_MESSAGE_INDENT " ")
80+
FetchContent_MakeAvailable(absl Protobuf pybind11)
81+
list(POP_BACK CMAKE_MESSAGE_INDENT)
4582

4683
# ============================================================================
4784
# pybind11_proto_utils pybind11 extension module
@@ -60,7 +97,7 @@ target_include_directories(
6097
# ============================================================================
6198
# pybind11_native_proto_caster shared library
6299
add_library(
63-
pybind11_native_proto_caster SHARED
100+
pybind11_native_proto_caster STATIC
64101
# bazel: pybind_library: native_proto_caster
65102
pybind11_protobuf/native_proto_caster.h
66103
# bazel: pybind_library: enum_type_caster
@@ -89,7 +126,7 @@ target_include_directories(
89126
# ============================================================================
90127
# pybind11_wrapped_proto_caster shared library
91128
add_library(
92-
pybind11_wrapped_proto_caster SHARED
129+
pybind11_wrapped_proto_caster STATIC
93130
# bazel: pybind_library: wrapped_proto_caster
94131
pybind11_protobuf/wrapped_proto_caster.h
95132
# bazel: pybind_library: proto_cast_util
@@ -113,6 +150,10 @@ target_include_directories(
113150
PRIVATE ${PROJECT_SOURCE_DIR} ${protobuf_INCLUDE_DIRS} ${protobuf_SOURCE_DIR}
114151
${pybind11_INCLUDE_DIRS})
115152

153+
if(BUILD_TESTING)
154+
add_subdirectory(pybind11_protobuf/tests)
155+
endif()
156+
116157
# bazel equivs. checklist
117158
#
118159
# bazel: pybind_library: enum_type_caster - enum_type_caster.h

cmake/dependencies/CMakeLists.txt

Lines changed: 0 additions & 62 deletions
This file was deleted.

pybind11_protobuf/tests/BUILD

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -197,21 +197,6 @@ py_test(
197197
deps = EXTENSION_TEST_DEPS_COMMON + ["@com_google_protobuf//:protobuf_python"],
198198
)
199199

200-
py_test(
201-
name = "extension_disallow_unknown_fields_test",
202-
srcs = ["extension_test.py"],
203-
data = [
204-
":extension_module.so",
205-
":proto_enum_module.so",
206-
],
207-
main = "extension_test.py",
208-
python_version = "PY3",
209-
srcs_version = "PY3",
210-
deps = EXTENSION_TEST_DEPS_COMMON + [
211-
"@com_google_protobuf//:protobuf_python",
212-
],
213-
)
214-
215200
pybind_extension(
216201
name = "message_module",
217202
srcs = ["message_module.cc"],
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#
2+
# Evaluate if Protobuf uses the system package, otherwise explicitly include the
3+
# required macro
4+
#
5+
FetchContent_GetProperties(Protobuf SOURCE_DIR Protobuf_SOURCE_DIR)
6+
if(Protobuf_SOURCE_DIR)
7+
# Use macros from content made available by FetchContent
8+
include(${Protobuf_SOURCE_DIR}/cmake/protobuf-generate.cmake)
9+
endif()
10+
11+
# cmake-format: off
12+
function(generate_cc_proto protoname)
13+
# Generate C++ files (.pb.h, .pb.cc)
14+
#
15+
add_library(${protoname}_cc_proto OBJECT)
16+
target_include_directories(${protoname}_cc_proto
17+
PRIVATE $<TARGET_PROPERTY:protobuf::libprotobuf,INCLUDE_DIRECTORIES>
18+
$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}>)
19+
protobuf_generate(
20+
TARGET ${protoname}_cc_proto
21+
PROTOS ${CMAKE_SOURCE_DIR}/pybind11_protobuf/tests/${protoname}.proto
22+
IMPORT_DIRS ${CMAKE_SOURCE_DIR}
23+
PROTOC_OUT_DIR ${CMAKE_BINARY_DIR})
24+
endfunction()
25+
26+
function(generate_py_proto protoname)
27+
# Generate Python files (_pb2.py)
28+
#
29+
add_custom_target(${protoname}_py_pb2 ALL)
30+
protobuf_generate(
31+
TARGET ${protoname}_py_pb2
32+
LANGUAGE PYTHON
33+
PROTOS ${CMAKE_SOURCE_DIR}/pybind11_protobuf/tests/${protoname}.proto
34+
IMPORT_DIRS ${CMAKE_SOURCE_DIR}
35+
PROTOC_OUT_DIR ${CMAKE_BINARY_DIR})
36+
endfunction()
37+
# cmake-format: on
38+
39+
generate_cc_proto("test")
40+
generate_cc_proto("extension")
41+
generate_cc_proto("extension_nest_repeated")
42+
generate_cc_proto("extension_in_other_file_in_deps")
43+
generate_cc_proto("extension_in_other_file")
44+
generate_cc_proto("we-love-dashes")
45+
46+
generate_py_proto("test")
47+
generate_py_proto("extension")
48+
generate_py_proto("extension_nest_repeated")
49+
generate_py_proto("extension_in_other_file_in_deps")
50+
generate_py_proto("extension_in_other_file")
51+
52+
function(generate_extension modulename deps)
53+
pybind11_add_module(${modulename}_module ${modulename}_module.cc)
54+
add_dependencies(${modulename}_module ${deps})
55+
target_include_directories(${modulename}_module #
56+
PRIVATE ${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR})
57+
target_link_libraries(${modulename}_module #
58+
PRIVATE protobuf::libprotobuf ${deps})
59+
endfunction()
60+
61+
generate_extension(proto_enum "test_cc_proto")
62+
generate_extension(dynamic_message "pybind11_native_proto_caster")
63+
generate_extension(
64+
extension #
65+
"extension_in_other_file_in_deps_cc_proto;extension_nest_repeated_cc_proto;test_cc_proto;extension_cc_proto;pybind11_native_proto_caster"
66+
)
67+
generate_extension(message "test_cc_proto;pybind11_native_proto_caster")
68+
generate_extension(pass_by "test_cc_proto;pybind11_native_proto_caster")
69+
generate_extension(pass_proto2_message "pybind11_native_proto_caster")
70+
generate_extension(wrapped_proto "test_cc_proto;pybind11_wrapped_proto_caster")
71+
generate_extension(thread "test_cc_proto;pybind11_native_proto_caster")
72+
generate_extension(regression_wrappers "pybind11_native_proto_caster")
73+
generate_extension(we_love_dashes_cc_only #
74+
"we-love-dashes_cc_proto;pybind11_native_proto_caster")
75+
76+
function(add_py_test testname)
77+
add_test(NAME ${testname}_test
78+
COMMAND ${Python_EXECUTABLE}
79+
${CMAKE_CURRENT_SOURCE_DIR}/${testname}_test.py)
80+
set_property(TEST ${testname}_test #
81+
PROPERTY ENVIRONMENT "PYTHONPATH=${CMAKE_BINARY_DIR}")
82+
endfunction()
83+
84+
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/compare.py
85+
DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
86+
87+
add_py_test(proto_enum)
88+
add_py_test(dynamic_message)
89+
add_py_test(extension)
90+
add_py_test(message)
91+
add_py_test(pass_by)
92+
add_py_test(wrapped_proto_module)
93+
add_py_test(thread_module)
94+
add_py_test(regression_wrappers)
95+
add_py_test(we_love_dashes_cc_only)

0 commit comments

Comments
 (0)