mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/2] kbuild: split off compiler helpers into separate Makefile.compiler
@ 2024-12-03 10:42 Ahmad Fatoum
  2024-12-03 10:42 ` [PATCH 2/2] kbuild: sync scripts/Kbuild.include with Linux Ahmad Fatoum
  2024-12-06 10:08 ` [PATCH 1/2] kbuild: split off compiler helpers into separate Makefile.compiler Sascha Hauer
  0 siblings, 2 replies; 4+ messages in thread
From: Ahmad Fatoum @ 2024-12-03 10:42 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

Linux has split the compile-specific helpers into a separate file, so
let's do the same for barebox to make it easier to sync
scripts/Kbuild.include in the follow-up commit.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 Makefile                  |  1 +
 scripts/Kbuild.include    | 83 ---------------------------------------
 scripts/Makefile.build    |  1 +
 scripts/Makefile.compiler | 78 ++++++++++++++++++++++++++++++++++++
 4 files changed, 80 insertions(+), 83 deletions(-)
 create mode 100644 scripts/Makefile.compiler

diff --git a/Makefile b/Makefile
index bc75c6335177..0b61d2875471 100644
--- a/Makefile
+++ b/Makefile
@@ -314,6 +314,7 @@ __build_one_by_one:
 else # !mixed-build
 
 include scripts/Kbuild.include
+include scripts/Makefile.compiler
 
 # Read KERNELRELEASE from include/config/kernel.release (if it exists)
 KERNELRELEASE = $(shell cat include/config/kernel.release 2> /dev/null)
diff --git a/scripts/Kbuild.include b/scripts/Kbuild.include
index 315e50a2f709..72c6a3c5d7d1 100644
--- a/scripts/Kbuild.include
+++ b/scripts/Kbuild.include
@@ -69,81 +69,6 @@ define filechk
 	fi
 endef
 
-######
-# gcc support functions
-# See documentation in Documentation/kbuild/makefiles.rst
-
-# cc-cross-prefix
-# Usage: CROSS_COMPILE := $(call cc-cross-prefix, m68k-linux-gnu- m68k-linux-)
-# Return first <prefix> where a <prefix>gcc is found in PATH.
-# If no gcc found in PATH with listed prefixes return nothing
-#
-# Note: '2>/dev/null' is here to force Make to invoke a shell. Otherwise, it
-# would try to directly execute the shell builtin 'command'. This workaround
-# should be kept for a long time since this issue was fixed only after the
-# GNU Make 4.2.1 release.
-cc-cross-prefix = $(firstword $(foreach c, $(1), \
-			$(if $(shell command -v -- $(c)gcc 2>/dev/null), $(c))))
-
-# output directory for tests below
-TMPOUT := $(if $(KBUILD_EXTMOD),$(firstword $(KBUILD_EXTMOD))/)
-
-# try-run
-# Usage: option = $(call try-run, $(CC)...-o "$$TMP",option-ok,otherwise)
-# Exit code chooses option. "$$TMP" is can be used as temporary file and
-# is automatically cleaned up.
-try-run = $(shell set -e;		\
-	TMP="$(TMPOUT).$$$$.tmp";	\
-	TMPO="$(TMPOUT).$$$$.o";	\
-	if ($(1)) >/dev/null 2>&1;	\
-	then echo "$(2)";		\
-	else echo "$(3)";		\
-	fi;				\
-	rm -f "$$TMP" "$$TMPO")
-
-# as-option
-# Usage: cflags-y += $(call as-option,-Wa$(comma)-isa=foo,)
-
-as-option = $(call try-run,\
-	$(CC) $(KBUILD_CFLAGS) $(1) -c -xassembler /dev/null -o "$$TMP",$(1),$(2))
-
-# as-instr
-# Usage: cflags-y += $(call as-instr,instr,option1,option2)
-
-as-instr = $(call try-run,\
-	printf "%b\n" "$(1)" | $(CC) $(KBUILD_AFLAGS) -c -xassembler -o "$$TMP" -,$(2),$(3))
-
-# cc-option
-# Usage: cflags-y += $(call cc-option,-march=winchip-c6,-march=i586)
-
-cc-option = $(call try-run,\
-	$(CC) -Werror $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) $(1) -c -xc /dev/null -o "$$TMP",$(1),$(2))
-
-# cc-option-yn
-# Usage: flag := $(call cc-option-yn,-march=winchip-c6)
-cc-option-yn = $(call try-run,\
-	$(CC) -Werror $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) $(1) -c -xc /dev/null -o "$$TMP",y,n)
-
-# cc-disable-warning
-# Usage: cflags-y += $(call cc-disable-warning,unused-but-set-variable)
-cc-disable-warning = $(call try-run,\
-	$(CC) -Werror $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) -W$(strip $(1)) -c -xc /dev/null -o "$$TMP",-Wno-$(strip $(1)))
-
-# cc-version
-# Usage gcc-ver := $(call cc-version)
-cc-version = $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-version.sh $(CC))
-
-# cc-ifversion
-# Usage:  EXTRA_CFLAGS += $(call cc-ifversion, -lt, 0402, -O1)
-cc-ifversion = $(shell [ $(call cc-version, $(CC)) $(1) $(2) ] && echo $(3))
-
-# ld-option
-# Usage: KBUILD_LDFLAGS += $(call ld-option, -X)
-ld-option = $(call try-run,\
-	$(CC) -x c /dev/null -c -o "$$TMPO" ; $(LD) $(1) "$$TMPO" -o "$$TMP",$(1),$(2))
-
-######
-
 ###
 # Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj=
 # Usage:
@@ -156,14 +81,6 @@ build := -f $(srctree)/scripts/Makefile.build obj
 # $(Q)$(MAKE) $(clean)=dir
 clean := -f $(srctree)/scripts/Makefile.clean obj
 
-# Prefix -I with $(srctree) if it is not an absolute path.
-# skip if -I has no parameter
-addtree = $(if $(patsubst -I%,%,$(1)), \
-$(if $(filter-out -I/%,$(1)),$(patsubst -I%,-I$(srctree)/%,$(1))) $(1),$(1))
-
-# Find all -I options and call addtree
-flags = $(foreach o,$($(1)),$(if $(filter -I%,$(o)),$(call addtree,$(o)),$(o)))
-
 # echo command.
 # Short version is used, if $(quiet) equals `quiet_', otherwise full one.
 echo-cmd = $(if $($(quiet)cmd_$(1)),\
diff --git a/scripts/Makefile.build b/scripts/Makefile.build
index 6b67ad17634b..1c511d38a12b 100644
--- a/scripts/Makefile.build
+++ b/scripts/Makefile.build
@@ -37,6 +37,7 @@ subdir-ccflags-y :=
 -include include/config/auto.conf
 
 include scripts/Kbuild.include
+include scripts/Makefile.compiler
 
 # The filename Kbuild has precedence over Makefile
 kbuild-dir := $(if $(filter /%,$(src)),$(src),$(srctree)/$(src))
diff --git a/scripts/Makefile.compiler b/scripts/Makefile.compiler
new file mode 100644
index 000000000000..1d34239b3bba
--- /dev/null
+++ b/scripts/Makefile.compiler
@@ -0,0 +1,78 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+# cc-cross-prefix
+# Usage: CROSS_COMPILE := $(call cc-cross-prefix, m68k-linux-gnu- m68k-linux-)
+# Return first <prefix> where a <prefix>gcc is found in PATH.
+# If no gcc found in PATH with listed prefixes return nothing
+#
+# Note: '2>/dev/null' is here to force Make to invoke a shell. Otherwise, it
+# would try to directly execute the shell builtin 'command'. This workaround
+# should be kept for a long time since this issue was fixed only after the
+# GNU Make 4.2.1 release.
+cc-cross-prefix = $(firstword $(foreach c, $(1), \
+			$(if $(shell command -v -- $(c)gcc 2>/dev/null), $(c))))
+
+# output directory for tests below
+TMPOUT := $(if $(KBUILD_EXTMOD),$(firstword $(KBUILD_EXTMOD))/)
+
+# try-run
+# Usage: option = $(call try-run, $(CC)...-o "$$TMP",option-ok,otherwise)
+# Exit code chooses option. "$$TMP" is can be used as temporary file and
+# is automatically cleaned up.
+try-run = $(shell set -e;		\
+	TMP="$(TMPOUT).$$$$.tmp";	\
+	TMPO="$(TMPOUT).$$$$.o";	\
+	if ($(1)) >/dev/null 2>&1;	\
+	then echo "$(2)";		\
+	else echo "$(3)";		\
+	fi;				\
+	rm -f "$$TMP" "$$TMPO")
+
+# as-option
+# Usage: cflags-y += $(call as-option,-Wa$(comma)-isa=foo,)
+
+as-option = $(call try-run,\
+	$(CC) $(KBUILD_CFLAGS) $(1) -c -xassembler /dev/null -o "$$TMP",$(1),$(2))
+
+# as-instr
+# Usage: cflags-y += $(call as-instr,instr,option1,option2)
+
+as-instr = $(call try-run,\
+	printf "%b\n" "$(1)" | $(CC) $(KBUILD_AFLAGS) -c -xassembler -o "$$TMP" -,$(2),$(3))
+
+# cc-option
+# Usage: cflags-y += $(call cc-option,-march=winchip-c6,-march=i586)
+
+cc-option = $(call try-run,\
+	$(CC) -Werror $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) $(1) -c -xc /dev/null -o "$$TMP",$(1),$(2))
+
+# cc-option-yn
+# Usage: flag := $(call cc-option-yn,-march=winchip-c6)
+cc-option-yn = $(call try-run,\
+	$(CC) -Werror $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) $(1) -c -xc /dev/null -o "$$TMP",y,n)
+
+# cc-disable-warning
+# Usage: cflags-y += $(call cc-disable-warning,unused-but-set-variable)
+cc-disable-warning = $(call try-run,\
+	$(CC) -Werror $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) -W$(strip $(1)) -c -xc /dev/null -o "$$TMP",-Wno-$(strip $(1)))
+
+# cc-version
+# Usage gcc-ver := $(call cc-version)
+cc-version = $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-version.sh $(CC))
+
+# cc-ifversion
+# Usage:  EXTRA_CFLAGS += $(call cc-ifversion, -lt, 0402, -O1)
+cc-ifversion = $(shell [ $(call cc-version, $(CC)) $(1) $(2) ] && echo $(3))
+
+# ld-option
+# Usage: KBUILD_LDFLAGS += $(call ld-option, -X)
+ld-option = $(call try-run,\
+	$(CC) -x c /dev/null -c -o "$$TMPO" ; $(LD) $(1) "$$TMPO" -o "$$TMP",$(1),$(2))
+
+# Prefix -I with $(srctree) if it is not an absolute path.
+# skip if -I has no parameter
+addtree = $(if $(patsubst -I%,%,$(1)), \
+$(if $(filter-out -I/%,$(1)),$(patsubst -I%,-I$(srctree)/%,$(1))) $(1),$(1))
+
+# Find all -I options and call addtree
+flags = $(foreach o,$($(1)),$(if $(filter -I%,$(o)),$(call addtree,$(o)),$(o)))
-- 
2.39.5




^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 2/2] kbuild: sync scripts/Kbuild.include with Linux
  2024-12-03 10:42 [PATCH 1/2] kbuild: split off compiler helpers into separate Makefile.compiler Ahmad Fatoum
@ 2024-12-03 10:42 ` Ahmad Fatoum
  2024-12-03 10:59   ` Ahmad Fatoum
  2024-12-06 10:08 ` [PATCH 1/2] kbuild: split off compiler helpers into separate Makefile.compiler Sascha Hauer
  1 sibling, 1 reply; 4+ messages in thread
From: Ahmad Fatoum @ 2024-12-03 10:42 UTC (permalink / raw)
  To: barebox

The scripts/Kbuild.include file has a collection of helpers for use
across in Kbuild. Let's bring up our version in-sync with Linux
v6.13-rc1 by importing the following Linux commits:

  875ef1a57f kbuild: use .NOTINTERMEDIATE for future GNU Make versions
  174a1dcc96 kbuild: sink stdout from cmd for silent build
  a7f3257da8 kbuild: remove the target in signal traps when interrupted
  6768fa4bcb kbuild: add read-file macro
  fccb3d3eda kbuild: add test-{ge,gt,le,lt} macros
  8402ee182c kbuild: remove leftover comment for filechk utility
  12fec3d601 kbuild: replace $(dot-target).tmp in filechk with $(tmp-target)
  e1f86d7b4b kbuild: warn if FORCE is missing for filechk
  8962b6b475 kbuild: print short log in addition to the whole command with V=1
  6ae4b9868a kbuild: allow to combine multiple V= levels
  214c0eea43 kbuild: add $(objtree)/ prefix to some in-kernel build artifacts
---
 Makefile               |  19 ++++---
 scripts/Kbuild.include | 109 ++++++++++++++++++++++++++++++++++-------
 scripts/tags.sh        |   2 +-
 3 files changed, 102 insertions(+), 28 deletions(-)

diff --git a/Makefile b/Makefile
index 0b61d2875471..1516e5685cbf 100644
--- a/Makefile
+++ b/Makefile
@@ -66,9 +66,8 @@ unexport GREP_OPTIONS
 #
 #	$(Q)ln $@ :<
 #
-# If KBUILD_VERBOSE equals 0 then the above command will be hidden.
-# If KBUILD_VERBOSE equals 1 then the above command is displayed.
-# If KBUILD_VERBOSE equals 2 then give the reason why each target is rebuilt.
+# If KBUILD_VERBOSE contains 1, the whole command is echoed.
+# If KBUILD_VERBOSE contains 2, the reason for rebuilding is printed.
 #
 # To put more focus on warnings, be less verbose as default
 # Use 'make V=1' to see the full commands
@@ -80,12 +79,11 @@ ifndef KBUILD_VERBOSE
   KBUILD_VERBOSE = 0
 endif
 
-ifeq ($(KBUILD_VERBOSE),1)
+quiet = quiet_
+Q = @
+ifneq ($(findstring 1, $(KBUILD_VERBOSE)),)
   quiet =
   Q =
-else
-  quiet=quiet_
-  Q = @
 endif
 
 # If the user is running make -s (silent mode), suppress echoing of
@@ -317,7 +315,7 @@ include scripts/Kbuild.include
 include scripts/Makefile.compiler
 
 # Read KERNELRELEASE from include/config/kernel.release (if it exists)
-KERNELRELEASE = $(shell cat include/config/kernel.release 2> /dev/null)
+KERNELRELEASE = $(call read-file, include/config/kernel.release)
 KERNELVERSION = $(VERSION)$(if $(PATCHLEVEL),.$(PATCHLEVEL)$(if $(SUBLEVEL),.$(SUBLEVEL)))$(EXTRAVERSION)
 BUILDSYSTEM_VERSION =
 export VERSION PATCHLEVEL SUBLEVEL KERNELRELEASE KERNELVERSION BUILDSYSTEM_VERSION
@@ -1326,8 +1324,9 @@ help:
 		printf "  %-24s - Build for %s\\n" $(b) $(subst _defconfig,,$(b));) \
 		echo '')
 
-	@echo  '  make V=0|1 [targets] 0 => quiet build (default), 1 => verbose build'
-	@echo  '  make V=2   [targets] 2 => give reason for rebuild of target'
+	@echo  '  make V=n   [targets] 0: quiet build (default), 1: verbose build'
+	@echo  '                       2: give reason for rebuild of target'
+	@echo  '                       V=1 and V=2 can be combined with V=12'
 	@echo  '  make O=dir [targets] Locate all output files in "dir", including .config'
 	@echo  '  make C=1   [targets] Check all c source with $$CHECK (sparse by default)'
 	@echo  '  make C=2   [targets] Force check of all c source with $$CHECK'
diff --git a/scripts/Kbuild.include b/scripts/Kbuild.include
index 72c6a3c5d7d1..8c311b997e24 100644
--- a/scripts/Kbuild.include
+++ b/scripts/Kbuild.include
@@ -10,13 +10,37 @@ empty   :=
 space   := $(empty) $(empty)
 space_escape := _-_SPACE_-_
 pound := \#
+define newline
+
+
+endef
+
+###
+# Comparison macros.
+# Usage: $(call test-lt, $(CONFIG_LLD_VERSION), 150000)
+#
+# Use $(intcmp ...) if supported. (Make >= 4.4)
+# Otherwise, fall back to the 'test' shell command.
+ifeq ($(intcmp 1,0,,,y),y)
+test-ge = $(intcmp $(strip $1)0, $(strip $2)0,,y,y)
+test-gt = $(intcmp $(strip $1)0, $(strip $2)0,,,y)
+else
+test-ge = $(shell test $(strip $1)0 -ge $(strip $2)0 && echo y)
+test-gt = $(shell test $(strip $1)0 -gt $(strip $2)0 && echo y)
+endif
+test-le = $(call test-ge, $2, $1)
+test-lt = $(call test-gt, $2, $1)
 
 ###
 # Name of target with a '.' as filename prefix. foo/bar.o => foo/.bar.o
 dot-target = $(dir $@).$(notdir $@)
 
 ###
-# The temporary file to save gcc -MD generated dependencies must not
+# Name of target with a '.tmp_' as filename prefix. foo/bar.o => foo/.tmp_bar.o
+tmp-target = $(dir $@).tmp_$(notdir $@)
+
+###
+# The temporary file to save gcc -MMD generated dependencies must not
 # contain a comma
 depfile = $(subst $(comma),_,$(dot-target).d)
 
@@ -36,6 +60,20 @@ escsq = $(subst $(squote),'\$(squote)',$1)
 # Quote a string to pass it to C files. foo => '"foo"'
 stringify = $(squote)$(quote)$1$(quote)$(squote)
 
+###
+# The path to Kbuild or Makefile. Kbuild has precedence over Makefile.
+kbuild-file = $(or $(wildcard $(src)/Kbuild),$(src)/Makefile)
+
+###
+# Read a file, replacing newlines with spaces
+#
+# Make 4.2 or later can read a file by using its builtin function.
+ifneq ($(filter-out 4.0 4.1, $(MAKE_VERSION)),)
+read-file = $(subst $(newline),$(space),$(file < $1))
+else
+read-file = $(shell cat $1 2>/dev/null)
+endif
+
 ###
 # Easy method for doing a status message
        kecho := :
@@ -56,16 +94,15 @@ kecho := $($(quiet)kecho)
 # - If no file exist it is created
 # - If the content differ the new file is used
 # - If they are equal no change, and no timestamp update
-# - stdin is piped in from the first prerequisite ($<) so one has
-#   to specify a valid file as first prerequisite (often the kbuild file)
 define filechk
+	$(check-FORCE)
 	$(Q)set -e;						\
 	mkdir -p $(dir $@);					\
-	trap "rm -f $(dot-target).tmp" EXIT;			\
-	{ $(filechk_$(1)); } > $(dot-target).tmp;		\
-	if [ ! -r $@ ] || ! cmp -s $@ $(dot-target).tmp; then	\
+	trap "rm -f $(tmp-target)" EXIT;			\
+	{ $(filechk_$(1)); } > $(tmp-target);			\
+	if [ ! -r $@ ] || ! cmp -s $@ $(tmp-target); then	\
 		$(kecho) '  UPD     $@';			\
-		mv -f $(dot-target).tmp $@;			\
+		mv -f $(tmp-target) $@;				\
 	fi
 endef
 
@@ -81,13 +118,39 @@ build := -f $(srctree)/scripts/Makefile.build obj
 # $(Q)$(MAKE) $(clean)=dir
 clean := -f $(srctree)/scripts/Makefile.clean obj
 
-# echo command.
-# Short version is used, if $(quiet) equals `quiet_', otherwise full one.
-echo-cmd = $(if $($(quiet)cmd_$(1)),\
-	echo '  $(call escsq,$($(quiet)cmd_$(1)))$(echo-why)';)
+# pring log
+#
+# If quiet is "silent_", print nothing and sink stdout
+# If quiet is "quiet_", print short log
+# If quiet is empty, print short log and whole command
+silent_log_print = exec >/dev/null;
+ quiet_log_print = $(if $(quiet_cmd_$1), echo '  $(call escsq,$(quiet_cmd_$1)$(why))';)
+       log_print = echo '$(pound) $(call escsq,$(or $(quiet_cmd_$1),cmd_$1 $@)$(why))'; \
+                   echo '  $(call escsq,$(cmd_$1))';
 
-# printing commands
-cmd = @set -e; $(echo-cmd) $(cmd_$(1))
+# Delete the target on interruption
+#
+# GNU Make automatically deletes the target if it has already been changed by
+# the interrupted recipe. So, you can safely stop the build by Ctrl-C (Make
+# will delete incomplete targets), and resume it later.
+#
+# However, this does not work when the stderr is piped to another program, like
+#  $ make >&2 | tee log
+# Make dies with SIGPIPE before cleaning the targets.
+#
+# To address it, we clean the target in signal traps.
+#
+# Make deletes the target when it catches SIGHUP, SIGINT, SIGQUIT, SIGTERM.
+# So, we cover them, and also SIGPIPE just in case.
+#
+# Of course, this is unneeded for phony targets.
+delete-on-interrupt = \
+	$(if $(filter-out $(PHONY), $@), \
+		$(foreach sig, HUP INT QUIT TERM PIPE, \
+			trap 'rm -f $@; trap - $(sig); kill -s $(sig) $$$$' $(sig);))
+
+# print and execute commands
+cmd = @$(if $(cmd_$(1)),set -e; $($(quiet)log_print) $(delete-on-interrupt) $(cmd_$(1)),:)
 
 ###
 # if_changed      - execute command if any prerequisite is newer than
@@ -142,7 +205,7 @@ if_changed_dep = $(if $(if-changed-cond),$(cmd_and_fixdep),@:)
 
 cmd_and_fixdep =                                                             \
 	$(cmd);                                                              \
-	scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(dot-target).cmd;\
+	$(objtree)/scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(dot-target).cmd;\
 	rm -f $(depfile)
 
 # Usage: $(call if_changed_rule,foo)
@@ -169,8 +232,8 @@ if_changed_rule = $(if $(if-changed-cond),$(rule_$(1)),@:)
 # (5) No dir/.target.cmd file (used to store command line)
 # (6) No dir/.target.cmd file and target not listed in $(targets)
 #     This is a good hint that there is a bug in the kbuild file
-ifeq ($(KBUILD_VERBOSE),2)
-why =                                                                        \
+ifneq ($(findstring 2, $(KBUILD_VERBOSE)),)
+_why =                                                                        \
     $(if $(filter $@, $(PHONY)),- due to target is PHONY,                    \
         $(if $(wildcard $@),                                                 \
             $(if $(newer-prereqs),- due to: $(newer-prereqs),                \
@@ -187,11 +250,23 @@ why =                                                                        \
          )                                                                   \
      )
 
-echo-why = $(call escsq, $(strip $(why)))
+why = $(space)$(strip $(_why))
 endif
 
+###############################################################################
+
 # delete partially updated (i.e. corrupted) files on error
 .DELETE_ON_ERROR:
 
 # do not delete intermediate files automatically
+#
+# .NOTINTERMEDIATE is more correct, but only available on newer Make versions.
+# Make 4.4 introduced .NOTINTERMEDIATE, and it appears in .FEATURES, but the
+# global .NOTINTERMEDIATE does not work. We can use it on Make > 4.4.
+# Use .SECONDARY for older Make versions, but "newer-prereq" cannot detect
+# deleted files.
+ifneq ($(and $(filter notintermediate, $(.FEATURES)),$(filter-out 4.4,$(MAKE_VERSION))),)
+.NOTINTERMEDIATE:
+else
 .SECONDARY:
+endif
diff --git a/scripts/tags.sh b/scripts/tags.sh
index 4e18ae5282a6..6e0e0a7c2270 100755
--- a/scripts/tags.sh
+++ b/scripts/tags.sh
@@ -8,7 +8,7 @@
 # Uses the following environment variables:
 # SUBARCH, SRCARCH, srctree
 
-if [ "$KBUILD_VERBOSE" = "1" ]; then
+if [[ "$KBUILD_VERBOSE" =~ "1" ]]; then
 	set -x
 fi
 
-- 
2.39.5




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 2/2] kbuild: sync scripts/Kbuild.include with Linux
  2024-12-03 10:42 ` [PATCH 2/2] kbuild: sync scripts/Kbuild.include with Linux Ahmad Fatoum
@ 2024-12-03 10:59   ` Ahmad Fatoum
  0 siblings, 0 replies; 4+ messages in thread
From: Ahmad Fatoum @ 2024-12-03 10:59 UTC (permalink / raw)
  To: barebox

On 03.12.24 11:42, Ahmad Fatoum wrote:
> The scripts/Kbuild.include file has a collection of helpers for use
> across in Kbuild. Let's bring up our version in-sync with Linux
> v6.13-rc1 by importing the following Linux commits:
> 
>   875ef1a57f kbuild: use .NOTINTERMEDIATE for future GNU Make versions
>   174a1dcc96 kbuild: sink stdout from cmd for silent build
>   a7f3257da8 kbuild: remove the target in signal traps when interrupted
>   6768fa4bcb kbuild: add read-file macro
>   fccb3d3eda kbuild: add test-{ge,gt,le,lt} macros
>   8402ee182c kbuild: remove leftover comment for filechk utility
>   12fec3d601 kbuild: replace $(dot-target).tmp in filechk with $(tmp-target)
>   e1f86d7b4b kbuild: warn if FORCE is missing for filechk
>   8962b6b475 kbuild: print short log in addition to the whole command with V=1
>   6ae4b9868a kbuild: allow to combine multiple V= levels
>   214c0eea43 kbuild: add $(objtree)/ prefix to some in-kernel build artifacts

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>

> ---
>  Makefile               |  19 ++++---
>  scripts/Kbuild.include | 109 ++++++++++++++++++++++++++++++++++-------
>  scripts/tags.sh        |   2 +-
>  3 files changed, 102 insertions(+), 28 deletions(-)
> 
> diff --git a/Makefile b/Makefile
> index 0b61d2875471..1516e5685cbf 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -66,9 +66,8 @@ unexport GREP_OPTIONS
>  #
>  #	$(Q)ln $@ :<
>  #
> -# If KBUILD_VERBOSE equals 0 then the above command will be hidden.
> -# If KBUILD_VERBOSE equals 1 then the above command is displayed.
> -# If KBUILD_VERBOSE equals 2 then give the reason why each target is rebuilt.
> +# If KBUILD_VERBOSE contains 1, the whole command is echoed.
> +# If KBUILD_VERBOSE contains 2, the reason for rebuilding is printed.
>  #
>  # To put more focus on warnings, be less verbose as default
>  # Use 'make V=1' to see the full commands
> @@ -80,12 +79,11 @@ ifndef KBUILD_VERBOSE
>    KBUILD_VERBOSE = 0
>  endif
>  
> -ifeq ($(KBUILD_VERBOSE),1)
> +quiet = quiet_
> +Q = @
> +ifneq ($(findstring 1, $(KBUILD_VERBOSE)),)
>    quiet =
>    Q =
> -else
> -  quiet=quiet_
> -  Q = @
>  endif
>  
>  # If the user is running make -s (silent mode), suppress echoing of
> @@ -317,7 +315,7 @@ include scripts/Kbuild.include
>  include scripts/Makefile.compiler
>  
>  # Read KERNELRELEASE from include/config/kernel.release (if it exists)
> -KERNELRELEASE = $(shell cat include/config/kernel.release 2> /dev/null)
> +KERNELRELEASE = $(call read-file, include/config/kernel.release)
>  KERNELVERSION = $(VERSION)$(if $(PATCHLEVEL),.$(PATCHLEVEL)$(if $(SUBLEVEL),.$(SUBLEVEL)))$(EXTRAVERSION)
>  BUILDSYSTEM_VERSION =
>  export VERSION PATCHLEVEL SUBLEVEL KERNELRELEASE KERNELVERSION BUILDSYSTEM_VERSION
> @@ -1326,8 +1324,9 @@ help:
>  		printf "  %-24s - Build for %s\\n" $(b) $(subst _defconfig,,$(b));) \
>  		echo '')
>  
> -	@echo  '  make V=0|1 [targets] 0 => quiet build (default), 1 => verbose build'
> -	@echo  '  make V=2   [targets] 2 => give reason for rebuild of target'
> +	@echo  '  make V=n   [targets] 0: quiet build (default), 1: verbose build'
> +	@echo  '                       2: give reason for rebuild of target'
> +	@echo  '                       V=1 and V=2 can be combined with V=12'
>  	@echo  '  make O=dir [targets] Locate all output files in "dir", including .config'
>  	@echo  '  make C=1   [targets] Check all c source with $$CHECK (sparse by default)'
>  	@echo  '  make C=2   [targets] Force check of all c source with $$CHECK'
> diff --git a/scripts/Kbuild.include b/scripts/Kbuild.include
> index 72c6a3c5d7d1..8c311b997e24 100644
> --- a/scripts/Kbuild.include
> +++ b/scripts/Kbuild.include
> @@ -10,13 +10,37 @@ empty   :=
>  space   := $(empty) $(empty)
>  space_escape := _-_SPACE_-_
>  pound := \#
> +define newline
> +
> +
> +endef
> +
> +###
> +# Comparison macros.
> +# Usage: $(call test-lt, $(CONFIG_LLD_VERSION), 150000)
> +#
> +# Use $(intcmp ...) if supported. (Make >= 4.4)
> +# Otherwise, fall back to the 'test' shell command.
> +ifeq ($(intcmp 1,0,,,y),y)
> +test-ge = $(intcmp $(strip $1)0, $(strip $2)0,,y,y)
> +test-gt = $(intcmp $(strip $1)0, $(strip $2)0,,,y)
> +else
> +test-ge = $(shell test $(strip $1)0 -ge $(strip $2)0 && echo y)
> +test-gt = $(shell test $(strip $1)0 -gt $(strip $2)0 && echo y)
> +endif
> +test-le = $(call test-ge, $2, $1)
> +test-lt = $(call test-gt, $2, $1)
>  
>  ###
>  # Name of target with a '.' as filename prefix. foo/bar.o => foo/.bar.o
>  dot-target = $(dir $@).$(notdir $@)
>  
>  ###
> -# The temporary file to save gcc -MD generated dependencies must not
> +# Name of target with a '.tmp_' as filename prefix. foo/bar.o => foo/.tmp_bar.o
> +tmp-target = $(dir $@).tmp_$(notdir $@)
> +
> +###
> +# The temporary file to save gcc -MMD generated dependencies must not
>  # contain a comma
>  depfile = $(subst $(comma),_,$(dot-target).d)
>  
> @@ -36,6 +60,20 @@ escsq = $(subst $(squote),'\$(squote)',$1)
>  # Quote a string to pass it to C files. foo => '"foo"'
>  stringify = $(squote)$(quote)$1$(quote)$(squote)
>  
> +###
> +# The path to Kbuild or Makefile. Kbuild has precedence over Makefile.
> +kbuild-file = $(or $(wildcard $(src)/Kbuild),$(src)/Makefile)
> +
> +###
> +# Read a file, replacing newlines with spaces
> +#
> +# Make 4.2 or later can read a file by using its builtin function.
> +ifneq ($(filter-out 4.0 4.1, $(MAKE_VERSION)),)
> +read-file = $(subst $(newline),$(space),$(file < $1))
> +else
> +read-file = $(shell cat $1 2>/dev/null)
> +endif
> +
>  ###
>  # Easy method for doing a status message
>         kecho := :
> @@ -56,16 +94,15 @@ kecho := $($(quiet)kecho)
>  # - If no file exist it is created
>  # - If the content differ the new file is used
>  # - If they are equal no change, and no timestamp update
> -# - stdin is piped in from the first prerequisite ($<) so one has
> -#   to specify a valid file as first prerequisite (often the kbuild file)
>  define filechk
> +	$(check-FORCE)
>  	$(Q)set -e;						\
>  	mkdir -p $(dir $@);					\
> -	trap "rm -f $(dot-target).tmp" EXIT;			\
> -	{ $(filechk_$(1)); } > $(dot-target).tmp;		\
> -	if [ ! -r $@ ] || ! cmp -s $@ $(dot-target).tmp; then	\
> +	trap "rm -f $(tmp-target)" EXIT;			\
> +	{ $(filechk_$(1)); } > $(tmp-target);			\
> +	if [ ! -r $@ ] || ! cmp -s $@ $(tmp-target); then	\
>  		$(kecho) '  UPD     $@';			\
> -		mv -f $(dot-target).tmp $@;			\
> +		mv -f $(tmp-target) $@;				\
>  	fi
>  endef
>  
> @@ -81,13 +118,39 @@ build := -f $(srctree)/scripts/Makefile.build obj
>  # $(Q)$(MAKE) $(clean)=dir
>  clean := -f $(srctree)/scripts/Makefile.clean obj
>  
> -# echo command.
> -# Short version is used, if $(quiet) equals `quiet_', otherwise full one.
> -echo-cmd = $(if $($(quiet)cmd_$(1)),\
> -	echo '  $(call escsq,$($(quiet)cmd_$(1)))$(echo-why)';)
> +# pring log
> +#
> +# If quiet is "silent_", print nothing and sink stdout
> +# If quiet is "quiet_", print short log
> +# If quiet is empty, print short log and whole command
> +silent_log_print = exec >/dev/null;
> + quiet_log_print = $(if $(quiet_cmd_$1), echo '  $(call escsq,$(quiet_cmd_$1)$(why))';)
> +       log_print = echo '$(pound) $(call escsq,$(or $(quiet_cmd_$1),cmd_$1 $@)$(why))'; \
> +                   echo '  $(call escsq,$(cmd_$1))';
>  
> -# printing commands
> -cmd = @set -e; $(echo-cmd) $(cmd_$(1))
> +# Delete the target on interruption
> +#
> +# GNU Make automatically deletes the target if it has already been changed by
> +# the interrupted recipe. So, you can safely stop the build by Ctrl-C (Make
> +# will delete incomplete targets), and resume it later.
> +#
> +# However, this does not work when the stderr is piped to another program, like
> +#  $ make >&2 | tee log
> +# Make dies with SIGPIPE before cleaning the targets.
> +#
> +# To address it, we clean the target in signal traps.
> +#
> +# Make deletes the target when it catches SIGHUP, SIGINT, SIGQUIT, SIGTERM.
> +# So, we cover them, and also SIGPIPE just in case.
> +#
> +# Of course, this is unneeded for phony targets.
> +delete-on-interrupt = \
> +	$(if $(filter-out $(PHONY), $@), \
> +		$(foreach sig, HUP INT QUIT TERM PIPE, \
> +			trap 'rm -f $@; trap - $(sig); kill -s $(sig) $$$$' $(sig);))
> +
> +# print and execute commands
> +cmd = @$(if $(cmd_$(1)),set -e; $($(quiet)log_print) $(delete-on-interrupt) $(cmd_$(1)),:)
>  
>  ###
>  # if_changed      - execute command if any prerequisite is newer than
> @@ -142,7 +205,7 @@ if_changed_dep = $(if $(if-changed-cond),$(cmd_and_fixdep),@:)
>  
>  cmd_and_fixdep =                                                             \
>  	$(cmd);                                                              \
> -	scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(dot-target).cmd;\
> +	$(objtree)/scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(dot-target).cmd;\
>  	rm -f $(depfile)
>  
>  # Usage: $(call if_changed_rule,foo)
> @@ -169,8 +232,8 @@ if_changed_rule = $(if $(if-changed-cond),$(rule_$(1)),@:)
>  # (5) No dir/.target.cmd file (used to store command line)
>  # (6) No dir/.target.cmd file and target not listed in $(targets)
>  #     This is a good hint that there is a bug in the kbuild file
> -ifeq ($(KBUILD_VERBOSE),2)
> -why =                                                                        \
> +ifneq ($(findstring 2, $(KBUILD_VERBOSE)),)
> +_why =                                                                        \
>      $(if $(filter $@, $(PHONY)),- due to target is PHONY,                    \
>          $(if $(wildcard $@),                                                 \
>              $(if $(newer-prereqs),- due to: $(newer-prereqs),                \
> @@ -187,11 +250,23 @@ why =                                                                        \
>           )                                                                   \
>       )
>  
> -echo-why = $(call escsq, $(strip $(why)))
> +why = $(space)$(strip $(_why))
>  endif
>  
> +###############################################################################
> +
>  # delete partially updated (i.e. corrupted) files on error
>  .DELETE_ON_ERROR:
>  
>  # do not delete intermediate files automatically
> +#
> +# .NOTINTERMEDIATE is more correct, but only available on newer Make versions.
> +# Make 4.4 introduced .NOTINTERMEDIATE, and it appears in .FEATURES, but the
> +# global .NOTINTERMEDIATE does not work. We can use it on Make > 4.4.
> +# Use .SECONDARY for older Make versions, but "newer-prereq" cannot detect
> +# deleted files.
> +ifneq ($(and $(filter notintermediate, $(.FEATURES)),$(filter-out 4.4,$(MAKE_VERSION))),)
> +.NOTINTERMEDIATE:
> +else
>  .SECONDARY:
> +endif
> diff --git a/scripts/tags.sh b/scripts/tags.sh
> index 4e18ae5282a6..6e0e0a7c2270 100755
> --- a/scripts/tags.sh
> +++ b/scripts/tags.sh
> @@ -8,7 +8,7 @@
>  # Uses the following environment variables:
>  # SUBARCH, SRCARCH, srctree
>  
> -if [ "$KBUILD_VERBOSE" = "1" ]; then
> +if [[ "$KBUILD_VERBOSE" =~ "1" ]]; then
>  	set -x
>  fi
>  


-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/2] kbuild: split off compiler helpers into separate Makefile.compiler
  2024-12-03 10:42 [PATCH 1/2] kbuild: split off compiler helpers into separate Makefile.compiler Ahmad Fatoum
  2024-12-03 10:42 ` [PATCH 2/2] kbuild: sync scripts/Kbuild.include with Linux Ahmad Fatoum
@ 2024-12-06 10:08 ` Sascha Hauer
  1 sibling, 0 replies; 4+ messages in thread
From: Sascha Hauer @ 2024-12-06 10:08 UTC (permalink / raw)
  To: barebox, Ahmad Fatoum


On Tue, 03 Dec 2024 11:42:24 +0100, Ahmad Fatoum wrote:
> Linux has split the compile-specific helpers into a separate file, so
> let's do the same for barebox to make it easier to sync
> scripts/Kbuild.include in the follow-up commit.
> 
> 

Applied, thanks!

[1/2] kbuild: split off compiler helpers into separate Makefile.compiler
      https://git.pengutronix.de/cgit/barebox/commit/?id=0e0a2ad162a1 (link may not be stable)
[2/2] kbuild: sync scripts/Kbuild.include with Linux
      https://git.pengutronix.de/cgit/barebox/commit/?id=7b319be7cab6 (link may not be stable)

Best regards,
-- 
Sascha Hauer <s.hauer@pengutronix.de>




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-12-06 10:09 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-12-03 10:42 [PATCH 1/2] kbuild: split off compiler helpers into separate Makefile.compiler Ahmad Fatoum
2024-12-03 10:42 ` [PATCH 2/2] kbuild: sync scripts/Kbuild.include with Linux Ahmad Fatoum
2024-12-03 10:59   ` Ahmad Fatoum
2024-12-06 10:08 ` [PATCH 1/2] kbuild: split off compiler helpers into separate Makefile.compiler Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox