summaryrefslogtreecommitdiff
path: root/scripts/makefile.build
blob: 6467d2ad211f4822a0434716375289fbb18e92df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
obj-y    :=
obj-m    :=
always   :=
targets  :=
.DEFAULT_GOAL := all

src := $(patsubst %/,%,$(obj))

-include $(srctree)/.config
-include $(obj)Kbuild

ifdef lib-y
  export cflags-y
endif

ifdef bin-y
  export cflags-y
  export ldflags-y
endif

objects := $(filter %.o,$(obj-y))
subdirs := $(filter %/,$(obj-y))

cflags := $(cflags-y) $(KBUILD_CFLAGS) -MMD -MP
asflags := $(asflags-y) $(KBUILD_ASFLAGS)
ldflags := $(ldflags-y) $(KBUILD_LDFLAGS)

collect-obj-y = $(foreach e,$(shell echo 'all:; @echo $$(obj-y)' | \
  $(MAKE) -f $(obj)$(1)Kbuild -f - --no-print-directory 2>/dev/null),\
  $(if $(filter %/,$(e)),$(addprefix $(e),$(call collect-obj-y,$(1)$(e))),$(e)))

ifdef lib-y
  subdir-objects := $(foreach d,$(subdirs),$(addprefix $(d),$(call collect-obj-y,$(d))))
  all-objects := $(objects) $(subdir-objects)
else ifdef bin-y
  subdir-objects := $(foreach d,$(subdirs),$(addprefix $(d),$(call collect-obj-y,$(d))))
  all-objects := $(objects) $(subdir-objects)
else
  all-objects := $(objects)
endif

deps := $(all-objects:.o=.d)
-include $(deps)

c-sources := $(wildcard $(addprefix $(obj),$(objects:.o=.c)))
s-sources := $(wildcard $(addprefix $(obj),$(objects:.o=.s)))
subdir-c-sources := $(wildcard $(addprefix $(obj),$(subdir-objects:.o=.c)))
subdir-s-sources := $(wildcard $(addprefix $(obj),$(subdir-objects:.o=.s)))

PHONY := all

ifdef lib-y
all: $(obj)$(lib-y) $(addprefix $(obj),$(always))
$(obj)$(lib-y): $(addprefix $(obj),$(all-objects))
	$(MSG) AR "$@"
	$(Q)$(AR) rcs $@ $^

$(O)/$(install-y): $(obj)$(lib-y)
	mkdir -p $(O)/$(dir $(install-y))
	install -m 0644 $< $(O)/$(install-y)

ifdef install-y
install: $(O)/$(install-y)
else
install:
endif

else ifdef bin-y
all: $(obj)$(bin-y) $(addprefix $(obj),$(always))

$(obj)$(bin-y): $(addprefix $(obj),$(all-objects)) $(libs-y) ${prereq}
	$(MSG) LD "$@"
	$(Q)$(LD) $(ldflags) -o $@ $^

ifdef install-y
$(O)/$(install-y): $(obj)$(bin-y)
	mkdir -p $(O)/$(dir $(install-y))
	install -m 0755 $< $(O)/$(install-y)

install: $(O)/$(install-y)
else
install:
endif

else
all: $(addprefix $(obj),$(all-objects)) $(subdirs) $(addprefix $(obj),$(always))

install: $(subdirs)
endif

PHONY += clean clean-%
clean: $(addprefix clean-,$(subdirs))
ifdef deps
	$(Q)rm -f $(addprefix $(obj),$(deps))
endif
ifdef targets
	$(Q)rm -f $(addprefix $(obj),$(targets))
endif
ifdef all-objects
	$(Q)rm -f $(addprefix $(obj),$(all-objects))
endif
ifdef lib-y
	$(Q)rm -f $(obj)$(lib-y)
endif
ifdef bin-y
	$(Q)rm -f $(obj)$(bin-y)
endif
ifdef always
	$(Q)rm -f $(addprefix $(obj),$(always))
endif

# Build C sources to objects
$(filter $(addprefix $(obj),$(all-objects)),$(c-sources:.c=.o) $(subdir-c-sources:.c=.o)): $(obj)%.o: $(obj)%.c
	$(MSG) CC "$@"
	$(Q)$(CC) $(cflags) -c $< -o $@

# Build assembly sources to objects
$(filter $(addprefix $(obj),$(all-objects)),$(s-sources:.s=.o) $(subdir-s-sources:.s=.o)): $(obj)%.o: $(obj)%.s
	$(MSG) AS "$@"
	$(Q)$(CC) $(asflags) -c $< -o $@

.PHONY: $(subdirs)
$(subdirs):
	$(Q)$(MAKE) -f $(srctree)/scripts/Makefile.build obj="$(obj)$@" ${MAKECMDGOALS}

clean-%:
	$(Q)$(MAKE) -f $(srctree)/scripts/Makefile.build obj="$(obj)$*" clean

.PHONY: $(PHONY)