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