Skip to content

Commit

Permalink
Fix reflection.fbs import path
Browse files Browse the repository at this point in the history
We need to copy the .fbs files into the package used for .bfbs files.
This is necessary as flatc doesn't provide support to specify the full
output file name for an .fbs file in a different folder.
I tried OUTPUT_FILE env var but this doesn't seem to be honored by
flatc.
  • Loading branch information
mering committed Jan 23, 2025
1 parent 6186629 commit a53e027
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 10 deletions.
26 changes: 24 additions & 2 deletions build_defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,34 @@ def flatbuffer_library_public(
"done",
])
reflection_outs = [
(out_prefix + "%s.bfbs") % (s.replace(".fbs", "").split("/")[-1])
(out_prefix + "%s.bfbs") % (native.package_relative_label(s).name.removesuffix(".fbs"))
for s in srcs
]

# Flatc generates the output file name based on the input basename
# So in order to generate the .bfbs file in the current package
# we need to copy the .fbs files into the package as well
srcs_reflection = []
for s in srcs:
label_src = native.package_relative_label(s)
label_in_current_package = native.package_relative_label(":invalid")
if label_src.repo_name != label_in_current_package.repo_name or label_src.package != label_in_current_package.package:
if not label_src.name.endswith(".fbs"):
fail("Filegroups in srcs are only supported for the current package")
copied_src_label = "copy_" + label_src.name
native.genrule(
name = copied_src_label,
srcs = [s],
outs = [label_src.name],
cmd = "cp $< $@",
)
srcs_reflection.append(copied_src_label)
else:
srcs_reflection.append(s)

native.genrule(
name = "%s_srcs" % reflection_name,
srcs = srcs + includes,
srcs = srcs_reflection + includes,
outs = reflection_outs,
output_to_bindir = output_to_bindir,
tools = [TRUE_FLATC_PATH],
Expand Down
6 changes: 6 additions & 0 deletions reflection/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ filegroup(
visibility = ["//visibility:public"],
)

# Needed by build_defs.bzl to copy the file to the evaluating package
exports_files(
srcs = ["reflection.fbs"],
visibility = ["//visibility:public"],
)

filegroup(
name = "reflection_fbs_schema",
srcs = ["reflection.fbs"],
Expand Down
17 changes: 10 additions & 7 deletions reflection/ts/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
load("//:typescript.bzl", "flatbuffer_ts_library")

genrule(
name = "copy_schema_to_folder",
srcs = ["//reflection:reflection_fbs_schema"],
outs = ["reflection.fbs"],
cmd = "cp $< $@",
)
# Flatc generates the output file name based on the input basename
# So in order to generate reflection/ts/reflection_fbs_schema.bfbs
# we need to copy reflection/reflection.fbs to reflection/ts/reflection.fbs.
# genrule(
# name = "copy_schema_to_folder",
# srcs = ["//reflection:reflection_fbs_schema"],
# outs = ["reflection.fbs"],
# cmd = "cp $< $@",
# )

flatbuffer_ts_library(
name = "reflection_ts_fbs",
srcs = [":reflection.fbs"],
srcs = ["//reflection:reflection.fbs"],
gen_reflections = True,
visibility = ["//visibility:public"],
)
1 change: 1 addition & 0 deletions tests/ts/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ flatbuffer_ts_library(
name = "typescript_ts_fbs",
srcs = ["typescript_keywords.fbs"],
deps = [
"//reflection/ts:reflection_ts_fbs",
"//tests/ts/test_dir:include_ts_fbs",
"//tests/ts/test_dir:typescript_transitive_ts_fbs",
],
Expand Down
2 changes: 1 addition & 1 deletion typescript.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def flatbuffer_ts_library(
reflection binaries for the schemas.
"""
srcs_lib = "%s_srcs" % (name)
out_base = [s.replace(".fbs", "").split("/")[-1].split(":")[-1] for s in srcs]
out_base = [native.package_relative_label(s).name.removesuffix(".fbs") for s in srcs]

if len(srcs) != 1:
fail("flatbuffer_ts_library only supports one .fbs file per target currently.")
Expand Down

0 comments on commit a53e027

Please sign in to comment.