mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] scripts: Add utility to convert make's output into JSON
@ 2024-03-19 21:24 Nícolas F. R. A. Prado
  0 siblings, 0 replies; only message in thread
From: Nícolas F. R. A. Prado @ 2024-03-19 21:24 UTC (permalink / raw)
  To: Masahiro Yamada, Rob Herring
  Cc: kernel, linux-kernel, Nícolas F. R. A. Prado

The output from make isn't well suited for machines: reading it requires
hardcoding details of the formatting of commands from make.

Add a script that parses the output from make and outputs it in a well
structured format in JSON. This allows third party programs to parse the
make output without caring about formatting details, which stay in the
kernel tree.

For a given command line from make's output:
  DTC_CHK arch/arm64/boot/dts/actions/s700-cubieboard7.dtb

where DTC_CHK is the command, arch/.../s700-cubieboard7.dtb is the
target and any non-command lines that follow it are the command's
output, the generated JSON output is in the format:
[
    ...,
    {
        "command": "DTC_CHK",
        "target": "arch/arm64/boot/dts/actions/s700-cubieboard7.dtb",
        "output": "<any following non-command lines>"
    },
    ...
]

This assumes the output from a command immediately follows it. If
building with multiple jobs (`make -j`), the `--output-sync` argument
also needs to be supplied to make in order for the output fields to be
correctly set.

For an example of the usage of this script, consider `make dtbs_check`
generates a very long output in the following format:

  DTC_CHK arch/arm64/boot/dts/arm/foundation-v8-psci.dtb
  DTC_CHK arch/arm64/boot/dts/allwinner/sun50i-a64-amarula-relic.dtb
/tmp/kci/linux/arch/arm64/boot/dts/allwinner/sun50i-a64-amarula-relic.dtb: thermal-zones: gpu0-thermal: 'trips' is a required property
	from schema $id: http://devicetree.org/schemas/thermal/thermal-zones.yaml#
/tmp/kci/linux/arch/arm64/boot/dts/allwinner/sun50i-a64-amarula-relic.dtb: thermal-zones: gpu1-thermal: 'trips' is a required property
	from schema $id: http://devicetree.org/schemas/thermal/thermal-zones.yaml#
  DTC_CHK arch/arm64/boot/dts/altera/socfpga_stratix10_socdk_nand.dtb

With this output converted into JSON, it will be much simpler to
identify that only the middle DTB in this example produces warnings. By
extension, CIs will be able to more easily report which DTBs are
currently producing warnings and which are not, and identify
regressions.

Signed-off-by: Nícolas F. R. A. Prado <nfraprado@collabora.com>
---
Hi,
I'm not sure which subsystem this script would be a part of, which is
why I haven't added an entry in MAINTAINERS. Suggestions appreciated.
---
 scripts/make-to-json.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)

diff --git a/scripts/make-to-json.py b/scripts/make-to-json.py
new file mode 100755
index 000000000000..7570134f6151
--- /dev/null
+++ b/scripts/make-to-json.py
@@ -0,0 +1,51 @@
+#!/bin/env python
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2024 Collabora Ltd
+#
+# Transform the output from make into JSON. This makes it easier for machines to
+# parse.
+#
+# NOTE: This assumes the output from a command immediately follows it. If
+# building with multiple jobs (`make -j`), make sure to also pass
+# `--output-sync`.
+#
+# Command lines from make are in the format:
+#   DTC_CHK arch/arm64/boot/dts/actions/s700-cubieboard7.dtb
+#
+# where DTC_CHK is the command, arch/.../s700-cubieboard7.dtb is the target and
+# any non-command lines that follow it are the command's output.
+#
+# The JSON output is in the format:
+# [
+#     {
+#         "command": "DTC_CHK",
+#         "target": "arch/arm64/boot/dts/actions/s700-cubieboard7.dtb",
+#         "output": ""
+#     },
+#     ...
+# ]
+
+import json
+import re
+import sys
+
+make_cmd_re = re.compile(r"^  (?P<command>[A-Z0-9_.]{1,8}( \[M\])?) +(?P<target>.*)")
+
+make_output = []
+step = {}
+
+for line in sys.stdin:
+    match = make_cmd_re.match(line)
+    if match:
+        if step:
+            make_output.append(step)  # save previous step, if any
+        step = {}
+        step["command"] = match.group("command")
+        step["target"] = match.group("target")
+        step["output"] = ""
+    else:
+        step["output"] += line
+if step:
+    make_output.append(step)  # save last step, if any
+
+json.dump(make_output, sys.stdout)

---
base-commit: dad309222e4c3fc7f88b20ce725ce1e0eea07cc7
change-id: 20240319-make-to-json-script-764103bf74d7

Best regards,
-- 
Nícolas F. R. A. Prado <nfraprado@collabora.com>


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2024-03-19 21:25 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-19 21:24 [PATCH] scripts: Add utility to convert make's output into JSON Nícolas F. R. A. Prado

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®