]> Git Repo - linux.git/blob - tools/build/Build.include
Linux 6.14-rc3
[linux.git] / tools / build / Build.include
1 ###
2 # build: Generic definitions
3 #
4 #  Lots of this code have been borrowed or heavily inspired from parts
5 #  of kbuild code, which is not credited, but mostly developed by:
6 #
7 #  Copyright (C) Sam Ravnborg <[email protected]>, 2015
8 #  Copyright (C) Linus Torvalds <[email protected]>, 2015
9 #
10
11 ###
12 # Convenient variables
13 comma   := ,
14 squote  := '
15 pound   := \#
16 empty   :=
17 space   := $(empty) $(empty)
18
19 ###
20 # Name of target with a '.' as filename prefix. foo/bar.o => foo/.bar.o
21 dot-target = $(dir $@).$(notdir $@)
22
23 ###
24 # filename of target with directory and extension stripped
25 basetarget = $(basename $(notdir $@))
26
27 ###
28 # The temporary file to save gcc -MD generated dependencies must not
29 # contain a comma
30 depfile = $(subst $(comma),_,$(dot-target).d)
31
32 ###
33 # Check if both arguments has same arguments. Result is empty string if equal.
34 arg-check = $(strip $(filter-out $(cmd_$(1)), $(cmd_$@)) \
35                     $(filter-out $(cmd_$@),   $(cmd_$(1))) )
36
37 ###
38 # Escape single quote for use in echo statements
39 escsq = $(subst $(squote),'\$(squote)',$1)
40
41 # Echo command
42 # Short version is used, if $(quiet) equals `quiet_', otherwise full one.
43 echo-cmd = $(if $($(quiet)cmd_$(1)),\
44            echo '  $(call escsq,$($(quiet)cmd_$(1)))';)
45
46 ###
47 # Replace >$< with >$$< to preserve $ when reloading the .cmd file
48 # (needed for make)
49 # Replace >#< with >$(pound)< to avoid starting a comment in the .cmd file
50 # (needed for make)
51 # Replace >'< with >'\''< to be able to enclose the whole string in '...'
52 # (needed for the shell)
53 make-cmd = $(call escsq,$(subst $(pound),$$(pound),$(subst $$,$$$$,$(cmd_$(1)))))
54
55 ###
56 # Find any prerequisites that is newer than target or that does not exist.
57 # PHONY targets skipped in both cases.
58 any-prereq = $(filter-out $(PHONY),$?) $(filter-out $(PHONY) $(wildcard $^),$^)
59
60 ###
61 # Copy dependency data into .cmd file
62 #  - gcc -M dependency info
63 #  - command line to create object 'cmd_object :='
64 dep-cmd = $(if $(wildcard $(fixdep)),                                           \
65            $(fixdep) $(depfile) $@ '$(make-cmd)' > $(dot-target).tmp;           \
66            rm -f $(depfile);                                                    \
67            mv -f $(dot-target).tmp $(dot-target).cmd,                           \
68            printf '$(pound) cannot find fixdep (%s)\n' $(fixdep) > $(dot-target).cmd; \
69            printf '$(pound) using basic dep data\n\n' >> $(dot-target).cmd;           \
70            cat $(depfile) >> $(dot-target).cmd;                                 \
71            printf '\n%s\n' 'cmd_$@ := $(make-cmd)' >> $(dot-target).cmd)
72
73 ###
74 # if_changed_dep  - execute command if any prerequisite is newer than
75 #                   target, or command line has changed and update
76 #                   dependencies in the cmd file
77 if_changed_dep = $(if $(strip $(any-prereq) $(arg-check)),         \
78                   @set -e;                                         \
79                   $(echo-cmd) $(cmd_$(1));                         \
80                   $(dep-cmd))
81
82 # if_changed      - execute command if any prerequisite is newer than
83 #                   target, or command line has changed
84 if_changed = $(if $(strip $(any-prereq) $(arg-check)),                   \
85               @set -e;                                                   \
86               $(echo-cmd) $(cmd_$(1));                                   \
87               printf '%s\n' 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd)
88
89 ###
90 # C flags to be used in rule definitions, includes:
91 # - depfile generation
92 # - global $(CFLAGS)
93 # - per target C flags
94 # - per object C flags
95 # - BUILD_STR macro to allow '-D"$(variable)"' constructs
96 c_flags_1 = -Wp,-MD,$(depfile) -Wp,-MT,$@ $(CFLAGS) -D"BUILD_STR(s)=\#s" $(CFLAGS_$(basetarget).o) $(CFLAGS_$(obj))
97 c_flags_2 = $(filter-out $(CFLAGS_REMOVE_$(basetarget).o), $(c_flags_1))
98 c_flags   = $(filter-out $(CFLAGS_REMOVE_$(obj)), $(c_flags_2))
99 cxx_flags = -Wp,-MD,$(depfile) -Wp,-MT,$@ $(CXXFLAGS) -D"BUILD_STR(s)=\#s" $(CXXFLAGS_$(basetarget).o) $(CXXFLAGS_$(obj))
100
101 ###
102 ## HOSTCC C flags
103
104 host_c_flags = -Wp,-MD,$(depfile) -Wp,-MT,$@ $(HOSTCFLAGS) -D"BUILD_STR(s)=\#s" $(HOSTCFLAGS_$(basetarget).o) $(HOSTCFLAGS_$(obj))
105
106 # output directory for tests below
107 TMPOUT = .tmp_$$$$
108
109 # try-run
110 # Usage: option = $(call try-run, $(CC)...-o "$$TMP",option-ok,otherwise)
111 # Exit code chooses option. "$$TMP" serves as a temporary file and is
112 # automatically cleaned up.
113 try-run = $(shell set -e;               \
114         TMP=$(TMPOUT)/tmp;              \
115         mkdir -p $(TMPOUT);             \
116         trap "rm -rf $(TMPOUT)" EXIT;   \
117         if ($(1)) >/dev/null 2>&1;      \
118         then echo "$(2)";               \
119         else echo "$(3)";               \
120         fi)
121
122 # cc-option
123 # Usage: cflags-y += $(call cc-option,-march=winchip-c6,-march=i586)
124 cc-option = $(call try-run, \
125         $(CC) -Werror $(1) -c -x c /dev/null -o "$$TMP",$(1),$(2))
126
127 # delete partially updated (i.e. corrupted) files on error
128 .DELETE_ON_ERROR:
This page took 0.033297 seconds and 4 git commands to generate.