#!/usr/bin/env bash # Copyright (c) 2016-2024 The Hush developers # Distributed under the GPLv3 software license, see the accompanying # file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html set -eu -o pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" BUILD_TARGET_FILE="$SCRIPT_DIR/src/.build_target" # Function to check and clean if target changed check_and_clean_target() { local new_target="$1" local current_target="" if [[ -f "$BUILD_TARGET_FILE" ]]; then current_target=$(cat "$BUILD_TARGET_FILE") fi if [[ -n "$current_target" && "$current_target" != "$new_target" ]]; then echo "Build target changed from '$current_target' to '$new_target'" echo "Cleaning old build artifacts to prevent cross-compilation contamination..." cd "$SCRIPT_DIR" make distclean 2>/dev/null || true # Clean leveldb separately as it often causes issues if [[ -d src/leveldb ]]; then make -C src/leveldb clean 2>/dev/null || true fi # Clean depends when switching between Docker and native builds, # since Docker builds bake container paths into .la files local TRIPLET TRIPLET=$(./depends/config.guess 2>/dev/null || true) if [[ -n "$TRIPLET" && -d "depends/$TRIPLET" ]]; then echo "Cleaning depends/$TRIPLET to avoid path contamination..." # Use sudo if files are root-owned (from Docker builds) if [[ "$(stat -c '%u' "depends/$TRIPLET" 2>/dev/null)" == "0" ]]; then sudo rm -rf "depends/$TRIPLET" "depends/built/$TRIPLET" else rm -rf "depends/$TRIPLET" "depends/built/$TRIPLET" fi elif [[ -n "$TRIPLET" && -d "depends/built/$TRIPLET" ]]; then echo "Cleaning depends/built/$TRIPLET to avoid path contamination..." if [[ "$(stat -c '%u' "depends/built/$TRIPLET" 2>/dev/null)" == "0" ]]; then sudo rm -rf "depends/built/$TRIPLET" else rm -rf "depends/built/$TRIPLET" fi fi echo "Clean complete." fi # Record the new target echo "$new_target" > "$BUILD_TARGET_FILE" } # Check for --win-release flag for cross-compilation if [[ "${1:-}" == "--win-release" ]]; then check_and_clean_target "windows" shift ./util/build-win.sh "$@" exit $? fi # Check for --linux-release flag (builds inside Ubuntu 20.04 container for max compatibility) if [[ "${1:-}" == "--linux-release" ]]; then check_and_clean_target "linux-docker" shift echo "Building Linux release inside Ubuntu 20.04 Docker container..." sudo docker build -t hush-builder -f Dockerfile.build . sudo docker run --rm -e HOST_UID=$(id -u) -e HOST_GID=$(id -g) -v "$SCRIPT_DIR:/hush3" hush-builder "$@" VERSION=$(grep -oP 'define\(_CLIENT_VERSION.*?,\s*\K[0-9]+' configure.ac | head -3 | tr '\n' '.' | sed 's/\.$//') VERSION=${VERSION:-3.10.5} echo "Docker build complete. Release is in release/${VERSION}-linux-amd64/" exit $? fi # Check for --mac-release flag for macOS release build (must be run on macOS) if [[ "${1:-}" == "--mac-release" ]]; then check_and_clean_target "macos-release" shift if [[ "$OSTYPE" != "darwin"* ]]; then echo "ERROR: --mac-release must be run on a native macOS system." echo " Copy this repo to a Mac and run: ./build.sh --mac-release $*" exit 1 fi echo "Building macOS release natively..." ./util/build-mac.sh "$@" echo "macOS release build complete." exit $? fi # run correct build script for detected OS if [[ "$OSTYPE" == "linux-gnu"* ]]; then check_and_clean_target "linux" ./util/build.sh --disable-tests "$@" elif [[ "$OSTYPE" == "darwin"* ]]; then check_and_clean_target "macos" ./util/build-mac.sh --disable-tests "$@" elif [[ "$OSTYPE" == "msys"* ]]; then check_and_clean_target "windows" ./util/build-win.sh --disable-tests "$@" #elif [[ "$OSTYPE" == "freebsd"* ]]; then # placeholder else echo "Unable to detect your OS. What are you using?" fi