Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions bazel/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ load_packages()

load("@toolshed_pip3//:requirements.bzl", "install_deps")
install_deps()

load("@crates//:defs.bzl", "crate_repositories")
crate_repositories()
13 changes: 12 additions & 1 deletion bazel/deps.bzl
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
load("@rules_foreign_cc//foreign_cc:repositories.bzl", "rules_foreign_cc_dependencies")
load("@rules_perl//perl:deps.bzl", "perl_register_toolchains", "perl_rules_dependencies")
load("@rules_python//python:repositories.bzl", "py_repositories")
load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains")
load("@toolchains_llvm//toolchain:deps.bzl", "bazel_toolchain_dependencies")
load("@toolchains_llvm//toolchain:rules.bzl", "llvm_toolchain")
load("//:versions.bzl", "VERSIONS")
load("//sysroot:sysroot.bzl", "setup_sysroots")
load("//autotools:setup.bzl", "setup_autotools")
load("//glint:setup.bzl", "setup_glint")

def resolve_dependencies(
cmake_version=None,
llvm_version=None,
ninja_version=None,
setup_autotools_toolchain=True):
setup_autotools_toolchain=True,
setup_glint_toolchain=True,
setup_rust_toolchain=True):
py_repositories()
bazel_toolchain_dependencies()
rules_foreign_cc_dependencies(
Expand All @@ -22,6 +26,11 @@ def resolve_dependencies(
)
perl_rules_dependencies()
perl_register_toolchains()

if setup_rust_toolchain:
rules_rust_dependencies()
rust_register_toolchains(versions = ["1.84.0"])

setup_sysroots()
llvm_toolchain(
name = "llvm_toolchain",
Expand All @@ -33,3 +42,5 @@ def resolve_dependencies(
)
if setup_autotools_toolchain:
setup_autotools()
if setup_glint_toolchain:
setup_glint()
1 change: 1 addition & 0 deletions bazel/glint/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
licenses(["notice"]) # Apache 2
71 changes: 71 additions & 0 deletions bazel/glint/IMPLEMENTATION_SUMMARY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Glint Bazel Toolchain - Summary

## What was implemented:

1. **Added rules_rust to versions.bzl**
- Added rules_rust dependency (v0.58.0)
- Added placeholders for glint binary SHA256 hashes (to be updated after first release)
- Added VERSION_GLINT constant

2. **Created glint toolchain structure** (`/bazel/toolchains/glint/`)
- `glint_toolchain.bzl`: Toolchain definition with GlintInfo provider
- `BUILD`: Toolchain targets (hermetic and preinstalled)
- `current_toolchain.bzl`: Helper rule for depending on current toolchain
- `README.md`: Documentation
- `test/BUILD`: Test to verify toolchain works
- `examples/BUILD`: Example usage in genrules

3. **Created glint archive setup** (`/bazel/glint/`)
- `archives.bzl`: Downloads prebuilt binaries for amd64/arm64
- `setup.bzl`: Main setup function
- `SHA256_UPDATE.md`: Instructions for updating hashes after release

4. **Updated main Bazel configuration**
- `deps.bzl`: Added rules_rust dependencies and glint setup
- `packages.bzl`: Added rust crate repository setup
- `WORKSPACE`: Added crate repositories loading
- `toolchains/register.bzl`: Added glint toolchain registration

5. **Created BUILD files for Rust code**
- `/rust/BUILD`: Exports Cargo files
- `/rust/glint/BUILD`: Defines glint_binary target for building from source

6. **Fixed issues**
- Fixed Rust edition from "2024" to "2021" in glint's Cargo.toml

## How it works:

1. **For amd64/arm64 architectures**: Downloads prebuilt binaries from GitHub releases
2. **For other architectures**: Falls back to building from source using rules_rust
3. **Toolchain priority**: Hermetic (prebuilt/source) > Preinstalled

## Next steps:

1. **After the next bazel-bins release**:
- Download the released glint binaries
- Calculate SHA256 hashes
- Update `glint_amd64_sha256` and `glint_arm64_sha256` in versions.bzl
- Test the toolchain with: `bazel test //toolchains/glint/test:glint_toolchain_test`

2. **Usage in other projects**:
```python
# In WORKSPACE:
load("@envoy_toolshed//bazel:deps.bzl", "resolve_dependencies")
resolve_dependencies()

# In BUILD files:
load("@envoy_toolshed//toolchains/glint:current_toolchain.bzl", "current_glint_toolchain")

genrule(
name = "lint_files",
srcs = ["file.txt"],
outs = ["lint_report.json"],
cmd = "$(location @envoy_toolshed//toolchains/glint:current_glint_toolchain) $(SRCS) > $@",
tools = ["@envoy_toolshed//toolchains/glint:current_glint_toolchain"],
)
```

## Notes:
- Glint is a whitespace linter, not a grep tool
- It checks for trailing whitespace, tabs, and missing final newlines
- Use `--fix` flag to automatically fix issues
41 changes: 41 additions & 0 deletions bazel/glint/SHA256_UPDATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Glint Toolchain Setup

## SHA256 Hash Update Instructions

After the first release with glint binaries:

1. Download the binaries from the release:
```bash
wget https://github.com/envoyproxy/toolshed/releases/download/bazel-bins-v0.1.11/glint-0.1.0-amd64
wget https://github.com/envoyproxy/toolshed/releases/download/bazel-bins-v0.1.11/glint-0.1.0-arm64
```

2. Calculate the SHA256 hashes:
```bash
sha256sum glint-0.1.0-amd64
sha256sum glint-0.1.0-arm64
```

3. Update the hashes in `/bazel/versions.bzl`:
- `glint_amd64_sha256`: Update with the amd64 hash
- `glint_arm64_sha256`: Update with the arm64 hash

4. Update the `bins_release` version if needed.

## Testing the Toolchain

After updating the hashes:

```bash
cd bazel
bazel test //toolchains/glint/test:glint_toolchain_test
```

## Building from Source (Fallback)

For architectures without prebuilt binaries:

```bash
cd bazel
bazel build //rust/glint:glint_binary
```
31 changes: 31 additions & 0 deletions bazel/glint/archives.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""Setup glint dependencies."""

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive", "http_file")
load("//:versions.bzl", "VERSION_GLINT", "VERSIONS")

def _glint_archive(name, arch):
"""Create a glint archive repository."""
# Map arch names to match what the workflow produces
arch_map = {
"x86_64": "amd64",
"aarch64": "arm64",
}
release_arch = arch_map.get(arch, arch)

# Download the binary directly (not a tar.xz)
http_file(
name = name,
urls = ["https://github.com/envoyproxy/toolshed/releases/download/bazel-bins-v%s/glint-%s-%s" % (
VERSIONS["bins_release"],
VERSION_GLINT,
release_arch,
)],
sha256 = VERSIONS.get("glint_%s_sha256" % release_arch, ""), # Will be empty string until first release
downloaded_file_path = "glint",
executable = True,
)

def setup_glint_archives():
"""Set up glint archives for supported architectures."""
_glint_archive("glint_amd64", "x86_64")
_glint_archive("glint_arm64", "aarch64")
25 changes: 25 additions & 0 deletions bazel/glint/setup.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""Full glint setup for external repositories."""

load("//glint:archives.bzl", "setup_glint_archives")

def setup_glint(register_toolchains = True):
"""Set up glint for use in external repositories.

This function:
1. Downloads the prebuilt glint binaries for supported architectures
2. Registers the toolchains

For unsupported architectures, it will fall back to building from source.

Args:
register_toolchains: Whether to register the glint toolchains (default: True)
"""
# Set up the archives for prebuilt binaries
setup_glint_archives()

# Register toolchains
if register_toolchains:
native.register_toolchains(
"@envoy_toolshed//toolchains/glint:hermetic_glint_toolchain",
"@envoy_toolshed//toolchains/glint:preinstalled_glint_toolchain",
)
11 changes: 11 additions & 0 deletions bazel/packages.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
load("@bazel_features//:deps.bzl", "bazel_features_deps")
load("@rules_python//python:pip.bzl", "pip_parse")
load("@rules_rust//crate_universe:defs.bzl", "crates_repository")
load("//:versions.bzl", "VERSIONS")

def load_packages():
Expand All @@ -11,6 +12,16 @@ def load_packages():
)
bazel_features_deps()

# Rust crate dependencies for glint
crates_repository(
name = "crates",
cargo_lockfile = "@envoy_toolshed//rust:Cargo.lock",
manifests = [
"@envoy_toolshed//rust:Cargo.toml",
"@envoy_toolshed//rust/glint:Cargo.toml",
],
)

def load_website_packages():
# Only call this if you wish to use the website functionality
pip_parse(
Expand Down
43 changes: 43 additions & 0 deletions bazel/toolchains/glint/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
load(":glint_toolchain.bzl", "glint_toolchain")
load(":current_toolchain.bzl", "current_glint_toolchain")

package(default_visibility = ["//visibility:public"])

# Toolchain type for glint
toolchain_type(
name = "glint_toolchain_type",
)

# Preinstalled glint (fallback)
glint_toolchain(
name = "preinstalled_glint",
glint_path = "glint",
)

toolchain(
name = "preinstalled_glint_toolchain",
toolchain = ":preinstalled_glint",
toolchain_type = ":glint_toolchain_type",
)

# Hermetic glint toolchain - uses prebuilt binaries or builds from source
glint_toolchain(
name = "hermetic_glint",
glint = select({
"@platforms//cpu:x86_64": "@glint_amd64//:glint",
"@platforms//cpu:aarch64": "@glint_arm64//:glint",
# Fall back to building from source for other architectures
"//conditions:default": "//rust/glint:glint_binary",
}),
)

toolchain(
name = "hermetic_glint_toolchain",
toolchain = ":hermetic_glint",
toolchain_type = ":glint_toolchain_type",
)

# Current toolchain for use in toolchains attribute
current_glint_toolchain(
name = "current_glint_toolchain",
)
44 changes: 44 additions & 0 deletions bazel/toolchains/glint/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Glint Toolchain

This directory contains the Bazel toolchain definition for glint, a whitespace linter written in Rust.

## Usage

The glint toolchain can be used in two ways:

### 1. As a toolchain dependency

Add the toolchain to your rule's `toolchains` attribute:

```python
my_rule(
name = "example",
toolchains = ["//toolchains/glint:current_glint_toolchain"],
)
```

Then in your rule implementation:

```python
load("//toolchains/glint:glint_toolchain.bzl", "get_glint_data")

def _my_rule_impl(ctx):
glint_data = get_glint_data(ctx)
glint_path = glint_data.glint
# Use glint_path in your commands
```

### 2. Direct usage

The toolchain will automatically use prebuilt binaries for x86_64 (amd64) and aarch64 (arm64) architectures.
For other architectures, it will fall back to building glint from source using Rust.

## Architecture Support

- **x86_64/amd64**: Downloads prebuilt binary from GitHub releases
- **aarch64/arm64**: Downloads prebuilt binary from GitHub releases
- **Other architectures**: Builds from source using rules_rust

## Configuration

The toolchain is automatically registered when using `setup_glint()` in your WORKSPACE file.
21 changes: 21 additions & 0 deletions bazel/toolchains/glint/current_toolchain.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""Rule for depending on the current glint toolchain."""

def _current_glint_toolchain_impl(ctx):
"""Implementation for current_glint_toolchain rule."""
toolchain = ctx.toolchains["//toolchains/glint:glint_toolchain_type"]
if not toolchain:
fail("No glint toolchain found. Did you register the toolchain?")

info = toolchain.glint_info
files = []
if hasattr(info, "glint_file") and info.glint_file:
files.append(info.glint_file)

return [
DefaultInfo(files = depset(files)),
]

current_glint_toolchain = rule(
implementation = _current_glint_toolchain_impl,
toolchains = ["//toolchains/glint:glint_toolchain_type"],
)
Loading
Loading