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

github.setup            pocl pocl 7.1 v
github.tarball_from     archive
revision                0

categories              lang devel
license                 MIT
maintainers             {i0ntempest @i0ntempest} openmaintainer

description             Portable OpenCL implementation
long_description        PoCL is a portable, LLVM-based implementation of the OpenCL standard.

checksums               rmd160  1043865b1fac6e6b71d42378cf1e764f72250ed8 \
                        sha256  1110057cb0736c74819ad65238655a03f7b93403a0ca60cdd8849082f515ca25 \
                        size    3889905

patchfiles-append       patch-private-dylib-install-names.diff

# PoCL 7.1 builds against LLVM 17-22, but requires the LLVM 21 header layout.
set llvm_version        21
set python_version      314
set python_branch       [string index ${python_version} 0].[string range ${python_version} 1 end]
set python_bin          ${prefix}/bin/python${python_branch}

compiler.cxx_standard   2017
cmake.build_type        Release
cmake.install_rpath-append \
                        @loader_path \
                        @loader_path/pocl

depends_build-append    port:pkgconfig
depends_lib-append      port:clang-${llvm_version} \
                        port:llvm-${llvm_version} \
                        port:libjpeg-turbo
depends_run-append      port:opencl-headers

# Do not install bundled OpenCL headers, opencl-headers port is better
configure.args-append   -DWITH_LLVM_CONFIG=${prefix}/libexec/llvm-${llvm_version}/bin/llvm-config \
                        -DEXTRA_HOST_CLANG_FLAGS=-isysroot${configure.sdkroot} \
                        -DENABLE_EXAMPLES=OFF \
                        -DENABLE_HWLOC=OFF \
                        -DENABLE_ICD=OFF \
                        -DENABLE_SPIRV=OFF \
                        -DENABLE_TESTS=OFF \
                        -DENABLE_VULKAN=OFF \
                        -DINSTALL_OPENCL_HEADERS=OFF

variant native description {Optimize kernel libraries for the build host CPU} {
    configure.args-append \
                        -DKERNELLIB_HOST_CPU_VARIANTS=native
}

if {![variant_isset native]} {
    if {${configure.build_arch} in {i386 x86_64}} {
        # Only x86 supports "distro" which allows runtime detection of SSE/AVX
        configure.args-append \
                        -DKERNELLIB_HOST_CPU_VARIANTS=distro
    } elseif {${configure.build_arch} eq "arm64"} {
        configure.args-append \
                        -DLLC_HOST_CPU=apple-m1
    } else {
        configure.args-append \
                        -DLLC_HOST_CPU=generic
    }
}

variant hwloc description {Use hwloc for CPU topology discovery} {
    depends_lib-append  port:hwloc
    configure.args-replace \
                        -DENABLE_HWLOC=OFF \
                        -DENABLE_HWLOC=ON
}

# Enable once MacPorts provides a compatible OpenCL ICD loader port.
# variant icd description {Use an external OpenCL ICD loader} {
#     depends_lib-append  port:opencl-icd-loader
#     configure.args-replace \
#                         -DENABLE_ICD=OFF \
#                         -DENABLE_ICD=ON
# }

variant spirv description {Enable SPIR-V program support} {
    depends_build-append \
                        port:python${python_version}
    depends_lib-append  port:spirv-llvm-translator-${llvm_version}
    configure.args-append \
                        -DPython3_EXECUTABLE=${python_bin}
    configure.args-replace \
                        -DENABLE_SPIRV=OFF \
                        -DENABLE_SPIRV=ON
}

variant vulkan description {Enable the experimental Vulkan device driver} {
    depends_build-append \
                        port:vulkan-headers
    depends_lib-append  path:lib/libMoltenVK.dylib:MoltenVK-latest \
                        port:vulkan-loader
    depends_run-append  port:clspv

    configure.args-replace \
                        -DENABLE_VULKAN=OFF \
                        -DENABLE_VULKAN=ON
}

default_variants        +hwloc
