treewide: reduce usage of shell in Makefiles - #22669
Conversation
|
I wasn't expecting miracles, but I hoped for a bit more to be honest. Full build took 03h 27m 11s. Its ~5 minutes faster, but I guess that is in the noise. Some older builds are equally 'fast' without the optimizations. |
I know that feeling 😅 |
ccc1ea2 to
6fe1bce
Compare
2e62b30 to
369bf52
Compare
|
I also added a section on $(shell) usage in the documentation. |
| $(error Too short to be a valid STM32 CPU_MODEL: '$(CPU_MODEL)') | ||
| endif | ||
|
|
||
| # 'MP' is the only type that is spelled with two characters |
There was a problem hiding this comment.
That is not true, there are also WB and WL.
There was a problem hiding this comment.
It wasn't captured by the original regex. I'll look into it.
| $(info +++ FIXIT: Makefile: Add USEMODULE += unicoap_driver_dtls for CoAP over DTLS driver) | ||
| $(info +++ FIXIT: Makefile: Add USEMODULE += unicoap_driver_udp for CoAP over UDP driver) | ||
| $(info +++ FIXIT: Makefile: Add USEMODULE += unicoap_driver_rfc7252_pdu for CoAP over UDP/DTLS PDU parser only) | ||
| $(info +++ FIXIT: Makefile: Add USEMODULE += unicoap_driver_slipmux for CoAP over Slipmux driver) |
There was a problem hiding this comment.
This is not a valid change, echoing to stderr is intentional here since some scripts of the build system interpret the output on stdout.
Ideally this should use $(call echoinfo,("+++ FIXIT...")), see https://doc.riot-os.org/build-system/build_system_basics/#printing-infos-warnings-and-error-messages
These functions do use a shell call, but printing to stderr is not supported with $(info) and printing colors with $(info) is not necessarily defined, although some code does that.
| # Check whether `which` is installed | ||
| ifeq ($(shell command -v which 2>/dev/null),) | ||
| $(error 'which' is required but not found in PATH.) | ||
| endif |
There was a problem hiding this comment.
But is there a big performance penalty for keeping the check? Perhaps @AnnsAnns can elaborate why he added the check?
|
Is the github PR review broken again, I cant directly comment for some reason? Anyways, RE which check, if I remember correctly this check exists because we use which to check for stuff but when which is not installed it broke those checks in UB ways that caused the build system to create incorrect and misleading errors |
| Before using a `$(shell ...)` call, check whether one of the following | ||
| alternatives already covers the case: | ||
|
|
||
| - Use `$(notdir ...)` instead of `$(shell basename ...)` to strip the |
There was a problem hiding this comment.
| - Use `$(notdir ...)` instead of `$(shell basename ...)` to strip the | |
| - Use `$(basename $(notdir ...))` instead of `$(shell basename ...)` to strip the |
$(basename) strips the suffix as shell basename does
| split_chars = $(strip $(subst 0, 0,$(subst 1, 1,$(subst 2, 2,$(subst 3, 3,$(subst 4, 4,$(subst 5, 5,$(subst 6, 6,$(subst 7, 7,$(subst 8, 8,$(subst 9, 9,$(subst a, a,$(subst b, b,$(subst c, c,$(subst d, d,$(subst e, e,$(subst f, f,$(subst g, g,$(subst h, h,$(subst i, i,$(subst j, j,$(subst k, k,$(subst l, l,$(subst m, m,$(subst n, n,$(subst o, o,$(subst p, p,$(subst q, q,$(subst r, r,$(subst s, s,$(subst t, t,$(subst u, u,$(subst v, v,$(subst w, w,$(subst x, x,$(subst y, y,$(subst z, z,$(subst A, A,$(subst B, B,$(subst C, C,$(subst D, D,$(subst E, E,$(subst F, F,$(subst G, G,$(subst H, H,$(subst I, I,$(subst J, J,$(subst K, K,$(subst L, L,$(subst M, M,$(subst N, N,$(subst O, O,$(subst P, P,$(subst Q, Q,$(subst R, R,$(subst S, S,$(subst T, T,$(subst U, U,$(subst V, V,$(subst W, W,$(subst X, X,$(subst Y, Y,$(subst Z, Z,$(subst _, _,$(subst -, -,$1))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) | ||
|
|
||
| # Returns the first $1 characters of $2, like 'cut -c -$1' does. | ||
| first_chars = $(subst $(space),,$(wordlist 1,$1,$(call split_chars,$2))) |
There was a problem hiding this comment.
chars_upto instead of first_chars
might fir better with chars_from
| first_chars = $(subst $(space),,$(wordlist 1,$1,$(call split_chars,$2))) | |
| chars_upto = $(subst $(space),,$(wordlist 1,$1,$(call split_chars,$2))) |
There was a problem hiding this comment.
or first_chars and last_chars
Contribution description
Using
$(shell ..)in Makefiles spawns a subprocess, which is less performant than built-in alternatives. Recalling #13174 from before, I let Claude analyze where this may improve on current master.An overview:
$(shell basename $(CURDIR))works the same as$(notdir $(CURDIR))(unless$(CURDIR)equals/)uppercase_and_underscorecan be used instead oftr a-z- A-Z_The impact will be unnoticeable for a single build and will probably be in the noise when actual compilation takes the overhand. But (hopefully) it will be more noticeable for CI builds with warmer caches. I don't expect miracles, and it started as an experiment.
To give an idea of the improvement (I let Claude do the benchmarking) on my server (i5 14600K with 64 GiB of memory):
For
make -j1(rebuild, so warm cache):For
make -j8(rebuild, so warm cache):Testing procedure
This should not impact the builds. The end result should be the same.
To count subprocessed, one can use for example
strace -f -qq -e trace=execve -o trace.txt make BOARD=nucleo-f767zi all. Then count the number ofexecvein the output file.Issues/PRs references
None
Declaration of AI-Tools / LLMs usage:
AI-Tools / LLMs that were used are:
strings.mk) and performing benchmarks. I have read an understand every line changed, but I am not a Makefile export, and it can probably be better.