From a0c1b24794eff9f872c727664841c4c900da074f Mon Sep 17 00:00:00 2001 From: Dave Allison Date: Thu, 5 Nov 2026 17:59:16 -0800 Subject: [PATCH] Bring the CMake build up to date and add C and Rust output The CMake build had fallen behind the Bazel one. protoc-gen-phaser did not compile, because the plugin sources and the include paths Bazel remaps were missing. The runtime lacked payload_buffer.cc and message_property.cc, and generated libraries did not link the descriptor bank or the Arrow, Avro and Parquet runtimes that generated code calls. - Build the full plugin, with forwarding headers for the remapped include paths. - Add the phaser_bank_protobuf, phaser_arrow, phaser_avro, phaser_parquet and phaser_c libraries, and link them into generated libraries. - phaser_add_library takes FRONTEND c, and RUST for the Rust bindings over the C API. Their source file is in the library's PHASER_RUST_SOURCE property. - Move the crate attributes out of rust_support.rs into rust_crate_attributes.rs, so that a Cargo crate can include!() the bindings. The Bazel rule prepends them, and its crate is unchanged. - Add a CMake smoke test project and a CI job that builds it, compiles the Rust bindings and runs a program that reads a C++ message through the C API. --- .github/workflows/ci.yml | 25 ++++ CMakeLists.txt | 160 +++++++++++++++++++++--- README.md | 16 +++ cmake/concatenate.cmake | 10 ++ cmake/smoke_test/CMakeLists.txt | 47 +++++++ cmake/smoke_test/smoke_test.cc | 34 +++++ phaser/phaser_library.bzl | 6 +- phaser/runtime/BUILD.bazel | 5 +- phaser/runtime/rust_crate_attributes.rs | 7 ++ phaser/runtime/rust_support.rs | 7 -- 10 files changed, 293 insertions(+), 24 deletions(-) create mode 100644 cmake/concatenate.cmake create mode 100644 cmake/smoke_test/CMakeLists.txt create mode 100644 cmake/smoke_test/smoke_test.cc create mode 100644 phaser/runtime/rust_crate_attributes.rs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9654afa..f4064ac 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -44,6 +44,31 @@ jobs: bazel test --disk_cache="$HOME/.cache/bazel-disk" $EXTRA_CONFIG \ -- //... -//phaser:perf_test + # The CMake build. The smoke test project generates a library with each + # frontend, compiles the Rust bindings and runs a program that links both. + # The policy minimum lets CMake 4 configure dependencies that declare an + # older one. + cmake: + name: cmake (${{ matrix.os }}) + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, macos-latest] + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@v4 + + - name: Configure + run: | + cmake -S cmake/smoke_test -B build/cmake -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_POLICY_VERSION_MINIMUM=3.5 + + - name: Build + run: cmake --build build/cmake --parallel 4 + + - name: Test + run: ctest --test-dir build/cmake --output-on-failure + # AddressSanitizer runs on macOS (Apple Silicon). asan: name: asan (macOS) diff --git a/CMakeLists.txt b/CMakeLists.txt index c34fc99..298ad72 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -55,7 +55,9 @@ if(NOT TARGET toolbelt) FetchContent_MakeAvailable(cpp_toolbelt) endif() -if(PHASER_BUILD_COMPILER AND NOT TARGET protobuf::libprotoc) +# phaser_bank_protobuf links libprotobuf, so a cross-compile needs it too. +if(NOT TARGET protobuf::libprotobuf OR + (PHASER_BUILD_COMPILER AND NOT TARGET protobuf::libprotoc)) set(protobuf_BUILD_TESTS OFF CACHE INTERNAL "") FetchContent_Declare( protobuf @@ -65,16 +67,44 @@ if(PHASER_BUILD_COMPILER AND NOT TARGET protobuf::libprotoc) FetchContent_MakeAvailable(protobuf) endif() -# --- Runtime library --- +set(_PHASER_DIR "${CMAKE_CURRENT_SOURCE_DIR}" CACHE INTERNAL "") + +# Bazel gives some plugin headers shorter paths with strip_include_prefix and +# include_prefix. These forwarding headers provide the same paths. +set(PHASER_FORWARDING_INCLUDE_DIR "${CMAKE_CURRENT_BINARY_DIR}/phaser_include") +foreach(header + "arrow/arrow.h=arrow/runtime/arrow.h" + "arrow/arrow_abi.h=arrow/runtime/arrow_abi.h" + "avro/avro.h=avro/runtime/avro.h" + "parquet/parquet.h=parquet/runtime/parquet.h" + "ros/ros_codegen_helpers.h=ros/gen/ros_codegen_helpers.h") + string(REPLACE "=" ";" paths "${header}") + list(GET paths 0 short_path) + list(GET paths 1 source_path) + file(GENERATE + OUTPUT "${PHASER_FORWARDING_INCLUDE_DIR}/phaser/plugins/${short_path}" + CONTENT "#pragma once\n#include \"phaser/plugins/${source_path}\"\n") +endforeach() + +# --- Runtime libraries --- add_library(phaser_runtime STATIC phaser/runtime/md5.cc phaser/runtime/message.cc + phaser/runtime/message_property.cc + phaser/runtime/payload_buffer.cc phaser/runtime/phaser_bank.cc ) add_library(phaser::runtime ALIAS phaser_runtime) -target_include_directories(phaser_runtime PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) +target_include_directories(phaser_runtime PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR} + ${PHASER_FORWARDING_INCLUDE_DIR} +) target_link_libraries(phaser_runtime PUBLIC + absl::core_headers absl::flat_hash_map + absl::flat_hash_set + absl::function_ref + absl::no_destructor absl::status absl::statusor absl::strings @@ -87,6 +117,27 @@ if(PHASER_TRUST_SERIALIZED_SIZE) PHASER_TRUST_SERIALIZED_SIZE) endif() +# Generated code registers its embedded descriptors here and calls the Arrow, +# Avro and Parquet conversions, so every generated library links these. +add_library(phaser_bank_protobuf STATIC phaser/runtime/phaser_bank_protobuf.cc) +target_link_libraries(phaser_bank_protobuf PUBLIC + phaser_runtime + protobuf::libprotobuf +) + +add_library(phaser_arrow STATIC phaser/plugins/arrow/runtime/arrow.cc) +target_link_libraries(phaser_arrow PUBLIC phaser_runtime) + +add_library(phaser_avro STATIC phaser/plugins/avro/runtime/avro.cc) +target_link_libraries(phaser_avro PUBLIC phaser_arrow) + +add_library(phaser_parquet STATIC phaser/plugins/parquet/runtime/parquet.cc) +target_link_libraries(phaser_parquet PUBLIC phaser_arrow) + +# The C API under FRONTEND c libraries and the Rust crates built over them. +add_library(phaser_c STATIC phaser/runtime/phaser_c.cc) +target_link_libraries(phaser_c PUBLIC phaser_runtime) + # --- protoc plugin --- if(PHASER_BUILD_COMPILER) if(DEFINED protobuf_SOURCE_DIR) @@ -115,16 +166,32 @@ if(PHASER_BUILD_COMPILER) COMMENT "Generating C++ code from phaser/options.proto" ) + # The plugins register their generators from static initializers, so + # their sources are compiled into the executable rather than linked from + # a static library. add_executable(protoc-gen-phaser phaser/compiler/enum_gen.cc phaser/compiler/gen.cc + phaser/compiler/generator_plugin.cc phaser/compiler/main.cc phaser/compiler/message_gen.cc + phaser/compiler/message_gen_c.cc + phaser/compiler/message_gen_python.cc + phaser/plugins/arrow/gen/arrow_message_codegen.cc + phaser/plugins/arrow/gen/codegen.cc + phaser/plugins/avro/gen/codegen.cc + phaser/plugins/parquet/gen/codegen.cc + phaser/plugins/protobuf/gen/codegen.cc + phaser/plugins/protobuf/gen/protobuf_wire_codegen.cc + phaser/plugins/ros/gen/codegen.cc + phaser/plugins/ros/gen/ros_codegen_helpers.cc + phaser/plugins/ros/gen/ros_wire_codegen.cc ${PHASER_OPTIONS_SRC} ) target_include_directories(protoc-gen-phaser PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} + ${PHASER_FORWARDING_INCLUDE_DIR} ) target_link_libraries(protoc-gen-phaser PRIVATE absl::flat_hash_map @@ -144,7 +211,8 @@ endif() # [IMPORTS ...] # [PACKAGE ] # [ADD_NAMESPACE ] -# [FRONTEND protobuf|ros] +# [FRONTEND protobuf|ros|c] +# [RUST] # [PROTOC ] # [PLUGIN ] # [COMPILE_OPTIONS