blob: cd72016c3cfa71159511549a5dc74e7cc36e82b0 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001# SPDX-License-Identifier: GPL-2.0
Jiri Olsac819e2c2014-12-29 13:51:45 +01002###
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 =
Jiri Olsaceed2522015-01-11 23:59:55 +010017 Q =
Jiri Olsac819e2c2014-12-29 13:51:45 +010018else
19 quiet=quiet_
Jiri Olsaceed2522015-01-11 23:59:55 +010020 Q=@
Jiri Olsac819e2c2014-12-29 13:51:45 +010021endif
22
Masahiro Yamada6f0fa582017-05-19 20:42:30 +090023ifneq ($(findstring s,$(filter-out --%,$(MAKEFLAGS))),)
Josh Poimboeufe572d082017-01-18 22:16:55 -060024 quiet=silent_
25endif
Josh Poimboeufe572d082017-01-18 22:16:55 -060026
Jiri Olsac819e2c2014-12-29 13:51:45 +010027build-dir := $(srctree)/tools/build
28
Jiri Olsa9fb813232015-09-23 12:34:00 +020029# Define $(fixdep) for dep-cmd function
30ifeq ($(OUTPUT),)
31 fixdep := $(build-dir)/fixdep
32else
33 fixdep := $(OUTPUT)/fixdep
34endif
35
Jiri Olsac819e2c2014-12-29 13:51:45 +010036# Generic definitions
37include $(build-dir)/Build.include
38
Jiri Olsafcfd6612014-12-31 17:37:00 +010039# do not force detected configuration
Aaro Koskinen64227372015-07-01 14:54:42 +030040-include $(OUTPUT).config-detected
Jiri Olsafcfd6612014-12-31 17:37:00 +010041
Jiri Olsac819e2c2014-12-29 13:51:45 +010042# 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
Jiri Olsad7a3d852015-05-29 17:42:58 +020052-include $(build-file)
Jiri Olsac819e2c2014-12-29 13:51:45 +010053
Jiri Olsa9352aab2014-12-29 17:42:46 +010054quiet_cmd_flex = FLEX $@
55quiet_cmd_bison = BISON $@
56
Jiri Olsa579ff6d2014-12-30 16:44:11 +010057# Create directory unless it exists
58quiet_cmd_mkdir = MKDIR $(dir $@)
59 cmd_mkdir = mkdir -p $(dir $@)
60 rule_mkdir = $(if $(wildcard $(dir $@)),,@$(call echo-cmd,mkdir) $(cmd_mkdir))
61
Jiri Olsac819e2c2014-12-29 13:51:45 +010062# Compile command
63quiet_cmd_cc_o_c = CC $@
64 cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $<
65
Jiri Olsa0c3b7e42016-09-27 16:18:46 +020066quiet_cmd_host_cc_o_c = HOSTCC $@
67 cmd_host_cc_o_c = $(HOSTCC) $(host_c_flags) -c -o $@ $<
68
Wang Nanf61bdc32016-09-26 07:26:55 +000069quiet_cmd_cxx_o_c = CXX $@
70 cmd_cxx_o_c = $(CXX) $(cxx_flags) -c -o $@ $<
71
Masahiro Yamada23d43842016-04-08 11:24:47 +090072quiet_cmd_cpp_i_c = CPP $@
73 cmd_cpp_i_c = $(CC) $(c_flags) -E -o $@ $<
Jiri Olsa19993072014-12-30 18:44:38 +010074
75quiet_cmd_cc_s_c = AS $@
76 cmd_cc_s_c = $(CC) $(c_flags) -S -o $@ $<
77
Adrian Hunter237fae72015-08-13 10:14:55 +030078quiet_cmd_gen = GEN $@
79
Jiri Olsac819e2c2014-12-29 13:51:45 +010080# Link agregate command
81# If there's nothing to link, create empty $@ object.
82quiet_cmd_ld_multi = LD $@
83 cmd_ld_multi = $(if $(strip $(obj-y)),\
Jiri Olsa0c3b7e42016-09-27 16:18:46 +020084 $(LD) -r -o $@ $(filter $(obj-y),$^),rm -f $@; $(AR) rcs $@)
85
86quiet_cmd_host_ld_multi = HOSTLD $@
87 cmd_host_ld_multi = $(if $(strip $(obj-y)),\
88 $(HOSTLD) -r -o $@ $(filter $(obj-y),$^),rm -f $@; $(HOSTAR) rcs $@)
89
90ifneq ($(filter $(obj),$(hostprogs)),)
91 host = host_
92endif
Jiri Olsac819e2c2014-12-29 13:51:45 +010093
94# Build rules
95$(OUTPUT)%.o: %.c FORCE
Jiri Olsa579ff6d2014-12-30 16:44:11 +010096 $(call rule_mkdir)
Jiri Olsa0c3b7e42016-09-27 16:18:46 +020097 $(call if_changed_dep,$(host)cc_o_c)
Jiri Olsac819e2c2014-12-29 13:51:45 +010098
Wang Nanf61bdc32016-09-26 07:26:55 +000099$(OUTPUT)%.o: %.cpp FORCE
100 $(call rule_mkdir)
101 $(call if_changed_dep,cxx_o_c)
102
Jiri Olsac819e2c2014-12-29 13:51:45 +0100103$(OUTPUT)%.o: %.S FORCE
Jiri Olsa579ff6d2014-12-30 16:44:11 +0100104 $(call rule_mkdir)
Jiri Olsa0c3b7e42016-09-27 16:18:46 +0200105 $(call if_changed_dep,$(host)cc_o_c)
Jiri Olsac819e2c2014-12-29 13:51:45 +0100106
Jiri Olsa19993072014-12-30 18:44:38 +0100107$(OUTPUT)%.i: %.c FORCE
108 $(call rule_mkdir)
Masahiro Yamada23d43842016-04-08 11:24:47 +0900109 $(call if_changed_dep,cpp_i_c)
Jiri Olsa19993072014-12-30 18:44:38 +0100110
Masahiro Yamada67678792016-02-01 02:59:00 +0900111$(OUTPUT)%.s: %.S FORCE
Jiri Olsa19993072014-12-30 18:44:38 +0100112 $(call rule_mkdir)
Masahiro Yamada23d43842016-04-08 11:24:47 +0900113 $(call if_changed_dep,cpp_i_c)
Jiri Olsa19993072014-12-30 18:44:38 +0100114
115$(OUTPUT)%.s: %.c FORCE
116 $(call rule_mkdir)
117 $(call if_changed_dep,cc_s_c)
118
Jiri Olsac819e2c2014-12-29 13:51:45 +0100119# Gather build data:
120# obj-y - list of build objects
121# subdir-y - list of directories to nest
122# subdir-obj-y - list of directories objects 'dir/$(obj)-in.o'
123obj-y := $($(obj)-y)
124subdir-y := $(patsubst %/,%,$(filter %/, $(obj-y)))
125obj-y := $(patsubst %/, %/$(obj)-in.o, $(obj-y))
126subdir-obj-y := $(filter %/$(obj)-in.o, $(obj-y))
127
128# '$(OUTPUT)/dir' prefix to all objects
Lukas Wunner75e84ab2015-06-18 13:00:32 +0200129objprefix := $(subst ./,,$(OUTPUT)$(dir)/)
130obj-y := $(addprefix $(objprefix),$(obj-y))
131subdir-obj-y := $(addprefix $(objprefix),$(subdir-obj-y))
Jiri Olsac819e2c2014-12-29 13:51:45 +0100132
133# Final '$(obj)-in.o' object
Lukas Wunner75e84ab2015-06-18 13:00:32 +0200134in-target := $(objprefix)$(obj)-in.o
Jiri Olsac819e2c2014-12-29 13:51:45 +0100135
136PHONY += $(subdir-y)
137
138$(subdir-y):
Jiri Olsaceed2522015-01-11 23:59:55 +0100139 $(Q)$(MAKE) -f $(build-dir)/Makefile.build dir=$(dir)/$@ obj=$(obj)
Jiri Olsac819e2c2014-12-29 13:51:45 +0100140
141$(sort $(subdir-obj-y)): $(subdir-y) ;
142
143$(in-target): $(obj-y) FORCE
144 $(call rule_mkdir)
Jiri Olsa0c3b7e42016-09-27 16:18:46 +0200145 $(call if_changed,$(host)ld_multi)
Jiri Olsac819e2c2014-12-29 13:51:45 +0100146
147__build: $(in-target)
148 @:
149
150PHONY += FORCE
151FORCE:
152
153# Include all cmd files to get all the dependency rules
154# for all objects included
Jiri Olsa19993072014-12-30 18:44:38 +0100155targets := $(wildcard $(sort $(obj-y) $(in-target) $(MAKECMDGOALS)))
Jiri Olsac819e2c2014-12-29 13:51:45 +0100156cmd_files := $(wildcard $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd))
157
158ifneq ($(cmd_files),)
159 include $(cmd_files)
160endif
161
162.PHONY: $(PHONY)