From da4cb17aa165a5c4b41efc09a6e7c606186d038f Mon Sep 17 00:00:00 2001 From: "Yann E. MORIN" Date: Sat, 13 Nov 2021 14:28:27 +0100 Subject: [PATCH] Makefile: introduce show-vars, a json-formatted equivalent to printvars The current printvars output suffers from a serious design flaw: variables are not delimited, which makes it impossible to reliably retrieve the value of variables; only variables that are known to not contain a \n can be relatively safely extracted. However, in some cases, it is important to be able to retrieve the multi-line value of a variable, notably the CMDS or the hooks. One such use-case (to follow in an unscheduled future) would be to hash the variables that make up a package "configuration", and cache or extract the files for that package to speed up the build. Modeled after printvars and show-info, we introduce show-vars (what a lack of imagination here) that outputs a json dictionary which keys are the variable names, and for each variable, provides the raw and expanded values. Unlike printvars, we do not provide a way to get either the raw or expanded value; both are systematically printed; a user will get just the one is needs. Additionally, strings in JSON are quoted, so there is no need to provide a way to quote variables; that would not make sense. Note: for printvars, we require that the user provides an explicit pattern to filter variables on. This is historical (see fd5bd12379dc, Makefile: printvars: don't print anything when VARS is not set). The underlying reasoning was that printvars is too "raw", and variables are not well delimited, so printvars was mostly used to extract a few values here and there, from scripts, or to quickly inspect a specific package's variables during debugging. But show-vars, although technically plain-text, being JSON, is not very human-readable, and is mostly aimed at tools that will parse it with a real JSON parser, and which will want to have a complete view of a lot of variables at once. As such, and contrary to printvars, it makes sense to report on all variables by default, unless the user explicitly requested a subset. As a final note: a lot of our variables only make sense in the context of an actual make target. For example, a variable of package foo, that contains $(@D)/bar, would expand to .../build/FOO-VERSION/bar. This is because our CMDS and hooks are expanded as the recipe of a stamp file that lies in the package build directory. But for show-info, this falls flat on its face: it is not the stamp file of a package, so there is no package directory, and show-info itself has not directory part, so $(@D) expands to '.' (dot). Additionally, some variables may contain calls to $(shell) (e.g. to call pkg-config), and this also does not work with show-info. These two issues make it impossible to emit the correct expanded value of variables. To be noted: printvars has the exact same limitations for the exact same reasons. Signed-off-by: Yann E. MORIN Signed-off-by: Thomas Petazzoni --- Makefile | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 99a34c1600..0b8c7b2867 100644 --- a/Makefile +++ b/Makefile @@ -141,7 +141,7 @@ nobuild_targets := source %-source \ clean distclean help show-targets graph-depends \ %-graph-depends %-show-depends %-show-version \ graph-build graph-size list-defconfigs \ - savedefconfig update-defconfig printvars + savedefconfig update-defconfig printvars show-vars ifeq ($(MAKECMDGOALS),) BR_BUILDING = y else ifneq ($(filter-out $(nobuild_targets),$(MAKECMDGOALS)),) @@ -1062,6 +1062,7 @@ endif # Makefiles. Alternatively, if a non-empty VARS variable is passed, # only the variables matching the make pattern passed in VARS are # displayed. +# show-vars does the same, but as a JSON dictionnary. .PHONY: printvars printvars: @: @@ -1074,6 +1075,22 @@ printvars: $(info $V=$(if $(RAW_VARS),$(value $V),$($V)))))) # ')))) # Syntax colouring... +.PHONY: show-vars +show-vars: VARS?=% +show-vars: + @: + $(info $(call clean-json, { \ + $(foreach V, \ + $(sort $(filter $(VARS),$(.VARIABLES))), \ + $(if $(filter-out environment% default automatic, $(origin $V)), \ + "$V": { \ + "expanded": $(call mk-json-str,$($V))$(comma) \ + "raw": $(call mk-json-str,$(value $V)) \ + }$(comma) \ + ) \ + ) \ + } )) + .PHONY: clean clean: rm -rf $(BASE_TARGET_DIR) $(BINARIES_DIR) $(HOST_DIR) $(HOST_DIR_SYMLINK) \ @@ -1166,6 +1183,8 @@ help: @echo ' pkg-stats - generate info about packages as JSON and HTML' @echo ' missing-cpe - generate XML snippets for missing CPE identifiers' @echo ' printvars - dump internal variables selected with VARS=...' + @echo ' show-vars - dump all internal variables as a JSON blurb; use VARS=...' + @echo ' to limit the list to variables names matching that pattern' @echo @echo ' make V=0|1 - 0 => quiet build (default), 1 => verbose build' @echo ' make O=dir - Locate all output files in "dir", including .config'