# -*- 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        include-what-you-use include-what-you-use 0.26
github.tarball_from archive
revision            1
categories          devel
license             NCSA
maintainers         {makr @mohd-akram} openmaintainer

description         A tool for use with clang to analyze #includes in C and C++ \
                    source files

long_description    {*}${description}

checksums           rmd160  d2cd5fa22930c0fcf5f44a38da0aacaa5c29e4d1 \
                    sha256  ca3f157a5e6bf293cfb2fea585b103ec87245d02eaee0b6ab832665e740016eb \
                    size    1146969

set llvm_version    22
set llvm_dir        ${prefix}/libexec/llvm-${llvm_version}

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

depends_build-append    port:python${py_ver_nodot}
depends_lib-append      port:clang-${llvm_version} \
                        port:llvm-${llvm_version}

configure.args-append   -DLLVM_DIR=${llvm_dir}/lib/cmake/llvm \
                        -DClang_DIR=${llvm_dir}/lib/cmake/clang \
                        -DPython3_EXECUTABLE=${prefix}/bin/python${py_ver}

# CMake Error at /opt/local/libexec/llvm-18/lib/cmake/llvm/CheckCompilerVersion.cmake:37 (message):
#  Host Apple Clang version must be at least 10.0
compiler.blacklist-append { clang <= 1100 }

post-destroot {
    # Move the binary next to clang so it picks up its include directory one
    # level above
    xinstall -d ${destroot}${llvm_dir}/bin
    move ${destroot}${prefix}/bin/${name} ${destroot}${llvm_dir}/bin
    ln -s ${llvm_dir}/bin/${name} ${destroot}${prefix}/bin
    platform darwin {
        delete ${destroot}${prefix}/bin/${name}
        # Generate mappings for macOS headers
        # Run via xcrun to pick up system include directories
        system -W "${destroot}${prefix}/bin" "
            ${prefix}/bin/python${py_ver} \
                ${worksrcpath}/mapgen/iwyu-mapgen-apple-libc.py \
                \$(xcrun --show-sdk-path)/usr/include \
                >${destroot}${prefix}/share/${name}/macos.imp
            echo '#!/bin/sh\nexec xcrun ${llvm_dir}/bin/${name} \
                -Xiwyu --mapping_file=${llvm_dir}/include/c++/v1/libcxx.imp \
                -Xiwyu --mapping_file=${prefix}/share/${name}/macos.imp \
                \"$@\"' >${name} && chmod +x ${name}
        "
    }
}
