diff --git a/BUILD.bazel b/BUILD.bazel index 5c8e803..88ab07b 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -20,6 +20,6 @@ buildifier( buildifier_test( name = "buildifier.test", - srcs = ["BUILD"], + srcs = ["BUILD.bazel"], lint_mode = "warn", ) diff --git a/docs/rules.md b/docs/rules.md index 3a4dda3..cd4a945 100644 --- a/docs/rules.md +++ b/docs/rules.md @@ -1,16 +1,58 @@ -Public API re-exports - +Rules for building flatbuffers with Bazel. -## example + + + +## default_include_paths
-example()
+default_include_paths()
 
-This is an example + + + +## flatbuffer_library_public + +
+flatbuffer_library_public(name, srcs, outs, language_flag, out_prefix, includes, include_paths,
+                          flatc_args, reflection_name, reflection_visibility, compatible_with,
+                          restricted_to, target_compatible_with, output_to_bindir, extra_env, kwargs)
+
+ +Generates code files for reading/writing the given flatbuffers in the requested language using the public compiler. + +This rule creates a filegroup(name) with all generated source files, and +optionally a Fileset([reflection_name]) with all generated reflection +binaries. + + +**PARAMETERS** + + +| Name | Description | Default Value | +| :------------- | :------------- | :------------- | +| name | Rule name. | none | +| 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]. | None | +| 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. | None | +| flatc_args | Optional, list of additional arguments to pass to flatc. | ["--gen-object-api", "--gen-compare", "--no-includes", "--gen-mutable", "--reflect-names", "--cpp-ptr-type flatbuffers::unique_ptr"] | +| reflection_name | Optional, if set this will generate the flatbuffer reflection binaries for the schemas. | "" | +| reflection_visibility | The visibility of the generated reflection Fileset. | None | +| compatible_with | Optional, The list of environments this rule can be built for, in addition to default-supported environments. | None | +| restricted_to | Optional, The list of environments this rule can be built for, instead of default-supported environments. | None | +| target_compatible_with | Optional, The list of target platform constraints to use. | None | +| output_to_bindir | Passed to genrule for output to bin directory. | False | +| extra_env | Optional, must be a string of "VAR1=VAL1 VAR2=VAL2". These get set as environment variables that "flatc_path" sees. | None | +| kwargs | Passed to the underlying genrule. | none | + + diff --git a/e2e/smoke/.bazelrc b/e2e/smoke/.bazelrc index e69de29..029335a 100644 --- a/e2e/smoke/.bazelrc +++ b/e2e/smoke/.bazelrc @@ -0,0 +1 @@ +build --output_filter=DO_NOT_MATCH_ANYTHING diff --git a/e2e/smoke/BUILD b/e2e/smoke/BUILD.bazel similarity index 78% rename from e2e/smoke/BUILD rename to e2e/smoke/BUILD.bazel index 3e6a3cf..53f7ab9 100644 --- a/e2e/smoke/BUILD +++ b/e2e/smoke/BUILD.bazel @@ -20,6 +20,10 @@ flatbuffer_library_public( name = "monster_fbs_java", srcs = ["monster.fbs"], outs = [ + # Bazel expects that we have the full list of generated files, the rule + # will output to stderr the generated list of files to stderr. If you + # build with --test_output_filter=DO_NOT_MATCH_ANYTHING then you will + # get the list of files when it fails "MyGame/Sample/Color.java", "MyGame/Sample/Equipment.java", "MyGame/Sample/EquipmentUnion.java", diff --git a/e2e/smoke/WORKSPACE.bzlmod b/e2e/smoke/WORKSPACE.bzlmod index 787a467..dc2f63d 100644 --- a/e2e/smoke/WORKSPACE.bzlmod +++ b/e2e/smoke/WORKSPACE.bzlmod @@ -5,7 +5,6 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_jar") http_jar( name = "flatbuffers-java", - integrity = "sha256-9bUANKU969qYCsqAO4sGlJ+TpAFjvB22zWlYHTcY41U=", sha256 = "f5b50034a53debda980aca803b8b06949f93a40163bc1db6cd69581d3718e355", url = "https://repo1.maven.org/maven2/com/google/flatbuffers/flatbuffers-java/24.3.25/flatbuffers-java-24.3.25.jar", ) diff --git a/flatbuffers/BUILD.bazel b/flatbuffers/BUILD.bazel index e2300b5..7bd9950 100644 --- a/flatbuffers/BUILD.bazel +++ b/flatbuffers/BUILD.bazel @@ -32,12 +32,6 @@ bzl_library( ], ) -bzl_library( - name = "defs", - srcs = ["defs.bzl"], - visibility = ["//visibility:public"], -) - bzl_library( name = "extensions", srcs = ["extensions.bzl"], @@ -45,12 +39,6 @@ bzl_library( deps = [":repositories"], ) -bzl_library( - name = "toolchain", - srcs = ["toolchain.bzl"], - visibility = ["//visibility:public"], -) - sh_binary( name = "flatc", srcs = ["flatc.sh"], @@ -60,3 +48,15 @@ sh_binary( }, visibility = ["//visibility:public"], ) + +bzl_library( + name = "defs", + srcs = ["defs.bzl"], + visibility = ["//visibility:public"], +) + +bzl_library( + name = "toolchain", + srcs = ["toolchain.bzl"], + visibility = ["//visibility:public"], +) diff --git a/flatbuffers/defs.bzl b/flatbuffers/defs.bzl index e0d8726..b7a1844 100644 --- a/flatbuffers/defs.bzl +++ b/flatbuffers/defs.bzl @@ -1,5 +1,165 @@ -"Public API re-exports" +# Description: +# BUILD rules for generating flatbuffer files in various languages. +# +# Based on https://github.com/google/flatbuffers/blob/204473cdb58a354506040826b47ef9329dc5c79c/build_defs.bzl -load("//flatbuffers/private:build_defs.bzl", _flatbuffer_library_public = "flatbuffer_library_public") +""" +Rules for building flatbuffers with Bazel. +""" -flatbuffer_library_public = _flatbuffer_library_public +def default_include_paths(): + return [ + "./", + "$(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 = None, + out_prefix = "", + includes = [], + include_paths = None, + flatc_args = DEFAULT_FLATC_ARGS, + reflection_name = "", + reflection_visibility = None, + compatible_with = None, + restricted_to = None, + target_compatible_with = None, + output_to_bindir = False, + extra_env = None, + **kwargs): + """Generates code files for reading/writing the given flatbuffers in the requested language using the public compiler. + + This rule creates a filegroup(name) with all generated source files, and + optionally a Fileset([reflection_name]) with all generated reflection + binaries. + + 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_visibility: The visibility of the generated reflection Fileset. + output_to_bindir: Passed to genrule for output to bin directory. + compatible_with: Optional, The list of environments this rule can be + built for, in addition to default-supported environments. + restricted_to: Optional, The list of environments this rule can be built + for, instead of default-supported environments. + target_compatible_with: Optional, The list of target platform constraints + to use. + output_to_bindir: Passed to genrule for output to bin directory. + extra_env: Optional, must be a string of "VAR1=VAL1 VAR2=VAL2". These get + set as environment variables that "flatc_path" sees. + **kwargs: Passed to the underlying genrule. + """ + + if language_flag == None: + fail("language_flag must be set") + + reflection_include_paths = include_paths + if include_paths == None: + include_paths = default_include_paths() + include_paths_cmd = ["-I %s" % (s) for s in include_paths] + + extra_env = extra_env or "" + + # '$(@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([ + "set -eou pipefail;", + "SRCS=($(SRCS));", + "for f in $${SRCS[@]:0:%s}; do" % len(srcs), + "OUTPUT_FILE=\"$(OUTS)\" %s $(location @com_bookingcom_rules_flatbuffers//flatbuffers:resolved_toolchain)" % (extra_env), + " ".join(include_paths_cmd), + " ".join(flatc_args), + language_flag, + output_directory, + "$$f;", + "done;", + ]) + + genrule_cmd += "; ".join([ + "pushd $(@D)", + "find . -type f | sort | sed 's!\\./!!g' > $$TMPDIR/generated", + "echo $(OUTS) | tr \" \" \"\n\" | sed \"s!$(@D)/!!g\"| sort > $$TMPDIR/expected", + "diff -u $$TMPDIR/expected $$TMPDIR/generated", + ]) + + native.genrule( + name = name, + srcs = srcs + includes, + outs = outs, + output_to_bindir = output_to_bindir, + tools = ["@com_bookingcom_rules_flatbuffers//flatbuffers:resolved_toolchain"], + cmd = genrule_cmd, + compatible_with = compatible_with, + target_compatible_with = target_compatible_with, + restricted_to = restricted_to, + message = "Generating flatbuffer files for %s:" % (name), + **kwargs + ) + if reflection_name: + if reflection_include_paths == None: + reflection_include_paths = default_include_paths() + reflection_include_paths_cmd = ["-I %s" % (s) for s in reflection_include_paths] + reflection_genrule_cmd = " ".join([ + "set -eou pipefail;", + "SRCS=($(SRCS));", + "for f in $${SRCS[@]:0:%s}; do" % len(srcs), + "$(location @com_bookingcom_rules_flatbuffers//flatbuffers:resolved_toolchain)", + "-b --schema", + " ".join(flatc_args), + " ".join(reflection_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, + compatible_with = compatible_with, + restricted_to = restricted_to, + target_compatible_with = target_compatible_with, + cmd = reflection_genrule_cmd, + message = "Generating flatbuffer reflection binary for %s:" % (name), + visibility = reflection_visibility, + tools = ["@com_bookingcom_rules_flatbuffers//flatbuffers:resolved_toolchain"], + ) + native.filegroup( + name = "%s_out" % reflection_name, + srcs = reflection_outs, + visibility = reflection_visibility, + compatible_with = compatible_with, + restricted_to = restricted_to, + ) diff --git a/flatbuffers/private/BUILD.bazel b/flatbuffers/private/BUILD.bazel index 2fcb67c..eccc32c 100644 --- a/flatbuffers/private/BUILD.bazel +++ b/flatbuffers/private/BUILD.bazel @@ -1,19 +1,19 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library") bzl_library( - name = "toolchains_repo", - srcs = ["toolchains_repo.bzl"], + name = "resolved_toolchain", + srcs = ["resolved_toolchain.bzl"], visibility = ["//flatbuffers:__subpackages__"], ) bzl_library( - name = "versions", - srcs = ["versions.bzl"], + name = "toolchains_repo", + srcs = ["toolchains_repo.bzl"], visibility = ["//flatbuffers:__subpackages__"], ) bzl_library( - name = "resolved_toolchain", - srcs = ["resolved_toolchain.bzl"], + name = "versions", + srcs = ["versions.bzl"], visibility = ["//flatbuffers:__subpackages__"], ) diff --git a/flatbuffers/private/build_defs.bzl b/flatbuffers/private/build_defs.bzl deleted file mode 100644 index 7014461..0000000 --- a/flatbuffers/private/build_defs.bzl +++ /dev/null @@ -1,158 +0,0 @@ -# Description: -# BUILD rules for generating flatbuffer files in various languages. -# -# Based on https://github.com/google/flatbuffers/blob/204473cdb58a354506040826b47ef9329dc5c79c/build_defs.bzl - -""" -Rules for building flatbuffers with Bazel. -""" - -def default_include_paths(): - return [ - "./", - "$(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 = None, - out_prefix = "", - includes = [], - include_paths = None, - flatc_args = DEFAULT_FLATC_ARGS, - reflection_name = "", - reflection_visibility = None, - compatible_with = None, - restricted_to = None, - target_compatible_with = None, - output_to_bindir = False, - extra_env = None, - **kwargs): - """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_visibility: The visibility of the generated reflection Fileset. - output_to_bindir: Passed to genrule for output to bin directory. - compatible_with: Optional, The list of environments this rule can be - built for, in addition to default-supported environments. - restricted_to: Optional, The list of environments this rule can be built - for, instead of default-supported environments. - target_compatible_with: Optional, The list of target platform constraints - to use. - output_to_bindir: Passed to genrule for output to bin directory. - extra_env: Optional, must be a string of "VAR1=VAL1 VAR2=VAL2". These get - set as environment variables that "flatc_path" sees. - **kwargs: Passed to the underlying genrule. - - - This rule creates a filegroup(name) with all generated source files, and - optionally a Fileset([reflection_name]) with all generated reflection - binaries. - """ - - if language_flag == None: - fail("language_flag must be set") - - reflection_include_paths = include_paths - if include_paths == None: - include_paths = default_include_paths() - include_paths_cmd = ["-I %s" % (s) for s in include_paths] - - extra_env = extra_env or "" - - # '$(@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([ - "set -eou pipefail;", - "SRCS=($(SRCS));", - "for f in $${SRCS[@]:0:%s}; do" % len(srcs), - "OUTPUT_FILE=\"$(OUTS)\" %s $(location @com_bookingcom_rules_flatbuffers//flatbuffers:resolved_toolchain)" % (extra_env), - " ".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 = ["@com_bookingcom_rules_flatbuffers//flatbuffers:resolved_toolchain"], - cmd = genrule_cmd, - compatible_with = compatible_with, - target_compatible_with = target_compatible_with, - restricted_to = restricted_to, - message = "Generating flatbuffer files for %s:" % (name), - **kwargs - ) - if reflection_name: - if reflection_include_paths == None: - reflection_include_paths = default_include_paths() - reflection_include_paths_cmd = ["-I %s" % (s) for s in reflection_include_paths] - reflection_genrule_cmd = " ".join([ - "set -eou pipefail;", - "SRCS=($(SRCS));", - "for f in $${SRCS[@]:0:%s}; do" % len(srcs), - "$(location @com_bookingcom_rules_flatbuffers//flatbuffers:resolved_toolchain)", - "-b --schema", - " ".join(flatc_args), - " ".join(reflection_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, - compatible_with = compatible_with, - restricted_to = restricted_to, - target_compatible_with = target_compatible_with, - cmd = reflection_genrule_cmd, - message = "Generating flatbuffer reflection binary for %s:" % (name), - visibility = reflection_visibility, - tools = ["@com_bookingcom_rules_flatbuffers//flatbuffers:resolved_toolchain"], - ) - native.filegroup( - name = "%s_out" % reflection_name, - srcs = reflection_outs, - visibility = reflection_visibility, - compatible_with = compatible_with, - restricted_to = restricted_to, - ) diff --git a/flatbuffers/tests/versions_test.bzl b/flatbuffers/tests/versions_test.bzl index e358822..166e1dc 100644 --- a/flatbuffers/tests/versions_test.bzl +++ b/flatbuffers/tests/versions_test.bzl @@ -7,7 +7,7 @@ load("//flatbuffers/private:versions.bzl", "TOOL_VERSIONS") def _smoke_test_impl(ctx): env = unittest.begin(ctx) - asserts.equals(env, "1.14.2", TOOL_VERSIONS.keys()[0]) + asserts.equals(env, "24.3.25", TOOL_VERSIONS.keys()[0]) return unittest.end(env) # The unittest library requires that we export the test cases as named test rules,