# Copyright (c) 2018, Amazon.com, Inc. or its affiliates. All Rights Reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 only, as
# published by the Free Software Foundation. Amazon designates this
# particular file as subject to the "Classpath" exception as provided
# by Oracle in the LICENSE file that accompanied this code.
#
# This code is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# version 2 for more details (a copy is included in the LICENSE file that
# accompanied this code).
#
# You should have received a copy of the GNU General Public License version
# 2 along with this work; if not, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.

%global javaver 1.8.0
%global java_major_version  %{javaver}
%global java_update @update@
%global buildver b@build@
%global corretto_revision @revision@
%global java_home %{_jvmdir}/%{name}.%{_arch}
%global experimental_feature     @experimental_feature@
%global additional_configure_options     @additional_configure_options@
%global zlib_option @zlib_option@
%global use_gcc_ver @use_gcc_ver@

# The experimental_feature macro gets set to %nil by the template, but that is still defined and
# the spec doesn't have a quick is not nil check, just define/not defined, this makes it easier to
# work with
%if 0%{?experimental_feature:1} && "%{experimental_feature}" == "%{nil}"
%undefine experimental_feature
%endif

%if 0%{?additional_configure_options:1} == 1 && "%{additional_configure_options}" == "%{nil}"
%undefine additional_configure_options
%endif

%if 0%{?use_gcc_ver:1} == 1 && "%{use_gcc_ver}" == "%{nil}"
%undefine use_gcc_ver
%endif
# Higher numbers win for the alternatives program.
# AL2 has chosen java-1.7.0-openjdk to be default (priority 17000)
# with java-1.8.0-openjdk as second (priority 1800)
%global alternatives_priority 100

# Instruct rpmbuild to copy jars rather than de-compress and re-compress.
%global __jar_repack 0

# Disable LinkTimeOptimization which causes issues with OpenJDK8
%global _lto_cflags %{nil}

# Disable checking of build-id headers.
# OpenJDK's build system was producing binaries with the
# same build-ids, but different md5 checksums.
# https://www.redhat.com/archives/rpm-list/2005-November/msg00023.html
%define debug_package %{nil}

# abs2rel: A macro to convert an absolute path to a relative path.
# When making a symlink in an RPM, prefer a symbolic link to ensure
# the link will work in a chroot environment.
%global script 'use File::Spec; print File::Spec->abs2rel($ARGV[0], $ARGV[1])'
%global abs2rel %{__perl} -e %{script}

# Filter out private libraries so RPM does not list them as provided/required
%global _privatelibs libjli[.]so.*|libattach[.]so.*|libawt[.]so.*|libawt_headless[.]so.*|libawt_xawt[.]so.*|libdt_socket[.]so.*|libfontmanager[.]so.*|libhprof[.]so.*|libinstrument[.]so.*|libj2gss[.]so.*|libj2pcsc[.]so.*|libj2pkcs11[.]so.*|libjaas_unix[.]so.*|libjava_crw_demo[.]so.*|libjdwp[.]so.*|libjpeg[.]so.*|libjsdt[.]so.*|libjsig[.]so.*|libjsound[.]so.*|libjsoundalsa[.]so.*|liblcms[.]so.*|libmanagement[.]so.*|libmlib_image[.]so.*|libnet[.]so.*|libnio[.]so.*|libnpt[.]so.*|libsaproc[.]so.*|libsctp[.]so.*|libsplashscreen[.]so.*|libsunec[.]so.*|libunpack[.]so.*|libzip[.]so.*|lib[.]so\\(SUNWprivate.*
%global _publiclibs libjawt[.]so.*|libjava[.]so.*|libjsig[.]so.*|libjvm[.]so.*|libverify[.]so.*
%global __provides_exclude ^(%{_privatelibs}|%{_publiclibs})$
%global __requires_exclude ^(%{_privatelibs}|%{_publiclibs})$

# By default install a bootstrap jdk. If one is already available,
# this can be skipped using --without bootjdk
%bcond_without bootjdk

# By default bundle OpenJFX on x86_64. To skip bundling, use --without bundlejfx
%bcond_without bundlejfx
%if "%{_arch}" == "x86_64" && %{with bundlejfx}
%global perform_openjfx_overlay 1
%else
%global perform_openjfx_overlay 0
%endif

# On aarch64 add additional C and CPP flags
%global platform_flags ""
%ifarch aarch64
%global platform_flags --with-extra-cflags="-moutline-atomics" --with-extra-cxxflags="-moutline-atomics"
%endif

Summary: Amazon Corretto runtime environment
Name: java-1.8.0-amazon-corretto%{?experimental_feature:-%{experimental_feature}}
# Matches the 'full version' from Java's release notes:
# https://www.oracle.com/technetwork/java/javase/8u181-relnotes-4479407.html
# Eg: 1.8.0_181-b13
Version: 1.8.0_%{java_update}.%{buildver}
Release: %{corretto_revision}%{?dist}
Epoch: 1
Group: Development/Languages
License: ASL 1.1 and ASL 2.0 and BSD and BSD with advertising and GPL+ and GPLv2 and GPLv2 with exceptions and IJG and LGPLv2+ and MIT and MPLv2.0 and Public Domain and W3C and zlib.
Vendor: Amazon
Url: https://github.com/corretto/corretto-8
Source0: java-1.8.0-amazon-corretto.tar.gz
ExclusiveArch: x86_64 aarch64

# jpackage-utils/javapackages-filesystem is required for both build and runtime.
# For build, it provides the jvmdir macro. For runtime,
# it provides the /usr/lib/jvm directory.
%if "%{dist}" == ".amzn2" || "%{dist}" == ".amzn2int"
BuildRequires: jpackage-utils
%else
BuildRequires: javapackages-filesystem
%endif
BuildRequires: autoconf
BuildRequires: automake
BuildRequires: alsa-lib-devel
BuildRequires: binutils
BuildRequires: cups-devel
BuildRequires: fontconfig-devel
BuildRequires: freetype-devel
BuildRequires: giflib-devel
BuildRequires: gcc%{?use_gcc_ver}-c++
BuildRequires: libjpeg-devel
BuildRequires: libpng-devel
BuildRequires: libxslt
BuildRequires: libX11-devel
BuildRequires: libXi-devel
BuildRequires: libXinerama-devel
BuildRequires: libXt-devel
BuildRequires: libXrender-devel
BuildRequires: libXtst-devel
BuildRequires: pkgconfig
BuildRequires: xorg-x11-proto-devel

# Use dist instead of al_ver to allow building the src.rpm on al2
%if "%{dist}" != ".amzn2"
BuildRequires: libXxf86vm-devel
BuildRequires: mesa-libGL-devel
%endif

%if %{with bootjdk}
BuildRequires: java-1.8.0-devel
%endif

# jpackage-utils/javapackages-filesystem is required for both build and runtime.
# For build, it provides the jvmdir macro. For runtime,
# it provides the /usr/lib/jvm directory.
%if "%{dist}" == ".amzn2" || "%{dist}" == ".amzn2int"
Requires: jpackage-utils
%else
Requires: javapackages-filesystem
%endif

Requires: dejavu-sans-fonts
Requires: dejavu-serif-fonts
Requires: dejavu-sans-mono-fonts
Requires: ca-certificates
Requires: zlib
Requires: giflib
Requires(post): chkconfig
Requires(postun): chkconfig
%if "%{dist}" == ".amzn2"
Requires: log4j-cve-2021-44228-cve-mitigations
%endif


Provides: java = %{epoch}:%{javaver}
Provides: java-%{javaver} = %{epoch}:%{version}-%{release}

%description
Amazon Corretto's packaging of the runtime core elements of the OpenJDK 8 code.

%package devel
Summary: Amazon Corretto development environment
Group:   Development/Tools

# Require base package.
Requires: %{name}%{?_isa} = %{epoch}:%{version}-%{release}

Provides: java-devel = %{epoch}:%{javaver}
Provides: java-%{javaver}-devel = %{epoch}:%{version}-%{release}

%description devel
Amazon Corretto's packaging of the OpenJDK 8 code.

%prep
%setup -q -n src -c

%build
bash ./configure \
%if 0%{?additional_configure_options:1}
    %{additional_configure_options} \
%endif
  --with-zlib="%{zlib_option}" \
  --with-giflib=system \
  --with-update-version="%{java_update}" \
  --with-build-number="%{buildver}" \
  --with-corretto-revision="%{corretto_revision}" \
  --with-milestone=%{?experimental_feature:%experimental_feature}%{!?experimental_feature:fcs} \
  --disable-debug-symbols \
  --disable-zip-debug-info \
  --enable-jfr \
  --with-vendor-name="Amazon.com Inc." \
  --with-vendor-url="https://aws.amazon.com/corretto/" \
  --with-vendor-bug-url="https://github.com/corretto/corretto-8/issues/" \
  --with-vendor-vm-bug-url="https://github.com/corretto/corretto-8/issues/" \
  --with-debug-level=@debug_level@ \
  %{platform_flags}

make images

%install
rm -rf %{buildroot}
install -d -m 755 %{buildroot}%{java_home}
cp -a build/linux-%{_arch}-normal-server-@debug_level@/images/j2sdk-image/* %{buildroot}%{java_home}

%if %{perform_openjfx_overlay}
cp -a -v javafx/* %{buildroot}%{java_home}
%endif

# Make a *relative* symlink pointing to the cacerts file from ca-certificates.
rm -f %{buildroot}%{java_home}/jre/lib/security/cacerts
pushd %{buildroot}%{java_home}/jre/lib/security
  RELATIVE=$(%{abs2rel} %{_sysconfdir}/pki/java %{buildroot}%{java_home}/jre/lib/security)
  ln -sf $RELATIVE/cacerts .
popd

%post devel
if [ $1 -eq 1 ] ; then
  alternatives --install %{_bindir}/javac javac %{java_home}/bin/javac %{alternatives_priority} \
               --slave %{_jvmdir}/java java_sdk %{java_home} \
               --slave %{_bindir}/appletviewer appletviewer %{java_home}/bin/appletviewer \
               --slave %{_bindir}/extcheck extcheck %{java_home}/bin/extcheck \
               --slave %{_bindir}/idlj idlj %{java_home}/bin/idlj \
               --slave %{_bindir}/jar jar %{java_home}/bin/jar \
               --slave %{_bindir}/jarsigner jarsigner %{java_home}/bin/jarsigner \
               --slave %{_bindir}/javadoc javadoc %{java_home}/bin/javadoc \
               --slave %{_bindir}/javah javah %{java_home}/bin/javah \
               --slave %{_bindir}/javap javap %{java_home}/bin/javap \
               --slave %{_bindir}/jcmd jcmd %{java_home}/bin/jcmd \
               --slave %{_bindir}/jconsole jconsole %{java_home}/bin/jconsole \
               --slave %{_bindir}/jdb jdb %{java_home}/bin/jdb \
               --slave %{_bindir}/jdeps jdeps %{java_home}/bin/jdeps \
               --slave %{_bindir}/jhat jhat %{java_home}/bin/jhat \
               --slave %{_bindir}/jinfo jinfo %{java_home}/bin/jinfo \
               --slave %{_bindir}/jmap jmap %{java_home}/bin/jmap \
               --slave %{_bindir}/jps jps %{java_home}/bin/jps \
               --slave %{_bindir}/jrunscript jrunscript %{java_home}/bin/jrunscript \
               --slave %{_bindir}/jsadebugd jsadebugd %{java_home}/bin/jsadebugd \
               --slave %{_bindir}/jstack jstack %{java_home}/bin/jstack \
               --slave %{_bindir}/jstat jstat %{java_home}/bin/jstat \
               --slave %{_bindir}/jstatd jstatd %{java_home}/bin/jstatd \
               --slave %{_bindir}/native2ascii native2ascii %{java_home}/bin/native2ascii \
               --slave %{_bindir}/rmic rmic %{java_home}/bin/rmic \
               --slave %{_bindir}/schemagen schemagen %{java_home}/bin/schemagen \
               --slave %{_bindir}/serialver serialver %{java_home}/bin/serialver \
               --slave %{_bindir}/wsgen wsgen %{java_home}/bin/wsgen \
               --slave %{_bindir}/wsimport wsimport %{java_home}/bin/wsimport \
               --slave %{_bindir}/xjc xjc %{java_home}/bin/xjc

  alternatives --install %{_jvmdir}/java-%{java_major_version} java_sdk_%{java_major_version} %{java_home} %{alternatives_priority} \
               --slave %{_jvmdir}/java-%{java_major_version}-openjdk java_sdk_%{java_major_version}_openjdk %{java_home}
fi

%postun devel
if [ $1 -eq 0 ] ; then
  alternatives --remove javac %{java_home}/bin/javac
  alternatives --remove java_sdk_%{java_major_version} %{java_home}
fi

%post
if [ $1 -eq 1 ] ; then
  alternatives --install %{_bindir}/java java %{java_home}/jre/bin/java %{alternatives_priority} \
               --slave %{_jvmdir}/%{name} %{name} %{java_home} \
               --slave %{_jvmdir}/jre jre %{java_home}/jre \
               --slave %{_jvmdir}/jre-openjdk jre_openjdk %{java_home}/jre \
               --slave %{_bindir}/jjs jjs %{java_home}/jre/bin/jjs \
               --slave %{_bindir}/keytool keytool %{java_home}/jre/bin/keytool \
               --slave %{_bindir}/orbd orbd %{java_home}/jre/bin/orbd \
               --slave %{_bindir}/pack200 pack200 %{java_home}/jre/bin/pack200 \
               --slave %{_bindir}/policytool policytool %{java_home}/jre/bin/policytool \
               --slave %{_bindir}/rmid rmid %{java_home}/jre/bin/rmid \
               --slave %{_bindir}/rmiregistry rmiregistry %{java_home}/jre/bin/rmiregistry \
               --slave %{_bindir}/servertool servertool %{java_home}/jre/bin/servertool \
               --slave %{_bindir}/tnameserv tnameserv %{java_home}/jre/bin/tnameserv \
               --slave %{_bindir}/unpack200 unpack200 %{java_home}/jre/bin/unpack200

  # Need to setup the jre alternatives separately when there is a higher priority jre java these don't get created above.
  alternatives --install %{_jvmdir}/jre-%{java_major_version} jre_%{java_major_version} %{java_home}/jre %{alternatives_priority} \
               --slave %{_jvmdir}/jre-%{java_major_version}-openjdk jre_%{java_major_version}_openjdk %{java_home}/jre

fi

%postun
if [ $1 -eq 0 ] ; then
  alternatives --remove java %{java_home}/jre/bin/java
  alternatives --remove jre_%{java_major_version} %{java_home}/jre
fi

%files devel
%{java_home}/ASSEMBLY_EXCEPTION
%{java_home}/LICENSE
%{java_home}/THIRD_PARTY_README
%{java_home}/bin
%{java_home}/demo
%{java_home}/include
%{java_home}/lib
%{java_home}/man
%{java_home}/release
%{java_home}/sample
%{java_home}/src.zip
%if %{perform_openjfx_overlay}
%{java_home}/javafx-src.zip
%endif

%files
%config(noreplace) %{java_home}/jre/lib/calendars.properties
%config(noreplace) %{java_home}/jre/lib/logging.properties
%config(noreplace) %{java_home}/jre/lib/net.properties
%config(noreplace) %{java_home}/jre/lib/security/policy/limited/local_policy.jar
%config(noreplace) %{java_home}/jre/lib/security/policy/limited/US_export_policy.jar
%config(noreplace) %{java_home}/jre/lib/security/policy/unlimited/local_policy.jar
%config(noreplace) %{java_home}/jre/lib/security/policy/unlimited/US_export_policy.jar
%config(noreplace) %{java_home}/jre/lib/security/blacklisted.certs
%config(noreplace) %{java_home}/jre/lib/security/java.policy
%config(noreplace) %{java_home}/jre/lib/security/java.security
%dir %{java_home}
%dir %{java_home}/jre
%{java_home}/jre/ASSEMBLY_EXCEPTION
%{java_home}/jre/LICENSE
%{java_home}/jre/THIRD_PARTY_README
%{java_home}/jre/bin
%{java_home}/jre/lib


%changelog
* Thu Aug 18 2022 Dan Lutker <lutkerd@amazon.com>
- Add ability to set debug_level

* Thu Jun 30 2022 Dan Lutker <lutkerd@amazon.com>
- Fixup java/jre symlinks

* Wed Jun 9 2022 Dan Lutker <lutkerd@amazon.com>
- Update libs for provides/requires exclusion

* Tue Feb 8 2022 Dan Lutker <lutkerd@amazon.com>
- Fixes for AL2022

* Thu Dec 16 2021 Clive Verghese <verghese@amazon.com>
- Add requires for log4j CVE-2021-44228 static mitigation