From 9f8ebcf1833e2f56c86df854c915385b0e9f43af Mon Sep 17 00:00:00 2001 From: matejcik Date: Thu, 18 Apr 2019 14:52:24 +0200 Subject: [PATCH] build: make build scripts work from monorepo root incidentally fixes #9 and the dreaded non-bug #8 --- .gitignore | 1 + core/build-docker.sh | 23 +++++++++---- legacy/build.sh | 17 ++++++---- legacy/script/bootstrap | 2 +- legacy/script/fullbuild | 71 ++++++++++++++++++++++++----------------- 5 files changed, 70 insertions(+), 44 deletions(-) diff --git a/.gitignore b/.gitignore index 809002621..492144ebd 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ .vscode/ __pycache__/ *.pyc +/build diff --git a/core/build-docker.sh b/core/build-docker.sh index deec6c7bd..2543b645a 100755 --- a/core/build-docker.sh +++ b/core/build-docker.sh @@ -1,6 +1,8 @@ #!/bin/sh set -e +cd "$(dirname $0)/.." + if [ "$1" = "--gcc_source" ]; then TOOLCHAIN_FLAVOR=src shift @@ -9,23 +11,30 @@ else fi IMAGE=trezor-core-build.$TOOLCHAIN_FLAVOR -TAG=${1:-master} +if [ -z "$1" ]; then + TAG=master +else + TAG=core/${1} +fi REPOSITORY=${2:-trezor} PRODUCTION=${PRODUCTION:-0} if [ "$REPOSITORY" = "local" ]; then REPOSITORY=file:///local/ else - REPOSITORY=https://github.com/$REPOSITORY/trezor-core.git + REPOSITORY=https://github.com/$REPOSITORY/trezor-firmware.git fi -docker build -t $IMAGE --build-arg TOOLCHAIN_FLAVOR=$TOOLCHAIN_FLAVOR . +docker build -t $IMAGE --build-arg TOOLCHAIN_FLAVOR=$TOOLCHAIN_FLAVOR core + +USER=$(ls -lnd . | awk '{ print $3 }') +GROUP=$(ls -lnd . | awk '{ print $4 }') -mkdir -p $(pwd)/build-docker -docker run -t -v $(pwd):/local -v $(pwd)/build-docker:/build:z --user="$(stat -c "%u:%g" .)" $IMAGE /bin/sh -c "\ +mkdir -p $(pwd)/build/core +docker run -t -v $(pwd):/local -v $(pwd)/build/core:/build:z --user="$USER:$GROUP" $IMAGE /bin/sh -c "\ cd /tmp && \ - git clone $REPOSITORY trezor-core && \ - cd trezor-core && \ + git clone $REPOSITORY trezor-firmware && \ + cd trezor-firmware/core && \ ln -s /build build && git checkout $TAG && \ git submodule update --init --recursive && \ diff --git a/legacy/build.sh b/legacy/build.sh index 6afb96c5d..02a3e6f46 100755 --- a/legacy/build.sh +++ b/legacy/build.sh @@ -1,19 +1,24 @@ #!/bin/bash set -e +cd "$(dirname $0)/.." + BOOTLOADER_COMMIT=${1:-HEAD} FIRMWARE_COMMIT=${2:-HEAD} if [ "$BOOTLOADER_COMMIT" = "EMU" ]; then - export EMULATOR=1 + export EMULATOR=1 fi if [ "$EMULATOR" = 1 ]; then - IMAGE=trezor-mcu-emulator + IMAGE=trezor-mcu-emulator else - IMAGE=trezor-mcu-build + IMAGE=trezor-mcu-build fi -docker build -t "$IMAGE" --build-arg EMULATOR=$EMULATOR . -docker run -it -v $(pwd):/src:z --user="$(stat -c "%u:%g" .)" "$IMAGE" \ - /src/script/fullbuild "$BOOTLOADER_COMMIT" "$FIRMWARE_COMMIT" +USER=$(ls -lnd . | awk '{ print $3 }') +GROUP=$(ls -lnd . | awk '{ print $4 }') + +docker build -t "$IMAGE" --build-arg EMULATOR=$EMULATOR legacy +docker run -it -v $(pwd):/src:z --user="$USER:$GROUP" "$IMAGE" \ + /src/legacy/script/fullbuild "$BOOTLOADER_COMMIT" "$FIRMWARE_COMMIT" diff --git a/legacy/script/bootstrap b/legacy/script/bootstrap index 500e9a061..617aa6911 100755 --- a/legacy/script/bootstrap +++ b/legacy/script/bootstrap @@ -5,6 +5,6 @@ set -e -cd "$(dirname "$0")/.." +cd "$(dirname "$0")/../.." git submodule update --init --recursive diff --git a/legacy/script/fullbuild b/legacy/script/fullbuild index f8ddb8742..193d52d54 100755 --- a/legacy/script/fullbuild +++ b/legacy/script/fullbuild @@ -9,22 +9,24 @@ export LANG=C.UTF-8 set -eu -cd "$(dirname "$0")/.." +cd "$(dirname "$0")/../.." + +readonly MCU_ROOT=legacy readonly ARTIFACT_EXTENSIONS=(bin elf) readonly BUILD_DIR="$(readlink -f build)" -readonly BOOTLOADER_DIR="$BUILD_DIR/bootloader" -readonly BOOTLOADER_FILENAME="bootloader/bootloader.bin" +readonly BOOTLOADER_DIR="$BUILD_DIR/legacy/bootloader" +readonly BOOTLOADER_FILENAME="$MCU_ROOT/bootloader/bootloader.bin" readonly BOOTLOADER_PATH="$BOOTLOADER_DIR/$BOOTLOADER_FILENAME" -readonly FIRMWARE_DIR="$BUILD_DIR/firmware" -readonly FIRMWARE_FILENAME="firmware/trezor.bin" +readonly FIRMWARE_DIR="$BUILD_DIR/legacy/firmware" +readonly FIRMWARE_FILENAME="$MCU_ROOT/firmware/trezor.bin" readonly FIRMWARE_PATH="$FIRMWARE_DIR/$FIRMWARE_FILENAME" readonly EMULATOR_DIR="$FIRMWARE_DIR" -readonly EMULATOR_FILENAME="firmware/trezor-emulator.elf" -readonly EMULATOR_PATH="$EMULATOR_DIR/firmware/trezor.elf" +readonly EMULATOR_FILENAME="$MCU_ROOT/firmware/trezor-emulator.elf" +readonly EMULATOR_PATH="$EMULATOR_DIR/$MCU_ROOT/firmware/trezor.elf" worktree_setup() { local path="$1" @@ -36,27 +38,25 @@ worktree_setup() { # Use `git rev-parse` so that we can use any reference from the working repository. git -C "$path" checkout "$(git rev-parse "$commit")" - ( cd "$path" && script/setup ) + ( cd "$path/$MCU_ROOT" && script/setup ) } worktree_build() { local path="$1" - if [ -e "$path/Pipfile" ]; then - pushd $path - if ! pipenv install; then - # older tags can fail because they don't have protobuf in Pipfile - pipenv run pip install "protobuf==3.4.0" - pipenv install - fi - pipenv run script/cibuild - popd - else - # even older tags don't have Pipfile! - # use current one + if [ ! -e "$path/$MCU_ROOT/Pipfile" ]; then + echo "Can't handle pre-monorepo tags properly. You will have to check out manually" + exit 1 + fi + + pushd $path/$MCU_ROOT + if ! pipenv install; then + # older tags can fail because they don't have protobuf in Pipfile + pipenv run pip install "protobuf==3.4.0" pipenv install - ( cd "$path" && pipenv run script/cibuild ) fi + pipenv run script/cibuild + popd } worktree_copy() { @@ -64,18 +64,23 @@ worktree_copy() { local filename="$2" local pattern="$3" - local describe="$(git -C "$path" describe --tags --match "$pattern")" + local describe="$(git -C "$path" describe --tags --match "legacy/$pattern")" + describe="${describe##legacy/}" local src="$path/$filename" local basename="$(basename "$filename")" local dest="$BUILD_DIR/${basename%.*}-$describe.${basename##*.}" - for extension in "${ARTIFACT_EXTENSIONS[@]}"; do - install -Dm0644 \ - "${src%.*}.$extension" \ - "${dest%.*}.$extension" - done + if [ "$EMULATOR" = 1 ]; then + install -Dm0644 "${src%.*}.elf" "${dest%.*}.elf" + else + for extension in "${ARTIFACT_EXTENSIONS[@]}"; do + install -Dm0644 \ + "${src%.*}.$extension" \ + "${dest%.*}.$extension" + done + fi printf "%s" "$dest" } @@ -84,7 +89,13 @@ main() { local bootloader_commit="$1" local firmware_commit="$2" - script/bootstrap + if [ "$bootloader_commit" != "HEAD" ]; then + bootloader_commit="legacy/$bootloader_commit" + fi + if [ "$firmware_commit" != "HEAD" ]; then + firmware_commit="legacy/$firmware_commit" + fi + worktree_setup "$FIRMWARE_DIR" "$firmware_commit" if [ "$EMULATOR" != 1 ]; then @@ -116,13 +127,13 @@ main() { "$BOOTLOADER_FILENAME" \ "bl*")" - printf "\n\n"; $PYTHON script/fingerprint \ + printf "\n\n"; $PYTHON $MCU_ROOT/script/fingerprint \ "$bootloader_path" \ --max-size 32768 \ --double fi - printf "\n\n"; $PYTHON script/fingerprint \ + printf "\n\n"; $PYTHON $MCU_ROOT/script/fingerprint \ "$firmware_path" \ --offset 256 \ --max-size 983296 # 256 + 64*1024 + 3*128*1024 + 4*128*1024