# $Id: Portfile 114328 2013-12-05 10:21:52Z ryandesign@macports.org $

PortSystem 1.0
PortGroup select 1.0

name			gcc42
version			4.2.4
revision		9
platforms		darwin
categories		lang
maintainers		mww openmaintainer
# an exception in the license allows dependents to not be GPL
license			{GPL-3+ GPL-2+ Permissive}
description		The GNU compiler collection
long_description	The GNU compiler collection, including front ends for \
			C, C++, Objective-C, Objective-C++, Java and Fortran95.

homepage		http://gcc.gnu.org/
master_sites		ftp://ftp.funet.fi/pub/mirrors/sources.redhat.com/pub/gcc/releases/gcc-${version}/ \
			ftp://ftp.gwdg.de/pub/linux/gcc/releases/gcc-${version}/ \
			ftp://gcc.ftp.nluug.nl/mirror/languages/gcc/releases/gcc-${version}/ \
			ftp://gcc.gnu.org/pub/gcc/releases/gcc-${version}/ \
			gnu:gcc/gcc-${version}

set dcore		gcc-core-${version}.tar.bz2
set dfort		gcc-fortran-${version}.tar.bz2
set dcxx		gcc-g++-${version}.tar.bz2
set djava		gcc-java-${version}.tar.bz2
set dobjc		gcc-objc-${version}.tar.bz2
distfiles		${dcore} ${dfort} ${dcxx} ${djava} ${dobjc}

checksums		${dcore} sha1 540fa6949415ded321a7cbe80bbf8befe85e3566 \
			${dfort} sha1 7b476983f8b8405a456b30704cd91c7b238381c9 \
			${dcxx} sha1 8b0ed65c5314e0a598a120a841cd00eedd183760 \
			${djava} sha1 ce9587329585c35f870157f3fc937c0dd746b4f3 \
			${dobjc} sha1 5e14e4960b2a0bf74dd867fe71071429b2631406

use_bzip2		yes

# gmp and mpfr are not universal
universal_variant	no

depends_lib		port:gmp port:mpfr port:libiconv path:lib/libgcc/libgcc_s.1.dylib:libgcc
depends_run		port:gcc_select port:ld64 port:cctools port:libgcc45

depends_skip_archcheck-append gcc_select ld64 cctools
license_noconflict  gmp mpfr

set major		4.2

worksrcdir		gcc-${version}

patch {
	# fix names of infopages
	foreach f { cpp cppinternals gcc gccint} {
		reinplace "s|${f}.info|${f}-mp-${version}.info|g" ${worksrcpath}/gcc/doc/${f}.texi
	}
}

# the generated compiler doesn't accept -arch
configure.cc-append [get_canonical_archflags]
configure.cc_archflags
configure.cxx_archflags
configure.objc_archflags
configure.ld_archflags
platform darwin {
    if {${os.major} >= 9} {
        depends_lib
        depends_run
        pre-fetch {
            ui_error "$name does not build on Leopard or later."
            error "unsupported platform"
        }
    }
    if {${configure.build_arch} eq "x86_64" || ${configure.build_arch} eq "ppc64"} {
        configure.pre_args-append --build=${configure.build_arch}-apple-${os.platform}${os.version} \
                                  --host=${configure.build_arch}-apple-${os.platform}${os.version}
    }
}

pre-configure {
	file mkdir ${configure.dir}
}

configure.dir		${workpath}/build
configure.cmd		${worksrcpath}/configure
configure.args		--enable-languages=c,c++,java,objc,obj-c++,fortran \
			--libdir=${prefix}/lib/${name} \
			--includedir=${prefix}/include/${name} \
			--infodir=${prefix}/share/info \
			--mandir=${prefix}/share/man \
			--with-local-prefix=${prefix} \
			--with-system-zlib \
			--disable-nls \
			--disable-multilib \
			--program-suffix=-mp-${major} \
			--with-gxx-include-dir=${prefix}/include/${name}/c++/ \
			--with-gmp=${prefix} \
			--with-mpfr=${prefix} \
			--with-as=${prefix}/bin/as \
			--with-ld=${prefix}/bin/ld \
			--with-ar=${prefix}/bin/ar
 
configure.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


build.dir		${configure.dir}
use_parallel_build	yes

destroot.target		install install-info-host

post-destroot {
    file delete ${destroot}${prefix}/share/info/dir

    foreach file [glob ${destroot}${prefix}/share/{info,man/man7}/*] {
        set extension [file extension ${file}]
        set newfile [regsub "${extension}$" ${file} "-mp-${major}${extension}"]
        
        file rename ${file} ${newfile}
    }

    foreach dylib {libgcc_ext.10.4.dylib libgcc_ext.10.5.dylib libgcc_s.1.dylib libgfortran.3.dylib libstdc++.6.dylib libobjc-gnu.2.dylib libgomp.1.dylib libssp.0.dylib} {
        # Different OS versions (eg: Leopard) or architectures (eg: PPC) don't produce all the dylibs
        # https://trac.macports.org/ticket/40098
        # https://trac.macports.org/ticket/40100
        if {[file exists ${destroot}${prefix}/lib/${name}/${dylib}]} {
            delete ${destroot}${prefix}/lib/${name}/${dylib}
            ln -s ${prefix}/lib/libgcc/${dylib} ${destroot}${prefix}/lib/${name}/${dylib}
        }

        if {[variant_isset universal]} {
            foreach archdir [glob ${destroot}${prefix}/lib/${name}/*/] {
                if {[file exists ${archdir}/${dylib}]} {
                    delete ${archdir}/${dylib}
                    ln -s ${prefix}/lib/libgcc/${dylib} ${archdir}/${dylib}
                }
            }
        }
    }
}

post-destroot {
	# install/copy ffitarget.h only if we have it
	if {![catch {set ffitarget.h [glob ${destroot}${prefix}/lib/${name}/gcc/*/${version}/include/ffitarget.h]} result]} {
		file copy ${ffitarget.h} ${destroot}${prefix}/include/${name}/
	}
}

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

livecheck.type	regex
livecheck.url	http://gcc.gnu.org/gcc-4.2/
livecheck.regex	GCC (4\\.2\\.\[0-9\])
