# TODO:
#  * Update clang subport to build against installed llvm

PortSystem              1.0
PortGroup select        1.0
PortGroup compiler_blacklist_versions 1.0
PortGroup active_variants 1.1
PortGroup cmake         1.0

set llvm_version        3.8
set llvm_version_no_dot 38
name                    llvm-${llvm_version}
revision                3
subport                 clang-${llvm_version} { revision 9 }
set suffix              mp-${llvm_version}
set sub_prefix          ${prefix}/libexec/llvm-${llvm_version}
dist_subdir             llvm
categories              lang
platforms               darwin
license                 NCSA
maintainers             {jeremyhu @jeremyhu} larryv

if {${subport} eq "llvm-${llvm_version}"} {
    homepage            http://llvm.org/
    description         llvm is a next generation compiler infrastructure
    long_description    The LLVM Core libraries provide a modern source- and \
                        target-independent optimizer, along with code \
                        generation support for many popular CPUs (as well as \
                        some less common ones!) These libraries are built \
                        around a well specified code representation known as \
                        the LLVM intermediate representation ("LLVM IR").

    depends_lib         port:libedit port:libffi port:ncurses port:zlib port:python27
    depends_run         bin:perl:perl5 port:llvm_select
} elseif {${subport} eq "clang-${llvm_version}"} {
    homepage            http://clang.llvm.org/
    description         C, C++, Objective C and Objective C++ compiler
    long_description    Clang is an "LLVM native" C/C++/Objective-C compiler, \
                        which aims to deliver amazingly fast compiles (e.g. \
                        about 3x faster than GCC when compiling Objective-C \
                        code in a debug configuration), extremely useful error \
                        and warning messages and to provide a platform for \
                        building great source level tools. The included Clang \
                        Static Analyzer is a tool that automatically finds bugs in \
                        your code, and is a great example of the sort of tool \
                        that can be built using the Clang frontend as a \
                        library to parse C/C++ code.

    depends_lib         port:libxml2 port:libomp port:llvm-${llvm_version} port:python27
    depends_run         port:clang_select port:ld64
    depends_skip_archcheck-append ld64

    # Clang doesn't actually use any of these, but the LLVM makefile
    # system overlinks its binaries. Remove (or convert to build deps?)
    # after #46040 is resolved.
    depends_lib-append  port:libedit port:libffi port:ncurses port:zlib

    # Older Xcode's lipo doesn't support x86_64h slices
    # https://trac.macports.org/ticket/53159#ticket
    if {[vercmp $xcodeversion "6.0.0"] < 0} {
        depends_build-append port:cctools
        depends_skip_archcheck-append cctools
    }

    default_variants    +analyzer
}

#fetch.type              svn
#svn.revision            262722
#
#set compiler_rt_rev     ${svn.revision}
#set libcxx_rev          ${svn.revision}
#set clang-modernize_rev ${svn.revision}
#version                 ${llvm_version}-r${svn.revision}
#revision                1
#worksrcdir              trunk
#svn.url                 https://llvm.org/svn/llvm-project/llvm/trunk
#worksrcdir              release_${llvm_version_no_dot}
#svn.url                 https://llvm.org/svn/llvm-project/llvm/branches/release_${llvm_version_no_dot}
#default_variants-append +assertions
#default_variants-append +debug

version                 ${llvm_version}.1
epoch                   1
master_sites            http://llvm.org/releases/${version}
use_xz                  yes
extract.suffix          .tar.xz
distfiles               llvm-${version}.src${extract.suffix}
worksrcdir              llvm-${version}.src

if {${distfiles} ne ""} {
    if {${subport} eq "llvm-${llvm_version}"} {
        if {[variant_isset polly]} {
            distfiles-append     polly-${version}.src${extract.suffix}
        }
    } elseif {${subport} eq "clang-${llvm_version}"} {
        distfiles-append     cfe-${version}.src${extract.suffix} compiler-rt-${version}.src${extract.suffix} libcxx-${version}.src${extract.suffix} clang-tools-extra-${version}.src${extract.suffix}
    }
}

checksums           llvm-3.8.1.src.tar.xz \
                    rmd160  5e4d2193e7d58abaa70ab53e67b6fe058b781e6a \
                    sha256  6e82ce4adb54ff3afc18053d6981b6aed1406751b8742582ed50f04b5ab475f9 \
                    cfe-3.8.1.src.tar.xz \
                    rmd160  a280504a3ba3aa59274120305f68fc8f8b3aca9c \
                    sha256  4cd3836dfb4b88b597e075341cae86d61c63ce3963e45c7fe6a8bf59bb382cdf \
                    compiler-rt-3.8.1.src.tar.xz \
                    rmd160  4fdd7be538f2c92bc175455620063f1e6ed8544f \
                    sha256  0df011dae14d8700499dfc961602ee0a9572fef926202ade5dcdfe7858411e5c \
                    libcxx-3.8.1.src.tar.xz \
                    rmd160  02f573b38a821f70c7feb0ec57259f608f1485c5 \
                    sha256  77d7f3784c88096d785bd705fa1bab7031ce184cd91ba8a7008abf55264eeecc \
                    clang-tools-extra-3.8.1.src.tar.xz \
                    rmd160  1733dff12fa849a0c9b23e81b95e1d22e1039914 \
                    sha256  664a5c60220de9c290bf2a5b03d902ab731a4f95fe73a00856175ead494ec396 \
                    polly-3.8.1.src.tar.xz \
                    rmd160  766873d4b88e0e9c5670aeb45313d47cca7bd79a \
                    sha256  453c27e1581614bb3b6351bf5a2da2939563ea9d1de99c420f85ca8d87b928a2

patch.pre_args  -p1
patchfiles \
        0001-Set-the-Mach-O-CPU-Subtype-to-ppc7400-when-targeting.patch \
        0002-Define-EXC_MASK_CRASH-and-MACH_EXCEPTION_CODES-if-th.patch \
        0003-MacPorts-Only-Update-install-targets-for-clang-subpo.patch \
        0004-MacPorts-Only-Use-full-path-for-the-dylib-id-instead.patch \
        0005-MacPorts-Only-Don-t-embed-the-deployment-target-in-t.patch \
        0006-MacPorts-Only-Skip-checking-for-python-in-configure.patch \
        0007-Remove-override-of-raise-abort-and-__assert_rtn.patch \
        0008-CMake-Use-CMake-s-default-RPATH-for-the-unit-tests.patch \
        0009-CMake-Fix-rpath-construction-for-out-of-tree-builds.patch \
        0010-CMake-Make-CMAKE_INSTALL_RPATH-work-again.patch \
        0011-CMake-Fix-llvm_setup_rpath-function.patch \
        llvm-skip-unittests.patch \
        4001-patch-llvm-Only-call-setpriority-PRIO_DARWIN_THREAD-0-PRIO_DARW.diff

if {${subport} eq "clang-${llvm_version}"} {
    patchfiles-append \
        1001-MacPorts-Only-Prepare-clang-format-for-replacement-w.patch \
        1002-MacPorts-Only-Fall-back-on-xcodebuild-sdk-when-xcrun.patch \
        1003-MacPorts-Only-Fix-name-of-scan-view-executable-insid.patch \
        1004-MacPorts-Only-Relocate-clang-resources-using-llvm-ve.patch \
        1005-Default-to-ppc7400-for-OSX-10.5.patch \
        1006-Only-call-setpriority-PRIO_DARWIN_THREAD-0-PRIO_DARW.patch \
        1007-Default-to-fragile-ObjC-runtime-when-targeting-darwi.patch \
        1008-Fixup-libstdc-header-search-paths-for-older-versions.patch \
        1009-Darwin-Stop-linking-libclang_rt.eprintf.a.patch \
        2001-MacPorts-Only-Comment-out-SL-cctools-workaround.patch \
        2002-Update-CheckArches-to-fallback-on-Intel-ppc-if-ld-v-.patch \
        2003-Fall-back-on-xcodebuild-sdk-when-xcrun-sdk-is-not-su.patch \
        2004-On-darwin-build-ppc-slices-of-the-compiler-runtime-i.patch \
        2005-MacPorts-Only-Don-t-build-x86_64h-slice-of-compiler-.patch \
        2006-MacPorts-Only-Fix-regression-introduced-when-fixing-.patch \
        2007-MacPorts-Only-Don-t-check-for-the-macosx.internal-SD.patch \
        2008-CMake-NFC-Move-macro-definitions-out-of-config-ix.cm.patch \
        2009-CMake-Adding-some-missing-CMake-includes.-NFC.patch \
        2010-CMake-Adding-another-missing-include.-NFC.patch \
        2011-CMake-NFC.-Add-support-for-testing-the-compiler-with.patch \
        2012-Add-missing-include.patch \
        3001-buildit-build-fix-for-Leopard.patch \
        3002-buildit-Set-compatibility-version-to-RC_ProjectSourc.patch \
        3003-Fix-local-and-iterator-when-building-with-Lion-and-n.patch \
        3004-Fix-missing-long-long-math-prototypes-when-using-the.patch \
        3005-implement-atomic-using-mutex-lock_guard-for-64b-ops-.patch \
        compiler_rt-toolchain.patch \
        openmp-locations.patch \
        4000-patch-clang-lib-codegen-targetinfo-ppc-38.diff \
        4001-patch-clang-3.8-gccabi-sema.diff \
        4002-patch-clang-3.8-gccabi-mangler.diff
    
    # 4000 - backport va_args support for PPC from trunk
    # <https://reviews.llvm.org/D33499>
    
    # 4001 and 4002 - backport gcc5.1 abi support from trunk to 3.8, using debian patches
    # <https://sources.debian.net/src/llvm-toolchain-3.8/1:3.8.1-24/debian/patches/>

    # I'm not sure if we need to delete this.  It seems to build fine with it
    # present, but we used to explicitly only use this patch for the llvm subport.
    patchfiles-delete \
        0005-MacPorts-Only-Skip-checking-for-python-in-configure.patch

    # https://llvm.org/bugs/show_bug.cgi?id=25681
    if {${worksrcdir} eq "trunk" || ${worksrcdir} eq "release_${llvm_version_no_dot}"} {
        # Cannot set cppflags due to https://cmake.org/Bug/view.php?id=12928
        # Cannot set cxxflags due to https://llvm.org/bugs/show_bug.cgi?id=25904
        #configure.cxxflags-append -DSVN_REVISION='"${svn.revision}"'

        post-patch {
            reinplace "1 i \\                             
                #define SVN_REVISION \"${svn.revision}\"
            " ${worksrcpath}/tools/clang/lib/Basic/Version.cpp
        }
    }
}

configure.post_args         ../${worksrcdir}
default configure.dir       {${workpath}/build}
default build.dir           {${workpath}/build}

cmake.install_prefix        ${sub_prefix}

# Adjust this once cmake-1.0.tcl is fixed:
#     https://github.com/macports/macports-ports/pull/103
# Also see:
#     https://llvm.org/bugs/show_bug.cgi?id=31425
configure.args-delete \
    -DCMAKE_INSTALL_NAME_DIR=${prefix}/lib \
    -DCMAKE_INSTALL_RPATH=${prefix}/lib \
    -DCMAKE_SYSTEM_PREFIX_PATH="${prefix}\;/usr"
configure.args-append \
    -DCMAKE_SYSTEM_PREFIX_PATH="${cmake.install_prefix}\;${prefix}\;/usr"

configure.args-append \
    -DLLVM_LINK_LLVM_DYLIB=ON \
    -DCMAKE_INSTALL_PREFIX="${sub_prefix}" \
    -DLLVM_ENABLE_ASSERTIONS=OFF \
    -DLLVM_ENABLE_RTTI=ON \
    -DLLVM_INCLUDE_TESTS=OFF \
    -DLLVM_INCLUDE_EXAMPLES=OFF \
    -DLLVM_ENABLE_FFI=ON \
    -DLLVM_BINDINGS_LIST=none \
    -DFFI_INCLUDE_DIR=`pkg-config --cflags-only-I libffi | sed 's/-I//'` \
    -DFFI_LIBRARY_DIR=${prefix}/lib

if {${subport} eq "llvm-${llvm_version}"} {
    select.group        llvm
    select.file         ${filespath}/mp-${subport}
} elseif {${subport} eq "clang-${llvm_version}"} {
    select.group        clang
    select.file         ${filespath}/mp-${subport}

    # CMAKE_LINKER is used to determine the value for HOST_LINK_VERSION
    configure.args-append \
        -DCMAKE_LINKER=${prefix}/bin/ld \
        -DCLANG_INCLUDE_TESTS=OFF \
        -DCLANG_ENABLE_STATIC_ANALYZER=OFF \
        -DLLVM_BUILD_RUNTIME=ON \
        -DLIBCXX_INSTALL_LIBRARY=OFF
}

# llvm-3.5 and later requires a C++11 runtime
# XCode 4.3's clang (318.x) fails per https://trac.macports.org/ticket/44161
# XCode 4.5's clang (421.11.66) fails due to http://llvm.org/bugs/show_bug.cgi?id=20184
# Xcode 4.6.3's clang (425.0.28) fails due to http://trac.macports.org/ticket/46897
# Xcode 5.1's clang (clang-503.0.40) has codegen issues (resulting compiler crashes)
# Xcode 6.2's clang (600.0.57) fails due to https://llvm.org/bugs/show_bug.cgi?id=25753
compiler.blacklist *gcc* {clang < 602}

if {${subport} eq "clang-${llvm_version}"} {
    # clang older than 3.5 fail due to https://llvm.org/bugs/show_bug.cgi?id=25753
    compiler.blacklist-append macports-clang-3.3 macports-clang-3.4
}

# blacklist current and future versions if they're not available in order to
# help break potential dependency cycles.
foreach ver {3.8 3.9 4.0 devel} {
    if {![file exists ${prefix}/bin/clang-mp-${ver}]} {
        compiler.blacklist-append macports-clang-${ver}
    }
}

# Set CMAKE_LIBTOOL if we're using MacPorts-provided cctools
if {[lsearch -exact $PortInfo(depends_build) port:cctools] != -1} {
    configure.args-append \
        -DCMAKE_LIBTOOL=${prefix}/bin/libtool
}

# restore this fix from an older clang portfile
if {${subport} eq "clang-${llvm_version}" && ${os.major} <= 10} {
        patchfiles-append 9002-compiler_rt-ppc_fallbacks.patch
}

platform darwin i386 {
    # Note that we are forcing this choice.  This means that anything linking
    # against llvm-3.5 needs to also be using libc++.  This is possibly
    # problematic, but luckily there is just a limited set of such dependents.

    configure.cxx_stdlib libc++
    depends_lib-append port:libcxx
    supported_archs i386 x86_64

    pre-fetch {
        if {![file exists /usr/lib/libc++.dylib]} {
            ui_error "$name requires a C++11 runtime, which your configuration does not allow"
            error "unsupported configuration"
        }
    }
}


# fix build on powerpc
platform darwin powerpc {

    # at present, we are going to build clang-3.8 using gcc6
    # it can link against either libc++ (on 10.5) or libstdc++ (on 10.4+)
    # it can also link against gcc5+ libstdc++ to get cxx11, using
    # -stdlib=macports-libstdc++ with the ABI mode set to be 4 compatible
    # to do this, it is best to build libgcc with ABI mode set to 4

    # these don't appear to be needed and are a PITA to build on PPC
    depends_lib-delete  port:libedit port:ncurses port:zlib

    # not as sure about this one - might be needed
    # depends_lib-delete  port:libffi

    #delete all the compiler blacklisting above
    compiler.blacklist
    
    # build with gcc6, which works. probably gcc 4.8+ would work as well, not tried
    compiler.whitelist macports-gcc-6
    supported_archs ppc ppc64
    
    default_variants +libstdcxx
    universal_variant no
    
    if {${subport} eq "clang-${llvm_version}" && ${os.major} <= 10} {
        patchfiles-append 9002-patch-clang-3.8-compiler-rt-ppc-additions.diff
        patchfiles-append 9003-patch-clang-3.8-libcxx-thread-nanoseconds-max-gcc.diff
    }

#    configure.args-append -DLLVM_TARGETS_TO_BUILD="PowerPC X86"
    configure.args-append -DLLVM_TARGETS_TO_BUILD="PowerPC"

    
    # libomp doesn't presently build on powerpc so
    # use gcc6's libgomp instead
    
    # To do - we will still need to fix up the macports patch to the directory for this library 
    # to point to libgcc's version

    depends_lib-delete port:libomp
    if {${subport} eq "clang-${llvm_version}"} {
        configure.args-append --with-clang-default-openmp-runtime=libgomp
    }

}




variant universal {
    post-extract {
        # workaround a bug in Apple's shipped gcc driver-driver, patched in
        # ours with driverdriver-num_infiles.patch
        if {${configure.compiler} eq "gcc-4.0" ||
            ${configure.compiler} eq "gcc-4.2" ||
            ${configure.compiler} eq "llvm-gcc-4.2"} {
            system "echo \"static int ___ignoreme;\" > ${worksrcpath}/tools/llvm-shlib/ignore.c"
        }
    }
}

variant assertions description "Enable assertions for error detection (has performance impacts, especially on JIT)" {
    configure.args-delete -DLLVM_ENABLE_ASSERTIONS=OFF
    configure.args-append -DLLVM_ENABLE_ASSERTIONS=ON
}

platform darwin {
    if {[string match "*ppc*" [get_canonical_archs]]} {
        # http://trac.macports.org/ticket/33987
        configure.optflags    -Os
    }

    if {${subport} eq "clang-${llvm_version}" && [vercmp $xcodeversion 5.0] < 0} {
        # https://llvm.org/bugs/show_bug.cgi?id=13671
        patchfiles-append leopard-no-asan.patch
        configure.args-append -DCOMPILER_RT_BUILD_SANITIZERS=OFF
    }

    if {${subport} eq "clang-${llvm_version}" && ${os.major} <= 9} {
        patchfiles-append leopard-no-blocks.patch
    }

    if {${os.major} < 11} {
        # Proxy for eliminating the dependency on native TLS
        # http://trac.macports.org/ticket/46887
        configure.args-append -DLLVM_ENABLE_BACKTRACES=OFF

        # https://llvm.org/bugs/show_bug.cgi?id=25680
        configure.cxxflags-append -U__STRICT_ANSI__
    }

    # https://llvm.org/bugs/show_bug.cgi?id=25674
    configure.cxxflags-append -std=c++11
}

post-extract {
    # Get HTTP proxy arguments if required, assuming we can use the same proxy for all repositories
    set proxy_args [portfetch::svn_proxy_args http://llvm.org/svn/llvm-project/cfe/trunk]
    if {${subport} eq "llvm-${llvm_version}"} {
        if {[variant_isset polly]} {
            if {${worksrcdir} eq "trunk"} {
                system -W ${worksrcpath}/tools "svn ${proxy_args} co -r ${svn.revision} http://llvm.org/svn/llvm-project/polly/trunk polly"
            } elseif {${worksrcdir} eq "release_${llvm_version_no_dot}" } {
                system -W ${worksrcpath}/tools "svn ${proxy_args} co -r ${svn.revision} http://llvm.org/svn/llvm-project/polly/branches/release_${llvm_version_no_dot} polly"
            } else {
                file rename ${workpath}/polly-${version}.src ${worksrcpath}/tools/polly
            }
        }
    } elseif {${subport} eq "clang-${llvm_version}"} {
        if {${worksrcdir} eq "trunk"} {
            system -W ${worksrcpath}/tools "svn ${proxy_args} co -r ${svn.revision} http://llvm.org/svn/llvm-project/cfe/trunk clang"
            system -W ${worksrcpath}/projects "svn ${proxy_args} co -r ${compiler_rt_rev} http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt"
            system -W ${worksrcpath}/projects "svn ${proxy_args} co -r ${libcxx_rev} http://llvm.org/svn/llvm-project/libcxx/trunk libcxx"
            system -W ${worksrcpath}/tools/clang/tools "svn ${proxy_args} co -r ${clang-modernize_rev} http://llvm.org/svn/llvm-project/clang-tools-extra/trunk extra"
        } elseif {${worksrcdir} eq "release_${llvm_version_no_dot}" } {
            system -W ${worksrcpath}/tools "svn ${proxy_args} co -r ${svn.revision} http://llvm.org/svn/llvm-project/cfe/branches/release_${llvm_version_no_dot} clang"
            system -W ${worksrcpath}/projects "svn ${proxy_args} co -r ${compiler_rt_rev} http://llvm.org/svn/llvm-project/compiler-rt/branches/release_${llvm_version_no_dot} compiler-rt"
            system -W ${worksrcpath}/projects "svn ${proxy_args} co -r ${libcxx_rev} http://llvm.org/svn/llvm-project/libcxx/branches/release_${llvm_version_no_dot} libcxx"
            system -W ${worksrcpath}/tools/clang/tools "svn ${proxy_args} co -r ${clang-modernize_rev} http://llvm.org/svn/llvm-project/clang-tools-extra/branches/release_${llvm_version_no_dot} extra"
        } else {
            file rename ${workpath}/cfe-${version}.src ${worksrcpath}/tools/clang
            file rename ${workpath}/compiler-rt-${version}.src ${worksrcpath}/projects/compiler-rt
            file rename ${workpath}/libcxx-${version}.src ${worksrcpath}/projects/libcxx
            file rename ${workpath}/clang-tools-extra-${version}.src ${worksrcpath}/tools/clang/tools/extra
        }
    }
}

if {${subport} eq "clang-${llvm_version}"} {
    destroot {
        system "cd ${destroot.dir}/tools/clang && ${destroot.cmd} ${destroot.pre_args} ${destroot.target} ${destroot.post_args}"
        system "cd ${destroot.dir}/projects/compiler-rt && ${destroot.cmd} ${destroot.pre_args} ${destroot.target} ${destroot.post_args}"
        system "cd ${destroot.dir}/projects/libcxx && ${destroot.cmd} ${destroot.pre_args} ${destroot.target} ${destroot.post_args}"

        delete ${destroot}${sub_prefix}/bin/clang
        file rename ${destroot}${sub_prefix}/bin/clang-${llvm_version} ${destroot}${sub_prefix}/bin/clang
    }
}

post-destroot {
    foreach bin [glob ${destroot}${sub_prefix}/bin/*] {
        set bin_filename [string map "${sub_prefix} ${prefix}" ${bin}]-${suffix}
        set exec_path [string map "${destroot}${sub_prefix} ${sub_prefix}" ${bin}]

        xinstall -m 755 "${filespath}/llvm-bin" "${bin_filename}"
        reinplace "s:EXEC_PATH:${exec_path}:" "${bin_filename}"
    }

    if {${subport} eq "llvm-${llvm_version}"} {
        # r156389 (a5d2435409858728970202226d0bbbee508fe408) temporarilary removed llvm man pages
        #foreach man [glob ${destroot}${sub_prefix}/share/man/man1/*.1] {
        #    set basename [string map "${destroot}${sub_prefix}/share/man/man1/ {}" ${man}]
        #    file rename ${man} ${destroot}${prefix}/share/man/man1/[string map ".1 -${suffix}.1" ${basename}]
        #}

        # https://trac.macports.org/ticket/53673
        ln -s llvm-dsymutil ${destroot}${sub_prefix}/bin/dsymutil

        # https://llvm.org/bugs/show_bug.cgi?id=19465
        if {[variant_isset polly]} {
            ln -s LLVMPolly.so ${destroot}${sub_prefix}/lib/LLVMPolly.dylib
        }
    } elseif {${subport} eq "clang-${llvm_version}"} {
        system "ditto ${worksrcpath}/projects/libcxx/include ${destroot}${sub_prefix}/lib/c++/v1"

        # http://trac.macports.org/ticket/33207
        ln -s ${prefix}/libexec/ld64/ld ${destroot}${sub_prefix}/bin/ld
    }
}

if {${subport} eq "llvm-${llvm_version}"} {
    variant polly description {Provide the polly polyhedral optimizer} {}

    # Unless upstream reverts to their old OCaml detection mechanism,
    # this variant will be broken until #46161 is resolved.
    variant ocaml description {Enable generation of OCaml binding} {
        depends_lib-append   port:ocaml

        configure.args-delete -DLLVM_BINDINGS_LIST=none
        configure.args-append -DLLVM_BINDINGS_LIST=ocaml

        destroot.args-append  OVERRIDE_libdir=${sub_prefix}/lib
    }
} elseif {${subport} eq "clang-${llvm_version}"} {
    if {[variant_isset assertions]} {
        # Need to match llvm +-assertions
        require_active_variants port:llvm-${llvm_version} assertions
    } else {
        # Need to match llvm +-assertions
        require_active_variants port:llvm-${llvm_version} {} assertions
    }

    variant analyzer description {Install clang static analyzer} {
        configure.args-delete \
            -DCLANG_ENABLE_STATIC_ANALYZER=OFF
        configure.args-append \
            -DCLANG_ENABLE_STATIC_ANALYZER=ON

        depends_run-append port:perl5

        post-patch {
            reinplace "s|/usr/bin/env perl|${prefix}/bin/perl5|g" \
                ${worksrcpath}/tools/clang/tools/scan-build/libexec/ccc-analyzer \
                ${worksrcpath}/tools/clang/tools/scan-build/libexec/c++-analyzer \
                ${worksrcpath}/tools/clang/tools/scan-build/bin/scan-build
            reinplace "s|/usr/bin/env python|${prefix}/bin/python2.7|g" \
                ${worksrcpath}/tools/clang/tools/scan-build/bin/set-xcode-analyzer \
                ${worksrcpath}/tools/clang/tools/scan-view/bin/scan-view
        }
    }
    
    
    variant libstdcxx description {-stdlib=macports-libstdc++ searches for MacPorts libstdc++} {
        depends_lib-append port:gcc6

        patchfiles-append 9001-macports-libstdcxx-3.8.diff

        post-patch {
            reinplace "s|@@MACPORTS_GCC_INCLUDE_DIR@@|${prefix}/include/gcc/c++|g" \
                ${worksrcpath}/tools/clang/lib/Frontend/InitHeaderSearch.cpp
            reinplace "s|@@MACPORTS_HOST_NAME@@|${build_arch}-apple-darwin${os.major}|g" \
                ${worksrcpath}/tools/clang/lib/Frontend/InitHeaderSearch.cpp
            reinplace "s|@@MACPORTS_libstdc++@@|${prefix}/lib/libgcc/libstdc++.6.dylib|g" \
                ${worksrcpath}/tools/clang/lib/Driver/ToolChains.cpp

            # GCC was built for ${build_arch}-apple-darwin${os.major}
            # if GCC was also built universal, it has an extra include directory
            # if ${build_arch} is 32-bit, there is an extra 64-bit subdirectory
            # if ${build_arch} is 64-bit, there is an extra 32-bit subdirectory
            # MacPorts GCC can not compile across platforms
            switch ${build_arch} {
                x86_64 {
                    set gcc_subdirectory_ppc    ""
                    set gcc_subdirectory_ppc64  "ppc64"
                    set gcc_subdirectory_i386   "i386"
                    set gcc_subdirectory_x86_64 ""
                }
                i386 {
                    set gcc_subdirectory_ppc    ""
                    set gcc_subdirectory_ppc64  "ppc64"
                    set gcc_subdirectory_i386   ""
                    set gcc_subdirectory_x86_64 "x86_64"
                }
                ppc64 {
                    set gcc_subdirectory_ppc    "ppc"
                    set gcc_subdirectory_ppc64  ""
                    set gcc_subdirectory_i386   ""
                    set gcc_subdirectory_x86_64 "x86_64"
                }
                ppc {
                    set gcc_subdirectory_ppc    ""
                    set gcc_subdirectory_ppc64  "ppc64"
                    set gcc_subdirectory_i386   ""
                    set gcc_subdirectory_x86_64 "x86_64"
                }
            }

            reinplace "s|@@MACPORTS_GCC_SUBDIRECTORY_x86_64@@|${gcc_subdirectory_x86_64}|g" \
                ${worksrcpath}/tools/clang/lib/Frontend/InitHeaderSearch.cpp
            reinplace "s|@@MACPORTS_GCC_SUBDIRECTORY_i386@@|${gcc_subdirectory_i386}|g" \
                ${worksrcpath}/tools/clang/lib/Frontend/InitHeaderSearch.cpp
            reinplace "s|@@MACPORTS_GCC_SUBDIRECTORY_ppc64@@|${gcc_subdirectory_ppc64}|g" \
                ${worksrcpath}/tools/clang/lib/Frontend/InitHeaderSearch.cpp
            reinplace "s|@@MACPORTS_GCC_SUBDIRECTORY_ppc@@|${gcc_subdirectory_ppc}|g" \
                ${worksrcpath}/tools/clang/lib/Frontend/InitHeaderSearch.cpp
        }
    }

    default_variants-append +libstdcxx

    variant defaultlibcxx description {default to -stdlib=libc++ on all systems}  {
        #patch clang to always default to -stdlib=libc++ if not otherwise specified
        patchfiles-append 9004-patch-clang-3.8-Toolchains-default-always-libcxx.diff
    }

    # if macports.conf is configured to stdlib=libc++ then make that the default if not otherwise specified
    # exactly matches the behaviour of newer systems
    if { ${cxx_stdlib} eq "libc++" } {
        default_variants-append +defaultlibcxx
    }


    post-patch {
        reinplace "s|@@PREFIX@@|${prefix}|" \
            ${worksrcpath}/tools/clang/lib/Driver/Tools.cpp

        reinplace "s|@CLANG_FORMAT_PATH@|${prefix}/bin/clang-format-${suffix}|g" \
            ${worksrcpath}/tools/clang/tools/clang-format/clang-format-bbedit.applescript \
            ${worksrcpath}/tools/clang/tools/clang-format/clang-format-diff.py            \
            ${worksrcpath}/tools/clang/tools/clang-format/clang-format-sublime.py         \
            ${worksrcpath}/tools/clang/tools/clang-format/clang-format.el                 \
            ${worksrcpath}/tools/clang/tools/clang-format/clang-format.py
    }

    post-destroot {
        file mkdir ${destroot}${sub_prefix}/libexec
        file copy ${worksrcpath}/tools/clang/tools/clang-format ${destroot}${sub_prefix}/libexec/clang-format

        file delete -force ${destroot}${sub_prefix}/libexec/clang-format/.svn
        file delete -force ${destroot}${sub_prefix}/libexec/clang-format/Makefile
        file delete -force ${destroot}${sub_prefix}/libexec/clang-format/Release+Debug+Asserts
        file delete -force ${destroot}${sub_prefix}/libexec/clang-format/CMakeLists.txt
        file delete -force ${destroot}${sub_prefix}/libexec/clang-format/ClangFormat.cpp
    }
}

livecheck.type          none
