# $Id: Portfile 80438 2011-07-13 05:52:35Z jeremyhu@macports.org $

PortSystem 1.0
PortGroup select 1.0

name			apple-gcc42
version			5666.3
set gcc_version		4.2.1
revision		1
categories		lang
platforms		darwin
maintainers		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, 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:
# gmp and mpfr are not universal
#universal_variant       no
#depends_lib             port:gmp port:mpfr port:libiconv

depends_run             port:gcc_select

use_configure no

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

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

# system-libstdc++.patch   : Adjust build system to use libstdc++ from / since we don't have it in MacPorts
# no-rm-system.patch       : Comment out a rogue rm /usr/... in the build script
# prefix.patch             : Fix hardcoded instances of /usr
# suffix.patch             : Make binaries end in -apple-4.2
# stage1-cc.patch          : Use $CC for our stage1 compiler
# other_langs.patch        : Add support for gcj and gfortran to build_gcc
# makeinfo.patch           : Fix version detection for makeinfo in configure scripts
# werror-*.patch           : Fix compilation errors when building for x86_64
patchfiles \
	system-libstdc++.patch \
	no-rm-system.patch \
	prefix.patch \
	suffix.patch \
	makeinfo.patch \
	stage1-cc.patch \
	other_langs.patch \
	werror-c-incpath.patch \
	werror-local-alloc.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-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

	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
	}

	# Disable -Werror for our bootstrap gcc
	if {${configure.compiler} == "clang" ||
	    ${configure.compiler} == "macports-clang"} {
		reinplace "s:--enable-werror : :" ${worksrcpath}/build_gcc
	}
}

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

if {[variant_isset universal]} {
	build.args-append \
		RC_ARCHS="${universal_archs}"
} else {
	build.args-append \
		RC_ARCHS="${build_arch}"
}

set build_targets {i386}

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

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