Skip to content

Commit 57422ba

Browse files
rwgkcopybara-github
authored andcommitted
Import #73 (again)
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. * Initially merged via #108 * Rolled back via #109 PiperOrigin-RevId: 515452762
1 parent a38bec0 commit 57422ba

File tree

4 files changed

+253
-1
lines changed

4 files changed

+253
-1
lines changed

.github/workflows/pre-commit.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
name: pre-commit
22

33
on:
4+
workflow_dispatch:
45
pull_request:
56
push:
6-
branches: [main]
7+
branches:
8+
- main
79

810
jobs:
911
pre-commit:

.github/workflows/ubuntu-build.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: ubuntu-build
2+
3+
on:
4+
workflow_dispatch:
5+
pull_request:
6+
push:
7+
branches:
8+
- main
9+
10+
concurrency:
11+
group: ci-${{github.workflow}}-${{ github.ref }}
12+
cancel-in-progress: true
13+
14+
jobs:
15+
build:
16+
runs-on: ubuntu-22.04
17+
name: ubuntu-build
18+
steps:
19+
- name: Show env
20+
run: env
21+
22+
- name: Checkout
23+
uses: actions/checkout@v3
24+
25+
- name: Install Build Dependencies
26+
shell: bash
27+
run: |
28+
sudo apt update && sudo apt install --no-install-recommends -y \
29+
cmake \
30+
make
31+
32+
- name: Show Python version and platform info
33+
run: |
34+
python --version
35+
python -m platform
36+
37+
- name: Show CMake version
38+
run: cmake --version
39+
40+
- name: CMake Configure
41+
shell: bash
42+
run: |
43+
cmake -S . -B build -DCMAKE_VERBOSE_MAKEFILE=ON
44+
45+
- name: CMake Build
46+
shell: bash
47+
run: |
48+
cmake --build build -j$(nproc)

CMakeLists.txt

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
15+
16+
# ============================================================================
17+
# Options
18+
19+
option(BUILD_TESTS "Build tests." OFF)
20+
21+
# ============================================================================
22+
# Find Python
23+
24+
find_package(Python COMPONENTS Interpreter Development)
25+
26+
# ============================================================================
27+
# Build dependencies
28+
29+
set(_absl_repository "https://github.com/abseil/abseil-cpp.git")
30+
set(_absl_version 20220623.1)
31+
set(_absl_tag 20220623.1)
32+
33+
set(_protobuf_repository "https://github.com/protocolbuffers/protobuf.git")
34+
set(_protobuf_version 21.5)
35+
set(_protobuf_tag v21.5)
36+
37+
set(_pybind11_repository "https://github.com/pybind/pybind11.git")
38+
set(_pybind11_version 2.10)
39+
set(_pybind11_tag v2.10.1)
40+
41+
add_subdirectory(cmake/dependencies dependencies)
42+
43+
# ============================================================================
44+
# pybind11_proto_utils pybind11 extension module
45+
pybind11_add_module(
46+
pybind11_proto_utils MODULE pybind11_protobuf/proto_utils.cc
47+
pybind11_protobuf/proto_utils.h)
48+
49+
target_link_libraries(
50+
pybind11_proto_utils PRIVATE absl::strings protobuf::libprotobuf
51+
${Python_LIBRARIES})
52+
53+
target_include_directories(
54+
pybind11_proto_utils PRIVATE ${PROJECT_SOURCE_DIR} ${protobuf_INCLUDE_DIRS}
55+
${protobuf_SOURCE_DIR} ${pybind11_INCLUDE_DIRS})
56+
57+
# ============================================================================
58+
# pybind11_native_proto_caster shared library
59+
add_library(
60+
pybind11_native_proto_caster SHARED
61+
# bazel: pybind_library: native_proto_caster
62+
pybind11_protobuf/native_proto_caster.h
63+
# bazel: pybind_library: enum_type_caster
64+
pybind11_protobuf/enum_type_caster.h
65+
# bazel: pybind_library: proto_cast_util
66+
pybind11_protobuf/proto_cast_util.cc
67+
pybind11_protobuf/proto_cast_util.h
68+
pybind11_protobuf/proto_caster_impl.h
69+
# bazel: cc_library::check_unknown_fields
70+
pybind11_protobuf/check_unknown_fields.cc
71+
pybind11_protobuf/check_unknown_fields.h)
72+
73+
target_link_libraries(
74+
pybind11_native_proto_caster
75+
absl::flat_hash_map
76+
absl::flat_hash_set
77+
absl::hash
78+
absl::strings
79+
absl::optional
80+
protobuf::libprotobuf
81+
pybind11::pybind11
82+
${Python_LIBRARIES})
83+
84+
target_include_directories(
85+
pybind11_native_proto_caster
86+
PRIVATE ${PROJECT_SOURCE_DIR} ${protobuf_INCLUDE_DIRS} ${protobuf_SOURCE_DIR}
87+
${pybind11_INCLUDE_DIRS})
88+
89+
# ============================================================================
90+
# pybind11_wrapped_proto_caster shared library
91+
add_library(
92+
pybind11_wrapped_proto_caster SHARED
93+
# bazel: pybind_library: wrapped_proto_caster
94+
pybind11_protobuf/wrapped_proto_caster.h
95+
# bazel: pybind_library: proto_cast_util
96+
pybind11_protobuf/proto_cast_util.cc
97+
pybind11_protobuf/proto_cast_util.h
98+
pybind11_protobuf/proto_caster_impl.h
99+
# bazel: cc_library: check_unknown_fields
100+
pybind11_protobuf/check_unknown_fields.cc
101+
pybind11_protobuf/check_unknown_fields.h)
102+
103+
target_link_libraries(
104+
pybind11_wrapped_proto_caster
105+
absl::flat_hash_map
106+
absl::flat_hash_set
107+
absl::hash
108+
absl::strings
109+
absl::optional
110+
protobuf::libprotobuf
111+
pybind11::pybind11
112+
${Python_LIBRARIES})
113+
114+
target_include_directories(
115+
pybind11_wrapped_proto_caster
116+
PRIVATE ${PROJECT_SOURCE_DIR} ${protobuf_INCLUDE_DIRS} ${protobuf_SOURCE_DIR}
117+
${pybind11_INCLUDE_DIRS})
118+
119+
# TODO set defines PYBIND11_PROTOBUF_ENABLE_PYPROTO_API see: bazel:
120+
# pybind_library: proto_cast_util
121+
122+
# bazel equivs. checklist
123+
#
124+
# bazel: pybind_library: enum_type_caster - enum_type_caster.h
125+
#
126+
# bazel: pybind_library: native_proto_caster - native_proto_caster.h
127+
#
128+
# check_unknown_fields enum_type_caster proto_cast_util
129+
#
130+
# bazel: pybind_library: proto_cast_util - proto_cast_util.cc -
131+
# proto_cast_util.h - proto_caster_impl.h
132+
#
133+
# check_unknown_fields
134+
#
135+
# bazel: pybind_library: wrapped_proto_caster - wrapped_proto_caster.h
136+
#
137+
# proto_cast_util
138+
#

cmake/dependencies/CMakeLists.txt

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
include(FetchContent)
2+
3+
# ============================================================================
4+
# Declare all dependencies first
5+
6+
find_package(absl ${_absl_version} EXACT QUIET)
7+
if(NOT absl_FOUND)
8+
set(ABSL_PROPAGATE_CXX_STD ON)
9+
FetchContent_Declare(
10+
absl
11+
GIT_REPOSITORY ${_absl_repository}
12+
GIT_TAG ${_absl_tag})
13+
endif()
14+
15+
# https://stackoverflow.com/questions/63309544/cmake-protobuf-external-to-application-code
16+
# https://cmake.org/cmake/help/latest/policy/CMP0077.html
17+
# https://gitlab.kitware.com/cmake/cmake/-/merge_requests/7565/diffs
18+
find_package(Protobuf ${_protobuf_version} EXACT QUIET)
19+
if(NOT Protobuf_FOUND)
20+
set(protobuf_BUILD_TESTS
21+
OFF
22+
CACHE INTERNAL "")
23+
FetchContent_Declare(
24+
Protobuf
25+
GIT_REPOSITORY ${_protobuf_repository}
26+
GIT_TAG ${_protobuf_tag}
27+
GIT_SUBMODULES "")
28+
endif()
29+
30+
find_package(pybind11 ${_pybind11_version} EXACT QUIET)
31+
if(NOT pybind11_FOUND)
32+
set(PYBIND11_TEST OFF)
33+
FetchContent_Declare(
34+
pybind11
35+
GIT_REPOSITORY ${_pybind11_repository}
36+
GIT_TAG ${_pybind11_tag})
37+
endif()
38+
39+
# ============================================================================
40+
# Make dependencies avaialble
41+
42+
if(NOT absl_FOUND)
43+
message(CHECK_START "Fetching Abseil-cpp")
44+
list(APPEND CMAKE_MESSAGE_INDENT " ")
45+
FetchContent_MakeAvailable(absl)
46+
list(POP_BACK CMAKE_MESSAGE_INDENT)
47+
message(CHECK_PASS "fetched")
48+
endif()
49+
50+
if(NOT Protobuf_FOUND)
51+
message(CHECK_START "Fetching Protobuf")
52+
list(APPEND CMAKE_MESSAGE_INDENT " ")
53+
FetchContent_MakeAvailable(Protobuf)
54+
list(POP_BACK CMAKE_MESSAGE_INDENT)
55+
message(CHECK_PASS "fetched")
56+
endif()
57+
58+
if(NOT pybind11_FOUND)
59+
message(CHECK_START "Fetching pybind11")
60+
list(APPEND CMAKE_MESSAGE_INDENT " ")
61+
FetchContent_MakeAvailable(pybind11)
62+
list(POP_BACK CMAKE_MESSAGE_INDENT)
63+
message(CHECK_PASS "fetched")
64+
endif()

0 commit comments

Comments
 (0)