# -*- 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           conflicts_build 1.0

name                openjdk8-powerpc
set real_name       openjdk8
conflicts           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          432
# Set to the build of the 'jdk8u${update}-b${build}' tag that corresponds to the latest tag with '-ga'
set build           06
version             ${major}u${update}
revision            0
categories          java lang devel
supported_archs     ppc ppc64
platforms           {darwin < 11}
license             GPL-2+
maintainers         {@barracuda156 gmail.com:vital.had}
description         OpenJDK 8 port for macOS PowerPC
long_description    JDK 8 and JRE 8 builds of OpenJDK, the Open-Source implementation \
                    of the Java Platform, Standard Edition, and related projects. \
                    This is an experimental port for PowerPC systems.
homepage            https://openjdk.org/projects/jdk8u/
master_sites        https://openjdk-sources.osci.io/openjdk8/
use_xz              yes
distname            openjdk${version}-b${build}
worksrcdir          jdk${version}-b${build}

checksums           rmd160  54c21a006f650d95b6fecdfd41fc0600a8f31bda \
                    sha256  472251057179a56789fa0f54ea8ed4e0bc257832e6d4425a5711e7400e6e394c \
                    size    60079816

# Generic patches:
patchfiles          0001-Fix-timebomb-bug-in-GenerateCurrencyData.patch \
                    0002-Ignore-__NSAutoreleaseNoPool-errors.patch \
                    0003-os_perf_bsd-allow-to-compile-with-ZERO-CPU.patch \
                    0004-Added-macosx-bin-zero-jvm.cfg.patch \
                    0005-ExtendedOptionsImpl.c-define-TCP_KEEPINTVL-and-TCP_K.patch \
                    0006-Revert-8211301-macos-support-full-window-content-opt.patch \
                    0007-Provide-definition-of-__private_extern__-where-undef.patch

# If we build with Xcode gcc, we do not need any C11.
if {![string match gcc-4.* ${configure.compiler}]} {
    patchfiles-append \
                    0008-8181503-Can-t-compile-hotspot-with-c-11.patch \
                    0009-Disable-C-11-warnings.patch
}

# PowerPC-specific patches:
platform powerpc {
    patchfiles-append \
                    1001-os_bsd_zero.cpp-implement-sync_val_compare_and_swap_.patch \
                    1002-CoreLibraries.gmk-do-not-hardcode-arch.patch \
                    1003-java_md_macosx-fix-broken-ObjC-code.patch \
                    1004-os_bsd-add-ppc64-case.patch \
                    1005-jniTypes_ppc-downgrade-error-to-warning.patch \
                    1006-os_bsd.cpp-fix-vm_statistics.patch \
                    1007-Awt2dLibraries-add-fnested-functions.patch \
                    1008-java_md_macosx.c-define-preferredJVM.patch \
                    1009-os_bsd.cpp-allow-headless-on-Apple.patch \
                    1010-Makefile-fix-LP64-define.patch \
                    1011-No-dtrace-for-now.patch \
                    1012-toolchain.m4-hack-around-a-broken-Xcode-version-dete.patch \
                    1013-os_bsd.cpp-fix-running-VM.patch \
                    1014-portconfig-try-fix-broken-pipe.patch \
                    1015-Some-hacks-to-fix-the-build-for-powerpc.patch
}

# Active libnet breaks linking of libnio which should link
# to JDK’s own internal libnet.
# libjpeg-turbo: https://trac.macports.org/ticket/67123
conflicts_build     libjpeg-turbo libnet

# If you want to use an alternative JDK for bootstrap,
# change these values accordingly:
depends_build       port:openjdk7-bootstrap
configure.args      --with-boot-jdk=/Library/Java/JavaVirtualMachines/openjdk7-bootstrap

depends_build-append \
                    port:autoconf \
                    port:automake \
                    port:bash \
                    port:gmake \
                    path:bin/pkg-config:pkgconfig

depends_lib-append  port:fontconfig \
                    port:freetype \
                    port:libffi \
                    port:libiconv

set tpath           ${prefix}/Library/Java

use_xcode           yes

if {${configure.build_arch} eq "ppc64"} {
    set datamodel   64
} else {
    set datamodel   32
}

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

    # Fix targets:
    reinplace "s|MACOSX_VERSION_MIN=10.7.0|MACOSX_VERSION_MIN=${macosx_deployment_target}.0|g" \
            ${worksrcpath}/common/autoconf/flags.m4 \
            ${worksrcpath}/common/autoconf/generated-configure.sh
    reinplace "s|MACOSX_VERSION_MIN=10.9.0|MACOSX_VERSION_MIN=${macosx_deployment_target}.0|g" \
            ${worksrcpath}/common/autoconf/flags.m4 \
            ${worksrcpath}/common/autoconf/generated-configure.sh
    reinplace "s|REQUIRED_OS_VERSION=11.2|REQUIRED_OS_VERSION=${macosx_deployment_target}|g" \
            ${worksrcpath}/common/autoconf/platform.m4
    reinplace "s|WARNINGS_ARE_ERRORS = -Werror|WARNING_FLAGS =|g" hotspot/make/bsd/makefiles/gcc.make
    reinplace "s|WARNING_FLAGS = -Wpointer-arith -Wsign-compare -Wundef -Wunused-function -Wformat=2|WARNING_FLAGS = |g" \
            hotspot/make/bsd/makefiles/gcc.make
    reinplace "s|@ZERO_ARCHDEF@|PPC32|g" hotspot/make/bsd/platform_zero.in
    reinplace "s|@ZERO_LIBARCH@|ppc|g" hotspot/make/bsd/platform_zero.in
}

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-append \
                    --with-conf-name=${real_name} \
                    --with-debug-level=release \
                    --with-extra-cflags="${configure.cflags}" \
                    --with-extra-cxxflags="${configure.cxxflags}" \
                    --with-extra-ldflags="${configure.ldflags}" \
                    --with-freetype-include=${prefix}/include/freetype2 \
                    --with-freetype-lib=${prefix}/lib \
                    --with-milestone=fcs \
                    --with-native-debug-symbols=none \
                    --with-target-bits=${datamodel} \
                    --with-update-version=${update} \
                    --with-vendor-bug-url="${bug_url}" \
                    --with-vendor-name="MacPorts" \
                    --with-vendor-url="https://www.macports.org" \
                    --with-vendor-vm-bug-url="${bug_url}"

# See notes below.
configure.args-append \
                    --with-num-cores=${buildmakejobs}

# https://chromium.googlesource.com/chromium/src/+/52.0.2743.117/docs/mac_build_instructions.md
# unable to execute command: posix_spawn failed: Resource temporarily unavailable
# https://github.com/nilsvanvelzen/mac_ppc_openjdk8u60/issues/2
pre-fetch {
    ui_msg "Consider running \`sudo sysctl -w kern.maxproc=2500\` and \`sudo sysctl -w kern.maxprocperuid=2500\` before building"
}

# https://github.com/nilsvanvelzen/mac_ppc_openjdk8u60/commit/ea85e37ea7d7299007697afc3ca9ef2fd1fde2a9
# This error may occur, and there is no perfect solution. Requiring to build on a single core will
# dramatically increase build time, which is already long. A confirmed-to-work hack is to switch
# to a manual build in a case of a linkage failure: cd to extracted sources, sudo gmake + args used by MacPorts.
# Once the build completes, you can resume a standard MacPorts installation. There is no need to reconfigure,
# just run `sudo port build` or `sudo port install`.
pre-build {
    ui_msg "Linking may fail when building in parallel. In such a case use --with-num-cores=1"
}

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
}

if {[string match macports-gcc* ${configure.compiler}]} {
    # Not needed for Xcode gcc. gcc14 converted
    # some warnings into errors by default, turn those off:
    configure.cflags-append \
                    -Wno-implicit-function-declaration \
                    -Wno-incompatible-pointer-types \
                    -Wno-int-conversion
}

configure.post_args --disable-headful

build.pre_args-prepend \
                    COMPILER_WARNINGS_FATAL=false

build.cmd           ${prefix}/bin/gmake

# Presently only zero is used (but keep these).

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} {
    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} {
    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
}

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_path ${tpath}/JavaVirtualMachines/${real_name}
set jre_path ${tpath}/JavaVirtualMachines/${real_name}-jre
set jvms /Library/Java/JavaVirtualMachines

if {[variant_isset zero]} {
    # We do not build macOS-style bundles here, so paths differ:
    set jdk_bundle_dir build/openjdk8/images/j2sdk-image
    set jre_bundle_dir build/openjdk8/images/j2re-image

    # But we want to install in a macOS-style:
    destroot {
        xinstall -m 755 -d ${destroot}${jdk_path}/Contents
        xinstall -m 755 -d ${destroot}${jre_path}/Contents
        xinstall -m 755 -d ${destroot}${jvms}
        # This renames j2*-image dirs to respective Homes:
        move ${worksrcpath}/${jdk_bundle_dir} ${destroot}${jdk_path}/Contents/Home
        move ${worksrcpath}/${jre_bundle_dir} ${destroot}${jre_path}/Contents/Home

        # macOS Java tools expect to find Java virtual machines under /Library/Java/JavaVirtualMachines.
        ln -s ${jdk_path} ${destroot}${jvms}/${real_name}
        # We need these:
        ln -s ${prefix}/lib/libfontconfig.1.dylib ${destroot}${jdk_path}/Contents/Home/jre/lib/libfontconfig.dylib
        ln -s ${prefix}/lib/libfontconfig.1.dylib ${destroot}${jre_path}/Contents/Home/lib/libfontconfig.dylib
    }
    # Binaries are gone from worksrcpath, so run from destroot:
    test.cmd        ${destroot}${jdk_path}/Contents/Home/bin/java

} else {
    set jdk_bundle_dir build/openjdk8/images/j2sdk-bundle/${jdkn}/Contents
    set jre_bundle_dir build/openjdk8/images/j2re-bundle/${jren}/Contents

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

        # macOS Java tools expect to find Java virtual machines under /Library/Java/JavaVirtualMachines.
        ln -s ${jdk_path} ${destroot}${jvms}/${real_name}
    }

    test.cmd        ${worksrcpath}/${jdk_bundle_dir}/Home/bin/java
}

destroot.violate_mtree  yes

test.run                yes
test.target             -version

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 ${real_name} the default JRE, add this to shell profile:
export JAVA_HOME=${jre_path}/Contents/Home
"

livecheck.type      regex
livecheck.url       ${master_sites}
livecheck.regex     openjdk(8u\\d+)-ga