# $Id: Portfile 92393 2012-04-27 18:13:04Z jeremyhu@macports.org $

PortSystem 1.0
PortGroup select 1.0

name			apple-gcc42
version			5666.3
set gcc_version		4.2.1
revision		4
categories		lang
platforms		darwin
license         GPL-2
maintainers		jeremyhu openmaintainer
installs_libs	no

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, anc C++

homepage		http://opensource.apple.com/
master_sites		gnu:/gcc/gcc-4.2.1: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-5666.3.tar.gz \
                    md5     188c7769db5c898922b0976fab148a63 \
                    sha1    292a0cfcfdc061cb083658efd9a3812a120a1f50 \
                    rmd160  a01d000f89c0e89dd0079dcd202bba7629ba78dc

#                    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

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

depends_run             port:gcc_select

use_configure no
variant universal {}

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

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

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

# makeinfo.patch                 : Fix version detection for makeinfo in configure scripts
# 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)

patchfiles \
	makeinfo.patch \
	werror-c-incpath.patch \
	werror-local-alloc.patch \
	incpath.patch \
	driverdriver-num_infiles.patch \
	driverdriver-armcheck.patch

# 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 {
	file copy -force ${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

	if {${build_arch} == "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 {${configure.compiler} == "clang" ||
	    ${configure.compiler} == "macports-clang"} {
		reinplace "s:--enable-werror : :" ${worksrcpath}/build_gcc
	}
}

compiler.cpath

# TODO: Use MacPorts dependencies
build.env \
	LIBRARY_PATH="/usr/lib" \
	CPATH="/usr/include" \
	PATH="/bin:/usr/bin:/sbin:/usr/sbin" \
	LANGUAGES="c,c++,objc,obj-c++"

#	MAKEINFO=${prefix}/bin/makeinfo

build.args-append \
	CC="${configure.cc} -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]"

set build_targets {}

if {${os.major} > 8 || ${os.arch} == "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} == "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}/4.2.1/as
        ln -s ../../../../../bin/as ${destroot}${prefix}/libexec/${name}/gcc/i686-apple-darwin${os.major}/4.2.1/as
        delete ${destroot}${prefix}/libexec/${name}/gcc/i686-apple-darwin${os.major}/4.2.1/ld
        ln -s ../../../../../bin/ld ${destroot}${prefix}/libexec/${name}/gcc/i686-apple-darwin${os.major}/4.2.1/ld
    }

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

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