nouveau2netbsd revision 1.2
1#!/bin/sh
2
3#	$NetBSD: nouveau2netbsd,v 1.2 2014/08/05 19:49:13 riastradh Exp $
4#
5# $ /path/to/nouveau2netbsd > /path/to/files.nouveau.new
6#
7# Run from the top-level Nouveau source directory.  This stupid kludge
8# reinterprets the GNU makefile as a BSD makefile to extract the source
9# file names, renames the ones that have obscure and/or colliding
10# basenames to be less obscure and unlikely (though not guaranteed) to
11# collide, and spits out config(5) directives for all of them.
12
13set -Ceu
14
15# Location of the Nouveau sources relative to $NETBSDSRCDIR.
16nouveau_top=external/bsd/drm2/dist/drm/nouveau
17
18# config(5) flag for the Nouveau driver.
19nouveau_flag=nouveau
20
21filemap=
22
23clean ()
24{
25	[ -z "$filemap" ] || rm -f -- "$filemap" || :
26}
27trap clean EXIT HUP INT TERM
28
29filemap="$(mktemp -t ${0##*/})"
30
31cat Makefile								\
32| sed -e 's,^include \(.*\)$,.include "\1",'				\
33| sed -e 's,^ifdef \(.*\)$,.if !empty(\1:M[yY][eE][sS]),'		\
34| sed -e 's,^endif$,.endif,'						\
35| make -f /dev/stdin -V nouveau-y src=.					\
36| tr ' ' '\n'								\
37| sed -e 's,^$,,'							\
38| sort -u								\
39| sed -e 's,\.o$,.c,'							\
40| awk '
41	BEGIN {
42		duplicates = 0
43	}
44	$1 ~ "nouveau_[^/]*$" {
45		if (seen[$1])
46			printf("Duplicate basename: %s\n", $1)
47		seen[$1] = $1
48		printf("%s %s\n", $1, $1)
49		next
50	}
51	{
52		if (index($1, "/")) {
53			dir = $1
54			sub("/[^/]*$", "/", dir)
55		} else {
56			dir = ""
57		}
58		base = $1
59		sub("^core/", "", base)
60		gsub("/", "_", base)
61		if (seen[base]) {
62			printf("Duplicate basename: %s %s\n", seen[base], $1) \
63			    > "/dev/stderr"
64			duplicates = 1
65		}
66		if (duplicates)
67			next
68		seen[base] = $1
69		printf("%s %s\n", $1, dir "nouveau_" base)
70	}
71	END {
72		if (duplicates) {
73			printf("Time to rewite me!\n") > "/dev/stderr"
74			exit 1
75		}
76	}
77' >> "$filemap"
78
79while read from to; do
80	if [ "x$from" != "x$to" ]; then
81		mv -f -- "$from" "$to"
82	fi
83        # Probably not necessary -- Linux tends not to have RCS ids --
84        # but a precaution out of paranoia.
85        cleantags "$to"
86	case $to in
87	*.c)
88		# Heuristically apply NetBSD RCS ids: a comment at the
89		# top of the file, and a __KERNEL_RCSID before the
90		# first cpp line, which, with any luck, should be the
91		# first non-comment line and lie between the copyright
92		# notice and the header.
93		awk '
94			BEGIN {
95				done = 0
96				printf("/*\t%c%s%c\t*/\n\n", "$","NetBSD","$")
97			}
98			/^#/ && !done {
99				printf("#include <sys/cdefs.h>\n")
100				printf("__KERNEL_RCSID(0, \"%c%s%c\");\n",
101				    "$","NetBSD","$")
102				printf("\n")
103				done = 1
104			}
105			{
106				print
107			}
108		' < "$to" > "$to".tmp
109		;;
110	esac
111	mv -f -- "$to".tmp "$to"
112	printf 'file\t%s\t%s\n' "$nouveau_top/$to" "$nouveau_flag"
113done < "$filemap" | sort
114
115# We sort the output again at the end because we renamed some files but
116# left $TOP/nouveau_* unchanged, so their sort order relative to the
117# ones that got renamed may have changed.
118