Skip to content

Instantly share code, notes, and snippets.

@adamhjk
Last active April 27, 2023 23:27
Show Gist options
  • Save adamhjk/5cf04d3e0e7fadd12ea34d985c78d3ad to your computer and use it in GitHub Desktop.
Save adamhjk/5cf04d3e0e7fadd12ea34d985c78d3ad to your computer and use it in GitHub Desktop.
Brutalist buck2 + cargo rule
load("//build-rules/cargo.bzl", "cargo_library")
cargo_library(
name = "dal",
package = "dal",
build_type = "debug",
srcs = glob(["lib/dal/**/*.rs", "lib/dal/build.rs"]),
out = "libdal"
)
def cargo_library_impl(ctx: "context") -> ["provider"]:
script = ctx.actions.write("cargo-build.sh", """#!/bin/bash
set -euxo pipefail
cargo build -p $1
mkdir -p $4
cp target/$2/$3.* $4
""", is_executable = True);
out = ctx.actions.declare_output(ctx.attrs.out, dir = True)
args = cmd_args([script, ctx.attrs.package, ctx.attrs.build_type, ctx.attrs.out, out.as_output()])
args.hidden([ctx.attrs.srcs])
args.hidden([ctx.attrs.deps])
ctx.actions.run(args, category = "cargo", identifier = "build", local_only = True)
return [DefaultInfo(default_outputs = [out])]
cargo_library = rule(impl = cargo_library_impl, attrs = {
"package": attrs.string(doc = "Cargo package name"),
"srcs": attrs.list(attrs.source(), doc = """List of package.json files to track"""),
"deps": attrs.list(attrs.source(), default = [], doc="A buck2 dep"),
"build_type": attrs.string(),
"out": attrs.string(doc="the basename of the file(s) in target to track as output"),
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment