Skip to content

Commit b478bbd

Browse files
committed
Export Rust build environment for use by other buildpacks
See emk#9. The theory here is that we might have a Ruby application that uses a gem implemented in Rust, and in order to compile the gem, the Ruby buildpack needs access to the Rust compiler. To support this, we need to create an `export` file in our buildpack directory containing all the environment variables needed to run `cargo` and `rustc`. Note that this still has some limitations: You almost certainly need to have a valid `Cargo.toml` in your project for the buildpack to detect and build before you can run the other buildpack that needs Rust.
1 parent e441fb7 commit b478bbd

File tree

3 files changed

+35
-19
lines changed

3 files changed

+35
-19
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/.vagrant/
22
/cache/
33
/heroku-rust-cargo-hello/
4+
/export

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ fall of 2016. Features include:
55
- Caching of builds between deployments.
66
- Automatic updates to the latest stable Rust by default.
77
- Optional pinning of Rust to a specific version.
8+
- Support for `export` so that other buildpacks can access the Rust
9+
toolchain.
810

911
For an example project, see [heroku-rust-cargo-hello][].
1012

bin/compile

+32-19
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
#!/bin/bash
22

3+
# Find all the directories we might need (based on
4+
# heroku-buildpack-nodejs code).
5+
BUILD_DIR=${1:-}
6+
CACHE_DIR=${2:-}
7+
ENV_DIR=${3:-}
8+
BP_DIR=$(cd $(dirname ${0:-}); cd ..; pwd)
9+
310
# Set defaults for our configuration variables. Stable Rust is sufficiently
411
# stable at this point that I think we can just default it.
512
VERSION=stable
613

714
# Load our configuration variables, if any were specified.
8-
if [ -f "$1/RustConfig" ]; then
9-
. "$1/RustConfig"
15+
if [ -f "$BUILD_DIR/RustConfig" ]; then
16+
. "$BUILD_DIR/RustConfig"
1017
fi
1118

1219
# Standard paranoia.
@@ -20,7 +27,7 @@ fi
2027

2128
# Notify users running old, unstable versions of Rust about how to deploy
2229
# successfully.
23-
if [ ! -z ${CARGO_URL+x} ] || [ ! -f "$1/Cargo.toml" ]; then
30+
if [ ! -z ${CARGO_URL+x} ] || [ ! -f "$BUILD_DIR/Cargo.toml" ]; then
2431
>&2 cat <<EOF
2532
To deploy a modern Rust app, make sure you have a Cargo.toml file, and that
2633
you do not define CARGO_URL or CARGO_VERSION in RustConfig. If you're
@@ -42,23 +49,29 @@ EOF
4249
exit 1
4350
fi
4451

45-
# Switch to our cache directory.
46-
mkdir -p "$2"
47-
cd "$2"
48-
49-
# Rustup wants to create `~/.multirust` and fill it with toolchains. If we
50-
# set $HOME to our cache directory, it will do the right thing.
51-
export HOME="`pwd`"
52+
# Record our Rust build environment configuration in an export file, in
53+
# case another buildpack needs it to build Ruby gems that use Rust or
54+
# something like that.
55+
cat <<EOF > $BP_DIR/export
56+
# Our rustup installation.
57+
export RUSTUP_HOME="$CACHE_DIR/multirust"
5258
53-
# This is where we will cache our downloaded compilers, as well as git
54-
# repositories downloaded by Cargo. We implicitly trust Rustup and Cargo
59+
# Our cargo installation. We implicitly trust Rustup and Cargo
5560
# to do the right thing when new versions are released.
56-
export CARGO_HOME="`pwd`/cargo"
61+
export CARGO_HOME="$CACHE_DIR/cargo"
62+
63+
# Include binaries installed by cargo and rustup in our path.
64+
PATH="\$CARGO_HOME/bin:\$PATH"
65+
EOF
66+
67+
# Read our build environment back in and evaluate it so that we can use it.
68+
. $BP_DIR/export
69+
70+
# Switch to our cache directory.
71+
mkdir -p "$CACHE_DIR"
72+
cd "$CACHE_DIR"
5773

58-
# Make sure we have the correct Rust binaries and set up PATH. We don't
59-
# skip this step even if $RUST_HOME exists, because rustup may want to
60-
# update our channel.
61-
PATH="$CARGO_HOME/bin:$PATH"
74+
# Make sure we have an appropriate Rust toolchain installed.
6275
if [ -d "$CARGO_HOME" ]; then
6376
echo "-----> Checking for new releases of Rust $VERSION channel"
6477
# It's possible that $VERSION has changed, or the `stable` channel has updated.
@@ -81,7 +94,7 @@ fi
8194
# This is where we will cache our Rust output. Note the suggestions at
8295
# https://github.com/alexcrichton/cargo/commit/014765f788ca1c2476936835ca32cc2746f99b42
8396
# which describe how this needs to be named.
84-
export CARGO_TARGET_DIR="$2/target"
97+
export CARGO_TARGET_DIR="$CACHE_DIR/target"
8598

8699
# Build our project (into CARGO_TARGET_DIR so we have caching) and copy it
87100
# back to the source tree. In theory, we could probably just copy the
@@ -92,7 +105,7 @@ export CARGO_TARGET_DIR="$2/target"
92105
#export RUST_LOG="cargo::sources::git=debug"
93106
# To debug compiler and linking issues, add `--verbose`.
94107
echo "-----> Building application using Cargo"
95-
cd "$1"
108+
cd "$BUILD_DIR"
96109
rm -rf target/
97110
cargo build --release
98111
cp -a "$CARGO_TARGET_DIR" target

0 commit comments

Comments
 (0)