1#!/usr/bin/env bash 2export LC_ALL=C 3set -e -o pipefail 4 5# Check that environment variables assumed to be set by the environment are set 6echo "Building for platform triple ${HOST:?not set} with reference timestamp ${SOURCE_DATE_EPOCH:?not set}..." 7echo "At most ${MAX_JOBS:?not set} jobs will run at once..." 8 9##################### 10# Environment Setup # 11##################### 12 13# The depends folder also serves as a base-prefix for depends packages for 14# $HOSTs after successfully building. 15BASEPREFIX="${PWD}/depends" 16 17# Setup an output directory for our build 18OUTDIR="${OUTDIR:-${PWD}/output}" 19[ -e "$OUTDIR" ] || mkdir -p "$OUTDIR" 20 21# Setup the directory where our Bitcoin Core build for HOST will occur 22DISTSRC="${DISTSRC:-${PWD}/distsrc-${HOST}}" 23if [ -e "$DISTSRC" ]; then 24 echo "DISTSRC directory '${DISTSRC}' exists, probably because of previous builds... Aborting..." 25 exit 1 26else 27 mkdir -p "$DISTSRC" 28fi 29 30# Given a package name and an output name, return the path of that output in our 31# current guix environment 32store_path() { 33 grep --extended-regexp "/[^-]{32}-${1}-[^-]+${2:+-${2}}" "${GUIX_ENVIRONMENT}/manifest" \ 34 | head --lines=1 \ 35 | sed --expression='s|^[[:space:]]*"||' \ 36 --expression='s|"[[:space:]]*$||' 37} 38 39# Determine output paths to use in CROSS_* environment variables 40CROSS_GLIBC="$(store_path glibc-cross-${HOST})" 41CROSS_GLIBC_STATIC="$(store_path glibc-cross-${HOST} static)" 42CROSS_KERNEL="$(store_path linux-libre-headers-cross-${HOST})" 43CROSS_GCC="$(store_path gcc-cross-${HOST})" 44CROSS_GCC_LIBS=( "${CROSS_GCC}/lib/gcc/${HOST}"/* ) # This expands to an array of directories... 45CROSS_GCC_LIB="${CROSS_GCC_LIBS[0]}" # ...we just want the first one (there should only be one) 46 47# Set environment variables to point Guix's cross-toolchain to the right 48# includes/libs for $HOST 49# 50# NOTE: CROSS_C_INCLUDE_PATH is missing ${CROSS_GCC_LIB}/include-fixed, because 51# the limits.h in it is missing a '#include_next <limits.h>' 52# 53export CROSS_C_INCLUDE_PATH="${CROSS_GCC_LIB}/include:${CROSS_GLIBC}/include:${CROSS_KERNEL}/include" 54export CROSS_CPLUS_INCLUDE_PATH="${CROSS_GCC}/include/c++:${CROSS_GCC}/include/c++/${HOST}:${CROSS_GCC}/include/c++/backward:${CROSS_C_INCLUDE_PATH}" 55export CROSS_LIBRARY_PATH="${CROSS_GCC}/lib:${CROSS_GCC}/${HOST}/lib:${CROSS_GCC_LIB}:${CROSS_GLIBC}/lib:${CROSS_GLIBC_STATIC}/lib" 56 57# Sanity check CROSS_*_PATH directories 58IFS=':' read -ra PATHS <<< "${CROSS_C_INCLUDE_PATH}:${CROSS_CPLUS_INCLUDE_PATH}:${CROSS_LIBRARY_PATH}" 59for p in "${PATHS[@]}"; do 60 if [ ! -d "$p" ]; then 61 echo "'$p' doesn't exist or isn't a directory... Aborting..." 62 exit 1 63 fi 64done 65 66# Disable Guix ld auto-rpath behavior 67export GUIX_LD_WRAPPER_DISABLE_RPATH=yes 68 69# Make /usr/bin if it doesn't exist 70[ -e /usr/bin ] || mkdir -p /usr/bin 71 72# Symlink file and env to a conventional path 73[ -e /usr/bin/file ] || ln -s --no-dereference "$(command -v file)" /usr/bin/file 74[ -e /usr/bin/env ] || ln -s --no-dereference "$(command -v env)" /usr/bin/env 75 76# Determine the correct value for -Wl,--dynamic-linker for the current $HOST 77glibc_dynamic_linker=$( 78 case "$HOST" in 79 i686-linux-gnu) echo /lib/ld-linux.so.2 ;; 80 x86_64-linux-gnu) echo /lib64/ld-linux-x86-64.so.2 ;; 81 arm-linux-gnueabihf) echo /lib/ld-linux-armhf.so.3 ;; 82 aarch64-linux-gnu) echo /lib/ld-linux-aarch64.so.1 ;; 83 riscv64-linux-gnu) echo /lib/ld-linux-riscv64-lp64d.so.1 ;; 84 *) exit 1 ;; 85 esac 86) 87 88# Environment variables for determinism 89export QT_RCC_TEST=1 90export QT_RCC_SOURCE_DATE_OVERRIDE=1 91export TAR_OPTIONS="--owner=0 --group=0 --numeric-owner --mtime='@${SOURCE_DATE_EPOCH}' --sort=name" 92export TZ="UTC" 93 94#################### 95# Depends Building # 96#################### 97 98# Build the depends tree, overriding variables that assume multilib gcc 99make -C depends --jobs="$MAX_JOBS" HOST="$HOST" \ 100 ${V:+V=1} \ 101 ${SOURCES_PATH+SOURCES_PATH="$SOURCES_PATH"} \ 102 i686_linux_CC=i686-linux-gnu-gcc \ 103 i686_linux_CXX=i686-linux-gnu-g++ \ 104 i686_linux_AR=i686-linux-gnu-ar \ 105 i686_linux_RANLIB=i686-linux-gnu-ranlib \ 106 i686_linux_NM=i686-linux-gnu-nm \ 107 i686_linux_STRIP=i686-linux-gnu-strip \ 108 x86_64_linux_CC=x86_64-linux-gnu-gcc \ 109 x86_64_linux_CXX=x86_64-linux-gnu-g++ \ 110 x86_64_linux_AR=x86_64-linux-gnu-ar \ 111 x86_64_linux_RANLIB=x86_64-linux-gnu-ranlib \ 112 x86_64_linux_NM=x86_64-linux-gnu-nm \ 113 x86_64_linux_STRIP=x86_64-linux-gnu-strip \ 114 qt_config_opts_i686_linux='-platform linux-g++ -xplatform bitcoin-linux-g++' 115 116 117########################### 118# Source Tarball Building # 119########################### 120 121# Create the source tarball and move it to "${OUTDIR}/src" if not already there 122if [ -z "$(find "${OUTDIR}/src" -name 'bitcoin-*.tar.gz')" ]; then 123 ./autogen.sh 124 env CONFIG_SITE="${BASEPREFIX}/${HOST}/share/config.site" ./configure --prefix=/ 125 make dist GZIP_ENV='-9n' ${V:+V=1} 126 mkdir -p "${OUTDIR}/src" 127 mv "$(find "${PWD}" -name 'bitcoin-*.tar.gz')" "${OUTDIR}/src/" 128fi 129 130# Determine the full path to our source tarball 131SOURCEDIST="$(find "${OUTDIR}/src" -name 'bitcoin-*.tar.gz')" 132# Determine our distribution name (e.g. bitcoin-0.18.0) 133DISTNAME="$(basename "$SOURCEDIST" '.tar.gz')" 134 135########################### 136# Binary Tarball Building # 137########################### 138 139# Similar flags to Gitian 140CONFIGFLAGS="--enable-glibc-back-compat --enable-reduce-exports --disable-bench --disable-gui-tests" 141HOST_CFLAGS="-O2 -g -ffile-prefix-map=${PWD}=." 142HOST_CXXFLAGS="-O2 -g -ffile-prefix-map=${PWD}=." 143HOST_LDFLAGS="-Wl,--as-needed -Wl,--dynamic-linker=$glibc_dynamic_linker -static-libstdc++" 144 145# Make $HOST-specific native binaries from depends available in $PATH 146export PATH="${BASEPREFIX}/${HOST}/native/bin:${PATH}" 147( 148 cd "$DISTSRC" 149 150 # Extract the source tarball 151 tar --strip-components=1 -xf "${SOURCEDIST}" 152 153 # Configure this DISTSRC for $HOST 154 # shellcheck disable=SC2086 155 env CONFIG_SITE="${BASEPREFIX}/${HOST}/share/config.site" \ 156 ./configure --prefix=/ \ 157 --disable-ccache \ 158 --disable-maintainer-mode \ 159 --disable-dependency-tracking \ 160 ${CONFIGFLAGS} \ 161 CFLAGS="${HOST_CFLAGS}" \ 162 CXXFLAGS="${HOST_CXXFLAGS}" \ 163 LDFLAGS="${HOST_LDFLAGS}" 164 165 sed -i.old 's/-lstdc++ //g' config.status libtool src/univalue/config.status src/univalue/libtool 166 167 # Build Bitcoin Core 168 make --jobs="$MAX_JOBS" ${V:+V=1} 169 170 # Perform basic ELF security checks on a series of executables. 171 make -C src --jobs=1 check-security ${V:+V=1} 172 # Check that executables only contain allowed gcc, glibc and libstdc++ 173 # version symbols for Linux distro back-compatibility. 174 make -C src --jobs=1 check-symbols ${V:+V=1} 175 176 # Setup the directory where our Bitcoin Core build for HOST will be 177 # installed. This directory will also later serve as the input for our 178 # binary tarballs. 179 INSTALLPATH="${PWD}/installed/${DISTNAME}" 180 mkdir -p "${INSTALLPATH}" 181 # Install built Bitcoin Core to $INSTALLPATH 182 make install DESTDIR="${INSTALLPATH}" ${V:+V=1} 183 ( 184 cd installed 185 186 # Prune libtool and object archives 187 find . -name "lib*.la" -delete 188 find . -name "lib*.a" -delete 189 190 # Prune pkg-config files 191 rm -r "${DISTNAME}/lib/pkgconfig" 192 193 # Split binaries and libraries from their debug symbols 194 { 195 find "${DISTNAME}/bin" -type f -executable -print0 196 find "${DISTNAME}/lib" -type f -print0 197 } | xargs -0 -n1 -P"$MAX_JOBS" -I{} "${DISTSRC}/contrib/devtools/split-debug.sh" {} {} {}.dbg 198 199 cp "${DISTSRC}/doc/README.md" "${DISTNAME}/" 200 201 # Finally, deterministically produce {non-,}debug binary tarballs ready 202 # for release 203 find "${DISTNAME}" -not -name "*.dbg" -print0 \ 204 | sort --zero-terminated \ 205 | tar --create --no-recursion --mode='u+rw,go+r-w,a+X' --null --files-from=- \ 206 | gzip -9n > "${OUTDIR}/${DISTNAME}-${HOST}.tar.gz" \ 207 || ( rm -f "${OUTDIR}/${DISTNAME}-${HOST}.tar.gz" && exit 1 ) 208 find "${DISTNAME}" -name "*.dbg" -print0 \ 209 | sort --zero-terminated \ 210 | tar --create --no-recursion --mode='u+rw,go+r-w,a+X' --null --files-from=- \ 211 | gzip -9n > "${OUTDIR}/${DISTNAME}-${HOST}-debug.tar.gz" \ 212 || ( rm -f "${OUTDIR}/${DISTNAME}-${HOST}-debug.tar.gz" && exit 1 ) 213 ) 214) 215