Skip to content
Draft
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
24 changes: 24 additions & 0 deletions score/flatbuffers/bazel/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# *******************************************************************************
# Copyright (c) 2026 Contributors to the Eclipse Foundation
#
# See the NOTICE file(s) distributed with this work for additional
# information regarding copyright ownership.
#
# This program and the accompanying materials are made available under the
# terms of the Apache License Version 2.0 which is available at
# https://www.apache.org/licenses/LICENSE-2.0
#
# SPDX-License-Identifier: Apache-2.0
# *******************************************************************************

load("@rules_python//python:defs.bzl", "py_binary")

py_binary(
name = "inject_buffer_version",
srcs = ["inject_buffer_version.py"],
# "manual" excludes this target from wildcard builds (e.g. //score/...) so it is not
# built directly for a cross-compilation target platform. It is only ever built for the
# exec (host) platform when referenced as a tool via cfg = "exec" in the starlark rules.
tags = ["manual"],
visibility = ["//visibility:public"],
)
27 changes: 24 additions & 3 deletions score/flatbuffers/bazel/codegen.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ def _generate_cpp_impl(ctx):
# Get the flatc compiler using absolute path from flatbuffers repository
flatc = ctx.executable._flatc

# Collect include directories from included .fbs files
include_files = ctx.files.includes + [ctx.file._buffer_version_fbs]
include_dirs = {files.dirname: True for files in include_files}

# flatc generates <basename>_generated.h by default
# Use temporary subdirectory based on target to avoid conflicts
default_name = fbs_file.basename.replace(".fbs", "_generated.h")
Expand Down Expand Up @@ -81,11 +85,13 @@ def _generate_cpp_impl(ctx):
args.add("--cpp")
args.add("--cpp-std", "c++11")
args.add("--scoped-enums")
for inc_dir in include_dirs:
args.add("-I", inc_dir)
args.add("-o", generated_file.dirname)
args.add(fbs_file.path)

ctx.actions.run(
inputs = [fbs_file],
inputs = [fbs_file] + include_files,
outputs = [generated_file],
executable = flatc,
arguments = [args],
Expand All @@ -110,23 +116,38 @@ generate_cpp = rule(
mandatory = True,
doc = "The name of the generated C++ header file",
),
"includes": attr.label_list(
allow_files = [".fbs"],
default = [],
doc = "Additional .fbs files required to resolve include directives in the schema.",
),
"_flatc": attr.label(
default = "@flatbuffers//:flatc",
executable = True,
cfg = "exec",
doc = "The flatc compiler (absolute path from flatbuffers repository)",
),
"_buffer_version_fbs": attr.label(
default = "@score_baselibs//score/flatbuffers/common:buffer_version.fbs",
allow_single_file = [".fbs"],
doc = "Automatically included buffer_version.fbs for common buffer version support.",
),
},
doc = """Generates a C++ header file from a FlatBuffer schema (.fbs) file.

This rule uses the flatc compiler from the @flatbuffers repository with
absolute paths, making it usable outside of this repository.


@score_baselibs//score/flatbuffers/common:buffer_version.fbs is always included
automatically. The schema must include buffer_version.fbs manually if it uses
the common buffer version (e.g. `include "buffer_version.fbs";`).

Example:
generate_cpp(
name = "demo_flatbuffer",
schema = "demo.fbs",
output = "demo_config.h",
includes = ["some_other.fbs"],
)
""",
)
70 changes: 70 additions & 0 deletions score/flatbuffers/bazel/inject_buffer_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/env python3
# *******************************************************************************
# Copyright (c) 2026 Contributors to the Eclipse Foundation
#
# See the NOTICE file(s) distributed with this work for additional
# information regarding copyright ownership.
#
# This program and the accompanying materials are made available under the
# terms of the Apache License Version 2.0 which is available at
# https://www.apache.org/licenses/LICENSE-2.0
#
# SPDX-License-Identifier: Apache-2.0
# *******************************************************************************

"""Patches a JSON data file with buffer version information.

This script is invoked by the serialize_buffer Bazel rule to inject
major_version and minor_version into the user's JSON data file before
flatc serialization. The version is stored as a 'version' object at
the top level of the JSON.

Usage:
inject_buffer_version.py --input <input.json> --output <output.json> \
--major <major_version> --minor <minor_version>
"""

import argparse
import json


def main():
parser = argparse.ArgumentParser(
description="Patch a JSON data file with buffer version information."
)
parser.add_argument("--input", required=True, help="Input JSON file path")
parser.add_argument("--output", required=True, help="Output patched JSON file path")
parser.add_argument(
"--major", type=int, required=True, help="Major version number (0-65535)"
)
parser.add_argument(
"--minor", type=int, required=True, help="Minor version number (0-65535)"
)

args = parser.parse_args()

_UINT16_MAX = 65535
if not (0 <= args.major <= _UINT16_MAX):
parser.error(
f"--major {args.major} is out of uint16_t range [0, {_UINT16_MAX}]"
)
if not (0 <= args.minor <= _UINT16_MAX):
parser.error(
f"--minor {args.minor} is out of uint16_t range [0, {_UINT16_MAX}]"
)

with open(args.input, "r") as f:
data = json.load(f)

data["version_info"] = {
"major_version": args.major,
"minor_version": args.minor,
}

with open(args.output, "w") as f:
json.dump(data, f, indent=2)
f.write("\n")


if __name__ == "__main__":
main()
Loading
Loading