blob: 99c0ccd2f176592c03140d8e92671ba78aeac8e0 [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
22build-dir := $(srctree)/tools/build
23
Jiri Olsa9fb813232015-09-23 12:34:00 +020024# Define $(fixdep) for dep-cmd function
25ifeq ($(OUTPUT),)
26 fixdep := $(build-dir)/fixdep
27else
28 fixdep := $(OUTPUT)/fixdep
29endif
30
Jiri Olsac819e2c2014-12-29 13:51:45 +010031# Generic definitions
32include $(build-dir)/Build.include
33
Jiri Olsafcfd6612014-12-31 17:37:00 +010034# do not force detected configuration
Aaro Koskinen64227372015-07-01 14:54:42 +030035-include $(OUTPUT).config-detected
Jiri Olsafcfd6612014-12-31 17:37:00 +010036
Jiri Olsac819e2c2014-12-29 13:51:45 +010037# Init all relevant variables used in build files so
38# 1) they have correct type
39# 2) they do not inherit any value from the environment
40subdir-y :=
41obj-y :=
42subdir-y :=
43subdir-obj-y :=
44
45# Build definitions
46build-file := $(dir)/Build
Jiri Olsad7a3d852015-05-29 17:42:58 +020047-include $(build-file)
Jiri Olsac819e2c2014-12-29 13:51:45 +010048
Jiri Olsa9352aab2014-12-29 17:42:46 +010049quiet_cmd_flex = FLEX $@
50quiet_cmd_bison = BISON $@
51
Jiri Olsa579ff6d2014-12-30 16:44:11 +010052# Create directory unless it exists
53quiet_cmd_mkdir = MKDIR $(dir $@)
54 cmd_mkdir = mkdir -p $(dir $@)
55 rule_mkdir = $(if $(wildcard $(dir $@)),,@$(call echo-cmd,mkdir) $(cmd_mkdir))
56
Jiri Olsac819e2c2014-12-29 13:51:45 +010057# Compile command
58quiet_cmd_cc_o_c = CC $@
59 cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $<
60
Jiri Olsa0c3b7e42016-09-27 16:18:46 +020061quiet_cmd_host_cc_o_c = HOSTCC $@
62 cmd_host_cc_o_c = $(HOSTCC) $(host_c_flags) -c -o $@ $<
63
Wang Nanf61bdc32016-09-26 07:26:55 +000064quiet_cmd_cxx_o_c = CXX $@
65 cmd_cxx_o_c = $(CXX) $(cxx_flags) -c -o $@ $<
66
Masahiro Yamada23d43842016-04-08 11:24:47 +090067quiet_cmd_cpp_i_c = CPP $@
68 cmd_cpp_i_c = $(CC) $(c_flags) -E -o $@ $<
Jiri Olsa19993072014-12-30 18:44:38 +010069
70quiet_cmd_cc_s_c = AS $@
71 cmd_cc_s_c = $(CC) $(c_flags) -S -o $@ $<
72
Adrian Hunter237fae72015-08-13 10:14:55 +030073quiet_cmd_gen = GEN $@
74
Jiri Olsac819e2c2014-12-29 13:51:45 +010075# Link agregate command
76# If there's nothing to link, create empty $@ object.
77quiet_cmd_ld_multi = LD $@
78 cmd_ld_multi = $(if $(strip $(obj-y)),\
Jiri Olsa0c3b7e42016-09-27 16:18:46 +020079 $(LD) -r -o $@ $(filter $(obj-y),$^),rm -f $@; $(AR) rcs $@)
80
81quiet_cmd_host_ld_multi = HOSTLD $@
82 cmd_host_ld_multi = $(if $(strip $(obj-y)),\
83 $(HOSTLD) -r -o $@ $(filter $(obj-y),$^),rm -f $@; $(HOSTAR) rcs $@)
84
85ifneq ($(filter $(obj),$(hostprogs)),)
86 host = host_
87endif
Jiri Olsac819e2c2014-12-29 13:51:45 +010088
89# Build rules
90$(OUTPUT)%.o: %.c FORCE
Jiri Olsa579ff6d2014-12-30 16:44:11 +010091 $(call rule_mkdir)
Jiri Olsa0c3b7e42016-09-27 16:18:46 +020092 $(call if_changed_dep,$(host)cc_o_c)
Jiri Olsac819e2c2014-12-29 13:51:45 +010093
Wang Nanf61bdc32016-09-26 07:26:55 +000094$(OUTPUT)%.o: %.cpp FORCE
95 $(call rule_mkdir)
96 $(call if_changed_dep,cxx_o_c)
97
Jiri Olsac819e2c2014-12-29 13:51:45 +010098$(OUTPUT)%.o: %.S FORCE
Jiri Olsa579ff6d2014-12-30 16:44:11 +010099 $(call rule_mkdir)
Jiri Olsa0c3b7e42016-09-27 16:18:46 +0200100 $(call if_changed_dep,$(host)cc_o_c)
Jiri Olsac819e2c2014-12-29 13:51:45 +0100101
Jiri Olsa19993072014-12-30 18:44:38 +0100102$(OUTPUT)%.i: %.c FORCE
103 $(call rule_mkdir)
Masahiro Yamada23d43842016-04-08 11:24:47 +0900104 $(call if_changed_dep,cpp_i_c)
Jiri Olsa19993072014-12-30 18:44:38 +0100105
Masahiro Yamada67678792016-02-01 02:59:00 +0900106$(OUTPUT)%.s: %.S FORCE
Jiri Olsa19993072014-12-30 18:44:38 +0100107 $(call rule_mkdir)
Masahiro Yamada23d43842016-04-08 11:24:47 +0900108 $(call if_changed_dep,cpp_i_c)
Jiri Olsa19993072014-12-30 18:44:38 +0100109
110$(OUTPUT)%.s: %.c FORCE
111 $(call rule_mkdir)
112 $(call if_changed_dep,cc_s_c)
113
Jiri Olsac819e2c2014-12-29 13:51:45 +0100114# Gather build data:
115# obj-y - list of build objects
116# subdir-y - list of directories to nest
117# subdir-obj-y - list of directories objects 'dir/$(obj)-in.o'
118obj-y := $($(obj)-y)
119subdir-y := $(patsubst %/,%,$(filter %/, $(obj-y)))
120obj-y := $(patsubst %/, %/$(obj)-in.o, $(obj-y))
121subdir-obj-y := $(filter %/$(obj)-in.o, $(obj-y))
122
123# '$(OUTPUT)/dir' prefix to all objects
Lukas Wunner75e84ab2015-06-18 13:00:32 +0200124objprefix := $(subst ./,,$(OUTPUT)$(dir)/)
125obj-y := $(addprefix $(objprefix),$(obj-y))
126subdir-obj-y := $(addprefix $(objprefix),$(subdir-obj-y))
Jiri Olsac819e2c2014-12-29 13:51:45 +0100127
128# Final '$(obj)-in.o' object
Lukas Wunner75e84ab2015-06-18 13:00:32 +0200129in-target := $(objprefix)$(obj)-in.o
Jiri Olsac819e2c2014-12-29 13:51:45 +0100130
131PHONY += $(subdir-y)
132
133$(subdir-y):
Jiri Olsaceed2522015-01-11 23:59:55 +0100134 $(Q)$(MAKE) -f $(build-dir)/Makefile.build dir=$(dir)/$@ obj=$(obj)
Jiri Olsac819e2c2014-12-29 13:51:45 +0100135
136$(sort $(subdir-obj-y)): $(subdir-y) ;
137
138$(in-target): $(obj-y) FORCE
139 $(call rule_mkdir)
Jiri Olsa0c3b7e42016-09-27 16:18:46 +0200140 $(call if_changed,$(host)ld_multi)
Jiri Olsac819e2c2014-12-29 13:51:45 +0100141
142__build: $(in-target)
143 @:
144
145PHONY += FORCE
146FORCE:
147
148# Include all cmd files to get all the dependency rules
149# for all objects included
Jiri Olsa19993072014-12-30 18:44:38 +0100150targets := $(wildcard $(sort $(obj-y) $(in-target) $(MAKECMDGOALS)))
Jiri Olsac819e2c2014-12-29 13:51:45 +0100151cmd_files := $(wildcard $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd))
152
153ifneq ($(cmd_files),)
154 include $(cmd_files)
155endif
156
157.PHONY: $(PHONY)