blob: 69c35cf09cad25d7a711388af6e1ca449cfffe77 [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
28-include .config-detected
29
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
Jiri Olsac819e2c2014-12-29 13:51:45 +010060# Link agregate command
61# If there's nothing to link, create empty $@ object.
62quiet_cmd_ld_multi = LD $@
63 cmd_ld_multi = $(if $(strip $(obj-y)),\
64 $(LD) -r -o $@ $(obj-y),rm -f $@; $(AR) rcs $@)
65
66# Build rules
67$(OUTPUT)%.o: %.c FORCE
Jiri Olsa579ff6d2014-12-30 16:44:11 +010068 $(call rule_mkdir)
Jiri Olsac819e2c2014-12-29 13:51:45 +010069 $(call if_changed_dep,cc_o_c)
70
71$(OUTPUT)%.o: %.S FORCE
Jiri Olsa579ff6d2014-12-30 16:44:11 +010072 $(call rule_mkdir)
Jiri Olsac819e2c2014-12-29 13:51:45 +010073 $(call if_changed_dep,cc_o_c)
74
Jiri Olsa19993072014-12-30 18:44:38 +010075$(OUTPUT)%.i: %.c FORCE
76 $(call rule_mkdir)
77 $(call if_changed_dep,cc_i_c)
78
79$(OUTPUT)%.i: %.S FORCE
80 $(call rule_mkdir)
81 $(call if_changed_dep,cc_i_c)
82
83$(OUTPUT)%.s: %.c FORCE
84 $(call rule_mkdir)
85 $(call if_changed_dep,cc_s_c)
86
Jiri Olsac819e2c2014-12-29 13:51:45 +010087# Gather build data:
88# obj-y - list of build objects
89# subdir-y - list of directories to nest
90# subdir-obj-y - list of directories objects 'dir/$(obj)-in.o'
91obj-y := $($(obj)-y)
92subdir-y := $(patsubst %/,%,$(filter %/, $(obj-y)))
93obj-y := $(patsubst %/, %/$(obj)-in.o, $(obj-y))
94subdir-obj-y := $(filter %/$(obj)-in.o, $(obj-y))
95
96# '$(OUTPUT)/dir' prefix to all objects
97prefix := $(subst ./,,$(OUTPUT)$(dir)/)
98obj-y := $(addprefix $(prefix),$(obj-y))
99subdir-obj-y := $(addprefix $(prefix),$(subdir-obj-y))
100
101# Final '$(obj)-in.o' object
102in-target := $(prefix)$(obj)-in.o
103
104PHONY += $(subdir-y)
105
106$(subdir-y):
Jiri Olsaceed2522015-01-11 23:59:55 +0100107 $(Q)$(MAKE) -f $(build-dir)/Makefile.build dir=$(dir)/$@ obj=$(obj)
Jiri Olsac819e2c2014-12-29 13:51:45 +0100108
109$(sort $(subdir-obj-y)): $(subdir-y) ;
110
111$(in-target): $(obj-y) FORCE
112 $(call rule_mkdir)
113 $(call if_changed,ld_multi)
114
115__build: $(in-target)
116 @:
117
118PHONY += FORCE
119FORCE:
120
121# Include all cmd files to get all the dependency rules
122# for all objects included
Jiri Olsa19993072014-12-30 18:44:38 +0100123targets := $(wildcard $(sort $(obj-y) $(in-target) $(MAKECMDGOALS)))
Jiri Olsac819e2c2014-12-29 13:51:45 +0100124cmd_files := $(wildcard $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd))
125
126ifneq ($(cmd_files),)
127 include $(cmd_files)
128endif
129
130.PHONY: $(PHONY)