Skip to content

Commit

Permalink
wip: rules_rust
Browse files Browse the repository at this point in the history
  • Loading branch information
jez committed May 10, 2020
1 parent 5676b66 commit e7df6d0
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# ---- Global C/C++ build options ---------------------------------------------

# Using a custom C++ toolchain to get around an issue with rules_rust on macOS:
# https://github.com/bazelbuild/rules_rust/issues/226
build --crosstool_top=@llvm_toolchain//:toolchain
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target
/bazel-*
51 changes: 51 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
BAZEL_VERSION = "3.1.0"

workspace(name = "io_jez_rs_as_tree")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "com_grail_bazel_toolchain",
url = "https://github.com/grailbio/bazel-toolchain/archive/0.5.zip",
sha256 = "33011cd77021017294d1982b48d0c3a081c777b9aad9a75f3e4eada0e10894d1",
strip_prefix = "bazel-toolchain-0.5",
)

load("@com_grail_bazel_toolchain//toolchain:deps.bzl", "bazel_toolchain_dependencies")
bazel_toolchain_dependencies()

load("@com_grail_bazel_toolchain//toolchain:rules.bzl", "llvm_toolchain")
llvm_toolchain(
name = "llvm_toolchain",
absolute_paths = True,
llvm_version = "9.0.0",
)

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

# rules_rust (or maybe Bazel itself?)
http_archive(
name = "io_bazel_rules_rust",
sha256 = "800ffbce5af3f196448b4844b8ad32f82f6ff1cda192ebf5edd5f5a9d132f348",
strip_prefix = "rules_rust-6835a3c8ed1dcd67040cccd603ff3daf611ce41c",
urls = [
"https://github.com/bazelbuild/rules_rust/archive/6835a3c8ed1dcd67040cccd603ff3daf611ce41c.zip",
],
)

http_archive(
name = "bazel_skylib",
sha256 = "9a737999532daca978a158f94e77e9af6a6a169709c0cee274f0a4c3359519bd",
strip_prefix = "bazel-skylib-1.0.0",
url = "https://github.com/bazelbuild/bazel-skylib/archive/1.0.0.tar.gz",
)

load("@io_bazel_rules_rust//rust:repositories.bzl", "rust_repositories")
rust_repositories()

load("@io_bazel_rules_rust//:workspace.bzl", "bazel_version")
bazel_version(name = "bazel_version")

BAZEL_INSTALLER_VERSION_darwin_SHA = "5cfa97031b43432b3c742c80e2e01c41c0acdca7ba1052fc8cf1e291271bc9cd"
BAZEL_INSTALLER_VERSION_linux_SHA = "7ba815cbac712d061fe728fef958651512ff394b2708e89f79586ec93d1185ed"
93 changes: 93 additions & 0 deletions bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#!/bin/bash

#
# ./bazel
#
# Reads the BAZEL_VERSION variable in the WORKSPACE file and executes the
# corresponding Bazel version. Downloads and installs it if needed.
#

set -euo pipefail

if [ "${LOCAL_BAZEL_OVERRIDE:-}" != "" ]; then
exec "$LOCAL_BAZEL_OVERRIDE" "$@"
fi

old_pwd="$PWD"
cd "$(dirname "${BASH_SOURCE[0]}")"

if ! [ -f WORKSPACE ]; then
echo >&2 "Didn't find the workspace"
exit 1
fi

repo_root="$PWD"
cd "$old_pwd"

workspace_contents=$(< "$repo_root/WORKSPACE")

bazel_version_regex='BAZEL_VERSION = "([^"]+)"'
if [[ $workspace_contents =~ $bazel_version_regex ]]; then
export BAZEL_VERSION="${BASH_REMATCH[1]}"
else
echo >&2 "$0: Failed to extract BAZEL_VERSION from WORKSPACE"
exit 1
fi

XDG_CACHE_HOME="${XDG_CACHE_HOME:-$HOME/.cache}"
bazel_bin_loc="${bazel_bin_loc:-$XDG_CACHE_HOME/bazel_binaries}"
bazel_exec_path="$bazel_bin_loc/$BAZEL_VERSION/bin/bazel-real"

if [ -f "$bazel_exec_path" ]; then
exec "$bazel_exec_path" "$@"
fi

# ----- slow path -----

echo >&2 "No cached Bazel v$BAZEL_VERSION found, installing..."

case "$(uname -s)" in
Linux) bazel_launcher_platform_name='linux' ;;
Darwin) bazel_launcher_platform_name='darwin' ;;
*) echo >&2 "Your platform $(uname -s) is unsupported, sorry" && exit 1 ;;
esac

bazel_installer_sha_regex="BAZEL_INSTALLER_VERSION_${bazel_launcher_platform_name}_SHA = \"([^\"]+)\""
if [[ $workspace_contents =~ $bazel_installer_sha_regex ]]; then
export expected_sha="${BASH_REMATCH[1]}"
else
echo >&2 "$0: Failed to extract Bazel version from WORKSPACE"
exit 1
fi

BUILD_DIR="$(mktemp -d)"
export BUILD_DIR
mkdir -p "$BUILD_DIR"

(
set -euo pipefail
cd "$BUILD_DIR"
echo "$PWD"

installer_name="bazel-${BAZEL_VERSION}-installer-${bazel_launcher_platform_name}-x86_64.sh"
BAZEL_REMOTE_SOURCE="${BAZEL_REMOTE_SOURCE:-https://github.com/bazelbuild/bazel/releases/download}"
BAZEL_INSTALLER_PATH="${BAZEL_INSTALLER_PATH:-$BAZEL_REMOTE_SOURCE/${BAZEL_VERSION}/$installer_name}"

curl -O -L "$BAZEL_INSTALLER_PATH"

actual_sha="$(shasum -a 256 "$installer_name" | awk '{print $1}')"
if [ "$actual_sha" != "$expected_sha" ]; then
echo >&2 "Installer checksum mismatch:"
echo >&2 " Expected: $expected_sha"
echo >&2 " Actual: $actual_sha"
echo >&2 "To accept this mismatch, update the SHA in the WORKSPACE file and re-run."
exit 1
fi

chmod +x "$installer_name"
mkdir -p "$bazel_bin_loc"
"./${installer_name}" --base="${bazel_bin_loc}/${BAZEL_VERSION}" --bin="${bazel_bin_loc}/${BAZEL_VERSION}/bin_t"
)
rm -rf "$BUILD_DIR"

exec "$bazel_exec_path" "$@"
1 change: 1 addition & 0 deletions src/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ rust_binary(
name = "as-tree",
srcs = ["main.rs"],
deps = [],
visibility = ["//visibility:public"],
)
1 change: 1 addition & 0 deletions tools/bazel

0 comments on commit e7df6d0

Please sign in to comment.