---
name: "fairbrix-core-linux-0.21"
enable_cache: true
distro: "ubuntu"
suites:
- "bionic"
architectures:
- "amd64"
packages:
# Common dependencies.
- "autoconf"
- "automake"
- "binutils"
- "bsdmainutils"
- "ca-certificates"
- "cmake"
- "curl"
- "faketime"
- "git"
- "libtool"
- "patch"
- "pkg-config"
- "python3"
# Cross compilation HOSTS:
#  - arm-linux-gnueabihf
- "binutils-arm-linux-gnueabihf"
- "g++-8-arm-linux-gnueabihf"
#  - aarch64-linux-gnu
- "binutils-aarch64-linux-gnu"
- "g++-8-aarch64-linux-gnu"
#  - riscv64-linux-gnu
- "binutils-riscv64-linux-gnu"
- "g++-8-riscv64-linux-gnu"
remotes:
- "url": "https://github.com/fairbrix-project/fairbrix.git"
  "dir": "fairbrix"
files: []
script: |
  set -e -o pipefail

  WRAP_DIR=$HOME/wrapped
  HOSTS="x86_64-linux-gnu arm-linux-gnueabihf aarch64-linux-gnu riscv64-linux-gnu"
  CONFIGFLAGS="--enable-glibc-back-compat --enable-reduce-exports --disable-bench --disable-gui-tests"
  FAKETIME_HOST_PROGS="gcc g++"
  FAKETIME_PROGS="date ar ranlib nm"
  HOST_CFLAGS="-O2 -g"
  HOST_CXXFLAGS="-O2 -g -Wno-psabi"
  HOST_LDFLAGS_BASE="-static-libstdc++ -Wl,-O2"

  export QT_RCC_TEST=1
  export QT_RCC_SOURCE_DATE_OVERRIDE=1
  export TZ="UTC"
  export BUILD_DIR="$PWD"
  mkdir -p ${WRAP_DIR}
  if test -n "$GBUILD_CACHE_ENABLED"; then
    export SOURCES_PATH=${GBUILD_COMMON_CACHE}
    export BASE_CACHE=${GBUILD_PACKAGE_CACHE}
    mkdir -p ${BASE_CACHE} ${SOURCES_PATH}
  fi

  # Use $LIB in LD_PRELOAD to avoid hardcoding the dir (See `man ld.so`)
  function create_global_faketime_wrappers {
  for prog in ${FAKETIME_PROGS}; do
    echo '#!/usr/bin/env bash' > ${WRAP_DIR}/${prog}
    echo "REAL=\`which -a ${prog} | grep -v ${WRAP_DIR}/${prog} | head -1\`" >> ${WRAP_DIR}/${prog}
    echo "export LD_PRELOAD='/usr/\$LIB/faketime/libfaketime.so.1'" >> ${WRAP_DIR}/${prog}
    echo "export FAKETIME=\"$1\"" >> ${WRAP_DIR}/${prog}
    echo "\$REAL \$@" >> $WRAP_DIR/${prog}
    chmod +x ${WRAP_DIR}/${prog}
  done
  }

  function create_per-host_faketime_wrappers {
  for i in $HOSTS; do
    for prog in ${FAKETIME_HOST_PROGS}; do
        if which ${i}-${prog}-8
        then
            echo '#!/usr/bin/env bash' > ${WRAP_DIR}/${i}-${prog}
            echo "REAL=\`which -a ${i}-${prog}-8 | grep -v ${WRAP_DIR}/${i}-${prog} | head -1\`" >> ${WRAP_DIR}/${i}-${prog}
            echo "export LD_PRELOAD='/usr/\$LIB/faketime/libfaketime.so.1'" >> ${WRAP_DIR}/${i}-${prog}
            echo "export FAKETIME=\"$1\"" >> ${WRAP_DIR}/${i}-${prog}
            echo "\$REAL \"\$@\"" >> $WRAP_DIR/${i}-${prog}
            chmod +x ${WRAP_DIR}/${i}-${prog}
        fi
    done
  done
  }

  # Faketime for depends so intermediate results are comparable
  export PATH_orig=${PATH}
  create_global_faketime_wrappers "2000-01-01 12:00:00"
  create_per-host_faketime_wrappers "2000-01-01 12:00:00"
  export PATH=${WRAP_DIR}:${PATH}

  cd fairbrix
  BASEPREFIX="${PWD}/depends"
  # Build dependencies for each host
  for i in $HOSTS; do
    make ${MAKEOPTS} -C ${BASEPREFIX} HOST="${i}"
  done

  # Faketime for binaries
  export PATH=${PATH_orig}
  create_global_faketime_wrappers "${REFERENCE_DATETIME}"
  create_per-host_faketime_wrappers "${REFERENCE_DATETIME}"
  export PATH=${WRAP_DIR}:${PATH}

  # Define DISTNAME variable.
  # shellcheck source=contrib/gitian-descriptors/assign_DISTNAME
  source contrib/gitian-descriptors/assign_DISTNAME

  GIT_ARCHIVE="${OUTDIR}/src/${DISTNAME}.tar.gz"

  # Create the source tarball
  mkdir -p "$(dirname "$GIT_ARCHIVE")"
  git archive --prefix="${DISTNAME}/" --output="$GIT_ARCHIVE" HEAD

  ORIGPATH="$PATH"
  # Extract the git archive into a dir for each host and build
  for i in ${HOSTS}; do
    export PATH=${BASEPREFIX}/${i}/native/bin:${ORIGPATH}
    if [ "${i}" = "riscv64-linux-gnu" ]; then
      # Workaround for https://bugs.launchpad.net/ubuntu/+source/gcc-8-cross-ports/+bug/1853740
      # TODO: remove this when no longer needed
      HOST_LDFLAGS="${HOST_LDFLAGS_BASE} -Wl,-z,noexecstack"
    else
      HOST_LDFLAGS="${HOST_LDFLAGS_BASE}"
    fi
    mkdir -p distsrc-${i}
    cd distsrc-${i}
    INSTALLPATH="${PWD}/installed/${DISTNAME}"
    mkdir -p ${INSTALLPATH}
    tar --strip-components=1 -xf "${GIT_ARCHIVE}"

    ./autogen.sh
    CONFIG_SITE=${BASEPREFIX}/${i}/share/config.site ./configure --prefix=/ --disable-ccache --disable-maintainer-mode --disable-dependency-tracking ${CONFIGFLAGS} CFLAGS="${HOST_CFLAGS}" CXXFLAGS="${HOST_CXXFLAGS}" LDFLAGS="${HOST_LDFLAGS}"
    make ${MAKEOPTS}
    make ${MAKEOPTS} -C src check-security
    make ${MAKEOPTS} -C src check-symbols
    make install DESTDIR=${INSTALLPATH}
    cd installed
    find . -name "lib*.la" -delete
    find . -name "lib*.a" -delete
    rm -rf ${DISTNAME}/lib/pkgconfig
    # Fairbrix: Delete libbitcoin from binary distribution for now, see #192
    rm -rf ${DISTNAME:?}/lib/
    rm -rf ${DISTNAME}/include/
    # Fairbrix: ends
    find ${DISTNAME}/bin -type f -executable -print0 | xargs -0 -n1 -I{} ../contrib/devtools/split-debug.sh {} {} {}.dbg
    # find ${DISTNAME}/lib -type f -print0 | xargs -0 -n1 -I{} ../contrib/devtools/split-debug.sh {} {} {}.dbg # Fairbrix: see issue 192
    cp ../README.md ${DISTNAME}/
    find ${DISTNAME} -not -name "*.dbg" | sort | tar --mtime="$REFERENCE_DATETIME" --no-recursion --mode='u+rw,go+r-w,a+X' --owner=0 --group=0 -c -T - | gzip -9n > ${OUTDIR}/${DISTNAME}-${i}.tar.gz
    find ${DISTNAME} -name "*.dbg" | sort | tar --mtime="$REFERENCE_DATETIME" --no-recursion --mode='u+rw,go+r-w,a+X' --owner=0 --group=0 -c -T - | gzip -9n > ${OUTDIR}/${DISTNAME}-${i}-debug.tar.gz
    cd ../../
    rm -rf distsrc-${i}
  done
