Linux kbuild/kconfig development
 help / color / mirror / Atom feed
From: Nicolas Schier <nsc@kernel.org>
To: Matthew Weber <matthew.l.weber@gmail.com>
Cc: Jonathan Corbet <corbet@lwn.net>,
	linux-doc@vger.kernel.org, Shuah Khan <skhan@linuxfoundation.org>,
	Thorsten Leemhuis <linux@leemhuis.info>,
	Nathan Chancellor <nathan@kernel.org>,
	linux-kbuild@vger.kernel.org, Dmitry Vyukov <dvyukov@google.com>,
	Andrey Konovalov <andreyknvl@gmail.com>,
	kasan-dev@googlegroups.com
Subject: Re: [PATCH 2/4] scripts: Add kernel-sloc helper script
Date: Thu, 5 Nov 2026 07:58:45 +0100	[thread overview]
Message-ID: <auwpJUhQ6VSz7y65@derry.ads.avm.de> (raw)
In-Reply-To: <20261103150845.13008-3-matthew.l.weber@gmail.com>

On Tue, Nov 03, 2026 at 09:08:43AM -0600, Matthew Weber wrote:
> Add a helper script scripts/kernel-sloc to count source lines of code
> (SLOC) for a configured Linux kernel build. It supports counting whole
> compiled sources from compile_commands.json as well as estimating
> distinct source lines represented in a vmlinux ELF using DWARF
> line-table addresses.
> 
> It is intended for system integrators and application developers who
> analyze a trimmed kernel, for example for safety critical applications,
> and need to report how much kernel code a given configuration builds
> and links. Add a MAINTAINERS entry for the script.
> 
> Signed-off-by: Matthew Weber <matthew.l.weber@gmail.com>
> ---
>  MAINTAINERS         |   6 +
>  scripts/kernel-sloc | 299 ++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 305 insertions(+)
>  create mode 100755 scripts/kernel-sloc
> 

Thanks for your patches.

Disclaimer: I did not really look into your patch set yet, but let me
note some major formal things ahead:

Something got heavily wrong with the patch set mails: while it looks
like this patch is number 2 from a series of 4 patches, the other
patches sent in the series are not numbered correctly.  Patch management
tooling (b4) even recognises this patch set as three different versions
of a patch set, thus reviewers/maintainers cannot use that here.
Usually, 'git send-email' takes care of that in a very proper way.  Did
you sent your patches one-by-one in some manual way?

> diff --git a/MAINTAINERS b/MAINTAINERS
> index d009e2da2..d83f37c3a 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -13762,6 +13762,12 @@ T:	git git://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest.git
>  F:	Documentation/dev-tools/kselftest*
>  F:	tools/testing/selftests/
>  
> +KERNEL SLOC HELPER
> +M:	Matthew Weber <matthew.l.weber@gmail.com>
> +L:	linux-kbuild@vger.kernel.org
> +S:	Maintained
> +F:	scripts/kernel-sloc

If 'kernel-sloc' is accepted, we will have to put it to scripts/contrib/,
as 'kernel-sloc' is not part of the Linux build.  'scripts/contrib/'
does not exist yet, but we are urged to stop mixing build-related
scripts with non-build-related ones in scripts/.


> +
>  KERNEL SMB3 SERVER (KSMBD)
>  M:	Namjae Jeon <linkinjeon@kernel.org>
>  M:	Namjae Jeon <linkinjeon@samba.org>
> diff --git a/scripts/kernel-sloc b/scripts/kernel-sloc
> new file mode 100755
> index 000000000..ed8b787e5
> --- /dev/null
> +++ b/scripts/kernel-sloc
> @@ -0,0 +1,299 @@
> +#!/bin/sh
> +# SPDX-License-Identifier: BSD-3-Clause

Almost all files below scripts/ are licensed under GPL.  Please consider
to switch to some GPL licensing or dual-license for consistency.

> +#
> +# kernel-sloc - Count source lines of code for a configured kernel build.
> +#
> +# Adapted from the Unikraft build-time SLOC reporting approach
> +# (https://github.com/orgs/unikraft/discussions/1849#discussioncomment-19352529),
> +# which counts compiled sources rather than the whole source tree.
> +#
> +# The kernel mode counts whole compiled files, including inactive #if branches,
> +# but not the headers they include. The dwarf mode estimates source lines
> +# represented by debug information in a linked ELF; it does not measure runtime
> +# coverage. Neither count is a certification or structural-coverage result.
> +
> +set -eu
> +
> +die() {
> +	echo "ERROR: $*" >&2
> +	exit 1
> +}
> +
> +need_command() {
> +	command -v "$1" >/dev/null 2>&1 || die "required command not found: $1"
> +}
> +
> +need_cloc() {
> +	command -v cloc >/dev/null 2>&1 ||
> +		die "required command not found: cloc (install cloc to use kernel, tree, or files mode)"
> +}
> +
> +usage() {
> +	printf 'Usage: %s [--report JSON_FILE] MODE TARGET...\n' "$0"
> +	printf '%s\n' \
> +		'  kernel BUILD_DIR  Count whole compiled sources from compile_commands.json.' \
> +		'                    Requires cloc and python3. Generate the database after' \
> +		'                    a clean build of the configuration being measured.' \
> +		'  dwarf ELF         Estimate distinct source lines using DWARF addresses.' \
> +		'                    Requires GNU objdump, addr2line, and python3, plus' \
> +		'                    an ELF built with debug information (CONFIG_DEBUG_INFO).' \
> +		'                    Set OBJDUMP and ADDR2LINE for cross builds, or set' \
> +		'                    CROSS_COMPILE to the toolchain prefix.' \
> +		'  tree DIR          Count all C, C++, and header sources; requires cloc.' \
> +		'  files FILE...     Count individual source files; requires cloc.' \
> +		'  --report FILE     JSON inventory for kernel or dwarf; parent must exist.' \
> +		'                    Lists compiled files, or DWARF source_lines and functions.' \
> +		'                    Does not measure execution or list every linked symbol.' \
> +		'  -h, --help        Show this help and exit successfully.' \
> +		'' \
> +		'Success prints one integer on stdout and a summary on stderr.' \
> +		'Errors exit non-zero without printing a count.' \
> +		'Counts depend on the tool versions and build options; record both.' \
> +		'' \
> +		'Examples:'
> +	printf '  %s --report compiled.json kernel .\n' "$0"
> +	printf '  %s --report linked.json dwarf vmlinux\n' "$0"
> +	printf '  %s files foo.c\n' "$0"
> +	printf '  %s tree /path/to/files\n' "$0"
> +	printf '%s\n' \
> +		'files omits linked libc; tree includes unused C/C++/headers, not other' \
> +		'languages. Neither is a deployed-footprint total.'

bike-shedding:

cat <<-eof
...
eof

might be valuable alternative to all those 'printf' calls.

> +}
> +
> +# Extract the code total from cloc CSV, including the empty-tree case.
> +cloc_sum() {
> +	awk -F',' '$2 == "SUM" {total = $5} END {print total + 0}'
> +}
> +
> +cloc_sum_from_list() {
> +	cloc --list-file="$1" --skip-uniqueness --csv --quiet >"$2" ||
> +		die "cloc failed while counting source files"
> +	cloc_sum <"$2"
> +}
> +
> +# Write the report before printing the count; failed writes are not success.
> +write_report() {
> +	[ -n "$report" ] || return 0
> +	python3 - "$report" "$mode" "$target" "$1" "$2" <<'PYREPORT'

Please do not embedd more than a handful of Python code lines in a shell
script.  Common Python linting is simplier in a pure Python script.  And
if Python is needed for the task, why is it wrapped in shell instead of
implementing the whole thing in Python?

I'd like to read your reply before reviewing this any further.

Kind regards,
Nicolas

  reply	other threads:[~2026-11-05  7:00 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-03 15:08 [PATCH 0/4] Documentation: Add kernel minimization guide Matthew Weber
2026-11-03 15:08 ` [PATCH] Documentation: kcov: clarify coverage example Matthew Weber
2026-11-03 15:08 ` [PATCH 2/4] scripts: Add kernel-sloc helper script Matthew Weber
2026-11-05  6:58   ` Nicolas Schier [this message]
2026-11-05 20:05     ` Nathan Chancellor
2026-11-03 15:08 ` [PATCH] Documentation: admin-guide: Add kernel minimization guide Matthew Weber
2026-11-04 15:09   ` sashiko-bot
2026-11-03 15:08 ` [PATCH] Documentation: admin-guide: Update workload tracing guide Matthew Weber

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=auwpJUhQ6VSz7y65@derry.ads.avm.de \
    --to=nsc@kernel.org \
    --cc=andreyknvl@gmail.com \
    --cc=corbet@lwn.net \
    --cc=dvyukov@google.com \
    --cc=kasan-dev@googlegroups.com \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kbuild@vger.kernel.org \
    --cc=linux@leemhuis.info \
    --cc=matthew.l.weber@gmail.com \
    --cc=nathan@kernel.org \
    --cc=skhan@linuxfoundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox