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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
# -*- makefile -*-
# Copyright 2012 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#
# Toolchain configuration
#
# BUILD - Refers to the system doing the "build".
# For example, you would compile servo_ectool to run on your local
# build machine.
# HOST - Refers to target platform on top of the EC.
# For example, you would compile ectool to run on the Chrome OS device.
min_make_version:=3.82
make_version_ok:=$(filter $(min_make_version), \
$(firstword $(sort $(MAKE_VERSION) $(min_make_version))))
ifeq ($(make_version_ok),)
$(error ERROR: GNU make version $(min_make_version) or higher required.)
endif
# Default to clang if TEST_ASAN is enabled
ifneq ($(TEST_FUZZ)$(TEST_ASAN)$(TEST_MSAN)$(TEST_UBSAN),)
CC=clang
endif
# When set to 1, use the subset of the C standard library implemented in EC
# (the "builtin" directory).
# When set to 0, link against the toolchain's implementation of the C standard
# library.
USE_BUILTIN_STDLIB:=1
# When set to 1, link with the C++ compiler.
# When set to 0, link with the C compiler.
USE_CXX_COMPILER:=0
# Boards that compile with clang by default.
# We can't use a config for this since the config generation uses the compiler.
CLANG_BOARDS:= \
bloonchipper \
dartmonkey \
hatch_fp \
nami_fp \
nocturne_fp \
nucleo-dartmonkey \
ifeq ($(filter $(BOARD), $(CLANG_BOARDS)), $(BOARD))
CC=clang
USE_BUILTIN_STDLIB:=0
USE_CXX_COMPILER:=1
# b/144957935: Generate exception on signed integer overflow.
COMMON_WARN+=-ftrapv
endif
ifeq ($(USE_CXX_COMPILER), 1)
COMPILER=$(CXX)
else
COMPILER=$(CC)
endif
# Set the prefix for the compiler if specified (e.g., "x86_64-pc-linux-gnu-").
ifeq ($(origin BUILDCC_PREFIX),undefined)
# If building with Portage use the environment variable it provides.
# https://wiki.gentoo.org/wiki/Embedded_Handbook/General/Introduction#Environment_variables
ifneq ($(CBUILD),)
BUILDCC_PREFIX:=$(CBUILD)-
else
BUILDCC_PREFIX:=x86_64-pc-linux-gnu-
endif
endif
# Extract cc-name
cc-name:=$(shell $(BUILDCC_PREFIX)$(CC) -v 2>&1 | grep -q "clang version" && echo clang || echo gcc)
# Assume we want to use same compiler for both C and C++
ifeq ($(cc-name),gcc)
cxx-name:=g++
else
cxx-name:=clang++
endif
# Try not to assume too much about optional tools and prefixes
CCACHE:=$(shell which ccache 2>/dev/null)
ifeq ($(origin HOST_CROSS_COMPILE),undefined)
# If building with Portage use the environment variable it provides.
# https://wiki.gentoo.org/wiki/Embedded_Handbook/General/Introduction#Environment_variables
ifneq ($(CHOST),)
HOST_CROSS_COMPILE:=$(CHOST)-
else
HOST_CROSS_COMPILE:=$(if $(shell which x86_64-pc-linux-gnu-$(cc-name) 2>/dev/null),x86_64-pc-linux-gnu-,)
endif
endif
AR=$(CROSS_COMPILE)ar
CC=$(CCACHE) $(CROSS_COMPILE)$(cc-name)
CPP=$(CC) -E
CXX=$(CROSS_COMPILE)$(cxx-name)
LD=$(CROSS_COMPILE)ld
NM=$(CROSS_COMPILE)nm
OBJCOPY=$(CROSS_COMPILE)objcopy
OBJDUMP=$(CROSS_COMPILE)objdump
ADDR2LINE=$(CROSS_COMPILE)addr2line
PKG_CONFIG?=pkg-config
BUILD_PKG_CONFIG?=$(PKG_CONFIG)
BUILDCC?=$(CCACHE) $(BUILDCC_PREFIX)$(cc-name)
HOSTCC?=$(CCACHE) $(HOST_CROSS_COMPILE)$(cc-name)
HOSTCXX?=$(CCACHE) $(HOST_CROSS_COMPILE)clang++
HOST_PKG_CONFIG?=$(HOST_CROSS_COMPILE)pkg-config
PROTOC?=protoc
GCOV=$(CROSS_COMPILE)gcov
ifeq ($(cc-name),gcc)
HOSTGCOV=$(HOST_CROSS_COMPILE)gcov
else
HOSTGCOV=$(CURDIR)/util/llvm-gcov.sh
endif
C_WARN = -Wstrict-prototypes -Wdeclaration-after-statement -Wno-pointer-sign
C_WARN += -Werror-implicit-function-declaration
COMMON_WARN += -Wall -Wundef \
-Wno-trigraphs -Wno-format-security -Wno-address-of-packed-member \
-fno-common -fno-strict-aliasing -fno-strict-overflow -Wimplicit-fallthrough
# See https://www.chromium.org/chromium-os/build/c-exception-support
COMMON_WARN+=-fno-exceptions -fno-unwind-tables -fno-asynchronous-unwind-tables
ifndef ALLOW_WARNINGS
COMMON_WARN+=-Werror
ifeq ($(cc-name),clang)
COMMON_WARN+=-Werror=uninitialized
endif
ifeq ($(cc-name),gcc)
COMMON_WARN+=-Werror=maybe-uninitialized
endif
endif
ifeq ($(cc-name),clang)
# TODO(b/172020503): Re-enabling this warning requires a large CL.
C_WARN+= -Wno-ignored-attributes
# TODO(b/172020503): Re-enabling this warning requires the following large CLs:
# https://crrev.com/c/3223973
# https://crrev.com/c/3227261
COMMON_WARN+= -Wno-unused-function
endif
UBSAN_FLAGS=-fsanitize=array-bounds,vla-bound \
-fno-sanitize=vptr \
-fno-sanitize-recover=all
CFLAGS_WARN = $(COMMON_WARN) $(C_WARN)
CXXFLAGS_WARN = $(COMMON_WARN)
CFLAGS_DEBUG= -g
CFLAGS_DEBUG+=$(CFLAGS_DEBUG_CHIP)
CFLAGS_INCLUDE=$(foreach i,$(filter-out builtin, $(includes)),-I$(i) ) -I.
CFLAGS_TEST=$(if $(TEST_BUILD),-DTEST_BUILD=$(EMPTY) \
-DTEST_LEGACY_BUILD=$(EMPTY) \
-DTEST_TASKFILE=$(PROJECT).tasklist,) \
$(if $(CTS_MODULE), $(CFLAGS_CTS)) \
$(if $(EMU_BUILD),-DEMU_BUILD=$(EMPTY)) \
-DTEST_$(PROJECT)=$(EMPTY) -DTEST_$(UC_PROJECT)=$(EMPTY) \
$(if $(TEST_ASAN),-fsanitize=address) \
$(if $(TEST_MSAN),-fsanitize=memory) \
$(if $(TEST_UBSAN),$(UBSAN_FLAGS)) \
$(if $(TEST_FUZZ),-fsanitize=fuzzer-no-link -fno-PIE \
-DTEST_FUZZ=$(EMPTY))
CFLAGS_COVERAGE=$(if $(TEST_COVERAGE),--coverage \
-DTEST_COVERAGE=$(EMPTY),)
CFLAGS_HOSTTEST=$(if $(TEST_HOSTTEST),-DTEST_HOSTTEST=$(EMPTY),) -Iinclude/driver
CFLAGS_DEFINE=-DOUTDIR=$(out)/$(BLD) -DCHIP=$(CHIP) -DBOARD_TASKFILE=$(_tsk_lst_file) \
-DBOARD=$(BOARD) -DCORE=$(CORE) -DPROJECT=$(PROJECT) \
-DCHIP_VARIANT=$(CHIP_VARIANT) -DCHIP_FAMILY=$(UC_CHIP_FAMILY) \
-DBOARD_$(UC_BOARD)=$(EMPTY) \
-DCHIP_$(UC_CHIP)=$(EMPTY) \
-DCORE_$(UC_CORE)=$(EMPTY) \
-DCHIP_VARIANT_$(UC_CHIP_VARIANT)=$(EMPTY) \
-DCHIP_FAMILY_$(UC_CHIP_FAMILY)=$(EMPTY) \
-DFINAL_OUTDIR=$(out) \
-DPROTOBUF_MIN_PROTOC_VERSION=0 \
$(CFLAGS_BASEBOARD)
CPPFLAGS=$(CFLAGS_DEFINE) $(CFLAGS_INCLUDE) $(CFLAGS_TEST) \
$(EXTRA_CFLAGS) $(CFLAGS_COVERAGE) $(CFLAGS_HOSTTEST) $(LATE_CFLAGS_DEFINE) \
-DSECTION_IS_$(BLD)=$(EMPTY) -DSECTION=$(BLD) $(CPPFLAGS_$(BLD))
BUILD_CPPFLAGS=$(CFLAGS_DEFINE) -Icore/host $(CFLAGS_INCLUDE) $(CFLAGS_TEST) \
$(EXTRA_CFLAGS) $(CFLAGS_COVERAGE) $(CFLAGS_HOSTTEST) $(LATE_CFLAGS_DEFINE) \
-DSECTION_IS_$(BLD)=$(EMPTY) -DSECTION=$(BLD) $(CPPFLAGS_$(BLD))
HOST_CPPFLAGS=$(CFLAGS_DEFINE) $(CFLAGS_INCLUDE) $(CFLAGS_TEST) \
$(EXTRA_CFLAGS) $(CFLAGS_COVERAGE) $(CFLAGS_HOSTTEST) $(LATE_CFLAGS_DEFINE) \
-DSECTION_IS_$(BLD)=$(EMPTY) -DSECTION=$(BLD) $(CPPFLAGS_$(BLD))
ifneq ($(BOARD),host)
CPPFLAGS+=-fno-PIC
ifeq ($(USE_BUILTIN_STDLIB), 1)
CPPFLAGS+=-ffreestanding -fno-builtin -nostdinc -nostdlib
CPPFLAGS+=-Ibuiltin/
endif
else
CPPFLAGS+=-Og
endif
CPPFLAGS+= -DCHROMIUM_EC=$(EMPTY)
CFLAGS=$(CPPFLAGS) $(CFLAGS_CPU) $(CFLAGS_DEBUG) $(COMMON_WARN) $(CFLAGS_y)
CFLAGS+= -ffunction-sections -fshort-wchar
CFLAGS+= -fno-delete-null-pointer-checks
CFLAGS+= -fno-PIC
ifneq ($(cc-name),clang)
CFLAGS+= -ffat-lto-objects
CFLAGS+= -fconserve-stack
endif
CXXFLAGS=$(CXXFLAGS_WARN)
CXXFLAGS+=-DPROTOBUF_INLINE_NOT_IN_HEADERS=0
ifeq ($(USE_BUILTIN_STDLIB), 1)
CPPFLAGS+=-DUSE_BUILTIN_STDLIB
else
# TODO(https://issuetracker.google.com/234507656#comment19): C++ include path
# should not be necessary.
CXXFLAGS+=-I/usr/armv7m-cros-eabi/usr/include/c++/v1
# TODO(https://issuetracker.google.com/254916723): Remove when toolchain is
# fixed.
CPPFLAGS+=-D_GNU_SOURCE
endif
ifeq ($(LIBFTDI_NAME),)
FTDIVERSION:=$(shell $(PKG_CONFIG) --modversion libftdi1 2>/dev/null)
ifneq ($(FTDIVERSION),)
LIBFTDI_NAME=ftdi1
else
LIBFTDI_NAME=ftdi
endif
endif
LIBFTDIUSB_HOST_CFLAGS:=$(shell $(HOST_PKG_CONFIG) --cflags lib${LIBFTDI_NAME} libusb-1.0)
LIBFTDIUSB_HOST_LDLIBS:=$(shell $(HOST_PKG_CONFIG) --libs lib${LIBFTDI_NAME} libusb-1.0)
LIBFTDIUSB_BUILD_CFLAGS:=$(shell $(BUILD_PKG_CONFIG) --cflags lib${LIBFTDI_NAME} libusb-1.0)
LIBFTDIUSB_BUILD_LDLIBS:=$(shell $(BUILD_PKG_CONFIG) --libs lib${LIBFTDI_NAME} libusb-1.0)
ifeq ($(TEST_FUZZ),y)
LIBPROTOBUF_CFLAGS:=$(shell $(PKG_CONFIG) --cflags protobuf)
LIBPROTOBUF_LDLIBS:=$(shell $(PKG_CONFIG) --libs protobuf)
LIBPROTOBUF_MUTATOR_CFLAGS:=${LIBPROTOBUF_CFLAGS} \
$(shell $(PKG_CONFIG) --cflags libprotobuf-mutator)
LIBPROTOBUF_MUTATOR_LDLIBS:=$(shell $(PKG_CONFIG) --libs libprotobuf-mutator) \
${LIBPROTOBUF_LDLIBS}
endif
BUILD_CFLAGS=$(LIBFTDIUSB_BUILD_CFLAGS) $(BUILD_CPPFLAGS) -O3 $(CFLAGS_DEBUG)
BUILD_CFLAGS+=$(CFLAGS_WARN)
BUILD_CFLAGS+=-DCHROMIUM_EC=$(EMPTY)
HOST_CFLAGS=$(HOST_CPPFLAGS) -O3 $(CFLAGS_DEBUG) $(CFLAGS_WARN) \
-DHOST_TOOLS_BUILD=$(EMPTY)
HOST_CFLAGS+=$(LIBFTDIUSB_HOST_CFLAGS)
HOST_CFLAGS+=-DCHROMIUM_EC=$(EMPTY)
HOST_CXXFLAGS=$(HOST_CFLAGS) -Wno-c99-designator
ifneq (${SYSROOT},)
LDFLAGS_EXTRA+=--sysroot=${SYSROOT}
endif
LDFLAGS=-g -no-pie -Wl,-X -Wl,--gc-sections -Wl,--build-id=none \
$(LDFLAGS_EXTRA) $(CFLAGS_CPU)
ifeq ($(USE_BUILTIN_STDLIB), 1)
LDFLAGS+=-nostdlib
else
LDFLAGS+=-lnosys
endif
MEMSIZE_FLAGS=
ifeq ($(cc-name),gcc)
MEMSIZE_FLAGS+=-Wl,--print-memory-usage
endif
BUILD_LDFLAGS=$(LIBFTDIUSB_BUILD_LDLIBS)
HOST_LDFLAGS=$(LIBFTDIUSB_HOST_LDLIBS)
ifneq (${HOST_SYSROOT},)
HOST_LDFLAGS+=--sysroot=${HOST_SYSROOT}
endif
HOST_TEST_LDFLAGS=-Wl,-T core/host/host_exe.lds -lrt -pthread -rdynamic -lm\
-fuse-ld=bfd -no-pie \
$(if $(TEST_COVERAGE), --coverage,) \
$(if $(TEST_ASAN), -fsanitize=address) \
$(if $(TEST_MSAN), -fsanitize=memory) \
$(if $(TEST_UBSAN), ${UBSAN_FLAGS}) \
$(if $(TEST_FUZZ), -fsanitize=fuzzer)
# utility function to provide overridable defaults
# $1: name of variable to set
# $2: first default to use
# $3: second default to use
set-option = $(eval $1=$$(strip $$(if $(2),$(2),$(3))))
|