blob: e279a71c650d2cdc7ca4f9e7c07f05a79e11b2eb [file] [log] [blame]
Jiri Olsac819e2c2014-12-29 13:51:45 +01001###
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 =
Jiri Olsaceed2522015-01-11 23:59:55 +010016 Q =
Jiri Olsac819e2c2014-12-29 13:51:45 +010017else
18 quiet=quiet_
Jiri Olsaceed2522015-01-11 23:59:55 +010019 Q=@
Jiri Olsac819e2c2014-12-29 13:51:45 +010020endif
21
Josh Poimboeufafdfe5f2017-01-18 22:16:55 -060022ifneq ($(filter 4.%,$(MAKE_VERSION)),) # make-4
23ifneq ($(filter %s ,$(firstword x$(MAKEFLAGS))),)
24 quiet=silent_
25endif
26else # make-3.8x
27ifneq ($(filter s% -s%,$(MAKEFLAGS)),)
28 quiet=silent_
29endif
30endif
31
Jiri Olsac819e2c2014-12-29 13:51:45 +010032build-dir := $(srctree)/tools/build
33
Jiri Olsa9fb813232015-09-23 12:34:00 +020034# Define $(fixdep) for dep-cmd function
35ifeq ($(OUTPUT),)
36 fixdep := $(build-dir)/fixdep
37else
38 fixdep := $(OUTPUT)/fixdep
39endif
40
Jiri Olsac819e2c2014-12-29 13:51:45 +010041# Generic definitions
42include $(build-dir)/Build.include
43
Jiri Olsafcfd6612014-12-31 17:37:00 +010044# do not force detected configuration
Aaro Koskinen64227372015-07-01 14:54:42 +030045-include $(OUTPUT).config-detected
Jiri Olsafcfd6612014-12-31 17:37:00 +010046
Jiri Olsac819e2c2014-12-29 13:51:45 +010047# Init all relevant variables used in build files so
48# 1) they have correct type
49# 2) they do not inherit any value from the environment
50subdir-y :=
51obj-y :=
52subdir-y :=
53subdir-obj-y :=
54
55# Build definitions
56build-file := $(dir)/Build
Jiri Olsad7a3d852015-05-29 17:42:58 +020057-include $(build-file)
Jiri Olsac819e2c2014-12-29 13:51:45 +010058
Jiri Olsa9352aab2014-12-29 17:42:46 +010059quiet_cmd_flex = FLEX $@
60quiet_cmd_bison = BISON $@
61
Jiri Olsa579ff6d2014-12-30 16:44:11 +010062# Create directory unless it exists
63quiet_cmd_mkdir = MKDIR $(dir $@)
64 cmd_mkdir = mkdir -p $(dir $@)
65 rule_mkdir = $(if $(wildcard $(dir $@)),,@$(call echo-cmd,mkdir) $(cmd_mkdir))
66
Jiri Olsac819e2c2014-12-29 13:51:45 +010067# Compile command
68quiet_cmd_cc_o_c = CC $@
69 cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $<
70
Jiri Olsa0c3b7e42016-09-27 16:18:46 +020071quiet_cmd_host_cc_o_c = HOSTCC $@
72 cmd_host_cc_o_c = $(HOSTCC) $(host_c_flags) -c -o $@ $<
73
Wang Nanf61bdc32016-09-26 07:26:55 +000074quiet_cmd_cxx_o_c = CXX $@
75 cmd_cxx_o_c = $(CXX) $(cxx_flags) -c -o $@ $<
76
Masahiro Yamada23d43842016-04-08 11:24:47 +090077quiet_cmd_cpp_i_c = CPP $@
78 cmd_cpp_i_c = $(CC) $(c_flags) -E -o $@ $<
Jiri Olsa19993072014-12-30 18:44:38 +010079
80quiet_cmd_cc_s_c = AS $@
81 cmd_cc_s_c = $(CC) $(c_flags) -S -o $@ $<
82
Adrian Hunter237fae72015-08-13 10:14:55 +030083quiet_cmd_gen = GEN $@
84
Jiri Olsac819e2c2014-12-29 13:51:45 +010085# Link agregate command
86# If there's nothing to link, create empty $@ object.
87quiet_cmd_ld_multi = LD $@
88 cmd_ld_multi = $(if $(strip $(obj-y)),\
Jiri Olsa0c3b7e42016-09-27 16:18:46 +020089 $(LD) -r -o $@ $(filter $(obj-y),$^),rm -f $@; $(AR) rcs $@)
90
91quiet_cmd_host_ld_multi = HOSTLD $@
92 cmd_host_ld_multi = $(if $(strip $(obj-y)),\
93 $(HOSTLD) -r -o $@ $(filter $(obj-y),$^),rm -f $@; $(HOSTAR) rcs $@)
94
95ifneq ($(filter $(obj),$(hostprogs)),)
96 host = host_
97endif
Jiri Olsac819e2c2014-12-29 13:51:45 +010098
99# Build rules
100$(OUTPUT)%.o: %.c FORCE
Jiri Olsa579ff6d2014-12-30 16:44:11 +0100101 $(call rule_mkdir)
Jiri Olsa0c3b7e42016-09-27 16:18:46 +0200102 $(call if_changed_dep,$(host)cc_o_c)
Jiri Olsac819e2c2014-12-29 13:51:45 +0100103
Wang Nanf61bdc32016-09-26 07:26:55 +0000104$(OUTPUT)%.o: %.cpp FORCE
105 $(call rule_mkdir)
106 $(call if_changed_dep,cxx_o_c)
107
Jiri Olsac819e2c2014-12-29 13:51:45 +0100108$(OUTPUT)%.o: %.S FORCE
Jiri Olsa579ff6d2014-12-30 16:44:11 +0100109 $(call rule_mkdir)
Jiri Olsa0c3b7e42016-09-27 16:18:46 +0200110 $(call if_changed_dep,$(host)cc_o_c)
Jiri Olsac819e2c2014-12-29 13:51:45 +0100111
Jiri Olsa19993072014-12-30 18:44:38 +0100112$(OUTPUT)%.i: %.c FORCE
113 $(call rule_mkdir)
Masahiro Yamada23d43842016-04-08 11:24:47 +0900114 $(call if_changed_dep,cpp_i_c)
Jiri Olsa19993072014-12-30 18:44:38 +0100115
Masahiro Yamada67678792016-02-01 02:59:00 +0900116$(OUTPUT)%.s: %.S FORCE
Jiri Olsa19993072014-12-30 18:44:38 +0100117 $(call rule_mkdir)
Masahiro Yamada23d43842016-04-08 11:24:47 +0900118 $(call if_changed_dep,cpp_i_c)
Jiri Olsa19993072014-12-30 18:44:38 +0100119
120$(OUTPUT)%.s: %.c FORCE
121 $(call rule_mkdir)
122 $(call if_changed_dep,cc_s_c)
123
Jiri Olsac819e2c2014-12-29 13:51:45 +0100124# Gather build data:
125# obj-y - list of build objects
126# subdir-y - list of directories to nest
127# subdir-obj-y - list of directories objects 'dir/$(obj)-in.o'
128obj-y := $($(obj)-y)
129subdir-y := $(patsubst %/,%,$(filter %/, $(obj-y)))
130obj-y := $(patsubst %/, %/$(obj)-in.o, $(obj-y))
131subdir-obj-y := $(filter %/$(obj)-in.o, $(obj-y))
132
133# '$(OUTPUT)/dir' prefix to all objects
Lukas Wunner75e84ab2015-06-18 13:00:32 +0200134objprefix := $(subst ./,,$(OUTPUT)$(dir)/)
135obj-y := $(addprefix $(objprefix),$(obj-y))
136subdir-obj-y := $(addprefix $(objprefix),$(subdir-obj-y))
Jiri Olsac819e2c2014-12-29 13:51:45 +0100137
138# Final '$(obj)-in.o' object
Lukas Wunner75e84ab2015-06-18 13:00:32 +0200139in-target := $(objprefix)$(obj)-in.o
Jiri Olsac819e2c2014-12-29 13:51:45 +0100140
141PHONY += $(subdir-y)
142
143$(subdir-y):
Jiri Olsaceed2522015-01-11 23:59:55 +0100144 $(Q)$(MAKE) -f $(build-dir)/Makefile.build dir=$(dir)/$@ obj=$(obj)
Jiri Olsac819e2c2014-12-29 13:51:45 +0100145
146$(sort $(subdir-obj-y)): $(subdir-y) ;
147
148$(in-target): $(obj-y) FORCE
149 $(call rule_mkdir)
Jiri Olsa0c3b7e42016-09-27 16:18:46 +0200150 $(call if_changed,$(host)ld_multi)
Jiri Olsac819e2c2014-12-29 13:51:45 +0100151
152__build: $(in-target)
153 @:
154
155PHONY += FORCE
156FORCE:
157
158# Include all cmd files to get all the dependency rules
159# for all objects included
Jiri Olsa19993072014-12-30 18:44:38 +0100160targets := $(wildcard $(sort $(obj-y) $(in-target) $(MAKECMDGOALS)))
Jiri Olsac819e2c2014-12-29 13:51:45 +0100161cmd_files := $(wildcard $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd))
162
163ifneq ($(cmd_files),)
164 include $(cmd_files)
165endif
166
167.PHONY: $(PHONY)