Skip to content

Commit d7d37cd

Browse files
committed
[CMake] Build and run the test cases
Building and running tests was never implemented for CMake, create the same test cases as for the Bazel build. Use the standard "BUILD_TESTING" option (implicitly added by CTest).
1 parent f4ab4bd commit d7d37cd

File tree

2 files changed

+108
-2
lines changed

2 files changed

+108
-2
lines changed

CMakeLists.txt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@ set(CMAKE_POSITION_INDEPENDENT_CODE ON)
1616
# ============================================================================
1717
# Options
1818

19-
option(BUILD_TESTS "Build tests." OFF)
2019
option(ENABLE_PYPROTO_API "Enable usage of proto_api." OFF)
2120
option(USE_SYSTEM_ABSEIL "Force usage of system provided abseil-cpp" OFF)
2221
option(USE_SYSTEM_PROTOBUF "Force usage of system provided Protobuf" OFF)
2322
option(USE_SYSTEM_PYBIND "Force usage of system provided pybind11" OFF)
2423

24+
# ============================================================================
25+
# Testing
26+
include(CTest)
27+
2528
# ============================================================================
2629
# Find Python
2730

@@ -57,13 +60,15 @@ FetchContent_Declare(
5760
GIT_TAG 20230125.3
5861
FIND_PACKAGE_ARGS ${_absl_package_args} NAMES absl)
5962

63+
# cmake-format: off
6064
FetchContent_Declare(
6165
Protobuf
6266
GIT_REPOSITORY "https://github.com/protocolbuffers/protobuf.git"
6367
GIT_TAG v23.3
64-
GIT_SUBMODULES "" #
68+
GIT_SUBMODULES ""
6569
FIND_PACKAGE_ARGS ${_protobuf_package_args} NAMES protobuf)
6670
set(protobuf_BUILD_TESTS OFF CACHE INTERNAL "")
71+
# cmake-format: on
6772

6873
FetchContent_Declare(
6974
pybind11
@@ -168,6 +173,10 @@ target_include_directories(
168173
PRIVATE ${PROJECT_SOURCE_DIR} ${protobuf_INCLUDE_DIRS} ${protobuf_SOURCE_DIR}
169174
${pybind11_INCLUDE_DIRS})
170175

176+
if(BUILD_TESTING)
177+
add_subdirectory(pybind11_protobuf/tests)
178+
endif()
179+
171180
# bazel equivs. checklist
172181
#
173182
# bazel: pybind_library: enum_type_caster - enum_type_caster.h
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
# FIXME What is the difference to the "extension_test"?
91+
# add_py_test(extension_disallow_unknown_fields)
92+
add_py_test(message)
93+
add_py_test(pass_by)
94+
add_py_test(wrapped_proto_module)
95+
add_py_test(thread_module)
96+
add_py_test(regression_wrappers)
97+
add_py_test(we_love_dashes_cc_only)

0 commit comments

Comments
 (0)