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