# -*- 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
if {${os.major} <= 17} {
    PortGroup           muniversal 1.0
}
PortGroup               clang_dependency 1.0

name                    libomp
maintainers             {eborisch @eborisch} openmaintainer
homepage                https://openmp.llvm.org
description             The OpenMP subproject of LLVM
long_description        {*}${description} is intended to contain all of the\
                        components required to build an executing OpenMP\
                        program that are outside the compiler itself. \
                        This port works with MacPorts' clang-3.7+ (optionally \
                        with +openmp variant for clang-3.7.)

categories              lang
supported_archs         arm64 i386 x86_64
license                 {MIT NCSA}

# Moved to epoch 1 for svn # -> version # change.
epoch                   1

if {${os.platform} eq "darwin" && ${configure.cxx_stdlib} ne "libstdc++"} {

    version             21.1.5
    revision            0

    livecheck.regex {"llvmorg-([0-9.]+)".*}

    distname        openmp-${version}.src
    distfiles       ${distname}.tar.xz cmake-${version}.src.tar.xz

    checksums       openmp-${version}.src.tar.xz \
                    rmd160  8c7467295a19fc45d27932d21b7d1044d9516fb7 \
                    sha256  4bbfe240f23f873fe5fdd4d65e07b6ec0c0a4721cd0d339018b1f520cee48a08 \
                    size    1101652 \
                    cmake-${version}.src.tar.xz \
                    rmd160  e4e0c576be7413c26f6ca92ddac9d6b0a372c2d3 \
                    sha256  48013d5714a96419bf993a2e5e4c5827377e8cf9c565070731fb2305d50d9511 \
                    size    8976

    if {${os.major} <= 12} {
        # kmp_alloc.c includes <atomic> but libc++ is not the default on
        # these systems. https://trac.macports.org/ticket/52554
        configure.cflags-append -stdlib=libc++
    }

    if {${os.platform} eq "darwin" && ${os.major} < 13} {
        # Last python version to not require C11 (and thus a clang
        # dependency on some OS versions). Used by libpsl (which is a
        # dependency of cmake) for the same reason.
        set python_branch   3.10
    } else {
        set python_branch   3.13
    }
    set python_ver          [string map {. ""} ${python_branch}]
    depends_build-append    port:python${python_ver}
    depends_skip_archcheck-append python${python_ver}
    configure.args-append   -DPython3_EXECUTABLE=${prefix}/bin/python${python_branch}

    # Build requires std::atomic
    configure.cxxflags-append -std=c++11
    compiler.cxx_standard     2011

    master_sites        https://github.com/llvm/llvm-project/releases/download/llvmorg-${version} \
                        https://releases.llvm.org/${version}
    use_xz              yes
    dist_subdir         openmp-release
    worksrcdir          ${distname}
    set rtpath          "runtime/"
    patchfiles-append   patch-libomp-use-gettid-on-Leopard.diff

    livecheck.url       https://api.github.com/repos/llvm/llvm-project/tags
} else {
    PortGroup       github 1.0
    # Last version working on libstdc++
    github.setup    llvm-mirror openmp 381 svn-tags/RELEASE_
    # Change github.tarball_from to 'releases' or 'archive' next update
    github.tarball_from tarball
    name            libomp

    worksrcdir      openmp-${version}/final/runtime
    version         3.8.1
    checksums       rmd160  a41054068a127ef84610afef8090109078cb6c46 \
                    sha256  4c46b5946fe9b2a701661746d11c7c85c51a7f18673194a7ebd2a43470948a34 \
                    size    5587986
    set rtpath      "./"

    # use cmake-bootstrap to minimize dependencies.
    depends_build-replace  \
                    path:bin/cmake:cmake port:cmake-bootstrap
    configure.cmd   ${prefix}/libexec/cmake-bootstrap/bin/cmake

    depends_build-append    port:perl5
    livecheck.type      none
}

cmake.out_of_source     yes

# According to documentation builds with clang >= 3.3
compiler.blacklist-append {clang < 500} *gcc*

# https://trac.macports.org/ticket/68490#
# Use clang-11-bootstrap on OSX10.11 and older
if {${os.major} <= 15} {
    configure.compiler.add_deps no
    depends_build-append port:clang-11-bootstrap
    depends_skip_archcheck-append clang-11-bootstrap
    pre-configure {
        configure.cc    ${prefix}/libexec/clang-11-bootstrap/bin/clang
        configure.cxx   ${prefix}/libexec/clang-11-bootstrap/bin/clang++
    }
}

if {${os.major} <= 17} {
    default_variants    +universal
}

# Do actual install into ${prefix}/(install|lib)/libomp
# A little unorthodox, but to have clang automatically find the includes and
# lib, with -fopenmp, it is nice to have them their own directories. Not
# exactly a config cmake supports directly; install into dummy location and
# move post-install
configure.pre_args      -DCMAKE_INSTALL_PREFIX=${prefix}/tmp

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

# With this, cmake sets the correct library name in the dylibs for the
# final destination we move them to
configure.args-append   -DCMAKE_INSTALL_RPATH=${prefix}/lib/libomp \
                        -DCMAKE_INSTALL_NAME_DIR=${prefix}/lib/libomp

if {${os.major} <= 10} {
    set hnames              {omp.h}
    configure.args-append   -DLIBOMP_OMPT_SUPPORT=FALSE
} else {
    set hnames              {omp-tools.h omp.h ompt.h ompx.h}
}

set lnames {libiomp5.dylib libomp.dylib libgomp.dylib libgomp.1.dylib}

variant top_level description \
    "Install (links to) omp.h and libs into ${prefix}/(include|lib)" {}

post-extract {
    # Patch tool for build with CMAKE_INSTALL_PREFIX
    if {[vercmp ${version} 15.0.0] >= 0} {
        system -W ${workpath} "mv cmake*.src/Modules/* openmp*/cmake/"
    }
}

post-destroot {
    set instdest ${destroot}${prefix}
    xinstall -d ${instdest}/share/doc/libomp

    xinstall -d ${instdest}/include/libomp
    foreach h ${hnames} {
        move ${instdest}/tmp/include/${h} ${instdest}/include/libomp/
    }

    xinstall -d ${instdest}/lib/libomp
    foreach p ${lnames} {
        move ${instdest}/tmp/lib/${p} ${instdest}/lib/libomp/
    }

    if {[variant_isset top_level]} {
        foreach h ${hnames} {
            ln -s libomp/${h} ${instdest}/include/${h}
        }
        foreach p ${lnames} {
            ln -s libomp/${p} ${instdest}/lib/${p}
        }
    }

    set fpath ${worksrcpath}/${rtpath}
    file copy ${fpath}/README.txt ${fpath}/../LICENSE.TXT \
        ${instdest}/share/doc/libomp/

}

notes "
 To use this OpenMP library:
  * For clang-3.8+, or clang-3.7 with +openmp variant:
     add \"-fopenmp\" during compilation / linking.
  * For clang-3.7 without +openmp variant, use:
     \"-I${prefix}/include/libomp -L${prefix}/lib/libomp -fopenmp\"
"

test.run                yes
test.target             libomp-micro-tests
