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

# getline
legacysupport.newest_darwin_requires_legacy 10

github.setup        libocca occa 2.0.0 v
name                libocca
revision            0
categories          devel parallel
license             BSD
maintainers         {@barracuda156 gmail.com:vital.had} openmaintainer
description         Portable and vendor-neutral framework for parallel programming on heterogeneous platforms
long_description    OCCA is an open source, portable and vendor-neutral framework for parallel programming \
                    on heterogeneous platforms. The OCCA API provides unified models for heterogeneous programming \
                    concepts — such as a device, memory or kernel — while the OCCA Kernel Language (OKL) \
                    enables the creation of portable device kernels using a directive-based extension to the C language.
homepage            htpps://libocca.org
checksums           rmd160  5ef013688aed5e47fec8217d73d07a51472412c3 \
                    sha256  f2521901fed5d199193d54c7db4186479a974bdae92ac97779c47fa2bb68badd \
                    size    1142694
github.tarball_from archive

compiler.c_standard     2011
compiler.cxx_standard   2017
compiler.openmp_version 4.0
compilers.setup         require_fortran

# By default it installs into a weird location:
patchfiles-append   0001-CMakeLists-fix-install-paths.patch

# https://github.com/libocca/occa/commit/c0c601f80eff8f4eb7cc453a6a9db5bdd6554495
patchfiles-append   0002-sys.cpp-fix-macOS-without-breaking-Linux.patch

post-patch {
    reinplace "s,@DESTROOTDIR@,${destroot}${prefix}," ${worksrcpath}/CMakeLists.txt
}

pre-configure {
    # sh: /bin/ps: Operation not permitted
    append portsandbox_profile " (allow process-exec (literal \"/bin/ps\") (with no-profile))"

    configure.args-append \
                    -DOCCA_CC=${configure.cc} \
                    -DOCCA_CXX=${configure.cxx}
}

configure.args-append \
                    -DOCCA_ENABLE_FORTRAN=ON \
                    -DOCCA_ENABLE_OPENMP=ON \
                    -DOCCA_ENABLE_CUDA=OFF \
                    -DOCCA_ENABLE_DPCPP=OFF \
                    -DOCCA_ENABLE_EXAMPLES=OFF \
                    -DOCCA_ENABLE_HIP=OFF \
                    -DOCCA_ENABLE_METAL=OFF \
                    -DOCCA_ENABLE_TESTS=OFF

if {[string match *clang* ${configure.compiler}]} {
    configure.ldflags-append \
                    -L${prefix}/lib/libomp \
                    -lomp
}

# https://github.com/libocca/occa/issues/653
if {${configure.cxx_stdlib} eq "libc++"} {
    configure.ldflags-append -lc++abi -lc++
}

if {${os.platform} eq "darwin" && ${os.major} < 11} {
    configure.args-append \
                    -DOCCA_ENABLE_OPENCL=OFF
}

pre-destroot {
    xinstall -d ${destroot}${prefix}/include/occa
    xinstall -d ${destroot}${prefix}/share/occa/modulefiles
}

# Some tests fail on PPC: https://github.com/libocca/occa/issues/652
variant tests description "Build tests" {
    configure.pre_args-replace \
                    -DCMAKE_BUILD_WITH_INSTALL_RPATH:BOOL=ON \
                    -DCMAKE_BUILD_WITH_INSTALL_RPATH:BOOL=OFF
    configure.args-replace \
                    -DOCCA_ENABLE_TESTS=OFF -DOCCA_ENABLE_TESTS=ON
    test.run        yes
    test.cmd        ctest
    test.env-append OCCA_CC=${configure.cc} \
                    OCCA_CXX=${configure.cxx}
}
