#!/bin/bash
[% c("var/set_default_env") -%]
[% pc(c('var/compiler'), 'var/setup', { compiler_tarfile => c('input_files_by_name/' _ c('var/compiler')) }) %]
distdir=/var/tmp/dist/[% project %]
mkdir -p /var/tmp/build
tar -C /var/tmp/build -xf [% project %]-[% c('version') %].tar.gz
cd /var/tmp/build/[% project %]-[% c('version') %]
[% IF c("var/osname") == "linux-i686" -%]
export CC='gcc -m32'
[% END %]
# OpenSSL prepends the host triple to $CC
[% IF c("var/macos") -%]
  # Tricking OpenSSL into using our clang as cross-compiler
  ln -s $clangdir/bin/clang $clangdir/bin/[% c("var/build_target") %]-cc
  export CC="cc [% c("var/FLAGS") %]"
[% END -%]
[% IF c("var/windows") -%]
  # Otherwise it falls back to gcc. This way we can remain generic and switch
  # from GCC to Clang and vice versa.
  export CC=cc
[% END -%]
export SOURCE_DATE_EPOCH='[% c("timestamp") %]'
./Configure [% c('var/configure_opts') %]
make -j[% c("num_procs") %]
# https://github.com/openssl/openssl/issues/8170
make -j[% c("num_procs") %] DESTDIR="$distdir" install
cd /var/tmp/dist
ln -s '[% c("var/openssldir") %]' openssl/openssl
[% c('tar', {
        tar_src => [ project ],
        tar_args => '-czf ' _ dest_dir _ '/' _ c('filename'),
    }) %]
