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

# link legacysupport statically for compilers
legacysupport.use_static                       yes
# limit legacysupport to OSX 10.11 and older (need clock_gettime())
legacysupport.newest_darwin_requires_legacy    15
# Note: getattrlistat() is also needed, which will be provided in a future
# release of legacy-support; see https://trac.macports.org/ticket/60671

name                     ispc
categories               lang parallel
supported_archs          x86_64
maintainers              nomaintainer

set py_ver               3.11
set py_ver_nodot         [string map {. {}} ${py_ver}]

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

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

    github.setup         ispc ispc 1.19.0 v
    # Change github.tarball_from to 'releases' or 'archive' next update
    github.tarball_from  tarball
    revision             0

    checksums            rmd160  90bb1d6aef518ef457114edcd84131a87a37f158 \
                         sha256  c1aeae4bdfb28004a6949394ea1b3daa3fdf12f646e17fcc0614861077dc8b6a \
                         size    19578123

    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. 

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

    depends_lib-append   port:ncurses \
                         port:zlib \
                         port:zstd

    post-patch {
        if {${os.platform} eq "darwin" && ${os.major} >= 18} {
            set sdkroot ${developer_dir}/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk
            reinplace "s|-emit-llvm|-isysroot ${sdkroot} -emit-llvm|" \
                ${worksrcpath}/cmake/GenerateBuiltins.cmake
        }
        # 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}
        }
    }

    # Need to use a recent MacPorts build.
    compiler.cxx_standard  2017
    compiler.blacklist     *gcc* clang {macports-clang-[5-8].0} macports-clang-3.*

    use_xcode              yes
    cmake.out_of_source    yes

    cmake.build_type       Release
    configure.args-append  -DBISON_EXECUTABLE=${prefix}/bin/bison \
                           -DFLEX_EXECUTABLE=${prefix}/bin/flex \
                           -DLLVM_DIR=${prefix}/libexec/ispc-clang/lib/cmake/llvm \
                           -DISPC_INCLUDE_TESTS=OFF

    # Build has problems finding zlib
    configure.ldflags-append  ${prefix}/lib/libz.a
    configure.args-append  -DZLIB_INCLUDE_DIR=${prefix}/include \
                           -DZLIB_LIBRARY=${prefix}/lib/libz.a

    test.run               yes
    test.dir               ${worksrcpath}
    test.cmd               ./run_tests.py
    test.args              --verbose --non-interactive --compiler=[file tail ${configure.cc}]
    test.target            ""
    depends_test-append    port:python${py_ver_nodot}

}

subport ispc-clang {

    # See ispc release notes for LLVM version each release is based on
    # https://github.com/ispc/ispc/releases
    github.setup           llvm llvm-project 14.0.6 llvmorg-
    revision               1
    # Change github.tarball_from to 'releases' or 'archive' next update
    github.tarball_from    tarball

    checksums              rmd160  1a92ac23507901e13fc26aea7dca33b8c6cb2d2c \
                           sha256  8e9e1948974cdcd642793f1fe195369d13950623c47f27d4266396d3576e4f5b \
                           size    158100103

    license                NCSA

    description            Clang build specifically for ispc compiler. 
    long_description       {*}${description} NOT TO BE USED IN GENERAL. This build is specifically tuned \
                           to satisfy the requirements of ispc which uses it as a build dependency only. \
                           Has assertions and dump enabled by default, which is not the case in the \
                           primary MacPorts LLVM/Clang builds.
    
    # Build configuration is largely taken from lang/llvm ports,
    # then simplified and adapted for usage here

    patchfiles             patch-compilerrtdarwinutils-find-macosxsdkversion.diff
    if {${os.platform} eq "darwin" && ${os.major} < 16} {
        # Avoid attempts to use 'clonefile'
        # llvm/lib/Support/Unix/Path.inc:1476:10: error: use of undeclared identifier 'clonefile'
        patchfiles-append  0031-Avoid-Use-Of-Clonefile.patch
    }

    # From https://github.com/ispc/ispc/tree/main/llvm_patches
    patchfiles-append      14_0_15_0_disable-DIArgList-in-SPIR-V.patch \
                           14_0_AVX512VP2INTERSECT.patch \
                           14_0_fp16_converts.partial.fix.patch

    depends_lib-append     port:libedit \
                           port:ncurses \
                           path:lib/libxar.dylib:xar \
                           port:z3 \
                           port:zlib \
                           port:zstd \
                           port:libxml2 \
                           port:libomp \
                           bin:perl:perl5 \
                           port:ld64
    depends_build-append   port:pkgconfig \
                           port:cctools \
                           port:python${py_ver_nodot}

    compiler.cxx_standard  2014
    compiler.blacklist     *gcc* {clang < 1204} macports-clang-3.* {macports-clang-[4-6].0} {macports-clang-1[0-2]}
    compiler.fallback      clang macports-clang-13 macports-clang-14

    cmake.install_prefix   ${prefix}/libexec/${subport}
    cmake.build_type       Release
    
    configure.post_args    ../${worksrcdir}/llvm
    default configure.dir  {${workpath}/build}
    default build.dir      {${workpath}/build}

    configure.args-delete  -DCMAKE_INSTALL_NAME_DIR=${cmake.install_prefix}/lib \
                           -DCMAKE_INSTALL_RPATH=${cmake.install_prefix}/lib

    configure.args-replace -DCMAKE_SYSTEM_PREFIX_PATH="${prefix}\;/usr" \
                           -DCMAKE_SYSTEM_PREFIX_PATH="${cmake.install_prefix}\;${prefix}\;/usr"

    configure.args-append  -DLLVM_ENABLE_PROJECTS="clang" \
                           -DLLVM_TARGETS_TO_BUILD="AArch64\;ARM\;X86" \
                           -DLLVM_LINK_LLVM_DYLIB=ON \
                           -DLLVM_ENABLE_ASSERTIONS=ON \
                           -DLLVM_ENABLE_DUMP=ON \
                           -DLLVM_ENABLE_RTTI=ON \
                           -DLLVM_INCLUDE_TESTS=OFF \
                           -DLLVM_INCLUDE_EXAMPLES=OFF \
                           -DLLVM_ENABLE_FFI=OFF \
                           -DLLVM_BINDINGS_LIST=none \
                           -DCMAKE_LINKER=${prefix}/bin/ld \
                           -DCLANG_INCLUDE_TESTS=OFF \
                           -DCLANG_ENABLE_STATIC_ANALYZER=OFF \
                           -DCLANG_ENABLE_ARCMT=OFF \
                           -DDARWIN_PREFER_PUBLIC_SDK=ON \
                           -DLLVM_BUILD_RUNTIME=ON \
                           -DLIBCXX_ENABLE_SHARED=OFF \
                           -DLIBCXX_INSTALL_LIBRARY=OFF \
                           -DCOMPILER_RT_BUILD_SANITIZERS=OFF \
                           -DCOMPILER_RT_ENABLE_IOS=OFF \
                           -DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD=WebAssembly \
                           -DPYTHON_EXECUTABLE=${prefix}/bin/python${py_ver}

    # Version determined by ispc release
    livecheck.type         none
}
