rules_multitool
v1.0.0
published 2 weeks ago
24 stars
5 forks
2 watchers
Apache License 2.0
public
1 assets
1,103,302 downloads
18 KB
Compatability level 1
uaiSchQXM7qGnjWHJrUELGClNcHWlcl7nH35sFrjQCA=
v1.0.0
November 8, 2024

Using Bzlmod (preferred)

  1. Create a multitool.lock.json (schema)
  2. Add to your MODULE.bazel file:
bazel_dep(name = "rules_multitool", version = "1.0.0")

multitool = use_extension("@rules_multitool//multitool:extension.bzl", "multitool")
multitool.hub(lockfile = "//:multitool.lock.json")
use_repo(multitool, "multitool")

Using WORKSPACE

  1. Create a multitool.lock.json (schema)
  2. Add to your WORKSPACE file:
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
    name = "bazel_features",
    sha256 = "06f02b97b6badb3227df2141a4b4622272cdcd2951526f40a888ab5f43897f14",
    strip_prefix = "bazel_features-1.9.0",
    url = "https://github.com/bazel-contrib/bazel_features/releases/download/v1.9.0/bazel_features-v1.9.0.tar.gz",
)

http_archive(
    name = "rules_multitool",
    sha256 = "b9a89272141733ba869e358726b5042c60a535c1d695c97b9c7df9b05ae34020",
    strip_prefix = "rules_multitool-1.0.0",
    url = "https://github.com/theoremlp/rules_multitool/releases/download/v1.0.0/rules_multitool-1.0.0.tar.gz",
)

load("@bazel_features//:deps.bzl", "bazel_features_deps")

bazel_features_deps()

load("@rules_multitool//multitool:multitool.bzl", "multitool")

multitool(
    name = "multitool",
    lockfile = "//:multitool.lock.json",
)

# required since 0.15.0 to enable only downloading tools used by this workspace
load("@multitool//:tools.bzl", "register_tools")

register_tools()

What's Changed

Full Changelog: https://github.com/theoremlp/rules_multitool/compare/v0.15.0...v1.0.0

Deps:
Assets:

rules_multitool

An ergonomic approach to defining a single tool target that resolves to a matching os and CPU architecture variant of the tool.

Usage

For a quickstart, see the module example or workspace example.

Define a lockfile that references the tools to load:

{
  "$schema": "https://raw.githubusercontent.com/theoremlp/rules_multitool/main/lockfile.schema.json",
  "tool-name": {
    "binaries": [
      {
        "kind": "file",
        "url": "https://...",
        "sha256": "sha256 of the file",
        "os": "linux|macos|windows",
        "cpu": "x86_64|arm64"
      }
    ]
  }
}

The lockfile supports the following binary kinds:

Bazel Module Usage

Once your lockfile is defined, load the ruleset in your MODULE.bazel and create a hub that refers to your lockfile:

bazel_dep(name = "rules_multitool", version = "0.0.0")

multitool = use_extension("@rules_multitool//multitool:extension.bzl", "multitool")
multitool.hub(lockfile = "//:multitool.lock.json")
use_repo(multitool, "multitool")

Tools may then be accessed using @multitool//tools/tool-name.

It's safe to call multitool.hub(...) multiple times, with multiple lockfiles. All lockfiles will be combined with a last-write-wins strategy.

Lockfiles defined across modules and applying to the same hub (including implicitly to the default "multitool" hub) will be combined such that the priority follows a breadth-first search originatiung from the root module.

It's possible to define multiple multitool hubs to group related tools together. To define an alternate hub:

multitool.hub(hub_name = "alt_hub", lockfile = "//:other_tools.lock.json")
use_repo(multitool, "alt_hub")

# register the tools from this hub
register_toolchains("@alt_hub//toolchains:all")

These alternate hubs also combine lockfiles according to the hub_name and follow the same merging rules as the default hub.

Workspace Usage

Instructions for using with WORKSPACE may be found in release notes.

Running tools in the current working directory

When running @multitool//tools/tool-name, Bazel will execute the tool at the root of the runfiles tree due to bazelbuild/bazel#3325.

To run a tool in the current working directory, use the convenience target @multitool//tools/tool-name:cwd.

To run a tool in the Bazel module or workspace root, use the convenience target @multitool/tools/tool-name:workspace_root.

A common pattern we recommend to further simplify invoking tools for repository users it to:

  1. Create a tools/ directory
  2. Create an executable shell script tools/_run_multitool.sh with the following code:
    #!/usr/bin/env bash
    bazel run "@multitool//tools/$( basename $0 ):cwd" -- "$@"
  3. Create symlinks of tools/tool-name to tools/_run_multitool.sh

Keeping Tools Up-to-Date

We provide a companion CLI multitool to help manage multitool lockfiles. The CLI supports basic updating of artifacts that come from GitHub releases, and may be extended in the future to support other common release channels.

See our docs on configuring a GitHub Action to check for updates and open PRs periodically.