# This compiler version is not supported beyond these very strict use cases:
#     1) Building webkit-gtk-2.0 on libstdc++ systems
#     2) Building llvm-3.4 on Leopard (https://trac.macports.org/ticket/52091)
#
# There should be no other usage of clang-3.3 nor llvm-3.3

PortSystem              1.0
PortGroup select        1.0

set llvm_version        3.3
name                    llvm-${llvm_version}
revision                13
subport                 clang-${llvm_version} { revision 14 }
set suffix              mp-${llvm_version}
set sub_prefix          ${prefix}/libexec/llvm-${llvm_version}
# prefix for deps using libstdc++ on a libc++ system
set bootstrap_prefix    ${prefix}/libexec/libcxx-bootstrap
dist_subdir             llvm
categories              lang
platforms               darwin
license                 NCSA
maintainers             {jeremyhu @jeremyhu} {larryv @larryv} {kencu @kencu}

if {${subport} eq "llvm-${llvm_version}"} {
    homepage            https://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:libffi
    depends_run         bin:perl:perl5 port:llvm_select
} elseif {${subport} eq "clang-${llvm_version}"} {
    homepage            https://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:llvm-${llvm_version} port:libffi
    depends_run         port:clang_select port:ld64
    depends_build       port:cctools
    depends_skip_archcheck-append cctools ld64

    # Avoid requiring a bootstrap version of perl5 on < 10.7
    if {${os.major} >= 11} {
        default_variants    +analyzer
    }
}

version                 ${llvm_version}
epoch                   1
master_sites            https://releases.llvm.org/${version}
extract.suffix          .tar.gz
distfiles               llvm-${version}.src${extract.suffix}
worksrcdir              llvm-${version}.src

checksums           llvm-3.3.src.tar.gz \
                    rmd160  22878ad746c50b02a7ac8713dd6f8c95c7af4220 \
                    sha256  68766b1e70d05a25e2f502e997a3cb3937187a3296595cf6e0977d5cd6727578 \
                    cfe-3.3.src.tar.gz \
                    rmd160  ff5d684c83b0c1aa36595dcb457da80b58eaf58f \
                    sha256  b1b55de4ab3a57d3e0331a83e0284610191c77d924e3446498d9113d08dfb996 \
                    compiler-rt-3.3.src.tar.gz \
                    rmd160  943675fe098d1fc197d381f55ad9a72681652d09 \
                    sha256  0e2f3180d6316e6c43f064fdd406c5c6515e682c5f31c57c28335b68c7525423 \
                    libcxx-3.3.src.tar.gz \
                    rmd160  b97ba28e87d30dfe5b4f540ce20a190ac0bcab9c \
                    sha256  c403ed18d2992719c794cdd760dc87a948b62a7c2a07beb39eb984dfeb1679f1

patchfiles      install_target.patch \
                0001-Fix-regular-expression-to-work-with-multiple-digit-v.patch \
                tiger.patch \
                tiger-undefined-_CS_DARWIN_USER_-macros.patch \
                tiger-shlib.patch

patch.pre_args  -p1

if {${subport} eq "clang-${llvm_version}"} {
    if {${distfiles} ne ""} {
        distfiles-append     cfe-${version}.src${extract.suffix} compiler-rt-${version}.src${extract.suffix} libcxx-${version}.src${extract.suffix}
    }

    patchfiles-append    tiger-shlib-clang.patch tiger-libclang.patch scan-build-PR-35006.patch \
                         0001-Comment-out-SL-cctools-workaround.patch \
                         1012-Fixup-libstdc-header-search-paths-for-older-versions.patch \
                         mavericks-libc++.patch \
                         snowleopard-cmath.patch \
                         yosemite-deployment-target.patch \
                         export_dynamic.patch

    build.target        clang-only
    destroot.target     install-clang
}

build.env-append        VERBOSE=1 \
                        REQUIRES_RTTI=1 \
                        LLVM_SUBMIT_VERSION=3 \
                        LLVM_SUBMIT_SUBVERSION=7.1
destroot.env-append     VERBOSE=1 \
                        REQUIRES_RTTI=1 \
                        LLVM_SUBMIT_VERSION=3 \
                        LLVM_SUBMIT_SUBVERSION=7.1

configure.dir           ${workpath}/build
build.dir               ${configure.dir}

configure.optflags
configure.cppflags
configure.ldflags
configure.cmd           ${worksrcpath}/configure
configure.pre_args-delete --prefix=${prefix}
configure.args          --enable-bindings=none --enable-libffi --enable-shared --enable-jit \
                        --enable-optimized --disable-profiling --enable-pic \
                        --disable-debug-symbols --disable-debug-runtime \
                        --prefix="${sub_prefix}" --disable-assertions

# need arm target or compiler-rt build fails
configure.args-append   --enable-targets=x86,powerpc,arm

set pythonfullpath   /usr/bin/python2.7

#  older systems need a newer python
if {${os.platform} eq "darwin" && ${os.major} < 11} {
    if {${cxx_stdlib} eq "libc++"} {
        # force to libstdc++ so we can use default bootstrap compilers
        configure.cxx_stdlib    libstdc++
        depends_build-append    port:python27-bootstrap
        set pythonfullpath      ${bootstrap_prefix}/bin/python2.7
    } else {
        depends_build-append    port:python27
        set pythonfullpath      ${prefix}/bin/python2.7
    }
}
configure.args-append   --with-python=${pythonfullpath}

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}
}

# g++-4.0 fails to build some of the newer C++ for ppc
# Intel looks ok, but I prefer using gcc-4.2 for consistency
compiler.blacklist gcc-4.0

# avoid dependency cycles
compiler.blacklist-append   macports-clang-*

platform darwin {
    supported_archs ppc ppc64 i386 x86_64
}

variant universal {
    build.env-append \
        UNIVERSAL=1 \
        UNIVERSAL_ARCH=[get_canonical_archs]
    destroot.env-append \
        UNIVERSAL=1 \
        UNIVERSAL_ARCH=[get_canonical_archs]

    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 --disable-assertions
    configure.args-append --enable-assertions
}

platform darwin {
    if {${build_arch} eq "i386"} {
        configure.pre_args-append --build=i686-apple-darwin${os.major}
    } elseif {${build_arch} eq "ppc"} {
        configure.pre_args-append --build=powerpc-apple-darwin${os.major}
    } elseif {${build_arch} eq "ppc64"} {
        configure.pre_args-append --build=powerpc64-apple-darwin${os.major}
    } else {
        configure.pre_args-append --build=${build_arch}-apple-darwin${os.major}
    }

    if {[string match "*ppc*" [get_canonical_archs]]} {
        # http://trac.macports.org/ticket/33987
        configure.args-append --with-optimize-option=-Os
        configure.optflags    -Os
    }

    if {${subport} eq "clang-${llvm_version}" && ${os.major} <= 9} {
        patchfiles-append leopard-no-asan.patch 0001-Leopard-Default-to-fno-blocks.patch
    }

    # TODO: The llvm build system sets a 10.1 deployment target on Yosemite
    if {[vercmp ${macosx_deployment_target} 10.10] >= 0} {
        macosx_deployment_target 10.9
    }

    if {${os.major} < 9} {
        # Tiger's DevTools make has issues with the nested ifs in compiler-rt
        depends_run-append      port:gmake
        build.cmd               ${prefix}/bin/gmake

        post-configure {
            reinplace "/^RPATH/s/=.*/=/" ${configure.dir}/Makefile.config
        }
    }
}

post-extract {
    if {${subport} eq "clang-${llvm_version}"} {
        file rename ${workpath}/cfe-${llvm_version}.src ${worksrcpath}/tools/clang
        file rename ${workpath}/compiler-rt-${llvm_version}.src ${worksrcpath}/projects/compiler-rt
        file rename ${workpath}/libcxx-${llvm_version}.src ${worksrcpath}/projects/libcxx
    }
}

post-patch {
    if {${subport} eq "clang-${llvm_version}"} {
        # http://trac.macports.org/ticket/33272
        if {![variant_isset arm_runtime]} {
            reinplace {/^SubDirs/ s/arm//} ${worksrcpath}/projects/compiler-rt/lib/Makefile.mk
            reinplace {/^UniversalArchs/ s/arm[^ )]* *//g} ${worksrcpath}/projects/compiler-rt/make/platform/clang_darwin.mk

            reinplace {/cc_kext_ios5/ s/^/#/} ${worksrcpath}/projects/compiler-rt/make/platform/clang_darwin.mk
            reinplace {s/cc_kext_ios5.a//} ${worksrcpath}/tools/clang/runtime/compiler-rt/Makefile
        }

        reinplace {s/VERBOSE := 0/VERBOSE := 1/} ${worksrcpath}/projects/compiler-rt/make/config.mk
    }
}

post-destroot {
    file mkdir ${destroot}${prefix}/share/doc
    file rename ${destroot}${sub_prefix}/docs/llvm ${destroot}${prefix}/share/doc/${subport}

    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}]
        #}

        foreach liba [glob ${destroot}${sub_prefix}/lib/*dylib] {
            set liba_nodr [string map "${destroot} {}" ${liba}]
            system "install_name_tool -id ${liba_nodr} ${liba}"

            foreach libb [glob ${destroot}${sub_prefix}/lib/*dylib] {
                set libb_base [string map "${destroot}${sub_prefix}/lib/ {}" ${libb}]
                set libb_nodr [string map "${destroot} {}" ${libb}]

                system "install_name_tool -change @executable_path/../lib/${libb_base} ${libb_nodr} ${liba}"
            }
        }
    } 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

        system "install_name_tool -id ${sub_prefix}/lib/libclang.dylib ${destroot}${sub_prefix}/lib/libclang.dylib"
        system "install_name_tool -change @executable_path/../lib/libLLVM-${llvm_version}svn.dylib ${sub_prefix}/lib/libLLVM-${llvm_version}svn.dylib ${destroot}${sub_prefix}/lib/libclang.dylib"
        system "install_name_tool -change @executable_path/../lib/libLLVM-${llvm_version}.dylib ${sub_prefix}/lib/libLLVM-${llvm_version}.dylib ${destroot}${sub_prefix}/lib/libclang.dylib"

        if {${os.major} > 9} {
            # http://trac.macports.org/ticket/38527
            system "install_name_tool -id ${sub_prefix}/lib/clang/${llvm_version}/lib/darwin/libclang_rt.asan_osx_dynamic.dylib ${destroot}${sub_prefix}/lib/clang/${llvm_version}/lib/darwin/libclang_rt.asan_osx_dynamic.dylib"
        }
    }
}

if {${subport} eq "llvm-${llvm_version}"} {
    variant ocaml description {Enable generation of OCaml binding} {
        depends_lib-append   port:ocaml

        configure.args-delete --enable-bindings=none
        configure.args-append --enable-bindings=ocaml

        destroot.args-append  OVERRIDE_libdir=${sub_prefix}/lib
    }
} elseif {${subport} eq "clang-${llvm_version}"} {
    variant arm_runtime description {Build and install the arm runtime for iOS development (requires iOS SDK)} {}

    # The iOS SDK was shipped with all Xcode releases after 4.0
    if {[info exists xcodeversion] && [vercmp "4.0" ${xcodeversion}] <= 0} {
       default_variants-append +arm_runtime
    }

    variant analyzer description {Install clang static analyzer} {

        depends_run-append port:perl5

        post-patch {
            reinplace "s|/usr/bin/env perl|${prefix}/bin/perl5|g" \
                ${worksrcpath}/tools/clang/tools/scan-build/ccc-analyzer \
                ${worksrcpath}/tools/clang/tools/scan-build/c++-analyzer \
                ${worksrcpath}/tools/clang/tools/scan-build/scan-build
            reinplace "s|/usr/bin/env python|${pythonfullpath}|g" \
                ${worksrcpath}/tools/clang/tools/scan-build/set-xcode-analyzer \
                ${worksrcpath}/tools/clang/tools/scan-view/scan-view
        }

        post-destroot {
            file mkdir ${destroot}${sub_prefix}/libexec
            file copy ${worksrcpath}/tools/clang/tools/scan-build ${destroot}${sub_prefix}/libexec/scan-build
            file copy ${worksrcpath}/tools/clang/tools/scan-view ${destroot}${sub_prefix}/libexec/scan-view

            file delete -force ${destroot}${sub_prefix}/libexec/scan-build/.svn
            file delete -force ${destroot}${sub_prefix}/libexec/scan-view/.svn
            file delete -force ${destroot}${sub_prefix}/libexec/scan-view/Resources/.svn

            ln -s ${sub_prefix}/libexec/scan-build/scan-build ${destroot}${sub_prefix}/bin/scan-build
            ln -s ${sub_prefix}/libexec/scan-view/scan-view ${destroot}${sub_prefix}/bin/scan-view
            ln -s ${sub_prefix}/bin ${destroot}${sub_prefix}/libexec/scan-build/bin

            xinstall -m 755 "${filespath}/llvm-bin" "${destroot}${prefix}/bin/scan-build-${suffix}"
            reinplace "s:EXEC_PATH:${sub_prefix}/bin/scan-build:" "${destroot}${prefix}/bin/scan-build-${suffix}"

            xinstall -m 755 "${filespath}/llvm-bin" "${destroot}${prefix}/bin/scan-view-${suffix}"
            reinplace "s:EXEC_PATH:${sub_prefix}/bin/scan-view:" "${destroot}${prefix}/bin/scan-view-${suffix}"
        }
    }

    if {${os.platform} eq "darwin" && ${os.major} > 15} {
        depends_build
        depends_lib
        depends_run
        archive_sites
        known_fail yes
        pre-fetch {
            ui_error "${subport} is not supported on macOS Sierra or newer."
            return -code error {unsupported platform}
        }
    }
}

livecheck.type          none
