Denis Vlasenko | 7d219aa | 2006-10-05 10:17:08 +0000 | [diff] [blame] | 1 | #### |
| 2 | # kbuild: Generic definitions |
| 3 | |
| 4 | # Convinient variables |
| 5 | comma := , |
| 6 | squote := ' |
Jean-Philippe Brucker | ed8af51 | 2020-03-12 17:19:45 +0100 | [diff] [blame] | 7 | quote := " |
Denis Vlasenko | 7d219aa | 2006-10-05 10:17:08 +0000 | [diff] [blame] | 8 | empty := |
| 9 | space := $(empty) $(empty) |
| 10 | |
| 11 | ### |
| 12 | # The temporary file to save gcc -MD generated dependencies must not |
| 13 | # contain a comma |
| 14 | depfile = $(subst $(comma),_,$(@D)/.$(@F).d) |
| 15 | |
| 16 | ### |
| 17 | # Escape single quote for use in echo statements |
| 18 | escsq = $(subst $(squote),'\$(squote)',$1) |
| 19 | |
| 20 | ### |
| 21 | # filechk is used to check if the content of a generated file is updated. |
| 22 | # Sample usage: |
| 23 | # define filechk_sample |
| 24 | # echo $KERNELRELEASE |
| 25 | # endef |
| 26 | # version.h : Makefile |
| 27 | # $(call filechk,sample) |
| 28 | # The rule defined shall write to stdout the content of the new file. |
| 29 | # The existing file will be compared with the new one. |
| 30 | # - If no file exist it is created |
| 31 | # - If the content differ the new file is used |
| 32 | # - If they are equal no change, and no timestamp update |
| 33 | # - stdin is piped in from the first prerequisite ($<) so one has |
| 34 | # to specify a valid file as first prerequisite (often the kbuild file) |
| 35 | define filechk |
| 36 | $(Q)set -e; \ |
| 37 | echo ' CHK $@'; \ |
| 38 | mkdir -p $(dir $@); \ |
| 39 | $(filechk_$(1)) < $< > $@.tmp; \ |
| 40 | if [ -r $@ ] && cmp -s $@ $@.tmp; then \ |
| 41 | rm -f $@.tmp; \ |
| 42 | else \ |
| 43 | echo ' UPD $@'; \ |
| 44 | mv -f $@.tmp $@; \ |
| 45 | fi |
| 46 | endef |
| 47 | |
| 48 | ###### |
| 49 | # gcc support functions |
| 50 | # See documentation in Documentation/kbuild/makefiles.txt |
| 51 | |
| 52 | # as-option |
| 53 | # Usage: cflags-y += $(call as-option, -Wa$(comma)-isa=foo,) |
| 54 | |
| 55 | as-option = $(shell if $(CC) $(CFLAGS) $(1) -Wa,-Z -c -o /dev/null \ |
| 56 | -xassembler /dev/null > /dev/null 2>&1; then echo "$(1)"; \ |
| 57 | else echo "$(2)"; fi ;) |
| 58 | |
| 59 | # cc-option |
| 60 | # Usage: cflags-y += $(call cc-option, -march=winchip-c6, -march=i586) |
| 61 | |
| 62 | cc-option = $(shell if $(CC) $(CFLAGS) $(1) -S -o /dev/null -xc /dev/null \ |
| 63 | > /dev/null 2>&1; then echo "$(1)"; else echo "$(2)"; fi ;) |
| 64 | |
Bernhard Reutner-Fischer | 1c747b3 | 2007-01-23 11:44:14 +0000 | [diff] [blame] | 65 | # hostcc-option |
| 66 | # Usage: hostcflags-y += $(call hostcc-option, -march=winchip-c6, -march=i586) |
| 67 | |
| 68 | hostcc-option = $(shell if $(HOSTCC) $(HOSTCFLAGS) $(1) -S -o /dev/null -xc /dev/null \ |
| 69 | > /dev/null 2>&1; then echo "$(1)"; else echo "$(2)"; fi ;) |
| 70 | |
Denis Vlasenko | 7d219aa | 2006-10-05 10:17:08 +0000 | [diff] [blame] | 71 | # cc-option-yn |
| 72 | # Usage: flag := $(call cc-option-yn, -march=winchip-c6) |
| 73 | cc-option-yn = $(shell if $(CC) $(CFLAGS) $(1) -S -o /dev/null -xc /dev/null \ |
| 74 | > /dev/null 2>&1; then echo "y"; else echo "n"; fi;) |
| 75 | |
| 76 | # cc-option-align |
| 77 | # Prefix align with either -falign or -malign |
| 78 | cc-option-align = $(subst -functions=0,,\ |
| 79 | $(call cc-option,-falign-functions=0,-malign-functions=0)) |
| 80 | |
| 81 | # cc-version |
| 82 | # Usage gcc-ver := $(call cc-version, $(CC)) |
Bernhard Reutner-Fischer | 1399282 | 2007-06-25 10:41:01 +0000 | [diff] [blame] | 83 | cc-version = $(shell PATH="$(PATH)" $(CONFIG_SHELL) $(srctree)/scripts/gcc-version.sh \ |
Denis Vlasenko | 7d219aa | 2006-10-05 10:17:08 +0000 | [diff] [blame] | 84 | $(if $(1), $(1), $(CC))) |
| 85 | |
| 86 | # cc-ifversion |
| 87 | # Usage: EXTRA_CFLAGS += $(call cc-ifversion, -lt, 0402, -O1) |
| 88 | cc-ifversion = $(shell if [ $(call cc-version, $(CC)) $(1) $(2) ]; then \ |
| 89 | echo $(3); fi;) |
| 90 | |
| 91 | ### |
| 92 | # Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj= |
| 93 | # Usage: |
| 94 | # $(Q)$(MAKE) $(build)=dir |
| 95 | build := -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.build obj |
| 96 | |
| 97 | # Prefix -I with $(srctree) if it is not an absolute path |
| 98 | addtree = $(if $(filter-out -I/%,$(1)),$(patsubst -I%,-I$(srctree)/%,$(1))) $(1) |
| 99 | # Find all -I options and call addtree |
| 100 | flags = $(foreach o,$($(1)),$(if $(filter -I%,$(o)),$(call addtree,$(o)),$(o))) |
| 101 | |
| 102 | # If quiet is set, only print short version of command |
| 103 | cmd = @$(echo-cmd) $(cmd_$(1)) |
| 104 | |
| 105 | # Add $(obj)/ for paths that is not absolute |
| 106 | objectify = $(foreach o,$(1),$(if $(filter /%,$(o)),$(o),$(obj)/$(o))) |
| 107 | |
| 108 | ### |
Denis Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 109 | # if_changed - execute command if any prerequisite is newer than |
Denis Vlasenko | 7d219aa | 2006-10-05 10:17:08 +0000 | [diff] [blame] | 110 | # target, or command line has changed |
| 111 | # if_changed_dep - as if_changed, but uses fixdep to reveal dependencies |
| 112 | # including used config symbols |
| 113 | # if_changed_rule - as if_changed but execute rule instead |
| 114 | # See Documentation/kbuild/makefiles.txt for more info |
| 115 | |
| 116 | ifneq ($(KBUILD_NOCMDDEP),1) |
| 117 | # Check if both arguments has same arguments. Result in empty string if equal |
| 118 | # User may override this check using make KBUILD_NOCMDDEP=1 |
| 119 | arg-check = $(strip $(filter-out $(1), $(2)) $(filter-out $(2), $(1)) ) |
| 120 | endif |
| 121 | |
| 122 | # echo command. Short version is $(quiet) equals quiet, otherwise full command |
| 123 | echo-cmd = $(if $($(quiet)cmd_$(1)), \ |
| 124 | echo ' $(call escsq,$($(quiet)cmd_$(1)))';) |
| 125 | |
| 126 | make-cmd = $(subst \#,\\\#,$(subst $$,$$$$,$(call escsq,$(cmd_$(1))))) |
| 127 | |
| 128 | # function to only execute the passed command if necessary |
| 129 | # >'< substitution is for echo to work, >$< substitution to preserve $ when reloading .cmd file |
| 130 | # note: when using inline perl scripts [perl -e '...$$t=1;...'] in $(cmd_xxx) double $$ your perl vars |
| 131 | # |
| 132 | if_changed = $(if $(strip $(filter-out $(PHONY),$?) \ |
| 133 | $(call arg-check, $(cmd_$(1)), $(cmd_$@)) ), \ |
| 134 | @set -e; \ |
| 135 | $(echo-cmd) $(cmd_$(1)); \ |
| 136 | echo 'cmd_$@ := $(make-cmd)' > $(@D)/.$(@F).cmd) |
| 137 | |
| 138 | # execute the command and also postprocess generated .d dependencies |
| 139 | # file |
| 140 | if_changed_dep = $(if $(strip $(filter-out $(PHONY),$?) \ |
| 141 | $(filter-out FORCE $(wildcard $^),$^) \ |
| 142 | $(call arg-check, $(cmd_$(1)), $(cmd_$@)) ), \ |
| 143 | @set -e; \ |
| 144 | $(echo-cmd) $(cmd_$(1)); \ |
| 145 | scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(@D)/.$(@F).tmp; \ |
| 146 | rm -f $(depfile); \ |
| 147 | mv -f $(@D)/.$(@F).tmp $(@D)/.$(@F).cmd) |
| 148 | |
| 149 | # Usage: $(call if_changed_rule,foo) |
| 150 | # will check if $(cmd_foo) changed, or any of the prequisites changed, |
| 151 | # and if so will execute $(rule_foo) |
| 152 | if_changed_rule = $(if $(strip $(filter-out $(PHONY),$?) \ |
| 153 | $(call arg-check, $(cmd_$(1)), $(cmd_$@)) ),\ |
| 154 | @set -e; \ |
| 155 | $(rule_$(1))) |