-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.mk
235 lines (186 loc) · 7.29 KB
/
build.mk
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
where-am-i = $(realpath $(dir $(lastword $(MAKEFILE_LIST))))
who-am-i = $(basename $(notdir $(lastword $(MAKEFILE_LIST))))
TOP := $(where-am-i)
# Do not use make's built-in rules and variables
# (this increases performance and avoids hard-to-debug behaviour)
MAKEFLAGS += -rR
# Do not print "Entering directory ..." at all for in-tree build.
MAKEFLAGS += --no-print-directory
# Beautify output
# ---------------------------------------------------------------------------
#
# Normally, we echo the whole command before executing it. By making
# that echo $($(quiet)$(cmd)), we now have the possibility to set
# $(quiet) to choose other forms of output instead, e.g.
#
# quiet_cmd_cc_o_c = Compiling $(RELDIR)/$@
# cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $<
#
# If $(quiet) is empty, the whole command will be printed.
# If it is set to "quiet_", only the short version will be printed.
# If it is set to "silent_", nothing will be printed at all, since
# the variable $(silent_cmd_cc_o_c) doesn't exist.
#
# A simple variant is to prefix commands with $(Q) - that's useful
# for commands that shall be hidden in non-verbose mode.
#
# $(Q)ln $@ :<
#
# If KBUILD_VERBOSE equals 0 then the above command will be hidden.
# If KBUILD_VERBOSE equals 1 then the above command is displayed.
#
# To put more focus on warnings, be less verbose as default
# Use 'make V=1' to see the full commands
ifeq ("$(origin V)", "command line")
KBUILD_VERBOSE = $(V)
endif
ifndef KBUILD_VERBOSE
KBUILD_VERBOSE = 0
endif
quiet := $(if $(KBUILD_VERBOSE:1=),quiet_)
Q := $(if $(KBUILD_VERBOSE:1=),@)
# If the user is running make -s (silent mode), suppress echoing of
# commands
ifneq ($(findstring s,$(filter-out --%,$(MAKEFLAGS))),)
quiet=silent_
endif
export quiet Q KBUILD_VERBOSE
# Print a command before executing it.
# Short version is used, if $(quiet) equals `quiet_', otherwise full one.
cmd = @set -e; $(if $($(quiet)cmd_$(1)),echo ' $($(quiet)cmd_$(1))';) $(cmd_$(1))
# Use make M=dir or set the environment variable KBUILD_EXTMOD to specify the
# directory of external module to build. Setting M= takes precedence.
ifeq ("$(origin M)", "command line")
KBUILD_EXTMOD := $(M)
endif
# To append cflags to a particular source file, do this:
# $(eval $(call add-local-cflags,src/foo.c,-I./inc))
define add-local-cflags =
cflags-$(notdir $(1))-y += $(2)
endef
# To include makefiles listed in a variable, do this:
# $(foreach s,$(foo),$(eval $(call include-mkfile,$s)))
define include-mkfile =
include $(1)
endef
# To recursively find all files in a directory:
# $(call find,foo/,*.c)
find = $(strip $(wildcard $1$2) $(foreach d,$(wildcard $1*),$(call find,$d/,$2)))
PHONY := default all clean lib init menuconfig guiconfig
default: all
# menuconfig/guiconfig are installed along with Kconfiglib, which is a Kconfig
# implementation in Python 2/3.
# https://github.com/ulfalizer/Kconfiglib
menuconfig guiconfig:
$(Q)$@ $(TOP)/Kconfig
HOSTARCH := $(shell uname -m | sed -e s/i.86/x86/ -e s/x86_64/x86/ \
-e s/sun4u/sparc64/ \
-e s/arm.*/arm/ -e s/sa110/arm/ \
-e s/s390x/s390/ -e s/parisc64/parisc/ \
-e s/ppc.*/powerpc/ -e s/mips.*/mips/ \
-e s/sh[234].*/sh/ -e s/aarch64.*/arm64/ \
-e s/riscv.*/riscv/)
-include .config
# Strip surrounding quotes of CONFIG_* variables
$(foreach s,$(filter CONFIG_%,$(.VARIABLES)),$(eval $s:=$(patsubst "%",%,$($s))))
-include $(TOP)/$(CONFIG_CHIP)/$(CONFIG_CHIP).mk
-include $(TOP)/external/extmod.mk
$(foreach s,$(extmod-y) $(KBUILD_EXTMOD),$(eval $(call include-mkfile,$(TOP)/external/$s.mk)))
ARCH ?= $(HOSTARCH)
# CROSS_COMPILE specify the prefix used for all executables used
# during compilation. Only gcc and related bin-utils executables
# are prefixed with $(CROSS_COMPILE).
# CROSS_COMPILE can be set on the command line
# make CROSS_COMPILE=ia64-linux-
# Alternatively CROSS_COMPILE can be set in the environment.
# Default value for CROSS_COMPILE is not to prefix executables
# Make variables (CC, etc...)
AS = $(CROSS_COMPILE)as
LD = $(CROSS_COMPILE)ld
CC = $(CROSS_COMPILE)gcc
CPP = $(CC) -E
AR = $(CROSS_COMPILE)ar
NM = $(CROSS_COMPILE)nm
STRIP = $(CROSS_COMPILE)strip
OBJCOPY = $(CROSS_COMPILE)objcopy
OBJDUMP = $(CROSS_COMPILE)objdump
export HOSTARCH ARCH CROSS_COMPILE AS LD CC CPP AR NM STRIP OBJCOPY OBJDUMP
# Variable 'defines-y' defines symbols for all source files compilation.
cflags-y += $(foreach s,$(defines-y),-D$s)
# Variable 'includes-y' lists header search folders for all source files.
cflags-y += -I. $(foreach s,$(includes-y),-I$s)
# Use 'make D=0' to disable source level debugging.
ifeq ("$(origin D)", "command line")
KBUILD_DEBUG = $(D)
endif
ifndef KBUILD_DEBUG
KBUILD_DEBUG = 1
endif
cflags-y += $(if $(KBUILD_DEBUG:1=),-DNDEBUG,-g -DDEBUG)
# Use 'make O=n' to pass optimization option to gcc.
ifeq ("$(origin O)", "command line")
KBUILD_OPTIMIZATION = $(O)
endif
ifndef KBUILD_OPTIMIZATION
KBUILD_OPTIMIZATION = s
endif
cflags-y += -O$(KBUILD_OPTIMIZATION)
# Utility functions
mkdir = mkdir -p $(abspath $(dir $(1)))
localpath = $(subst $(TOP)/,,$(1))
outdir ?= out/
program ?= $(notdir $(shell pwd))
crt := $(addsuffix .o,$(basename $(subst $(TOP)/,$(outdir),$(abspath $(startup-y)))))
obj-c := $(addsuffix .o,$(basename $(subst $(TOP)/,$(outdir),$(abspath $(src-y)))))
lflags := -lgcc $(lflags)
objtree := $(dir $(crt) $(obj-c))
$(outdir)%.o:: $(TOP)/%.c
$(call cmd,cc_o_c)
$(outdir)%.o:: $(TOP)/%.cpp
$(call cmd,cc_o_c)
quiet_cmd_cc_o_c = CC $(call localpath,$<)
cmd_cc_o_c = $(CC) -c $(cflags-y) $(cflags-$(notdir $<)-y) $< -o $@
$(outdir)%.o:: $(TOP)/%.asm
$(call cmd,cc_o_s)
$(outdir)%.o:: $(TOP)/%.s
$(call cmd,cc_o_s)
$(outdir)%.o:: $(TOP)/%.S
$(call cmd,cc_o_s)
quiet_cmd_cc_o_s = AS $(call localpath,$<)
cmd_cc_o_s = $(CC) -c $(asflags-y) $(cflags-y) $(cflags-$(notdir $<)-y) $< -o $@
all: init $(outdir)$(program)
$(outdir)$(program): lib $(crt) $(obj-c) $(outdir)$(program).ld
$(call cmd,gen_elf)
$(call cmd,gen_hex)
$(call cmd,gen_bin)
ifneq ($(libs-y),)
lib: $(foreach s,$(libs-y),$(outdir)lib$s.a)
define add-lib-rule =
obj-$(1) := $(addsuffix .o,$(basename $(subst $(TOP)/,$(outdir),$(src-$(1)-y))))
objtree += $$(dir $$(obj-$(1)))
$(outdir)lib$(1).a: $$(obj-$(1))
$$(call cmd,mklib)
endef
$(foreach s,$(strip $(libs-y)),$(eval $(call add-lib-rule,$s)))
lflags := $(foreach s,$(libs-y),-l$s) $(lflags)
quiet_cmd_mklib = AR $(call localpath,$@)
cmd_mklib = $(AR) -rcs $@ $^
endif
init:
$(Q)$(call mkdir,$(sort $(objtree)))
$(outdir)$(program).ld: $(ldscript-y)
$(Q)cp $^ $@
quiet_cmd_gen_elf = LD $(call localpath,$@)
cmd_gen_elf = $(CC) -o $@ -nostdlib $(cflags-y) \
$(ldflags-y) -L $(outdir) -T [email protected] \
-Xlinker [email protected] -Xlinker --gc-sections \
$(crt) $(obj-c) $(lflags)
quiet_cmd_gen_hex = OBJCOPY $(call localpath,[email protected])
cmd_gen_hex = $(OBJCOPY) -O ihex $@ [email protected]
quiet_cmd_gen_bin = OBJCOPY $(call localpath,[email protected])
cmd_gen_bin = $(OBJCOPY) -O binary $@ [email protected]
clean:
$(call cmd,cleanup)
quiet_cmd_cleanup = CLEANUP
cmd_cleanup = rm -rf $(outdir)
.PHONY: $(PHONY)