nouveau2netbsd revision 1.3
11.1Sriastrad#!/bin/sh
21.1Sriastrad
31.3Sriastrad#	$NetBSD: nouveau2netbsd,v 1.3 2018/08/27 00:46:04 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.3Sriastrad: ${MV:=mv}
161.3Sriastrad
171.1Sriastrad# Location of the Nouveau sources relative to $NETBSDSRCDIR.
181.1Sriastradnouveau_top=external/bsd/drm2/dist/drm/nouveau
191.1Sriastrad
201.1Sriastrad# config(5) flag for the Nouveau driver.
211.1Sriastradnouveau_flag=nouveau
221.1Sriastrad
231.1Sriastradfilemap=
241.1Sriastrad
251.1Sriastradclean ()
261.1Sriastrad{
271.1Sriastrad	[ -z "$filemap" ] || rm -f -- "$filemap" || :
281.1Sriastrad}
291.1Sriastradtrap clean EXIT HUP INT TERM
301.1Sriastrad
311.1Sriastradfilemap="$(mktemp -t ${0##*/})"
321.1Sriastrad
331.1Sriastradcat Makefile								\
341.1Sriastrad| sed -e 's,^include \(.*\)$,.include "\1",'				\
351.1Sriastrad| sed -e 's,^ifdef \(.*\)$,.if !empty(\1:M[yY][eE][sS]),'		\
361.1Sriastrad| sed -e 's,^endif$,.endif,'						\
371.1Sriastrad| make -f /dev/stdin -V nouveau-y src=.					\
381.1Sriastrad| tr ' ' '\n'								\
391.1Sriastrad| sed -e 's,^$,,'							\
401.1Sriastrad| sort -u								\
411.1Sriastrad| sed -e 's,\.o$,.c,'							\
421.1Sriastrad| awk '
431.1Sriastrad	BEGIN {
441.1Sriastrad		duplicates = 0
451.1Sriastrad	}
461.1Sriastrad	$1 ~ "nouveau_[^/]*$" {
471.1Sriastrad		if (seen[$1])
481.1Sriastrad			printf("Duplicate basename: %s\n", $1)
491.1Sriastrad		seen[$1] = $1
501.1Sriastrad		printf("%s %s\n", $1, $1)
511.1Sriastrad		next
521.1Sriastrad	}
531.1Sriastrad	{
541.1Sriastrad		if (index($1, "/")) {
551.1Sriastrad			dir = $1
561.1Sriastrad			sub("/[^/]*$", "/", dir)
571.1Sriastrad		} else {
581.1Sriastrad			dir = ""
591.1Sriastrad		}
601.1Sriastrad		base = $1
611.1Sriastrad		sub("^core/", "", base)
621.1Sriastrad		gsub("/", "_", base)
631.1Sriastrad		if (seen[base]) {
641.1Sriastrad			printf("Duplicate basename: %s %s\n", seen[base], $1) \
651.1Sriastrad			    > "/dev/stderr"
661.1Sriastrad			duplicates = 1
671.1Sriastrad		}
681.1Sriastrad		if (duplicates)
691.1Sriastrad			next
701.1Sriastrad		seen[base] = $1
711.1Sriastrad		printf("%s %s\n", $1, dir "nouveau_" base)
721.1Sriastrad	}
731.1Sriastrad	END {
741.1Sriastrad		if (duplicates) {
751.1Sriastrad			printf("Time to rewite me!\n") > "/dev/stderr"
761.1Sriastrad			exit 1
771.1Sriastrad		}
781.1Sriastrad	}
791.1Sriastrad' >> "$filemap"
801.1Sriastrad
811.1Sriastradwhile read from to; do
821.1Sriastrad	if [ "x$from" != "x$to" ]; then
831.3Sriastrad		${MV} -f -- "$from" "$to"
841.1Sriastrad	fi
851.1Sriastrad        # Probably not necessary -- Linux tends not to have RCS ids --
861.1Sriastrad        # but a precaution out of paranoia.
871.1Sriastrad        cleantags "$to"
881.1Sriastrad	case $to in
891.1Sriastrad	*.c)
901.2Sriastrad		# Heuristically apply NetBSD RCS ids: a comment at the
911.2Sriastrad		# top of the file, and a __KERNEL_RCSID before the
921.2Sriastrad		# first cpp line, which, with any luck, should be the
931.2Sriastrad		# first non-comment line and lie between the copyright
941.2Sriastrad		# notice and the header.
951.1Sriastrad		awk '
961.1Sriastrad			BEGIN {
971.1Sriastrad				done = 0
981.1Sriastrad				printf("/*\t%c%s%c\t*/\n\n", "$","NetBSD","$")
991.1Sriastrad			}
1001.2Sriastrad			/^#/ && !done {
1011.1Sriastrad				printf("#include <sys/cdefs.h>\n")
1021.1Sriastrad				printf("__KERNEL_RCSID(0, \"%c%s%c\");\n",
1031.1Sriastrad				    "$","NetBSD","$")
1041.1Sriastrad				printf("\n")
1051.1Sriastrad				done = 1
1061.1Sriastrad			}
1071.1Sriastrad			{
1081.1Sriastrad				print
1091.1Sriastrad			}
1101.1Sriastrad		' < "$to" > "$to".tmp
1111.1Sriastrad		;;
1121.1Sriastrad	esac
1131.1Sriastrad	mv -f -- "$to".tmp "$to"
1141.1Sriastrad	printf 'file\t%s\t%s\n' "$nouveau_top/$to" "$nouveau_flag"
1151.1Sriastraddone < "$filemap" | sort
1161.1Sriastrad
1171.1Sriastrad# We sort the output again at the end because we renamed some files but
1181.1Sriastrad# left $TOP/nouveau_* unchanged, so their sort order relative to the
1191.1Sriastrad# ones that got renamed may have changed.
120