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

# mozjs<version>.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter for Slackware ARM.
# Copyright 2019-2024
#
# Copyright 2011-2024  Robby Workman, Tuscaloosa, Alabama, USA
# Copyright 2019-2025  Patrick J. Volkerding, Sebeka, MN, 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

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

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

# 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
LDFLAGS="-Wl,--copy-dt-needed-entries" \
./configure \
   $SLKCONFARGS \
   --prefix=/usr \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --sysconfdir=/etc \
   --localstatedir=/var \
   --mandir=/usr/man \
   --with-system-icu \
   --with-system-zlib \
   --disable-tests \
   --disable-strip \
   --with-intl-api \
   --enable-linker=bfd \
   --enable-readline \
   --enable-shared-js \
   --enable-optimize \
   --disable-debug \
   --enable-pie \
   --disable-jemalloc \
   --host=${SLK_ARCH_HOST} || failconfig

# Build:
make $NUMJOBS || make || 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:
SHORTVER=$(echo $PKGNAM | cut -b 6-)
if [ ! -r $PKG/usr/lib${LIBDIRSUFFIX}/libmozjs-${SHORTVER}.so ]; then
   echo "FATAL: trying to rename library but $PKG/usr/lib${LIBDIRSUFFIX}/libmozjs-${SHORTVER}.so is not found."
   exit 1
fi
mv -fv $PKG/usr/lib${LIBDIRSUFFIX}/libmozjs-${SHORTVER}.so $PKG/usr/lib${LIBDIRSUFFIX}/libmozjs-${SHORTVER}.so.0.0.0

( cd $PKG/usr/lib${LIBDIRSUFFIX}
  ln -vsf libmozjs-${SHORTVER}.so.0.0.0 libmozjs-${SHORTVER}.so.0
  ln -vsf libmozjs-${SHORTVER}.so.0 libmozjs-${SHORTVER}.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
