Skip to content

Commit

Permalink
Import #162:
Browse files Browse the repository at this point in the history
* 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: 647572543
  • Loading branch information
StefanBruens authored and copybara-github committed Jun 28, 2024
1 parent e90f33e commit d8f3dde
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 80 deletions.
77 changes: 59 additions & 18 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ set(CMAKE_POSITION_INDEPENDENT_CODE ON)
# ============================================================================
# Options

option(BUILD_TESTS "Build tests." OFF)
option(USE_SYSTEM_ABSEIL "Force usage of system provided abseil-cpp" OFF)
option(USE_SYSTEM_PROTOBUF "Force usage of system provided Protobuf" OFF)
option(USE_SYSTEM_PYBIND "Force usage of system provided pybind11" OFF)

# ============================================================================
# Testing
include(CTest)

# ============================================================================
# Find Python
Expand All @@ -26,22 +32,53 @@ find_package(Python COMPONENTS Interpreter Development)
# ============================================================================
# Build dependencies

set(_absl_repository "https://github.com/abseil/abseil-cpp.git")
set(_absl_version 20230125)
set(_absl_tag 20230125.3)
find_package(absl ${_absl_version} QUIET)

set(_protobuf_repository "https://github.com/protocolbuffers/protobuf.git")
set(_protobuf_version 3.23.3)
set(_protobuf_tag v23.3)
find_package(Protobuf ${_protobuf_version} QUIET)

set(_pybind11_repository "https://github.com/pybind/pybind11.git")
set(_pybind11_version 2.11.1)
set(_pybind11_tag v2.11.1)
find_package(pybind11 ${_pybind11_version} QUIET)
if(USE_SYSTEM_ABSEIL)
# Version omitted, as absl only allows EXACT version matches
set(_absl_package_args REQUIRED)
else()
set(_absl_package_args 20230125)
endif()
if(USE_SYSTEM_PROTOBUF)
set(_protobuf_package_args 4.23.3 REQUIRED)
else()
set(_protobuf_package_args 4.23.3)
endif()
if(USE_SYSTEM_PYBIND)
set(_pybind11_package_args 2.11.1 REQUIRED)
else()
set(_pybind11_package_args 2.11.1)
endif()

add_subdirectory(cmake/dependencies dependencies)
set(ABSL_PROPAGATE_CXX_STD ON)
set(ABSL_ENABLE_INSTALL ON)

include(FetchContent)
FetchContent_Declare(
absl
GIT_REPOSITORY "https://github.com/abseil/abseil-cpp.git"
GIT_TAG 20230125.3
FIND_PACKAGE_ARGS ${_absl_package_args} NAMES absl)

# cmake-format: off
FetchContent_Declare(
Protobuf
GIT_REPOSITORY "https://github.com/protocolbuffers/protobuf.git"
GIT_TAG v23.3
GIT_SUBMODULES ""
FIND_PACKAGE_ARGS ${_protobuf_package_args} NAMES protobuf)
set(protobuf_BUILD_TESTS OFF CACHE INTERNAL "")
# cmake-format: on

FetchContent_Declare(
pybind11
GIT_REPOSITORY "https://github.com/pybind/pybind11.git"
GIT_TAG v2.11.1
FIND_PACKAGE_ARGS ${_pybind11_package_args} NAMES pybind11)

message(CHECK_START "Checking for external dependencies")
list(APPEND CMAKE_MESSAGE_INDENT " ")
FetchContent_MakeAvailable(absl Protobuf pybind11)
list(POP_BACK CMAKE_MESSAGE_INDENT)

# ============================================================================
# pybind11_proto_utils pybind11 extension module
Expand All @@ -60,7 +97,7 @@ target_include_directories(
# ============================================================================
# pybind11_native_proto_caster shared library
add_library(
pybind11_native_proto_caster SHARED
pybind11_native_proto_caster STATIC
# bazel: pybind_library: native_proto_caster
pybind11_protobuf/native_proto_caster.h
# bazel: pybind_library: enum_type_caster
Expand Down Expand Up @@ -89,7 +126,7 @@ target_include_directories(
# ============================================================================
# pybind11_wrapped_proto_caster shared library
add_library(
pybind11_wrapped_proto_caster SHARED
pybind11_wrapped_proto_caster STATIC
# bazel: pybind_library: wrapped_proto_caster
pybind11_protobuf/wrapped_proto_caster.h
# bazel: pybind_library: proto_cast_util
Expand All @@ -113,6 +150,10 @@ target_include_directories(
PRIVATE ${PROJECT_SOURCE_DIR} ${protobuf_INCLUDE_DIRS} ${protobuf_SOURCE_DIR}
${pybind11_INCLUDE_DIRS})

if(BUILD_TESTING)
add_subdirectory(pybind11_protobuf/tests)
endif()

# bazel equivs. checklist
#
# bazel: pybind_library: enum_type_caster - enum_type_caster.h
Expand Down
62 changes: 0 additions & 62 deletions cmake/dependencies/CMakeLists.txt

This file was deleted.

97 changes: 97 additions & 0 deletions pybind11_protobuf/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#
# Evaluate if Protobuf uses the system package, otherwise explicitly include the
# required macro
#
FetchContent_GetProperties(Protobuf SOURCE_DIR Protobuf_SOURCE_DIR)
if(Protobuf_SOURCE_DIR)
# Use macros from content made available by FetchContent
include(${Protobuf_SOURCE_DIR}/cmake/protobuf-generate.cmake)
endif()

# cmake-format: off
function(generate_cc_proto protoname)
# Generate C++ files (.pb.h, .pb.cc)
#
add_library(${protoname}_cc_proto OBJECT)
target_include_directories(${protoname}_cc_proto
PRIVATE $<TARGET_PROPERTY:protobuf::libprotobuf,INCLUDE_DIRECTORIES>
$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}>)
protobuf_generate(
TARGET ${protoname}_cc_proto
PROTOS ${CMAKE_SOURCE_DIR}/pybind11_protobuf/tests/${protoname}.proto
IMPORT_DIRS ${CMAKE_SOURCE_DIR}
PROTOC_OUT_DIR ${CMAKE_BINARY_DIR})
endfunction()

function(generate_py_proto protoname)
# Generate Python files (_pb2.py)
#
add_custom_target(${protoname}_py_pb2 ALL)
protobuf_generate(
TARGET ${protoname}_py_pb2
LANGUAGE PYTHON
PROTOS ${CMAKE_SOURCE_DIR}/pybind11_protobuf/tests/${protoname}.proto
IMPORT_DIRS ${CMAKE_SOURCE_DIR}
PROTOC_OUT_DIR ${CMAKE_BINARY_DIR})
endfunction()
# cmake-format: on

generate_cc_proto("test")
generate_cc_proto("extension")
generate_cc_proto("extension_nest_repeated")
generate_cc_proto("extension_in_other_file_in_deps")
generate_cc_proto("extension_in_other_file")
generate_cc_proto("we-love-dashes")

generate_py_proto("test")
generate_py_proto("extension")
generate_py_proto("extension_nest_repeated")
generate_py_proto("extension_in_other_file_in_deps")
generate_py_proto("extension_in_other_file")

function(generate_extension modulename deps)
pybind11_add_module(${modulename}_module ${modulename}_module.cc)
add_dependencies(${modulename}_module ${deps})
target_include_directories(${modulename}_module #
PRIVATE ${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR})
target_link_libraries(${modulename}_module #
PRIVATE protobuf::libprotobuf ${deps})
endfunction()

generate_extension(proto_enum "test_cc_proto")
generate_extension(dynamic_message "pybind11_native_proto_caster")
generate_extension(
extension #
"extension_in_other_file_in_deps_cc_proto;extension_nest_repeated_cc_proto;test_cc_proto;extension_cc_proto;pybind11_native_proto_caster"
)
generate_extension(message "test_cc_proto;pybind11_native_proto_caster")
generate_extension(pass_by "test_cc_proto;pybind11_native_proto_caster")
generate_extension(pass_proto2_message "pybind11_native_proto_caster")
generate_extension(wrapped_proto "test_cc_proto;pybind11_wrapped_proto_caster")
generate_extension(thread "test_cc_proto;pybind11_native_proto_caster")
generate_extension(regression_wrappers "pybind11_native_proto_caster")
generate_extension(we_love_dashes_cc_only #
"we-love-dashes_cc_proto;pybind11_native_proto_caster")

function(add_py_test testname)
add_test(NAME ${testname}_test
COMMAND ${Python_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/${testname}_test.py)
set_property(TEST ${testname}_test #
PROPERTY ENVIRONMENT "PYTHONPATH=${CMAKE_BINARY_DIR}")
endfunction()

file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/compare.py
DESTINATION ${CMAKE_CURRENT_BINARY_DIR})

add_py_test(proto_enum)
add_py_test(dynamic_message)
add_py_test(extension)
# FIXME What is the difference to the "extension_test"?
# add_py_test(extension_disallow_unknown_fields)
add_py_test(message)
add_py_test(pass_by)
add_py_test(wrapped_proto_module)
add_py_test(thread_module)
add_py_test(regression_wrappers)
add_py_test(we_love_dashes_cc_only)

0 comments on commit d8f3dde

Please sign in to comment.