Subject: Proposed improvements to toolchain2netbsd and config.guess
To: Todd Vierling <tv@wasabisystems.com>
From: Frederick Bruckman <fredb@immanent.net>
List: tech-toolchain
Date: 02/14/2002 05:41:27
It was suggested to run "toolchain2netbsd" on m68k, as a fix to
port-m68k/15514. Doing so, I found it really annoying that...

1) It's not quite right -- libbfd/arch/m68k/config.h still defines
VERSION twice. The native compiler lets that slide by, but an i386->
cross build fails.

2) Many files get updated with no change except for the header.

3) It starts out "building for m68k-unknown-netbsdelf1.5AZ, where
history and bsd.own.mk prefers m68k--netbsdelf1.5AZ, leading to more
spurious changes.

So, I finally came up with this. It adds a function to collate two
configs, and uses it on libbfd/.../config.h and gdb/.../config.h.
A side-effect, is that all no-op comment blocks are removed. This
isn't such a bad thing, though, as it'll minimize non-functional
changes even further. It also begins the comparison in writefile() on
the third line, so header-only changes are ignored.

config.guess now omits the vendor portion entirely, on all platforms,
just like ${MACHINE_GNU_PLATFORM} in bsd.own.mk.

With this, all the changes from running toolchain2netbsd are limited
to libbfd and gdb. I verified that libbfd, binutils, and gdb will
build native on mac68k/ELF, and that the whole userland may be
cross-built from 1.5.3_ALPHA/i386. I'm not sure if it would still be
OK on m68k/a.out (probably *not*), so maybe it's not correct to commit
the __result__ just yet, but the I believe the changes to the tools
themselves are still good. Comments?

Frederick

Index: toolchain2netbsd
===================================================================
RCS file: /cvsroot/gnusrc/gnu/toolchain2netbsd,v
retrieving revision 1.14
diff -u -r1.14 toolchain2netbsd
--- toolchain2netbsd	2002/01/22 13:13:00	1.14
+++ toolchain2netbsd	2002/02/14 11:39:01
@@ -20,6 +20,25 @@
 EOF
 }

+# usage: combine_configs FILENAME [FILENAME...]
+combine_configs () {
+	awk '
+		/^[^#]/ { comment[c++] = $0 }
+		/^$/ { c = 0 }
+		# For same define, different value, first FILENAME rules.
+		/^#define/ {
+			if (!($2 in define)) {
+				define_vars[d++] = $2
+				for (i = 0; i < c; i++)
+					define[$2] = define[$2] comment[i] "\n"
+				define[$2] = define[$2] $0
+			}
+			c = 0
+		}
+		END { for (i = 0; i < d; i++) print define[define_vars[i]] "\n" }
+	' $@
+}
+
 # usage: write_c FILENAME
 write_c () {
 	echo '/* This file is automatically generated.  DO NOT EDIT! */' >$1.tmp || \
@@ -41,7 +60,9 @@
 writefile () {
 	sed 's,netbsd\(elf\)*1[0-9\.]*\(_\)*[A-Z]*,netbsd\1,' >>$1.tmp
 	# will not overwrite a file that has the same content
-	if cmp $1.tmp $1 >/dev/null 2>&1; then
+	skip1="`head -2 $1.tmp | wc -c`"
+	skip2="`head -2 $1 | wc -c`"
+	if cmp $1.tmp $1 $skip1 $skip2 >/dev/null 2>&1; then
 		echo >&2 "$1 is unchanged"
 		rm -f $1.tmp
 	else
@@ -105,10 +126,8 @@

 write_c lib/libbfd/arch/$MACHINE_ARCH/bfd.h <$_TMPDIR/bfd/bfd.h

-{
-	cat $_TMPDIR/bfd/config.h
-	grep -v PACKAGE $_TMPDIR/opcodes/config.h
-} | write_c lib/libbfd/arch/$MACHINE_ARCH/config.h
+combine_configs $_TMPDIR/bfd/config.h $_TMPDIR/opcodes/config.h \
+| write_c lib/libbfd/arch/$MACHINE_ARCH/config.h

 ##### gnu/lib/libf2c #####

@@ -273,8 +292,8 @@

 (cd $_TMPDIR/gdb && $MAKE init.c version.c)

-cat $_TMPDIR/gdb/config.h $_TMPDIR/readline/config.h \
-	| write_c usr.bin/gdb/arch/$MACHINE_ARCH/config.h
+combine_configs $_TMPDIR/gdb/config.h $_TMPDIR/readline/config.h \
+| write_c usr.bin/gdb/arch/$MACHINE_ARCH/config.h

 write_c usr.bin/gdb/arch/$MACHINE_ARCH/init.c <$_TMPDIR/gdb/init.c
 write_c usr.bin/gdb/arch/$MACHINE_ARCH/version.c <$_TMPDIR/gdb/version.c
Index: dist/toolchain/config.guess
===================================================================
RCS file: /cvsroot/gnusrc/gnu/dist/toolchain/config.guess,v
retrieving revision 1.10
diff -u -r1.10 config.guess
--- dist/toolchain/config.guess	2002/01/29 18:52:09	1.10
+++ dist/toolchain/config.guess	2002/02/14 11:39:02
@@ -136,14 +136,14 @@
 	# object file format.
 	#
 	# Note: NetBSD doesn't particularly care about the vendor
-	# portion of the name.  We always set it to "unknown".
+	# portion of the name.  We leave it empty.
 	UNAME_MACHINE_ARCH=`(uname -p) 2>/dev/null` || \
 	    UNAME_MACHINE_ARCH=unknown
 	case "${UNAME_MACHINE_ARCH}" in
-	    arm*) machine=arm-unknown ;;
-	    sh3el) machine=shle-unknown ;;
-	    sh3eb) machine=sh-unknown ;;
-	    *) machine=${UNAME_MACHINE_ARCH}-unknown ;;
+	    arm*) machine=arm- ;;
+	    sh3el) machine=shle- ;;
+	    sh3eb) machine=sh- ;;
+	    *) machine=${UNAME_MACHINE_ARCH}- ;;
 	esac
 	# The Operating System including object format, if it has switched
 	# to ELF recently, or will in the future.