# -*- 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           github 1.0
PortGroup           java 1.0
PortGroup           app 1.0

github.setup        NationalSecurityAgency ghidra 12.2 Ghidra_ _build
github.tarball_from archive
revision            0
checksums           rmd160  8bdf2591795f490fba6a285ca8e2bdcd2129e662 \
                    sha256  ade4fd6e277a10c82db4e75ff5acaeedb3113ef0c7d126ea2c8b38c37128f3c3 \
                    size    80858383

categories          devel java
license             Apache-2
maintainers         {1e0.co.uk:dev @hexagonal-sun} {crowell.biz:jeff @crowell} openmaintainer
description         A software reverse engineering (SRE) suite of tools developed by NSA's \
                    Research Directorate in support of the Cybersecurity mission
long_description    {*}${description}.
homepage            https://ghidra-sre.org
supported_archs     arm64 x86_64

java.version        25
java.fallback       openjdk25

universal_variant   no
depends_build-append    port:gradle port:py314-pip port:python314
depends_run-append      port:python314

worksrcdir          ${name}-${github.tag_prefix}${version}${github.tag_suffix}

set javadest        ${prefix}/share/java/${name}-${version}
# Gradle finds its native toolchain on PATH, where gcc_select and
# clang_select can put a MacPorts compiler linking libgcc's libstdc++
pre-configure {
    xinstall -m 755 -d ${workpath}/bin
    ln -s ${configure.cc}  ${workpath}/bin/clang
    ln -s ${configure.cxx} ${workpath}/bin/clang++
    ln -s ${configure.cc}  ${workpath}/bin/gcc
    ln -s ${configure.cxx} ${workpath}/bin/g++
}
configure.env-append    PATH=${workpath}/bin:$env(PATH)
build.env-append        PATH=${workpath}/bin:$env(PATH)

configure.env-append    GRADLE_USER_HOME=${worksrcpath}
configure.pre_args  ""
configure.cmd       gradle -I gradle/support/fetchDependencies.gradle
build.env-append    GRADLE_USER_HOME=${worksrcpath}
build.env-append    _JAVA_OPTIONS=-Duser.home=${worksrcpath}
build.cmd           gradle
build.target        buildGhidra

set ghidra_copy_list \
[list \
    Extensions \
    GPL \
    Ghidra \
    LICENSE \
    docs \
    ghidraRun \
    licenses \
    server \
    support \
]

destroot {
    system "unzip ${worksrcpath}/build/dist/ghidra*.zip -d ${worksrcpath}/ghbuild"
    xinstall -d ${destroot}${javadest}
    foreach item ${ghidra_copy_list} {
        copy ${worksrcpath}/ghbuild/ghidra_${version}_DEV/${item} ${destroot}${javadest}/
    }

    # Remove binaries for other architectures
    if {${os.arch} ne "arm"} {
        delete \
            ${destroot}${javadest}/GPL/DemanglerGnu/os/mac_arm_64 \
            ${destroot}${javadest}/Ghidra/Features/Decompiler/os/mac_arm_64 \
            ${destroot}${javadest}/Ghidra/Features/FileFormats/os/mac_arm_64
    }
    if {${os.arch} ne "i386"} {
        delete \
            ${destroot}${javadest}/GPL/DemanglerGnu/os/mac_x86_64 \
            ${destroot}${javadest}/Ghidra/Features/Decompiler/os/mac_x86_64 \
            ${destroot}${javadest}/Ghidra/Features/FileFormats/os/mac_x86_64
    }
}

# app settings
app.executable      ${javadest}/ghidraRun
app.icon            ${worksrcpath}/Ghidra/RuntimeScripts/support/ghidra.ico
app.retina          yes
