# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4

PortSystem          1.0
PortGroup           deprecated 1.0

deprecated.macports_support no

name                openjdk8
# use format based on macports:
# https://guide.macports.org/chunked/reference.portgroup.html#reference.portgroup.java
# https://github.com/openjdk/jdk8u/tags
# Tags: https://github.com/openjdk/jdk8u/tags
set major 8
set update 372
# Set to the build of the 'jdk8u${update}-b${build}' tag that corresponds to the latest tag with '-ga'
set build 07
version             ${major}u${update}
revision            2
categories          java devel
supported_archs     ppc x86_64 arm64
license             GPL-2+
maintainers         nomaintainer
description         OpenJDK 8
long_description    JDK 8 and JRE 8 builds of OpenJDK, the Open-Source implementation \
                    of the Java Platform, Standard Edition, and related projects.
homepage            https://openjdk.org/
master_sites        https://git.openjdk.org/jdk8u/archive/refs/tags/
distname            jdk${major}u${update}-ga
worksrcdir          jdk8u-${distname}

if {${configure.build_arch} eq "ppc"} {
    # On PowerPC this is a stub, the real JDK
    # is installed by openjdk8-powerpc port.
    # Independent versioning, pegged here.
    version         8u
    revision        0
    epoch           1

    depends_lib     port:${name}-powerpc

    distfiles
    patchfiles
    use_configure   no
    build           {}
    destroot {
        xinstall -d ${destroot}${prefix}/share/doc/${name}
        system "echo $name is a stub port > ${destroot}${prefix}/share/doc/${name}/README"
    }
} else {
    # x86* and arm64

checksums           rmd160  7ffec6da71e24d2913b717b5f31d6be9c2fa7b7e \
                    sha256  3235a744b51896beb1e8b738412982ebc06e2affb9d50ae3371203d9a46504da \
                    size    88002433

patchfiles          0001-8181503-Can-t-compile-hotspot-with-c-11.patch \
                    0002-Support-XCode-3-14.patch \
                    0003-Ignore-__NSAutoreleaseNoPool-errors.patch \
                    0004-8030812-Change-the-solaris-DTrace-implementation-to-.patch \
                    0005-os_perf_bsd-allow-to-compile-with-ZERO-CPU.patch \
                    0006-Disable-C-11-warnings.patch \
                    0007-Added-macosx-bin-zero-jvm.cfg.patch \
                    0008-ExtendedOptionsImpl.c-define-TCP_KEEPINTVL-and-TCP_K.patch \
                    Support-arm64-and-fix-zero-assembler.diff \
                    finite.patch

# NSFullSizeContentViewWindowMask is available since 10.10
if {${os.platform} eq "darwin" && ${os.major} < 14} {
    patchfiles-append \
                    0009-Revert-8211301-macos-support-full-window-content-opt.patch
}

depends_lib         port:freetype \
                    port:libiconv
depends_build       port:autoconf \
                    port:gmake \
                    port:pkgconfig \
                    port:bash

set tpath /Library/Java
use_xcode           yes
use_configure    yes
if {${configure.build_arch} in [list i386 ppc]} {
    set datamodel 32
} else {
    set datamodel 64
}

# Update config.{guess,sub} to recognise arm64
depends_build-append    port:automake
post-patch {
    set automake_dirs [glob -directory ${prefix}/share automake-*]
    set automake_dir [lindex [lsort -command vercmp $automake_dirs] end]
    copy -force ${automake_dir}/config.guess \
        ${worksrcpath}/common/autoconf/build-aux/autoconf-config.guess
    copy -force ${automake_dir}/config.sub \
        ${worksrcpath}/common/autoconf/build-aux/autoconf-config.sub
}

configure.cmd       ${prefix}/bin/bash ./configure
configure.pre_args  --prefix=${tpath}
set bug_url "https://trac.macports.org/newticket?port=${name}"
# default configure args
configure.args      --with-debug-level=release \
                    --with-freetype-include=${prefix}/include/freetype2 \
                    --with-freetype-lib=${prefix}/lib \
                    --with-target-bits=${datamodel} \
                    --with-extra-cflags="${configure.cflags}" \
                    --with-extra-cxxflags="${configure.cxxflags}" \
                    --with-extra-ldflags="${configure.ldflags}" \
                    --with-milestone=fcs \
                    --with-vendor-name="MacPorts" \
                    --with-vendor-url="https://www.macports.org" \
                    --with-vendor-bug-url="${bug_url}" \
                    --with-vendor-vm-bug-url="${bug_url}" \
                    --with-update-version=${update} \
                    --with-conf-name=openjdk8 \
                    --with-native-debug-symbols=none

if {[option configure.ccache]} {
    # replace MacPorts ccache integration into JDK
    configure.ccache        no
    depends_build-append    path:bin/ccache:ccache
    configure.args-append   --enable-ccache \
                            --with-ccache-dir=${ccache_dir}
}

if { [string match *clang* ${configure.compiler}] } {
    configure.args-append \
                    --with-toolchain-type=clang
} elseif { [string match *gcc* ${configure.compiler}] } {
    configure.args-append \
                    --with-toolchain-type=gcc
}

build.pre_args-prepend \
                    COMPILER_WARNINGS_FATAL=false

post-patch {
    # NOTE: JDK had hardcoded MACOSX_VERSION_MIN as 10.9.0 when uses clang toolchain
    # and 10.7.0 for gcc toolchain.
    if {${os.platform} eq "darwin" && ${os.major} < 13} {
        reinplace "s|MACOSX_VERSION_MIN=10.9.0|MACOSX_VERSION_MIN=10.7.0|g" \
            ${worksrcpath}/common/autoconf/flags.m4 \
            ${worksrcpath}/common/autoconf/generated-configure.sh
    }
}

if {${configure.build_arch} in "arm64 x86_64"} {
    if {${os.platform} eq "darwin" && ${os.major} < 11} {
        # NOTE: openjdk8-bootstrap's doesn't work enough to bootstrap openjdk on 10.6
        # It is fails with errors like:
        #   2022-10-01 01:35:43.344 java[12281:1d03] *** NSInvocation: warning: object 0x11478b468 of class 'ThreadUtilities' does not implement methodSignatureForSelector: -- trouble ahead
        #   2022-10-01 01:35:43.345 java[12281:1d03] *** NSInvocation: warning: object 0x11478b468 of class 'ThreadUtilities' does not implement doesNotRecognizeSelector: -- abort
        depends_lib
        depends_build
        known_fail      yes
        pre-fetch {
            ui_error "${name} @${version} requires OS X 10.7 or greater."
            return -code error "incompatible Mac OS X version"
        }
    }

    depends_build-append       port:openjdk8-bootstrap
    configure.args-append      --with-boot-jdk=/Library/Java/JavaVirtualMachines/openjdk8-bootstrap/Contents/Home
}

variant server \
    conflicts core zero zeroshark \
    description {JVM with normal interpreter and a tiered C1/C2 compiler} {
    configure.args-append  --with-jvm-variants=server
}

variant release \
    conflicts debug \
    description {OpenJDK with no debug information, all optimizations and no asserts} {
    configure.args-append  --with-debug-level=release 
}

variant debug \
    conflicts release \
    description {OpenJDK with debug information, all optimizations and all asserts} {
    configure.args-append  --with-debug-level=fastdebug
    configure.args-delete   --with-native-debug-symbols=none
}

variant core \
    conflicts server \
    description {JVM with interpreter only and no compiler} {
    configure.args-append   --with-jvm-variants=core
}

variant zero \
    conflicts core zeroshark server \
    description {JVM with no assembler, no machine code interpreter and no compiler} {
    set archuc [string toupper ${configure.build_arch}]
    depends_lib-append         port:libffi
    configure.args-append  --with-jvm-variants=zero \
                           --with-extra-cflags="${configure.cflags} -I${prefix}/include -arch ${configure.build_arch}" \
                           --with-extra-cxxflags="${configure.cxxflags} -arch ${configure.build_arch}" \
                           --with-extra-ldflags="${configure.ldflags} -I${prefix}/lib -arch ${configure.build_arch}"
    configure.args-delete  --with-extra-cflags="${configure.cflags}" \
                           --with-extra-cxxflags="${configure.cxxflags}" \
                           --with-extra-ldflags="${configure.ldflags}"
}

variant zeroshark \
    conflicts core zero server \
    description {JVM with no assembler, zero interpreter and shark/llvm compiler backend} {
    set archuc [string toupper ${configure.build_arch}]
    depends_lib-append         port:libffi
    configure.args-append  --with-jvm-variants=zeroshark \
                           --with-extra-cflags="${configure.cflags} -I${prefix}/include -arch ${configure.build_arch}" \
                           --with-extra-cxxflags="${configure.cxxflags} -arch ${configure.build_arch}" \
                           --with-extra-ldflags="${configure.ldflags} -I${prefix}/lib -arch ${configure.build_arch}"
    configure.args-delete  --with-extra-cflags="${configure.cflags}" \
                           --with-extra-cxxflags="${configure.cxxflags}" \
                           --with-extra-ldflags="${configure.ldflags}"
}

if {![variant_isset debug] && ![variant_isset release]} {
    default_variants-append +release
}

if {![variant_isset core] && ![variant_isset zeroshark] && ![variant_isset zero] && ![variant_isset server]} {
    if {${os.major} < 14} {
        default_variants-append +zero
    } else {
        if {${configure.build_arch} eq "x86_64"} {
            default_variants-append +server
        } elseif {${configure.build_arch} eq "arm64"} {
            default_variants-append +zero
        }
    }
}

build.type          gnu
build.target        images
use_parallel_build  no
set jdkn jdk1.${major}.0_${update}.jdk
set jren jre1.${major}.0_${update}.jre
set jdk_bundle_dir build/openjdk8/images/j2sdk-bundle/${jdkn}/Contents
set jre_bundle_dir build/openjdk8/images/j2re-bundle/${jren}/Contents
set jdk_path ${tpath}/JavaVirtualMachines/${name}
set jre_path ${tpath}/JavaVirtualMachines/${name}-jre

test.run            yes
test.cmd            ${jdk_bundle_dir}/Home/bin/java
test.target         --version

destroot {
    xinstall -m 755 -d ${destroot}${jdk_path}
    xinstall -m 755 -d ${destroot}${jre_path}
    copy ${worksrcpath}/${jdk_bundle_dir} ${destroot}${jdk_path}
    copy ${worksrcpath}/${jre_bundle_dir} ${destroot}${jre_path}
}
destroot.violate_mtree      yes

post-destroot {
    delete ${worksrcpath}
}

notes "
If you want to make ${name} the default JDK, add this to shell profile:
export JAVA_HOME=${jdk_path}/Contents/Home
If you want to make the JRE installed by the ${name} the default JRE, add this to shell profile:
export JAVA_HOME=${jre_path}/Contents/Home
"

}

livecheck.type      regex
livecheck.url       https://github.com/openjdk/jdk8u/tags
livecheck.regex     jdk(8u\[0-9\]+)-ga
