Vendor dependencies

Let's see how I like this workflow.
This commit is contained in:
John Doty 2022-12-19 08:27:18 -08:00
parent 34d1830413
commit 9c435dc440
7500 changed files with 1665121 additions and 99 deletions

7
vendor/cxx/tools/bazel/BUILD vendored Normal file
View file

@ -0,0 +1,7 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
bzl_library(
name = "bzl_srcs",
srcs = glob(["**/*.bzl"]),
visibility = ["//visibility:public"],
)

View file

@ -0,0 +1,49 @@
# buildifier: disable=module-docstring
load("@bazel_skylib//rules:run_binary.bzl", "run_binary")
load("@rules_cc//cc:defs.bzl", "cc_library")
def rust_cxx_bridge(name, src, deps = []):
"""A macro defining a cxx bridge library
Args:
name (string): The name of the new target
src (string): The rust source file to generate a bridge for
deps (list, optional): A list of dependencies for the underlying cc_library. Defaults to [].
"""
native.alias(
name = "%s/header" % name,
actual = src + ".h",
)
native.alias(
name = "%s/source" % name,
actual = src + ".cc",
)
run_binary(
name = "%s/generated" % name,
srcs = [src],
outs = [
src + ".h",
src + ".cc",
],
args = [
"$(location %s)" % src,
"-o",
"$(location %s.h)" % src,
"-o",
"$(location %s.cc)" % src,
],
tool = "@cxx.rs//:codegen",
)
cc_library(
name = name,
srcs = [src + ".cc"],
deps = deps + [":%s/include" % name],
)
cc_library(
name = "%s/include" % name,
hdrs = [src + ".h"],
)

18
vendor/cxx/tools/bazel/third_party.bzl vendored Normal file
View file

@ -0,0 +1,18 @@
load("@rules_rust//cargo:cargo_build_script.bzl", "cargo_build_script")
load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library")
load("@third-party//:vendor.bzl", "vendored")
def third_party_glob(include):
return vendored and native.glob(include)
def third_party_cargo_build_script(rustc_flags = [], **kwargs):
rustc_flags = rustc_flags + ["--cap-lints=allow"]
cargo_build_script(rustc_flags = rustc_flags, **kwargs)
def third_party_rust_binary(rustc_flags = [], **kwargs):
rustc_flags = rustc_flags + ["--cap-lints=allow"]
rust_binary(rustc_flags = rustc_flags, **kwargs)
def third_party_rust_library(rustc_flags = [], **kwargs):
rustc_flags = rustc_flags + ["--cap-lints=allow"]
rust_library(rustc_flags = rustc_flags, **kwargs)

81
vendor/cxx/tools/bazel/vendor.bzl vendored Normal file
View file

@ -0,0 +1,81 @@
"""A module defining a repository rule for vendoring the dependencies
of a crate in the current workspace.
"""
load("@rules_rust//rust:defs.bzl", "rust_common")
load("@rules_rust//rust:repositories.bzl", "load_arbitrary_tool")
load("@rules_rust//rust/platform:triple.bzl", "get_host_triple")
def _impl(repository_ctx):
# Link cxx repository into @third-party.
lockfile = repository_ctx.path(repository_ctx.attr.lockfile)
workspace = lockfile.dirname.dirname
repository_ctx.symlink(workspace, "workspace")
# Figure out which version of cargo to use.
if repository_ctx.attr.target_triple:
target_triple = repository_ctx.attr.target_triple
else:
target_triple = get_host_triple(repository_ctx).str
# Download cargo.
load_arbitrary_tool(
ctx = repository_ctx,
tool_name = "cargo",
tool_subdirectories = ["cargo"],
version = repository_ctx.attr.cargo_version,
iso_date = repository_ctx.attr.cargo_iso_date,
target_triple = target_triple,
)
cmd = ["{}/bin/cargo".format(repository_ctx.path(".")), "vendor", "--versioned-dirs"]
result = repository_ctx.execute(
cmd,
quiet = True,
working_directory = "workspace/third-party",
)
_log_cargo_vendor(repository_ctx, result)
if result.return_code != 0:
fail("failed to execute `{}`".format(" ".join(cmd)))
# Produce a token for third_party_glob to depend on so that the necessary
# sequencing is visible to Bazel.
repository_ctx.file("BUILD", executable = False)
repository_ctx.file("vendor.bzl", "vendored = True", executable = False)
def _copy_file(repository_ctx, *, src, dst):
content = repository_ctx.read(src)
if not dst.exists or content != repository_ctx.read(dst):
repository_ctx.file(dst, content = content, executable = False)
def _log_cargo_vendor(repository_ctx, result):
relevant = ""
for line in result.stderr.splitlines(True):
if line.strip() and not line.startswith("To use vendored sources,"):
relevant += line
if relevant:
# Render it as command output.
# If we just use print(), Bazel will cache and repeat the output even
# when not rerunning the command.
print = ["echo", relevant]
repository_ctx.execute(print, quiet = False)
vendor = repository_rule(
doc = "A rule used to vendor the dependencies of a crate in the current workspace",
attrs = {
"cargo_version": attr.string(
doc = "The version of cargo to use",
),
"cargo_iso_date": attr.string(
doc = "The date of the tool (or None, if the version is a specific version)",
),
"target_triple": attr.string(
doc = "The target triple of the cargo binary to download",
),
"lockfile": attr.label(
doc = "A lockfile providing the set of crates to vendor",
),
},
local = True,
implementation = _impl,
)