# $Id$

PortSystem 1.0
PortGroup select 1.0
PortGroup compiler_blacklist_versions 1.0

name                    llvm-gcc42
version                 2336.11
revision                3
set gcc_version         4.2.1
categories              lang
platforms               darwin
license                 {GPL-2+ Permissive}
maintainers             jeremyhu
description             llvm-gcc42 is a gcc compiler frontend for llvm
long_description        llvm-gcc42 integrates the LLVM optimizers and code \
                        generator with the GCC 4.2 parser. This allows LLVM to \
                        compile languages supported by the GCC compiler \
                        frontends, and provides high-fidelity drop-in \
                        compatibility with version 4.2 of GCC.

homepage                http://opensource.apple.com/
master_sites            http://opensource.apple.com/tarballs/llvmgcc42/

distname                llvmgcc42-${version}

checksums               md5     046629f7b3ce59bcb32b5116d29c27cd \
                        sha1    97b82b328780d1b4f261301dc084bb256172ee2a \
                        rmd160  90e496dc075ebd3daa088f818e52e7ecbb4a182e

depends_lib             port:ld64 port:cctools
depends_run             port:gcc_select
depends_skip_archcheck  ld64 cctools

use_configure no
variant universal {}

supported_archs i386 x86_64

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

select.group    gcc
select.file     ${filespath}/mp-llvm-gcc42

set srcroot ${worksrcpath}
set objroot ${workpath}/objroot
set symroot ${workpath}/symroot

# Don't compile with self
compiler.blacklist-append macports-llvm-gcc-4.2

# werror-*.patch                 : Fix compilation errors when building for x86_64
# incpath.patch                  : Don't prepend the -isysroot option argument to the compiler's own search paths
# driverdriver-num_infiles.patch : Fix linking with no input files (ie all static archives passed with -l...)
# driverdriver-armcheck.patch    : Build fix for the driverdriver when arm support is not available (eg: Tiger)
# deployment_target.patch        : Build fix to correctly get the OS version
# remove-kernel-version-check.patch : Don't complain on Yosemite and later
# version-min.patch              : Interpret OS X and iOS versions correctly
# autogen.patch                  : Update to work with modern autogen (necessary to create fixincludes.patch)
# fixincludes.patch              : Stub out unrecognized macros in system headers

patchfiles-append \
	werror-local-alloc.patch \
	incpath.patch \
	driverdriver-num_infiles.patch \
	driverdriver-armcheck.patch \
	deployment_target.patch \
	remove-kernel-version-check.patch \
	version-min.patch \
	autogen.patch \
	fixincludes.patch

if {[string match *clang* ${configure.cc}]} {
    patchfiles-append clang-optimization.patch
}

post-extract {
    xinstall -W ${filespath} build_gcc ${worksrcpath}
}

post-patch {
    # Set our deployment target
    reinplace "s/@@DEPLOYMENT_TARGET@@/${macosx_deployment_target}/" \
        ${worksrcpath}/llvmCore/Makefile.rules

    # Install documentation in a MacPorts location
    #reinplace "/^HTMLDIR=/ s:=.*$:=\"${prefix}/share/doc/${name}/html\":" ${worksrcpath}/build_gcc

    # arch returns i386 even when we want x86_64
    reinplace "/^BUILD=/ s:arch:echo ${build_arch}:" ${worksrcpath}/build_gcc

    # This is set to isysroot/Developer/SDKs/MacOSX10.5.sdk mmacosx-version-min=10.5
    # which is not always right.  Our CFLAGS should take care of this instead
    reinplace "/^MULTILIB_EXTRA_OPTS/d" ${worksrcpath}/gcc/config/rs6000/t-darwin

    # Brand our compiler
    reinplace "/VERSUFFIX/s/)\"/) (MacPorts ${name} ${version}_${revision}${portvariants})\"/" \
        ${worksrcpath}/gcc/version.c
    reinplace "/bug_report_url/s|http:.*>|https://trac.macports.org/newticket>|" \
        ${worksrcpath}/gcc/version.c

    # Don't use the DEVELOPER_DIR variable
    reinplace "s/DEVELOPER_DIR/MP_DEVELOPER_DIR/g" \
        ${worksrcpath}/GNUmakefile \
        ${worksrcpath}/llvmCore/utils/buildit/build_llvm

    if {${build_arch} eq "ppc"} {
        reinplace "/^PPC_SYSROOT=/ s:=.*$:=/:" ${worksrcpath}/build_gcc
    } elseif {[file exists ${developer_dir}/SDKs/MacOSX10.5.sdk]} {
        reinplace "/^PPC_SYSROOT=/ s:=.*$:=${developer_dir}/SDKs/MacOSX10.5.sdk:" ${worksrcpath}/build_gcc
    } elseif {[file exists ${developer_dir}/SDKs/MacOSX10.4u.sdk]} {
        reinplace "/^PPC_SYSROOT=/ s:=.*$:=${developer_dir}/SDKs/MacOSX10.4u.sdk:" ${worksrcpath}/build_gcc
    } elseif {[file exists ${developer_dir}/SDKs/MacOSX10.3.9.sdk]} {
        reinplace "/^PPC_SYSROOT=/ s:=.*$:=${developer_dir}/SDKs/MacOSX10.3.9.sdk:" ${worksrcpath}/build_gcc
    }

    if {${os.major} < 9} {
        reinplace "/vproc.h/d" ${worksrcpath}/gcc/libgcov.c
    }

    # Disable -Werror for our bootstrap gcc
    if {[string match "*clang*" ${configure.compiler}]} {
        reinplace "s:--enable-werror : :" ${worksrcpath}/build_gcc
    }

    # ltmain.sh notes 'On all known operating systems, these are identical', and -module has gone
    # away with Apple's clang-3.4
    if {[string match *clang* ${configure.cc}]} {
         reinplace "s:module:export_dynamic:" ${worksrcpath}/llvmCore/Makefile.rules
         reinplace "s:-no-undefined::" ${worksrcpath}/llvmCore/tools/lto/Makefile
    }
}

compiler.cpath /usr/include
compiler.library_path /usr/lib

# TODO: Better use of MacPorts dependencies
build.env \
    LIBRARY_PATH="/usr/lib" \
    CPATH="/usr/include" \
    PATH="/bin:/usr/bin:/sbin:/usr/sbin" \
    LANGUAGES="c,c++,objc,obj-c++" \
    AR_FOR_TARGET=${prefix}/bin/ar \
    AS_FOR_TARGET=${prefix}/bin/as \
    LD_FOR_TARGET=${prefix}/bin/ld \
    NM_FOR_TARGET=${prefix}/bin/nm \
    OBJDUMP_FOR_TARGET=${prefix}/bin/objdump \
    RANLIB_FOR_TARGET=${prefix}/bin/ranlib \
    STRIP_FOR_TARGET=${prefix}/bin/strip \
    DSYMUTIL_FOR_HOST=${prefix}/bin/dsymutil \
    RANLIB_FOR_HOST=${prefix}/bin/ranlib \
    STRIP_FOR_HOST=${prefix}/bin/strip \
    OTOOL=${prefix}/bin/otool \
    OTOOL64=${prefix}/bin/otool

build.args-append \
    CC="${configure.cc} -pipe -std=gnu89" \
    CXX="${configure.cxx} -pipe" \
    PREFIX="${prefix}" \
    SRCROOT="${srcroot}" \
    OBJROOT="${objroot}" \
    SYMROOT="${symroot}" \
    DSTROOT="${destroot}" \
    RC_NONARCH_CFLAGS="-pipe -std=gnu89" \
    RC_OS="macos" \
    RC_ARCHS="[get_canonical_archs]" \
    RC_ProjectSourceVersion="[lindex [split ${version} .] 0]" \
    RC_ProjectSourceSubversion="[lindex [split ${version} .] 1]"

if {[string match "*clang*" ${configure.compiler}]} {
    # Build fails using libc++

    build.args-delete \
        CXX="${configure.cxx} -pipe"

    build.args-append \
        CXX="${configure.cxx} -pipe -stdlib=libstdc++"
}

set build_targets {}

if {${os.major} > 8 || ${os.arch} eq "i386"} {
    lappend build_targets i386
}

# Support for PPC was dropped in Darwin 11 (Mac OS X 10.7)
#if {${os.major} < 11} {
#    if {[file exists ${developer_dir}/SDKs/MacOSX10.3.9.sdk] ||
#            [file exists ${developer_dir}/SDKs/MacOSX10.4u.sdk] ||
#            [file exists ${developer_dir}/SDKs/MacOSX10.5.sdk] ||
#            ${build_arch} eq "ppc"} {
#        lappend build_targets ppc
#    }
#}
if {${os.major} < 11 && ${build_arch} eq "ppc"} {
    lappend build_targets ppc
}

build.args-append \
    TARGETS="${build_targets}"

# Yes, use "install" ... the build system does make/install in one go
# TODO: split the build_gcc script into two to better match MacPorts
build.target install
destroot {}
post-destroot {
    if {[string match "*i386*" ${build_targets}]} {
        delete ${destroot}${prefix}/libexec/${name}/gcc/i686-apple-darwin${os.major}/${gcc_version}/as
        ln -s ../../../../../bin/as ${destroot}${prefix}/libexec/${name}/gcc/i686-apple-darwin${os.major}/${gcc_version}/as
        delete ${destroot}${prefix}/libexec/${name}/gcc/i686-apple-darwin${os.major}/${gcc_version}/ld
        ln -s ../../../../../bin/ld ${destroot}${prefix}/libexec/${name}/gcc/i686-apple-darwin${os.major}/${gcc_version}/ld
        delete ${destroot}${prefix}/libexec/${name}/gcc/i686-apple-darwin${os.major}/${gcc_version}/dsymutil

        # http://trac.macports.org/ticket/35770
        delete  ${destroot}${prefix}/lib/${name}/gcc/i686-apple-darwin${os.major}/${gcc_version}/libstdc++.dylib
        ln -s /usr/lib/libstdc++.6.dylib ${destroot}${prefix}/lib/${name}/gcc/i686-apple-darwin${os.major}/${gcc_version}/libstdc++.dylib
    }

    if {[string match "*ppc*" ${build_targets}]} {
        delete ${destroot}${prefix}/libexec/${name}/gcc/powerpc-apple-darwin${os.major}/${gcc_version}/as
        ln -s ../../../../../bin/as ${destroot}${prefix}/libexec/${name}/gcc/powerpc-apple-darwin${os.major}/${gcc_version}/as
        delete ${destroot}${prefix}/libexec/${name}/gcc/powerpc-apple-darwin${os.major}/${gcc_version}/ld
        ln -s ../../../../../bin/ld ${destroot}${prefix}/libexec/${name}/gcc/powerpc-apple-darwin${os.major}/${gcc_version}/ld
        delete ${destroot}${prefix}/libexec/${name}/gcc/powerpc-apple-darwin${os.major}/${gcc_version}/dsymutil

        # http://trac.macports.org/ticket/35770
        delete  ${destroot}${prefix}/lib/${name}/gcc/powerpc-apple-darwin${os.major}/${gcc_version}/libstdc++.dylib
        ln -s /usr/lib/libstdc++.6.dylib ${destroot}${prefix}/lib/${name}/gcc/powerpc-apple-darwin${os.major}/${gcc_version}/libstdc++.dylib
    }
}

livecheck.type		regex
livecheck.url		[lindex ${master_sites} 0]
livecheck.regex		llvmgcc42-(\\d+(?:\\.\\d+)*)\\.tar