# Produce a build suitable for release, i.e. use PGO/LTO. You can turn it off
# when building locally to reduce build time.
%global release_build     1

# Run Mozilla test suite as a part of compile rpm section. Turn off when
# building locally and don't want to spend 24 hours waiting for results.
%global run_firefox_tests 0

# Don't create debuginfo rpm packages. It reduces build time as
# exctracting debuginfo takes long time.
%global create_debuginfo  1


# Produce debug (non-optimized) package build. Suitable for debugging only
# as the build is *very* slow.
%global debug_build       0

%global system_nss        1

%global build_with_asan   0

%if %{run_firefox_tests} 
%global test_on_wayland   1
%endif


%global llvm_version      7.0
%if "%{toolchain}" == "clang"
%global build_with_clang 1
%else
%global build_with_clang 0
%endif

%ifarch armv7hl
%global create_debuginfo  0
%endif

# Temporary disabled due to
%global enable_mozilla_crashreporter 0

%global system_ffi        1
%ifarch armv7hl
%global system_libvpx     1
%else
%global system_libvpx     0
%endif
%global hardened_build    1
%global system_jpeg       1
%global disable_elfhack   1
%global use_bundled_cbindgen  1
%if %{debug_build}
%global release_build     0
%endif
# Build PGO+LTO on x86_64 only due to build issues
# on other arches.
%global build_with_pgo    0
%ifarch x86_64
%if %{release_build}
%global build_with_pgo    0
%endif
%endif
%global dts_version       10
%global rust_version      1.62
%global wayland_backend_default 0
%if 0%{?flatpak}
%global wayland_backend_default 0
%global build_with_pgo    0
%endif

# Big endian platforms
%ifarch ppc64 s390x
%global big_endian        1
%endif

%if 0%{?build_with_pgo}
%global use_xvfb          1
%global build_tests       1
%endif

%if 0%{?run_firefox_tests}
%global use_xvfb          1
%global build_tests       1
%endif

%global launch_wayland_compositor 0
%if %{build_with_pgo} && 0%{?test_on_wayland}
%global launch_wayland_compositor 1
%endif
%if %{run_firefox_tests} && 0%{?test_on_wayland}
%global launch_wayland_compositor 1
%endif

# We need to use full path because of flatpak where datadir is /app/share
%global default_bookmarks_file  /usr/share/bookmarks/default-bookmarks.html
%global firefox_app_id  \{ec8030f7-c20a-464f-9b0e-13a3a9e97384\}
# Minimal required versions
%global cairo_version 1.13.1
%global freetype_version 2.1.9
%global libnotify_version 0.7.0
%if %{?system_libvpx}
%global libvpx_version 1.8.2
%endif

%if %{?system_nss}
%global nspr_version 4.34
%global nspr_build_version %{nspr_version}
%global nss_version 3.79
%global nss_build_version %{nss_version}
%endif

%global mozappdir     %{_libdir}/firefox
%global mozappdirdev  %{_libdir}/firefox-devel-%{version}
%global langpackdir   %{mozappdir}/browser/extensions
%global tarballdir    firefox-%{version}

%define bundled_install_path %{mozappdir}/bundled
%global official_branding       1

# We could use %%include, but in %%files, %%post and other sections, but in these
# sections it could lead to syntax errors about unclosed %%if. Work around it by
# using the following macro
%define include_file() %{expand:%(cat '%1')}

%bcond_without langpacks

%global pre_version   esr
%if !%{release_build}
%global pre_tag .npgo
%endif
%if %{build_with_clang}
%global pre_tag .clang
%endif
%if %{build_with_asan}
%global pre_tag .asan
%global build_with_pgo    0
%endif
%if !%{system_nss}
%global nss_tag .nss
%endif
%if %{debug_build}
%global pre_tag .debug
%endif

# Exclude private libraries from autogenerated provides and requires
%global __provides_exclude_from ^%{mozappdir}
%global __requires_exclude ^(%%(find %{buildroot}%{mozappdir} -name '*.so' | xargs -n1 basename | sort -u | paste -s -d '|' -))

Summary:        Mozilla Firefox Web browser
Name:           firefox
Version:        102.15.0
Release:        2
URL:            https://www.mozilla.org/firefox/
License:        MPLv1.1 or GPLv2+ or LGPLv2+

# We can't use the official tarball as it contains some test files that use
# licenses that are rejected.
# The official tarball has to be always processed by the process-official-tarball
# script
# Link to original tarball: https://archive.mozilla.org/pub/firefox/releases/%{version}%{?pre_version}/source/firefox-%{version}%{?pre_version}.source.tar.xz
Source0:        firefox-%{version}%{?pre_version}.processed-source.tar.xz
%if %{with langpacks}
# wget -r -np -nH -R index.html https://archive.mozilla.org/pub/firefox/releases/%{version}%{?pre_version}/linux-x86_64/xpi/
Source1:        firefox-langpacks-%{version}%{?pre_version}-20230824.tar.xz
%endif
Source2:        cbindgen-vendor.tar.xz
Source3:        process-official-tarball
Source10:       firefox-mozconfig
Source12:       firefox-openeuler-default-prefs.js
Source20:       firefox.desktop
Source21:       firefox.sh.in
Source23:       firefox.1
Source24:       mozilla-api-key
Source25:       firefox-symbolic.svg
Source26:       distribution.ini
Source27:       google-api-key
Source30:       firefox-x11.sh.in
Source31:       firefox-x11.desktop
Source32:       node-stdout-nonblocking-wrapper
Source33:       firefox.appdata.xml.in
Source34:       firefox-search-provider.ini
Source35:       google-loc-api-key
Source37:       mochitest-python.tar.gz
Source38:       print_results
Source39:       print-errors
Source40:       run-tests-x11
Source41:       run-tests-wayland
Source42:       psummary
Source43:       print_failures
Source44:       print-error-reftest
Source45:       run-wayland-compositor

# Build patches
Patch3:         mozilla-build-arm.patch
Patch25:        rhbz-1219542-s390-build.patch
# Fixing missing cacheFlush when JS_CODEGEN_NONE is used (s390x)
Patch40:        build-aarch64-skia.patch
Patch41:        build-disable-elfhack.patch
Patch44:        build-arm-libopus.patch
Patch49:        build-arm-libaom.patch
Patch53:        firefox-gcc-build.patch
Patch54:        mozilla-1669639.patch
Patch71:        0001-GLIBCXX-fix-for-GCC-12.patch
Patch72:        build-remove-dav1d-from-wayland-dep.patch
Patch75:        build-big-endian-errors.patch
Patch76:        firefox-nss-version.patch
Patch77:        mozilla-1775202.patch


# Test patches
# Generate without context by
# GENDIFF_DIFF_ARGS=-U0 gendiff firefox-xxxx .firefox-tests-xpcshell
# GENDIFF_DIFF_ARGS=-U0 gendiff firefox-xxxx .firefox-tests-reftest
Patch102:       firefox-tests-xpcshell-freeze.patch

# Fedora specific patches
Patch215:        firefox-enable-addons.patch
Patch219:        rhbz-1173156.patch
Patch224:        mozilla-1170092.patch
#ARM run-time patch
Patch226:        rhbz-1354671.patch
Patch229:        firefox-nss-addon-hack.patch
Patch230:        D110204-fscreen.patch
Patch231:        webrtc-nss-fix.patch
# Backported WebRTC changes for PipeWire/Wayland screen sharing support
Patch232:        libwebrtc-screen-cast-sync.patch

# Upstream patches
Patch415:        mozilla-1670333.patch
Patch416:        mozilla-bmo1005535.patch
Patch417:        mozilla-bmo1504834-part1.patch
Patch418:        mozilla-bmo1504834-part3.patch
Patch419:        mozilla-bmo849632.patch
Patch420:        mozilla-bmo998749.patch
Patch421:        mozilla-s390x-skia-gradient.patch
Patch422:        one_swizzle_to_rule_them_all.patch
Patch423:        svg-rendering.patch
Patch424:        D158770.patch
Patch425:        disable-glean-sdk,psutil,zstandard.patch
Patch426:        mozilla-1833330.patch
# https://hg.mozilla.org/releases/mozilla-esr115/rev/96bd93fca47ae72ff0385d2bd87ec7bd18382b0c
Patch427:        CVE-2023-4863-1.patch
# https://hg.mozilla.org/releases/mozilla-esr115/rev/cbbf997c33890c2c49d24079db83b6ebb74cd7d8
Patch428:        CVE-2023-4863-2.patch

# PGO/LTO patches
Patch600:        pgo.patch
Patch602:        mozilla-1516803.patch
%if %{?system_nss}
BuildRequires:  pkgconfig(nspr) >= %{nspr_version}
BuildRequires:  pkgconfig(nss) >= %{nss_version}
BuildRequires:  nss-static >= %{nss_version}
%endif
BuildRequires:  pkgconfig(libpng)
%if %{?system_jpeg}
BuildRequires:  libjpeg-devel
%endif
BuildRequires:  zip
BuildRequires:  bzip2-devel
BuildRequires:  pkgconfig(zlib)
BuildRequires:  pkgconfig(gtk+-3.0)
BuildRequires:  pkgconfig(gtk+-2.0)
BuildRequires:  pkgconfig(krb5)
BuildRequires:  pkgconfig(pango)
BuildRequires:  pkgconfig(freetype2) >= %{freetype_version}
BuildRequires:  pkgconfig(xt)
BuildRequires:  pkgconfig(xtst)
BuildRequires:  pkgconfig(xrender)
BuildRequires:  pkgconfig(libstartup-notification-1.0)
BuildRequires:  pkgconfig(libnotify) >= %{libnotify_version}
BuildRequires:  pkgconfig(dri)
BuildRequires:  pkgconfig(libcurl)
BuildRequires:  pkgconfig(alsa)
BuildRequires:  libstdc++-devel
BuildRequires:  libstdc++-static
BuildRequires:  dbus-glib-devel
%if %{?system_libvpx}
BuildRequires:  libvpx-devel >= %{libvpx_version}
%endif
BuildRequires:  m4
BuildRequires:  pkgconfig(libpulse)
BuildRequires:  gcc
BuildRequires:  gcc-c++
BuildRequires:  nodejs >= 10.21
BuildRequires:  cargo
BuildRequires:  rust >= %{rust_version}
BuildRequires:  llvm >= %{llvm_version}
BuildRequires:  llvm-devel >= %{llvm_version}
BuildRequires:  clang >= %{llvm_version}
BuildRequires:  clang-devel >= %{llvm_version}
BuildRequires:  rustfmt >= %{rust_version}
BuildRequires:  pipewire-devel
%if !0%{?use_bundled_cbindgen}
BuildRequires:  cbindgen
%endif
BuildRequires:  nasm >= 1.13
BuildRequires:  libappstream-glib

%if 0%{?big_endian}
BuildRequires:  icu
%endif

Requires:       mozilla-filesystem
#Recommends:     mozilla-openh264 >= 2.1.1
#Recommends:     libva
Requires:       p11-kit-trust
Requires:       pciutils-libs
%if %{?system_nss}
Requires:       nspr >= %{nspr_build_version}
Requires:       nss >= %{nss_build_version}
%endif
BuildRequires:  python3-devel
BuildRequires:  python3-setuptools

BuildRequires:  desktop-file-utils

#%if !0%{?flatpak}
#TODO
#BuildRequires:  system-bookmarks
#%endif
%if %{?system_ffi}
BuildRequires:  pkgconfig(libffi)
%endif

%if 0%{?use_xvfb}
BuildRequires:  xorg-x11-server-Xvfb
%endif
%if %{build_with_asan}
BuildRequires:  libasan
BuildRequires:  libasan-static
%endif
BuildRequires:  perl-interpreter
#BuildRequires:  fdk-aac-free-devel not on openEuler
%if 0%{?test_on_wayland}
BuildRequires:  mutter
BuildRequires:  gsettings-desktop-schemas
BuildRequires:  gnome-settings-daemon
BuildRequires:  mesa-dri-drivers
BuildRequires:  xorg-x11-server-Xwayland
BuildRequires:  dbus-x11
BuildRequires:  gnome-keyring
%endif
%if 0%{?run_firefox_tests}
BuildRequires:  procps-ng
BuildRequires:  nss-tools
BuildRequires:  dejavu-sans-mono-fonts
BuildRequires:  dejavu-sans-fonts
BuildRequires:  dejavu-serif-fonts
BuildRequires:  dbus-x11
BuildRequires:  gnome-keyring
BuildRequires:  mesa-dri-drivers
# ----------------------------------------
BuildRequires:  liberation-fonts-common
BuildRequires:  liberation-mono-fonts
BuildRequires:  liberation-sans-fonts
BuildRequires:  liberation-serif-fonts
# ----------------------------------
BuildRequires:  google-crosextra-carlito-fonts
BuildRequires:  google-droid-sans-fonts
BuildRequires:  google-noto-fonts-common
BuildRequires:  google-noto-cjk-fonts-common
BuildRequires:  google-noto-sans-cjk-ttc-fonts
BuildRequires:  google-noto-sans-gurmukhi-fonts
BuildRequires:  google-noto-sans-fonts
BuildRequires:  google-noto-emoji-color-fonts
BuildRequires:  google-noto-sans-sinhala-vf-fonts
# -----------------------------------
BuildRequires:  thai-scalable-fonts-common
BuildRequires:  thai-scalable-waree-fonts
BuildRequires:  khmeros-base-fonts
BuildRequires:  jomolhari-fonts
# ----------------------------------
BuildRequires:  lohit-tamil-fonts
BuildRequires:  lohit-telugu-fonts
# ----------------------------------
BuildRequires:  paktype-naskh-basic-fonts
BuildRequires:  paratype-pt-sans-fonts
BuildRequires:  smc-meera-fonts
BuildRequires:  stix-fonts
BuildRequires:  abattis-cantarell-fonts
BuildRequires:  xorg-x11-fonts-ISO8859-1-100dpi
BuildRequires:  xorg-x11-fonts-misc
%endif
BuildRequires:  make
BuildRequires:  pciutils-libs

Obsoletes:      mozilla <= 37:1.7.13
Provides:       webclient

# Bundled libraries
Provides: bundled(angle)
Provides: bundled(audioipc-2)
Provides: bundled(brotli)
Provides: bundled(cairo)
Provides: bundled(cfworker)
Provides: bundled(d3.js)
Provides: bundled(double-conversion)
Provides: bundled(expat)
Provides: bundled(fdlibm)
Provides: bundled(ffvpx)
Provides: bundled(freetype2)
Provides: bundled(graphite2)
Provides: bundled(harfbuzz)
Provides: bundled(highway)
Provides: bundled(intgemm)
Provides: bundled(kissfft)
Provides: bundled(libaom)
Provides: bundled(libcubeb)
Provides: bundled(libdav1d)
Provides: bundled(libjpeg)
Provides: bundled(libjxl)
Provides: bundled(libjxl)
Provides: bundled(libmar)
Provides: bundled(libmkv)
Provides: bundled(libnestegg)
Provides: bundled(libogg)
Provides: bundled(libopus)
Provides: bundled(libpng)
Provides: bundled(libprio)
Provides: bundled(libsoundtouch)
Provides: bundled(libspeex_resampler)
Provides: bundled(libsrtp)
Provides: bundled(libtheora)
Provides: bundled(libtremor)
Provides: bundled(libvorbis)
Provides: bundled(libvpx)
Provides: bundled(libwebp)
Provides: bundled(libwebrtc)
Provides: bundled(libyuv)
Provides: bundled(mp4parse-rust)
Provides: bundled(mp4parse-rust)
Provides: bundled(msgpack-c)
Provides: bundled(msgpack-c)
Provides: bundled(mtransport)
Provides: bundled(openmax_dl)
Provides: bundled(openmax_il)
Provides: bundled(openmax_il)
Provides: bundled(ots)
Provides: bundled(qcms)
Provides: bundled(rlbox_sandboxing_api)
Provides: bundled(sfntly)
Provides: bundled(sipcc)
Provides: bundled(skia)
Provides: bundled(sqlite3)
Provides: bundled(thebes)
Provides: bundled(wasm2c)
Provides: bundled(WebRender)
Provides: bundled(woff2)
Provides: bundled(xz-embedded)
Provides: bundled(ycbcr)
Provides: bundled(zlib)

%description
Mozilla Firefox is an open-source web browser, designed for standards
compliance, performance and portability.

%if 0%{?run_firefox_tests}
%global testsuite_pkg_name %{name}-testresults
%package -n %{testsuite_pkg_name}
Summary: Results of testsuite
%description -n %{testsuite_pkg_name}
This package contains results of tests executed during build.
%files -n %{testsuite_pkg_name}
/%{version}-%{release}/test_results
/%{version}-%{release}/test_summary.txt
/%{version}-%{release}/failures-*
%endif

%if 0%{?wayland_backend_default}
%package x11
Summary: Firefox X11 launcher.
Requires: %{name}
%description x11
The firefox-x11 package contains launcher and desktop file
to run Firefox explicitly on X11.
%files x11
%{_bindir}/firefox-x11
%{_datadir}/applications/firefox-x11.desktop
%endif

#---------------------------------------------------------------------

%prep
%setup -q -n %{tarballdir}

# Build patches, can't change backup suffix from default because during build
# there is a compare of config and js/config directories and .orig suffix is
# ignored during this compare.

%ifarch s390
%patch25 -p1 -b .rhbz-1219542-s390
%endif
%patch40 -p1 -b .aarch64-skia
%if 0%{?disable_elfhack}
%patch41 -p1 -b .disable-elfhack
%endif
%patch3  -p1 -b .arm
%patch44 -p1 -b .build-arm-libopus
%patch49 -p1 -b .build-arm-libaom
%patch53 -p1 -b .firefox-gcc-build
%patch54 -p1 -b .1669639
%patch71 -p1 -b .0001-GLIBCXX-fix-for-GCC-12
%patch72 -p1 -b .build-remove-dav1d-from-wayland-dep

%patch75 -p1 -b .build-big-endian-errors
%patch76 -p1 -b .firefox-nss-version
#ppc64le build failure because of webrtc
%patch77 -p1 -b .mozilla-1775202

# Test patches
%patch102 -p1 -b .firefox-tests-xpcshell-freeze

# Fedora patches
%patch215 -p1 -b .addons
%patch219 -p1 -b .rhbz-1173156
%patch224 -p1 -b .1170092
#ARM run-time patch
%ifarch aarch64
%patch226 -p1 -b .1354671
%endif
%patch229 -p1 -b .firefox-nss-addon-hack
%patch230 -p1 -b .D110204-fscreen.diff
%patch231 -p1 -b .webrtc-nss-fix
%patch232 -p1 -b .libwebrtc-screen-cast-sync

%patch415 -p1 -b .1670333
%patch416 -p1 -b .mozilla-bmo1005535
%patch417 -p1 -b .mozilla-bmo1504834-part1
%patch418 -p1 -b .mozilla-bmo1504834-part3
%patch419 -p1 -b .mozilla-bmo849632
%patch420 -p1 -b .mozilla-bmo998749
%patch421 -p1 -b .mozilla-s390x-skia-gradient
%patch422 -p1 -b .one_swizzle_to_rule_them_all
%patch423 -p1 -b .svg-rendering
%patch424 -p1 -b .D158770.diff
%patch425 -p1
%patch426 -p1 -b .mozilla-1833330
%patch427 -p1
%patch428 -p1

# PGO patches
%if %{build_with_pgo}
%if !%{build_with_clang}
%patch600 -p1 -b .pgo
%patch602 -p1 -b .1516803
%endif
%endif

%{__rm} -f .mozconfig
%{__cp} %{SOURCE10} .mozconfig
echo "ac_add_options --enable-default-toolkit=cairo-gtk3-wayland" >> .mozconfig
%if %{official_branding}
echo "ac_add_options --enable-official-branding" >> .mozconfig
%endif
%{__cp} %{SOURCE24} mozilla-api-key
%{__cp} %{SOURCE27} google-api-key
%{__cp} %{SOURCE35} google-loc-api-key

echo "ac_add_options --prefix=\"%{_prefix}\"" >> .mozconfig
echo "ac_add_options --libdir=\"%{_libdir}\"" >> .mozconfig

%if %{?system_nss}
echo "ac_add_options --with-system-nspr" >> .mozconfig
echo "ac_add_options --with-system-nss" >> .mozconfig
%else
echo "ac_add_options --without-system-nspr" >> .mozconfig
echo "ac_add_options --without-system-nss" >> .mozconfig
%endif

%if %{?system_ffi}
echo "ac_add_options --enable-system-ffi" >> .mozconfig
%endif

%ifarch %{arm}
echo "ac_add_options --disable-elf-hack" >> .mozconfig
%endif

%if %{?debug_build}
echo "ac_add_options --enable-debug" >> .mozconfig
echo "ac_add_options --disable-optimize" >> .mozconfig
%else
%global optimize_flags "none"
%ifarch s390x
%global optimize_flags "-g -O1"
%endif
%ifarch ppc64le aarch64
%global optimize_flags "-g -O2"
%endif
%if %{optimize_flags} != "none"
echo 'ac_add_options --enable-optimize=%{?optimize_flags}' >> .mozconfig
%else
echo 'ac_add_options --enable-optimize' >> .mozconfig
%endif
echo "ac_add_options --disable-debug" >> .mozconfig
%endif

# Second arches fail to start with jemalloc enabled
%ifnarch %{ix86} x86_64
echo "ac_add_options --disable-jemalloc" >> .mozconfig
%endif

%if 0%{?build_tests}
echo "ac_add_options --enable-tests" >> .mozconfig
%else
echo "ac_add_options --disable-tests" >> .mozconfig
%endif

%if !%{?system_jpeg}
echo "ac_add_options --without-system-jpeg" >> .mozconfig
%else
echo "ac_add_options --with-system-jpeg" >> .mozconfig
%endif

%if %{?system_libvpx}
echo "ac_add_options --with-system-libvpx" >> .mozconfig
%else
echo "ac_add_options --without-system-libvpx" >> .mozconfig
%endif

%ifarch s390 s390x
echo "ac_add_options --disable-jit" >> .mozconfig
%endif

%if %{build_with_asan}
echo "ac_add_options --enable-address-sanitizer" >> .mozconfig
echo "ac_add_options --disable-jemalloc" >> .mozconfig
%endif

%if !%{enable_mozilla_crashreporter}
echo "ac_add_options --disable-crashreporter" >> .mozconfig
%endif

# api keys full path
echo "ac_add_options --with-mozilla-api-keyfile=`pwd`/mozilla-api-key" >> .mozconfig
# It seems that the api key we have is for the safe browsing only
echo "ac_add_options --with-google-location-service-api-keyfile=`pwd`/google-loc-api-key" >> .mozconfig
echo "ac_add_options --with-google-safebrowsing-api-keyfile=`pwd`/google-api-key" >> .mozconfig

#echo 'export NODEJS="%{_buildrootdir}/bin/node-stdout-nonblocking-wrapper"' >> .mozconfig

# Remove executable bit to make brp-mangle-shebangs happy.
chmod -x third_party/rust/itertools/src/lib.rs
chmod a-x third_party/rust/ash/src/extensions/ext/*.rs
chmod a-x third_party/rust/ash/src/extensions/khr/*.rs
chmod a-x third_party/rust/ash/src/extensions/nv/*.rs

#---------------------------------------------------------------------

%build
# Disable LTO to work around rhbz#1883904
%define _lto_cflags %{nil}
export PATH="%{_buildrootdir}/bin:$PATH"
# Cleanup buildroot for existing rpms from bundled nss/nspr and other packages
rm -rf %{_buildrootdir}/*

function install_rpms_to_current_dir() {
    PACKAGE_RPM=$(eval echo $1)
    PACKAGE_DIR=%{_rpmdir}

    if [ ! -f $PACKAGE_DIR/$PACKAGE_RPM ]; then
        # Hack for tps tests
        ARCH_STR=%{_arch}
        %ifarch %{ix86}
            ARCH_STR="i?86"
        %endif
        PACKAGE_DIR="$PACKAGE_DIR/$ARCH_STR"
     fi

     for package in $(ls $PACKAGE_DIR/$PACKAGE_RPM)
     do
         echo "$package"
         rpm2cpio "$package" | cpio -idu
     done
}

env
which gcc
which c++
which g++
which ld
which nasm
which node
%if 0%{?use_bundled_cbindgen}
mkdir -p my_rust_vendor
cd my_rust_vendor
%{__tar} xf %{SOURCE2}
mkdir -p .cargo
cat > .cargo/config <<EOL
[source.crates-io]
replace-with = "vendored-sources"

[source.vendored-sources]
directory = "`pwd`"
EOL

%ifarch aarch64
#export RUSTFLAGS="-Cdebuginfo=0 -Clinker=/opt/rh/gcc-toolset-12/root/usr/bin/gcc"
%endif

env CARGO_HOME=.cargo cargo install cbindgen
export PATH=`pwd`/.cargo/bin:$PATH
cd -
%endif

mkdir %{_buildrootdir}/bin || :
cp %{SOURCE32} %{_buildrootdir}/bin || :
ls -l %{_buildrootdir}/bin
ls -l %{_buildrootdir}
chmod a+x %{_buildrootdir}/bin

# Update the various config.guess to upstream release for aarch64 support
# Do not update config.guess in the ./third_party/rust because that would break checksums
find ./ -path ./third_party/rust -prune -o -name config.guess -exec cp /usr/lib/rpm/config.guess {} ';'

MOZ_OPT_FLAGS=$(echo "%{optflags}" | %{__sed} -e 's/-Wall//')
MOZ_OPT_FLAGS=$(echo "$MOZ_OPT_FLAGS" | %{__sed} -e 's/-Werror=format-security//')
MOZ_OPT_FLAGS="$MOZ_OPT_FLAGS -fpermissive"
%if %{?hardened_build}
MOZ_OPT_FLAGS="$MOZ_OPT_FLAGS -fPIC -Wl,-z,relro -Wl,-z,now"
%endif
%if %{?debug_build}
MOZ_OPT_FLAGS=$(echo "$MOZ_OPT_FLAGS" | %{__sed} -e 's/-O2//')
%endif
%ifarch s390
MOZ_OPT_FLAGS=$(echo "$MOZ_OPT_FLAGS" | %{__sed} -e 's/-g/-g1/')
# If MOZ_DEBUG_FLAGS is empty, firefox's build will default it to "-g" which
# overrides the -g1 from line above and breaks building on s390/arm
# (OOM when linking, rhbz#1238225)
export MOZ_DEBUG_FLAGS=" "
%endif
%ifarch %{arm} %{ix86}
MOZ_OPT_FLAGS=$(echo "$MOZ_OPT_FLAGS" | %{__sed} -e 's/-g/-g0/')
export MOZ_DEBUG_FLAGS=" "
%endif
%if !%{build_with_clang}
%ifarch s390x ppc aarch64 %{ix86}
MOZ_LINK_FLAGS="-Wl,--no-keep-memory -Wl,--reduce-memory-overheads"
%endif
%ifarch %{arm}
MOZ_LINK_FLAGS="-Wl,--no-keep-memory -Wl,--strip-debug"
echo "ac_add_options --enable-linker=gold" >> .mozconfig
%endif
%endif
%if 0%{?flatpak}
# Make sure the linker can find libraries in /app/lib64 as we don't use
# __global_ldflags that normally sets this.
MOZ_LINK_FLAGS="$MOZ_LINK_FLAGS -L%{_libdir}"
%endif
%ifarch %{arm} %{ix86} %{s390x}
export RUSTFLAGS="-Cdebuginfo=0"
echo 'export RUSTFLAGS="-Cdebuginfo=0"' >> .mozconfig
%endif
#%ifarch aarch64
#export RUSTFLAGS="-Cdebuginfo=0 -Clinker=/opt/rh/gcc-toolset-12/root/usr/bin/gcc"
#echo 'export RUSTFLAGS="-Cdebuginfo=0 -Clinker=/opt/rh/gcc-toolset-12/root/usr/bin/gcc"' >> .mozconfig
#%endif
%if %{build_with_asan}
MOZ_OPT_FLAGS="$MOZ_OPT_FLAGS -fsanitize=address -Dxmalloc=myxmalloc"
MOZ_LINK_FLAGS="$MOZ_LINK_FLAGS -fsanitize=address -ldl"
%endif

# We don't wantfirefox to use CK_GCM_PARAMS_V3 in nss
MOZ_OPT_FLAGS="$MOZ_OPT_FLAGS -DNSS_PKCS11_3_0_STRICT"

echo "export CFLAGS=\"$MOZ_OPT_FLAGS\"" >> .mozconfig
echo "export CXXFLAGS=\"$MOZ_OPT_FLAGS\"" >> .mozconfig
echo "export LDFLAGS=\"$MOZ_LINK_FLAGS\"" >> .mozconfig

%if %{build_with_clang}
echo "export LLVM_PROFDATA=\"llvm-profdata\"" >> .mozconfig
echo "export AR=\"llvm-ar\"" >> .mozconfig
echo "export NM=\"llvm-nm\"" >> .mozconfig
echo "export RANLIB=\"llvm-ranlib\"" >> .mozconfig
echo "ac_add_options --enable-linker=lld" >> .mozconfig
%else
echo "export CC=gcc" >> .mozconfig
echo "export CXX=g++" >> .mozconfig
echo "export AR=\"gcc-ar\"" >> .mozconfig
echo "export NM=\"gcc-nm\"" >> .mozconfig
echo "export RANLIB=\"gcc-ranlib\"" >> .mozconfig
%endif
%if 0%{?build_with_pgo}
echo "ac_add_options MOZ_PGO=1" >> .mozconfig
# PGO build doesn't work with ccache
export CCACHE_DISABLE=1
%endif

MOZ_SMP_FLAGS=-j1
# On x86_64 architectures, Mozilla can build up to 4 jobs at once in parallel,
# however builds tend to fail on other arches when building in parallel.
#%ifarch %{ix86} s390x %{arm} aarch64 ppc64le
#[ -z "$RPM_BUILD_NCPUS" ] && \
#     RPM_BUILD_NCPUS="`/usr/bin/getconf _NPROCESSORS_ONLN`"
#[ "$RPM_BUILD_NCPUS" -ge 2 ] && MOZ_SMP_FLAGS=-j2
#%endif
#%ifarch x86_64 ppc ppc64 ppc64le
[ -z "$RPM_BUILD_NCPUS" ] && \
       RPM_BUILD_NCPUS="`/usr/bin/getconf _NPROCESSORS_ONLN`"
[ "$RPM_BUILD_NCPUS" -ge 2 ] && MOZ_SMP_FLAGS=-j2
[ "$RPM_BUILD_NCPUS" -ge 4 ] && MOZ_SMP_FLAGS=-j4
[ "$RPM_BUILD_NCPUS" -ge 8 ] && MOZ_SMP_FLAGS=-j8
[ "$RPM_BUILD_NCPUS" -ge 16 ] && MOZ_SMP_FLAGS=-j16
#%endif

echo "mk_add_options MOZ_MAKE_FLAGS=\"$MOZ_SMP_FLAGS\"" >> .mozconfig
echo "mk_add_options MOZ_SERVICES_SYNC=1" >> .mozconfig
echo "export STRIP=/bin/true" >> .mozconfig
#export MACH_USE_SYSTEM_PYTHON=1

%if %{launch_wayland_compositor}
cp %{SOURCE45} .
. ./run-wayland-compositor
%endif


%if %{build_with_pgo}
%if 0%{?test_on_wayland}
env | grep "WAYLAND"
MOZ_ENABLE_WAYLAND=1 ./mach build -v 2>&1 || exit 1
%else
xvfb-run ./mach build -v 2>&1 || exit 1
%endif
%else
./mach build -v 2>&1 || exit 1
%endif

#---------------------------------------------------------------------
%install
export MACH_USE_SYSTEM_PYTHON=1
function install_rpms_to_current_dir() {
    PACKAGE_RPM=$(eval echo $1)
    PACKAGE_DIR=%{_rpmdir}

    if [ ! -f $PACKAGE_DIR/$PACKAGE_RPM ]; then
        # Hack for tps tests
        ARCH_STR=%{_arch}
        %ifarch %{ix86}
            ARCH_STR="i?86"
        %endif
        PACKAGE_DIR="$PACKAGE_DIR/$ARCH_STR"
     fi

     for package in $(ls $PACKAGE_DIR/$PACKAGE_RPM)
     do
         echo "$package"
         rpm2cpio "$package" | cpio -idu
     done
}

%if %{launch_wayland_compositor}
cp %{SOURCE45} .
. ./run-wayland-compositor
%endif

%if 0%{?run_firefox_tests}
mkdir -p objdir/_virtualenvs/init_py3
%{__cat} > objdir/_virtualenvs/init_py3/pip.conf << EOF
[global]
find-links=`pwd`/mochitest-python
no-index=true
EOF
tar xf %{SOURCE37}
cp %{SOURCE40} %{SOURCE41} %{SOURCE42} %{SOURCE38} %{SOURCE39} %{SOURCE43} %{SOURCE44} .
mkdir -p test_results
%if %{?test_on_wayland}
./run-tests-wayland || true
%else
./run-tests-x11 || true
%endif
./print_results > test_summary.txt 2>&1 || true
./print_failures || true
%endif

# set up our default bookmarks
#%if !0%{?flatpak}
#%{__cp} -p %{default_bookmarks_file} objdir/dist/bin/browser/chrome/browser/content/browser/default-bookmarks.html
#%endif

# Make sure locale works for langpacks
%{__cat} > objdir/dist/bin/browser/defaults/preferences/firefox-l10n.js << EOF
pref("general.useragent.locale", "chrome://global/locale/intl.properties");
EOF

DESTDIR=%{buildroot} make -C objdir install

%{__mkdir_p} %{buildroot}{%{_libdir},%{_bindir},%{_datadir}/applications}

desktop-file-install --dir %{buildroot}%{_datadir}/applications %{SOURCE20}
%if 0%{?wayland_backend_default}
desktop-file-install --dir %{buildroot}%{_datadir}/applications %{SOURCE31}
%endif

# set up the firefox start script
%{__rm} -rf %{buildroot}%{_bindir}/firefox
%{__sed} -e 's,/__PREFIX__,%{_prefix},g' %{SOURCE21} > %{buildroot}%{_bindir}/firefox
%{__chmod} 755 %{buildroot}%{_bindir}/firefox

%if 0%{?flatpak}
sed -i -e 's|%FLATPAK_ENV_VARS%|export TMPDIR="$XDG_CACHE_HOME/tmp"|' %{buildroot}%{_bindir}/firefox
%else
sed -i -e 's|%FLATPAK_ENV_VARS%||' %{buildroot}%{_bindir}/firefox
%endif

# Run firefox under wayland only on RHEL9 and newer
%if !0%{?wayland_backend_default}
sed -i -e 's|%DISABLE_WAYLAND_PLACEHOLDER%|export MOZ_DISABLE_WAYLAND=1|' %{buildroot}%{_bindir}/firefox
%else
sed -i -e 's|%DISABLE_WAYLAND_PLACEHOLDER%||' %{buildroot}%{_bindir}/firefox
# firefox-x11 launch script for RHEL9 only
%{__sed} -e 's,/__PREFIX__,%{_prefix},g' %{SOURCE30} > %{buildroot}%{_bindir}/firefox-x11
%{__chmod} 755 %{buildroot}%{_bindir}/firefox-x11
%endif

%{__install} -p -D -m 644 %{SOURCE23} %{buildroot}%{_mandir}/man1/firefox.1

%{__rm} -f %{buildroot}/%{mozappdir}/firefox-config
%{__rm} -f %{buildroot}/%{mozappdir}/update-settings.ini

for s in 16 22 24 32 48 256; do
    %{__mkdir_p} %{buildroot}%{_datadir}/icons/hicolor/${s}x${s}/apps
    %{__cp} -p browser/branding/official/default${s}.png \
               %{buildroot}%{_datadir}/icons/hicolor/${s}x${s}/apps/firefox.png
done

# Install hight contrast icon
%{__mkdir_p} %{buildroot}%{_datadir}/icons/hicolor/symbolic/apps
%{__cp} -p %{SOURCE25} \
           %{buildroot}%{_datadir}/icons/hicolor/symbolic/apps

echo > %{name}.lang
%if %{with langpacks}
# Extract langpacks, make any mods needed, repack the langpack, and install it.
%{__mkdir_p} %{buildroot}%{langpackdir}
%{__tar} xf %{SOURCE1}
for langpack in `ls firefox-langpacks/*.xpi`; do
  language=`basename $langpack .xpi`
  extensionID=langpack-$language@firefox.mozilla.org
  %{__mkdir_p} $extensionID
  unzip -qq $langpack -d $extensionID
  find $extensionID -type f | xargs chmod 644

  cd $extensionID
  zip -qq -r9mX ../${extensionID}.xpi *
  cd -

  %{__install} -m 644 ${extensionID}.xpi %{buildroot}%{langpackdir}
  language=`echo $language | sed -e 's/-/_/g'`
%if 0%{?flatpak}
  echo "%{langpackdir}/${extensionID}.xpi" >> %{name}.lang
%else
  echo "%%lang($language) %{langpackdir}/${extensionID}.xpi" >> %{name}.lang
%endif
done
%{__rm} -rf firefox-langpacks

# Install langpack workaround (see #707100, #821169)
function create_default_langpack() {
language_long=$1
language_short=$2
cd %{buildroot}%{langpackdir}
ln -s langpack-$language_long@firefox.mozilla.org.xpi langpack-$language_short@firefox.mozilla.org.xpi
cd -
echo "%%lang($language_short) %{langpackdir}/langpack-$language_short@firefox.mozilla.org.xpi" >> %{name}.lang
}

# Table of fallbacks for each language
#create_default_langpack "bn-IN" "bn"
create_default_langpack "es-AR" "es"
create_default_langpack "fy-NL" "fy"
create_default_langpack "ga-IE" "ga"
create_default_langpack "gu-IN" "gu"
create_default_langpack "hi-IN" "hi"
create_default_langpack "hy-AM" "hy"
create_default_langpack "nb-NO" "nb"
create_default_langpack "nn-NO" "nn"
create_default_langpack "pa-IN" "pa"
create_default_langpack "pt-PT" "pt"
create_default_langpack "sv-SE" "sv"
create_default_langpack "zh-TW" "zh"
%endif

# Keep compatibility with the old preference location.
%{__mkdir_p} %{buildroot}%{mozappdir}/browser/defaults/preferences
# Default preferences
%{__cp} %{SOURCE12} %{buildroot}%{mozappdir}/browser/defaults/preferences/firefox.js
sed -i -e 's|%PREFIX%|%{_prefix}|' %{buildroot}%{mozappdir}/browser/defaults/preferences/firefox.js
# System config dir
%{__mkdir_p} %{buildroot}/%{_sysconfdir}/%{name}/pref

# System extensions
%{__mkdir_p} %{buildroot}%{_datadir}/mozilla/extensions/%{firefox_app_id}
%{__mkdir_p} %{buildroot}%{_libdir}/mozilla/extensions/%{firefox_app_id}

# Copy over the LICENSE
%{__install} -p -c -m 644 LICENSE %{buildroot}/%{mozappdir}

# Use the system hunspell dictionaries
%{__rm} -rf %{buildroot}%{mozappdir}/dictionaries
ln -s %{_datadir}/myspell %{buildroot}%{mozappdir}/dictionaries

%if 0%{?run_firefox_tests}
%{__mkdir_p} %{buildroot}/%{version}-%{release}/test_results
%{__cp} test_results/* %{buildroot}/%{version}-%{release}/test_results
%{__cp} test_summary.txt %{buildroot}/%{version}-%{release}/
%{__cp} failures-* %{buildroot}/%{version}-%{release}/ || true
%endif

# Copy over run-mozilla.sh
%{__cp} build/unix/run-mozilla.sh %{buildroot}%{mozappdir}

# Add distribution.ini
%{__mkdir_p} %{buildroot}%{mozappdir}/distribution
%{__cp} %{SOURCE26} %{buildroot}%{mozappdir}/distribution

# Install appdata file
mkdir -p %{buildroot}%{_datadir}/metainfo
%{__sed} -e "s/__VERSION__/%{version}/" \
         -e "s/__DATE__/$(date '+%Y-%m-%d')/" \
         %{SOURCE33} > %{buildroot}%{_datadir}/metainfo/firefox.appdata.xml

# Install Gnome search provider files
mkdir -p %{buildroot}%{_datadir}/gnome-shell/search-providers
%{__cp} %{SOURCE34} %{buildroot}%{_datadir}/gnome-shell/search-providers

# Remove gtk2 support as flash plugin is no longer supported
rm -rf %{buildroot}%{mozappdir}/gtk2/

# Remove copied libraries to speed up build
rm -f %{buildroot}%{mozappdirdev}/sdk/lib/libmozjs.so
rm -f %{buildroot}%{mozappdirdev}/sdk/lib/libmozalloc.so
rm -f %{buildroot}%{mozappdirdev}/sdk/lib/libxul.so

# Create a symlink to replace libnssckbi.so with p11-kit-client.so
# instead of p11-kit-trust.so, so that Firefox can see the system
# trust store on the host through the p11-kit RPC protocol.  A symlink
# to libnss3.so is also needed, because Firefox tries to load
# libnssckbi.so from the same directory where libnss3.so is loaded (as
# of Firefox 89).
%if 0%{?flatpak}
ln -sf /usr/lib64/libnss3.so %{buildroot}%{_libdir}/libnss3.so
ln -sf /usr/lib64/pkcs11/p11-kit-client.so %{buildroot}%{_libdir}/libnssckbi.so
%endif
#---------------------------------------------------------------------

%check
appstream-util validate-relax --nonet %{buildroot}%{_datadir}/metainfo/*.appdata.xml

%preun
# is it a final removal?
if [ $1 -eq 0 ]; then
  %{__rm} -rf %{mozappdir}/components
  %{__rm} -rf %{mozappdir}/extensions
  %{__rm} -rf %{mozappdir}/plugins
fi

%post
update-desktop-database &> /dev/null || :
touch --no-create %{_datadir}/icons/hicolor &>/dev/null || :

%postun
update-desktop-database &> /dev/null || :
if [ $1 -eq 0 ] ; then
    touch --no-create %{_datadir}/icons/hicolor &>/dev/null
    gtk-update-icon-cache %{_datadir}/icons/hicolor &>/dev/null || :
    %{__rm} -rf %{langpackdir}
fi

%posttrans
gtk-update-icon-cache %{_datadir}/icons/hicolor &>/dev/null || :

%files -f %{name}.lang
%{_bindir}/firefox
%{mozappdir}/firefox
%{mozappdir}/firefox-bin
%doc %{_mandir}/man1/*
%dir %{_sysconfdir}/%{name}
%dir %{_sysconfdir}/%{name}/*
%dir %{_datadir}/mozilla/extensions/*
%dir %{_libdir}/mozilla/extensions/*
%{_datadir}/applications/%{name}.desktop
%{_datadir}/metainfo/*.appdata.xml
%{_datadir}/gnome-shell/search-providers/*.ini
%dir %{mozappdir}
%license %{mozappdir}/LICENSE
%{mozappdir}/browser/chrome
%{mozappdir}/browser/defaults/preferences/firefox.js
%{mozappdir}/browser/features/*.xpi
%{mozappdir}/distribution/distribution.ini
# That's Windows only
%ghost %{mozappdir}/browser/features/aushelper@mozilla.org.xpi
%if %{with langpacks}
%dir %{langpackdir}
%endif
%{mozappdir}/browser/omni.ja
%{mozappdir}/run-mozilla.sh
%{mozappdir}/application.ini
%{mozappdir}/pingsender
%exclude %{mozappdir}/removed-files
%if 0%{?flatpak}
%{_libdir}/libnss3.so
%{_libdir}/libnssckbi.so
%endif
%{_datadir}/icons/hicolor/16x16/apps/firefox.png
%{_datadir}/icons/hicolor/22x22/apps/firefox.png
%{_datadir}/icons/hicolor/24x24/apps/firefox.png
%{_datadir}/icons/hicolor/256x256/apps/firefox.png
%{_datadir}/icons/hicolor/32x32/apps/firefox.png
%{_datadir}/icons/hicolor/48x48/apps/firefox.png
%{_datadir}/icons/hicolor/symbolic/apps/firefox-symbolic.svg
%if %{enable_mozilla_crashreporter}
%{mozappdir}/crashreporter
%{mozappdir}/crashreporter.ini
%{mozappdir}/minidump-analyzer
%{mozappdir}/Throbber-small.gif
%{mozappdir}/browser/crashreporter-override.ini
%endif
%{mozappdir}/*.so
%{mozappdir}/defaults/pref/channel-prefs.js
%{mozappdir}/dependentlibs.list
%{mozappdir}/dictionaries
%{mozappdir}/omni.ja
%{mozappdir}/platform.ini
%{mozappdir}/plugin-container
%{mozappdir}/gmp-clearkey
%{mozappdir}/fonts/TwemojiMozilla.ttf
%if !%{?system_nss}
%exclude %{mozappdir}/libnssckbi.so
%endif
%if %{build_with_asan}
%{mozappdir}/llvm-symbolizer
%endif

%changelog
* Thu Sep 28 2023 wangkai <13474090681@163.com> - 102.15.0-2
- Fix for libwebp CVE-2023-4863

* Wed Sep 20 2023 lvfei <lvfei@kylinos.cn> - 102.15.0-1
- Update to 102.15.0
- Fix CVE-2023-4573 CVE-2023-4574 CVE-2023-4575 CVE-2023-4576  CVE-2023-4581 CVE-2023-4584

* Tue Aug 08 2023 wangkai <13474090681@163.com> - 102.14.0-1
- Update to 102.14.0
- Fix CVE-2023-4045 CVE-2023-4046 CVE-2023-4047 CVE-2023-4048 CVE-2023-4049 CVE-2023-4050 CVE-2023-4054 CVE-2023-4055 CVE-2023-4056

* Thu Jul 06 2023 wangkai <13474090681@163.com> - 102.13.0-1
- Update to 102.13.0
- Fix CVE-2023-34416 CVE-2023-34414 CVE-2023-37201 CVE-2023-37202 CVE-2023-37207 CVE-2023-37208 CVE-2023-37211
- Remove unused patch

* Wed May 10 2023 wangkai <13474090681@163.com> - 102.11.0-1
- Update to 102.11.0

* Fri Mar 31 2023 wangkai <wangkai385@h-partners.com> - 102.9.0-1
- Update to 102.9.0

* Fri Mar 03 2023 wangkai <wangkai385@h-partners.com> - 102.8.0-1
- Update to 102.8.0

* Tue Feb 14 2023 Ge Wang <wangge20@h-partners.com> - 79.0-14
- Fix build failure due to arc4random feature activated

* Thu Jul 21 2022 xu_ping <xuping33@h-partners.com> - 79.0-13
- Fix attribute error module distutils has no attribute sysconfig

* Fri May 6 2022 liyanan <liyanan32@h-partners.com> - 79.0-12
- Fix compilation failure by python3.10 and wayland 1.20

* Thu Oct 14 2021 zhangweiguo <zhangweiguo2@huawei.com> - 79.0-11
- Set RUSTFLAGS:debuginfo to 1

* Mon Aug 23 2021 lingsheng <lingsheng@huawei.com> - 79.0-10
- Fix firefox tab crash with clone3 in glibc 2.34

* Wed Aug 11 2021 caodongxia <caodongxia@huawei.com> - 79.0-9
- Fix glibc dynstack 

* Wed Jul  7 2021 lingsheng <lingsheng@huawei.com> - 79.0-8
- Fix firefox video tab crash with rust 1.51

* Mon Jul 05 2021 caodongxia <caodongxia@huawei.com> - 79.0-7
- Fix build with pipewire 0.3

* Fri May 14 2021 lingsheng <lingsheng@huawei.com> - 79.0-6
- Fix build with rust 1.51

* Thu Jan 07 2021 wangxiao <wangxiao65@huawei.com> - 79.0-5
- Fix CVE-2020-15969 CVE-2020-15999 CVE-2020-16012 CVE-2020-26951
  CVE-2020-26953 CVE-2020-26956 CVE-2020-26957 CVE-2020-26958
  CVE-2020-26959 CVE-2020-26960 CVE-2020-26961 CVE-2020-26963
  CVE-2020-26965 CVE-2020-25966 CVE-2020-26967

* Wed Dec 16 2020 wangxiao <wangxiao65@huawei.com> - 79.0-4
- Fix CVE-2020-15664 CVE-2020-15665 CVE-2020-15666 CVE-2020-15667
  CVE-2020-15668 CVE-2020-15676 CVE-2020-15677 CVE-2020-15678

* Thu Nov 28 2020 Jeffery.Gao <gaojianxing@huawei.com> - 79.0-3
- Fix firefox downgrade error

* Mon Oct 26 2020 lingsheng <lingsheng@huawei.com> - 79.0-2
- Fix firefox remove warning

* Mon Aug 17 2020 zhangjiapeng <zhangjiapeng9@huawei.com> - 79.0-1
- Update to 79.0-1

* Wed Jul 22 2020 lingsheng <lingsheng@huawei.com> - 62.0.3-7
- Fix firefox build with rust 1.38

* Mon May 25 2020 huanghaitao <huanghaitao8@huawei.com> - 62.0.3-6
- Type:cves
- ID: CVE-2020-6811 CVE-2020-6814
- SUG:restart
- DESC: fix CVE-2020-6811 CVE-2020-6814

* Mon May 25 2020 Captain Wei<captain.a.wei@gmail.com> - 62.0.3-5
- fix unstable_name_collisions problem when compile

* Wen Feb 26 2020 xuxijian<xuxijian@huawei.com> - 62.0.3-4
- Package init
