blob: 692e1b1542000fe3542e8bcd8e49effa563575a3 [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 Olsa579ff6d2014-12-30 16:44:11 +010040# Create directory unless it exists
41quiet_cmd_mkdir = MKDIR $(dir $@)
42 cmd_mkdir = mkdir -p $(dir $@)
43 rule_mkdir = $(if $(wildcard $(dir $@)),,@$(call echo-cmd,mkdir) $(cmd_mkdir))
44
Jiri Olsac819e2c2014-12-29 13:51:45 +010045# Compile command
46quiet_cmd_cc_o_c = CC $@
47 cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $<
48
49# Link agregate command
50# If there's nothing to link, create empty $@ object.
51quiet_cmd_ld_multi = LD $@
52 cmd_ld_multi = $(if $(strip $(obj-y)),\
53 $(LD) -r -o $@ $(obj-y),rm -f $@; $(AR) rcs $@)
54
55# Build rules
56$(OUTPUT)%.o: %.c FORCE
Jiri Olsa579ff6d2014-12-30 16:44:11 +010057 $(call rule_mkdir)
Jiri Olsac819e2c2014-12-29 13:51:45 +010058 $(call if_changed_dep,cc_o_c)
59
60$(OUTPUT)%.o: %.S FORCE
Jiri Olsa579ff6d2014-12-30 16:44:11 +010061 $(call rule_mkdir)
Jiri Olsac819e2c2014-12-29 13:51:45 +010062 $(call if_changed_dep,cc_o_c)
63
64# Gather build data:
65# obj-y - list of build objects
66# subdir-y - list of directories to nest
67# subdir-obj-y - list of directories objects 'dir/$(obj)-in.o'
68obj-y := $($(obj)-y)
69subdir-y := $(patsubst %/,%,$(filter %/, $(obj-y)))
70obj-y := $(patsubst %/, %/$(obj)-in.o, $(obj-y))
71subdir-obj-y := $(filter %/$(obj)-in.o, $(obj-y))
72
73# '$(OUTPUT)/dir' prefix to all objects
74prefix := $(subst ./,,$(OUTPUT)$(dir)/)
75obj-y := $(addprefix $(prefix),$(obj-y))
76subdir-obj-y := $(addprefix $(prefix),$(subdir-obj-y))
77
78# Final '$(obj)-in.o' object
79in-target := $(prefix)$(obj)-in.o
80
81PHONY += $(subdir-y)
82
83$(subdir-y):
84 @$(MAKE) -f $(build-dir)/Makefile.build dir=$(dir)/$@ obj=$(obj)
85
86$(sort $(subdir-obj-y)): $(subdir-y) ;
87
88$(in-target): $(obj-y) FORCE
89 $(call rule_mkdir)
90 $(call if_changed,ld_multi)
91
92__build: $(in-target)
93 @:
94
95PHONY += FORCE
96FORCE:
97
98# Include all cmd files to get all the dependency rules
99# for all objects included
100targets := $(wildcard $(sort $(obj-y) $(in-target)))
101cmd_files := $(wildcard $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd))
102
103ifneq ($(cmd_files),)
104 include $(cmd_files)
105endif
106
107.PHONY: $(PHONY)