nouveau2netbsd revision 1.6
1#!/bin/sh
2
3#	$NetBSD: nouveau2netbsd,v 1.6 2024/04/16 14:26:53 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: ${MV:=mv}
16
17# Location of the Nouveau sources relative to $NETBSDSRCDIR.
18nouveau_top=external/bsd/drm2/dist/drm/nouveau
19
20# config(5) flag for the Nouveau driver.
21nouveau_flag=nouveau
22
23filemap=
24
25clean ()
26{
27	[ -z "$filemap" ] || rm -f -- "$filemap" || :
28}
29trap clean EXIT HUP INT TERM
30
31filemap="$(mktemp -t ${0##*/})"
32
33cat Kbuild								\
34| sed -e 's,^include \(.*\)$,.include "\1",'				\
35| sed -e 's,^ifdef \(.*\)$,.if !empty(\1:M[yY]),'			\
36| sed -e 's,^endif$,.endif,'						\
37| env									\
38	env CONFIG_ACPI=y						\
39	env CONFIG_X86=y						\
40	env src=.							\
41	make -f /dev/stdin -V '$(nouveau-y)'				\
42| tr ' ' '\n'								\
43| sed -e 's,^$,,'							\
44| sort -u								\
45| sed -e 's,\.o$,.c,'							\
46| awk '
47	BEGIN {
48		duplicates = 0
49	}
50	$1 ~ "nouveau_[^/]*$" {
51		if (seen[$1])
52			printf("Duplicate basename: %s\n", $1)
53		seen[$1] = $1
54		printf("%s %s\n", $1, $1)
55		next
56	}
57	{
58		if (index($1, "/")) {
59			dir = $1
60			sub("/[^/]*$", "/", dir)
61		} else {
62			dir = ""
63		}
64		base = $1
65		sub("^core/", "", base)
66		gsub("/", "_", base)
67		if (seen[base]) {
68			printf("Duplicate basename: %s %s\n", seen[base], $1) \
69			    > "/dev/stderr"
70			duplicates = 1
71		}
72		if (duplicates)
73			next
74		seen[base] = $1
75		printf("%s %s\n", $1, dir "nouveau_" base)
76	}
77	END {
78		if (duplicates) {
79			printf("Time to rewite me!\n") > "/dev/stderr"
80			exit 1
81		}
82	}
83' >> "$filemap"
84
85while read from to; do
86	if [ "x$from" != "x$to" ]; then
87		${MV} -f -- "$from" "$to"
88	fi
89	printf 'file\t%s\t%s\n' "$nouveau_top/$to" "$nouveau_flag"
90done < "$filemap" | sort
91
92# We sort the output again at the end because we renamed some files but
93# left $TOP/nouveau_* unchanged, so their sort order relative to the
94# ones that got renamed may have changed.
95