#!/bin/bash
[% c("var/set_default_env") -%]
builddir=/var/tmp/build
mkdir $builddir
distdir=/var/tmp/dist/[% 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') %]
tar -C /var/tmp/dist -xf [% c('input_files_by_name/clang') %]
tar -C $distdir -xf [% c('input_files_by_name/SDK') %]
tar -C $distdir -xf [% c('input_files_by_name/cctools') %]
tar -C $builddir -xf [% c('input_files_by_name/llvm-project') %]

arch=x86_64
clangdir=/var/tmp/dist/clang/bin
cctoolsdir=$distdir/cctools/bin
sysrootdir=$distdir/MacOSX[% c("version") %].sdk/
target=x86_64-apple-darwin
# We still need to put the cctoolsdir on the path. That's because of `lipo`. See
# the respective comment in the cctools build script.
export PATH="/var/tmp/dist/ninja:/var/tmp/dist/cmake/bin:$cctoolsdir:$PATH"
export MACOSX_DEPLOYMENT_TARGET=[% c("var/macosx_deployment_target") %]
export MACH_USE_SYSTEM_PYTHON=1

cd $builddir/clang-source
patch -p1 < $rootdir/compiler-rt-cross-compile.patch
patch -p1 < $rootdir/compiler-rt-no-codesign.patch
cd ..
mkdir build_clang
pushd build_clang
# We follow quite closely Mozilla's build/build-clang/build-clang.py with the
# clang-macosx64.json flavor.
cmake -GNinja \
      -DCMAKE_C_COMPILER=$clangdir/clang \
      -DCMAKE_CXX_COMPILER=$clangdir/clang++ \
      -DCMAKE_ASM_COMPILER=$clangdir/clang \
      -DCMAKE_LINKER=$clangdir/clang \
      -DCMAKE_AR=$cctoolsdir/$target-ar \
      -DCMAKE_C_FLAGS="[% c('var/FLAGS') %] -I$sysrootdir/usr/include -iframework $sysrootdir/System/Library/Frameworks" \
      -DCMAKE_CXX_FLAGS="-stdlib=libc++ [% c('var/FLAGS') %] -I$sysrootdir/usr/include -iframework $sysrootdir/System/Library/Frameworks" \
      -DCMAKE_ASM_FLAGS="[% c('var/FLAGS') %] -I$sysrootdir/usr/include -iframework $sysrootdir/System/Library/Frameworks" \
      -DCMAKE_EXE_LINKER_FLAGS="[% c('var/LDFLAGS') %]" \
      -DCMAKE_SHARED_LINKER_FLAGS="[% c('var/LDFLAGS') %]" \
      -DCMAKE_BUILD_TYPE=Release \
      -DCMAKE_INSTALL_PREFIX=$distdir/clang \
      -DLLVM_TARGETS_TO_BUILD="X86;ARM;AArch64" \
      -DLLVM_ENABLE_ASSERTIONS=OFF \
      -DPYTHON_EXECUTABLE=/usr/bin/python3 \
      -DLLVM_TOOL_LIBCXX_BUILD=ON \
      -DLLVM_ENABLE_BINDINGS=OFF \
      -DLLVM_ENABLE_LIBXML2=FORCE_ON \
      -DLLVM_LINK_LLVM_DYLIB=ON \
      -DCMAKE_RANLIB=$cctoolsdir/$target-ranlib \
      -DCMAKE_LIBTOOL=$cctoolsdir/$target-libtool \
      -DCMAKE_SYSTEM_NAME=Darwin \
      -DCMAKE_SYSTEM_VERSION=$MACOSX_DEPLOYMENT_TARGET \
      -DLLVM_ENABLE_THREADS=OFF \
      -DCOMPILER_RT_BUILD_XRAY=OFF \
      -DLIBCXXABI_LIBCXX_INCLUDES=$builddir/clang-source/projects/libcxx/include \
      -DCMAKE_OSX_SYSROOT=$sysrootdir \
      -DCMAKE_FIND_ROOT_PATH=$sysrootdir \
      -DCMAKE_FIND_ROOT_PATH_MODE_PROGRAM=NEVER \
      -DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY=ONLY \
      -DCMAKE_FIND_ROOT_PATH_MODE_INCLUDE=ONLY \
      -DCMAKE_MACOSX_RPATH=ON \
      -DCMAKE_OSX_ARCHITECTURES=$arch \
      -DDARWIN_macosx_OVERRIDE_SDK_VERSION=$MACOSX_DEPLOYMENT_TARGET \
      -DDARWIN_osx_ARCHS=$arch \
      -DDARWIN_osx_SYSROOT=$sysrootdir \
      -DLLVM_DEFAULT_TARGET_TRIPLE=x86_64-apple-darwin $builddir/clang-source/llvm

ninja install -v

popd
mkdir build_compiler_rt
pushd build_compiler_rt

# And we build compiler-rt by following taskcluster/scripts/misc/build-compiler-rt.sh.
cmake -GNinja \
      -DCMAKE_C_COMPILER=$clangdir/clang \
      -DCMAKE_CXX_COMPILER=$clangdir/clang++ \
      -DCMAKE_C_COMPILER_TARGET=$target \
      -DCMAKE_CXX_COMPILER_TARGET=$target \
      -DCMAKE_ASM_COMPILER_TARGET=$target \
      -DCMAKE_AR=$clangdir/llvm-ar \
      -DCMAKE_RANLIB=$clangdir/llvm-ranlib \
      -DCMAKE_C_FLAGS="[% c('var/FLAGS') %] -I$sysrootdir/usr/include -iframework $sysrootdir/System/Library/Frameworks" \
      -DCMAKE_CXX_FLAGS="-stdlib=libc++ [% c('var/FLAGS') %] -I$sysrootdir/usr/include -iframework $sysrootdir/System/Library/Frameworks" \
      -DCMAKE_ASM_FLAGS="[% c('var/FLAGS') %] -I$sysrootdir/usr/include -iframework $sysrootdir/System/Library/Frameworks" \
      -DCMAKE_EXE_LINKER_FLAGS="[% c('var/LDFLAGS') %]" \
      -DCMAKE_SHARED_LINKER_FLAGS="[% c('var/LDFLAGS') %]" \
      -DCMAKE_BUILD_TYPE=Release \
      -DLLVM_ENABLE_ASSERTIONS=OFF \
      -DCMAKE_INSTALL_PREFIX=$distdir/clang/lib/clang/[% pc("clang", "version") %]/ \
      -DLLVM_CONFIG_PATH=$clangdir/llvm-config \
      -DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON \
      -DCMAKE_LINKER=$cctoolsdir/$target-ld \
      -DCMAKE_LIPO=$cctoolsdir/lipo \
      -DCMAKE_SYSTEM_NAME=Darwin \
      -DCMAKE_SYSTEM_VERSION=$MACOSX_DEPLOYMENT_TARGET \
      -DDARWIN_macosx_OVERRIDE_SDK_VERSION=$MACOSX_DEPLOYMENT_TARGET \
      -DDARWIN_osx_ARCHS=$arch \
      -DDARWIN_osx_SYSROOT=$sysrootdir \
      -DDARWIN_osx_BUILTIN_ARCHS=$arch \
      -DCMAKE_OSX_SYSROOT=$sysrootdir \
      -DCMAKE_FIND_ROOT_PATH=$sysrootdir \
      -DCMAKE_FIND_ROOT_PATH_MODE_PROGRAM=NEVER \
      -DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY=ONLY \
      -DCMAKE_FIND_ROOT_PATH_MODE_INCLUDE=ONLY \
      -DDARWIN_osx_BUILTIN_ARCHS=$arch \
      $builddir/clang-source/compiler-rt

ninja install -v

# We now have a native macosx64 toolchain.
# What we want is a native linux64 toolchain which can target macosx64.
# Overlay the linux64 toolchain that we used for this build. Note: we don't do
# any ASan builds for macOS (yet). Once we do that we need to preserve at least
# `llvm-symbolizer` as that one seems to get shipped with sanitizer builds (at
# least that's what Mozilla claims).
cd $distdir
cp --remove-destination -lr /var/tmp/dist/clang/* clang/

cd cctools/bin
ln -s ../../clang/bin/clang x86_64-apple-darwin-clang
ln -s ../../clang/bin/clang++ x86_64-apple-darwin-clang++

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