blob: b5ded207f49b5e24212871e758371652ef10bc0b [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 =
16else
17 quiet=quiet_
18endif
19
20build-dir := $(srctree)/tools/build
21
22# Generic definitions
23include $(build-dir)/Build.include
24
Jiri Olsafcfd6612014-12-31 17:37:00 +010025# do not force detected configuration
26-include .config-detected
27
Jiri Olsac819e2c2014-12-29 13:51:45 +010028# Init all relevant variables used in build files so
29# 1) they have correct type
30# 2) they do not inherit any value from the environment
31subdir-y :=
32obj-y :=
33subdir-y :=
34subdir-obj-y :=
35
36# Build definitions
37build-file := $(dir)/Build
38include $(build-file)
39
Jiri Olsa9352aab2014-12-29 17:42:46 +010040quiet_cmd_flex = FLEX $@
41quiet_cmd_bison = BISON $@
42
Jiri Olsa579ff6d2014-12-30 16:44:11 +010043# Create directory unless it exists
44quiet_cmd_mkdir = MKDIR $(dir $@)
45 cmd_mkdir = mkdir -p $(dir $@)
46 rule_mkdir = $(if $(wildcard $(dir $@)),,@$(call echo-cmd,mkdir) $(cmd_mkdir))
47
Jiri Olsac819e2c2014-12-29 13:51:45 +010048# Compile command
49quiet_cmd_cc_o_c = CC $@
50 cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $<
51
Jiri Olsa19993072014-12-30 18:44:38 +010052quiet_cmd_cc_i_c = CPP $@
53 cmd_cc_i_c = $(CC) $(c_flags) -E -o $@ $<
54
55quiet_cmd_cc_s_c = AS $@
56 cmd_cc_s_c = $(CC) $(c_flags) -S -o $@ $<
57
Jiri Olsac819e2c2014-12-29 13:51:45 +010058# Link agregate command
59# If there's nothing to link, create empty $@ object.
60quiet_cmd_ld_multi = LD $@
61 cmd_ld_multi = $(if $(strip $(obj-y)),\
62 $(LD) -r -o $@ $(obj-y),rm -f $@; $(AR) rcs $@)
63
64# Build rules
65$(OUTPUT)%.o: %.c FORCE
Jiri Olsa579ff6d2014-12-30 16:44:11 +010066 $(call rule_mkdir)
Jiri Olsac819e2c2014-12-29 13:51:45 +010067 $(call if_changed_dep,cc_o_c)
68
69$(OUTPUT)%.o: %.S 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
Jiri Olsa19993072014-12-30 18:44:38 +010073$(OUTPUT)%.i: %.c FORCE
74 $(call rule_mkdir)
75 $(call if_changed_dep,cc_i_c)
76
77$(OUTPUT)%.i: %.S FORCE
78 $(call rule_mkdir)
79 $(call if_changed_dep,cc_i_c)
80
81$(OUTPUT)%.s: %.c FORCE
82 $(call rule_mkdir)
83 $(call if_changed_dep,cc_s_c)
84
Jiri Olsac819e2c2014-12-29 13:51:45 +010085# Gather build data:
86# obj-y - list of build objects
87# subdir-y - list of directories to nest
88# subdir-obj-y - list of directories objects 'dir/$(obj)-in.o'
89obj-y := $($(obj)-y)
90subdir-y := $(patsubst %/,%,$(filter %/, $(obj-y)))
91obj-y := $(patsubst %/, %/$(obj)-in.o, $(obj-y))
92subdir-obj-y := $(filter %/$(obj)-in.o, $(obj-y))
93
94# '$(OUTPUT)/dir' prefix to all objects
95prefix := $(subst ./,,$(OUTPUT)$(dir)/)
96obj-y := $(addprefix $(prefix),$(obj-y))
97subdir-obj-y := $(addprefix $(prefix),$(subdir-obj-y))
98
99# Final '$(obj)-in.o' object
100in-target := $(prefix)$(obj)-in.o
101
102PHONY += $(subdir-y)
103
104$(subdir-y):
105 @$(MAKE) -f $(build-dir)/Makefile.build dir=$(dir)/$@ obj=$(obj)
106
107$(sort $(subdir-obj-y)): $(subdir-y) ;
108
109$(in-target): $(obj-y) FORCE
110 $(call rule_mkdir)
111 $(call if_changed,ld_multi)
112
113__build: $(in-target)
114 @:
115
116PHONY += FORCE
117FORCE:
118
119# Include all cmd files to get all the dependency rules
120# for all objects included
Jiri Olsa19993072014-12-30 18:44:38 +0100121targets := $(wildcard $(sort $(obj-y) $(in-target) $(MAKECMDGOALS)))
Jiri Olsac819e2c2014-12-29 13:51:45 +0100122cmd_files := $(wildcard $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd))
123
124ifneq ($(cmd_files),)
125 include $(cmd_files)
126endif
127
128.PHONY: $(PHONY)