#!/bin/bash
[% 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/binutils') %]
export PATH=/var/tmp/dist/binutils/bin:$PATH
tar -C /var/tmp/dist -xf [% c('input_files_by_name/ninja') %]
export PATH=/var/tmp/dist/ninja:$PATH
tar -C /var/tmp/dist -xf [% c('input_files_by_name/rust_prebuilt') %]
cd /var/tmp/dist/rust-[% c('version') %]-x86_64-unknown-linux-gnu
./install.sh --prefix=$distdir-rust-prebuilt
export PATH="$distdir-rust-prebuilt/bin:$PATH"

[% pc(c('var/compiler'), 'var/setup', { compiler_tarfile => c('input_files_by_name/' _ c('var/compiler')) }) %]
[% IF c("var/linux") || c("var/android") || c("var/windows") -%]
  # See the config file for more details on why we do that also on Windows
  tar -C /var/tmp/dist -xf $rootdir/[% c('input_files_by_name/clang') %]
  export PATH=/var/tmp/dist/clang/bin:$PATH
[% END -%]
[% IF c("var/linux") -%]
  [% pc('python', 'var/setup', { python_tarfile => c('input_files_by_name/python') }) %]
  export LLVM_CONFIG="/var/tmp/dist/clang/bin/llvm-config"
  export CC=clang
  export CXX=clang++
  export CFLAGS='--gcc-toolchain=/var/tmp/dist/gcc'
  export CXXFLAGS='--gcc-toolchain=/var/tmp/dist/gcc'
  export RUSTFLAGS="-L/var/tmp/dist/gcc/lib64"
[% END -%]

[% IF c("var/macos") %]
  # We need to clear `CC` and `LDFLAGS` as they are used for the host platform
  # (i.e. Linux).
  unset CC
  unset LDFLAGS

  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/[% c("var/build_target") %]-clang << 'EOF'
#!/bin/sh
BASEDIR=/var/tmp/dist/macosx-toolchain
$BASEDIR/cctools/bin/[% c("var/build_target") %]-clang -target [% c("var/build_target") %] -B $BASEDIR/cctools/bin -isysroot $sysrootdir -Wl,-syslibroot,$sysrootdir -Wl,-dead_strip -Wl,-pie [% IF c('var/macos-aarch64') %]-mcpu=apple-m1[% END %] "$@"
EOF

  chmod +x $distdir/helper/[% c("var/build_target") %]-clang
  export PATH=$distdir/helper:$PATH
[% END %]

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

# 1.69 only, remove when updating to a later version!
patch -p1 < $rootdir/external-llvm.patch

# This is a workaround to get access to the libunwind header files. Our LLVM_CONFIG
# thinks that the llvm source root is at /var/tmp/dist/clang-source because that's
# where it was when clang was compiled.
ln -s "/var/tmp/build/rustc-[% c('version') %]-src/src/llvm-project" "/var/tmp/build/clang-source"

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

make -j[% c("num_procs") %]
make install
cd /var/tmp/dist

[% IF c("var/linux") -%]
  # When we compile some Rust-only projects such as cbindgen, we do not include
  # the C/C++ compilers and their modern libstdc++ library.
  # For simplicity, we keep it also with Rust, and add it to LD_LIBRARY_PATH
  # if needed.
  cp gcc/lib64/libstdc++.so* rust/lib/
[% END -%]

[% c('tar', {
        tar_src => [ project ],
        tar_args => '-caf ' _ dest_dir _ '/' _ c('filename'),
        }) %]
