kbuild: set EXIT trap before creating temporary directory
authorMasahiro Yamada <masahiroy@kernel.org>
Thu, 28 Jul 2022 03:14:33 +0000 (12:14 +0900)
committerMasahiro Yamada <masahiroy@kernel.org>
Wed, 3 Aug 2022 13:56:38 +0000 (22:56 +0900)
Swap the order of 'mkdir' and 'trap' just in case the subshell is
interrupted between 'mkdir' and 'trap' although the effect might be
subtle.

This does not intend to make the cleanup perfect. There are more cases
that miss to remove the tmp directory, for example:

 - When interrupted, dash does not invoke the EXIT trap (bash does)

 - 'rm' command might be interrupted before removing the directory

I am not addressing all the cases since the tmp directory is harmless
after all.

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>
scripts/Kconfig.include
scripts/Makefile.compiler

index c1f4222d223d2ae5ef213db896beee86fe854e57..a0ccceb22cf8b5ccfae079f6aea5499a7cdfae0a 100644 (file)
@@ -25,7 +25,7 @@ failure = $(if-success,$(1),n,y)
 
 # $(cc-option,<flag>)
 # Return y if the compiler supports <flag>, n otherwise
-cc-option = $(success,mkdir .tmp_$$; trap "rm -rf .tmp_$$" EXIT; $(CC) -Werror $(CLANG_FLAGS) $(1) -c -x c /dev/null -o .tmp_$$/tmp.o)
+cc-option = $(success,trap "rm -rf .tmp_$$" EXIT; mkdir .tmp_$$; $(CC) -Werror $(CLANG_FLAGS) $(1) -c -x c /dev/null -o .tmp_$$/tmp.o)
 
 # $(ld-option,<flag>)
 # Return y if the linker supports <flag>, n otherwise
index 86ecd2ac874c394104e2c7d2aa32735941ba8c0b..94d0d40cddb3d614facd505e46d4d83cbc7f7e10 100644 (file)
@@ -21,8 +21,8 @@ TMPOUT = $(if $(KBUILD_EXTMOD),$(firstword $(KBUILD_EXTMOD))/).tmp_$$$$
 # automatically cleaned up.
 try-run = $(shell set -e;              \
        TMP=$(TMPOUT)/tmp;              \
-       mkdir -p $(TMPOUT);             \
        trap "rm -rf $(TMPOUT)" EXIT;   \
+       mkdir -p $(TMPOUT);             \
        if ($(1)) >/dev/null 2>&1;      \
        then echo "$(2)";               \
        else echo "$(3)";               \