#!/bin/bash
[% c('var/setarch') %]
[% c("var/set_default_env") -%]
distdir=/var/tmp/dist/[% project %]
mkdir -p $distdir
tar -C /var/tmp/dist -xf [% c('input_files_by_name/cmake') %]
export PATH="/var/tmp/dist/cmake/bin:$PATH"
tar -C /var/tmp/dist -xf [% c('input_files_by_name/prev_rust') %]
cd /var/tmp/dist/rust-[% c('var/prev_version') %]-[% c('var/rust_arch') %]-unknown-linux-gnu
./install.sh --prefix=$distdir-rust-old
export PATH="$distdir-rust-old/bin:$PATH"

[% pc(c('var/compiler'), 'var/setup', { compiler_tarfile => c('input_files_by_name/' _ c('var/compiler')) }) %]

[% IF c("var/osx") %]
  # We need to clear `CC` and `LDFLAGS` as they are used for the host platform
  # (i.e. Linux).
  unset CC
  unset LDFLAGS
  # Target 10.7 as our toolchain does. Without this explicit declaration Bad
  # Things will happen, as a lot of dependent code then assumes that the
  # official macOS target, x86_64-apple-darwin, essentially means 10.4.
  export MACOSX_DEPLOYMENT_TARGET=10.7
  # The Rust target for macOS is x86_64-apple-darwin, yet our toolchain is built
  # for x86_64-apple-darwin11. We can't mix those targets as clang gets confused
  # that way. Changing the Rust target to x86_64-apple-darwin11 would require a
  # fair amount of patching, thus we create symlinks to provide Rust with the
  # necessary tools while using our toolchain underneath, targeting 10.7.
  cd $cctoolsdir
  for f in `ls x86_64-apple-darwin11-*`; do
    ln -s $f ${f//x86_64-apple-darwin11/x86_64-apple-darwin}
  done
  cd ..
  ln -s x86_64-apple-darwin11 x86_64-apple-darwin
  mkdir $distdir/helper

  # We need to adapt our CFLAGS and make sure our flags are passed down to all
  # dependencies. Using `CFLAGS_x86_apple-darwin` did not do the trick, so resort
  # to a wrapper script.
  cat > $distdir/helper/x86_64-apple-darwin-clang << 'EOF'
#!/bin/sh
BASEDIR=/var/tmp/dist/macosx-toolchain
$BASEDIR/cctools/bin/x86_64-apple-darwin-clang -target x86_64-apple-darwin -B $BASEDIR/cctools/bin -isysroot $BASEDIR/SDK/ -Wl,-syslibroot,$BASEDIR/SDK/ -Wl,-dead_strip -Wl,-pie "$@"
EOF

  chmod +x $distdir/helper/x86_64-apple-darwin-clang
  export PATH=$distdir/helper:$PATH
[% END %]

cd $rootdir
mkdir /var/tmp/build
tar -C /var/tmp/build -xf  [% c('input_files_by_name/rust') %]
cd /var/tmp/build/rustc-[% c('version') %]-src

[% IF c("var/windows-i686") %]
  # Cross-compiling for Windows 32bit is currently not possible without any
  # patches. The reason for that is libstd expecting DWARF unwinding while most
  # toolchains on Linux, targeting Windows 32bit, use SjLj unwinding.
  # See: https://github.com/rust-lang/rust/issues/12859 for discussion about
  # that and https://github.com/rust-lang/rust/pull/49633 for a newer attempt to
  # fix this problem. We apply the patch from neersighted.
  [% IF !c("input_file_var/unwind_128") %]
    patch -p1 < $rootdir/unwind.patch
  [% ELSE %]
    patch -p1 < $rootdir/unwind_128.patch
  [% END %]
[% END %]

[% IF c("var/android") %]
  patch -p1 < $rootdir/replace_pagesize_in_mmap.patch
  # The additional Rust patch is not necessary for x86.
  [% IF c("var/android-armv7") %]
    patch -p1 < $rootdir/0001-Make-sure-dl_iterate_phdr-is-undefined-on-Android.patch
  [% END %]
[% END %]

mkdir build
cd build
../configure --prefix=$distdir [% c("var/configure_opt") %]

# We need to disable Thin LTO due to reproducibility issues on macOS and
# probably Linux. Alas, there is no direct option available in the config.toml
# in 1.26.1 yet, so we need to toggle this indirectly via `codegen-units`.
[% IF c("var/osx") || c("var/linux") %]
  # It seems hard to pass the proper value via ./configure so we resort to our
  # old friend `sed`.
  sed -i 's/#codegen-units = 1/codegen-units = 1/' config.toml
[% END %]

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