blob: ee566e8bd1cff56efde9200bf4a76caa840669f1 [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 Olsa19993072014-12-30 18:44:38 +010061quiet_cmd_cc_i_c = CPP $@
62 cmd_cc_i_c = $(CC) $(c_flags) -E -o $@ $<
63
64quiet_cmd_cc_s_c = AS $@
65 cmd_cc_s_c = $(CC) $(c_flags) -S -o $@ $<
66
Adrian Hunter237fae72015-08-13 10:14:55 +030067quiet_cmd_gen = GEN $@
68
Jiri Olsac819e2c2014-12-29 13:51:45 +010069# Link agregate command
70# If there's nothing to link, create empty $@ object.
71quiet_cmd_ld_multi = LD $@
72 cmd_ld_multi = $(if $(strip $(obj-y)),\
Jiri Olsa0bdede82015-08-26 15:01:03 +020073 $(LD) -r -o $@ $(filter $(obj-y),$^),rm -f $@; $(AR) rcs $@)
Jiri Olsac819e2c2014-12-29 13:51:45 +010074
75# Build rules
76$(OUTPUT)%.o: %.c FORCE
Jiri Olsa579ff6d2014-12-30 16:44:11 +010077 $(call rule_mkdir)
Jiri Olsac819e2c2014-12-29 13:51:45 +010078 $(call if_changed_dep,cc_o_c)
79
80$(OUTPUT)%.o: %.S FORCE
Jiri Olsa579ff6d2014-12-30 16:44:11 +010081 $(call rule_mkdir)
Jiri Olsac819e2c2014-12-29 13:51:45 +010082 $(call if_changed_dep,cc_o_c)
83
Jiri Olsa19993072014-12-30 18:44:38 +010084$(OUTPUT)%.i: %.c FORCE
85 $(call rule_mkdir)
86 $(call if_changed_dep,cc_i_c)
87
Masahiro Yamada67678792016-02-01 02:59:00 +090088$(OUTPUT)%.s: %.S FORCE
Jiri Olsa19993072014-12-30 18:44:38 +010089 $(call rule_mkdir)
90 $(call if_changed_dep,cc_i_c)
91
92$(OUTPUT)%.s: %.c FORCE
93 $(call rule_mkdir)
94 $(call if_changed_dep,cc_s_c)
95
Jiri Olsac819e2c2014-12-29 13:51:45 +010096# Gather build data:
97# obj-y - list of build objects
98# subdir-y - list of directories to nest
99# subdir-obj-y - list of directories objects 'dir/$(obj)-in.o'
100obj-y := $($(obj)-y)
101subdir-y := $(patsubst %/,%,$(filter %/, $(obj-y)))
102obj-y := $(patsubst %/, %/$(obj)-in.o, $(obj-y))
103subdir-obj-y := $(filter %/$(obj)-in.o, $(obj-y))
104
105# '$(OUTPUT)/dir' prefix to all objects
Lukas Wunner75e84ab2015-06-18 13:00:32 +0200106objprefix := $(subst ./,,$(OUTPUT)$(dir)/)
107obj-y := $(addprefix $(objprefix),$(obj-y))
108subdir-obj-y := $(addprefix $(objprefix),$(subdir-obj-y))
Jiri Olsac819e2c2014-12-29 13:51:45 +0100109
110# Final '$(obj)-in.o' object
Lukas Wunner75e84ab2015-06-18 13:00:32 +0200111in-target := $(objprefix)$(obj)-in.o
Jiri Olsac819e2c2014-12-29 13:51:45 +0100112
113PHONY += $(subdir-y)
114
115$(subdir-y):
Jiri Olsaceed2522015-01-11 23:59:55 +0100116 $(Q)$(MAKE) -f $(build-dir)/Makefile.build dir=$(dir)/$@ obj=$(obj)
Jiri Olsac819e2c2014-12-29 13:51:45 +0100117
118$(sort $(subdir-obj-y)): $(subdir-y) ;
119
120$(in-target): $(obj-y) FORCE
121 $(call rule_mkdir)
122 $(call if_changed,ld_multi)
123
124__build: $(in-target)
125 @:
126
127PHONY += FORCE
128FORCE:
129
130# Include all cmd files to get all the dependency rules
131# for all objects included
Jiri Olsa19993072014-12-30 18:44:38 +0100132targets := $(wildcard $(sort $(obj-y) $(in-target) $(MAKECMDGOALS)))
Jiri Olsac819e2c2014-12-29 13:51:45 +0100133cmd_files := $(wildcard $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd))
134
135ifneq ($(cmd_files),)
136 include $(cmd_files)
137endif
138
139.PHONY: $(PHONY)