From 9e84f125a600f64fbb68041bd573027ef2251da0 Mon Sep 17 00:00:00 2001 From: Nicolas Mattia Date: Thu, 16 Jun 2022 13:08:45 +0200 Subject: [PATCH] Pass RUSTFLAGS in dependency build (#687) * 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. --- .github/actions/check-build/action.yml | 2 +- Dockerfile | 8 +- dfx.json | 2 +- scripts/build | 118 +++++++++++++++++++++++++ src/canister_tests/README.md | 2 +- src/canister_tests/src/framework.rs | 2 +- src/internet_identity/build.sh | 53 ----------- 7 files changed, 127 insertions(+), 60 deletions(-) create mode 100755 scripts/build delete mode 100755 src/internet_identity/build.sh diff --git a/.github/actions/check-build/action.yml b/.github/actions/check-build/action.yml index b2b4969f9d..a844017426 100644 --- a/.github/actions/check-build/action.yml +++ b/.github/actions/check-build/action.yml @@ -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 diff --git a/Dockerfile b/Dockerfile index 84d22c1321..4d80761de6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 @@ -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 @@ -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 diff --git a/dfx.json b/dfx.json index ba8882cd1a..23e530ec13 100644 --- a/dfx.json +++ b/dfx.json @@ -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": { diff --git a/scripts/build b/scripts/build new file mode 100755 index 0000000000..27ec694741 --- /dev/null +++ b/scripts/build @@ -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 diff --git a/src/canister_tests/README.md b/src/canister_tests/README.md index 0b87a76d1b..e473834fa6 100644 --- a/src/canister_tests/README.md +++ b/src/canister_tests/README.md @@ -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 diff --git a/src/canister_tests/src/framework.rs b/src/canister_tests/src/framework.rs index 79b2263cfe..ecf4731383 100644 --- a/src/canister_tests/src/framework.rs +++ b/src/canister_tests/src/framework.rs @@ -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) }; diff --git a/src/internet_identity/build.sh b/src/internet_identity/build.sh deleted file mode 100755 index f5b5cd4bb4..0000000000 --- a/src/internet_identity/build.sh +++ /dev/null @@ -1,53 +0,0 @@ -#!/usr/bin/env bash -set -euo pipefail - -# 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 - -# Compile frontend assets to dist -echo Compiling frontend assets -npm run build - -II_DIR="$(dirname "$0")" -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[@]}" - -CARGO_TARGET_DIR="${CARGO_TARGET_DIR:-$II_DIR/../../target/}" - -ic-cdk-optimizer \ - "$CARGO_TARGET_DIR/$TARGET/release/internet_identity.wasm" \ - -o "$II_DIR/../../internet_identity.wasm"