Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 43 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
cmake_minimum_required(VERSION 3.18)
# The minimum CMake version and C++ standard follow Google's Foundational C++
# Support matrix; keep them in sync with it:
# https://github.com/google/oss-policies-info/blob/main/foundational-cxx-support-matrix.md
cmake_minimum_required(VERSION 3.22)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

project(pybind11_protobuf LANGUAGES CXX)

if(NOT DEFINED CMAKE_CXX_STANDARD)
if(MSVC)
set(CMAKE_CXX_STANDARD 20)
else()
set(CMAKE_CXX_STANDARD 17)
endif()
set(CMAKE_CXX_STANDARD 17)
endif()
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION FALSE)

if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.24)
cmake_policy(SET CMP0135 NEW)
endif()

# ============================================================================
# Options
Expand All @@ -32,10 +36,42 @@ option(PYBIND11_PROTOBUF_BUILD_TESTING
# Find Python
find_package(Python3 REQUIRED COMPONENTS Interpreter Development.Module)

# Place runtime artifacts (such as protoc.exe and shared Abseil/Protobuf DLLs on
# Windows) in a single directory so build-time protoc invocations find their
# dependent DLLs.
if(WIN32 AND NOT DEFINED CMAKE_RUNTIME_OUTPUT_DIRECTORY)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
endif()

# ============================================================================
# Build dependencies
add_subdirectory(cmake/dependencies dependencies)

set(TOP_LEVEL_DIR ${CMAKE_CURRENT_LIST_DIR})
include_directories(${TOP_LEVEL_DIR} ${pybind11_INCLUDE_DIRS})

# Makes extension module `target` importable from the build tree:
#
# * Multi-config generators (e.g. Xcode, Visual Studio) put build outputs in a
# per-configuration subdirectory, outside the Python package directory. A
# generator expression in the output directory disables that subdirectory.
# * On Windows, Python loads the DLLs an extension module depends on from the
# module's directory, not from PATH, so copy them there. There are none if
# dependencies are linked statically, and `copy_if_different` fails without
# files, so run `cmake -E true` instead then.
function(pybind11_protobuf_build_tree_module target)
set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY
"$<1:${CMAKE_CURRENT_BINARY_DIR}>")
if(WIN32)
add_custom_command(
TARGET ${target}
POST_BUILD
COMMAND
${CMAKE_COMMAND} -E
$<IF:$<BOOL:$<TARGET_RUNTIME_DLLS:${target}>>,copy_if_different,true>
$<TARGET_RUNTIME_DLLS:${target}> $<TARGET_FILE_DIR:${target}>
COMMAND_EXPAND_LISTS)
endif()
endfunction()

add_subdirectory(pybind11_protobuf)
3 changes: 3 additions & 0 deletions cmake/dependencies/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ if(NOT TARGET protobuf::libprotobuf)
message(CHECK_START "Fetching Protobuf")
list(APPEND CMAKE_MESSAGE_INDENT " ")
set(protobuf_BUILD_TESTS OFF)
# Match the dynamic MSVC C runtime (/MD) used by Python, pybind11, and
# Abseil on Windows.
set(protobuf_MSVC_STATIC_RUNTIME OFF)
FetchContent_Declare(
Protobuf
GIT_REPOSITORY "https://github.com/protocolbuffers/protobuf.git"
Expand Down
25 changes: 4 additions & 21 deletions pybind11_protobuf/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# ============================================================================
# pybind11_proto_utils pybind11 extension module
pybind11_add_module(pybind11_proto_utils MODULE proto_utils.cc proto_utils.h)
pybind11_protobuf_build_tree_module(pybind11_proto_utils)
add_library(pybind11_protobuf::pybind11_proto_utils ALIAS pybind11_proto_utils)

# note: macOS is APPLE and also UNIX !
Expand All @@ -15,7 +16,7 @@ target_link_libraries(pybind11_proto_utils PUBLIC absl::strings
protobuf::libprotobuf)

# ============================================================================
# pybind11_native_proto_caster shared library
# pybind11_native_proto_caster static library
add_library(
pybind11_native_proto_caster STATIC
# bazel: pybind_library: native_proto_caster
Expand Down Expand Up @@ -43,7 +44,7 @@ target_include_directories(pybind11_native_proto_caster
PUBLIC $<BUILD_INTERFACE:${TOP_LEVEL_DIR}>)

# ============================================================================
# pybind11_wrapped_proto_caster shared library
# pybind11_wrapped_proto_caster static library
add_library(
pybind11_wrapped_proto_caster STATIC
# bazel: pybind_library: wrapped_proto_caster
Expand All @@ -66,24 +67,6 @@ target_link_libraries(
target_include_directories(pybind11_wrapped_proto_caster
PUBLIC $<BUILD_INTERFACE:${TOP_LEVEL_DIR}>)

if(BUILD_TESTING)
if(PYBIND11_PROTOBUF_BUILD_TESTING)
add_subdirectory(tests)
endif()

if(CMAKE_INSTALL_PYDIR)
# Copying to two target directories for simplicity. It is currently unknown
# how to determine here which copy is actually being used.
install(
TARGETS status_py_extension_stub ok_status_singleton
EXPORT pybind11_abseilTargets
LIBRARY DESTINATION ${CMAKE_INSTALL_PYDIR}/pybind11_abseil
ARCHIVE DESTINATION ${CMAKE_INSTALL_PYDIR}/pybind11_abseil
RUNTIME DESTINATION ${CMAKE_INSTALL_PYDIR}/pybind11_abseil)

install(
TARGETS status_py_extension_stub ok_status_singleton
EXPORT pybind11_abseil_cppTargets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()
21 changes: 14 additions & 7 deletions pybind11_protobuf/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ function(generate_cc_proto protoname)
#
add_library(${protoname}_cc_proto OBJECT)
target_include_directories(${protoname}_cc_proto
PRIVATE $<TARGET_PROPERTY:protobuf::libprotobuf,INCLUDE_DIRECTORIES>
$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}>)
PRIVATE $<BUILD_INTERFACE:${CMAKE_BINARY_DIR}>)
target_link_libraries(${protoname}_cc_proto PUBLIC protobuf::libprotobuf)
if(ARGN)
add_dependencies(${protoname}_cc_proto ${ARGN})
endif()
protobuf_generate(
TARGET ${protoname}_cc_proto
PROTOS ${CMAKE_SOURCE_DIR}/pybind11_protobuf/tests/${protoname}.proto
Expand All @@ -37,10 +40,10 @@ 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("extension" test_cc_proto)
generate_cc_proto("extension_nest_repeated" extension_cc_proto)
generate_cc_proto("extension_in_other_file_in_deps" extension_cc_proto)
generate_cc_proto("extension_in_other_file" extension_cc_proto)
generate_cc_proto("we-love-dashes")

generate_py_proto("test")
Expand All @@ -50,7 +53,11 @@ 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)
pybind11_add_module(${modulename}_module MODULE ${modulename}_module.cc)
pybind11_protobuf_build_tree_module(${modulename}_module)
if(APPLE)
set_target_properties(${modulename}_module PROPERTIES SUFFIX ".so")
endif()
add_dependencies(${modulename}_module ${deps})
target_include_directories(${modulename}_module #
PRIVATE ${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR})
Expand Down
Loading