Skip to content

Commit

Permalink
Pass RUSTFLAGS in dependency build (#687)
Browse files Browse the repository at this point in the history
* Pass RUSTFLAGS in dependency build

This instructs the Dockerfile to use the official build script, which
now takes an extra option for building only rust dependencies.

When introducing `--remap-path-prefix` I forgot to set the build flag
during the dependency build, the actual build in Docker would rebuild
most of the crates. Now the Dockerfile uses the build file as well.

I've also added a new script, `./scripts/build`, for ease of use.

* Drop src/i...dentity/build.sh

This moves all the build logic to ./scripts/build, which is more
standard than src/.../build.sh.
  • Loading branch information
nmattia authored Jun 16, 2022
1 parent 94b9049 commit 9e84f12
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 60 deletions.
2 changes: 1 addition & 1 deletion .github/actions/check-build/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ runs:
# run the build
- run: npm ci
shell: bash
- run: ./src/internet_identity/build.sh
- run: ./scripts/build
shell: bash

# check the hash
Expand Down
8 changes: 5 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ ENV CARGO_HOME=/cargo \
PATH=/cargo/bin:$PATH

# Install IC CDK optimizer
# (keep version in sync with src/internet_identity/build.sh)
# (keep version in sync with scripts/build)
RUN cargo install ic-cdk-optimizer --version 0.3.1

COPY ./scripts ./scripts

# Pre-build all cargo dependencies. Because cargo doesn't have a build option
# to build only the dependecies, we pretend that our project is a simple, empty
# `lib.rs`. When we COPY the actual files we make sure to `touch` lib.rs so
Expand All @@ -60,7 +62,7 @@ RUN mkdir -p src/internet_identity/src \
&& touch src/internet_identity_interface/src/lib.rs \
&& mkdir -p src/canister_tests/src \
&& touch src/canister_tests/src/lib.rs \
&& cargo build --manifest-path ./src/internet_identity/Cargo.toml --target wasm32-unknown-unknown --release -j1 \
&& ./scripts/build --only-dependencies \
&& rm -rf src

FROM deps as build
Expand All @@ -76,7 +78,7 @@ RUN touch src/internet_identity_interface/src/lib.rs
RUN touch src/canister_tests/src/lib.rs
RUN npm ci

RUN ./src/internet_identity/build.sh
RUN ./scripts/build
RUN sha256sum /internet_identity.wasm

FROM scratch AS scratch
Expand Down
2 changes: 1 addition & 1 deletion dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "custom",
"candid": "src/internet_identity/internet_identity.did",
"wasm": "internet_identity.wasm",
"build": "src/internet_identity/build.sh"
"build": "scripts/build"
}
},
"defaults": {
Expand Down
118 changes: 118 additions & 0 deletions scripts/build
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#!/usr/bin/env bash

set -euo pipefail

# Make sure we always run from the root
SCRIPTS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd "$SCRIPTS_DIR/.."

#########
# USAGE #
#########

function title() {
echo "Build Internet Identity"
}

function usage() {
cat << EOF
Usage:
$0 [--only-dependencies]
Options:
--only-dependencies only build rust dependencies (no js build, no wasm optimization)
EOF
}

function help() {
cat << EOF
Builds the Internet Identity canister.
NOTE: This requires a working rust toolchain as well as ic-cdk-optimizer.
EOF

}

ONLY_DEPS=

while [[ $# -gt 0 ]]
do
case "$1" in
-h|--help)
title
usage
help
exit 0
;;
--only-dependencies)
ONLY_DEPS=1
shift
;;
*)
echo "ERROR: unknown argument $1"
usage
echo
echo "Use 'build --help' for more information"
exit 1
;;
esac
done

# Checking for dependencies
# XXX: we currently cannot check for the exact version of ic-cdk-optimizer
# because of https://github.com/dfinity/cdk-rs/issues/181
# Once the issue is fixed, we can ensure that the correct version is installed
if ! command -v ic-cdk-optimizer
then
echo could not find ic-cdk-optimizer
echo "ic-cdk-optimizer version 0.3.1 is needed, please run the following command:"
echo " cargo install ic-cdk-optimizer --version 0.3.1"
exit 1
fi

if [ "$ONLY_DEPS" != "1" ]
then
# Compile frontend assets to dist
echo Compiling frontend assets
npm run build
fi

II_DIR="$PWD/src/internet_identity"
TARGET="wasm32-unknown-unknown"

# standardize source references
CARGO_HOME="${CARGO_HOME:-"$HOME/.cargo"}"
RUSTFLAGS="--remap-path-prefix $CARGO_HOME=/cargo"

cargo_build_args=(
--manifest-path "$II_DIR/Cargo.toml"
--target "$TARGET"
--release
-j1
)

# This enables the "dummy_captcha" feature which makes sure the captcha string
# is always "a".
# WARNING: this MUST be opt-in, because we DO NOT want this in production,
# EVAR.
if [ "${II_DUMMY_CAPTCHA:-}" == "1" ]
then
echo "USING DUMMY CAPTCHA"
cargo_build_args+=( --features dummy_captcha )
fi

echo Running cargo build "${cargo_build_args[@]}"
echo RUSTFLAGS: "$RUSTFLAGS"

RUSTFLAGS="$RUSTFLAGS" cargo build "${cargo_build_args[@]}"

if [ "$ONLY_DEPS" != "1" ]
then
CARGO_TARGET_DIR="${CARGO_TARGET_DIR:-$II_DIR/../../target/}"

ic-cdk-optimizer \
"$CARGO_TARGET_DIR/$TARGET/release/internet_identity.wasm" \
-o "./internet_identity.wasm"
fi
2 changes: 1 addition & 1 deletion src/canister_tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This is a test suite for the Internet Identity canister. To run the tests:

``` bash
# Make sure II is built with the "test" flavor
II_DUMMY_CAPTHA=1 ./src/internet_identity/build.sh
II_DUMMY_CAPTHA=1 ./scripts/build

# Make sure you have a copy of the latest release of II
curl -SL https://github.com/dfinity/internet-identity/releases/latest/download/internet_identity_test.wasm -o internet_identity_previous.wasm
Expand Down
2 changes: 1 addition & 1 deletion src/canister_tests/src/framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ lazy_static! {
I will look for it at {:?}, and you can specify another path with the environment variable II_WASM (note that I run from {:?}).
In order to build the Wasm module, please run the following command:
II_DUMMY_CAPTHA=1 ./src/internet_identity/build.sh
II_DUMMY_CAPTHA=1 ./scripts/build
", &def_path, &std::env::current_dir().map(|x| x.display().to_string()).unwrap_or("an unknown directory".to_string()));
get_wasm_path("II_WASM".to_string(), &def_path).expect(&err)
};
Expand Down
53 changes: 0 additions & 53 deletions src/internet_identity/build.sh

This file was deleted.

0 comments on commit 9e84f12

Please sign in to comment.