#!/bin/bash
[% c("var/set_default_env") -%]
distdir=/var/tmp/dist/binutils
[% IF c("var/linux") %]
  # Config options for hardening-wrapper
  export DEB_BUILD_HARDENING=1
  export DEB_BUILD_HARDENING_STACKPROTECTOR=1
  export DEB_BUILD_HARDENING_FORTIFY=1
  export DEB_BUILD_HARDENING_FORMAT=1
  export DEB_BUILD_HARDENING_PIE=1
[% END %]
tar xf [% project %]-[% c("version") %].tar.xz
cd [% project %]-[% c("version") %]
./configure --prefix=$distdir [% c('var/configure_opt') %]
make -j[% c("buildconf/num_procs") %]
make install

[% IF c("var/linux") && ! c("var/linux-cross") %]
  # Make sure gold is used with the hardening wrapper for full RELRO, see #13031.
  cd $distdir/bin
  rm ld
  cp /usr/bin/hardened-ld ./
  mv ld.gold ld.gold.real
  ln -sf hardened-ld ld.gold
  ln -sf ld.gold ld
[% END %]
[% IF c("var/linux-cross") && 0 %]
  # gold is disabled for cross-compiles, because of
  # https://sourceware.org/bugzilla/show_bug.cgi?id=14995
  # Once we upgrade to glibc 2.26 and binutils 2.28, we should be able to
  # re-introduce gold for cross-compiles.

  # Make sure gold is used with the hardening wrapper for full RELRO, see #13031.
  # First do the filename-prefixed ones...
  cd $distdir/bin
  rm [% c('var/crosstarget') %]-ld
  cp /usr/bin/hardened-ld ./
  mv [% c('var/crosstarget') %]-ld.gold [% c('var/crosstarget') %]-ld.gold.real
  ln -sf hardened-ld [% c('var/crosstarget') %]-ld.gold
  ln -sf [% c('var/crosstarget') %]-ld.gold [% c('var/crosstarget') %]-ld
  # Then do the directory-prefixed ones...
  cd $distdir/[% c('var/crosstarget') %]/bin
  rm ld
  cp /usr/bin/hardened-ld ./
  mv ld.gold ld.gold.real
  ln -sf hardened-ld ld.gold
  ln -sf ld.gold ld
[% END %]

cd /var/tmp/dist
[% c('tar', {
        tar_src => [ project ],
        tar_args => '-czf ' _ dest_dir _ '/' _ c('filename'),
        }) %]
