#!/bin/bash
[% c("var/set_default_env") -%]
distdir=/var/tmp/dist/[% project %]
mkdir -p /var/tmp/dist
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/ninja:/var/tmp/dist/cmake/bin:$PATH"
[% IF c("var/linux") %]
  [% pc('gcc', 'var/setup', { compiler_tarfile => c('input_files_by_name/gcc'),
                              hardened_gcc => 0 }) %]
  [% pc('python', 'var/setup', { python_tarfile => c('input_files_by_name/python') }) %]
  tar -C /var/tmp/dist -xf [% c('input_files_by_name/binutils') %]
  export PATH="/var/tmp/dist/binutils/bin:$PATH"
[% END -%]
mkdir -p /var/tmp/build
cd /var/tmp/build
tar -xf $rootdir/[% c('input_files_by_name/clang-source') %]
cd clang-source
export LLVM_HOME=$(pwd)
mkdir build
cd build
# LLVM_ENABLE_ZLIB solves the "contains a compressed section, but zlib is not available" on lld
# LLVM_INSTALL_UTILS allows this LLVM to be used to compile Rust
cmake ../llvm -GNinja \
              -DCMAKE_INSTALL_PREFIX=$distdir \
              -DCMAKE_BUILD_TYPE=Release \
              -DLLVM_TARGETS_TO_BUILD="X86;ARM;AArch64;WebAssembly" \
              -DLLVM_ENABLE_PROJECTS="clang;lld" \
              -DLLVM_ENABLE_ZLIB=ON \
              -DLLVM_INSTALL_UTILS=ON \

ninja -j[% c("num_procs") %] -v install

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