11.1Sriastrad#!/bin/sh
21.1Sriastrad
31.7Sriastrad#	$NetBSD: nouveau2netbsd,v 1.7 2024/07/02 20:09:01 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.7Sriastrad	env CONFIG_COMPAT=y						\
401.7Sriastrad	env CONFIG_DRM_NOUVEAU_SVM=y					\
411.7Sriastrad	env CONFIG_LEDS_CLASS=y						\
421.7Sriastrad	env CONFIG_NOUVEAU=y						\
431.7Sriastrad	env CONFIG_NOUVEAU_BACKLIGHT=y					\
441.6Sriastrad	env CONFIG_X86=y						\
451.6Sriastrad	env src=.							\
461.6Sriastrad	make -f /dev/stdin -V '$(nouveau-y)'				\
471.1Sriastrad| tr ' ' '\n'								\
481.1Sriastrad| sed -e 's,^$,,'							\
491.1Sriastrad| sort -u								\
501.1Sriastrad| sed -e 's,\.o$,.c,'							\
511.1Sriastrad| awk '
521.1Sriastrad	BEGIN {
531.1Sriastrad		duplicates = 0
541.1Sriastrad	}
551.1Sriastrad	$1 ~ "nouveau_[^/]*$" {
561.1Sriastrad		if (seen[$1])
571.1Sriastrad			printf("Duplicate basename: %s\n", $1)
581.1Sriastrad		seen[$1] = $1
591.1Sriastrad		printf("%s %s\n", $1, $1)
601.1Sriastrad		next
611.1Sriastrad	}
621.1Sriastrad	{
631.1Sriastrad		if (index($1, "/")) {
641.1Sriastrad			dir = $1
651.1Sriastrad			sub("/[^/]*$", "/", dir)
661.1Sriastrad		} else {
671.1Sriastrad			dir = ""
681.1Sriastrad		}
691.1Sriastrad		base = $1
701.1Sriastrad		sub("^core/", "", base)
711.1Sriastrad		gsub("/", "_", base)
721.1Sriastrad		if (seen[base]) {
731.1Sriastrad			printf("Duplicate basename: %s %s\n", seen[base], $1) \
741.1Sriastrad			    > "/dev/stderr"
751.1Sriastrad			duplicates = 1
761.1Sriastrad		}
771.1Sriastrad		if (duplicates)
781.1Sriastrad			next
791.1Sriastrad		seen[base] = $1
801.1Sriastrad		printf("%s %s\n", $1, dir "nouveau_" base)
811.1Sriastrad	}
821.1Sriastrad	END {
831.1Sriastrad		if (duplicates) {
841.1Sriastrad			printf("Time to rewite me!\n") > "/dev/stderr"
851.1Sriastrad			exit 1
861.1Sriastrad		}
871.1Sriastrad	}
881.1Sriastrad' >> "$filemap"
891.1Sriastrad
901.1Sriastradwhile read from to; do
911.7Sriastrad	# If the move already happened, that's fine: the makefile
921.7Sriastrad	# detects duplicates.
931.7Sriastrad	if [ "x$from" != "x$to" -a \! -f "$to" ]; then
941.3Sriastrad		${MV} -f -- "$from" "$to"
951.1Sriastrad	fi
961.1Sriastrad	printf 'file\t%s\t%s\n' "$nouveau_top/$to" "$nouveau_flag"
971.1Sriastraddone < "$filemap" | sort
981.1Sriastrad
991.1Sriastrad# We sort the output again at the end because we renamed some files but
1001.1Sriastrad# left $TOP/nouveau_* unchanged, so their sort order relative to the
1011.1Sriastrad# ones that got renamed may have changed.
102