blob: 0c5f485521d6707786188652cb42a3fb7d23c8e6 [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
24# Generic definitions
25include $(build-dir)/Build.include
26
Jiri Olsafcfd6612014-12-31 17:37:00 +010027# do not force detected configuration
Aaro Koskinen64227372015-07-01 14:54:42 +030028-include $(OUTPUT).config-detected
Jiri Olsafcfd6612014-12-31 17:37:00 +010029
Jiri Olsac819e2c2014-12-29 13:51:45 +010030# Init all relevant variables used in build files so
31# 1) they have correct type
32# 2) they do not inherit any value from the environment
33subdir-y :=
34obj-y :=
35subdir-y :=
36subdir-obj-y :=
37
38# Build definitions
39build-file := $(dir)/Build
Jiri Olsad7a3d852015-05-29 17:42:58 +020040-include $(build-file)
Jiri Olsac819e2c2014-12-29 13:51:45 +010041
Jiri Olsa9352aab2014-12-29 17:42:46 +010042quiet_cmd_flex = FLEX $@
43quiet_cmd_bison = BISON $@
44
Jiri Olsa579ff6d2014-12-30 16:44:11 +010045# Create directory unless it exists
46quiet_cmd_mkdir = MKDIR $(dir $@)
47 cmd_mkdir = mkdir -p $(dir $@)
48 rule_mkdir = $(if $(wildcard $(dir $@)),,@$(call echo-cmd,mkdir) $(cmd_mkdir))
49
Jiri Olsac819e2c2014-12-29 13:51:45 +010050# Compile command
51quiet_cmd_cc_o_c = CC $@
52 cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $<
53
Jiri Olsa19993072014-12-30 18:44:38 +010054quiet_cmd_cc_i_c = CPP $@
55 cmd_cc_i_c = $(CC) $(c_flags) -E -o $@ $<
56
57quiet_cmd_cc_s_c = AS $@
58 cmd_cc_s_c = $(CC) $(c_flags) -S -o $@ $<
59
Adrian Hunter237fae72015-08-13 10:14:55 +030060quiet_cmd_gen = GEN $@
61
Jiri Olsac819e2c2014-12-29 13:51:45 +010062# Link agregate command
63# If there's nothing to link, create empty $@ object.
64quiet_cmd_ld_multi = LD $@
65 cmd_ld_multi = $(if $(strip $(obj-y)),\
Jiri Olsa0bdede82015-08-26 15:01:03 +020066 $(LD) -r -o $@ $(filter $(obj-y),$^),rm -f $@; $(AR) rcs $@)
Jiri Olsac819e2c2014-12-29 13:51:45 +010067
68# Build rules
69$(OUTPUT)%.o: %.c FORCE
Jiri Olsa579ff6d2014-12-30 16:44:11 +010070 $(call rule_mkdir)
Jiri Olsac819e2c2014-12-29 13:51:45 +010071 $(call if_changed_dep,cc_o_c)
72
73$(OUTPUT)%.o: %.S FORCE
Jiri Olsa579ff6d2014-12-30 16:44:11 +010074 $(call rule_mkdir)
Jiri Olsac819e2c2014-12-29 13:51:45 +010075 $(call if_changed_dep,cc_o_c)
76
Jiri Olsa19993072014-12-30 18:44:38 +010077$(OUTPUT)%.i: %.c FORCE
78 $(call rule_mkdir)
79 $(call if_changed_dep,cc_i_c)
80
81$(OUTPUT)%.i: %.S FORCE
82 $(call rule_mkdir)
83 $(call if_changed_dep,cc_i_c)
84
85$(OUTPUT)%.s: %.c FORCE
86 $(call rule_mkdir)
87 $(call if_changed_dep,cc_s_c)
88
Jiri Olsac819e2c2014-12-29 13:51:45 +010089# Gather build data:
90# obj-y - list of build objects
91# subdir-y - list of directories to nest
92# subdir-obj-y - list of directories objects 'dir/$(obj)-in.o'
93obj-y := $($(obj)-y)
94subdir-y := $(patsubst %/,%,$(filter %/, $(obj-y)))
95obj-y := $(patsubst %/, %/$(obj)-in.o, $(obj-y))
96subdir-obj-y := $(filter %/$(obj)-in.o, $(obj-y))
97
98# '$(OUTPUT)/dir' prefix to all objects
Lukas Wunner75e84ab2015-06-18 13:00:32 +020099objprefix := $(subst ./,,$(OUTPUT)$(dir)/)
100obj-y := $(addprefix $(objprefix),$(obj-y))
101subdir-obj-y := $(addprefix $(objprefix),$(subdir-obj-y))
Jiri Olsac819e2c2014-12-29 13:51:45 +0100102
103# Final '$(obj)-in.o' object
Lukas Wunner75e84ab2015-06-18 13:00:32 +0200104in-target := $(objprefix)$(obj)-in.o
Jiri Olsac819e2c2014-12-29 13:51:45 +0100105
106PHONY += $(subdir-y)
107
108$(subdir-y):
Jiri Olsaceed2522015-01-11 23:59:55 +0100109 $(Q)$(MAKE) -f $(build-dir)/Makefile.build dir=$(dir)/$@ obj=$(obj)
Jiri Olsac819e2c2014-12-29 13:51:45 +0100110
111$(sort $(subdir-obj-y)): $(subdir-y) ;
112
113$(in-target): $(obj-y) FORCE
114 $(call rule_mkdir)
115 $(call if_changed,ld_multi)
116
117__build: $(in-target)
118 @:
119
120PHONY += FORCE
121FORCE:
122
123# Include all cmd files to get all the dependency rules
124# for all objects included
Jiri Olsa19993072014-12-30 18:44:38 +0100125targets := $(wildcard $(sort $(obj-y) $(in-target) $(MAKECMDGOALS)))
Jiri Olsac819e2c2014-12-29 13:51:45 +0100126cmd_files := $(wildcard $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd))
127
128ifneq ($(cmd_files),)
129 include $(cmd_files)
130endif
131
132.PHONY: $(PHONY)