mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-23 22:01:48 +00:00
Add flatbuffer_cc library support (#5061)
* Add flatbuffer_cc library support * Update flags so all the tests pass Tests now all pass! * Modify the tests to use the generated code This should be a simple serialize/deserialize test of the new generated code to make sure the bazel rules are doing something sane. * Use generated monster_test.fb in testing/test.cpp cmake drops it's generated code in tests/monster_test_generated.h Instead of checking that in, let's generate it with bazel. * Make grpc tests depend on monster_test_generated.h * Remove redundant cmake dependency This should address @aardappel's feedback. * Run flatc for Android as well This will fix the last travis.ci failure * Add generated output folder and fix flags * Move flatbuffers_header_build_rules to the library that uses it * Use --cpp-ptr-type to fix android Android was the only target using the STL emulation layer. It needed the --cpp-ptr-type flatbuffers::unique_ptr flag to work. Add it! * Roll back changes to use autogenerated monster_test_generated. Flip tests/test.cpp to use the autogenerated file as well.
This commit is contained in:
committed by
Wouter van Oortmerssen
parent
30c4948541
commit
d56a4055da
25
BUILD
25
BUILD
@@ -12,6 +12,8 @@ exports_files([
|
|||||||
"LICENSE",
|
"LICENSE",
|
||||||
])
|
])
|
||||||
|
|
||||||
|
load(":build_defs.bzl", "flatbuffer_cc_library")
|
||||||
|
|
||||||
# Public flatc library to compile flatbuffer files at runtime.
|
# Public flatc library to compile flatbuffer files at runtime.
|
||||||
cc_library(
|
cc_library(
|
||||||
name = "flatbuffers",
|
name = "flatbuffers",
|
||||||
@@ -88,8 +90,8 @@ cc_binary(
|
|||||||
"src/idl_gen_grpc.cpp",
|
"src/idl_gen_grpc.cpp",
|
||||||
"src/idl_gen_js_ts.cpp",
|
"src/idl_gen_js_ts.cpp",
|
||||||
"src/idl_gen_json_schema.cpp",
|
"src/idl_gen_json_schema.cpp",
|
||||||
"src/idl_gen_lua.cpp",
|
|
||||||
"src/idl_gen_lobster.cpp",
|
"src/idl_gen_lobster.cpp",
|
||||||
|
"src/idl_gen_lua.cpp",
|
||||||
"src/idl_gen_php.cpp",
|
"src/idl_gen_php.cpp",
|
||||||
"src/idl_gen_python.cpp",
|
"src/idl_gen_python.cpp",
|
||||||
"src/idl_gen_rust.cpp",
|
"src/idl_gen_rust.cpp",
|
||||||
@@ -109,6 +111,7 @@ cc_library(
|
|||||||
hdrs = [
|
hdrs = [
|
||||||
"include/flatbuffers/base.h",
|
"include/flatbuffers/base.h",
|
||||||
"include/flatbuffers/flatbuffers.h",
|
"include/flatbuffers/flatbuffers.h",
|
||||||
|
"include/flatbuffers/flexbuffers.h",
|
||||||
"include/flatbuffers/stl_emulation.h",
|
"include/flatbuffers/stl_emulation.h",
|
||||||
"include/flatbuffers/util.h",
|
"include/flatbuffers/util.h",
|
||||||
],
|
],
|
||||||
@@ -130,14 +133,14 @@ cc_test(
|
|||||||
"src/idl_parser.cpp",
|
"src/idl_parser.cpp",
|
||||||
"src/reflection.cpp",
|
"src/reflection.cpp",
|
||||||
"src/util.cpp",
|
"src/util.cpp",
|
||||||
"tests/monster_test_generated.h",
|
"monster_test_generated.h",
|
||||||
"tests/namespace_test/namespace_test1_generated.h",
|
"tests/namespace_test/namespace_test1_generated.h",
|
||||||
"tests/namespace_test/namespace_test2_generated.h",
|
"tests/namespace_test/namespace_test2_generated.h",
|
||||||
"tests/test.cpp",
|
"tests/test.cpp",
|
||||||
"tests/test_builder.h",
|
"tests/test_assert.cpp",
|
||||||
"tests/test_assert.h",
|
"tests/test_assert.h",
|
||||||
"tests/test_builder.cpp",
|
"tests/test_builder.cpp",
|
||||||
"tests/test_assert.cpp",
|
"tests/test_builder.h",
|
||||||
"tests/union_vector/union_vector_generated.h",
|
"tests/union_vector/union_vector_generated.h",
|
||||||
":public_headers",
|
":public_headers",
|
||||||
],
|
],
|
||||||
@@ -155,8 +158,20 @@ cc_test(
|
|||||||
":tests/prototest/test.golden",
|
":tests/prototest/test.golden",
|
||||||
":tests/prototest/test.proto",
|
":tests/prototest/test.proto",
|
||||||
":tests/prototest/test_union.golden",
|
":tests/prototest/test_union.golden",
|
||||||
":tests/union_vector/union_vector.fbs",
|
|
||||||
":tests/unicode_test.json",
|
":tests/unicode_test.json",
|
||||||
|
":tests/union_vector/union_vector.fbs",
|
||||||
],
|
],
|
||||||
includes = ["include/"],
|
includes = ["include/"],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Test bzl rules
|
||||||
|
|
||||||
|
flatbuffer_cc_library(
|
||||||
|
name = "monster_test_cc_fbs",
|
||||||
|
srcs = ["tests/monster_test.fbs"],
|
||||||
|
include_paths = ["tests/include_test"],
|
||||||
|
includes = [
|
||||||
|
"tests/include_test/include_test1.fbs",
|
||||||
|
"tests/include_test/sub/include_test2.fbs",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|||||||
@@ -1,2 +1,11 @@
|
|||||||
workspace(name = "com_github_google_flatbuffers")
|
workspace(name = "com_github_google_flatbuffers")
|
||||||
|
|
||||||
|
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
|
||||||
|
http_archive(
|
||||||
|
name = "io_bazel_rules_go",
|
||||||
|
urls = ["https://github.com/bazelbuild/rules_go/releases/download/0.13.0/rules_go-0.13.0.tar.gz"],
|
||||||
|
sha256 = "ba79c532ac400cefd1859cbc8a9829346aa69e3b99482cd5a54432092cbc3933",
|
||||||
|
)
|
||||||
|
load("@io_bazel_rules_go//go:def.bzl", "go_rules_dependencies", "go_register_toolchains")
|
||||||
|
go_rules_dependencies()
|
||||||
|
go_register_toolchains()
|
||||||
|
|||||||
230
build_defs.bzl
Normal file
230
build_defs.bzl
Normal file
@@ -0,0 +1,230 @@
|
|||||||
|
# Description:
|
||||||
|
# BUILD rules for generating flatbuffer files in various languages.
|
||||||
|
|
||||||
|
flatc_path = "//:flatc"
|
||||||
|
|
||||||
|
DEFAULT_INCLUDE_PATHS = [
|
||||||
|
"./",
|
||||||
|
"$(GENDIR)",
|
||||||
|
"$(BINDIR)",
|
||||||
|
]
|
||||||
|
|
||||||
|
DEFAULT_FLATC_ARGS = [
|
||||||
|
"--gen-object-api",
|
||||||
|
"--gen-compare",
|
||||||
|
"--no-includes",
|
||||||
|
"--gen-mutable",
|
||||||
|
"--reflect-names",
|
||||||
|
"--cpp-ptr-type flatbuffers::unique_ptr",
|
||||||
|
]
|
||||||
|
|
||||||
|
def flatbuffer_library_public(
|
||||||
|
name,
|
||||||
|
srcs,
|
||||||
|
outs,
|
||||||
|
language_flag,
|
||||||
|
out_prefix = "",
|
||||||
|
includes = [],
|
||||||
|
include_paths = DEFAULT_INCLUDE_PATHS,
|
||||||
|
flatc_args = DEFAULT_FLATC_ARGS,
|
||||||
|
reflection_name = "",
|
||||||
|
reflection_visiblity = None,
|
||||||
|
output_to_bindir = False):
|
||||||
|
"""Generates code files for reading/writing the given flatbuffers in the requested language using the public compiler.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
name: Rule name.
|
||||||
|
srcs: Source .fbs files. Sent in order to the compiler.
|
||||||
|
outs: Output files from flatc.
|
||||||
|
language_flag: Target language flag. One of [-c, -j, -js].
|
||||||
|
out_prefix: Prepend this path to the front of all generated files except on
|
||||||
|
single source targets. Usually is a directory name.
|
||||||
|
includes: Optional, list of filegroups of schemas that the srcs depend on.
|
||||||
|
include_paths: Optional, list of paths the includes files can be found in.
|
||||||
|
flatc_args: Optional, list of additional arguments to pass to flatc.
|
||||||
|
reflection_name: Optional, if set this will generate the flatbuffer
|
||||||
|
reflection binaries for the schemas.
|
||||||
|
reflection_visiblity: The visibility of the generated reflection Fileset.
|
||||||
|
output_to_bindir: Passed to genrule for output to bin directory.
|
||||||
|
Outs:
|
||||||
|
filegroup(name): all generated source files.
|
||||||
|
Fileset([reflection_name]): (Optional) all generated reflection binaries.
|
||||||
|
"""
|
||||||
|
include_paths_cmd = ["-I %s" % (s) for s in include_paths]
|
||||||
|
|
||||||
|
# '$(@D)' when given a single source target will give the appropriate
|
||||||
|
# directory. Appending 'out_prefix' is only necessary when given a build
|
||||||
|
# target with multiple sources.
|
||||||
|
output_directory = (
|
||||||
|
("-o $(@D)/%s" % (out_prefix)) if len(srcs) > 1 else ("-o $(@D)")
|
||||||
|
)
|
||||||
|
genrule_cmd = " ".join([
|
||||||
|
"SRCS=($(SRCS));",
|
||||||
|
"for f in $${SRCS[@]:0:%s}; do" % len(srcs),
|
||||||
|
"$(location %s)" % (flatc_path),
|
||||||
|
" ".join(include_paths_cmd),
|
||||||
|
" ".join(flatc_args),
|
||||||
|
language_flag,
|
||||||
|
output_directory,
|
||||||
|
"$$f;",
|
||||||
|
"done",
|
||||||
|
])
|
||||||
|
native.genrule(
|
||||||
|
name = name,
|
||||||
|
srcs = srcs + includes,
|
||||||
|
outs = outs,
|
||||||
|
output_to_bindir = output_to_bindir,
|
||||||
|
tools = [flatc_path],
|
||||||
|
cmd = genrule_cmd,
|
||||||
|
message = "Generating flatbuffer files for %s:" % (name),
|
||||||
|
)
|
||||||
|
if reflection_name:
|
||||||
|
reflection_genrule_cmd = " ".join([
|
||||||
|
"SRCS=($(SRCS));",
|
||||||
|
"for f in $${SRCS[@]:0:%s}; do" % len(srcs),
|
||||||
|
"$(location %s)" % (flatc_path),
|
||||||
|
"-b --schema",
|
||||||
|
" ".join(flatc_args),
|
||||||
|
" ".join(include_paths_cmd),
|
||||||
|
language_flag,
|
||||||
|
output_directory,
|
||||||
|
"$$f;",
|
||||||
|
"done",
|
||||||
|
])
|
||||||
|
reflection_outs = [
|
||||||
|
(out_prefix + "%s.bfbs") % (s.replace(".fbs", "").split("/")[-1])
|
||||||
|
for s in srcs
|
||||||
|
]
|
||||||
|
native.genrule(
|
||||||
|
name = "%s_srcs" % reflection_name,
|
||||||
|
srcs = srcs + includes,
|
||||||
|
outs = reflection_outs,
|
||||||
|
output_to_bindir = output_to_bindir,
|
||||||
|
tools = [flatc_path],
|
||||||
|
cmd = reflection_genrule_cmd,
|
||||||
|
message = "Generating flatbuffer reflection binary for %s:" % (name),
|
||||||
|
)
|
||||||
|
native.Fileset(
|
||||||
|
name = reflection_name,
|
||||||
|
out = "%s_out" % reflection_name,
|
||||||
|
entries = [
|
||||||
|
native.FilesetEntry(files = reflection_outs),
|
||||||
|
],
|
||||||
|
visibility = reflection_visiblity,
|
||||||
|
)
|
||||||
|
|
||||||
|
def flatbuffer_cc_library(
|
||||||
|
name,
|
||||||
|
srcs,
|
||||||
|
srcs_filegroup_name = "",
|
||||||
|
out_prefix = "",
|
||||||
|
includes = [],
|
||||||
|
include_paths = DEFAULT_INCLUDE_PATHS,
|
||||||
|
flatc_args = DEFAULT_FLATC_ARGS,
|
||||||
|
visibility = None,
|
||||||
|
srcs_filegroup_visibility = None,
|
||||||
|
gen_reflections = False):
|
||||||
|
'''A cc_library with the generated reader/writers for the given flatbuffer definitions.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
name: Rule name.
|
||||||
|
srcs: Source .fbs files. Sent in order to the compiler.
|
||||||
|
srcs_filegroup_name: Name of the output filegroup that holds srcs. Pass this
|
||||||
|
filegroup into the `includes` parameter of any other
|
||||||
|
flatbuffer_cc_library that depends on this one's schemas.
|
||||||
|
out_prefix: Prepend this path to the front of all generated files. Usually
|
||||||
|
is a directory name.
|
||||||
|
includes: Optional, list of filegroups of schemas that the srcs depend on.
|
||||||
|
** SEE REMARKS BELOW **
|
||||||
|
include_paths: Optional, list of paths the includes files can be found in.
|
||||||
|
flatc_args: Optional list of additional arguments to pass to flatc
|
||||||
|
(e.g. --gen-mutable).
|
||||||
|
visibility: The visibility of the generated cc_library. By default, use the
|
||||||
|
default visibility of the project.
|
||||||
|
srcs_filegroup_visibility: The visibility of the generated srcs filegroup.
|
||||||
|
By default, use the value of the visibility parameter above.
|
||||||
|
gen_reflections: Optional, if true this will generate the flatbuffer
|
||||||
|
reflection binaries for the schemas.
|
||||||
|
Outs:
|
||||||
|
filegroup([name]_srcs): all generated .h files.
|
||||||
|
filegroup(srcs_filegroup_name if specified, or [name]_includes if not):
|
||||||
|
Other flatbuffer_cc_library's can pass this in for their `includes`
|
||||||
|
parameter, if they depend on the schemas in this library.
|
||||||
|
Fileset([name]_reflection): (Optional) all generated reflection binaries.
|
||||||
|
cc_library([name]): library with sources and flatbuffers deps.
|
||||||
|
|
||||||
|
Remarks:
|
||||||
|
** Because the genrule used to call flatc does not have any trivial way of
|
||||||
|
computing the output list of files transitively generated by includes and
|
||||||
|
--gen-includes (the default) being defined for flatc, the --gen-includes
|
||||||
|
flag will not work as expected. The way around this is to add a dependency
|
||||||
|
to the flatbuffer_cc_library defined alongside the flatc included Fileset.
|
||||||
|
For example you might define:
|
||||||
|
|
||||||
|
flatbuffer_cc_library(
|
||||||
|
name = "my_fbs",
|
||||||
|
srcs = [ "schemas/foo.fbs" ],
|
||||||
|
includes = [ "//third_party/bazz:bazz_fbs_includes" ],
|
||||||
|
)
|
||||||
|
|
||||||
|
In which foo.fbs includes a few files from the Fileset defined at
|
||||||
|
//third_party/bazz:bazz_fbs_includes. When compiling the library that
|
||||||
|
includes foo_generated.h, and therefore has my_fbs as a dependency, it
|
||||||
|
will fail to find any of the bazz *_generated.h files unless you also
|
||||||
|
add bazz's flatbuffer_cc_library to your own dependency list, e.g.:
|
||||||
|
|
||||||
|
cc_library(
|
||||||
|
name = "my_lib",
|
||||||
|
deps = [
|
||||||
|
":my_fbs",
|
||||||
|
"//third_party/bazz:bazz_fbs"
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
|
Happy dependent Flatbuffering!
|
||||||
|
'''
|
||||||
|
output_headers = [
|
||||||
|
(out_prefix + "%s_generated.h") % (s.replace(".fbs", "").split("/")[-1])
|
||||||
|
for s in srcs
|
||||||
|
]
|
||||||
|
reflection_name = "%s_reflection" % name if gen_reflections else ""
|
||||||
|
|
||||||
|
srcs_lib = "%s_srcs" % (name)
|
||||||
|
flatbuffer_library_public(
|
||||||
|
name = srcs_lib,
|
||||||
|
srcs = srcs,
|
||||||
|
outs = output_headers,
|
||||||
|
language_flag = "-c",
|
||||||
|
out_prefix = out_prefix,
|
||||||
|
includes = includes,
|
||||||
|
include_paths = include_paths,
|
||||||
|
flatc_args = flatc_args,
|
||||||
|
reflection_name = reflection_name,
|
||||||
|
reflection_visiblity = visibility,
|
||||||
|
)
|
||||||
|
native.cc_library(
|
||||||
|
name = name,
|
||||||
|
hdrs = [
|
||||||
|
":" + srcs_lib,
|
||||||
|
],
|
||||||
|
srcs = [
|
||||||
|
":" + srcs_lib,
|
||||||
|
],
|
||||||
|
features = [
|
||||||
|
"-parse_headers",
|
||||||
|
],
|
||||||
|
deps = [
|
||||||
|
"//:runtime_cc",
|
||||||
|
],
|
||||||
|
includes = [],
|
||||||
|
linkstatic = 1,
|
||||||
|
visibility = visibility,
|
||||||
|
)
|
||||||
|
|
||||||
|
# A filegroup for the `srcs`. That is, all the schema files for this
|
||||||
|
# Flatbuffer set.
|
||||||
|
native.filegroup(
|
||||||
|
name = srcs_filegroup_name if srcs_filegroup_name else "%s_includes" % (name),
|
||||||
|
srcs = srcs,
|
||||||
|
visibility = srcs_filegroup_visibility if srcs_filegroup_visibility != None else visibility,
|
||||||
|
)
|
||||||
Reference in New Issue
Block a user