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