1# SPDX-License-Identifier: GPL-2.0 2#### 3# kbuild: Generic definitions 4 5# Convenient variables 6squote := ' 7empty := 8space := $(empty) $(empty) 9space_escape := _-_SPACE_-_ 10pound := \# 11 12### 13# Name of target with a '.' as filename prefix. foo/bar.o => foo/.bar.o 14dot-target = $(@D)/.$(@F) 15 16### 17# dependencies 18DEPS = .*.d 19DEPS_INCLUDE = $(addsuffix .d2, $(basename $(wildcard $(DEPS)))) 20 21### 22# real prerequisites without phony targets 23real-prereqs = $(filter-out $(PHONY), $^) 24 25### 26# Escape single quote for use in echo statements 27escsq = $(subst $(squote),'\$(squote)',$1) 28 29# as-insn: Check whether assembler supports an instruction. 30# Usage: cflags-y += $(call as-insn,CC FLAGS,"insn",option-yes,option-no) 31as-insn = $(if $(shell echo 'void _(void) { asm volatile ( $(2) ); }' \ 32 | $(filter-out -M% %.d -include %/include/xen/config.h,$(1)) \ 33 -c -x c -o /dev/null - 2>&1),$(4),$(3)) 34 35# as-option-add: Conditionally add options to flags 36# Usage: $(call as-option-add,CFLAGS,CC,"insn",option-yes,option-no) 37as-option-add = $(eval $(call as-option-add-closure,$(1),$(2),$(3),$(4),$(5))) 38define as-option-add-closure 39 ifeq ($$(call as-insn,$$($(2)) $$($(1)),$(3),y,n),y) 40 $(1) += $(4) 41 else 42 $(1) += $(5) 43 endif 44endef 45 46# cc-ifversion 47# Usage: EXTRA_CFLAGS += $(call cc-ifversion, -lt, 0402, -O1) 48cc-ifversion = $(shell [ $(CONFIG_GCC_VERSION)0 $(1) $(2)000 ] && echo $(3) || echo $(4)) 49 50# Shorthand for $(MAKE) clean 51# Usage: 52# $(MAKE) $(clean) dir 53clean := -f $(BASEDIR)/scripts/Makefile.clean clean -C 54 55# echo command. 56# Short version is used, if $(quiet) equals `quiet_', otherwise full one. 57echo-cmd = $(if $($(quiet)cmd_$(1)),\ 58 echo ' $(call escsq,$($(quiet)cmd_$(1)))$(echo-why)';) 59 60# printing commands 61cmd = @set -e; $(echo-cmd) $(cmd_$(1)) 62 63### 64# if_changed - execute command if any prerequisite is newer than 65# target, or command line has changed 66# if_changed_rule - as if_changed but execute rule instead 67 68ifneq ($(KBUILD_NOCMDDEP),1) 69# Check if both commands are the same including their order. Result is empty 70# string if equal. User may override this check using make KBUILD_NOCMDDEP=1 71cmd-check = $(filter-out $(subst $(space),$(space_escape),$(strip $(cmd_$@))), \ 72 $(subst $(space),$(space_escape),$(strip $(cmd_$1)))) 73else 74cmd-check = $(if $(strip $(cmd_$@)),,1) 75endif 76 77# Replace >$< with >$$< to preserve $ when reloading the .cmd file 78# (needed for make) 79# Replace >#< with >$(pound)< to avoid starting a comment in the .cmd file 80# (needed for make) 81# Replace >'< with >'\''< to be able to enclose the whole string in '...' 82# (needed for the shell) 83make-cmd = $(call escsq,$(subst $(pound),$$(pound),$(subst $$,$$$$,$(cmd_$(1))))) 84 85# Find any prerequisites that is newer than target or that does not exist. 86# PHONY targets skipped in both cases. 87any-prereq = $(filter-out $(PHONY),$?)$(filter-out $(PHONY) $(wildcard $^),$^) 88 89# Execute command if command has changed or prerequisite(s) are updated. 90if_changed = $(if $(any-prereq)$(cmd-check), \ 91 $(cmd); \ 92 printf '%s\n' 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd, @:) 93 94# Usage: $(call if_changed_rule,foo) 95# Will check if $(cmd_foo) or any of the prerequisites changed, 96# and if so will execute $(rule_foo). 97if_changed_rule = $(if $(any-prereq)$(cmd-check),$(rule_$(1)),@:) 98 99cmd_and_record = \ 100 $(cmd); \ 101 printf '%s\n' 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd 102 103### 104# why - tell why a target got built 105# enabled by make V=2 106# Output (listed in the order they are checked): 107# (1) - due to target is PHONY 108# (2) - due to target missing 109# (3) - due to: file1.h file2.h 110# (4) - due to command line change 111# (5) - due to missing .cmd file 112# (6) - due to target not in $(targets) 113# (1) PHONY targets are always build 114# (2) No target, so we better build it 115# (3) Prerequisite is newer than target 116# (4) The command line stored in the file named dir/.target.cmd 117# differed from actual command line. This happens when compiler 118# options changes 119# (5) No dir/.target.cmd file (used to store command line) 120# (6) No dir/.target.cmd file and target not listed in $(targets) 121# This is a good hint that there is a bug in the kbuild file 122ifeq ($(KBUILD_VERBOSE),2) 123why = \ 124 $(if $(filter $@, $(PHONY)),- due to target is PHONY, \ 125 $(if $(wildcard $@), \ 126 $(if $(any-prereq),- due to: $(any-prereq), \ 127 $(if $(cmd-check), \ 128 $(if $(cmd_$@),- due to command line change, \ 129 $(if $(filter $@, $(targets)), \ 130 - due to missing .cmd file, \ 131 - due to $(notdir $@) not in $$(targets) \ 132 ) \ 133 ) \ 134 ) \ 135 ), \ 136 - due to target missing \ 137 ) \ 138 ) 139 140echo-why = $(call escsq, $(strip $(why))) 141endif 142