# $Id: Portfile,v 1.4 2006/06/07 00:18:32 landonf Exp $
PortSystem			1.0

name			i386-mingw32-gcc
version			3.4.5-20060117-1
set gnu_version		3.4.5
maintainers		landonf@opendarwin.org
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++-${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 b72bffc6e7d5c7d1de099ad76ef0d45d \
			gcc-g++-${version}-src.tar.gz md5 42c92b923adfe4c8943f05c326af1736 \
			gcc-g77-${version}-src.tar.gz md5 37e2558839f70ff2dd8c16c359e5e327 \
			gcc-java-${version}-src.tar.gz md5 30e9d5984ca587f378d343eb455f1ce9 \
			gcc-objc-${version}-src.tar.gz md5 d22f92c5c4ffe9b1d2e0244be95db3ba
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-minw32-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}/

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

}

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