#!/bin/bash
ulimit -s unlimited
ulimit -n 4096
shopt -s extglob

# mozjs68.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter for Slackware ARM.
#
# Copyright 2011, 2018  Robby Workman, Tuscaloosa, Alabama, USA
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
slackset_var_cwds

# Temporary build locations:
export TMPBUILD=$TMP/build-$PKGNAM
export PKG=$TMP/package-$PKGNAM
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Architecture settings:
case $ARCH in
   arm)  SLKCFLAGS="$SLKCFLAGS -fexceptions -g0" #  -flto=auto -ffat-lto-objects"
         SLKCXXFLAGS="$SLKCFLAGS"
         SLKCPPFLAGS="$SLKCXXFLAGS"
         SLKLDFLAGS="-Wl,-z,relro -Wl,--as-needed  -Wl,-z,now"
         #
         export SLKCONFARGS=""
         export LIBDIRSUFFIX="" ;;
   *)    export SLKCFLAGS="-O1" ;;
esac

# Extract source:
echo "Extracting source - will take a few mins.."
tar xf $CWD/firefox-${VERSION}*source*.tar.!(*sign|*asc|*sig)
cd firefox-*/ || exit 1
slackhousekeeping

# Patches from Fedora:
zcat $CWD/patches/fix-soname.patch.gz | patch -p1 --verbose || exit 1
zcat $CWD/patches/copy-headers.patch.gz | patch -p1 --verbose || exit 1
zcat $CWD/patches/tests-increase-timeout.patch.gz | patch -p1 --verbose || exit 1
zcat $CWD/patches/icu_sources_data.py-Decouple-from-Mozilla-build-system.patch.gz | patch -p1 --verbose || exit 1
zcat $CWD/patches/icu_sources_data-Write-command-output-to-our-stderr.patch.gz | patch -p1 --verbose || exit 1
zcat $CWD/patches/emitter.patch.gz | patch -p1 --verbose || exit 1
zcat $CWD/patches/init_patch.patch.gz | patch -p1 --verbose || exit 1
zcat $CWD/patches/spidermonkey_checks_disable.patch.gz | patch -p1 --verbose || exit 1

# ARM specific patches:
# This is commented in the x86 SlackBuild, but is included in the source and Fedora are applying it.
zcat $CWD/patches/armv7_disable_WASM_EMULATE_ARM_UNALIGNED_FP_ACCESS.patch.gz | patch -p1 --verbose || exit 1

# ARM change---
# : On Slackware-upstream this dir is removed, but on ARM the build
# fails because one of the Cargo.toml files references another within this dir.
# It may work on x86 because it builds with clang although I can't see why that'd be a key differentiator!
#
# Remove bundled security libraries so that we don't link to them:
#rm -rf security
## END ARM change ---

# Remove bundled zlib directory and use system version:
rm -rf modules/zlib

cd js/src

# ARM changes
# ----------
# mozjs defaults to clang, but there's an integration issue with rust and clang where
# they pick the wrong target architecture.  It works with gcc:
export CC=cc
export CXX=g++
export CFLAGS="$SLKCFLAGS -fno-delete-null-pointer-checks -fno-strict-aliasing -fno-tree-vrp "
export CXXFLAGS="$SLKCFLAGS"
export CPPFLAGS="$SLKCXXFLAGS"
export LINKFLAGS="$SLKLDFLAGS"

# This is taken from Fedora's spec file:
# Workaround
# error: options `-C embed-bitcode=no` and `-C lto` are incompatible
# error: could not compile `jsrust`.
# https://github.com/japaric/cargo-call-stack/issues/25
export RUSTFLAGS="-Cembed-bitcode"

# Put Rust objects on a diet to keep the linker from running into memory
# issues (especially on 32-bit):
# These cause a build failure.  rust on Slackware ARM sets these as default also, so they
# may not be needed.
export RUSTFLAGS="$RUSTFLAGS -Cdebuginfo=0"
#export RUSTFLAGS="-Cdebuginfo=0 -Copt-level=0"
export MOZ_RUST_DEFAULT_FLAGS="$RUSTFLAGS"

# Disable debuginfo being compiled in. Helps reduce RAM use at build time.
export MOZ_DEBUG_FLAGS="-g0"

# Building with LTO:
# https://github.com/ptomato/mozjs/commit/36bb7982b41e0ef9a65f7174252ab996cd6777bd
export CARGO_PROFILE_RELEASE_LTO=true
# End ARM changes ----------

# We need to do this because patches above touch configure.in:
autoconf2.13
./configure \
   $SLKCONFARGS \
   --prefix=/usr \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --sysconfdir=/etc \
   --localstatedir=/var \
   --mandir=/usr/man \
   --without-system-icu \
   --with-system-zlib \
   --enable-tests \
   --disable-strip \
   --with-intl-api \
   --enable-readline \
   --enable-shared-js \
   --disable-optimize \
   --enable-pie \
   --disable-jemalloc \
   --host=${SLK_ARCH_HOST} || failconfig

# Reduce the number of parallel jobs because this uses clang and we don't
# yet have a clang cross compiler for the Slackware ARM build system:
NUMJOBS="-j$( nproc )"
#NUMJOBS="-j1"

# Build:
make $NUMJOBS || make || failmake
#make $NUMJOBS || { disable_distcc ; make ;} || { distcc_cleanup ; failmake ;}

# Install into package framework:
make install DESTDIR=$PKG || failinstall

# No need for the huge static library:
rm -f $PKG/usr/lib${LIBDIRSUFFIX}/libjs_static.*

# Don't ship -config:
rm -f $PKG/usr/bin/js*-config

# Rename shared library to have a proper soname:
mv $PKG/usr/lib${LIBDIRSUFFIX}/libmozjs-78.so $PKG/usr/lib${LIBDIRSUFFIX}/libmozjs-78.so.0.0.0
( cd $PKG/usr/lib${LIBDIRSUFFIX}
  ln -sf libmozjs-78.so.0.0.0 libmozjs-78.so.0
  ln -sf libmozjs-78.so.0 libmozjs-78.so )

# Add documentation:
mkdir -vpm755 $PKG/usr/doc/$PKGNAM-$VERSION
cp -fav \
  ../../README.txt README.html \
  $PKG/usr/doc/$PKGNAM-$VERSION
changelogliposuction ChangeLog $PKGNAM $VERSION # Trim down a "ChangeLog" file

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall        # strip all .a archives and all ELFs
#slackstriprpaths     # strip rpaths
slack_delete_lafiles # delete usr/lib{,64}/*.la
slackgzpages -i      # compress man & info pages and delete usr/info/dir
slackslack           # set standard Slackware file/dir permissions and ownerships
slackdesc            # install slack-desc and doinst.sh
slackmp         # run makepkg -l y -c n

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links
