# -*- 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           cmake 1.1

name                ispc
categories          lang parallel
maintainers         nomaintainer

# Upstream builds its x86_64 macOS binaries for 10.13 (superbuild/CMakeLists.txt,
# MACOS_VERSION_MIN); ispc uses std::variant, which needs the 10.13 libc++ runtime.
platforms           {darwin >= 17}

# ispc 1.31.0 requires LLVM >= 20. llvm-21 is the newest MacPorts LLVM whose
# clang subport (its static libraries are linked into ispc) supports darwin 17.
set llvm_version    21

# For build instructions see https://github.com/ispc/ispc/wiki/Building-ispc

if {${subport} eq ${name}} {

    github.setup        ispc ispc 1.31.0 v
    github.tarball_from archive
    revision            0

    checksums           rmd160  c884659fc85bc3c0bdbb2355e3c16668fe52db07 \
                        sha256  671c79bdff2d97aead3897da1a6b67a163af8c4e329350d07cf3e63d592955ff \
                        size    20164383

    license             BSD

    description         Intel SPMD program compiler
    long_description    ${name} is a compiler for a variant of the C programming language, \
                        with extensions for single program, multiple data programming.

    set py_ver          3.14
    set py_ver_nodot    [string map {. {}} ${py_ver}]
    set llvm_prefix     ${prefix}/libexec/llvm-${llvm_version}

    depends_build-append \
                        port:bison \
                        port:flex \
                        port:clang-${llvm_version} \
                        port:python${py_ver_nodot}

    depends_lib-append  port:llvm-${llvm_version} \
                        port:ncurses \
                        port:zlib \
                        port:zstd

    post-patch {
        # Fix python shebang to use explicit path to MP python 3.x
        foreach f [exec find ${worksrcpath}/ -name "*.py"] {
            reinplace -q "s|/usr/bin/env python3|${prefix}/bin/python${py_ver}|" ${f}
        }
    }

    compiler.c_standard   2011
    compiler.cxx_standard 2017
    compiler.blacklist  *gcc* {clang < 1204} macports-clang-3.*

    use_xcode           yes

    cmake.build_type    Release
    cmake.install_rpath-append \
                        ${llvm_prefix}/lib

    configure.args-append \
                        -DBISON_EXECUTABLE=${prefix}/bin/bison \
                        -DFLEX_EXECUTABLE=${prefix}/bin/flex \
                        -DLLVM_CONFIG_EXECUTABLE=${llvm_prefix}/bin/llvm-config \
                        -DLLVM_DIR=${llvm_prefix}/lib/cmake/llvm \
                        -DISPC_INCLUDE_EXAMPLES=OFF \
                        -DISPC_INCLUDE_TESTS=OFF \
                        -DISPC_INCLUDE_UTILS=OFF \
                        -DXE_ENABLED=OFF

    # Upstream enables x86 targets only on x86 hosts by default; its macOS
    # release builds enable both x86 and ARM (superbuild/CMakeLists.txt).
    configure.args-append \
                        -DARM_ENABLED=ON \
                        -DX86_ENABLED=ON

    test.run            yes
    test.dir            ${worksrcpath}
    test.cmd            ./scripts/run_tests.py
    test.args           --verbose --non-interactive --jobs=${build.jobs} \
                        --compiler=[file tail ${configure.cxx}]
    test.env-append     ISPC_HOME=${build.dir}/bin
    test.target         ""
    depends_test-append port:python${py_ver_nodot}

}

# remove after September 25, 2027
subport ispc-clang {
    # Formerly a private LLVM/Clang build with assertions for ispc.
    # Modern ispc builds against the stock MacPorts LLVM ports instead.
    PortGroup           obsolete 1.0

    version             14.0.6
    revision            2
    replaced_by         llvm-${llvm_version}
    license             NCSA
    description         Obsolete Clang build formerly used only by ispc
    long_description    {*}${description}. Use llvm-${llvm_version} and \
                        clang-${llvm_version} instead.
}
