#!/bin/bash
[% c("var/set_default_env") -%]
[% IF c("var/linux") %]
  # We need a link to our GCC, otherwise the system cc gets used which points to
  # /usr/bin/gcc.
  [% pc('gcc', 'var/setup', { compiler_tarfile => c('input_files_by_name/gcc'),
                              hardened_gcc => 0 }) %]
  ln -s gcc /var/tmp/dist/gcc/bin/cc
  tar -C /var/tmp/dist -xf $rootdir/[% c('input_files_by_name/binutils') %]
  export PATH="/var/tmp/dist/binutils/bin:$PATH"
[% END -%]
distdir=/var/tmp/dist
builddir=/var/tmp/build/[% project %]
mkdir -p $distdir
tar -C /var/tmp/dist -xf [% c('input_files_by_name/cmake') %]
tar -C /var/tmp/dist -xf [% c('input_files_by_name/ninja') %]
export PATH=/var/tmp/dist/binutils/bin:/var/tmp/dist/cmake/bin:/var/tmp/dist/ninja:$PATH

mkdir -p /var/tmp/build
tar -C /var/tmp/build -xf [% project %]-[% c('version') %].tar.gz

mv /var/tmp/build/[% project %]-[% c('version') %] $builddir
cd $builddir
# We have no .git in our tar ball but the Makefile depends on that. Just
# hard-code the version for now.
patch -p1 < $rootdir/no-git.patch

# XXX: We don't want to use the clang 9.0 wasi-sdk would use, see:
# https://bugzilla.mozilla.org/show_bug.cgi?id=1605708
make -j[% c("buildconf/num_procs") %] PREFIX=$distdir/[% project %]

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