PortSystem 1.0
PortGroup select 1.0
PortGroup xcodeversion 1.0

name			apple-gcc42
version			5666.3
set gcc_version		4.2.1
revision		15
categories		lang
platforms		darwin
license			{GPL-2+ Permissive}
maintainers		{jeremyhu @jeremyhu} openmaintainer

description		Apple's version of gcc 4.2
long_description	Apple's version of the GNU compiler collection, \
			version 4.2. Supports C, Objective-C, and C++

variant gpl3 description {Merge in changes from gcc-4.2.4} {
	license 	{GPL-3+ Permissive}
	maintainers	googlemail.com:tobias.netzel openmaintainer

	patch_sites-append   macports
	patchfiles-append    gcc-4.2.1-4.2.4-v2.patch gcc-4.2.4-emmintrin.patch
}
if {[variant_isset gpl3]} {
	set gcc_version	4.2.4
}

homepage		http://opensource.apple.com/
master_sites		gnu:/gcc/gcc-${gcc_version}:gnu \
			http://opensource.apple.com/tarballs/gcc/:apple

distname		gcc-${version}

set dcore		${distname}.tar.gz
set dfort		gcc-fortran-${gcc_version}.tar.bz2
set djava		gcc-java-${gcc_version}.tar.bz2

distfiles               ${dcore}:apple
# ${dfort}:gnu ${djava}:gnu

checksums           gcc-4.2.1-4.2.4-v2.patch \
                    rmd160  f33804c6f04e853a6248bf4941f4a0ace1e3f30f \
                    sha256  0f22916b8b1c96bafaccbb17b4350530d4ae903475ae934efe76a26b9c35e03f \
                    gcc-5666.3.tar.gz \
                    rmd160  a01d000f89c0e89dd0079dcd202bba7629ba78dc \
                    sha256  6c46f4376d11ada5be0cfc3894b9296edf574af3c616ab71803ea78edfd7015b

#                    gcc-fortran-4.2.1.tar.bz2 \
#                    md5     2a91d467b50a404ca0cd3b10b413f9b2 \
#                    sha1    37a0466b393ade9291dcec315c69c371e42db7f5 \
#                    rmd160  2475594dad66277fd0c5fe42389fb8fa2665e5ce \
#                    gcc-java-4.2.1.tar.bz2 \
#                    md5     3fc64e96388b939fada65fe3bffb5924 \
#                    sha1    4615c85ef9010304bf412adc2f2c33d256e177fd \
#                    rmd160  bb4b256f7e7779361214593343821a127ee78d1a

#                    gcc-fortran-4.2.4.tar.bz2 \
#                    md5     d8896f33f91eca12135df2c1e5aa0887 \
#                    sha1    ? \
#                    rmd160  ? \
#                    gcc-java-4.2.4.tar.bz2 \
#                    md5     dd05ac72f8563c98af6946f9334668f1 \
#                    sha1    ? \
#                    rmd160  ?

# Needed for gfortran:
#files/other_langs.patch:
#+  --with-gmp=$DEST_ROOT \
#+  --with-mpfr=$DEST_ROOT \
#depends_lib             port:gmp port:mpfr

depends_run             port:gcc_select

use_configure no
if {${os.major} >= 9} {
    universal_variant   yes
    if {${build_arch} ne "ppc" && ${build_arch} ne "ppc64"} {
        default_variants-append +universal
    }
}

configure.cxx_stdlib    libstdc++

minimum_xcodeversions   {8 2.5}

if {${os.platform} eq "darwin" && ${os.major} > 15} {
    depends_lib
    depends_run
    archive_sites
    pre-fetch {
        ui_error "${name} is not supported on OS X versions newer than El Capitan."
        return -code error {unsupported platform}
    }
}

# Building universal with a ppc slice doesn't work (Tried on Leopard)
if {${build_arch} eq "i386" || ${build_arch} eq "x86_64"} {
    supported_archs i386 x86_64
    configure.universal_archs i386 x86_64
} elseif {${build_arch} eq "ppc" || ${build_arch} eq "ppc64"} {
    supported_archs ppc ppc64
    configure.universal_archs ppc ppc64
}

select.group    gcc
select.file     ${filespath}/${name}

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

# 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 (i.e. all static archives passed with -l...)
# driverdriver-armcheck.patch        : Build fix for the driverdriver when arm support is not available (e.g. Tiger)
# tiger-Unwind_GetIPInfo.patch       : https://trac.macports.org/ticket/36226
# remove-kernel-version-check.patch  : Don't complain about Yosemite and later (#45449)
# version-min.patch                  : Interpret OS X and iOS versions correctly (#43978, #45449)
# autogen.patch                      : Update to work with modern AutoGen (necessary to create fixincludes.patch)
# fixincludes.patch                  : Stub out unrecognized macros in system headers (#45449)

patchfiles-append \
	werror-c-incpath.patch \
	werror-local-alloc.patch \
	incpath.patch \
	driverdriver-num_infiles.patch \
	driverdriver-armcheck.patch \
	tiger-Unwind_GetIPInfo.patch \
	remove-kernel-version-check.patch \
	version-min.patch \
	autogen.patch \
	fixincludes.patch \
	patch-gcc-texi.diff

# gcj fails to build:
# gcc/java/lang.c: In function 'java_init':
# gcc/java/lang.c:378: error: 'force_align_functions_log' undeclared (first use in this function)
# gcc/java/lang.c:378: error: (Each undeclared identifier is reported only once
# gcc/java/lang.c:378: error: for each function it appears in.)
#
# gfortrain fails to link due to functionality moving around in core in a way the module doesn't expect

#extract {
#	system "cd ${workpath} && tar xzf ${distpath}/${dfort}"
#	system "cd ${workpath} && tar xzf ${distpath}/${djava}"
#	file rename ${workpath}/gcc-${gcc_version} ${worksrcpath}
#	system "cd ${workpath} && tar xjf ${distpath}/${dcore}"
#}

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

post-patch {
    # 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

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

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

platform darwin 8 {
    if {![file exists ${prefix}/bin/gcc-apple-4.2]} {
        configure.compiler gcc-4.0
        if {![file exists ${prefix}/bin/ld] || ![file exists ${prefix}/bin/as]} {
            default_variants-append +bootstrap
        }
    }
}

build.env \
    PATH=/bin:/usr/bin:/sbin:/usr/sbin \
    LANGUAGES=c,c++,objc,obj-c++

if {![variant_isset bootstrap]} {
    #depends_lib-append              port:libiconv
    depends_lib-append              port:ld64 port:cctools
    depends_skip_archcheck-append   ld64 cctools
    
    # TODO: Better use of MacPorts dependencies
    build.env-append \
        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" \
    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]"

# On Tiger, we need apple-gcc42 to build ld64, which is needed for cctools
variant bootstrap description {Variant to break a dependency cycle on Tiger by first building an apple-gcc42 using host ld and cctools} {
    if {([file exists ${prefix}/bin/ld] && [file exists ${prefix}/bin/as]) || ${os.major} > 8} {
        pre-fetch {
            ui_error "Please install this port without the bootstrap variant."
            error "Please install this port without the bootstrap variant."
        }
    } else {
        notes-append "
Now please reinstall ${name} without the bootstrap variant by running:

sudo port install ${name}

This will ensure that ${name} uses MacPorts versions of cctools and ld64\
which are newer than the ones included with Xcode and will work better\
and prevent some ports from failing to build.
"
    }
}

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)
# Additionally, we dropped support for PPC from MacPorts cctools on SnowLeopard
if {${os.major} < 10} {
    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
    }
}

# TODO: arm?

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

        # 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

        # 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		none