diff --git a/.bcr/metadata.template.json b/.bcr/metadata.template.json index 003a961..edb8104 100644 --- a/.bcr/metadata.template.json +++ b/.bcr/metadata.template.json @@ -1,7 +1,7 @@ { - "homepage": "https://github.com/myorg/rules_mylang", + "homepage": "https://github.com/bookingcom/rules_flatbuffers", "maintainers": [], - "repository": ["github:myorg/rules_mylang"], + "repository": ["github:bookingcom/rules_flatbuffers"], "versions": [], "yanked_versions": {} } diff --git a/.gitattributes b/.gitattributes index 36d720c..3ce544b 100644 --- a/.gitattributes +++ b/.gitattributes @@ -11,4 +11,4 @@ examples export-ignore # Occasionally there's a need to "stamp" the release version into a file # See https://blog.aspect.build/releasing-bazel-rulesets-rust for details if you need this. -# mylang/version.bzl export-subst +# flatbuffers/version.bzl export-subst diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 32f924d..2d6e5d9 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -15,4 +15,4 @@ jobs: release: uses: bazel-contrib/.github/.github/workflows/release_ruleset.yaml@v6 with: - release_files: rules_mylang-*.tar.gz + release_files: rules_flatbuffers-*.tar.gz diff --git a/.github/workflows/release_prep.sh b/.github/workflows/release_prep.sh index 135e882..6f339aa 100755 --- a/.github/workflows/release_prep.sh +++ b/.github/workflows/release_prep.sh @@ -8,8 +8,8 @@ TAG=${GITHUB_REF_NAME} # The prefix is chosen to match what GitHub generates for source archives # This guarantees that users can easily switch from a released artifact to a source archive # with minimal differences in their code (e.g. strip_prefix remains the same) -PREFIX="rules_mylang-${TAG:1}" -ARCHIVE="rules_mylang-$TAG.tar.gz" +PREFIX="rules_flatbuffers-${TAG:1}" +ARCHIVE="rules_flatbuffers-$TAG.tar.gz" # NB: configuration for 'git archive' is in /.gitattributes git archive --format=tar --prefix=${PREFIX}/ ${TAG} | gzip > $ARCHIVE @@ -22,7 +22,7 @@ cat << EOF 2. Add to your \`MODULE.bazel\` file: \`\`\`starlark -bazel_dep(name = "com_myorg_rules_mylang", version = "${TAG:1}") +bazel_dep(name = "com_bookingcom_rules_flatbuffers", version = "${TAG:1}") \`\`\` ## Using WORKSPACE @@ -32,12 +32,12 @@ Paste this snippet into your \`WORKSPACE.bazel\` file: \`\`\`starlark load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") http_archive( - name = "com_myorg_rules_mylang", + name = "com_bookingcom_rules_flatbuffers", sha256 = "${SHA}", strip_prefix = "${PREFIX}", - url = "https://github.com/myorg/rules_mylang/releases/download/${TAG}/${ARCHIVE}", + url = "https://github.com/bookingcom/rules_flatbuffers/releases/download/${TAG}/${ARCHIVE}", ) EOF awk 'f;/--SNIP--/{f=1}' e2e/smoke/WORKSPACE.bazel -echo "\`\`\`" +echo "\`\`\`" diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 3d755de..c244c62 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -28,7 +28,7 @@ Run `bazel run //:gazelle` to keep them up-to-date. ## Using this as a development dependency of other rules You'll commonly find that you develop in another WORKSPACE, such as -some other ruleset that depends on rules_mylang, or in a nested +some other ruleset that depends on rules_flatbuffers, or in a nested WORKSPACE in the integration_tests folder. To always tell Bazel to use this directory rather than some release @@ -36,11 +36,11 @@ artifact or a version fetched from the internet, run this from this directory: ```sh -OVERRIDE="--override_repository=rules_mylang=$(pwd)/rules_mylang" +OVERRIDE="--override_repository=rules_flatbuffers=$(pwd)/rules_flatbuffers" echo "common $OVERRIDE" >> ~/.bazelrc ``` -This means that any usage of `@rules_mylang` on your system will point to this folder. +This means that any usage of `@rules_flatbuffers` on your system will point to this folder. ## Releasing diff --git a/MODULE.bazel b/MODULE.bazel index 9884c03..b62ed25 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -1,7 +1,7 @@ "Bazel dependencies" module( - name = "com_myorg_rules_mylang", + name = "com_bookingcom_rules_flatbuffers", version = "0.0.0", compatibility_level = 1, ) @@ -14,8 +14,8 @@ bazel_dep(name = "bazel_skylib_gazelle_plugin", version = "1.4.1", dev_dependenc 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) -mylang = use_extension("//mylang:extensions.bzl", "mylang") -mylang.toolchain(mylang_version = "1.14.2") -use_repo(mylang, "mylang_toolchains") +flatbuffers = use_extension("//flatbuffers:extensions.bzl", "flatbuffers") +flatbuffers.toolchain(flatbuffers_version = "1.14.2") +use_repo(flatbuffers, "flatbuffers_toolchains") -register_toolchains("@mylang_toolchains//:all") +register_toolchains("@flatbuffers_toolchains//:all") diff --git a/README.md b/README.md index 32c657a..4bd6bcb 100644 --- a/README.md +++ b/README.md @@ -1,51 +1,17 @@ -# Template for Bazel rules - -Copy this template to create a Bazel ruleset. - -Features: - -- follows the official style guide at https://bazel.build/rules/deploying -- allows for both WORKSPACE.bazel and bzlmod (MODULE.bazel) usage -- includes Bazel formatting as a pre-commit hook (using [buildifier]) -- includes stardoc API documentation generator -- includes typical toolchain setup -- CI configured with GitHub Actions -- release using GitHub Actions just by pushing a tag -- the release artifact doesn't need to be built by Bazel, but can still exclude files and stamp the version - -Ready to get started? Copy this repo, then - -1. search for "com_myorg_rules_mylang" and replace with the name you'll use for your workspace -1. search for "myorg" and replace with GitHub org -1. search for "mylang", "Mylang", "MYLANG" and replace with the language/tool your rules are for -1. rename directory "mylang" similarly -1. run `pre-commit install` to get lints (see CONTRIBUTING.md) -1. if you don't need to fetch platform-dependent tools, then remove anything toolchain-related. -1. (optional) install the [Renovate app](https://github.com/apps/renovate) to get auto-PRs to keep the dependencies up-to-date. -1. delete this section of the README (everything up to the SNIP). - -Optional: if you write tools for your rules to call, you should avoid toolchain dependencies for those tools leaking to all users. -For example, https://github.com/aspect-build/rules_py actions rely on a couple of binaries written in Rust, but we don't want users to be forced to -fetch a working Rust toolchain. Instead we want to ship pre-built binaries on our GH releases, and the ruleset fetches these as toolchains. -See https://blog.aspect.build/releasing-bazel-rulesets-rust for information on how to do this. -Note that users who *do* want to build tools from source should still be able to do so, they just need to register a different toolchain earlier. - ----- SNIP ---- - -# Bazel rules for mylang +# Bazel rules for flatbuffers ## Installation From the release you wish to use: - + copy the WORKSPACE snippet into your `WORKSPACE` file. To use a commit rather than a release, you can point at any SHA of the repo. For example to use commit `abc123`: -1. Replace `url = "https://github.com/myorg/rules_mylang/releases/download/v0.1.0/rules_mylang-v0.1.0.tar.gz"` with a GitHub-provided source archive like `url = "https://github.com/myorg/rules_mylang/archive/abc123.tar.gz"` -1. Replace `strip_prefix = "rules_mylang-0.1.0"` with `strip_prefix = "rules_mylang-abc123"` +1. Replace `url = "https://github.com/bookingcom/rules_flatbuffers/releases/download/v0.1.0/rules_flatbuffers-v0.1.0.tar.gz"` with a GitHub-provided source archive like `url = "https://github.com/bookingcom/rules_flatbuffers/archive/abc123.tar.gz"` +1. Replace `strip_prefix = "rules_flatbuffers-0.1.0"` with `strip_prefix = "rules_flatbuffers-abc123"` 1. Update the `sha256`. The easiest way to do this is to comment out the line, then Bazel will print a message with the correct value. Note that GitHub source archives don't have a strong guarantee on the sha256 stability, see diff --git a/docs/BUILD.bazel b/docs/BUILD.bazel index 99c2ed5..d636cc1 100644 --- a/docs/BUILD.bazel +++ b/docs/BUILD.bazel @@ -4,7 +4,7 @@ load("@aspect_bazel_lib//lib:docs.bzl", "stardoc_with_diff_test", "update_docs") stardoc_with_diff_test( name = "rules", - bzl_library_target = "//mylang:defs", + bzl_library_target = "//flatbuffers:defs", ) update_docs(name = "update") diff --git a/e2e/smoke/BUILD b/e2e/smoke/BUILD index 31dfb97..d5e392e 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_myorg_rules_mylang//mylang:defs.bzl", "...") +# load("@com_bookingcom_rules_flatbuffers//flatbuffers:defs.bzl", "...") # Replace with a usage of your rule/macro filegroup(name = "empty") diff --git a/e2e/smoke/MODULE.bazel b/e2e/smoke/MODULE.bazel index 742eceb..8411d49 100644 --- a/e2e/smoke/MODULE.bazel +++ b/e2e/smoke/MODULE.bazel @@ -1,7 +1,7 @@ -bazel_dep(name = "com_myorg_rules_mylang", version = "0.0.0", dev_dependency = True) +bazel_dep(name = "com_bookingcom_rules_flatbuffers", version = "0.0.0", dev_dependency = True) bazel_dep(name = "bazel_skylib", version = "1.7.1", dev_dependency = True) local_path_override( - module_name = "com_myorg_rules_mylang", + module_name = "com_bookingcom_rules_flatbuffers", path = "../..", ) diff --git a/e2e/smoke/README.md b/e2e/smoke/README.md index 3135480..1a78a88 100644 --- a/e2e/smoke/README.md +++ b/e2e/smoke/README.md @@ -1,5 +1,5 @@ # smoke test This e2e exercises the repo from an end-users perpective. -It catches mistakes in our install instructions, or usages that fail when called from an "external" repository to rules_mylang. +It catches mistakes in our install instructions, or usages that fail when called from an "external" repository to rules_flatbuffers. It is also used by the presubmit check for the Bazel Central Registry. diff --git a/e2e/smoke/WORKSPACE.bazel b/e2e/smoke/WORKSPACE.bazel index e487105..387690b 100644 --- a/e2e/smoke/WORKSPACE.bazel +++ b/e2e/smoke/WORKSPACE.bazel @@ -1,19 +1,24 @@ # Override http_archive for local testing local_repository( - name = "com_myorg_rules_mylang", + name = "com_bookingcom_rules_flatbuffers", path = "../..", ) #---SNIP--- Below here is re-used in the workspace snippet published on releases ###################### -# rules_mylang setup # +# rules_flatbuffers setup # ###################### -# Fetches the rules_mylang dependencies. +# Fetches the rules_flatbuffers dependencies. # If you want to have a different version of some dependency, # you should fetch it *before* calling this. # Alternatively, you can skip calling this function, so long as you've # already fetched all the dependencies. -load("@com_myorg_rules_mylang//mylang:repositories.bzl", "rules_mylang_dependencies") +load("@com_bookingcom_rules_flatbuffers//flatbuffers:repositories.bzl", "flatbuffers_register_toolchains", "rules_flatbuffers_dependencies") -rules_mylang_dependencies() +rules_flatbuffers_dependencies() + +flatbuffers_register_toolchains( + "flatbuffers", + flatbuffers_version = "24.3.25", +) diff --git a/mylang/BUILD.bazel b/flatbuffers/BUILD.bazel similarity index 78% rename from mylang/BUILD.bazel rename to flatbuffers/BUILD.bazel index 27ac1af..2a104f3 100644 --- a/mylang/BUILD.bazel +++ b/flatbuffers/BUILD.bazel @@ -1,5 +1,5 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library") -load("//mylang/private:resolved_toolchain.bzl", "resolved_toolchain") +load("//flatbuffers/private:resolved_toolchain.bzl", "resolved_toolchain") # For stardoc to reference the files exports_files(["defs.bzl"]) @@ -25,8 +25,8 @@ bzl_library( srcs = ["repositories.bzl"], visibility = ["//visibility:public"], deps = [ - "//mylang/private:toolchains_repo", - "//mylang/private:versions", + "//flatbuffers/private:toolchains_repo", + "//flatbuffers/private:versions", "@bazel_tools//tools/build_defs/repo:http.bzl", "@bazel_tools//tools/build_defs/repo:utils.bzl", ], @@ -50,3 +50,12 @@ bzl_library( srcs = ["toolchain.bzl"], visibility = ["//visibility:public"], ) + +sh_binary( + name = "flatc", + srcs = ["flatc.sh"], + data = [":resolved_toolchain"], + env = { + "FLATC_BINARY": "$(location :resolved_toolchain)", + }, +) diff --git a/flatbuffers/defs.bzl b/flatbuffers/defs.bzl new file mode 100644 index 0000000..7790733 --- /dev/null +++ b/flatbuffers/defs.bzl @@ -0,0 +1,7 @@ +"Public API re-exports" + +load("@com_github_google_flatbuffers//:build_defs.bzl", _flatbuffer_library_plubic = "flatbuffer_library_public") + +def flatbuffer_library_plubic(**kwargs): + kwargs.pop("flatc_path", None) + _flatbuffer_library_plubic(flatc_path = "", **kwargs) diff --git a/mylang/extensions.bzl b/flatbuffers/extensions.bzl similarity index 63% rename from mylang/extensions.bzl rename to flatbuffers/extensions.bzl index e2ae7d0..0a9582a 100644 --- a/mylang/extensions.bzl +++ b/flatbuffers/extensions.bzl @@ -1,25 +1,25 @@ """Extensions for bzlmod. -Installs a mylang toolchain. -Every module can define a toolchain version under the default name, "mylang". +Installs a flatbuffers toolchain. +Every module can define a toolchain version under the default name, "flatbuffers". The latest of those versions will be selected (the rest discarded), -and will always be registered by rules_mylang. +and will always be registered by rules_flatbuffers. Additionally, the root module can define arbitrarily many more toolchain versions under different names (the latest version will be picked for each name) and can register them as it sees fit, effectively overriding the default named toolchain due to toolchain resolution precedence. """ -load(":repositories.bzl", "mylang_register_toolchains") +load(":repositories.bzl", "flatbuffers_register_toolchains") -_DEFAULT_NAME = "mylang" +_DEFAULT_NAME = "flatbuffers" -mylang_toolchain = tag_class(attrs = { +flatbuffers_toolchain = tag_class(attrs = { "name": attr.string(doc = """\ -Base name for generated repositories, allowing more than one mylang toolchain to be registered. +Base name for generated repositories, allowing more than one flatbuffers toolchain to be registered. Overriding the default is only permitted in the root module. """, default = _DEFAULT_NAME), - "mylang_version": attr.string(doc = "Explicit version of mylang.", mandatory = True), + "flatbuffers_version": attr.string(doc = "Explicit version of flatbuffers.", mandatory = True), }) def _toolchain_extension(module_ctx): @@ -28,29 +28,29 @@ def _toolchain_extension(module_ctx): for toolchain in mod.tags.toolchain: if toolchain.name != _DEFAULT_NAME and not mod.is_root: fail("""\ - Only the root module may override the default name for the mylang toolchain. + Only the root module may override the default name for the flatbuffers toolchain. This prevents conflicting registrations in the global namespace of external repos. """) if toolchain.name not in registrations.keys(): registrations[toolchain.name] = [] - registrations[toolchain.name].append(toolchain.mylang_version) + registrations[toolchain.name].append(toolchain.flatbuffers_version) for name, versions in registrations.items(): if len(versions) > 1: # TODO: should be semver-aware, using MVS selected = sorted(versions, reverse = True)[0] # buildifier: disable=print - print("NOTE: mylang toolchain {} has multiple versions {}, selected {}".format(name, versions, selected)) + print("NOTE: flatbuffers toolchain {} has multiple versions {}, selected {}".format(name, versions, selected)) else: selected = versions[0] - mylang_register_toolchains( + flatbuffers_register_toolchains( name = name, - mylang_version = selected, + flatbuffers_version = selected, register = False, ) -mylang = module_extension( +flatbuffers = module_extension( implementation = _toolchain_extension, - tag_classes = {"toolchain": mylang_toolchain}, + tag_classes = {"toolchain": flatbuffers_toolchain}, ) diff --git a/flatbuffers/flatc.sh b/flatbuffers/flatc.sh new file mode 100755 index 0000000..47efeb5 --- /dev/null +++ b/flatbuffers/flatc.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +exec $FLATC_BINARY $@ diff --git a/mylang/private/BUILD.bazel b/flatbuffers/private/BUILD.bazel similarity index 65% rename from mylang/private/BUILD.bazel rename to flatbuffers/private/BUILD.bazel index eb2edb6..2fcb67c 100644 --- a/mylang/private/BUILD.bazel +++ b/flatbuffers/private/BUILD.bazel @@ -3,17 +3,17 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library") bzl_library( name = "toolchains_repo", srcs = ["toolchains_repo.bzl"], - visibility = ["//mylang:__subpackages__"], + visibility = ["//flatbuffers:__subpackages__"], ) bzl_library( name = "versions", srcs = ["versions.bzl"], - visibility = ["//mylang:__subpackages__"], + visibility = ["//flatbuffers:__subpackages__"], ) bzl_library( name = "resolved_toolchain", srcs = ["resolved_toolchain.bzl"], - visibility = ["//mylang:__subpackages__"], + visibility = ["//flatbuffers:__subpackages__"], ) diff --git a/mylang/private/resolved_toolchain.bzl b/flatbuffers/private/resolved_toolchain.bzl similarity index 80% rename from mylang/private/resolved_toolchain.bzl rename to flatbuffers/private/resolved_toolchain.bzl index 20728fd..9f14105 100644 --- a/mylang/private/resolved_toolchain.bzl +++ b/flatbuffers/private/resolved_toolchain.bzl @@ -9,11 +9,11 @@ Workaround for https://github.com/bazelbuild/bazel/issues/14009 # Forward all the providers def _resolved_toolchain_impl(ctx): - toolchain_info = ctx.toolchains["//mylang:toolchain_type"] + toolchain_info = ctx.toolchains["//flatbuffers:toolchain_type"] return [ toolchain_info, toolchain_info.default, - toolchain_info.mylanginfo, + toolchain_info.flatbuffersinfo, toolchain_info.template_variables, ] @@ -21,6 +21,6 @@ def _resolved_toolchain_impl(ctx): # https://cs.opensource.google/bazel/bazel/+/master:tools/jdk/java_toolchain_alias.bzl resolved_toolchain = rule( implementation = _resolved_toolchain_impl, - toolchains = ["//mylang:toolchain_type"], + toolchains = ["//flatbuffers:toolchain_type"], doc = DOC, ) diff --git a/mylang/private/toolchains_repo.bzl b/flatbuffers/private/toolchains_repo.bzl similarity index 91% rename from mylang/private/toolchains_repo.bzl rename to flatbuffers/private/toolchains_repo.bzl index 49a3d07..592ead5 100644 --- a/mylang/private/toolchains_repo.bzl +++ b/flatbuffers/private/toolchains_repo.bzl @@ -50,7 +50,7 @@ def _toolchains_repo_impl(repository_ctx): build_content = """# Generated by toolchains_repo.bzl # # These can be registered in the workspace file or passed to --extra_toolchains flag. -# By default all these toolchains are registered by the mylang_register_toolchains macro +# By default all these toolchains are registered by the flatbuffers_register_toolchains macro # so you don't normally need to interact with these targets. """ @@ -62,8 +62,8 @@ def _toolchains_repo_impl(repository_ctx): toolchain( name = "{platform}_toolchain", exec_compatible_with = {compatible_with}, - toolchain = "@{user_repository_name}_{platform}//:mylang_toolchain", - toolchain_type = "@com_myorg_rules_mylang//mylang:toolchain_type", + toolchain = "@{user_repository_name}_{platform}//:flatbuffers_toolchain", + toolchain_type = "@com_bookingcom_rules_flatbuffers//flatbuffers:toolchain_type", ) """.format( platform = platform, diff --git a/flatbuffers/private/versions.bzl b/flatbuffers/private/versions.bzl new file mode 100644 index 0000000..4c568a9 --- /dev/null +++ b/flatbuffers/private/versions.bzl @@ -0,0 +1,20 @@ +"""Prebuilts for flatc +""" + +# The integrity hashes can be computed with +# shasum -b -a 384 [downloaded file] | awk '{ print $1 }' | xxd -r -p | base64 +TOOL_VERSIONS = { + "24.3.25": { + "x86_64-apple-darwin": "", # "sha384-ws4+rANvv0YxM1SgIBUXSG9jT8dKw83nls6R5qYkEKzPUB+viBIEozSsyq2e6i+f", + "aarch64-apple-darwin": "", #"sha384-HcvJbxoJtGSavkGu0e7CyD00cBlmDb0TBWJ4JSaNa70zuU3N7XlMOYm3bbQcAv2U", + "x86_64-pc-windows-msvc": "", #"sha384-35YN6TKpT0L9qyRBmq48NucvyXEtHnkeC+txf2YZmmJTmOzrAKREA74BA0EZvpar", + "x86_64-unknown-linux-gnu": "", #"sha384-QgGOwTaetxY0h5HWCKc/3ZtBs4N/fgaaORthn7UcEv++Idm9W+ntCCZRwvBdwHPD", + }, +} + +PLATFORMS_MAPPING = { + "x86_64-unknown-linux-gnu": "Linux.flatc.binary.clang++-15", + "x86_64-apple-darwin": "MacIntel.flatc.binary", + "aarch64-apple-darwin": "Mac.flatc.binary", + "x86_64-pc-windows-msvc": "Windows.flatc.binary", +} diff --git a/mylang/repositories.bzl b/flatbuffers/repositories.bzl similarity index 58% rename from mylang/repositories.bzl rename to flatbuffers/repositories.bzl index a0252b0..70eea5f 100644 --- a/mylang/repositories.bzl +++ b/flatbuffers/repositories.bzl @@ -4,14 +4,17 @@ 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") +load("@bazel_tools//tools/build_defs/repo:http.bzl", _http_archive = "http_archive", _http_file = "http_file") load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe") -load("//mylang/private:toolchains_repo.bzl", "PLATFORMS", "toolchains_repo") -load("//mylang/private:versions.bzl", "TOOL_VERSIONS") +load("//flatbuffers/private:toolchains_repo.bzl", "PLATFORMS", "toolchains_repo") +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 @@ -19,7 +22,7 @@ def http_archive(name, **kwargs): # changes in this function should be marked as BREAKING in the commit message # and released only in semver majors. # This is all fixed by bzlmod, so we just tolerate it for now. -def rules_mylang_dependencies(): +def rules_flatbuffers_dependencies(): # The minimal version of bazel_skylib we require http_archive( name = "bazel_skylib", @@ -30,32 +33,41 @@ def rules_mylang_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. ######## -_DOC = "Fetch external tools needed for mylang toolchain" +_DOC = "Fetch external tools needed for flatbuffers toolchain" _ATTRS = { - "mylang_version": attr.string(mandatory = True, values = TOOL_VERSIONS.keys()), + "flatbuffers_version": attr.string(mandatory = True, values = TOOL_VERSIONS.keys()), "platform": attr.string(mandatory = True, values = PLATFORMS.keys()), } -def _mylang_repo_impl(repository_ctx): - url = "https://github.com/someorg/someproject/releases/download/v{0}/mylang-{1}.zip".format( - repository_ctx.attr.mylang_version, - repository_ctx.attr.platform, +def _flatbuffers_repo_impl(repository_ctx): + url = "https://github.com/google/flatbuffers/releases/download/v{0}/{1}.zip".format( + repository_ctx.attr.flatbuffers_version, + PLATFORMS_MAPPING.get(repository_ctx.attr.platform), ) repository_ctx.download_and_extract( url = url, - integrity = TOOL_VERSIONS[repository_ctx.attr.mylang_version][repository_ctx.attr.platform], + #integrity = TOOL_VERSIONS[repository_ctx.attr.flatbuffers_version][repository_ctx.attr.platform], + sha256 = TOOL_VERSIONS[repository_ctx.attr.flatbuffers_version][repository_ctx.attr.platform], ) - build_content = """# Generated by mylang/repositories.bzl -load("@com_myorg_rules_mylang//mylang:toolchain.bzl", "mylang_toolchain") + build_content = """# Generated by flatbuffers/repositories.bzl +load("@com_bookingcom_rules_flatbuffers//flatbuffers:toolchain.bzl", "flatbuffers_toolchain") -mylang_toolchain( - name = "mylang_toolchain", +flatbuffers_toolchain( + name = "flatbuffers_toolchain", target_tool = select({ - "@bazel_tools//src/conditions:host_windows": "mylang_tool.exe", - "//conditions:default": "mylang_tool", + "@bazel_tools//src/conditions:host_windows": "flatc.exe", + "//conditions:default": "flatc", }), ) """ @@ -63,29 +75,29 @@ mylang_toolchain( # Base BUILD file for this repository repository_ctx.file("BUILD.bazel", build_content) -mylang_repositories = repository_rule( - _mylang_repo_impl, +flatbuffers_repositories = repository_rule( + _flatbuffers_repo_impl, doc = _DOC, attrs = _ATTRS, ) # Wrapper macro around everything above, this is the primary API -def mylang_register_toolchains(name, register = True, **kwargs): +def flatbuffers_register_toolchains(name, register = True, **kwargs): """Convenience macro for users which does typical setup. - - create a repository for each built-in platform like "mylang_linux_amd64" - - TODO: create a convenience repository for the host platform like "mylang_host" - - create a repository exposing toolchains for each platform like "mylang_platforms" + - create a repository for each built-in platform like "flatbuffers_linux_amd64" + - TODO: create a convenience repository for the host platform like "flatbuffers_host" + - create a repository exposing toolchains for each platform like "flatbuffers_platforms" - register a toolchain pointing at each platform Users can avoid this macro and do these steps themselves, if they want more control. Args: - name: base name for all created repos, like "mylang1_14" + name: base name for all created repos, like "flatbuffers1_14" register: whether to call through to native.register_toolchains. Should be True for WORKSPACE users, but false when used under bzlmod extension - **kwargs: passed to each mylang_repositories call + **kwargs: passed to each flatbuffers_repositories call """ for platform in PLATFORMS.keys(): - mylang_repositories( + flatbuffers_repositories( name = name + "_" + platform, platform = platform, **kwargs diff --git a/mylang/tests/BUILD.bazel b/flatbuffers/tests/BUILD.bazel similarity index 100% rename from mylang/tests/BUILD.bazel rename to flatbuffers/tests/BUILD.bazel diff --git a/mylang/tests/versions_test.bzl b/flatbuffers/tests/versions_test.bzl similarity index 90% rename from mylang/tests/versions_test.bzl rename to flatbuffers/tests/versions_test.bzl index 7a9a988..e358822 100644 --- a/mylang/tests/versions_test.bzl +++ b/flatbuffers/tests/versions_test.bzl @@ -3,7 +3,7 @@ See https://bazel.build/rules/testing#testing-starlark-utilities """ load("@bazel_skylib//lib:unittest.bzl", "asserts", "unittest") -load("//mylang/private:versions.bzl", "TOOL_VERSIONS") +load("//flatbuffers/private:versions.bzl", "TOOL_VERSIONS") def _smoke_test_impl(ctx): env = unittest.begin(ctx) diff --git a/mylang/toolchain.bzl b/flatbuffers/toolchain.bzl similarity index 87% rename from mylang/toolchain.bzl rename to flatbuffers/toolchain.bzl index cfa816c..94f5e84 100644 --- a/mylang/toolchain.bzl +++ b/flatbuffers/toolchain.bzl @@ -1,7 +1,7 @@ """This module implements the language-specific toolchain rule. """ -MylangInfo = provider( +FlatBuffersInfo = provider( doc = "Information about how to invoke the tool executable.", fields = { "target_tool_path": "Path to the tool executable for the target platform.", @@ -18,7 +18,7 @@ def _to_manifest_path(ctx, file): else: return ctx.workspace_name + "/" + file.short_path -def _mylang_toolchain_impl(ctx): +def _flatbuffers_toolchain_impl(ctx): if ctx.attr.target_tool and ctx.attr.target_tool_path: fail("Can only set one of target_tool or target_tool_path but both were set.") if not ctx.attr.target_tool and not ctx.attr.target_tool_path: @@ -34,13 +34,13 @@ def _mylang_toolchain_impl(ctx): # Make the $(tool_BIN) variable available in places like genrules. # See https://docs.bazel.build/versions/main/be/make-variables.html#custom_variables template_variables = platform_common.TemplateVariableInfo({ - "MYLANG_BIN": target_tool_path, + "FLATBUFFERS_BIN": target_tool_path, }) default = DefaultInfo( files = depset(tool_files), runfiles = ctx.runfiles(files = tool_files), ) - mylanginfo = MylangInfo( + flatbuffersinfo = FlatBuffersInfo( target_tool_path = target_tool_path, tool_files = tool_files, ) @@ -48,7 +48,7 @@ def _mylang_toolchain_impl(ctx): # Export all the providers inside our ToolchainInfo # so the resolved_toolchain rule can grab and re-export them. toolchain_info = platform_common.ToolchainInfo( - mylanginfo = mylanginfo, + flatbuffersinfo = flatbuffersinfo, template_variables = template_variables, default = default, ) @@ -58,8 +58,8 @@ def _mylang_toolchain_impl(ctx): template_variables, ] -mylang_toolchain = rule( - implementation = _mylang_toolchain_impl, +flatbuffers_toolchain = rule( + implementation = _flatbuffers_toolchain_impl, attrs = { "target_tool": attr.label( doc = "A hermetically downloaded executable target for the target platform.", @@ -71,7 +71,7 @@ mylang_toolchain = rule( mandatory = False, ), }, - doc = """Defines a mylang compiler/runtime toolchain. + doc = """Defines a flatbuffers compiler/runtime toolchain. For usage see https://docs.bazel.build/versions/main/toolchains.html#defining-toolchains. """, diff --git a/mylang/defs.bzl b/mylang/defs.bzl deleted file mode 100644 index b48c6fc..0000000 --- a/mylang/defs.bzl +++ /dev/null @@ -1,5 +0,0 @@ -"Public API re-exports" - -def example(): - """This is an example""" - pass diff --git a/mylang/private/versions.bzl b/mylang/private/versions.bzl deleted file mode 100644 index 7f6c5bd..0000000 --- a/mylang/private/versions.bzl +++ /dev/null @@ -1,14 +0,0 @@ -"""Mirror of release info - -TODO: generate this file from GitHub API""" - -# The integrity hashes can be computed with -# shasum -b -a 384 [downloaded file] | awk '{ print $1 }' | xxd -r -p | base64 -TOOL_VERSIONS = { - "1.14.2": { - "x86_64-apple-darwin": "sha384-ws4+rANvv0YxM1SgIBUXSG9jT8dKw83nls6R5qYkEKzPUB+viBIEozSsyq2e6i+f", - "aarch64-apple-darwin": "sha384-HcvJbxoJtGSavkGu0e7CyD00cBlmDb0TBWJ4JSaNa70zuU3N7XlMOYm3bbQcAv2U", - "x86_64-pc-windows-msvc": "sha384-35YN6TKpT0L9qyRBmq48NucvyXEtHnkeC+txf2YZmmJTmOzrAKREA74BA0EZvpar", - "x86_64-unknown-linux-gnu": "sha384-QgGOwTaetxY0h5HWCKc/3ZtBs4N/fgaaORthn7UcEv++Idm9W+ntCCZRwvBdwHPD", - }, -}