Add basic python rule for flatbuffers
Not 100% sure I'm actually going to *use* this now that I've looked at
things more carefully, but it may be helpful for doing some data
analysis stuff.
Change-Id: I695cfe019639617da5baec4d45f5de71ad79b223
diff --git a/build_tests/BUILD b/build_tests/BUILD
index 9dd539a..076e18c 100644
--- a/build_tests/BUILD
+++ b/build_tests/BUILD
@@ -1,5 +1,6 @@
load("@com_google_protobuf//:protobuf.bzl", "cc_proto_library")
load("//tools/cpp/emscripten:defs.bzl", "emcc_binary")
+load("@com_github_google_flatbuffers//:build_defs.bzl", "flatbuffer_python_library")
emcc_binary(
name = "helloworld.html",
@@ -79,3 +80,20 @@
"//aos/testing:googletest",
],
)
+
+flatbuffer_python_library(
+ name = "test_python_fbs",
+ srcs = ["test.fbs"],
+ namespace = "aos.examples",
+ tables = [
+ "Foo",
+ "Bar",
+ ],
+ visibility = ["//visibility:public"],
+)
+
+py_test(
+ name = "python_fbs",
+ srcs = ["python_fbs.py"],
+ deps = [":test_python_fbs"],
+)
diff --git a/build_tests/python_fbs.py b/build_tests/python_fbs.py
new file mode 100644
index 0000000..6345a2d
--- /dev/null
+++ b/build_tests/python_fbs.py
@@ -0,0 +1 @@
+from aos.examples import Foo, Bar
diff --git a/build_tests/test.fbs b/build_tests/test.fbs
new file mode 100644
index 0000000..71db3e7
--- /dev/null
+++ b/build_tests/test.fbs
@@ -0,0 +1,12 @@
+namespace aos.examples;
+
+table Foo {
+ value:int;
+}
+
+table Bar {
+ value:int;
+}
+
+root_type Foo;
+root_type Bar;
diff --git a/third_party/flatbuffers/BUILD b/third_party/flatbuffers/BUILD
index 67a3a14..492f158 100644
--- a/third_party/flatbuffers/BUILD
+++ b/third_party/flatbuffers/BUILD
@@ -231,3 +231,9 @@
"--cpp-ptr-type flatbuffers::unique_ptr",
],
)
+
+py_library(
+ name = "flatpy",
+ srcs = glob(["python/flatbuffers/*.py"]),
+ imports = ["python/"],
+)
diff --git a/third_party/flatbuffers/build_defs.bzl b/third_party/flatbuffers/build_defs.bzl
index 743c666..facb24b 100644
--- a/third_party/flatbuffers/build_defs.bzl
+++ b/third_party/flatbuffers/build_defs.bzl
@@ -243,3 +243,52 @@
visibility = srcs_filegroup_visibility if srcs_filegroup_visibility != None else visibility,
compatible_with = compatible_with,
)
+
+def flatbuffer_python_library(
+ name,
+ srcs,
+ namespace,
+ tables,
+ compatible_with = None,
+ includes = [],
+ include_paths = DEFAULT_INCLUDE_PATHS,
+ flatc_args = DEFAULT_FLATC_ARGS,
+ visibility = None,
+ srcs_filegroup_visibility = None):
+ """Generates a py_library rule for a given flatbuffer definition.
+
+ Args:
+ name: Name of the generated py_library rule.
+ srcs: Source .fbs file(s).
+ namespace: Namespace of the specified flatbuffer schema. Until
+ we make the rules sophisticated enough to figure out what
+ python files will be generated from a given schema, the user
+ has to manually specify this.
+ tables: List of table names--currently, we don't do anything to
+ automatically figure out how to handle the fact that a separate
+ python file will be generated for every table definition, and that
+ we can't know what files will be output until after the file has
+ been parsed. As such, we just force the user to manually specify
+ things.
+ """
+ python_files = ["%s/%s.py" % (namespace.replace('.', '/'), table) for table in tables]
+
+ srcs_lib = "%s_srcs" % (name)
+ flatbuffer_library_public(
+ name = srcs_lib,
+ srcs = srcs,
+ outs = python_files,
+ language_flag = "--python",
+ includes = includes,
+ include_paths = include_paths,
+ flatc_args = flatc_args,
+ compatible_with = compatible_with,
+ )
+ native.py_library(
+ name = name,
+ srcs = python_files,
+ visibility = visibility,
+ compatible_with = compatible_with,
+ imports = ["."],
+ deps = ["@com_github_google_flatbuffers//:flatpy"],
+ )