nouveau2netbsd revision 1.6
11.1Sriastrad#!/bin/sh
21.1Sriastrad
31.6Sriastrad#	$NetBSD: nouveau2netbsd,v 1.6 2024/04/16 14:26:53 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.4Sriastradcat Kbuild								\
341.1Sriastrad| sed -e 's,^include \(.*\)$,.include "\1",'				\
351.6Sriastrad| sed -e 's,^ifdef \(.*\)$,.if !empty(\1:M[yY]),'			\
361.1Sriastrad| sed -e 's,^endif$,.endif,'						\
371.6Sriastrad| env									\
381.6Sriastrad	env CONFIG_ACPI=y						\
391.6Sriastrad	env CONFIG_X86=y						\
401.6Sriastrad	env src=.							\
411.6Sriastrad	make -f /dev/stdin -V '$(nouveau-y)'				\
421.1Sriastrad| tr ' ' '\n'								\
431.1Sriastrad| sed -e 's,^$,,'							\
441.1Sriastrad| sort -u								\
451.1Sriastrad| sed -e 's,\.o$,.c,'							\
461.1Sriastrad| awk '
471.1Sriastrad	BEGIN {
481.1Sriastrad		duplicates = 0
491.1Sriastrad	}
501.1Sriastrad	$1 ~ "nouveau_[^/]*$" {
511.1Sriastrad		if (seen[$1])
521.1Sriastrad			printf("Duplicate basename: %s\n", $1)
531.1Sriastrad		seen[$1] = $1
541.1Sriastrad		printf("%s %s\n", $1, $1)
551.1Sriastrad		next
561.1Sriastrad	}
571.1Sriastrad	{
581.1Sriastrad		if (index($1, "/")) {
591.1Sriastrad			dir = $1
601.1Sriastrad			sub("/[^/]*$", "/", dir)
611.1Sriastrad		} else {
621.1Sriastrad			dir = ""
631.1Sriastrad		}
641.1Sriastrad		base = $1
651.1Sriastrad		sub("^core/", "", base)
661.1Sriastrad		gsub("/", "_", base)
671.1Sriastrad		if (seen[base]) {
681.1Sriastrad			printf("Duplicate basename: %s %s\n", seen[base], $1) \
691.1Sriastrad			    > "/dev/stderr"
701.1Sriastrad			duplicates = 1
711.1Sriastrad		}
721.1Sriastrad		if (duplicates)
731.1Sriastrad			next
741.1Sriastrad		seen[base] = $1
751.1Sriastrad		printf("%s %s\n", $1, dir "nouveau_" base)
761.1Sriastrad	}
771.1Sriastrad	END {
781.1Sriastrad		if (duplicates) {
791.1Sriastrad			printf("Time to rewite me!\n") > "/dev/stderr"
801.1Sriastrad			exit 1
811.1Sriastrad		}
821.1Sriastrad	}
831.1Sriastrad' >> "$filemap"
841.1Sriastrad
851.1Sriastradwhile read from to; do
861.1Sriastrad	if [ "x$from" != "x$to" ]; then
871.3Sriastrad		${MV} -f -- "$from" "$to"
881.1Sriastrad	fi
891.1Sriastrad	printf 'file\t%s\t%s\n' "$nouveau_top/$to" "$nouveau_flag"
901.1Sriastraddone < "$filemap" | sort
911.1Sriastrad
921.1Sriastrad# We sort the output again at the end because we renamed some files but
931.1Sriastrad# left $TOP/nouveau_* unchanged, so their sort order relative to the
941.1Sriastrad# ones that got renamed may have changed.
95