|
| 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 | +generate_extension(message "test_cc_proto;pybind11_native_proto_caster") |
| 67 | +generate_extension(pass_by "test_cc_proto;pybind11_native_proto_caster") |
| 68 | +generate_extension(pass_proto2_message "pybind11_native_proto_caster") |
| 69 | +generate_extension(wrapped_proto "test_cc_proto;pybind11_wrapped_proto_caster") |
| 70 | +generate_extension(thread "test_cc_proto;pybind11_native_proto_caster") |
| 71 | +generate_extension(regression_wrappers "pybind11_native_proto_caster") |
| 72 | +generate_extension(we_love_dashes_cc_only # |
| 73 | + "we-love-dashes_cc_proto;pybind11_native_proto_caster") |
| 74 | + |
| 75 | +function(add_py_test testname) |
| 76 | + add_test(NAME ${testname}_test |
| 77 | + COMMAND ${Python_EXECUTABLE} |
| 78 | + ${CMAKE_CURRENT_SOURCE_DIR}/${testname}_test.py) |
| 79 | + set_property(TEST ${testname}_test # |
| 80 | + PROPERTY ENVIRONMENT "PYTHONPATH=${CMAKE_BINARY_DIR}") |
| 81 | +endfunction() |
| 82 | + |
| 83 | +file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/compare.py |
| 84 | + DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) |
| 85 | + |
| 86 | +add_py_test(proto_enum) |
| 87 | +add_py_test(dynamic_message) |
| 88 | +add_py_test(extension) |
| 89 | +# FIXME What is the difference to the "extension_test"? |
| 90 | +# add_py_test(extension_disallow_unknown_fields) |
| 91 | +add_py_test(message) |
| 92 | +add_py_test(pass_by) |
| 93 | +add_py_test(wrapped_proto_module) |
| 94 | +add_py_test(thread_module) |
| 95 | +add_py_test(regression_wrappers) |
| 96 | +add_py_test(we_love_dashes_cc_only) |
0 commit comments