Skip to content
Open
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
25 changes: 25 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
160 changes: 145 additions & 15 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -144,7 +211,8 @@ endif()
# [IMPORTS <proto>...]
# [PACKAGE <package>]
# [ADD_NAMESPACE <namespace>]
# [FRONTEND protobuf|ros]
# [FRONTEND protobuf|ros|c]
# [RUST]
# [PROTOC <protoc>]
# [PLUGIN <protoc-gen-phaser>]
# [COMPILE_OPTIONS <option>...])
Expand All @@ -160,10 +228,18 @@ endif()
# a short header, so "proto/foo.proto" is included as "proto/foo.phaser.h".
# PACKAGE defaults to the directory of the first file in PROTOS.
#
# FRONTEND c also generates the C API in <proto path>.phaser.c.h, with a short
# header as for the C++ one. RUST generates the Rust bindings over the C API
# and implies FRONTEND c. The bindings for all the protos are assembled into
# the file named by the library's PHASER_RUST_SOURCE property. Unlike the
# crate the Bazel rule builds, the file has no crate attributes, so a crate
# root includes it with include!() and allows the lints listed in
# phaser/runtime/rust_crate_attributes.rs. The Rust crate links <name>.
#
# PROTOC defaults to the protobuf::protoc target. PLUGIN defaults to the
# protoc-gen-phaser target, or PHASER_PLUGIN_EXECUTABLE when that is set.
function(phaser_add_library name)
cmake_parse_arguments(ARG ""
cmake_parse_arguments(ARG "RUST"
"PACKAGE;ADD_NAMESPACE;FRONTEND;PROTOC;PLUGIN"
"PROTOS;IMPORTS;IMPORT_DIRS;COMPILE_OPTIONS"
${ARGN})
Expand All @@ -176,7 +252,18 @@ function(phaser_add_library name)
get_filename_component(ARG_PACKAGE "${first_proto}" DIRECTORY)
endif()
if(NOT ARG_FRONTEND)
set(ARG_FRONTEND protobuf)
if(ARG_RUST)
set(ARG_FRONTEND c)
else()
set(ARG_FRONTEND protobuf)
endif()
endif()
if(NOT ARG_FRONTEND MATCHES "^(protobuf|ros|c)$")
message(FATAL_ERROR
"phaser_add_library(${name}): FRONTEND must be protobuf, ros or c")
endif()
if(ARG_RUST AND NOT ARG_FRONTEND STREQUAL "c")
message(FATAL_ERROR "phaser_add_library(${name}): RUST needs FRONTEND c")
endif()

set(tool_depends)
Expand Down Expand Up @@ -219,10 +306,19 @@ function(phaser_add_library name)
if(ARG_ADD_NAMESPACE)
set(options "add_namespace=${ARG_ADD_NAMESPACE},${options}")
endif()
if(ARG_RUST)
set(options "${options},rust=true")
endif()

set(header_suffixes .phaser.h)
if(ARG_FRONTEND STREQUAL "c")
list(APPEND header_suffixes .phaser.c.h)
endif()

set(inputs)
set(outputs)
set(short_headers)
set(rust_sources)
foreach(proto ${ARG_PROTOS} ${ARG_IMPORTS})
set(found)
foreach(dir ${ARG_IMPORT_DIRS})
Expand All @@ -237,19 +333,25 @@ function(phaser_add_library name)
list(APPEND inputs "${found}")

string(REGEX REPLACE "\\.proto$" "" base "${proto}")
list(APPEND outputs "${gen_dir}/${base}.phaser.cc" "${gen_dir}/${base}.phaser.h")
if(proto IN_LIST ARG_PROTOS)
set(short_header "${out_dir}/${base}.phaser.h")
file(GENERATE OUTPUT "${short_header}"
CONTENT "#pragma once\n#include \"${gen_include}/${base}.phaser.h\"\n")
list(APPEND short_headers "${short_header}")
list(APPEND outputs "${gen_dir}/${base}.phaser.cc")
foreach(suffix ${header_suffixes})
list(APPEND outputs "${gen_dir}/${base}${suffix}")
if(proto IN_LIST ARG_PROTOS)
set(short_header "${out_dir}/${base}${suffix}")
file(GENERATE OUTPUT "${short_header}"
CONTENT "#pragma once\n#include \"${gen_include}/${base}${suffix}\"\n")
list(APPEND short_headers "${short_header}")
endif()
endforeach()
if(ARG_RUST)
list(APPEND rust_sources "${gen_dir}/${base}.rs")
endif()
endforeach()

# The protos are named by import path. An absolute path could map to a
# second name when one import directory contains another.
add_custom_command(
OUTPUT ${outputs}
OUTPUT ${outputs} ${rust_sources}
COMMAND ${ARG_PROTOC}
ARGS --plugin=protoc-gen-phaser=${ARG_PLUGIN}
--phaser_out=${options}:${out_dir}
Expand All @@ -261,9 +363,37 @@ function(phaser_add_library name)
VERBATIM
)

add_library(${name} STATIC ${outputs} ${short_headers})
set(rust_file)
if(ARG_RUST)
set(rust_file "${out_dir}/${name}.rs")
set(rust_inputs "${_PHASER_DIR}/phaser/runtime/rust_support.rs" ${rust_sources})
add_custom_command(
OUTPUT "${rust_file}"
COMMAND ${CMAKE_COMMAND}
"-DOUTPUT=${rust_file}"
"-DINPUTS=${rust_inputs}"
-P "${_PHASER_DIR}/cmake/concatenate.cmake"
DEPENDS ${rust_inputs} "${_PHASER_DIR}/cmake/concatenate.cmake"
COMMENT "Assembling the Rust bindings for ${name}"
VERBATIM
)
endif()

add_library(${name} STATIC ${outputs} ${short_headers} ${rust_file})
target_include_directories(${name} PUBLIC "${out_dir}")
target_link_libraries(${name} PUBLIC phaser_runtime)
target_link_libraries(${name} PUBLIC
phaser_runtime
phaser_bank_protobuf
phaser_arrow
phaser_avro
phaser_parquet
)
if(ARG_FRONTEND STREQUAL "c")
target_link_libraries(${name} PUBLIC phaser_c)
endif()
if(ARG_RUST)
set_target_properties(${name} PROPERTIES PHASER_RUST_SOURCE "${rust_file}")
endif()
if(ARG_COMPILE_OPTIONS)
target_compile_options(${name} PRIVATE ${ARG_COMPILE_OPTIONS})
endif()
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,22 @@ need phaser code too. The library's short header is included as `"foo/foo.phaser
The plugin runs on the build host, so a cross-compile does not build it; build it for the
host and pass its path in `PHASER_PLUGIN_EXECUTABLE`.

Generated libraries link `phaser_runtime`, `phaser_bank_protobuf` (and so libprotobuf)
and the Arrow, Avro and Parquet runtimes, as they do in Bazel. `FRONTEND c` generates the
C API too, in `"foo/foo.phaser.c.h"`, and links `phaser_c`. `RUST` generates the Rust
bindings over the C API, and implies `FRONTEND c`. Their source file is in the library's
`PHASER_RUST_SOURCE` property. It has no crate attributes, so the crate root that includes
it allows the lints in
[`phaser/runtime/rust_crate_attributes.rs`](phaser/runtime/rust_crate_attributes.rs).
With the path passed to Cargo in `FOO_PHASER_RS`, for example:

```rust
#![allow(non_camel_case_types, non_snake_case, non_upper_case_globals, dead_code)]
include!(env!("FOO_PHASER_RS"));
```

The Rust crate links the generated library.

### Accessor names

A field named `has_<name>` next to a field `<name>` would collide with `<name>`'s presence
Expand Down
10 changes: 10 additions & 0 deletions cmake/concatenate.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# cmake -DOUTPUT=<file> -DINPUTS=<file>;<file>... -P concatenate.cmake
#
# Writes the INPUTS, in order, to OUTPUT.

file(WRITE "${OUTPUT}.tmp" "")
foreach(input IN LISTS INPUTS)
file(READ "${input}" contents)
file(APPEND "${OUTPUT}.tmp" "${contents}")
endforeach()
file(RENAME "${OUTPUT}.tmp" "${OUTPUT}")
47 changes: 47 additions & 0 deletions cmake/smoke_test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Builds phaser as a parent project does and generates libraries from the test
# protos with each frontend. CI runs this to keep the CMake build in step
# with Bazel.

cmake_minimum_required(VERSION 3.15)
project(phaser_cmake_smoke_test LANGUAGES CXX)

get_filename_component(PHASER_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/../.." ABSOLUTE)
add_subdirectory("${PHASER_ROOT}" phaser)

phaser_add_library(test_phaser
PROTOS phaser/testdata/Test.proto
IMPORTS phaser/testdata/Foo.proto google/protobuf/any.proto
IMPORT_DIRS "${PHASER_ROOT}" "${protobuf_SOURCE_DIR}/src"
ADD_NAMESPACE smoke
)

phaser_add_library(test_phaser_c
PROTOS phaser/testdata/Test.proto
IMPORTS phaser/testdata/Foo.proto google/protobuf/any.proto
IMPORT_DIRS "${PHASER_ROOT}" "${protobuf_SOURCE_DIR}/src"
ADD_NAMESPACE smoke_c
RUST
)

add_executable(smoke_test smoke_test.cc)
target_link_libraries(smoke_test PRIVATE test_phaser test_phaser_c)

find_program(RUSTC_EXECUTABLE rustc)
if(RUSTC_EXECUTABLE)
set(rust_lib "${CMAKE_CURRENT_BINARY_DIR}/rust/lib.rs")
file(GENERATE OUTPUT "${rust_lib}" CONTENT
"#![allow(non_camel_case_types, non_snake_case, non_upper_case_globals, dead_code)]\ninclude!(\"$<TARGET_PROPERTY:test_phaser_c,PHASER_RUST_SOURCE>\");\n")
add_custom_target(smoke_test_rust ALL
COMMAND "${RUSTC_EXECUTABLE}" --edition 2021 --crate-type rlib
--crate-name test_phaser_c
--out-dir "${CMAKE_CURRENT_BINARY_DIR}/rust" "${rust_lib}"
COMMENT "Compiling the Rust bindings for test_phaser_c"
VERBATIM
)
add_dependencies(smoke_test_rust test_phaser_c)
else()
message(STATUS "rustc not found: not compiling the Rust bindings")
endif()

enable_testing()
add_test(NAME smoke_test COMMAND smoke_test)
Loading
Loading