diff mbox series

[2/2] scripts: add kconfig lookup script

Message ID 20230801174922.333700-3-bmasney@redhat.com (mailing list archive)
State New, archived
Headers show
Series scripts: add two new scripts to look up Kconfigs | expand

Commit Message

Brian Masney Aug. 1, 2023, 5:49 p.m. UTC
Add a script that allows looking up the full Kconfig entry based on
the symbol name. Documentation and example usage is found at the top
of the script itself.

Signed-off-by: Brian Masney <bmasney@redhat.com>
---
 scripts/kconfig/lookup.sh | 77 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 77 insertions(+)
 create mode 100755 scripts/kconfig/lookup.sh

Comments

Masahiro Yamada Aug. 3, 2023, 6:23 a.m. UTC | #1
On Wed, Aug 2, 2023 at 2:49 AM Brian Masney <bmasney@redhat.com> wrote:
>
> Add a script that allows looking up the full Kconfig entry based on
> the symbol name. Documentation and example usage is found at the top
> of the script itself.
>
> Signed-off-by: Brian Masney <bmasney@redhat.com>
> ---
>  scripts/kconfig/lookup.sh | 77 +++++++++++++++++++++++++++++++++++++++
>  1 file changed, 77 insertions(+)
>  create mode 100755 scripts/kconfig/lookup.sh



Everyone tends to have their own utility scripts
on their machines.

I think this patch set falls into that category
as "create a wrapper script of grep" is what everyone
does to reduce typing.




FWIW, I have the following scripts in my ~/bin directory.



$ cat ~/bin/kgrep
#!/bin/sh

exec find . -name .repo -prune -o -name .git -prune -o -type f \
\( -name 'Kconfig*' -o -name 'Config.in' \) \
-print0 | xargs -0 grep --color -n "$@"


$ cat ~/bin/mgrep
#!/bin/sh

exec find . -name .repo -prune -o -name .git -prune -o -type f \
\( -name 'Makefile*' -o -name 'Kbuild*' -o -name "*.mk" \) \
-print0 | xargs -0 grep --color -n "$@"




masahiro@zoe:~/ref/linux(master)$ kgrep -A5 TSL2772
./drivers/iio/light/Kconfig:564:config TSL2772
./drivers/iio/light/Kconfig-565- tristate "TAOS TSL/TMD2x71 and
TSL/TMD2x72 Family of light and proximity sensors"
./drivers/iio/light/Kconfig-566- depends on I2C
./drivers/iio/light/Kconfig-567- help
./drivers/iio/light/Kconfig-568-   Support for: tsl2571, tsl2671,
tmd2671, tsl2771, tmd2771, tsl2572, tsl2672,
./drivers/iio/light/Kconfig-569-   tmd2672, tsl2772, tmd2772 devices.

masahiro@zoe:~/ref/linux(master)$ mgrep efivarfs.o
./fs/efivarfs/Makefile:6:obj-$(CONFIG_EFIVAR_FS) += efivarfs.o
./fs/efivarfs/Makefile:8:efivarfs-objs := inode.o file.o super.o vars.o



That's my local way to satisfy my demand.
I do not intend to force my way or merge it in the upstream.










>
> diff --git a/scripts/kconfig/lookup.sh b/scripts/kconfig/lookup.sh
> new file mode 100755
> index 000000000000..d1ff52b23835
> --- /dev/null
> +++ b/scripts/kconfig/lookup.sh
> @@ -0,0 +1,77 @@
> +#!/usr/bin/env bash
> +# SPDX-License-Identifier: GPL-2.0-only
> +#
> +# Copyright (C) 2023 Red Hat, Inc. All Rights Reserved.
> +# Written by Brian Masney <bmasney@redhat.com>
> +#
> +# This script takes as input one or more Kconfig symbols and outputs the full
> +# entry from the Kconfig file. It can be invoked by reading a list of symbol
> +# names from either stdin or as command line arguments. Example output:
> +#
> +#   x1:~/src/linux$ ./scripts/kconfig/lookup.sh TSL2772 SOUND
> +#   # drivers/iio/light/Kconfig
> +#   config TSL2772
> +#     tristate "TAOS TSL/TMD2x71 and TSL/TMD2x72 Family of light and proximity sensors"
> +#     depends on I2C
> +#     help
> +#       Support for: tsl2571, tsl2671, tmd2671, tsl2771, tmd2771, tsl2572, tsl2672,
> +#       tmd2672, tsl2772, tmd2772 devices.
> +#       Provides iio_events and direct access via sysfs.
> +#
> +#   # arch/um/drivers/Kconfig
> +#   config SOUND
> +#     tristate
> +#     default UML_SOUND
> +#
> +#   # sound/Kconfig
> +#   menuconfig SOUND
> +#     tristate "Sound card support"
> +#     depends on HAS_IOMEM
> +#     help
> +#       If you have a sound card in your computer, i.e. if it can say more
> +#       than an occasional beep, say Y.
> +
> +
> +process_kconfig()
> +{
> +       KCONFIG="${1/CONFIG_/}"
> +
> +       FOUND=0
> +       for KCONFIG_FILE in $(git grep -E "^(config|menuconfig) ${KCONFIG}$" | \
> +                             awk -F: '{print $1}') ; do
> +               echo "# ${KCONFIG_FILE}"
> +               awk "/^(config|menuconfig) ${KCONFIG}$/{ m=1; print; next; } \
> +                    /^(choice|comment|config|end|if|menuconfig|source)/ { m=0; } m" \
> +                   "${KCONFIG_FILE}"
> +               FOUND=1
> +       done
> +
> +       if [[ "${FOUND}" = "0" ]] ; then
> +               echo "Skipping ${KCONFIG} since Kconfig symbol is not found" >&2
> +               return 1
> +       fi
> +
> +}
> +
> +# Run this script from the toplevel kernel source directory.
> +SCRIPT_PATH=$(readlink -f "$0")
> +cd "$(dirname "${SCRIPT_PATH}")/../.." || exit 1
> +
> +RET=0
> +if [[ $# == 0 ]] ; then
> +       # Read Kconfig names from stdin
> +       while read -r KCONFIG ; do
> +               if ! process_kconfig "${KCONFIG}" ; then
> +                       RET=1
> +               fi
> +       done
> +else
> +       # Read Kconfig names from the command line arguments
> +       for NUM in $(seq 1 "$#") ; do
> +               if ! process_kconfig "${!NUM}" ; then
> +                       RET=1
> +               fi
> +       done
> +fi
> +
> +exit "${RET}"
> --
> 2.41.0
>


--
Best Regards

Masahiro Yamada
Brian Masney Aug. 3, 2023, 10:14 a.m. UTC | #2
On Thu, Aug 03, 2023 at 03:23:16PM +0900, Masahiro Yamada wrote:
> Everyone tends to have their own utility scripts
> on their machines.
> 
> I think this patch set falls into that category
> as "create a wrapper script of grep" is what everyone
> does to reduce typing.
> 
> 
> 
> 
> FWIW, I have the following scripts in my ~/bin directory.
> 
> 
> 
> $ cat ~/bin/kgrep
> #!/bin/sh
> 
> exec find . -name .repo -prune -o -name .git -prune -o -type f \
> \( -name 'Kconfig*' -o -name 'Config.in' \) \
> -print0 | xargs -0 grep --color -n "$@"
> 
> 
> $ cat ~/bin/mgrep
> #!/bin/sh
> 
> exec find . -name .repo -prune -o -name .git -prune -o -type f \
> \( -name 'Makefile*' -o -name 'Kbuild*' -o -name "*.mk" \) \
> -print0 | xargs -0 grep --color -n "$@"
> 
> 
> 
> 
> masahiro@zoe:~/ref/linux(master)$ kgrep -A5 TSL2772
> ./drivers/iio/light/Kconfig:564:config TSL2772
> ./drivers/iio/light/Kconfig-565- tristate "TAOS TSL/TMD2x71 and
> TSL/TMD2x72 Family of light and proximity sensors"
> ./drivers/iio/light/Kconfig-566- depends on I2C
> ./drivers/iio/light/Kconfig-567- help
> ./drivers/iio/light/Kconfig-568-   Support for: tsl2571, tsl2671,
> tmd2671, tsl2771, tmd2771, tsl2572, tsl2672,
> ./drivers/iio/light/Kconfig-569-   tmd2672, tsl2772, tmd2772 devices.
> 
> masahiro@zoe:~/ref/linux(master)$ mgrep efivarfs.o
> ./fs/efivarfs/Makefile:6:obj-$(CONFIG_EFIVAR_FS) += efivarfs.o
> ./fs/efivarfs/Makefile:8:efivarfs-objs := inode.o file.o super.o vars.o
> 
> 
> 
> That's my local way to satisfy my demand.
> I do not intend to force my way or merge it in the upstream.

OK, fair enough.

Those are useful little utilities and simpler than what I posted. If
something like these had been in the scripts/ directory, then I
wouldn't have spent the time to write yet another script that does
basically the same thing. I get what you are saying, however having
a script to lookup a Kconfig by name or module will be useful to other
people.

Brian
Masahiro Yamada Aug. 6, 2023, 12:45 p.m. UTC | #3
On Thu, Aug 3, 2023 at 7:14 PM Brian Masney <bmasney@redhat.com> wrote:
>
> On Thu, Aug 03, 2023 at 03:23:16PM +0900, Masahiro Yamada wrote:
> > Everyone tends to have their own utility scripts
> > on their machines.
> >
> > I think this patch set falls into that category
> > as "create a wrapper script of grep" is what everyone
> > does to reduce typing.
> >
> >
> >
> >
> > FWIW, I have the following scripts in my ~/bin directory.
> >
> >
> >
> > $ cat ~/bin/kgrep
> > #!/bin/sh
> >
> > exec find . -name .repo -prune -o -name .git -prune -o -type f \
> > \( -name 'Kconfig*' -o -name 'Config.in' \) \
> > -print0 | xargs -0 grep --color -n "$@"
> >
> >
> > $ cat ~/bin/mgrep
> > #!/bin/sh
> >
> > exec find . -name .repo -prune -o -name .git -prune -o -type f \
> > \( -name 'Makefile*' -o -name 'Kbuild*' -o -name "*.mk" \) \
> > -print0 | xargs -0 grep --color -n "$@"
> >
> >
> >
> >
> > masahiro@zoe:~/ref/linux(master)$ kgrep -A5 TSL2772
> > ./drivers/iio/light/Kconfig:564:config TSL2772
> > ./drivers/iio/light/Kconfig-565- tristate "TAOS TSL/TMD2x71 and
> > TSL/TMD2x72 Family of light and proximity sensors"
> > ./drivers/iio/light/Kconfig-566- depends on I2C
> > ./drivers/iio/light/Kconfig-567- help
> > ./drivers/iio/light/Kconfig-568-   Support for: tsl2571, tsl2671,
> > tmd2671, tsl2771, tmd2771, tsl2572, tsl2672,
> > ./drivers/iio/light/Kconfig-569-   tmd2672, tsl2772, tmd2772 devices.
> >
> > masahiro@zoe:~/ref/linux(master)$ mgrep efivarfs.o
> > ./fs/efivarfs/Makefile:6:obj-$(CONFIG_EFIVAR_FS) += efivarfs.o
> > ./fs/efivarfs/Makefile:8:efivarfs-objs := inode.o file.o super.o vars.o
> >
> >
> >
> > That's my local way to satisfy my demand.
> > I do not intend to force my way or merge it in the upstream.
>
> OK, fair enough.
>
> Those are useful little utilities and simpler than what I posted. If
> something like these had been in the scripts/ directory, then I
> wouldn't have spent the time to write yet another script that does
> basically the same thing. I get what you are saying, however having
> a script to lookup a Kconfig by name or module will be useful to other
> people.
>
> Brian
>

I do not want to fill the scripts/ directory
with random trivial scripts.

I need to draw a line between
"the time cost for writing the code by myself" vs
"the time cost for searching for a similar script".

I'd rather write the code by myself
if it is a script of a few lines.
diff mbox series

Patch

diff --git a/scripts/kconfig/lookup.sh b/scripts/kconfig/lookup.sh
new file mode 100755
index 000000000000..d1ff52b23835
--- /dev/null
+++ b/scripts/kconfig/lookup.sh
@@ -0,0 +1,77 @@ 
+#!/usr/bin/env bash
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# Copyright (C) 2023 Red Hat, Inc. All Rights Reserved.
+# Written by Brian Masney <bmasney@redhat.com>
+#
+# This script takes as input one or more Kconfig symbols and outputs the full
+# entry from the Kconfig file. It can be invoked by reading a list of symbol
+# names from either stdin or as command line arguments. Example output:
+#
+#   x1:~/src/linux$ ./scripts/kconfig/lookup.sh TSL2772 SOUND
+#   # drivers/iio/light/Kconfig
+#   config TSL2772
+#     tristate "TAOS TSL/TMD2x71 and TSL/TMD2x72 Family of light and proximity sensors"
+#     depends on I2C
+#     help
+#       Support for: tsl2571, tsl2671, tmd2671, tsl2771, tmd2771, tsl2572, tsl2672,
+#       tmd2672, tsl2772, tmd2772 devices.
+#       Provides iio_events and direct access via sysfs.
+#
+#   # arch/um/drivers/Kconfig
+#   config SOUND
+#     tristate
+#     default UML_SOUND
+#
+#   # sound/Kconfig
+#   menuconfig SOUND
+#     tristate "Sound card support"
+#     depends on HAS_IOMEM
+#     help
+#       If you have a sound card in your computer, i.e. if it can say more
+#       than an occasional beep, say Y.
+
+
+process_kconfig()
+{
+	KCONFIG="${1/CONFIG_/}"
+
+	FOUND=0
+	for KCONFIG_FILE in $(git grep -E "^(config|menuconfig) ${KCONFIG}$" | \
+	                      awk -F: '{print $1}') ; do
+		echo "# ${KCONFIG_FILE}"
+		awk "/^(config|menuconfig) ${KCONFIG}$/{ m=1; print; next; } \
+		     /^(choice|comment|config|end|if|menuconfig|source)/ { m=0; } m" \
+		    "${KCONFIG_FILE}"
+		FOUND=1
+	done
+
+	if [[ "${FOUND}" = "0" ]] ; then
+		echo "Skipping ${KCONFIG} since Kconfig symbol is not found" >&2
+		return 1
+	fi
+
+}
+
+# Run this script from the toplevel kernel source directory.
+SCRIPT_PATH=$(readlink -f "$0")
+cd "$(dirname "${SCRIPT_PATH}")/../.." || exit 1
+
+RET=0
+if [[ $# == 0 ]] ; then
+	# Read Kconfig names from stdin
+	while read -r KCONFIG ; do
+		if ! process_kconfig "${KCONFIG}" ; then
+			RET=1
+		fi
+	done
+else
+	# Read Kconfig names from the command line arguments
+	for NUM in $(seq 1 "$#") ; do
+		if ! process_kconfig "${!NUM}" ; then
+			RET=1
+		fi
+	done
+fi
+
+exit "${RET}"