nouveau2netbsd revision 1.2
11.1Sriastrad#!/bin/sh
21.1Sriastrad
31.2Sriastrad#	$NetBSD: nouveau2netbsd,v 1.2 2014/08/05 19:49:13 riastradh Exp $
41.1Sriastrad#
51.1Sriastrad# $ /path/to/nouveau2netbsd > /path/to/files.nouveau.new
61.1Sriastrad#
71.1Sriastrad# Run from the top-level Nouveau source directory.  This stupid kludge
81.1Sriastrad# reinterprets the GNU makefile as a BSD makefile to extract the source
91.1Sriastrad# file names, renames the ones that have obscure and/or colliding
101.1Sriastrad# basenames to be less obscure and unlikely (though not guaranteed) to
111.1Sriastrad# collide, and spits out config(5) directives for all of them.
121.1Sriastrad
131.1Sriastradset -Ceu
141.1Sriastrad
151.1Sriastrad# Location of the Nouveau sources relative to $NETBSDSRCDIR.
161.1Sriastradnouveau_top=external/bsd/drm2/dist/drm/nouveau
171.1Sriastrad
181.1Sriastrad# config(5) flag for the Nouveau driver.
191.1Sriastradnouveau_flag=nouveau
201.1Sriastrad
211.1Sriastradfilemap=
221.1Sriastrad
231.1Sriastradclean ()
241.1Sriastrad{
251.1Sriastrad	[ -z "$filemap" ] || rm -f -- "$filemap" || :
261.1Sriastrad}
271.1Sriastradtrap clean EXIT HUP INT TERM
281.1Sriastrad
291.1Sriastradfilemap="$(mktemp -t ${0##*/})"
301.1Sriastrad
311.1Sriastradcat Makefile								\
321.1Sriastrad| sed -e 's,^include \(.*\)$,.include "\1",'				\
331.1Sriastrad| sed -e 's,^ifdef \(.*\)$,.if !empty(\1:M[yY][eE][sS]),'		\
341.1Sriastrad| sed -e 's,^endif$,.endif,'						\
351.1Sriastrad| make -f /dev/stdin -V nouveau-y src=.					\
361.1Sriastrad| tr ' ' '\n'								\
371.1Sriastrad| sed -e 's,^$,,'							\
381.1Sriastrad| sort -u								\
391.1Sriastrad| sed -e 's,\.o$,.c,'							\
401.1Sriastrad| awk '
411.1Sriastrad	BEGIN {
421.1Sriastrad		duplicates = 0
431.1Sriastrad	}
441.1Sriastrad	$1 ~ "nouveau_[^/]*$" {
451.1Sriastrad		if (seen[$1])
461.1Sriastrad			printf("Duplicate basename: %s\n", $1)
471.1Sriastrad		seen[$1] = $1
481.1Sriastrad		printf("%s %s\n", $1, $1)
491.1Sriastrad		next
501.1Sriastrad	}
511.1Sriastrad	{
521.1Sriastrad		if (index($1, "/")) {
531.1Sriastrad			dir = $1
541.1Sriastrad			sub("/[^/]*$", "/", dir)
551.1Sriastrad		} else {
561.1Sriastrad			dir = ""
571.1Sriastrad		}
581.1Sriastrad		base = $1
591.1Sriastrad		sub("^core/", "", base)
601.1Sriastrad		gsub("/", "_", base)
611.1Sriastrad		if (seen[base]) {
621.1Sriastrad			printf("Duplicate basename: %s %s\n", seen[base], $1) \
631.1Sriastrad			    > "/dev/stderr"
641.1Sriastrad			duplicates = 1
651.1Sriastrad		}
661.1Sriastrad		if (duplicates)
671.1Sriastrad			next
681.1Sriastrad		seen[base] = $1
691.1Sriastrad		printf("%s %s\n", $1, dir "nouveau_" base)
701.1Sriastrad	}
711.1Sriastrad	END {
721.1Sriastrad		if (duplicates) {
731.1Sriastrad			printf("Time to rewite me!\n") > "/dev/stderr"
741.1Sriastrad			exit 1
751.1Sriastrad		}
761.1Sriastrad	}
771.1Sriastrad' >> "$filemap"
781.1Sriastrad
791.1Sriastradwhile read from to; do
801.1Sriastrad	if [ "x$from" != "x$to" ]; then
811.1Sriastrad		mv -f -- "$from" "$to"
821.1Sriastrad	fi
831.1Sriastrad        # Probably not necessary -- Linux tends not to have RCS ids --
841.1Sriastrad        # but a precaution out of paranoia.
851.1Sriastrad        cleantags "$to"
861.1Sriastrad	case $to in
871.1Sriastrad	*.c)
881.2Sriastrad		# Heuristically apply NetBSD RCS ids: a comment at the
891.2Sriastrad		# top of the file, and a __KERNEL_RCSID before the
901.2Sriastrad		# first cpp line, which, with any luck, should be the
911.2Sriastrad		# first non-comment line and lie between the copyright
921.2Sriastrad		# notice and the header.
931.1Sriastrad		awk '
941.1Sriastrad			BEGIN {
951.1Sriastrad				done = 0
961.1Sriastrad				printf("/*\t%c%s%c\t*/\n\n", "$","NetBSD","$")
971.1Sriastrad			}
981.2Sriastrad			/^#/ && !done {
991.1Sriastrad				printf("#include <sys/cdefs.h>\n")
1001.1Sriastrad				printf("__KERNEL_RCSID(0, \"%c%s%c\");\n",
1011.1Sriastrad				    "$","NetBSD","$")
1021.1Sriastrad				printf("\n")
1031.1Sriastrad				done = 1
1041.1Sriastrad			}
1051.1Sriastrad			{
1061.1Sriastrad				print
1071.1Sriastrad			}
1081.1Sriastrad		' < "$to" > "$to".tmp
1091.1Sriastrad		;;
1101.1Sriastrad	esac
1111.1Sriastrad	mv -f -- "$to".tmp "$to"
1121.1Sriastrad	printf 'file\t%s\t%s\n' "$nouveau_top/$to" "$nouveau_flag"
1131.1Sriastraddone < "$filemap" | sort
1141.1Sriastrad
1151.1Sriastrad# We sort the output again at the end because we renamed some files but
1161.1Sriastrad# left $TOP/nouveau_* unchanged, so their sort order relative to the
1171.1Sriastrad# ones that got renamed may have changed.
118