prelude.bash raw

   1  #!/usr/bin/env bash
   2  export LC_ALL=C
   3  set -e -o pipefail
   4  
   5  source contrib/shell/realpath.bash
   6  source contrib/shell/git-utils.bash
   7  
   8  ################
   9  # Required non-builtin commands should be invocable
  10  ################
  11  
  12  check_tools() {
  13      for cmd in "$@"; do
  14          if ! command -v "$cmd" > /dev/null 2>&1; then
  15              echo "ERR: This script requires that '$cmd' is installed and available in your \$PATH"
  16              exit 1
  17          fi
  18      done
  19  }
  20  
  21  ################
  22  # SOURCE_DATE_EPOCH should not unintentionally be set
  23  ################
  24  
  25  check_source_date_epoch() {
  26      if [ -n "$SOURCE_DATE_EPOCH" ] && [ -z "$FORCE_SOURCE_DATE_EPOCH" ]; then
  27          cat << EOF
  28  ERR: Environment variable SOURCE_DATE_EPOCH is set which may break reproducibility.
  29  
  30       Aborting...
  31  
  32  Hint: You may want to:
  33        1. Unset this variable: \`unset SOURCE_DATE_EPOCH\` before rebuilding
  34        2. Set the 'FORCE_SOURCE_DATE_EPOCH' environment variable if you insist on
  35           using your own epoch
  36  EOF
  37          exit 1
  38      fi
  39  }
  40  
  41  check_tools cat env readlink dirname basename git
  42  
  43  ################
  44  # We should be at the top directory of the repository
  45  ################
  46  
  47  same_dir() {
  48      local resolved1 resolved2
  49      resolved1="$(bash_realpath "${1}")"
  50      resolved2="$(bash_realpath "${2}")"
  51      [ "$resolved1" = "$resolved2" ]
  52  }
  53  
  54  if ! same_dir "${PWD}" "$(git_root)"; then
  55  cat << EOF
  56  ERR: This script must be invoked from the top level of the git repository
  57  
  58  Hint: This may look something like:
  59      env FOO=BAR ./contrib/guix/guix-<blah>
  60  
  61  EOF
  62  exit 1
  63  fi
  64  
  65  ################
  66  # Execute "$@" in a pinned, possibly older version of Guix, for reproducibility
  67  # across time.
  68  time-machine() {
  69      # shellcheck disable=SC2086
  70      guix time-machine --url=https://codeberg.org/guix/guix.git \
  71                        --commit=c5eee3336cc1d10a3cc1c97fde2809c3451624d3 \
  72                        --cores="$JOBS" \
  73                        --keep-failed \
  74                        --fallback \
  75                        ${SUBSTITUTE_URLS:+--substitute-urls="$SUBSTITUTE_URLS"} \
  76                        ${ADDITIONAL_GUIX_COMMON_FLAGS} ${ADDITIONAL_GUIX_TIMEMACHINE_FLAGS} \
  77                        -- "$@"
  78  }
  79  
  80  # Usage: distsrc_for_host HOST [SUFFIX] [BASE]
  81  #
  82  #   HOST: The current platform triple we're building for
  83  #   SUFFIX: Optional. If provided, appended to the directory name as "-SUFFIX"
  84  #   BASE: Optional. If provided, replaces ${DISTSRC_BASE}
  85  #
  86  distsrc_for_host() {
  87      echo "${3:-${DISTSRC_BASE}}/distsrc-${VERSION}-${1}${2:+-${2}}"
  88  }
  89  
  90  # Usage: outdir_for_host HOST [SUFFIX] [BASE]
  91  #
  92  #   HOST: The current platform triple we're building for
  93  #   SUFFIX: Optional. If provided, appended to the directory name as "-SUFFIX"
  94  #   BASE: Optional. If provided, replaces ${OUTDIR_BASE}
  95  #
  96  outdir_for_host() {
  97      echo "${3:-${OUTDIR_BASE}}/${1}${2:+-${2}}"
  98  }
  99  
 100  # Usage: profiledir_for_host HOST [SUFFIX]
 101  #
 102  #   HOST: The current platform triple we're building for
 103  #   SUFFIX: Optional. If provided, appended to the directory name as "-SUFFIX"
 104  #
 105  profiledir_for_host() {
 106      echo "${PROFILES_BASE}/${1}${2:+-${2}}"
 107  }
 108  
 109  ################
 110  # Set common variables
 111  ################
 112  
 113  VERSION="${FORCE_VERSION:-$(git_head_version)}"
 114  DISTNAME="${DISTNAME:-bitcoin-${VERSION}}"
 115  
 116  version_base_prefix="${PWD}/guix-build-"
 117  VERSION_BASE="${version_base_prefix}${VERSION}"  # TOP
 118  
 119  DISTSRC_BASE="${DISTSRC_BASE:-${VERSION_BASE}}"
 120  
 121  OUTDIR_BASE="${OUTDIR_BASE:-${VERSION_BASE}/output}"
 122  
 123  var_base_basename="var"
 124  VAR_BASE="${VAR_BASE:-${VERSION_BASE}/${var_base_basename}}"
 125  
 126  profiles_base_basename="profiles"
 127  PROFILES_BASE="${PROFILES_BASE:-${VAR_BASE}/${profiles_base_basename}}"
 128