# add our own bug reporting URL
eval "$(
  declare -f build | \
    sed '
      s,https://bugs.archlinux.org/,https://bugs.archlinux32.org/,
    '
)"

# enable BFD for x86_64 otherwise we have problems with cross-compilation
# or compiling things like grub
eval "$(
  declare -f build | \
    sed '
      s/--enable/--enable-64-bit-bfd \0/
    '
)"

# "configure: error: debuginfod is missing or unusable"
eval "$(
  declare -f build | \
    sed '
      s/--with-debuginfod//g
    '
)"

# should be in a cross-compilation binutils for targeting EFI binaries IMHO,
# not in the main platform binutils (see also https://bugs.archlinux.org/task/42540)
eval "$(
  declare -f build | \
    sed '
      s/--enable-targets=x86_64-pep,bpf-unknown-none/--enable-targets=bpf-unknown-none/g
    '
)"

# i486-specific
if [ "${CARCH}" = "i486" ]; then
  # disable CET (Control Flow instructions endbr32/enbr64)
  eval "$(
    declare -f build | \
      sed '
        s/--enable-cet/--enable-cet=no/
      '
  )"
else
  # explicitely enable CET (Control Flow instructions endbr32/enbr64)
  # avoid "corrupt GNU_PROPERTY_TYPE (5) size: 0" warnings,
  # see:
  # https://bbs.archlinux32.org/viewtopic.php?pid=6160#p6160
  # https://bugs.archlinux32.org/index.php?do=details&task_id=82
  #
  eval "$(
    declare -f build | \
      sed '
        s/--enable-cet/--enable-cet=yes/
      '
  )"
fi

# i486-specific: disable PGO/LTO build, uses too much resources,
# also disabling LTO for now, also disabling the gold linker
# as it runs out of disk space (whatever that means)
# also use the normal version of libiberty.a, there is no PIC
# version on i486 for now
if [ "${CARCH}" = "i486" ]; then
  eval "$(
    declare -f build | \
      sed '
        s/--enable-pgo-build=.*/--disable-pgo-build/
        s/--enable-lto/--disable-lto/
        s/--enable-gold/--disable-gold/
      '
    declare -f package | \
      sed '
        /pic\/libiberty.a/d
      '
  )"  
fi

# 2.38, FAIL: Build ifunc-1a with PIE -z ibtplt
eval "$(
  declare -f check | \
    sed '
      s/ check/ check || true/
    '
)"

# we get a clash with files from gdb, delete them (they should not be there?)
eval "$(
  declare -f package | \
    sed '
      /make.*install.*/ a \
        rm -rf "$pkgdir"/usr/bin/{gcore,gdb,gdb-add-index,gdbserver} \
        rm -rf "$pkgdir"/usr/include/gdb \
        rm -rf "$pkgdir"/usr/lib/libinproctrace.so \
        rm -rf "$pkgdir"/usr/share/gdb/{guile,syscalls,system-gdbinit} \
        rm -rf "$pkgdir"/usr/share/info/{annotate,gdb,stabs}.info* \
        rm -rf "$pkgdir"/usr/share/man/man1/{gcore,gdb-add-index,gdb,gdbserver}.1* \
        rm -rf "$pkgdir"/usr/share/man/man5/gdbinit.5*
    '
)"