Skip to content

Commit 845f73e

Browse files
committed
bazel: Add glint toolchain
Signed-off-by: Ryan Northey <[email protected]>
1 parent 6656601 commit 845f73e

File tree

16 files changed

+510
-1
lines changed

16 files changed

+510
-1
lines changed

bazel/WORKSPACE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@ load_packages()
1414

1515
load("@toolshed_pip3//:requirements.bzl", "install_deps")
1616
install_deps()
17+
18+
load("@crates//:defs.bzl", "crate_repositories")
19+
crate_repositories()

bazel/deps.bzl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
load("@rules_foreign_cc//foreign_cc:repositories.bzl", "rules_foreign_cc_dependencies")
22
load("@rules_perl//perl:deps.bzl", "perl_register_toolchains", "perl_rules_dependencies")
33
load("@rules_python//python:repositories.bzl", "py_repositories")
4+
load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains")
45
load("@toolchains_llvm//toolchain:deps.bzl", "bazel_toolchain_dependencies")
56
load("@toolchains_llvm//toolchain:rules.bzl", "llvm_toolchain")
67
load("//:versions.bzl", "VERSIONS")
78
load("//sysroot:sysroot.bzl", "setup_sysroots")
89
load("//autotools:setup.bzl", "setup_autotools")
10+
load("//glint:setup.bzl", "setup_glint")
911

1012
def resolve_dependencies(
1113
cmake_version=None,
1214
llvm_version=None,
1315
ninja_version=None,
14-
setup_autotools_toolchain=True):
16+
setup_autotools_toolchain=True,
17+
setup_glint_toolchain=True,
18+
setup_rust_toolchain=True):
1519
py_repositories()
1620
bazel_toolchain_dependencies()
1721
rules_foreign_cc_dependencies(
@@ -22,6 +26,11 @@ def resolve_dependencies(
2226
)
2327
perl_rules_dependencies()
2428
perl_register_toolchains()
29+
30+
if setup_rust_toolchain:
31+
rules_rust_dependencies()
32+
rust_register_toolchains(versions = ["1.84.0"])
33+
2534
setup_sysroots()
2635
llvm_toolchain(
2736
name = "llvm_toolchain",
@@ -33,3 +42,5 @@ def resolve_dependencies(
3342
)
3443
if setup_autotools_toolchain:
3544
setup_autotools()
45+
if setup_glint_toolchain:
46+
setup_glint()

bazel/glint/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
licenses(["notice"]) # Apache 2
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Glint Bazel Toolchain - Summary
2+
3+
## What was implemented:
4+
5+
1. **Added rules_rust to versions.bzl**
6+
- Added rules_rust dependency (v0.58.0)
7+
- Added placeholders for glint binary SHA256 hashes (to be updated after first release)
8+
- Added VERSION_GLINT constant
9+
10+
2. **Created glint toolchain structure** (`/bazel/toolchains/glint/`)
11+
- `glint_toolchain.bzl`: Toolchain definition with GlintInfo provider
12+
- `BUILD`: Toolchain targets (hermetic and preinstalled)
13+
- `current_toolchain.bzl`: Helper rule for depending on current toolchain
14+
- `README.md`: Documentation
15+
- `test/BUILD`: Test to verify toolchain works
16+
- `examples/BUILD`: Example usage in genrules
17+
18+
3. **Created glint archive setup** (`/bazel/glint/`)
19+
- `archives.bzl`: Downloads prebuilt binaries for amd64/arm64
20+
- `setup.bzl`: Main setup function
21+
- `SHA256_UPDATE.md`: Instructions for updating hashes after release
22+
23+
4. **Updated main Bazel configuration**
24+
- `deps.bzl`: Added rules_rust dependencies and glint setup
25+
- `packages.bzl`: Added rust crate repository setup
26+
- `WORKSPACE`: Added crate repositories loading
27+
- `toolchains/register.bzl`: Added glint toolchain registration
28+
29+
5. **Created BUILD files for Rust code**
30+
- `/rust/BUILD`: Exports Cargo files
31+
- `/rust/glint/BUILD`: Defines glint_binary target for building from source
32+
33+
6. **Fixed issues**
34+
- Fixed Rust edition from "2024" to "2021" in glint's Cargo.toml
35+
36+
## How it works:
37+
38+
1. **For amd64/arm64 architectures**: Downloads prebuilt binaries from GitHub releases
39+
2. **For other architectures**: Falls back to building from source using rules_rust
40+
3. **Toolchain priority**: Hermetic (prebuilt/source) > Preinstalled
41+
42+
## Next steps:
43+
44+
1. **After the next bazel-bins release**:
45+
- Download the released glint binaries
46+
- Calculate SHA256 hashes
47+
- Update `glint_amd64_sha256` and `glint_arm64_sha256` in versions.bzl
48+
- Test the toolchain with: `bazel test //toolchains/glint/test:glint_toolchain_test`
49+
50+
2. **Usage in other projects**:
51+
```python
52+
# In WORKSPACE:
53+
load("@envoy_toolshed//bazel:deps.bzl", "resolve_dependencies")
54+
resolve_dependencies()
55+
56+
# In BUILD files:
57+
load("@envoy_toolshed//toolchains/glint:current_toolchain.bzl", "current_glint_toolchain")
58+
59+
genrule(
60+
name = "lint_files",
61+
srcs = ["file.txt"],
62+
outs = ["lint_report.json"],
63+
cmd = "$(location @envoy_toolshed//toolchains/glint:current_glint_toolchain) $(SRCS) > $@",
64+
tools = ["@envoy_toolshed//toolchains/glint:current_glint_toolchain"],
65+
)
66+
```
67+
68+
## Notes:
69+
- Glint is a whitespace linter, not a grep tool
70+
- It checks for trailing whitespace, tabs, and missing final newlines
71+
- Use `--fix` flag to automatically fix issues

bazel/glint/SHA256_UPDATE.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Glint Toolchain Setup
2+
3+
## SHA256 Hash Update Instructions
4+
5+
After the first release with glint binaries:
6+
7+
1. Download the binaries from the release:
8+
```bash
9+
wget https://github.com/envoyproxy/toolshed/releases/download/bazel-bins-v0.1.11/glint-0.1.0-amd64
10+
wget https://github.com/envoyproxy/toolshed/releases/download/bazel-bins-v0.1.11/glint-0.1.0-arm64
11+
```
12+
13+
2. Calculate the SHA256 hashes:
14+
```bash
15+
sha256sum glint-0.1.0-amd64
16+
sha256sum glint-0.1.0-arm64
17+
```
18+
19+
3. Update the hashes in `/bazel/versions.bzl`:
20+
- `glint_amd64_sha256`: Update with the amd64 hash
21+
- `glint_arm64_sha256`: Update with the arm64 hash
22+
23+
4. Update the `bins_release` version if needed.
24+
25+
## Testing the Toolchain
26+
27+
After updating the hashes:
28+
29+
```bash
30+
cd bazel
31+
bazel test //toolchains/glint/test:glint_toolchain_test
32+
```
33+
34+
## Building from Source (Fallback)
35+
36+
For architectures without prebuilt binaries:
37+
38+
```bash
39+
cd bazel
40+
bazel build //rust/glint:glint_binary
41+
```

bazel/glint/archives.bzl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""Setup glint dependencies."""
2+
3+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive", "http_file")
4+
load("//:versions.bzl", "VERSION_GLINT", "VERSIONS")
5+
6+
def _glint_archive(name, arch):
7+
"""Create a glint archive repository."""
8+
# Map arch names to match what the workflow produces
9+
arch_map = {
10+
"x86_64": "amd64",
11+
"aarch64": "arm64",
12+
}
13+
release_arch = arch_map.get(arch, arch)
14+
15+
# Download the binary directly (not a tar.xz)
16+
http_file(
17+
name = name,
18+
urls = ["https://github.com/envoyproxy/toolshed/releases/download/bazel-bins-v%s/glint-%s-%s" % (
19+
VERSIONS["bins_release"],
20+
VERSION_GLINT,
21+
release_arch,
22+
)],
23+
sha256 = VERSIONS.get("glint_%s_sha256" % release_arch, ""), # Will be empty string until first release
24+
downloaded_file_path = "glint",
25+
executable = True,
26+
)
27+
28+
def setup_glint_archives():
29+
"""Set up glint archives for supported architectures."""
30+
_glint_archive("glint_amd64", "x86_64")
31+
_glint_archive("glint_arm64", "aarch64")

bazel/glint/setup.bzl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""Full glint setup for external repositories."""
2+
3+
load("//glint:archives.bzl", "setup_glint_archives")
4+
5+
def setup_glint(register_toolchains = True):
6+
"""Set up glint for use in external repositories.
7+
8+
This function:
9+
1. Downloads the prebuilt glint binaries for supported architectures
10+
2. Registers the toolchains
11+
12+
For unsupported architectures, it will fall back to building from source.
13+
14+
Args:
15+
register_toolchains: Whether to register the glint toolchains (default: True)
16+
"""
17+
# Set up the archives for prebuilt binaries
18+
setup_glint_archives()
19+
20+
# Register toolchains
21+
if register_toolchains:
22+
native.register_toolchains(
23+
"@envoy_toolshed//toolchains/glint:hermetic_glint_toolchain",
24+
"@envoy_toolshed//toolchains/glint:preinstalled_glint_toolchain",
25+
)

bazel/packages.bzl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
load("@bazel_features//:deps.bzl", "bazel_features_deps")
22
load("@rules_python//python:pip.bzl", "pip_parse")
3+
load("@rules_rust//crate_universe:defs.bzl", "crates_repository")
34
load("//:versions.bzl", "VERSIONS")
45

56
def load_packages():
@@ -11,6 +12,16 @@ def load_packages():
1112
)
1213
bazel_features_deps()
1314

15+
# Rust crate dependencies for glint
16+
crates_repository(
17+
name = "crates",
18+
cargo_lockfile = "@envoy_toolshed//rust:Cargo.lock",
19+
manifests = [
20+
"@envoy_toolshed//rust:Cargo.toml",
21+
"@envoy_toolshed//rust/glint:Cargo.toml",
22+
],
23+
)
24+
1425
def load_website_packages():
1526
# Only call this if you wish to use the website functionality
1627
pip_parse(

bazel/toolchains/glint/BUILD

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
load(":glint_toolchain.bzl", "glint_toolchain")
2+
load(":current_toolchain.bzl", "current_glint_toolchain")
3+
4+
package(default_visibility = ["//visibility:public"])
5+
6+
# Toolchain type for glint
7+
toolchain_type(
8+
name = "glint_toolchain_type",
9+
)
10+
11+
# Preinstalled glint (fallback)
12+
glint_toolchain(
13+
name = "preinstalled_glint",
14+
glint_path = "glint",
15+
)
16+
17+
toolchain(
18+
name = "preinstalled_glint_toolchain",
19+
toolchain = ":preinstalled_glint",
20+
toolchain_type = ":glint_toolchain_type",
21+
)
22+
23+
# Hermetic glint toolchain - uses prebuilt binaries or builds from source
24+
glint_toolchain(
25+
name = "hermetic_glint",
26+
glint = select({
27+
"@platforms//cpu:x86_64": "@glint_amd64//:glint",
28+
"@platforms//cpu:aarch64": "@glint_arm64//:glint",
29+
# Fall back to building from source for other architectures
30+
"//conditions:default": "//rust/glint:glint_binary",
31+
}),
32+
)
33+
34+
toolchain(
35+
name = "hermetic_glint_toolchain",
36+
toolchain = ":hermetic_glint",
37+
toolchain_type = ":glint_toolchain_type",
38+
)
39+
40+
# Current toolchain for use in toolchains attribute
41+
current_glint_toolchain(
42+
name = "current_glint_toolchain",
43+
)

bazel/toolchains/glint/README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Glint Toolchain
2+
3+
This directory contains the Bazel toolchain definition for glint, a whitespace linter written in Rust.
4+
5+
## Usage
6+
7+
The glint toolchain can be used in two ways:
8+
9+
### 1. As a toolchain dependency
10+
11+
Add the toolchain to your rule's `toolchains` attribute:
12+
13+
```python
14+
my_rule(
15+
name = "example",
16+
toolchains = ["//toolchains/glint:current_glint_toolchain"],
17+
)
18+
```
19+
20+
Then in your rule implementation:
21+
22+
```python
23+
load("//toolchains/glint:glint_toolchain.bzl", "get_glint_data")
24+
25+
def _my_rule_impl(ctx):
26+
glint_data = get_glint_data(ctx)
27+
glint_path = glint_data.glint
28+
# Use glint_path in your commands
29+
```
30+
31+
### 2. Direct usage
32+
33+
The toolchain will automatically use prebuilt binaries for x86_64 (amd64) and aarch64 (arm64) architectures.
34+
For other architectures, it will fall back to building glint from source using Rust.
35+
36+
## Architecture Support
37+
38+
- **x86_64/amd64**: Downloads prebuilt binary from GitHub releases
39+
- **aarch64/arm64**: Downloads prebuilt binary from GitHub releases
40+
- **Other architectures**: Builds from source using rules_rust
41+
42+
## Configuration
43+
44+
The toolchain is automatically registered when using `setup_glint()` in your WORKSPACE file.

0 commit comments

Comments
 (0)