#!/bin/bash
[% c("var/set_default_env") -%]
is_compiler_rt=[% c("var/is_compiler_rt") %]
[% IF c("var/is_compiler_rt") -%]
  distdir=/var/tmp/dist/wasi-compiler-rt
[% ELSE -%]
  distdir=/var/tmp/dist/[% project %]
[% END -%]
builddir=/var/tmp/build/[% project %]-[% c("abbrev") %]
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/ninja:/var/tmp/dist/cmake/bin:$PATH"

[% IF c("var/linux") -%]
  [% pc(c('var/compiler'), 'var/setup', { compiler_tarfile => c('input_files_by_name/' _ c('var/compiler')) }) %]
  [% pc('python', 'var/setup', { python_tarfile => c('input_files_by_name/python') }) %]
[% END -%]

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

cd /var/tmp/build
tar -xf $rootdir/[% project %]-[% c('version') %].tar.[% c('compress_tar') %]
cd $builddir

pushd src
rmdir config llvm-project wasi-libc
tar -xf $rootdir/[% c('input_files_by_name/wasi-config') %]
mv wasi-config-* config
tar -xf $rootdir/[% c('input_files_by_name/clang-source') %]
mv clang-source llvm-project
export LLVM_HOME=$(pwd)/llvm-project
tar -xf $rootdir/[% c('input_files_by_name/wasi-libc') %]
mv wasi-libc-* wasi-libc
popd

# What follows has been copied from Firefox's
# taskcluster/scripts/misc/build-sysroot-wasi.sh and adapted to
# tor-browser-build.

patch -p1 < $rootdir/wasi-sdk.patch

mkdir -p build/install/wasi

tar -C build -xf $rootdir/[% c('input_files_by_name/clang') %]
[% IF c("var/is_compiler_rt") -%]
  mv build/clang build/llvm
[% ELSE -%]
  mv build/[% c("var/clang-project") %] build/llvm
[% END -%]
# The wasi-sdk build system wants to build clang itself. We trick it into
# thinking it did, and put our own clang where it would have built its own.
touch build/llvm.BUILT

# The wasi-sdk build system wants a clang and an ar binary in
# build/install/$PREFIX/bin
[% IF !c("var/is_compiler_rt") && c("var/macos") -%]
  ln -s $(pwd)/build/llvm/clang/bin build/install/wasi/bin
[% ELSE -%]
  ln -s $(pwd)/build/llvm/bin build/install/wasi/bin
[% END -%]

ln -s llvm-ar build/install/wasi/bin/ar

# tor-browser-build: we have no .git in our tarball but the script version.sh
# used by the Makefile depends on that. We replace the script.
cat > 'version.sh' << EOF
#!/bin/sh
echo '[% c("abbrev") %]'
EOF

# Build wasi-libc, and re-pack it, to make sure we get a deterministic output.
make PREFIX=/wasi build/wasi-libc.BUILT
