diff --git a/BUILD.bazel b/BUILD.bazel index bdbc789..5c8e803 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -1,4 +1,5 @@ load("@bazel_gazelle//:def.bzl", "gazelle", "gazelle_binary") +load("@buildifier_prebuilt//:rules.bzl", "buildifier", "buildifier_test") gazelle_binary( name = "gazelle_bin", @@ -9,3 +10,16 @@ gazelle( name = "gazelle", gazelle = "gazelle_bin", ) + +buildifier( + name = "buildifier", + exclude_patterns = [ + "./.git/*", + ], +) + +buildifier_test( + name = "buildifier.test", + srcs = ["BUILD"], + lint_mode = "warn", +) diff --git a/MODULE.bazel b/MODULE.bazel index d8fcb42..7e817d6 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -8,11 +8,13 @@ module( bazel_dep(name = "bazel_skylib", version = "1.4.1") bazel_dep(name = "platforms", version = "0.0.5") +bazel_dep(name = "bazel_features", version = "1.15.0") +bazel_dep(name = "rules_cc", version = "0.0.9") bazel_dep(name = "gazelle", version = "0.35.0", dev_dependency = True, repo_name = "bazel_gazelle") bazel_dep(name = "bazel_skylib_gazelle_plugin", version = "1.4.1", dev_dependency = True) bazel_dep(name = "aspect_bazel_lib", version = "1.32.1", dev_dependency = True) -bazel_dep(name = "buildifier_prebuilt", version = "6.1.2", dev_dependency = True) +bazel_dep(name = "buildifier_prebuilt", version = "7.3.1", dev_dependency = True) flatbuffers = use_extension("//flatbuffers:extensions.bzl", "flatbuffers") flatbuffers.toolchain(flatbuffers_version = "24.3.25") diff --git a/e2e/smoke/BUILD b/e2e/smoke/BUILD index d5e392e..d6e5123 100644 --- a/e2e/smoke/BUILD +++ b/e2e/smoke/BUILD @@ -3,7 +3,7 @@ Add a basic smoke-test target below. """ load("@bazel_skylib//rules:build_test.bzl", "build_test") -# load("@com_bookingcom_rules_flatbuffers//flatbuffers:defs.bzl", "...") +load("@com_bookingcom_rules_flatbuffers//flatbuffers:defs.bzl", "flatbuffer_library_public") # Replace with a usage of your rule/macro filegroup(name = "empty") @@ -15,3 +15,25 @@ build_test( ":empty", ], ) + +flatbuffer_library_public( + name = "monster_fbs_java", + srcs = ["monster.fbs"], + outs = [ + "MyGame/Sample/Color.java", + "MyGame/Sample/Equipment.java", + "MyGame/Sample/EquipmentUnion.java", + "MyGame/Sample/Monster.java", + "MyGame/Sample/MonsterT.java", + "MyGame/Sample/Vec3.java", + "MyGame/Sample/Vec3T.java", + "MyGame/Sample/Weapon.java", + "MyGame/Sample/WeaponT.java", + ], + language_flag = "--java", +) + +java_library( + name = "monster_java", + srcs = [":monster_fbs_java"], +) diff --git a/e2e/smoke/monster.fbs b/e2e/smoke/monster.fbs new file mode 100644 index 0000000..af22451 --- /dev/null +++ b/e2e/smoke/monster.fbs @@ -0,0 +1,33 @@ +// Example IDL file for our monster's schema. + +namespace MyGame.Sample; + +enum Color:byte { Red = 0, Green, Blue = 2 } + +union Equipment { Weapon } // Optionally add more tables. + +struct Vec3 { + x:float; + y:float; + z:float; +} + +table Monster { + pos:Vec3; + mana:short = 150; + hp:short = 100; + name:string; + friendly:bool = false (deprecated); + inventory:[ubyte]; + color:Color = Blue; + weapons:[Weapon]; + equipped:Equipment; + path:[Vec3]; +} + +table Weapon { + name:string; + damage:short; +} + +root_type Monster; diff --git a/flatbuffers/BUILD.bazel b/flatbuffers/BUILD.bazel index 2a104f3..e2300b5 100644 --- a/flatbuffers/BUILD.bazel +++ b/flatbuffers/BUILD.bazel @@ -58,4 +58,5 @@ sh_binary( env = { "FLATC_BINARY": "$(location :resolved_toolchain)", }, + visibility = ["//visibility:public"], ) diff --git a/flatbuffers/defs.bzl b/flatbuffers/defs.bzl index 7790733..e0d8726 100644 --- a/flatbuffers/defs.bzl +++ b/flatbuffers/defs.bzl @@ -1,7 +1,5 @@ "Public API re-exports" -load("@com_github_google_flatbuffers//:build_defs.bzl", _flatbuffer_library_plubic = "flatbuffer_library_public") +load("//flatbuffers/private:build_defs.bzl", _flatbuffer_library_public = "flatbuffer_library_public") -def flatbuffer_library_plubic(**kwargs): - kwargs.pop("flatc_path", None) - _flatbuffer_library_plubic(flatc_path = "", **kwargs) +flatbuffer_library_public = _flatbuffer_library_public diff --git a/flatbuffers/extensions.bzl b/flatbuffers/extensions.bzl index 0a9582a..e11df54 100644 --- a/flatbuffers/extensions.bzl +++ b/flatbuffers/extensions.bzl @@ -24,6 +24,7 @@ Overriding the default is only permitted in the root module. def _toolchain_extension(module_ctx): registrations = {} + for mod in module_ctx.modules: for toolchain in mod.tags.toolchain: if toolchain.name != _DEFAULT_NAME and not mod.is_root: diff --git a/flatbuffers/flatc.sh b/flatbuffers/flatc.sh index 47efeb5..6806d81 100755 --- a/flatbuffers/flatc.sh +++ b/flatbuffers/flatc.sh @@ -1,3 +1,10 @@ #!/usr/bin/env bash -exec $FLATC_BINARY $@ +set -eou pipefail + +if [ -z "${FLATC_BINARY:-}" ]; then + echo "Please set FLATC_BINARY to the path of the flatc binary." > /dev/stderr + exit 1 +fi + +exec $FLATC_BINARY "$@" diff --git a/flatbuffers/private/build_defs.bzl b/flatbuffers/private/build_defs.bzl new file mode 100644 index 0000000..edc3936 --- /dev/null +++ b/flatbuffers/private/build_defs.bzl @@ -0,0 +1,164 @@ +# 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 -x;", + "set -eou pipefail;", + "pwd;", + "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, + # toolchains = ["@com_bookingcom_rules_flatbuffers//flatbuffers:toolchain"], + 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 -x;", + "set -eou pipefail;", + "pwd", + "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"], + # toolchains = ["@com_bookingcom_rules_flatbuffers//flatbuffers: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/repositories.bzl b/flatbuffers/repositories.bzl index 5a120bd..ac14dfe 100644 --- a/flatbuffers/repositories.bzl +++ b/flatbuffers/repositories.bzl @@ -4,7 +4,7 @@ These are needed for local dev, and users must install them as well. See https://docs.bazel.build/versions/main/skylark/deploying.html#dependencies """ -load("@bazel_tools//tools/build_defs/repo:http.bzl", _http_archive = "http_archive", _http_file = "http_file") +load("@bazel_tools//tools/build_defs/repo:http.bzl", _http_archive = "http_archive") load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe") load("//flatbuffers/private:toolchains_repo.bzl", "PLATFORMS", "toolchains_repo") load("//flatbuffers/private:versions.bzl", "PLATFORMS_MAPPING", "TOOL_VERSIONS") @@ -12,9 +12,6 @@ load("//flatbuffers/private:versions.bzl", "PLATFORMS_MAPPING", "TOOL_VERSIONS") def http_archive(name, **kwargs): maybe(_http_archive, name = name, **kwargs) -def http_file(name, **kwargs): - maybe(_http_file, name = name, **kwargs) - # WARNING: any changes in this function may be BREAKING CHANGES for users # because we'll fetch a dependency which may be different from one that # they were previously fetching later in their WORKSPACE setup, and now @@ -33,14 +30,6 @@ def rules_flatbuffers_dependencies(): ], ) - http_file( - name = "com_github_google_flatbuffers", - urls = [ - "https://raw.githubusercontent.com/google/flatbuffers/master/build_defs.bzl", - ], - downloaded_file_path = "build_defs.bzl", - ) - ######## # Remaining content of the file is only used to support toolchains. ########