# -*- 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           gpg_verify 1.0
PortGroup           compilers 1.0

compilers.choose    fc f77 f90
compilers.setup     require_fortran -g95
compiler.blacklist-append {clang < 900}

github.setup        JuliaLang julia 1.12.0 v
revision            0
categories-append   lang math science
maintainers         {ieee.org:s.t.smith @essandess} \
                    {b-data.ch:olivier.benz @benz0li} \
                    openmaintainer
platforms           {darwin >= 14}
license             MIT
homepage            http://julialang.org

description         The Julia Language: A fresh approach to technical computing.
long_description    Julia is a high-level, high-performance dynamic programming \
                    language for technical computing, with syntax that is familiar \
                    to users of other technical computing environments.

github.tarball_from releases

distfiles           ${name}-${version}-full${extract.suffix}

checksums           ${name}-${version}-full${extract.suffix} \
                    rmd160  48a1370df8db2f35a6c072531ef7c6c318ec013e \
                    sha256  2dba42b3f143564f96c60948d1100db0902aa3cb98ebf89c156b4091f05d9b89 \
                    size    575249348

extract.only        ${distfiles}

gpg_verify.use_gpg_verification \
                    yes

if {[option gpg_verify.use_gpg_verification]} {
    distfiles-append \
                    ${name}-${version}-full${extract.suffix}.asc
    checksums-append \
                    ${name}-${version}-full${extract.suffix}.asc \
                    size    866

    post-checksum {
        gpg_verify.verify_gpg_signature \
            ${filespath}/juliareleases.asc \
            ${distpath}/${name}-${version}-full${extract.suffix}.asc \
            ${distpath}/${name}-${version}-full${extract.suffix}
    }
}

# julia build is `make && make install`
# see https://github.com/JuliaLang/julia/blob/master/Makefile

use_configure       no

build.target        {}

pre-build {
    # create a `gfortran` symbolic link
    xinstall -m 0755 -d ${workpath}/bin
    ln -sf ${configure.fc} ${workpath}/bin/gfortran

    build.env-append \
        PATH=$env(PATH):${workpath}/bin \
        PREFIX=${prefix} \
        FC=${configure.fc}
}

pre-destroot {
    destroot.env-append \
        PATH=$env(PATH):${workpath}/bin \
        PREFIX=${prefix} \
        FC=${configure.fc}
}

post-destroot {
    # julia doesn't do destroot quite right
    set short_version [join [lrange [split ${version} .] 0 1] .]
    set major_version [lindex [split ${version} .] 0]
    set dpw ${destroot}${worksrcpath}
    delete ${destroot}${prefix}/bin
    move ${dpw}/julia-${version}/bin ${destroot}${prefix}/bin
    move ${dpw}/julia-${version}/etc/julia ${destroot}${prefix}/etc
    move ${dpw}/julia-${version}/include/julia ${destroot}${prefix}/include
    move ${dpw}/julia-${version}/lib/julia ${destroot}${prefix}/lib
    move ${dpw}/julia-${version}/lib/libjulia.${short_version}.dylib ${destroot}${prefix}/lib
    move ${dpw}/julia-${version}/lib/libjulia.dylib ${destroot}${prefix}/lib
    move ${dpw}/julia-${version}/lib/libjulia.${version}.dylib ${destroot}${prefix}/lib
    # see https://github.com/JuliaLang/julia/pull/48931
    move ${dpw}/julia-${version}/libexec/julia ${destroot}${prefix}/libexec/julia
    move ${dpw}/julia-${version}/share/julia ${destroot}${prefix}/share
    delete ${destroot}${prefix}/share/man
    move ${dpw}/julia-${version}/share/man ${destroot}${prefix}/share

    delete ${destroot}${prefix}/var

    system -W ${destroot}${prefix}/bin "install_name_tool -add_rpath ${prefix}/lib julia"
    system -W ${destroot}${prefix}/lib/julia "install_name_tool -change ${worksrcpath}/usr/lib/libccalllazyfoo.dylib @rpath/libccalllazyfoo.dylib libccalllazybar.dylib"
    if {${os.arch} eq "arm"} {
        # manually fix code signature on arm
        system -W ${destroot}${prefix} "/usr/bin/codesign --sign - --force lib/libjulia.${version}.dylib"
    }
}

pre-test {
    test.env-append \
        PATH=$env(PATH):${workpath}/bin \
        PREFIX=${prefix} \
        FC=${configure.fc}
}

test.run            yes

if {${os.arch} eq "arm"} {
    notes-append \
"
On Apple Silicon machines, access the macOS Accelerate\
and Metal frameworks using, respectively:\

    using AppleAccelerate
    using Metal
"
}

if {${os.platform} eq "darwin" && ${os.major} < 14} {
    known_fail      yes
    pre-fetch {
        ui_error "${name} @${version} requires OS X 10.10 or later"
        return -code error "incompatible OS X version"
    }
}

github.livecheck.regex  {([^"-]+)}
