#!/bin/bash
[% c("var/set_default_env") -%]
# 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

distdir=/var/tmp/dist
builddir=/var/tmp/build/[% project %]
mkdir -p $distdir
tar -C /var/tmp/dist -xf [% c('input_files_by_name/binutils') %]
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

make -j[% c("buildconf/num_procs") %] PREFIX=$distdir/[% project %]

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