From: Breno Rodrigues Alves <breno3011alves@gmail.com>
To: linux-kernel@vger.kernel.org
Cc: Andy Whitcroft <apw@canonical.com>, Joe Perches <joe@perches.com>,
Dwaipayan Ray <dwaipayanray1@gmail.com>,
Lukas Bulwahn <lukas.bulwahn@gmail.com>,
Breno Rodrigues Alves <breno3011alves@gmail.com>
Subject: [RFC PATCH v2 4/7] scripts: convert headerdep.pl to Python
Date: Sun, 18 Oct 2026 10:57:19 -0300 [thread overview]
Message-ID: <20261018135722.27410-5-breno3011alves@gmail.com> (raw)
In-Reply-To: <20261018135722.27410-1-breno3011alves@gmail.com>
Faithful re-implementation of headerdep.pl in Python 3
(stdlib only). Same include graph construction, same cycle
detection, same GraphViz output mode (--graph).
Verified byte-for-byte identical to the Perl version on a
synthetic cyclic include chain.
Signed-off-by: Breno Rodrigues Alves <breno3011alves@gmail.com>
---
| 137 +++++++++++++++++++++++++++++++++++++++++++
1 file changed, 137 insertions(+)
create mode 100755 scripts/headerdep.py
--git a/scripts/headerdep.py b/scripts/headerdep.py
new file mode 100755
index 000000000..4b9a57ab1
--- /dev/null
+++ b/scripts/headerdep.py
@@ -0,0 +1,137 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0
+#
+# Detect cycles in the header file dependency graph.
+# Vegard Nossum <vegardno@ifi.uio.no>
+
+import argparse
+import os
+import re
+import sys
+
+deps = {}
+opt_include = []
+
+
+def strip(filename):
+ for inc in opt_include:
+ prefix = inc + '/'
+ if filename.startswith(prefix):
+ return filename[len(prefix):]
+ return filename
+
+
+def search(filename):
+ if os.path.isfile(filename):
+ return filename
+ for inc in opt_include:
+ path = f'{inc}/{filename}'
+ if os.path.isfile(path):
+ return path
+ return None
+
+
+def parse_all(headers):
+ queue = list(headers)
+ while queue:
+ h = queue.pop()
+ if h in deps:
+ continue
+ deps[h] = []
+ path = search(h)
+ if path is None:
+ continue
+ with open(path) as f:
+ lines = f.readlines()
+ for i, line in enumerate(lines):
+ m = re.match(r'^#\s*include\s*<(.*?)>', line)
+ if m:
+ dep = m.group(1)
+ queue.append(dep)
+ deps[h].append((i + 1, dep))
+
+
+def print_cycle(cycle):
+ # Shift line numbers to align with the "In file included" message
+ cycle = list(cycle)
+ for i in range(len(cycle) - 1):
+ cycle[i] = (cycle[i + 1][0], cycle[i][1])
+ cycle[-1] = (0, cycle[-1][1])
+
+ first = cycle.pop(0)
+ last = cycle.pop() if cycle else None
+
+ msg = 'In file included'
+ if last is not None:
+ print(f'{msg} from {last[1]},')
+ for hdr in reversed(cycle):
+ marker = ' <-- here' if last is not None and hdr[1] == last[1] else ''
+ print(f'{" " * len(msg)} from {hdr[1]}:{hdr[0]}{marker}')
+ print(f'{first[1]}:{first[0]}: warning: recursive header inclusion')
+
+
+def detect_cycles(headers, opt_all):
+ queue = [[(0, h)] for h in headers]
+ while queue:
+ top = queue.pop()
+ name = top[-1][1]
+ for dep in deps.get(name, []):
+ chain = top + [(dep[0], dep[1])]
+ if any(c[1] == dep[1] for c in top):
+ print_cycle(chain)
+ if not opt_all:
+ return
+ continue
+ queue.append(chain)
+
+
+def mangle(name):
+ return name.replace('/', '__').replace('.', '_').replace('-', '_')
+
+
+def graph():
+ print('digraph {')
+ print()
+ print('\t/* vertices */')
+ for h in deps:
+ print(f'\t{mangle(h)} [label="{h}"];')
+ print()
+ print('\t/* edges */')
+ for h in deps:
+ for dep in deps[h]:
+ print(f'\t{mangle(h)} -> {mangle(dep[1])};')
+ print('}')
+
+
+def main(argv):
+ global opt_include
+
+ p = argparse.ArgumentParser(add_help=False)
+ p.add_argument('--all', action='store_true')
+ p.add_argument('--graph', action='store_true')
+ p.add_argument('-I', dest='includes', action='append', default=[])
+ p.add_argument('files', nargs='*')
+ p.add_argument('-h', '--help', '--version', action='store_true')
+ args = p.parse_args(argv)
+
+ if args.help:
+ print(f'Usage: {sys.argv[0]} [options] file...')
+ return 0
+
+ opt_include = list(args.includes) + ['include']
+ # Match Perl: grep { strip($_) } keeps the ORIGINAL path, using
+ # strip() only as a truthiness filter. This makes deps keys
+ # consistent with the Perl version (e.g. 'include/x.h', not 'x.h').
+ headers = [h for h in args.files if strip(h)]
+
+ parse_all(headers)
+
+ if args.graph:
+ graph()
+ else:
+ detect_cycles(headers, args.all)
+ return 0
+
+
+if __name__ == '__main__':
+ sys.exit(main(sys.argv[1:]))
\ No newline at end of file
--
2.43.0
next prev parent reply other threads:[~2026-10-18 13:57 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-18 11:28 [RFC PATCH 0/7] scripts: convert remaining Perl utilities in scripts/ Breno Rodrigues Alves
2026-10-18 11:28 ` [RFC PATCH 1/7] scripts: convert checkstack.pl to Python Breno Rodrigues Alves
2026-10-18 11:28 ` [RFC PATCH 2/7] scripts: convert extract-module-sig.pl " Breno Rodrigues Alves
2026-10-18 11:28 ` [RFC PATCH 3/7] scripts: convert extract-sys-certs.pl " Breno Rodrigues Alves
2026-10-18 11:28 ` [RFC PATCH 4/7] scripts: convert headerdep.pl " Breno Rodrigues Alves
2026-10-18 11:28 ` [RFC PATCH 5/7] scripts: convert kconfig-sym-check.pl " Breno Rodrigues Alves
2026-10-18 11:28 ` [RFC PATCH 6/7] scripts: convert parse-maintainers.pl " Breno Rodrigues Alves
2026-10-18 11:28 ` [RFC PATCH 7/7] scripts: convert streamline_config.pl " Breno Rodrigues Alves
2026-10-18 13:57 ` [RFC PATCH v2 0/7] scripts: convert remaining Perl utilities in scripts/ Breno Rodrigues Alves
2026-10-18 13:57 ` [RFC PATCH v2 1/7] scripts: convert checkstack.pl to Python Breno Rodrigues Alves
2026-10-18 13:57 ` [RFC PATCH v2 2/7] scripts: convert extract-module-sig.pl " Breno Rodrigues Alves
2026-10-18 13:57 ` [RFC PATCH v2 3/7] scripts: convert extract-sys-certs.pl " Breno Rodrigues Alves
2026-10-18 13:57 ` Breno Rodrigues Alves [this message]
2026-10-18 13:57 ` [RFC PATCH v2 5/7] scripts: convert kconfig-sym-check.pl " Breno Rodrigues Alves
2026-10-18 13:57 ` [RFC PATCH v2 6/7] scripts: convert parse-maintainers.pl " Breno Rodrigues Alves
2026-10-18 13:57 ` [RFC PATCH v2 7/7] scripts: convert streamline_config.pl " Breno Rodrigues Alves
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=20261018135722.27410-5-breno3011alves@gmail.com \
--to=breno3011alves@gmail.com \
--cc=apw@canonical.com \
--cc=dwaipayanray1@gmail.com \
--cc=joe@perches.com \
--cc=linux-kernel@vger.kernel.org \
--cc=lukas.bulwahn@gmail.com \
/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
all inboxes | Powered by JetHome®