Skip to content

Commit

Permalink
Adding flatc
Browse files Browse the repository at this point in the history
Updating rules according to README.md instructions, adding flatc binaries

TODO:
* add sha256 hashes to the binaries
* add test to verify that we can generate java code
  • Loading branch information
manuelnaranjo committed Sep 2, 2024
1 parent fba3b7e commit 528e0f9
Show file tree
Hide file tree
Showing 26 changed files with 150 additions and 147 deletions.
4 changes: 2 additions & 2 deletions .bcr/metadata.template.json
Original file line number Diff line number Diff line change
@@ -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": {}
}
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 6 additions & 6 deletions .github/workflows/release_prep.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 "\`\`\`"
6 changes: 3 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ 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
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

Expand Down
10 changes: 5 additions & 5 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"Bazel dependencies"

module(
name = "com_myorg_rules_mylang",
name = "com_bookingcom_rules_flatbuffers",
version = "0.0.0",
compatibility_level = 1,
)
Expand All @@ -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")
42 changes: 4 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
@@ -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:
<https://github.com/myorg/rules_mylang/releases>
<https://github.com/bookingcom/rules_flatbuffers/releases>
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
Expand Down
2 changes: 1 addition & 1 deletion docs/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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")
2 changes: 1 addition & 1 deletion e2e/smoke/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions e2e/smoke/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -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 = "../..",
)
2 changes: 1 addition & 1 deletion e2e/smoke/README.md
Original file line number Diff line number Diff line change
@@ -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.
15 changes: 10 additions & 5 deletions e2e/smoke/WORKSPACE.bazel
Original file line number Diff line number Diff line change
@@ -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",
)
15 changes: 12 additions & 3 deletions mylang/BUILD.bazel → flatbuffers/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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"])
Expand All @@ -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",
],
Expand All @@ -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)",
},
)
7 changes: 7 additions & 0 deletions flatbuffers/defs.bzl
Original file line number Diff line number Diff line change
@@ -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)
30 changes: 15 additions & 15 deletions mylang/extensions.bzl → flatbuffers/extensions.bzl
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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},
)
3 changes: 3 additions & 0 deletions flatbuffers/flatc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

exec $FLATC_BINARY $@
Original file line number Diff line number Diff line change
Expand Up @@ -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__"],
)
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ 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,
]

# Copied from java_toolchain_alias
# 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,
)
Loading

0 comments on commit 528e0f9

Please sign in to comment.