xref: /linux/tools/build/Makefile.build (revision bb1c928df78ee6e3665a0d013e74108cc9abf34b)
1###
2# Main build makefile.
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 <sam@mars.ravnborg.org>, 2015
8#  Copyright (C) Linus Torvalds <torvalds@linux-foundation.org>, 2015
9#
10
11PHONY := __build
12__build:
13
14ifeq ($(V),1)
15  quiet =
16  Q =
17else
18  quiet=quiet_
19  Q=@
20endif
21
22ifneq ($(findstring s,$(filter-out --%,$(MAKEFLAGS))),)
23  quiet=silent_
24endif
25
26build-dir := $(srctree)/tools/build
27
28# Define $(fixdep) for dep-cmd function
29ifeq ($(OUTPUT),)
30  fixdep := $(build-dir)/fixdep
31else
32  fixdep := $(OUTPUT)/fixdep
33endif
34
35# Generic definitions
36include $(build-dir)/Build.include
37
38# do not force detected configuration
39-include $(OUTPUT).config-detected
40
41# Init all relevant variables used in build files so
42# 1) they have correct type
43# 2) they do not inherit any value from the environment
44subdir-y     :=
45obj-y        :=
46subdir-y     :=
47subdir-obj-y :=
48
49# Build definitions
50build-file := $(dir)/Build
51-include $(build-file)
52
53quiet_cmd_flex  = FLEX     $@
54quiet_cmd_bison = BISON    $@
55
56# Create directory unless it exists
57quiet_cmd_mkdir = MKDIR    $(dir $@)
58      cmd_mkdir = mkdir -p $(dir $@)
59     rule_mkdir = $(if $(wildcard $(dir $@)),,@$(call echo-cmd,mkdir) $(cmd_mkdir))
60
61# Compile command
62quiet_cmd_cc_o_c = CC       $@
63      cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $<
64
65quiet_cmd_host_cc_o_c = HOSTCC   $@
66      cmd_host_cc_o_c = $(HOSTCC) $(host_c_flags) -c -o $@ $<
67
68quiet_cmd_cxx_o_c = CXX      $@
69      cmd_cxx_o_c = $(CXX) $(cxx_flags) -c -o $@ $<
70
71quiet_cmd_cpp_i_c = CPP      $@
72      cmd_cpp_i_c = $(CC) $(c_flags) -E -o $@ $<
73
74quiet_cmd_cc_s_c = AS       $@
75      cmd_cc_s_c = $(CC) $(c_flags) -S -o $@ $<
76
77quiet_cmd_gen = GEN      $@
78
79# Link agregate command
80# If there's nothing to link, create empty $@ object.
81quiet_cmd_ld_multi = LD       $@
82      cmd_ld_multi = $(if $(strip $(obj-y)),\
83                     $(LD) -r -o $@  $(filter $(obj-y),$^),rm -f $@; $(AR) rcs $@)
84
85quiet_cmd_host_ld_multi = HOSTLD   $@
86      cmd_host_ld_multi = $(if $(strip $(obj-y)),\
87                          $(HOSTLD) -r -o $@  $(filter $(obj-y),$^),rm -f $@; $(HOSTAR) rcs $@)
88
89ifneq ($(filter $(obj),$(hostprogs)),)
90  host = host_
91endif
92
93# Build rules
94$(OUTPUT)%.o: %.c FORCE
95	$(call rule_mkdir)
96	$(call if_changed_dep,$(host)cc_o_c)
97
98$(OUTPUT)%.o: %.cpp FORCE
99	$(call rule_mkdir)
100	$(call if_changed_dep,cxx_o_c)
101
102$(OUTPUT)%.o: %.S FORCE
103	$(call rule_mkdir)
104	$(call if_changed_dep,$(host)cc_o_c)
105
106$(OUTPUT)%.i: %.c FORCE
107	$(call rule_mkdir)
108	$(call if_changed_dep,cpp_i_c)
109
110$(OUTPUT)%.s: %.S FORCE
111	$(call rule_mkdir)
112	$(call if_changed_dep,cpp_i_c)
113
114$(OUTPUT)%.s: %.c FORCE
115	$(call rule_mkdir)
116	$(call if_changed_dep,cc_s_c)
117
118# Gather build data:
119#   obj-y        - list of build objects
120#   subdir-y     - list of directories to nest
121#   subdir-obj-y - list of directories objects 'dir/$(obj)-in.o'
122obj-y        := $($(obj)-y)
123subdir-y     := $(patsubst %/,%,$(filter %/, $(obj-y)))
124obj-y        := $(patsubst %/, %/$(obj)-in.o, $(obj-y))
125subdir-obj-y := $(filter %/$(obj)-in.o, $(obj-y))
126
127# '$(OUTPUT)/dir' prefix to all objects
128objprefix    := $(subst ./,,$(OUTPUT)$(dir)/)
129obj-y        := $(addprefix $(objprefix),$(obj-y))
130subdir-obj-y := $(addprefix $(objprefix),$(subdir-obj-y))
131
132# Final '$(obj)-in.o' object
133in-target := $(objprefix)$(obj)-in.o
134
135PHONY += $(subdir-y)
136
137$(subdir-y):
138	$(Q)$(MAKE) -f $(build-dir)/Makefile.build dir=$(dir)/$@ obj=$(obj)
139
140$(sort $(subdir-obj-y)): $(subdir-y) ;
141
142$(in-target): $(obj-y) FORCE
143	$(call rule_mkdir)
144	$(call if_changed,$(host)ld_multi)
145
146__build: $(in-target)
147	@:
148
149PHONY += FORCE
150FORCE:
151
152# Include all cmd files to get all the dependency rules
153# for all objects included
154targets   := $(wildcard $(sort $(obj-y) $(in-target) $(MAKECMDGOALS)))
155cmd_files := $(wildcard $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd))
156
157ifneq ($(cmd_files),)
158  include $(cmd_files)
159endif
160
161.PHONY: $(PHONY)
162