๐Ÿ“ฆ Turbo87 / heroku-buildpack-crates-io

๐Ÿ“„ compile ยท 98 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98#!/bin/bash

# Find all the directories we might need (based on
# heroku-buildpack-nodejs code).
BUILD_DIR=${1:-}
CACHE_DIR=${2:-}
ENV_DIR=${3:-}
BP_DIR=$(cd $(dirname ${0:-}); cd ..; pwd)

# Export DATABASE_URL at build time, mostly because Diesel is the best way to
# do SQL in Rust right now, and Diesel will use it to generate code for the
# database schema.
if [ -e "$ENV_DIR/DATABASE_URL" ]; then
  export DATABASE_URL="$(cat $ENV_DIR/DATABASE_URL)";
fi

# If your Rust code is not at the root directory of the repository, specify a
# `BUILD_PATH` to the correct directory in the `RustConfig`
BUILD_PATH=""

# Default build flags to pass to `cargo build`.
RUST_CARGO_BUILD_FLAGS="--release"

# Load our configuration variables, if any were specified.
if [ -f "$BUILD_DIR/RustConfig" ]; then
    . "$BUILD_DIR/RustConfig"
fi

# Standard paranoia.
set -eu

# Record our Rust build environment configuration in an export file, in
# case another buildpack needs it to build Ruby gems that use Rust or
# something like that.
cat <<EOF > $BP_DIR/export
# Our rustup installation.
export RUSTUP_HOME="$CACHE_DIR/rustup"

# Our cargo installation.  We implicitly trust Rustup and Cargo
# to do the right thing when new versions are released.
export CARGO_HOME="$CACHE_DIR/cargo"

# Include binaries installed by cargo and rustup in our path.
PATH="\$CARGO_HOME/bin:\$PATH"
EOF

# Read our build environment back in and evaluate it so that we can use it.
. $BP_DIR/export

# Switch to our cache directory.
mkdir -p "$CACHE_DIR"
cd "$CACHE_DIR"

# Make sure we have an appropriate Rust toolchain installed.
if [ -d "$CARGO_HOME" ]; then
    echo "-----> Checking for new releases of rustup"
    rustup self update

    echo "-----> Checking for new releases of Rust"
    rustup update
else
    echo "-----> Downloading rustup"
    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs > rustup.sh
    chmod u+x rustup.sh

    echo "-----> Installing rustup"
    ./rustup.sh -y --default-toolchain none
    rm rustup.sh
fi

if [ ! -x "$CARGO_HOME/bin/rustc" ]; then
  echo "failed: Cannot find Rust binaries at $CARGO_HOME"
  exit 1
fi

# This is where we will cache our Rust output.  Note the suggestions at
# https://github.com/alexcrichton/cargo/commit/014765f788ca1c2476936835ca32cc2746f99b42
# which describe how this needs to be named.
export CARGO_TARGET_DIR="$CACHE_DIR/target"

# Build our project (into CARGO_TARGET_DIR so we have caching) and copy it
# back to the source tree.  In theory, we could probably just copy the
# binary or do something clever with `cargo install`, but we haven't
# figured that out yet.
#
# To debug git issues:
#export RUST_LOG="cargo::sources::git=debug"
# To debug compiler and linking issues, add `--verbose`.
echo "-----> Building application using Cargo"
cd "$BUILD_DIR/$BUILD_PATH"
rm -rf target/
cargo build $RUST_CARGO_BUILD_FLAGS
mkdir -p target/release

# Copy compiled binaries from the cached `target` folder so that they are
# available at e.g. `./target/release/foo`
find "$CARGO_TARGET_DIR/release" -maxdepth 1 -type f -executable -exec cp -a -t target/release {} \;