Commit | Line | Data |
---|---|---|
c819e2cf JO |
1 | ### |
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 | ||
11 | PHONY := __build | |
12 | __build: | |
13 | ||
14 | ifeq ($(V),1) | |
15 | quiet = | |
16 | else | |
17 | quiet=quiet_ | |
18 | endif | |
19 | ||
20 | build-dir := $(srctree)/tools/build | |
21 | ||
22 | # Generic definitions | |
23 | include $(build-dir)/Build.include | |
24 | ||
25 | # Init all relevant variables used in build files so | |
26 | # 1) they have correct type | |
27 | # 2) they do not inherit any value from the environment | |
28 | subdir-y := | |
29 | obj-y := | |
30 | subdir-y := | |
31 | subdir-obj-y := | |
32 | ||
33 | # Build definitions | |
34 | build-file := $(dir)/Build | |
35 | include $(build-file) | |
36 | ||
37 | # Compile command | |
38 | quiet_cmd_cc_o_c = CC $@ | |
39 | cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $< | |
40 | ||
41 | # Link agregate command | |
42 | # If there's nothing to link, create empty $@ object. | |
43 | quiet_cmd_ld_multi = LD $@ | |
44 | cmd_ld_multi = $(if $(strip $(obj-y)),\ | |
45 | $(LD) -r -o $@ $(obj-y),rm -f $@; $(AR) rcs $@) | |
46 | ||
47 | # Build rules | |
48 | $(OUTPUT)%.o: %.c FORCE | |
49 | $(call if_changed_dep,cc_o_c) | |
50 | ||
51 | $(OUTPUT)%.o: %.S FORCE | |
52 | $(call if_changed_dep,cc_o_c) | |
53 | ||
54 | # Gather build data: | |
55 | # obj-y - list of build objects | |
56 | # subdir-y - list of directories to nest | |
57 | # subdir-obj-y - list of directories objects 'dir/$(obj)-in.o' | |
58 | obj-y := $($(obj)-y) | |
59 | subdir-y := $(patsubst %/,%,$(filter %/, $(obj-y))) | |
60 | obj-y := $(patsubst %/, %/$(obj)-in.o, $(obj-y)) | |
61 | subdir-obj-y := $(filter %/$(obj)-in.o, $(obj-y)) | |
62 | ||
63 | # '$(OUTPUT)/dir' prefix to all objects | |
64 | prefix := $(subst ./,,$(OUTPUT)$(dir)/) | |
65 | obj-y := $(addprefix $(prefix),$(obj-y)) | |
66 | subdir-obj-y := $(addprefix $(prefix),$(subdir-obj-y)) | |
67 | ||
68 | # Final '$(obj)-in.o' object | |
69 | in-target := $(prefix)$(obj)-in.o | |
70 | ||
71 | PHONY += $(subdir-y) | |
72 | ||
73 | $(subdir-y): | |
74 | @$(MAKE) -f $(build-dir)/Makefile.build dir=$(dir)/$@ obj=$(obj) | |
75 | ||
76 | $(sort $(subdir-obj-y)): $(subdir-y) ; | |
77 | ||
78 | $(in-target): $(obj-y) FORCE | |
79 | $(call rule_mkdir) | |
80 | $(call if_changed,ld_multi) | |
81 | ||
82 | __build: $(in-target) | |
83 | @: | |
84 | ||
85 | PHONY += FORCE | |
86 | FORCE: | |
87 | ||
88 | # Include all cmd files to get all the dependency rules | |
89 | # for all objects included | |
90 | targets := $(wildcard $(sort $(obj-y) $(in-target))) | |
91 | cmd_files := $(wildcard $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd)) | |
92 | ||
93 | ifneq ($(cmd_files),) | |
94 | include $(cmd_files) | |
95 | endif | |
96 | ||
97 | .PHONY: $(PHONY) |