# $Id: Portfile 50984 2009-05-14 21:08:43Z and.damore@macports.org $

PortSystem			1.0
name				avr-binutils
version				2.16.1

# Parameters for this port.
set crossgcc-target avr

description			FSF Binutils for avr cross development
long_description	Free Software Foundation development toolchain ("binutils") for \
					avr cross development.
platforms			darwin
categories			cross devel
maintainers			kallisys.net:pguyot gmail.com:metamagix
homepage			http://www.gnu.org/software/binutils/binutils.html
master_sites		gnu:binutils
distname			binutils-${version}
use_bzip2			yes
checksums			md5 6a9d529efb285071dad10e1f3d2b2967 \
					sha1 5c80fd5657da47efc16a63fdd93ef7395319fbbf \
					rmd160 638861a6b709ac380f1bc13d55c6b48d0532b3d0

# All cross ports violate the mtree layout.
destroot.violate_mtree	yes

# Download everything to binutils/
dist_subdir			binutils

# Patches
patchfiles		patch-bfd-Makefile.am.diff \
				patch-bfd-Makefile.in.diff \
				patch-bfd-coff-avr.c.diff \
				patch-bfd-coff-ext-avr.c.diff \
				patch-bfd-coffcode.h.diff \
				patch-bfd-coffgen.c.diff \
				patch-bfd-coffswap.h.diff \
				patch-bfd-config.bfd.diff \
				patch-bfd-configure.diff \
				patch-bfd-configure.in.diff \
				patch-bfd-targets.c.diff \
				patch-binutils-Makefile.am.diff \
				patch-binutils-Makefile.in.diff \
				patch-binutils-bucomm.c.diff \
				patch-binutils-bucomm.h.diff \
				patch-binutils-budbg.h.diff \
				patch-binutils-debug.c.diff \
				patch-binutils-debug.h.diff \
				patch-binutils-doc-objcopy.1.diff \
				patch-binutils-objcopy.c.diff \
				patch-binutils-rdcoff.c.diff \
				patch-binutils-wrcoff.c.diff \
				patch-gas-config-tc-avr.c.diff \
				patch-include-coff-avr.h.diff \
				patch-include-coff-internal.h.diff \
				patch-include-opcode-avr.h.diff \
				patch-libiberty-Makefile.diff \
				patch-opcodes-Makefile.diff 

# 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   	${worksrcpath}/configure
configure.cc    	"cc -no-cpp-precomp"
configure.args  	--infodir='${prefix}/share/info' --target=${crossgcc-target} \
					--program-prefix=${crossgcc-target}- --mandir='${prefix}/share/man'
build.dir       	${workpath}/build
					
# We don't want the etc module.
post-extract {
	system "rm -rf ${worksrcpath}/etc"
}

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|--info-dir=\$(DESTDIR)\$(infodir)|--dir-file=\$(DESTDIR)\$(infodir)/${crossgcc-target}-dir|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"
		}
	}

	# gas/doc/as.texinfo
	crossgcc::fixinfo gas/doc/ gas/doc/Makefile.in as texinfo

	# bfd/doc/bfd.texinfo
	crossgcc::fixinfo bfd/doc/ bfd/doc/Makefile.in bfd texinfo

	# binutils/doc/binutils.texi
	crossgcc::fixinfo binutils/doc/ binutils/doc/Makefile.in binutils texi

	# gprof/gprof.texi
	crossgcc::fixinfo gprof/ gprof/Makefile.in gprof texi

	# ld/ld.texinfo
	crossgcc::fixinfo ld/ ld/Makefile.in ld texinfo
	
	# gettext stuff.
	crossgcc::fixgettext bfd
	crossgcc::fixgettext binutils
	crossgcc::fixgettext gas
	crossgcc::fixgettext gprof
	crossgcc::fixgettext ld
	crossgcc::fixgettext opcodes
}

post-destroot {
	# Installing (host) libiberty was a mistake.
	file delete "${destroot}/${prefix}/lib/libiberty.a"
}

# livecheck.
# This release is equivalent to FreeBSD 2.16.1_5
# It doesn't make much sense to compare to binutils release.
livecheck.regex				">$name-(.*?)<"
livecheck.url				"http://www.freebsd.org/cgi/ports.cgi?query=${name}&stype=all"
livecheck.version			2.16.1_5
