# -*- 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.eol_version yes

set feature 11
name             openjdk${feature}-graalvm
categories       java devel
maintainers      nomaintainer
platforms        {darwin any}
# This port uses prebuilt binaries; 'NoMirror' makes sure MacPorts doesn't mirror/distribute these third-party binaries
license          GPL-2 NoMirror
# This port uses prebuilt binaries for a particular architecture; they are not universal binaries
universal_variant no

# https://github.com/graalvm/graalvm-ce-builds/releases
supported_archs  x86_64 arm64

if {${configure.build_arch} eq "arm64"} {
    # There is no macOS aarch64 build for 22.3.2+
    version 22.3.1
} else {
    version 22.3.3
}

revision     2

master_sites https://github.com/graalvm/graalvm-ce-builds/releases/download/vm-${version}/

homepage     https://www.graalvm.org

livecheck.type  none

use_configure    no
build {}

set jvms /Library/Java/JavaVirtualMachines
set jdk ${jvms}/openjdk${feature}-graalvm

if {${subport} eq ${name}} {
    description  GraalVM Community Edition based on OpenJDK ${feature}
    long_description GraalVM is a universal virtual machine for running applications written in JavaScript, Python, Ruby, R,\
                     JVM-based languages like Java, Scala, Groovy, Kotlin, Clojure, and LLVM-based languages such as C and C++.\
                     Version ${feature} of GraalVM Community Edition is no longer getting updates, so consider upgrading to a maintained version.

    if {${configure.build_arch} eq "x86_64"} {
        set arch_classifier amd64
        checksums    rmd160  e596dc7f73ca0f602ae60ca5c1bc4247373ec158 \
                     sha256  cab6a1436626adc28ec0f72791772315678e7c758e2fbae2cb6758a38f27c56a \
                     size    254523003
    } elseif {${configure.build_arch} eq "arm64"} {
        set arch_classifier aarch64
        checksums    rmd160  3421d1754a491b0cfa44d09df798ff5053fcefc8 \
                     sha256  c59f32289d92671bea46a34f4227fef484a4aa9eeece159e59a486205aaa6c31 \
                     size    249144742
    } else {
        set arch_classifier unsupported_arch
    }

    distname     graalvm-ce-java${feature}-darwin-${arch_classifier}-${version}

    worksrcdir   graalvm-ce-java${feature}-${version}

    variant Applets \
        description { Advertise the JVM capability "Applets".} {}

    variant BundledApp \
        description { Advertise the JVM capability "BundledApp". This is required by some java-based app bundles to recognize and use the JVM.} {}

    variant JNI \
        description { Advertise the JVM capability "JNI". This is required by some java-based app bundles to recognize and use the JVM.} {}

    variant WebStart \
        description { Advertise the JVM capability "WebStart".} {}

    patch {
        foreach var { Applets BundledApp JNI WebStart } {
            if {[variant_isset ${var}]} {
                reinplace -E "s|^(\[\[:space:\]\]*<string>)CommandLine(</string>)|\\1${var}\\2\\\n\\1CommandLine\\2|" ${worksrcpath}/Contents/Info.plist
            }
        }
    }

    test.run    yes
    test.cmd    Contents/Home/bin/java
    test.target
    test.args   -version

    # macOS Java tools expect to find Java virtual machines under /Library/Java/JavaVirtualMachines, which is not under ${prefix}.
    destroot.violate_mtree yes

    pre-install {
        # Clean up previous installation, because some old subport files may have been left behind (https://trac.macports.org/ticket/67935)
        # Don't remove before 2024-08-20
        delete ${jdk}
    }

    destroot {
        xinstall -m 755 -d ${destroot}${prefix}${jdk}
        copy ${worksrcpath}/Contents ${destroot}${prefix}${jdk}

        # macOS Java tools expect to find Java virtual machines under /Library/Java/JavaVirtualMachines, so let's create a symlink there
        xinstall -m 755 -d ${destroot}${jvms}
        ln -s ${prefix}${jdk} ${destroot}${jdk}
    }

    notes "
    If you have more than one JDK installed you can make ${name} the default\
    by adding the following line to your shell profile:

        export JAVA_HOME=${jdk}/Contents/Home
    "
}

subport ${name}-native-image {
    depends_lib        port:${name}
    description        Native Image component for GraalVM
    long_description   ${description}

    if {${configure.build_arch} eq "x86_64"} {
        set jar_file native-image-installable-svm-java${feature}-darwin-amd64-${version}.jar
        distfiles    ${jar_file}
        checksums    rmd160  373bfe27fe1acd74596411b52d4506cdbd5260d0 \
                     sha256  56ddaf1f1d7e69efb88d8e391a4071f3a237723bdcee67794be1c56a03f56536 \
                     size    28688654
    } elseif {${configure.build_arch} eq "arm64"} {
        set jar_file native-image-installable-svm-java${feature}-darwin-aarch64-${version}.jar
        distfiles    ${jar_file}
        checksums    rmd160  65a84d8dd894be0830673ffdf7e77a0ca19cf1ac \
                     sha256  df923df186826dcb7cb0eac0f5e93c3e215584f0377bad0c76310e09d7dc58cd \
                     size    28504616
    }

    set java_home ${prefix}${jdk}/Contents/Home

    extract {}

    destroot {
        xinstall -d -m 0755 ${destroot}${prefix}/share/java/${subport}
        file copy ${distpath}/${jar_file} ${destroot}${prefix}/share/java/${subport}
    }

    post-activate {
        # Graal Updater doesn't signal errors if the component is already installed.
        # Unfortunately, we require root privileges to invoke Graal Updater.
        system "sudo ${java_home}/bin/gu -L install ${prefix}/share/java/${subport}/${jar_file}"
    }

    post-deactivate {
        # This helps prevent breakage if the user removed native-image themselves
        # and wants to deactivate or uninstall this port.
        if {[regexp {(?i)native-image} [exec ${java_home}/bin/gu list] match]} {
            system "sudo ${java_home}/bin/gu remove native-image"
        }
    }

    notes "The Native Image component of GraalVM has been installed for you"
}
