Skip to content

Commit

Permalink
chore: add release rules for binaries (#280)
Browse files Browse the repository at this point in the history
Adds release infra for the rust binaries.

---

### Type of change

- Chore (any other change that doesn't affect source or test files, such
as configuration)
  • Loading branch information
Matt Mackay authored Feb 29, 2024
1 parent 82ad68f commit 9e2c653
Show file tree
Hide file tree
Showing 12 changed files with 347 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ common --noenable_bzlmod
# Define value used by tests
build --define=SOME_VAR=SOME_VALUE

common --features=-libtool
common --incompatible_enable_cc_toolchain_resolution

# Use local rules_python
# Enable with --config=dev
common:dev --override_repository=rules_python=~/workspace/rules_python
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/ci.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@

# Debug where options came from
build --announce_rc

# This directory is configured in GitHub actions to be persisted between runs.
build --disk_cache=~/.cache/bazel
build --repository_cache=~/.cache/bazel-repo

# Don't rely on test logs being easily accessible from the test runner,
# though it makes the log noisier.
test --test_output=errors

# Allows tests to run bazelisk-in-bazel, since this is the cache folder used
test --test_env=XDG_CACHE_HOME

# Ensure the binaries are build in opt during release.
build:release --compilation_mode opt
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@
**/*.whl
.venv/

!third_party/**

# TODO: enable when it is more stable, maybe Bazel 7.1
MODULE.bazel.lock
70 changes: 63 additions & 7 deletions WORKSPACE
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
# Declare the local Bazel workspace.
workspace(name = "aspect_rules_py")

load("//tools/release:fetch.bzl", _release_deps = "fetch_deps")
load(":internal_deps.bzl", "rules_py_internal_deps")

# Fetch deps needed only locally for development
rules_py_internal_deps()

# Fetch deps needed only for a release.
_release_deps()

load("//py:repositories.bzl", "rules_py_dependencies")

# Fetch dependencies which users need as well
Expand All @@ -30,13 +34,39 @@ load("@aspect_bazel_lib//lib:repositories.bzl", "register_coreutils_toolchains")
register_coreutils_toolchains()

############################################
## CC toolchain using zig
load("@hermetic_cc_toolchain//toolchain:defs.bzl", zig_toolchains = "toolchains")
## CC toolchain using llvm
load("@toolchains_llvm//toolchain:deps.bzl", "bazel_toolchain_dependencies")

bazel_toolchain_dependencies()

load("@toolchains_llvm//toolchain:rules.bzl", "llvm_toolchain")

llvm_toolchain(
name = "llvm_toolchain",
llvm_version = "17.0.2",
sha256 = {
"darwin-aarch64": "bb5144516c94326981ec78c8b055c85b1f6780d345128cae55c5925eb65241ee",
"darwin-x86_64": "800ec8401344a95f84588815e97523a0ed31fd05b6ffa9e1b58ce20abdcf69f1",
"linux-aarch64": "49eec0202b8cd4be228c8e92878303317f660bc904cf6e6c08917a55a638917d",
"linux-x86_64": "0c5096c157e196a04fc6ac58543266caef0da3e3c921414a7c279feacc2309d9",
},
sysroot = {
"darwin-aarch64": "@sysroot_darwin_universal//:sysroot",
"darwin-x86_64": "@sysroot_darwin_universal//:sysroot",
"linux-aarch64": "@org_chromium_sysroot_linux_arm64//:sysroot",
"linux-x86_64": "@org_chromium_sysroot_linux_x86_64//:sysroot",
},
urls = {
"darwin-aarch64": ["https://github.com/dzbarsky/static-clang/releases/download/v17.0.2-8/darwin_arm64_minimal.tar.xz"],
"darwin-x86_64": ["https://github.com/dzbarsky/static-clang/releases/download/v17.0.2-8/darwin_amd64_minimal.tar.xz"],
"linux-aarch64": ["https://github.com/dzbarsky/static-clang/releases/download/v17.0.2-8/linux_arm64_minimal.tar.xz"],
"linux-x86_64": ["https://github.com/dzbarsky/static-clang/releases/download/v17.0.2-8/linux_amd64_minimal.tar.xz"],
},
)

load("@llvm_toolchain//:toolchains.bzl", "llvm_register_toolchains")

# Plain zig_toolchains() will pick reasonable defaults. See
# toolchain/defs.bzl:toolchains on how to change the Zig SDK version and
# download URL.
zig_toolchains()
llvm_register_toolchains()

############################################
# Development dependencies from pypi
Expand Down Expand Up @@ -102,17 +132,43 @@ _py_image_repos()

############################################
# rules_rust dependencies for building tools
load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains")
load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains", "rust_repository_set")

rules_rust_dependencies()

rust_register_toolchains(
edition = "2021",
extra_target_triples = [
"x86_64-apple-darwin",
],
versions = [
"1.74.1",
],
)

# Declare cross-compilation toolchains
rust_repository_set(
name = "apple_darwin_86_64",
edition = "2021",
exec_triple = "x86_64-apple-darwin",
# and cross-compile to these platforms:
extra_target_triples = [
"aarch64-apple-darwin",
],
versions = ["1.74.1"],
)

rust_repository_set(
name = "linux_x86_64",
edition = "2021",
exec_triple = "x86_64-unknown-linux-gnu",
# and cross-compile to these platforms:
extra_target_triples = [
"aarch64-unknown-linux-gnu",
],
versions = ["1.74.1"],
)

load("@rules_rust//crate_universe:repositories.bzl", "crate_universe_dependencies")

crate_universe_dependencies()
Expand Down
20 changes: 20 additions & 0 deletions py/tools/unpack_bin/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@rules_rust//rust:defs.bzl", "rust_binary")
load("//tools/release:defs.bzl", "multi_arch_rust_binary_release")

rust_binary(
name = "unpack_bin",
Expand All @@ -14,3 +15,22 @@ rust_binary(
"@crate_index//:miette",
],
)

alias(
name = "unpack",
actual = ":unpack_bin",
)

multi_arch_rust_binary_release(
name = "macos",
src = ":unpack",
os = "macos",
visibility = ["//tools/release:__pkg__"],
)

multi_arch_rust_binary_release(
name = "linux",
src = ":unpack",
os = "linux",
visibility = ["//tools/release:__pkg__"],
)
20 changes: 20 additions & 0 deletions py/tools/venv_bin/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@rules_rust//rust:defs.bzl", "rust_binary")
load("//tools/release:defs.bzl", "multi_arch_rust_binary_release")

rust_binary(
name = "venv_bin",
Expand All @@ -14,3 +15,22 @@ rust_binary(
"@crate_index//:miette",
],
)

alias(
name = "venv",
actual = ":venv_bin",
)

multi_arch_rust_binary_release(
name = "macos",
src = ":venv",
os = "macos",
visibility = ["//tools/release:__pkg__"],
)

multi_arch_rust_binary_release(
name = "linux",
src = ":venv",
os = "linux",
visibility = ["//tools/release:__pkg__"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
exports_files(
["clang_ldd.patch"],
visibility = ["//visibility:public"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
diff --git a/toolchain/cc_toolchain_config.bzl b/toolchain/cc_toolchain_config.bzl
index b2aa92a..87c6614 100644
--- a/toolchain/cc_toolchain_config.bzl
+++ b/toolchain/cc_toolchain_config.bzl
@@ -112,8 +112,6 @@ def cc_toolchain_config(
"-fdebug-prefix-map={}=__bazel_toolchain_llvm_repo__/".format(toolchain_path_prefix),
]

- is_xcompile = not (host_os == target_os and host_arch == target_arch)
-
# Default compiler flags:
compile_flags = [
"--target=" + target_system_name,
@@ -149,12 +147,15 @@ def cc_toolchain_config(
# unused symbols are not stripped.
link_libs = []

+ use_lld = True
+
# Linker flags:
- if host_os == "darwin" and not is_xcompile:
+ if host_os == "darwin":
# lld is experimental for Mach-O, so we use the native ld64 linker.
# TODO: How do we cross-compile from Linux to Darwin?
use_lld = False
link_flags.extend([
+ "-mmacosx-version-min=11.0",
"-headerpad_max_install_names",
"-fobjc-link-runtime",
])
@@ -175,8 +176,8 @@ def cc_toolchain_config(
# always link C++ libraries.
cxx_standard = compiler_configuration["cxx_standard"]
stdlib = compiler_configuration["stdlib"]
- if stdlib == "builtin-libc++" and is_xcompile:
- stdlib = "stdc++"
+ # if stdlib == "builtin-libc++":
+ # stdlib = "stdc++"
if stdlib == "builtin-libc++":
cxx_flags = [
"-std=" + cxx_standard,
@@ -186,11 +187,11 @@ def cc_toolchain_config(
# For single-platform builds, we can statically link the bundled
# libraries.
link_flags.extend([
- "-l:libc++.a",
- "-l:libc++abi.a",
- "-l:libunwind.a",
+ # "-l:libc++.a",
+ # "-l:libc++abi.a",
+ # "-l:libunwind.a",
# Compiler runtime features.
- "-rtlib=compiler-rt",
+ # "-rtlib=compiler-rt",
# To support libunwind.
"-lpthread",
"-ldl",
@@ -207,7 +208,7 @@ def cc_toolchain_config(
# have the sysroot directory on the search path and then add the
# toolchain directory back after we are done.
link_flags.extend([
- "-L{}/usr/lib".format(compiler_configuration["sysroot_path"]),
+ # "-L{}/usr/lib".format(compiler_configuration["sysroot_path"]),
"-lc++",
"-lc++abi",
])
42 changes: 42 additions & 0 deletions tools/release/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[
platform(
name = "{}_{}".format(os, cpu),
constraint_values = [
"@platforms//os:" + os,
"@platforms//cpu:" + cpu,
],
visibility = ["//py/tools:__subpackages__"],
)
for os in [
"linux",
"macos",
]
for cpu in [
"aarch64",
"x86_64",
]
]

LINUX_ARTIFACTS = [
"//py/tools/unpack_bin:linux",
"//py/tools/venv_bin:linux",
]

MACOS_ARTIFACTS = [
"//py/tools/unpack_bin:macos",
"//py/tools/venv_bin:macos",
]

sh_binary(
name = "copy_release_artifacts",
srcs = ["copy_release_artifacts.sh"],
args = select({
"@platforms//os:linux": ["$(rlocationpaths {})".format(s) for s in LINUX_ARTIFACTS],
"@platforms//os:macos": ["$(rlocationpaths {})".format(s) for s in MACOS_ARTIFACTS],
}),
data = select({
"@platforms//os:linux": LINUX_ARTIFACTS,
"@platforms//os:macos": MACOS_ARTIFACTS,
}),
deps = ["@bazel_tools//tools/bash/runfiles"],
)
24 changes: 24 additions & 0 deletions tools/release/copy_release_artifacts.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash
# Copies release files from bazel-out to a common folder so the GitHub Actions
# configuration can easily find them all

# --- begin runfiles.bash initialization v3 ---
# Copy-pasted from the Bazel Bash runfiles library v3.
set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
source "$0.runfiles/$f" 2>/dev/null || \
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
# --- end runfiles.bash initialization v3 ---

if [[ -z "${DEST:-}" ]]; then
>&2 echo "ERROR: specify DEST environment variable"
exit 1
fi

cd $BUILD_WORKSPACE_DIRECTORY
for arg in "$@"
do cp -pv "$(rlocation $arg)" $DEST
done
42 changes: 42 additions & 0 deletions tools/release/defs.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"Make releases for platforms supported by rules_py"

load("@aspect_bazel_lib//lib:transitions.bzl", "platform_transition_filegroup")
load("@aspect_bazel_lib//lib:copy_file.bzl", "copy_file")
load("@aspect_bazel_lib//tools/release:hashes.bzl", "hashes")

# buildozer: disable=function-docstring
def multi_arch_rust_binary_release(name, src, os, archs = ["aarch64", "x86_64"], **kwargs):
outs = []
for arch in archs:
bin = Label(src).name
platform_transition_filegroup(
name = "{}_{}_{}_build".format(bin, os, arch),
srcs = [src],
target_platform = "//tools/release:{}_{}".format(os, arch),
target_compatible_with = ["@platforms//os:{}".format(os)],
)

artifact = "{}-{}-{}".format(bin, os, arch)
outs.append(artifact)
copy_file(
name = "copy_{}_{}_{}".format(bin, os, arch),
src = "{}_{}_{}_build".format(bin, os, arch),
out = artifact,
target_compatible_with = ["@platforms//os:{}".format(os)],
)

hash_file = "{}_{}_{}.sha256".format(bin, os, arch)
outs.append(hash_file)
hashes(
name = hash_file,
src = artifact,
target_compatible_with = ["@platforms//os:{}".format(os)],
)

native.filegroup(
name = name,
srcs = outs,
target_compatible_with = ["@platforms//os:{}".format(os)],
tags = ["manual"],
**kwargs
)
Loading

0 comments on commit 9e2c653

Please sign in to comment.