Skip to contentSkip to conversation

scripts/config: honor $KBUILD_OUTPUT by default

PATCH In review Dmitry Voytik 3 participants 4 messages Nov 17 – Nov 18 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH v8 1/1 scripts/config: honor $KBUILD_OUTPUT by default

If $KBUILD_OUTPUT is set, use it by default if --file is not passed.

Fix the issue when the script silently updates a stale .config in the root directory, while the user expects ${KBUILD_OUTPUT}/.config to be updated.
Handle properly if the config file is a relative path.

The problem can be worked around with --file, but this is easy to miss, and is inconsistent with how the Makefile and other tools work (e.g., scripts/diffconfig).

How to reproduce the issue:

	make mrproper
	export KBUILD_OUTPUT=.out
	make defconfig
	./scripts/config \
	       -e CONFIG_UBSAN

Error reported:
grep: .config: No such file or directory

---
How the patch was tested:

	make mrproper
	export KBUILD_OUTPUT=.out
	make defconfig
	grep "CONFIG_UBSAN=y" $KBUILD_OUTPUT/.config || echo "OK"

Test the basic usage:

	./scripts/config -e CONFIG_UBSAN
	grep "CONFIG_UBSAN=y" $KBUILD_OUTPUT/.config && echo "OK"

Test --refresh picks up correct config file by using $KBUILD_OUTPUT:

	./scripts/config -d CONFIG_UBSAN
	grep "CONFIG_UBSAN=y" $KBUILD_OUTPUT/.config && echo "FAIL"
	./scripts/config --refresh -e CONFIG_UBSAN
	grep "CONFIG_UBSAN=y" $KBUILD_OUTPUT/.config && echo "OK"

Test --file takes precedence over $KBUILD_OUTPUT and a relative path is handled properly:

	./scripts/config -d CONFIG_UBSAN
	mkdir .out2
	cp .out/.config .out2/.my_config
	grep "CONFIG_UBSAN=y" .out2/.my_config  && echo "FAIL"
	./scripts/config --file .out2/.my_config --refresh \
	       -e CONFIG_UBSAN
	grep "CONFIG_UBSAN=y" .out2/.my_config && echo "OK"
	ls .out/.out2 && echo "FAIL"
	ls .out/.out && echo "FAIL"

Test --file can accept files with hyphen:

	./scripts/config -d CONFIG_UBSAN
	cp .out/.config -- -my_config
	grep "CONFIG_UBSAN=y" -- -my_config && echo "FAIL"
	./scripts/config  --refresh \
	       -e CONFIG_UBSAN --file -my_config
	grep "CONFIG_UBSAN=y" -- -my_config && echo "OK"

---
v8:
* Nathan Chancellor ironed out the patch to the acceptable state
v7:
* Adressed https://sashiko.dev/#/patchset/20261006142337.821859-1-voytikd%40gmail.com
v6:
* Per Nathan Chancellor's suggestions, simplified
* https://lore.kernel.org/lkml/179121581235.2275809.12565811683284940522.b4-review@b4/
v5:
* Per Nathan Chancellor's suggestion, improved handling $FN path by realpath
* https://lore.kernel.org/lkml/179097210878.309835.10523307262544578337.b4-review@b4/
* Documented the new test scenario in the git commit message
v4:
* Addressed Nicolas Schier's comments - dropped the change how '--refresh' is handled
* Implemented conversion of $FN to the absolute path which helps with '--refresh' and relative paths
* Documented testing steps in the commit message
v3:
* Added an example in the commit message how to reproduce the issue
v2:
* Addressed comments by sashiko:
* https://sashiko.dev/#/patchset/20260920064350.17999-1-voytikd%40gmail.com
* use basename when --refresh is used
* remove echo which can be misleading when --file is passed
v1:
* https://lore.kernel.org/all/[email protected]/

---
 scripts/config | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)
scripts/config +19 -3
index 32428ea909c2..13b87b7f316a 100755
@@ -144,6 +144,22 @@ while [[ $# -gt 0 ]]; do
 	fi
 done
 
+# If no '--file' was specified, use KBUILD_OUTPUT as the base if it exists
+if [[ "$FN" = ".config" && -n "$KBUILD_OUTPUT" ]]; then
+	if [[ ! -d "$KBUILD_OUTPUT" ]]; then
+		echo "KBUILD_OUTPUT is set ('$KBUILD_OUTPUT') but does not exist or is not a directory." >&2
+		echo "Create it before running $myname or use '--file' to specify configuration file." >&2
+		exit 1
+	fi
+	FN="$KBUILD_OUTPUT/$FN"
+fi
+# Turn FN into an absolute path if it is relative so that '--refresh' works on
+# the correct file. This does not use realpath, as $FN may not exist yet.
+case "$FN" in
+	/*) ;;
+	*) FN="$PWD/$FN" ;;
+esac
+
 set -- "${CMDS[@]}"
 if [ "$1" = "" ] ; then
 	usage
@@ -200,10 +216,10 @@ while [ "$1" != "" ] ; do
 		;;
 
 	--state|-s)
-		if grep -q "# ${CONFIG_}$ARG is not set" $FN ; then
+		if grep -q "# ${CONFIG_}$ARG is not set" "$FN" ; then
 			echo n
 		else
-			V="$(grep "^${CONFIG_}$ARG=" $FN)"
+			V="$(grep "^${CONFIG_}$ARG=" "$FN")"
 			if [ $? != 0 ] ; then
 				echo undef
 			else
@@ -229,7 +245,7 @@ while [ "$1" != "" ] ; do
 		;;
 
 	--refresh)
-		yes "" | make oldconfig KCONFIG_CONFIG=$FN
+		yes "" | make oldconfig KCONFIG_CONFIG="$FN"
 		;;
 
 	*)
Signature · 1 line
2.56.1
Tested-by Reviewed-by on v8 1/1
#1 lore raw

Hi Dmitry,

Quoted 1 line · Dmitry Voytik

If $KBUILD_OUTPUT is set, use it by default if --file is not passed.

I guess this isn't inaccurate, but it should perhaps mention for completeness that 'scripts/config' will also now default to using '$KBUILD_OUTPUT' if passing '.config' with '--file'.

Quoted 96 lines

Fix the issue when the script silently updates a stale .config in the root directory, while the user expects ${KBUILD_OUTPUT}/.config to be updated.
Handle properly if the config file is a relative path.

The problem can be worked around with --file, but this is easy to miss, and is inconsistent with how the Makefile and other tools work (e.g., scripts/diffconfig).

How to reproduce the issue:

	make mrproper
	export KBUILD_OUTPUT=.out
	make defconfig
	./scripts/config \
	       -e CONFIG_UBSAN

Error reported:
grep: .config: No such file or directory

---
How the patch was tested:

	make mrproper
	export KBUILD_OUTPUT=.out
	make defconfig
	grep "CONFIG_UBSAN=y" $KBUILD_OUTPUT/.config || echo "OK"

Test the basic usage:

	./scripts/config -e CONFIG_UBSAN
	grep "CONFIG_UBSAN=y" $KBUILD_OUTPUT/.config && echo "OK"

Test --refresh picks up correct config file by using $KBUILD_OUTPUT:

	./scripts/config -d CONFIG_UBSAN
	grep "CONFIG_UBSAN=y" $KBUILD_OUTPUT/.config && echo "FAIL"
	./scripts/config --refresh -e CONFIG_UBSAN
	grep "CONFIG_UBSAN=y" $KBUILD_OUTPUT/.config && echo "OK"

Test --file takes precedence over $KBUILD_OUTPUT and a relative path is handled properly:

	./scripts/config -d CONFIG_UBSAN
	mkdir .out2
	cp .out/.config .out2/.my_config
	grep "CONFIG_UBSAN=y" .out2/.my_config  && echo "FAIL"
	./scripts/config --file .out2/.my_config --refresh \
	       -e CONFIG_UBSAN
	grep "CONFIG_UBSAN=y" .out2/.my_config && echo "OK"
	ls .out/.out2 && echo "FAIL"
	ls .out/.out && echo "FAIL"

Test --file can accept files with hyphen:

	./scripts/config -d CONFIG_UBSAN
	cp .out/.config -- -my_config
	grep "CONFIG_UBSAN=y" -- -my_config && echo "FAIL"
	./scripts/config  --refresh \
	       -e CONFIG_UBSAN --file -my_config
	grep "CONFIG_UBSAN=y" -- -my_config && echo "OK"

---
v8:
* Nathan Chancellor ironed out the patch to the acceptable state
v7:
* Adressed https://sashiko.dev/#/patchset/20261006142337.821859-1-voytikd%40gmail.com
v6:
* Per Nathan Chancellor's suggestions, simplified
* https://lore.kernel.org/
lkml/179121581235.2275809.12565811683284940522.b4-review@b4/
v5:
* Per Nathan Chancellor's suggestion, improved handling $FN path by realpath
* https://lore.kernel.org/
lkml/179097210878.309835.10523307262544578337.b4-review@b4/
* Documented the new test scenario in the git commit message
v4:
* Addressed Nicolas Schier's comments - dropped the change how '--refresh' is handled
* Implemented conversion of $FN to the absolute path which helps with '--refresh' and relative paths
* Documented testing steps in the commit message
v3:
* Added an example in the commit message how to reproduce the issue
v2:
* Addressed comments by sashiko:
* https://sashiko.dev/#/patchset/20260920064350.17999-1-voytikd%40gmail.com
* use basename when --refresh is used
* remove echo which can be misleading when --file is passed
v1:
* https://lore.kernel.org/all/[email protected]/

---
 scripts/config | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)
scripts/config +0 -0
index 32428ea909c2..13b87b7f316a 100755

scripts/config while [[ $# -gt 0 ]]; do · line 148

Quoted 5 lines
scripts/config +2 -0
@@ -144,6 +144,22 @@ while [[ $# -gt 0 ]]; do
 	fi
 done
 
+# If no '--file' was specified, use KBUILD_OUTPUT as the base if it exists
+if [[ "$FN" = ".config" && -n "$KBUILD_OUTPUT" ]]; then

Right so this will give different results with:

    '--file .config'
and:
    '--file ./.config'

but maybe that was intentional -- i.e. we're assuming a user passing '.config' intends it to be '$KBUILD_OUTPUT/.config' while a user passing './.config' would intend CWD/.config?

Quoted 32 lines
scripts/config +16 -2
+	if [[ ! -d "$KBUILD_OUTPUT" ]]; then
+		echo "KBUILD_OUTPUT is set ('$KBUILD_OUTPUT') but does not exist or is not a directory." >&2
+		echo "Create it before running $myname or use '--file' to specify configuration file." >&2
+		exit 1
+	fi
+	FN="$KBUILD_OUTPUT/$FN"
+fi
+# Turn FN into an absolute path if it is relative so that '--refresh' works on
+# the correct file. This does not use realpath, as $FN may not exist yet.
+case "$FN" in
+	/*) ;;
+	*) FN="$PWD/$FN" ;;
+esac
+
 set -- "${CMDS[@]}"
 if [ "$1" = "" ] ; then
 	usage
@@ -200,10 +216,10 @@ while [ "$1" != "" ] ; do
 		;;
 
 	--state|-s)
-		if grep -q "# ${CONFIG_}$ARG is not set" $FN ; then
+		if grep -q "# ${CONFIG_}$ARG is not set" "$FN" ; then
 			echo n
 		else
-			V="$(grep "^${CONFIG_}$ARG=" $FN)"
+			V="$(grep "^${CONFIG_}$ARG=" "$FN")"
 			if [ $? != 0 ] ; then
 				echo undef
 			else

scripts/config while [ "$1" != "" ] ; do

Quoted 9 lines
scripts/config +1 -2
@@ -229,7 +245,7 @@ while [ "$1" != "" ] ; do
 		;;
 
 	--refresh)
-		yes "" | make oldconfig KCONFIG_CONFIG=$FN
+		yes "" | make oldconfig KCONFIG_CONFIG="$FN"
 		;;
 
 	*)
-- 2.56.1
#2 lore raw
Quoted 5 lines · Julian Braha

Review

Quoted 2 lines · Dmitry Voytik
(quoted diff) +2 -0
+# If no '--file' was specified, use KBUILD_OUTPUT as the base if it exists
+if [[ "$FN" = ".config" && -n "$KBUILD_OUTPUT" ]]; then

Right so this will give different results with:

Quoted 7 lines
    '--file .config'
and:
    '--file ./.config'

but maybe that was intentional -- i.e. we're assuming a user passing '.config' intends it to be '$KBUILD_OUTPUT/.config' while a user passing './.config' would intend CWD/.config?

It was intentional since I had figured they were passing the default value so they should get the default behavior. Maybe it would be better to only do this when no '--file' value was provided period though to avoid some unexpected behavior?

scripts/config +21 -3
index 32428ea909c2..b09058ea1815 100755
@@ -130,12 +130,14 @@ on_exit() {
 trap on_exit EXIT
 
 FN=.config
+USER_SET_FILE=0
 CMDS=()
 while [[ $# -gt 0 ]]; do
 	if [ "$1" = "--file" ]; then
 		if [ "$2" = "" ]; then
 			usage
 		fi
+		USER_SET_FILE=1
 		FN="$2"
 		shift 2
 	else
@@ -144,6 +146,22 @@ while [[ $# -gt 0 ]]; do
 	fi
 done
 
+# If no '--file' was specified, use KBUILD_OUTPUT as the base if it exists
+if [[ "$USER_SET_FILE" = "0" && -n "$KBUILD_OUTPUT" ]]; then
+	if [[ ! -d "$KBUILD_OUTPUT" ]]; then
+		echo "KBUILD_OUTPUT is set ('$KBUILD_OUTPUT') but does not exist or is not a directory." >&2
+		echo "Create it before running $myname or use '--file' to specify configuration file." >&2
+		exit 1
+	fi
+	FN="$KBUILD_OUTPUT/$FN"
+fi
+# Turn FN into an absolute path if it is relative so that '--refresh' works on
+# the correct file. This does not use realpath, as $FN may not exist yet.
+case "$FN" in
+	/*) ;;
+	*) FN="$PWD/$FN" ;;
+esac
+
 set -- "${CMDS[@]}"
 if [ "$1" = "" ] ; then
 	usage
@@ -200,10 +218,10 @@ while [ "$1" != "" ] ; do
 		;;
 
 	--state|-s)
-		if grep -q "# ${CONFIG_}$ARG is not set" $FN ; then
+		if grep -q "# ${CONFIG_}$ARG is not set" "$FN" ; then
 			echo n
 		else
-			V="$(grep "^${CONFIG_}$ARG=" $FN)"
+			V="$(grep "^${CONFIG_}$ARG=" "$FN")"
 			if [ $? != 0 ] ; then
 				echo undef
 			else
@@ -229,7 +247,7 @@ while [ "$1" != "" ] ; do
 		;;
 
 	--refresh)
-		yes "" | make oldconfig KCONFIG_CONFIG=$FN
+		yes "" | make oldconfig KCONFIG_CONFIG="$FN"
 		;;
 
 	*)
Signature · 2 lines
Cheers,
Nathan
#3 lore raw
Quoted 9 lines · Nathan Chancellor
Quoted 8 lines · Julian Braha

Review

Quoted 2 lines · Dmitry Voytik
(quoted diff) +2 -0
+# If no '--file' was specified, use KBUILD_OUTPUT as the base if it exists
+if [[ "$FN" = ".config" && -n "$KBUILD_OUTPUT" ]]; then

Right so this will give different results with:

    '--file .config'
and:
    '--file ./.config'
Quoted 7 lines
Quoted 3 lines

but maybe that was intentional -- i.e. we're assuming a user passing '.config' intends it to be '$KBUILD_OUTPUT/.config' while a user passing './.config' would intend CWD/.config?

It was intentional since I had figured they were passing the default value so they should get the default behavior. Maybe it would be better to only do this when no '--file' value was provided period though to avoid some unexpected behavior?

All good, I think it's reasonable. Just wanted to confirm the intention

- Julian Braha