Skip to contentSkip to conversation

prepare-kernel.sh: Tame link mode cleanup code

PATCH In review Richard Weinberger 2 participants 3 messages Jun 25, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 2/15 prepare-kernel.sh: Tame link mode cleanup code

The existing cleanup code tries to delete stale links from a previous run of prepare-kernel.sh. It removes all links below $target_dir which don't exist in the Xenomai $link_dir directory. patch_link() implicitly assumes ascending order of calls, the new reverse mode feature explicitly needs descending call order.

Digging deeper unearthed further odds in the cleanup code. Originally it used to rm -Rf stale folders and files, but after commit e72d695a7 ("scripts/prepare-kernel: only purge links from target directories") in 2010 code tested whether the target file is a symlink.
So $dir_opt and rm'ing recursively is not needed anymore.

To make reverse mode work, change the removal test to remove symlinks only if they are broken.
e.g. when they point to a moved/old Xenomai installation.

---
 scripts/prepare-kernel.sh | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
scripts/prepare-kernel.sh +2 -4
index 352893224..7a1c4d923 100755
@@ -83,18 +83,16 @@ patch_link() {
 		fi
 		if test x$recursive = xr; then
 			recursive_opt="-mindepth 1"
-			dir_opt="-type d -o"
 		else
 			recursive_opt="-maxdepth 1"
-			dir_opt=""
 		fi
-		find_clean_opt="$recursive_opt \( $dir_opt $link_makefiles_opt -name Kconfig -o -name '*.[chS]' -o -name '*.sh' \)"
+		find_clean_opt="$recursive_opt \( $link_makefiles_opt -name Kconfig -o -name '*.[chS]' -o -name '*.sh' \)"
 		find_link_opt="$recursive_opt \( $link_makefiles_opt -name Kconfig -o -name '*.[chS]' -o -name '*.sh' \)"
 	fi
 
 	if test "x$output_patch" = "x" -a -e $linux_tree/$link_dir; then
 		cd $linux_tree/$link_dir && eval find . $find_clean_opt | while read f; do
-			if test -L $f -a ! -e $xenomai_root/$target_dir/$f; then rm -Rf $f; fi
+			if test -L $f -a ! -e $f; then rm -f $f; fi
 		done
 	fi
 
Signature · 1 line
2.35.3
#1 lore raw

PATCH 3/15 prepare-kernel.sh: Add reverse mode

There is currently no way to undo preparing of a kernel, except using --outpatch and reverting the resulting patch later.
But in link mode the kernel tree needs manual cleaning, which can be tedious if a git clean -f -d -x is not possible.

The new --reverse flag offers this feature, it removes all previously created symlinks and directories.
To be able to undo effects by patch_append(), keep a .orig file link as the script already does for symlinks in the linux tree.

[Jan: add missing init/Kconfig reversion, fix indention]
---
 scripts/prepare-kernel.sh | 75 +++++++++++++++++++++++++++++++--------
 1 file changed, 60 insertions(+), 15 deletions(-)
scripts/prepare-kernel.sh +60 -15
index 7a1c4d923..c9a02583b 100755
@@ -55,6 +55,8 @@ patch_append() {
 		if test -L "$linux_tree/$file" ; then
 			mv "$linux_tree/$file" "$linux_tree/$file.orig"
 			cp "$linux_tree/$file.orig" "$linux_tree/$file"
+		else
+			cp "$linux_tree/$file" "$linux_tree/$file.orig"
 		fi
 		chmod +w "$linux_tree/$file"
 		cat >> "$linux_tree/$file"
@@ -66,6 +68,16 @@ patch_append() {
 	fi
 }
 
+patch_reverse() {
+	file="$1"
+	if test "x$output_patch" = "x"; then
+		if test -L "$linux_tree/$file" ; then
+			rm "$linux_tree/$file"
+		fi
+		mv "$linux_tree/$file.orig" "$linux_tree/$file"
+	fi
+}
+
 patch_link() {
 	recursive="$1"		   # "r" or "n"
 	link_file="$2"		   # "m", "n" or some file (basename) from $target_dir
@@ -90,7 +102,7 @@ patch_link() {
 		find_link_opt="$recursive_opt \( $link_makefiles_opt -name Kconfig -o -name '*.[chS]' -o -name '*.sh' \)"
 	fi
 
-	if test "x$output_patch" = "x" -a -e $linux_tree/$link_dir; then
+	if test "x$output_patch" = "x" -a "x$reverse" = "x0" -a -e $linux_tree/$link_dir; then
 		cd $linux_tree/$link_dir && eval find . $find_clean_opt | while read f; do
 			if test -L $f -a ! -e $f; then rm -f $f; fi
 		done
@@ -102,11 +114,15 @@ patch_link() {
 		f=`echo $f | cut -d/ -f2-`
 		d=`dirname $f`
 		if test "x$output_patch" = "x"; then
-			mkdir -p $linux_tree/$link_dir/$d
-			if test x$forcelink = x1 -o \
-				! $xenomai_root/$target_dir/$f -ef $linux_tree/$link_dir/$f;
-			then
-				ln -sf $xenomai_root/$target_dir/$f $linux_tree/$link_dir/$f
+			if test x$reverse = x1; then
+				rm -f $linux_tree/$link_dir/$f
+			else
+				mkdir -p $linux_tree/$link_dir/$d
+				if test x$forcelink = x1 -o \
+					! $xenomai_root/$target_dir/$f -ef $linux_tree/$link_dir/$f;
+				then
+					ln -sf $xenomai_root/$target_dir/$f $linux_tree/$link_dir/$f
+				fi
 			fi
 		else
 			if test `check_filter $link_dir/$f` = "ok"; then
@@ -118,6 +134,11 @@ patch_link() {
 			fi
 		fi
 	done
+	if test x$reverse = x1; then
+		if ! find $linux_tree/$link_dir -not -type d | grep -q . ; then
+			rm -rf $linux_tree/$link_dir
+		fi
+	fi
 	)
 }
 
@@ -133,8 +154,7 @@ generate_patch() {
 	)
 }
 
-
-usage='usage: prepare-kernel --linux=<linux-tree> [--dovetail=<dovetail-patch>] [--arch=<arch>] [--outpatch=<file> [--filterkvers=y|n] [--filterarch=y|n]] [--forcelink] [--default] [--verbose]'
+usage='usage: prepare-kernel --linux=<linux-tree> [--dovetail=<dovetail-patch>] [--arch=<arch>] [--outpatch=<file> [--filterkvers=y|n] [--filterarch=y|n]] [--forcelink] [--default] [--verbose] [--reverse]'
 me=`basename $0`
 
 while test $# -gt 0; do
@@ -170,6 +190,9 @@ while test $# -gt 0; do
 	--verbose)
 		verbose=1
 		;;
+	--reverse)
+		reverse=1
+		;;
 	--help)
 		echo "$usage"
 		exit 0
@@ -219,6 +242,13 @@ if test \! -r $linux_tree/Makefile; then
 	exit 2
 fi
 
+if test x$reverse = x1; then
+	if ! grep -q CONFIG_XENOMAI $linux_tree/arch/$linux_arch/Makefile; then
+		echo "$me: $linux_tree is not prepared with Xenomai" >&2
+		exit 2
+	fi
+fi
+
 # Create an empty output patch file, and initialize the temporary tree.
 if test "x$output_patch" != "x"; then
 	temp_tree=`mktemp -d prepare-kernel-XXX --tmpdir`
@@ -227,6 +257,11 @@ if test "x$output_patch" != "x"; then
 		exit 1
 	fi
 
+	if test x$reverse = x1; then
+		echo "$me: --reverse and --outpatch are not compatible"
+		exit 1
+	fi
+
 	patchdir=`dirname $output_patch`
 	patchdir=`cd $patchdir && pwd`
 	output_patch=$patchdir/`basename $output_patch`
@@ -353,15 +388,18 @@ case $linux_VERSION.$linux_PATCHLEVEL in
 			-e "s,@SRCARCH@,$linux_arch,g" \
 			$xenomai_root/scripts/Kconfig.frag |
 			patch_append init/Kconfig
+	elif test x$reverse = x1; then
+		patch_reverse init/Kconfig
 	fi
 
 	test "x$CONFIG_XENO_REVISION_LEVEL" = "x" && CONFIG_XENO_REVISION_LEVEL=0
 
 	if ! grep -q CONFIG_XENOMAI $linux_tree/arch/$linux_arch/Makefile; then
-		p="KBUILD_CFLAGS += -I\$(srctree)/arch/\$(SRCARCH)/xenomai/include -I\$(srctree)/include/xenomai"
-		(echo; echo $p) | patch_append arch/$linux_arch/Makefile
-		p="core-\$(CONFIG_XENOMAI)	+= arch/$linux_arch/xenomai/"
-		echo $p | patch_append arch/$linux_arch/Makefile
+		p1="KBUILD_CFLAGS += -I\$(srctree)/arch/\$(SRCARCH)/xenomai/include -I\$(srctree)/include/xenomai"
+		p2="core-\$(CONFIG_XENOMAI)	+= arch/$linux_arch/xenomai/"
+		(echo; echo $p1; echo $p2) | patch_append arch/$linux_arch/Makefile
+	elif test x$reverse = x1; then
+		patch_reverse arch/$linux_arch/Makefile
 	fi
 
 	patch_architecture_specific="n"
@@ -369,11 +407,15 @@ case $linux_VERSION.$linux_PATCHLEVEL in
 	if ! grep -q CONFIG_XENOMAI $linux_tree/drivers/Makefile; then
 		p="obj-\$(CONFIG_XENOMAI)		+= xenomai/"
 		( echo ; echo $p ) | patch_append drivers/Makefile
+	elif test x$reverse = x1; then
+		patch_reverse drivers/Makefile
 	fi
 
 	if ! grep -q CONFIG_XENOMAI $linux_tree/kernel/Makefile; then
 		p="obj-\$(CONFIG_XENOMAI)		+= xenomai/"
 		( echo ; echo $p ) | patch_append kernel/Makefile
+	elif test x$reverse = x1; then
+		patch_reverse kernel/Makefile
 	fi
 	;;
 esac
@@ -381,13 +423,15 @@ esac
 # Create local directories then symlink to the source files from
 # there, so that we don't pollute the Xenomai source tree with
 # compilation files.
+#
+# Keep link dirs (4th parameter) descending, otherwise --reverse
+# is not able to cleanup these directories!
 
 patch_kernelversion_specific="n"
 patch_architecture_specific="y"
 patch_link r m kernel/cobalt/arch/$linux_arch arch/$linux_arch/xenomai
 patch_link n n kernel/cobalt/include/dovetail arch/$linux_arch/include/dovetail
 patch_architecture_specific="n"
-patch_link n m kernel/cobalt kernel/xenomai
 patch_link n cobalt-core.h kernel/cobalt/trace include/trace/events
 patch_link n cobalt-rtdm.h kernel/cobalt/trace include/trace/events
 patch_link n cobalt-posix.h kernel/cobalt/trace include/trace/events
@@ -395,14 +439,15 @@ patch_link r n kernel/cobalt/include/asm-generic/xenomai include/asm-generic/xen
 patch_link n m kernel/cobalt/posix kernel/xenomai/posix
 patch_link n m kernel/cobalt/rtdm kernel/xenomai/rtdm
 patch_link n m kernel/cobalt/dovetail kernel/xenomai/pipeline
+patch_link n m kernel/cobalt kernel/xenomai
 patch_link r m kernel/drivers drivers/xenomai
 patch_link n n include/cobalt/kernel include/xenomai/cobalt/kernel
-patch_link r n include/cobalt/kernel/rtdm include/xenomai/rtdm
 patch_link r n include/cobalt/kernel/dovetail/pipeline include/xenomai/pipeline
 patch_link r n include/cobalt/uapi include/xenomai/cobalt/uapi
 patch_link r n include/rtdm/uapi include/xenomai/rtdm/uapi
-patch_link n version.h include/xenomai include/xenomai
+patch_link r n include/cobalt/kernel/rtdm include/xenomai/rtdm
 patch_link n stdarg.h kernel/cobalt/include/linux include/xenomai/linux
+patch_link n version.h include/xenomai include/xenomai
 
 if test "x$output_patch" != "x"; then
 	if test x$verbose = x1; then
Signature · 1 line
2.35.3
#2 lore raw

Re: [PATCH 03/15] prepare-kernel.sh: Add reverse mode

Quoted 199 lines · Richard Weinberger

There is currently no way to undo preparing of a kernel, except using --outpatch and reverting the resulting patch later.
But in link mode the kernel tree needs manual cleaning, which can be tedious if a git clean -f -d -x is not possible.

The new --reverse flag offers this feature, it removes all previously created symlinks and directories.
To be able to undo effects by patch_append(), keep a .orig file link as the script already does for symlinks in the linux tree.

[Jan: add missing init/Kconfig reversion, fix indention]
---
 scripts/prepare-kernel.sh | 75 +++++++++++++++++++++++++++++++--------
 1 file changed, 60 insertions(+), 15 deletions(-)
scripts/prepare-kernel.sh +58 -14
index 7a1c4d923..c9a02583b 100755
@@ -55,6 +55,8 @@ patch_append() {
 		if test -L "$linux_tree/$file" ; then
 			mv "$linux_tree/$file" "$linux_tree/$file.orig"
 			cp "$linux_tree/$file.orig" "$linux_tree/$file"
+		else
+			cp "$linux_tree/$file" "$linux_tree/$file.orig"
 		fi
 		chmod +w "$linux_tree/$file"
 		cat >> "$linux_tree/$file"
@@ -66,6 +68,16 @@ patch_append() {
 	fi
 }
 
+patch_reverse() {
+	file="$1"
+	if test "x$output_patch" = "x"; then
+		if test -L "$linux_tree/$file" ; then
+			rm "$linux_tree/$file"
+		fi
+		mv "$linux_tree/$file.orig" "$linux_tree/$file"
+	fi
+}
+
 patch_link() {
 	recursive="$1"		   # "r" or "n"
 	link_file="$2"		   # "m", "n" or some file (basename) from $target_dir
@@ -90,7 +102,7 @@ patch_link() {
 		find_link_opt="$recursive_opt \( $link_makefiles_opt -name Kconfig -o -name '*.[chS]' -o -name '*.sh' \)"
 	fi
 
-	if test "x$output_patch" = "x" -a -e $linux_tree/$link_dir; then
+	if test "x$output_patch" = "x" -a "x$reverse" = "x0" -a -e $linux_tree/$link_dir; then
 		cd $linux_tree/$link_dir && eval find . $find_clean_opt | while read f; do
 			if test -L $f -a ! -e $f; then rm -f $f; fi
 		done
@@ -102,11 +114,15 @@ patch_link() {
 		f=`echo $f | cut -d/ -f2-`
 		d=`dirname $f`
 		if test "x$output_patch" = "x"; then
-			mkdir -p $linux_tree/$link_dir/$d
-			if test x$forcelink = x1 -o \
-				! $xenomai_root/$target_dir/$f -ef $linux_tree/$link_dir/$f;
-			then
-				ln -sf $xenomai_root/$target_dir/$f $linux_tree/$link_dir/$f
+			if test x$reverse = x1; then
+				rm -f $linux_tree/$link_dir/$f
+			else
+				mkdir -p $linux_tree/$link_dir/$d
+				if test x$forcelink = x1 -o \
+					! $xenomai_root/$target_dir/$f -ef $linux_tree/$link_dir/$f;
+				then
+					ln -sf $xenomai_root/$target_dir/$f $linux_tree/$link_dir/$f
+				fi
 			fi
 		else
 			if test `check_filter $link_dir/$f` = "ok"; then
@@ -118,6 +134,11 @@ patch_link() {
 			fi
 		fi
 	done
+	if test x$reverse = x1; then
+		if ! find $linux_tree/$link_dir -not -type d | grep -q . ; then
+			rm -rf $linux_tree/$link_dir
+		fi
+	fi
 	)
 }
 
@@ -133,8 +154,7 @@ generate_patch() {
 	)
 }
 
-
-usage='usage: prepare-kernel --linux=<linux-tree> [--dovetail=<dovetail-patch>] [--arch=<arch>] [--outpatch=<file> [--filterkvers=y|n] [--filterarch=y|n]] [--forcelink] [--default] [--verbose]'
+usage='usage: prepare-kernel --linux=<linux-tree> [--dovetail=<dovetail-patch>] [--arch=<arch>] [--outpatch=<file> [--filterkvers=y|n] [--filterarch=y|n]] [--forcelink] [--default] [--verbose] [--reverse]'
 me=`basename $0`
 
 while test $# -gt 0; do
@@ -170,6 +190,9 @@ while test $# -gt 0; do
 	--verbose)
 		verbose=1
 		;;
+	--reverse)
+		reverse=1
+		;;
 	--help)
 		echo "$usage"
 		exit 0
@@ -219,6 +242,13 @@ if test \! -r $linux_tree/Makefile; then
 	exit 2
 fi
 
+if test x$reverse = x1; then
+	if ! grep -q CONFIG_XENOMAI $linux_tree/arch/$linux_arch/Makefile; then
+		echo "$me: $linux_tree is not prepared with Xenomai" >&2
+		exit 2
+	fi
+fi
+
 # Create an empty output patch file, and initialize the temporary tree.
 if test "x$output_patch" != "x"; then
 	temp_tree=`mktemp -d prepare-kernel-XXX --tmpdir`
@@ -227,6 +257,11 @@ if test "x$output_patch" != "x"; then
 		exit 1
 	fi
 
+	if test x$reverse = x1; then
+		echo "$me: --reverse and --outpatch are not compatible"
+		exit 1
+	fi
+
 	patchdir=`dirname $output_patch`
 	patchdir=`cd $patchdir && pwd`
 	output_patch=$patchdir/`basename $output_patch`
@@ -353,15 +388,18 @@ case $linux_VERSION.$linux_PATCHLEVEL in
 			-e "s,@SRCARCH@,$linux_arch,g" \
 			$xenomai_root/scripts/Kconfig.frag |
 			patch_append init/Kconfig
+	elif test x$reverse = x1; then
+		patch_reverse init/Kconfig
 	fi
 
 	test "x$CONFIG_XENO_REVISION_LEVEL" = "x" && CONFIG_XENO_REVISION_LEVEL=0
 
 	if ! grep -q CONFIG_XENOMAI $linux_tree/arch/$linux_arch/Makefile; then
-		p="KBUILD_CFLAGS += -I\$(srctree)/arch/\$(SRCARCH)/xenomai/include -I\$(srctree)/include/xenomai"
-		(echo; echo $p) | patch_append arch/$linux_arch/Makefile
-		p="core-\$(CONFIG_XENOMAI)	+= arch/$linux_arch/xenomai/"
-		echo $p | patch_append arch/$linux_arch/Makefile
+		p1="KBUILD_CFLAGS += -I\$(srctree)/arch/\$(SRCARCH)/xenomai/include -I\$(srctree)/include/xenomai"
+		p2="core-\$(CONFIG_XENOMAI)	+= arch/$linux_arch/xenomai/"
+		(echo; echo $p1; echo $p2) | patch_append arch/$linux_arch/Makefile
+	elif test x$reverse = x1; then
+		patch_reverse arch/$linux_arch/Makefile
 	fi
 
 	patch_architecture_specific="n"
@@ -369,11 +407,15 @@ case $linux_VERSION.$linux_PATCHLEVEL in
 	if ! grep -q CONFIG_XENOMAI $linux_tree/drivers/Makefile; then
 		p="obj-\$(CONFIG_XENOMAI)		+= xenomai/"
 		( echo ; echo $p ) | patch_append drivers/Makefile
+	elif test x$reverse = x1; then
+		patch_reverse drivers/Makefile
 	fi
 
 	if ! grep -q CONFIG_XENOMAI $linux_tree/kernel/Makefile; then
 		p="obj-\$(CONFIG_XENOMAI)		+= xenomai/"
 		( echo ; echo $p ) | patch_append kernel/Makefile
+	elif test x$reverse = x1; then
+		patch_reverse kernel/Makefile
 	fi
 	;;
 esac
@@ -381,13 +423,15 @@ esac
 # Create local directories then symlink to the source files from
 # there, so that we don't pollute the Xenomai source tree with
 # compilation files.
+#
+# Keep link dirs (4th parameter) descending, otherwise --reverse
+# is not able to cleanup these directories!
 
 patch_kernelversion_specific="n"
 patch_architecture_specific="y"
 patch_link r m kernel/cobalt/arch/$linux_arch arch/$linux_arch/xenomai
 patch_link n n kernel/cobalt/include/dovetail arch/$linux_arch/include/dovetail
 patch_architecture_specific="n"
-patch_link n m kernel/cobalt kernel/xenomai
 patch_link n cobalt-core.h kernel/cobalt/trace include/trace/events
 patch_link n cobalt-rtdm.h kernel/cobalt/trace include/trace/events
 patch_link n cobalt-posix.h kernel/cobalt/trace include/trace/events
@@ -395,14 +439,15 @@ patch_link r n kernel/cobalt/include/asm-generic/xenomai include/asm-generic/xen
 patch_link n m kernel/cobalt/posix kernel/xenomai/posix
 patch_link n m kernel/cobalt/rtdm kernel/xenomai/rtdm
 patch_link n m kernel/cobalt/dovetail kernel/xenomai/pipeline
+patch_link n m kernel/cobalt kernel/xenomai
 patch_link r m kernel/drivers drivers/xenomai
 patch_link n n include/cobalt/kernel include/xenomai/cobalt/kernel
-patch_link r n include/cobalt/kernel/rtdm include/xenomai/rtdm

scripts/prepare-kernel.sh patch_link r n kernel/cobalt/include/asm-generic/xenomai include/asm-generic/xen · line 453

Quoted 10 lines
scripts/prepare-kernel.sh +2 -1
 patch_link r n include/cobalt/kernel/dovetail/pipeline include/xenomai/pipeline
 patch_link r n include/cobalt/uapi include/xenomai/cobalt/uapi
 patch_link r n include/rtdm/uapi include/xenomai/rtdm/uapi
-patch_link n version.h include/xenomai include/xenomai
+patch_link r n include/cobalt/kernel/rtdm include/xenomai/rtdm
 patch_link n stdarg.h kernel/cobalt/include/linux include/xenomai/linux
+patch_link n version.h include/xenomai include/xenomai
 
 if test "x$output_patch" != "x"; then
 	if test x$verbose = x1; then

Thanks, both folded into next.

Jan

Signature · 2 lines
Siemens AG, Technology
Linux Expert Center