Skip to content

Commit e9bd2d2

Browse files
authored
Merge pull request #893 from ccontavalli/hermetic-cargo
bazel: don't use cargo in $PATH, make the build hermetic.
2 parents 251cf91 + 4fbb797 commit e9bd2d2

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

WORKSPACE

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
workspace(name = "cxx.rs")
22

33
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
4-
load("//tools/bazel:vendor.bzl", "vendor")
54

65
http_archive(
76
name = "rules_rust",
@@ -15,12 +14,17 @@ http_archive(
1514

1615
load("@rules_rust//rust:repositories.bzl", "rust_repositories")
1716

17+
RUST_VERSION = "1.51.0"
18+
1819
rust_repositories(
1920
edition = "2018",
20-
version = "1.51.0",
21+
version = RUST_VERSION,
2122
)
2223

24+
load("//tools/bazel:vendor.bzl", "vendor")
25+
2326
vendor(
2427
name = "third-party",
2528
lockfile = "//third-party:Cargo.lock",
29+
cargo_version = RUST_VERSION,
2630
)

tools/bazel/vendor.bzl

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
of a crate in the current workspace.
33
"""
44

5+
load("@rules_rust//rust:repositories.bzl", "load_arbitrary_tool", "DEFAULT_RUST_VERSION")
6+
57
def _impl(repository_ctx):
68
# Link cxx repository into @third-party.
79
lockfile = repository_ctx.path(repository_ctx.attr.lockfile)
@@ -14,8 +16,27 @@ def _impl(repository_ctx):
1416
root_lockfile = repository_ctx.path("workspace/Cargo.lock")
1517
_copy_file(repository_ctx, src = vendor_lockfile, dst = root_lockfile)
1618

17-
# Execute cargo vendor.
18-
cmd = ["cargo", "vendor", "--versioned-dirs", "third-party/vendor"]
19+
# Figure out which version of cargo to use.
20+
if repository_ctx.attr.target_triple:
21+
target_triple = repository_ctx.attr.target_triple
22+
elif "mac" in repository_ctx.os.name:
23+
target_triple = "x86_64-apple-darwin"
24+
elif "windows" in repository_ctx.os.name:
25+
target_triple = "x86_64-pc-windows-msvc"
26+
else:
27+
target_triple = "x86_64-unknown-linux-gnu"
28+
29+
# Download cargo.
30+
load_arbitrary_tool(
31+
ctx = repository_ctx,
32+
tool_name = "cargo",
33+
tool_subdirectories = ["cargo"],
34+
version = repository_ctx.attr.cargo_version,
35+
iso_date = repository_ctx.attr.cargo_iso_date,
36+
target_triple = target_triple,
37+
)
38+
39+
cmd = ["{}/bin/cargo".format(repository_ctx.path(".")), "vendor", "--versioned-dirs", "third-party/vendor"]
1940
result = repository_ctx.execute(
2041
cmd,
2142
quiet = True,
@@ -54,6 +75,16 @@ def _log_cargo_vendor(repository_ctx, result):
5475
vendor = repository_rule(
5576
doc = "A rule used to vendor the dependencies of a crate in the current workspace",
5677
attrs = {
78+
"cargo_version": attr.string(
79+
doc = "The version of cargo to use",
80+
default = DEFAULT_RUST_VERSION,
81+
),
82+
"cargo_iso_date": attr.string(
83+
doc = "The date of the tool (or None, if the version is a specific version)",
84+
),
85+
"target_triple": attr.string(
86+
doc = "The target triple of the cargo binary to download",
87+
),
5788
"lockfile": attr.label(
5889
doc = "A lockfile providing the set of crates to vendor",
5990
),

0 commit comments

Comments
 (0)