# $Id: Portfile 61621 2009-12-16 22:55:31Z jmr@macports.org $
PortSystem			1.0

name			i386-mingw32-gcc
version			3.4.5-20060117-2
revision		1
set gnu_version		3.4.5
maintainers		landonf
description		Mingw32 cross-compiler for i386-Win32
long_description	Mingw32 GNU Compiler for i386-Win32. \
			Supports C, C++, Objective-C and Java.
homepage		http://www.mingw.org
platforms       	darwin
categories      	cross devel

# Parameters for this port.
set crossgcc-target i386-mingw32

master_sites		sourceforge:mingw
distfiles		gcc-core-${version}-src.tar.gz \
			gcc-g%2B%2B-${version}-src.tar.gz \
			gcc-g77-${version}-src.tar.gz \
			gcc-java-${version}-src.tar.gz \
			gcc-objc-${version}-src.tar.gz

checksums		gcc-core-${version}-src.tar.gz \
				md5     d0dc9d57d493889bc1ca88b127290b9d \
				sha1    75d59049f26dc62f3732d11abbf056efc7bc8e95 \
				rmd160  624dd55ac00fba15269f1b3b3bbd6c383cc20827 \
			gcc-g%2B%2B-${version}-src.tar.gz \
				md5     ca81c8420ab9d1d05ff9c73a9d4f7d63 \
				sha1    edafa412d86085cf39f0db1c19a6ea2890d05946 \
				rmd160  7e3341c6ef1494462dfd3f682ad4cc6871b73e98 \
			gcc-g77-${version}-src.tar.gz \
				md5     57d489658743f0d9048e440b312740c8 \
				sha1    156baeb2bd0dbbd15ed397b0a609e7e7cdbdab4b \
				rmd160  3c0ac52b0c18712a29219803c95399dfce53e461 \
			gcc-java-${version}-src.tar.gz \
				md5     96b039f4e11bff4d3d8a4e7da6e3998d \
				sha1    a9152dd7ffa35fc028fcca369a729ec2bbad55e7 \
				rmd160  4971ce8ffa01292567f36983101d4b99901da50d \
			gcc-objc-${version}-src.tar.gz \
				md5     2bffc1e314f2efb1034b3d3ea1ce2f7d \
				sha1    c96f1531d9d73e9279e0b6faf085deec5c2bda7b \
				rmd160  a02ad94da674760799ec945c7ff75ed99f9e8c3a
worksrcdir		gcc-${version}

# Patches for infodir
patchfiles		patch-gcc-Makefile.in \
			patch-fastjar-Makefile.in

depends_lib		port:i386-mingw32-w32api \
			port:i386-mingw32-runtime \
			port:i386-mingw32-binutils \
			port:gettext

# Build in a different directory, as advised in the README file.
pre-configure	{ system "cd ${workpath} && mkdir -p build" }
configure.dir	${workpath}/build
configure.cmd	${workpath}/gcc-${version}/configure
configure.args	--infodir='${prefix}/share/info' \
		--mandir='${prefix}/share/man' \
		--target=${crossgcc-target} \
		--without-newlib \
		--disable-nls \
		--with-gnu-as \
		--with-gnu-ld \
		--disable-multilib \
		--with-gxx-include-dir=${prefix}/${crossgcc-target}/include/c++/${gnu_version}/ \
		--enable-sjlj-exceptions
if {$build_arch != ""} {
    configure.cc_archflags
    configure.cxx_archflags
    configure.objc_archflags
    if {${os.platform} == "darwin"} {
        configure.args-append --build=${build_arch}-apple-darwin${os.major} \
                              --host=${build_arch}-apple-darwin${os.major}
    }
}

build.dir       ${workpath}/build

post-patch {
	namespace eval crossgcc {}

	# Fix the info pages and related stuff.
	#	
	# path: path to the doc directory (e.g. gas/doc/)
	# makefile: path to Makefile.in (e.g. gas/doc/Makefile.in)
	# name: name of the info page (e.g. as)
	# suffix: suffix of the souce page (texinfo or texi)
	proc crossgcc::fixinfo { path makefile name suffix } {
		global crossgcc-target worksrcpath

		# Fix the source
		reinplace "s|setfilename ${name}.info|setfilename ${crossgcc-target}-${name}.info|g" \
			${worksrcpath}/${path}/${name}.${suffix}
		reinplace "s|(${name})|(${crossgcc-target}-${name})|g" \
			${worksrcpath}/${path}/${name}.${suffix}
		reinplace "s|@file{${name}}|@file{${crossgcc-target}-${name}}|g" \
			${worksrcpath}/${path}/${name}.${suffix}
		
		# Fix the Makefile
		reinplace "s|${name}.info|${crossgcc-target}-${name}.info|g" \
			${worksrcpath}/${makefile}
		reinplace "s|${name}.${suffix}|${crossgcc-target}-${name}.${suffix}|g" \
			${worksrcpath}/${makefile}
		
		# Rename the source
		file rename ${worksrcpath}/${path}/${name}.${suffix} \
			${worksrcpath}/${path}/${crossgcc-target}-${name}.${suffix}
		
		# Fix install-info's dir.
		# (note: this may be effectless if there was no info dir to be fixed)
		reinplace "s|__TARGET|${crossgcc-target}|g" \
			"${worksrcpath}/${makefile}"
	}

	# Fix the gettext files and related stuff.
	#	
	# module: name of the module (e.g. gas)
	proc crossgcc::fixgettext { module } {
		global crossgcc-target worksrcpath

		if { [ file exists "${worksrcpath}/${module}/Makefile.in" ] } {
			reinplace "s|@PACKAGE@|${crossgcc-target}-@PACKAGE@|g" \
				"${worksrcpath}/${module}/Makefile.in"
		}
		if { [ file exists "${worksrcpath}/${module}/doc/Makefile.in" ] } {
			reinplace "s|@PACKAGE@|${crossgcc-target}-@PACKAGE@|g" \
				"${worksrcpath}/${module}/doc/Makefile.in"
		}
		if { [ file exists "${worksrcpath}/${module}/po/Make-in" ] } {
			reinplace "s|@PACKAGE@|${crossgcc-target}-@PACKAGE@|g" \
				"${worksrcpath}/${module}/po/Make-in"
		}
	}

	# gcc/doc/cpp.texi
	crossgcc::fixinfo gcc/doc/ gcc/Makefile.in cpp texi

	# gcc/doc/cppinternals.texi
	crossgcc::fixinfo gcc/doc/ gcc/Makefile.in cppinternals texi

	# gcc/doc/gcc.texi
	crossgcc::fixinfo gcc/doc/ gcc/Makefile.in gcc texi
	# XXX: Gross hack
	# Clean up the mess in gcc/Makefile.in
	reinplace s|lib${crossgcc-target}-gcc.texi|libgcc.texi|g ${worksrcpath}/gcc/Makefile.in

	# gcc/doc/gccint.texi
	crossgcc::fixinfo gcc/doc/ gcc/Makefile.in gccint texi

	# gcc/f/g77.texi
	crossgcc::fixinfo gcc/f/ gcc/f/Make-lang.in g77 texi

	# gcc/java/gcj.texi
	crossgcc::fixinfo gcc/java/ gcc/java/Make-lang.in gcj texi

	# fastjar/fastjar.texi
	crossgcc::fixinfo fastjar/ fastjar/Makefile.in fastjar texi
	
	# gettext stuff.
	crossgcc::fixgettext gcc
	crossgcc::fixgettext fastjar

}

destroot.violate_mtree	yes
post-destroot {
	# Stuff I don't want (either because they're in the system
	# or because they would conflict with other FSF ports)
	# (it's easier for maintainability purposes to fix things here)

	# aliases for locales (should be on the system)
	file delete "${destroot}/${prefix}/share/locale/locale.alias"
	
	# FSF propaganda (should already be there or would conflict)
	file delete -force "${destroot}/${prefix}/share/man/man7"

	# (host) libiberty
	file delete "${destroot}/${prefix}/lib/libiberty.a"

	# aliases for charsets (should already be there)
	file delete "${destroot}/${prefix}/lib/charset.alias"
	
	# Remove man pages for tools that are not built as part of cross-gcc
	file delete "${destroot}/${prefix}/share/man/man1/rmic.1"
	file delete "${destroot}/${prefix}/share/man/man1/rmiregistry.1"
	file delete "${destroot}/${prefix}/share/man/man1/jv-convert.1"
	file delete "${destroot}/${prefix}/share/man/man1/gij.1"

	# There is no need for documentation on installing gcc
	# given that this port just installed gcc.
	file delete "${destroot}/${prefix}/share/info/gccinstall.info"
}
