# -*- 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.25
github.tarball_from archive
revision            0
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  db5155549a961df64b2dadec18da1fca31519dbf \
                    sha256  2e8381368ec0a6ecb770834bce00fc62efa09a2b2f9710ed569acbb823ead9cc \
                    size    847859

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

depends_build-append    port:python313
depends_lib-append      port:clang-${llvm_version}

cmake.install_rpath-append  ${llvm_dir}/lib

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

# 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/python3.13 \
                ${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}
        "
    }
}
