summaryrefslogtreecommitdiff
path: root/Makefile.rules
blob: e3d6e3164a892975aed2224e6c8eaae12e6d5eb4 (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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
# -*- makefile -*-
# vim: set filetype=make :
# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#
# Embedded Controller firmware build system - common targets
#

FLATSIZE_FILE ?= .sizes.txt
BUILD_DIR := $(firstword $(subst /, ,$(out)))

build-utils := $(foreach u,$(build-util-bin),$(out)/util/$(u))
host-utils := $(foreach u,$(host-util-bin),$(out)/util/$(u))
build-art := $(foreach u,$(build-util-art),$(out)/$(u))
build-srcs := $(foreach u,$(build-util-bin),$(sort $($(u)-objs:%.o=util/%.c) $(wildcard util/$(u).c)))
host-srcs := $(foreach u,$(host-util-bin),$(sort $($(u)-objs:%.o=util/%.c) $(wildcard util/$(u).c)))

# Don't do a build test on the following boards:
skip_boards = OWNERS host kevin gru
boards := $(filter-out $(skip_boards),$(notdir $(wildcard board/* private*/board/*)))

# Create output directories if necessary
_common_dir_create := $(foreach d,$(common_dirs),$(shell [ -d $(out)/$(d) ] || \
	    mkdir -p $(out)/$(d)))
_sharedlib_dir_create := $(foreach d,$(dirs),$(shell \
	[ -d $(out)/$(SHOBJLIB)/$(d) ] || mkdir -p $(out)/$(SHOBJLIB)/$(d)))
_dir_create := $(foreach d,$(dirs) $(dirs-y),\
	$(shell [ -d $(out)/$(BLD)/$(d) ] || mkdir -p $(out)/RO/$(d); \
	    mkdir -p $(out)/RW/$(d); mkdir -p $(out)/gen/$(d)))

# V unset for normal output, V=1 for verbose output, V=0 for silent build
# (warnings/errors only). Use echo thus: $(call echo,"stuff  to  echo")
ifeq ($(V),0)
Q := @
echo = echo -n;
quiet = echo -n; $(cmd_$(1))
silent = 1>/dev/null
silent_err = 2>/dev/null
else
echo = echo $(1);
ifeq ($(V),)
Q := @
quiet = @echo '  $(2)' $(subst $(out)/,,$@) ; $(cmd_$(1))
silent = 1>/dev/null
silent_err = 2>/dev/null
else
Q :=
quiet = $(cmd_$(1))
endif
endif

# commands to build all targets
cmd_libec = $(LD) -r $^ -o $@.1.o && \
	$(OBJCOPY) --localize-hidden $@.1.o $@.2.o && \
	$(AR) scr $@ $@.2.o
cmd_lds = $(CPP) -P -C -MMD -MF $@.d -MT $@ $(CPPFLAGS) $< -o $@
cmd_lds_b = $(cmd_lds) -DRW_B_LDS
# Allow obj_to_bin to be overridden by board or chip specific commands
cmd_obj_to_bin ?= $(OBJCOPY) --gap-fill=0xff -O binary $^ $(out)/$*.bin.tmp
cmd_flat_to_obj = $(CC) -Wl,-T $(out)/firmware_image.lds -nostdlib $(CPPFLAGS) \
                  -Wl,--build-id=none -o $@ $<
# Allow the .roshared section to overlap other sections (itself)
cmd_ec_elf_to_flat ?= $(OBJCOPY) --set-section-flags .roshared=share \
				-O binary $< $@
cmd_elf_to_signed ?= $(SIGNER) --key=util/signer/$(3) \
	--b --input=$< --format=bin --output=$@.signed $(SIGNER_EXTRAS) \
	&& sudo chown $(shell whoami) $@.signed && mv $@.signed $@
cmd_elf_to_dis = $(OBJDUMP) -D $< > $@
cmd_elf_to_hex = $(OBJCOPY) -O ihex $< $@
cmd_bin_to_hex = $(OBJCOPY) -I binary -O ihex \
	--change-addresses $(_program_memory_base) $^ $@
cmd_smap = $(NM) $< | sort > $@
cmd_elf = $(CC) $(objs) $(libsharedobjs_elf-y) $(LDFLAGS) \
	-o $@ -Wl,-T,$< -Wl,-Map,$(patsubst %.elf,%.map,$@)
cmd_fuzz_exe = $(CXX) $^ $(HOST_TEST_LDFLAGS) $(LDFLAGS_EXTRA) -o $@
cmd_exe = $(CC) $(ro-objs) $(HOST_TEST_LDFLAGS) -o $@
cmd_c_to_o = $(CC) $(CFLAGS) -MMD -MP -MF $@.d -c $< -o $(@D)/$(@F)
cmd_cxx_to_o = $(CXX) -std=c++11 $(CFLAGS) $(CXXFLAGS) -MMD -MP -MF $@.d -c $< \
		      -o $(@D)/$(@F)
cmd_c_to_build = $(BUILDCC) $(BUILD_CFLAGS) \
	         $(sort $(foreach c,$($(*F)-objs),util/$(c:%.o=%.c)) $(wildcard $*.c)) \
	         $(BUILD_LDFLAGS) \
	         -MMD -MF $@.d -o $@
cmd_c_to_vif = $(BUILDCC) $(BUILD_CFLAGS) $(STANDALONE_FLAGS) \
		-MMD -MF $@.d -c $< -flto -o $@
cmd_c_to_host = $(HOSTCC) $(HOST_CFLAGS) $(HOST_LDFLAGS) -MMD -MF $@.d  -o $@ \
	         $(sort $(foreach c,$($(*F)-objs),util/$(c:%.o=%.c)) $(wildcard $*.c))
cmd_cxx_to_host = $(HOSTCXX) -std=c++0x $(COMMON_WARN) $(HOST_CXXFLAGS)\
	-I ./$($(notdir $@)_ROOT) -o $@ $(filter %.cc,$^) $($(notdir $@)_LIBS)
cmd_host_test = $(MAKE) --no-print-directory BOARD=host PROJECT=$* \
	V=$(V) out=build/host/$* TEST_BUILD=y EMU_BUILD=y $(TEST_FLAG) \
	CROSS_COMPILE= build/host/$*/$*.exe
cmd_run_host_test = ./util/run_host_test $* $(silent)
# generate new version.h, compare if it changed and replace if so
cmd_version = ./util/getversion.sh > $@.tmp && cmp -s $@.tmp $@ || mv $@.tmp $@; rm -f $@.tmp
cmd_vif = $(out)/util/genvif -b $(BOARD) -o $(out)
cmd_mv_from_tmp = mv $(out)/$*.bin.tmp $(out)/$*.bin
cmd_extractrw-y = dd if=$(out)/$(PROJECT).bin.tmp of=$(out)/$(PROJECT).RW.bin \
	       bs=1 count=$(_rw_size) skip=$(_rw_off) $(silent_err)
cmd_copyrw-y = cd $(out) && cp RW/$(PROJECT).RW.flat RW/$(PROJECT).RW.bin
cmd_sharedlib_elf = $(CC) $(libsharedobjs_deps) \
	-Wl,-T,common/ec.$(SHOBJLIB).ld $(LDFLAGS) \
	-o $(out)/$(SHOBJLIB)/$(SHOBJLIB).elf \
	-Wl,-Map,$(out)/$(SHOBJLIB)/$(SHOBJLIB).map
cmd_c_to_taskinfo = $(BUILDCC) \
	$(filter-out -DSECTION_IS_$(BLD),$(BUILD_CFLAGS)) -DSECTION_IS_$(3) \
	-MMD -MF $@.d -c $< -flto -o $@
cmd_link_taskinfo = $(BUILDCC) $(BUILD_CFLAGS) --shared -fPIC $^ \
	$(BUILD_LDFLAGS) -flto -o $@
cmd_proto_to_cxx = $(PROTOC) -I. --cpp_out=$(out)/gen $^

cmd_tp_hash = $(out)/util/gen_touchpad_hash \
		$(if $(TOUCHPAD_FW),-f $(TOUCHPAD_FW)) -o $@

cmd_emmc_bootblock = $(out)/util/gen_emmc_transfer_data \
		$(if $(BOOTBLOCK),-i $(BOOTBLOCK)) -o $@

# commands for RSA signature: rwsig does not need to sign the whole image
# (it signs the RW part separately). usbpd1 type needs to sign the final image.
ifeq ($(CONFIG_RWSIG_TYPE_RWSIG),)
	cmd_rsasign = futility sign --type usbpd1 --pem $(PEM) $(out)/$*.bin.tmp
else
	cmd_rsasign =
endif

cmd_key_extract = futility create $(PEM) $(out)/$* $(silent)

cmd_rsasign_rwsig = futility sign --type rwsig \
                                    --prikey $(out)/key.vbprik2 $< $@

# commands to build optional xref files
cmd_deps_to_list = cat $(deps) | tr -d ':\\' | tr ' ' '\012' \
	| egrep '\.[chS]$$' | sort | uniq > $@
cmd_etags = etags -o $@ $(shell cat $<)
cmd_ctags = ctags -o $@ $(shell cat $<)
targ_if_prog = $(if $(shell which $(1) 2>/dev/null),$(2),)

# By default, the "build_boards" and "try_build_boards" targets will build all
# of the boards listed in $(boards). However, the invoker can provide a
# different list via the BOARDS variable. Providing an empty value for BOARDS
# is not allowed.
BOARDS ?= $(boards)
ifeq ($(BOARDS),)
$(error BOARDS must be non-empty)
endif

FAILED_BOARDS_DIR = .failedboards
# When building with -j, it's easy to miss errors. If you don't have your shell
# configured to warn you about nonzero exit, you may not even notice that "make
# buildall -j" failed. To make it more obvious, we'll do one level of recursion
# here.
.PHONY: try_build_boards
try_build_boards: $(foreach b, $(BOARDS), proj-$(b))

.PHONY: build_boards
build_boards:
	@rm -rf $(FAILED_BOARDS_DIR)
	@mkdir $(FAILED_BOARDS_DIR)
	@for b in $(BOARDS); do echo 'starting' > $(FAILED_BOARDS_DIR)/$$b; done
	$(MAKE) try_build_boards

.PHONY: buildall
buildall: build_boards
	$(MAKE) build_cts
	$(MAKE) runtests
	@touch .tests-passed
	@echo "$@ completed successfully!"
	@echo "Tightest boards' RW flash images, bytes free:"
	@grep . build/*/RW/space_free_flash.txt | \
	 sed 's,build/\([^/]*\)/RW/space_free_flash.txt:\(.*\)$$,\2 \1,' | \
	 sort -n | head -3 | while read size board; do \
		printf "%-10s: %6d\n" $$board $$size; \
	 done
	@echo "Tightest boards' RW RAM images, bytes free:"
	@grep . build/*/RW/space_free_ram.txt | \
	 sed 's,build/\([^/]*\)/RW/space_free_ram.txt:\(.*\)$$,\2 \1,' | \
	 sort -n | head -3 | while read size board; do \
		printf "%-10s: %6d\n" $$board $$size; \
	 done

.PHONY: try_build_tests
try_build_tests: $(foreach b, $(BOARDS), tests-$(b))

.PHONY: buildalltests
buildalltests:
	@rm -rf $(FAILED_BOARDS_DIR)
	@mkdir $(FAILED_BOARDS_DIR)
	@for b in $(BOARDS); do echo 'starting' > $(FAILED_BOARDS_DIR)/$$b; done
	$(MAKE) try_build_tests
	@echo "$@ completed successfully!"

showboards:
	@echo $(sort $(boards))

# Print any important notices at the end of the build.
.PHONY: notice
notice: $(config)
ifeq ($(CONFIG_EXPERIMENTAL_CONSOLE),y)
ifeq ($(TEST_BUILD),)
	@echo "*** NOTE: The experimental console is ENABLED. ***"
	@echo "You will need to run the EC-3PO interactive console in the util"
	@echo "directory!  Otherwise, you won't be able to enter any commands."
endif # not a TEST_BUILD
endif # CONFIG_EXPERIMENTAL_CONSOLE=y

proj-%:
	@echo 'building' > $(FAILED_BOARDS_DIR)/$*
	@echo "======= building $*"
	$(MAKE) --no-print-directory BOARD=$* V=$(V)
	@rm $(FAILED_BOARDS_DIR)/$*

tests-%:
	@echo 'building tests' > $(FAILED_BOARDS_DIR)/$*
	@echo "======= building $* tests"
	$(MAKE) --no-print-directory BOARD=$* V=$(V) tests
	@rm $(FAILED_BOARDS_DIR)/$*

dis-y := $(out)/RW/$(PROJECT).RW.dis
dis-$(CONFIG_FW_INCLUDE_RO) += $(out)/RO/$(PROJECT).RO.dis
dis-$(CONFIG_SHAREDLIB) += $(out)/$(SHOBJLIB)/$(SHOBJLIB).dis
dis: $(dis-y)
.PHONY: dis

hex-y := $(out)/RO/$(PROJECT).RO.hex $(out)/RW/$(PROJECT).RW.hex $(out)/$(PROJECT).hex
hex: $(hex-y)
.PHONY: hex

.PHONY: utils-art
utils-art: $(build-art)

.PHONY: utils-host
utils-host: $(host-utils)

.PHONY: utils-build
utils-build: $(build-utils)

.PHONY: utils
utils: utils-host utils-build utils-art

# On board test binaries
test-targets=$(foreach t,$(test-list-y),test-$(t))
.PHONY: $(test-targets)

ifeq "$(CONFIG_COMMON_RUNTIME)" "y"
$(test-targets): test-%:
	@set -e ; \
	$(call echo,"  BUILD   $(out)/$*") \
	$(MAKE) --no-print-directory BOARD=$(BOARD) PROJECT=$* \
	        V=$(V) out=$(out)/$* TEST_BUILD=y; \
	cp $(out)/$*/$*.bin $(out)/test-$*.bin
endif

.PHONY: tests
tests: $(test-targets)

# Emulator test executables
host-test-targets=$(foreach t,$(test-list-host),host-$(t))
run-test-targets=$(foreach t,$(test-list-host),run-$(t))
.PHONY: $(host-test-targets) $(run-test-targets)

$(host-test-targets): host-%:
	$(call quiet,host_test,BUILD  )

$(run-test-targets): run-%: host-%
	$(call quiet,run_host_test,TEST   )

.PHONY: hosttests runtests
hosttests: $(host-test-targets)
runtests: $(run-test-targets)

# Automatically enumerate all suites.
cts_excludes := common
cts_suites := $(filter-out $(cts_excludes), \
	$(shell find cts -maxdepth 1 -mindepth 1 -type d -printf "%f "))

# Add boards below as CTS is expanded.
cts_boards := stm32l476g-eval nucleo-f072rb

.PHONY: build_cts

# Create CTS rule automatically for given suite and board
# $1: suite name
# $2: board name
define make-cts =
build_cts: cts-$(1)-$(2)
cts-$(1)-$(2):
	$$(MAKE) CTS_MODULE=$(1) BOARD=$(2)
# Do not remove this blank line

endef

# Create rules for all cts-suite-board combinations. Additionally, we serialize
# targets per board: cts-x-board -> cts-y-board -> ...
# If we don't serialize targets, parallel make fails because all suites
# try to produce ec.bin in the same directory (e.g. build/stm32l476g-eval).
$(foreach b, $(cts_boards), \
	$(foreach s, $(cts_suites), \
		$(eval $(call make-cts,$(s),$(b))) \
	) \
)

cov-test-targets=$(foreach t,$(test-list-host),build/host/$(t).info)
bldversion=$(shell (./util/getversion.sh ; echo VERSION) | $(CPP) -P -)

# lcov fails when multiple instances run at the same time.
# We need to run them sequentially by using flock
cmd_lcov=flock /tmp/ec-lcov-lock -c "lcov -q -o $@ -c -d build/host/$*"
cmd_report_cov=genhtml -q -o build/host/coverage_rpt -t \
	       "EC Unittest "$(bldversion) $^

# Unless V is set to 0 we always want the 'size:' target to report its output,
# there is no point in generating a short form command trace when calculating
# size.
ifeq ($(V),0)
cmd_size=
else
cmd_size=$(Q)awk '\
  /__image_size =/ {image_size = strtonum($$1)} \
  /^FLASH/ {flash_size = strtonum($$3)} \
  /__ram_free =/ {ram_free = strtonum($$1)} \
  END {room_free = flash_size - image_size; \
       print room_free > "$(out)/RW/space_free_flash.txt"; \
       print ram_free > "$(out)/RW/space_free_ram.txt"; \
       printf ("  *** %s bytes in flash and %s bytes in RAM still available on $(BOARD) ****\n", \
	       room_free, ram_free)}' $(out)/RW/$(PROJECT).RW.map
endif

build/host/%.info: run-%
	$(call quiet,lcov,COV    )

.PHONY: coverage
coverage: TEST_FLAG=TEST_COVERAGE=y
coverage: $(cov-test-targets)
	$(call quiet,report_cov,REPORT )

# Fuzzing tests

fuzz-test-targets=$(foreach t,$(fuzz-test-list-host),host-$(t))

.PHONY: $(fuzz-test-targets)

$(fuzz-test-targets): host-%:
	$(call quiet,host_test,BUILD  )

.PHONY: buildfuzztests
buildfuzztests: TEST_FLAG=TEST_FUZZ=y TEST_ASAN=y
buildfuzztests: $(fuzz-test-targets)

$(out)/libec.a: $(ro-objs)
	$(call quiet,libec,BUILD )

$(out)/firmware_image.lds: common/firmware_image.lds.S
	$(call quiet,lds,LDS    )
$(out)/%.lds: core/$(CORE)/ec.lds.S
	$(call quiet,lds,LDS    )

$(out)/%_B.lds: core/$(CORE)/ec.lds.S
	$(call quiet,lds_b,LDS_B  )

$(out)/%.bin: $(out)/%.obj
	$(call quiet,obj_to_bin,OBJCOPY)
	$(if $(wildcard $(PEM)),$(call quiet,rsasign,SIGN   ),)
	$(if $(wildcard $(PEM)),$(call quiet,extractrw-y,EXTR_RW), \
			$(call quiet,copyrw-y,COPY_RW))
	$(call quiet,mv_from_tmp,MV     )

$(out)/$(BOARD)_vif.txt: $(out)/util/genvif
	$(call quiet,vif,VIF    )

flat-y := $(out)/RW/$(PROJECT).RW.flat
flat-$(CONFIG_FW_INCLUDE_RO) += $(out)/RO/$(PROJECT).RO.flat

deps += $(out)/firmware_image.lds.d $(flat-y:%.flat=%.lds.d)

flat-$(CONFIG_RWSIG_TYPE_RWSIG) += $(out)/key.vbpubk2
flat-$(CONFIG_RWSIG_TYPE_RWSIG) += $(out)/RW/$(PROJECT).RW.flat.sig

flat-$(CONFIG_SHAREDLIB) += $(libsharedobjs-y)

$(out)/$(PROJECT).obj: common/firmware_image.S $(out)/firmware_image.lds \
	$(flat-y)
	$(call quiet,flat_to_obj,CAT    )

$(out)/%.dis: $(out)/%.elf
	$(call quiet,elf_to_dis,OBJDUMP)

$(out)/RW/%.hex: $(out)/RW/%.elf $(out)/RW/%.smap
	$(call quiet,elf_to_hex,OBJCOPY)

ifeq ($(SIGNED_IMAGES),)
$(out)/%.flat: $(out)/%.elf $(out)/%.smap utils-build
	$(call quiet,ec_elf_to_flat,OBJCOPY)

$(out)/RO/%.hex: $(out)/RO/%.elf $(out)/RO/%.smap
	$(call quiet,elf_to_hex,OBJCOPY)
else
$(out)/RO/%.flat: $(out)/RO/%.elf  $(out)/RO/%.smap
	$(call quiet,elf_to_signed,RO_SIGN,$(CR50_RO_KEY))

$(out)/RW/%.flat: $(out)/RW/%.elf  $(out)/RW/%.smap
	$(call quiet,elf_to_signed,RW_SIGN,$(CR50_RW_KEY))

$(out)/RO/%.hex: $(out)/RO/%.flat
	$(call quiet,bin_to_hex,OBJCOPY)
endif
$(out)/$(PROJECT).hex: $(out)/$(PROJECT).bin
	$(call quiet,bin_to_hex,OBJCOPY)

$(out)/%.vbprik2 $(out)/%.vbpubk2: $(PEM)
	$(call quiet,key_extract,KEY    )

$(out)/RW/%.flat.sig: $(out)/RW/%.flat $(out)/key.vbprik2
	$(call quiet,rsasign_rwsig,SIGN   )

$(out)/RW/%.elf: override BLD:=RW
$(out)/RW/%.elf: private objs := $(rw-objs)
$(out)/RW/%.elf: $(out)/RW/%.lds $(rw-objs) $(libsharedobjs_elf-y)
	$(call quiet,elf,LD     )

$(out)/RO/%.elf: override BLD:=RO
$(out)/RO/%.elf: private objs := $(ro-objs)
$(out)/RO/%.elf: $(out)/RO/%.lds $(ro-objs) $(libsharedobjs_elf-y)
	$(call quiet,elf,LD     )

$(out)/%.elf: $(out)/%.lds $(objs)
	$(call quiet,elf,LD     )

$(out)/$(SHOBJLIB)/$(SHOBJLIB).elf: $(sharedlib-objs)
	@mkdir -p $(out)/$(SHOBJLIB)
	$(call quiet,sharedlib_elf,LD     )

$(out)/%.smap: $(out)/%.elf
	$(call quiet,smap,NM     )

ifeq ($(TEST_FUZZ),y)
$(out)/$(PROJECT).exe: $(rw-only-objs) $(out)/libec.a
	$(call quiet,fuzz_exe,EXE    )
else
$(out)/$(PROJECT).exe: $(ro-objs)
	$(call quiet,exe,EXE    )
endif

$(out)/RO/%.o:%.c
	$(call quiet,c_to_o,CC     )
$(out)/RW/%.o:%.c
	$(call quiet,c_to_o,CC     )

$(out)/RO/%.o:%.cc
	$(call quiet,cxx_to_o,CXX    )
$(out)/RW/%.o:%.cc
	$(call quiet,cxx_to_o,CXX    )

$(out)/RO/%.pb.o:$(out)/gen/%.pb.cc
	$(call quiet,cxx_to_o,CXX    )
$(out)/RW/%.pb.o:$(out)/gen/%.pb.cc
	$(call quiet,cxx_to_o,CXX    )
$(out)/gen/%.pb.cc:%.proto
	$(call quiet,proto_to_cxx,PROTOC )
$(out)/gen/%.pb.h:%.proto
	$(call quiet,proto_to_cxx,PROTOC )

$(out)/$(SHOBJLIB)/%.o: override LATE_CFLAGS_DEFINE:=-DSHAREDLIB_IMAGE
$(out)/$(SHOBJLIB)/%.o:%.c
	$(call quiet,c_to_o,CC     )

$(out)/RO/%.o:%.S
	$(call quiet,c_to_o,AS     )
$(out)/RW/%.o:%.S
	$(call quiet,c_to_o,AS     )


# Conditionally force the rebuilding of ec_version.h only if it would be
# changed.
old_version_hash := $(shell cat $(out)/ec_version.h 2> /dev/null | md5sum -)
new_version_hash := $(shell BOARD=$(BOARD) ./util/getversion.sh | md5sum -)

ifneq ($(old_version_hash),$(new_version_hash))
.PHONY: $(out)/ec_version.h
endif

# All of the objects have an order only dependency on the ec_version header.
# This ensures that if ec_version.h needs to be built (because it was marked
# PHONY above) then it will be rebuilt before any objects.  This is important
# because some source files will include ec_version.h and fail to compile if
# it doesn't already exist.  This dependency shouldn't be a normal dependency
# because that would cause every object to be rebuilt when ec_version.h
# changes, instead of just the ones that actually depend on it.  The objects
# that truly depend on ec_version.h will have that information encoded in their
# .d file.
$(ro-objs): | $(out)/ec_version.h
$(rw-objs): | $(out)/ec_version.h
$(sharedlib-objs): | $(out)/ec_version.h

$(out)/ec_version.h:
	$(call quiet,version,VERSION)

$(build-utils): $(out)/%:$(build-srcs)
	$(call quiet,c_to_build,BUILDCC)

$(host-utils): $(out)/%:$(host-srcs)
	$(call quiet,c_to_host,HOSTCC )

$(out)/cscope.files: $(out)/$(PROJECT).bin
	$(call quiet,deps_to_list,SH     )

$(out)/TAGS: $(out)/cscope.files
	$(call quiet,etags,ETAGS  )

$(out)/tags: $(out)/cscope.files
	$(call quiet,ctags,CTAGS  )

# TODO: optional make rules for PROJECT_EXTRA
$(npcx-monitor-fw-bin):
	$(if $(V),,@echo '  EXTBIN ' $(subst $(out)/,,$@) ; )
	-@ mkdir -p $(@D)
	-@ $(CC) $(CFLAGS) -MMD -MF $(out)/$(npcx-lfw).d -c $(npcx-monitor-fw).c \
		-o $(out)/$(npcx-monitor-fw).o
	-@ $(CC) $(out)/$(npcx-monitor-fw).o $(LDFLAGS) \
		-o $(out)/$(npcx-monitor-fw).elf -Wl,-T,$(npcx-monitor-fw).ld \
		-Wl,-Map,$(out)/$(npcx-monitor-fw).map
	-@ $(OBJCOPY) -O binary $(out)/$(npcx-monitor-fw).elf $@

$(out)/$(npcx-monitor-hdr)_ro.o:$(npcx-monitor-hdr).c
	-@ $(CC) $(CFLAGS) -D SECTION_IS_RO -MMD -c $(npcx-monitor-hdr).c -o $@

$(npcx-monitor-hdr-ro-bin):$(out)/$(npcx-monitor-hdr)_ro.o
	$(if $(V),,@echo '  EXTBIN ' $(subst $(out)/,,$@) ; )
	-@ $(OBJCOPY) -O binary $< $@

$(out)/$(npcx-monitor-hdr)_rw.o:$(npcx-monitor-hdr).c
	-@ $(CC) $(CFLAGS) -MMD -c $(npcx-monitor-hdr).c -o $@

$(npcx-monitor-hdr-rw-bin):$(out)/$(npcx-monitor-hdr)_rw.o
	$(if $(V),,@echo '  EXTBIN ' $(subst $(out)/,,$@) ; )
	-@ $(OBJCOPY) -O binary $< $@

.PHONY: xrefs
xrefs: $(call targ_if_prog,etags,$(out)/TAGS) \
	$(call targ_if_prog,ctags,$(out)/tags)

.PHONY: flash
flash: $(out)/ec.bin
	openocd -c "set BOARD $(BOARD)"\
		-c "set BUILD_DIR $(out)"\
		-f $(BDIR)/openocd-flash.cfg

.PHONY: flash_ec
flash_ec: $(out)/ec.bin
	./util/flash_ec --board $(BOARD) --image $(out)/ec.bin

.PHONY: flash_dfu
flash_dfu: $(out)/ec.bin
	sudo ./$(BDIR)/dfu $(out)/ec.bin

.PHONY: print-baseboard
print-baseboard:
	@echo "${BASEBOARD}"

.PHONY: print-configs
print-configs:
	@echo "----------------------------------------------------------------"
	@echo "| RO Config:                                                   |"
	@echo "----------------------------------------------------------------"
	@$(CPP) $(CPPFLAGS) -P -dM -Ichip/$(CHIP) \
		-I$(BASEDIR) -I$(BDIR) -DSECTION_IS_RO include/config.h | \
		grep "#define CONFIG_" | cut -c9- | sort
	@echo "----------------------------------------------------------------"
	@echo "| RW Config:                                                   |"
	@echo "----------------------------------------------------------------"
	@$(_tsk_cfg_rw) $(CPP) $(CPPFLAGS) -P -dM -Ichip/$(CHIP) \
		-I$(BASEDIR) -I$(BDIR) -DSECTION_IS_RW include/config.h | \
		grep "#define CONFIG_" | cut -c9- | sort

.PHONY: clean
clean:
	-rm -rf $(out)

.PHONY: clobber
clobber:
	-rm -rf build TAGS cscope.files cscope.out
ifneq ($(CCACHE),)
	$(CCACHE) -c
endif

.PHONY: help
help:
	@echo "Google Chromium EC build"
	@echo "Common Targets:"
	@echo "  all   [BOARD=]       - Build a single board (Default target)"
	@echo "  clean [BOARD=]       - Clean a single board"
	@echo "  buildall             - Build and test all boards"
	@echo "  clobber              - Clean all boards"
	@echo "  proj-<boardname>     - Build a single board (similar to 'all BOARD=boardname')"
	@echo "  savesizes            - Save the filesizes of currently built boards for comparison"
	@echo "  newsizes             - Compare previously saved filesizes against new sizes"
	@echo "Common Variables:"
	@echo "  V=1                  - Show make output"
	@echo "  BOARD=               - Set the board name to build (Default is $(BOARD))"
	@echo "  CROSS_COMPILE=       - Set the compiler for the board"
	@echo "  CROSS_COMPILE_arch=  - Set the compiler for arch"
	@echo "     The board picks its CROSS_COMPILE_arch if CROSS_COMPILE is not set."
	@echo "     arch may be one of 'arm', 'i386', 'nds32'."
	@echo "Example:"
	@echo "  make BOARD=reef CROSS_COMPILE_arm='arm-eabi-'"

.PHONY: savesizes
savesizes:
	@grep . build/*/*/space_free_*.txt | \
		sed 's,\(.*\).txt:\(.*\),\2 \1,' | sort --key 2 > \
		$(FLATSIZE_FILE)
	@if [ -s $(FLATSIZE_FILE) ]; then \
		echo "Saved sizes for $$(cat $(FLATSIZE_FILE) | wc -l) files"; \
	else \
		echo "Error: No file sizes saved.  Are they built?"; \
	fi

.PHONY: newsizes
newsizes:
	@if [ ! -s "$(FLATSIZE_FILE)" ]; then \
		echo "Error: no saved size file ($(FLATSIZE_FILE))."; \
		echo " Run 'make savesizes' first"; \
		exit 1; \
	fi
	@FILES_CHANGED=0; \
	FILES_IN_LIST=0; \
	FILES_COMPARED=0; \
	FILE_SIZE_CHANGE=0; \
	NEW_SIZES=$$(grep . build/*/*/space_free_*.txt | \
		sed 's,\(.*\).txt:\(.*\),\2 \1,'); \
	while read -r -u 10 line; do \
		FILES_IN_LIST=$$((FILES_IN_LIST+1)); \
		FLATFILE=$$(echo "$$line" | cut -f2 -d ' '); \
		FLATSIZE_ORG=$$(echo "$$line" | cut -f1 -d ' '); \
		FLATSIZE_NEW="$$(grep "$$FLATFILE" <<< "$$NEW_SIZES" | \
			sed 's/ .*$$//')"; \
		if [ -n "$$FLATSIZE_NEW" ]; then \
			FILES_COMPARED=$$((FILES_COMPARED+1)); \
			if [ "$$FLATSIZE_NEW" -gt "$$FLATSIZE_ORG" ]; then \
				FILES_CHANGED=$$((FILES_CHANGED+1)); \
				FILE_SIZE_CHANGE=$$((FILE_SIZE_CHANGE+ \
				FLATSIZE_NEW-FLATSIZE_ORG)); \
				printf "%s grew by %s bytes: (%d to %d)\n" \
					"$$FLATFILE" \
					"$$((FLATSIZE_NEW-FLATSIZE_ORG))" \
					"$$FLATSIZE_ORG" "$$FLATSIZE_NEW"; \
			elif [ "$$FLATSIZE_NEW" -lt "$$FLATSIZE_ORG" ]; then \
				FILES_CHANGED=$$((FILES_CHANGED+1)); \
				FILE_SIZE_CHANGE=$$((FILE_SIZE_CHANGE+ \
				FLATSIZE_NEW-FLATSIZE_ORG)); \
				printf "%s shrank by %s bytes: (%d to %d)\n" \
					"$$FLATFILE" \
					"$$((FLATSIZE_ORG-FLATSIZE_NEW))" \
					"$$FLATSIZE_ORG" "$$FLATSIZE_NEW"; \
			fi; \
		fi; \
	done 10< "$(FLATSIZE_FILE)"; \
	echo "Compared $$FILES_COMPARED of $$FILES_IN_LIST files."; \
	if [ $$FILES_COMPARED -ne 0 ] && [ $$FILES_CHANGED -eq 0 ]; then \
		echo "File sizes are unchanged."; \
	else \
		printf "%d files changed.\n" "$$FILES_CHANGED"; \
		printf "Total size change: %s bytes.\n" "$$FILE_SIZE_CHANGE"; \
		printf "Average size change: %d bytes.\n" \
			"$$((FILE_SIZE_CHANGE / FILES_CHANGED))"; \
	fi

# The reason why don't add elf files as dependencies, but ask users to build
# them first is because elf dependencies will cause the elf files be rebuilt for
# updating date, which shouldn't happen when analyzing the existing firmwares.
.PHONY: analyzestack
ANNOTATION ?= ./$(BDIR)/analyzestack.yaml
analyzestack: $(out)/util/export_taskinfo.so
	@if [ "$(SECTION)" != "RO" ] && [ "$(SECTION)" != "RW" ]; then \
		echo "Please specify SECTION=RO or RW. The default is RW."; \
		SECTION="RW"; \
	fi; \
	ELF=$(out)/$$SECTION/ec.$$SECTION.elf; \
	EXPORT_TASKINFO=$(out)/util/export_taskinfo.so; \
	if [ ! -f "$$ELF" ]; then \
		echo "Some files are missing.  Are they built?"; \
		exit 1; \
	fi; \
	extra/stack_analyzer/stack_analyzer.py --objdump "$(OBJDUMP)" \
	        --addr2line "$(ADDR2LINE)" --section "$$SECTION" \
	        --annotation $(ANNOTATION) \
	        --export_taskinfo "$$EXPORT_TASKINFO" "$$ELF"

# Calculate size of remaining room in flash, using variables generated by
# linker.
size: $(out)/$(PROJECT).bin
	$(call cmd_size)

.SECONDARY:

-include $(deps)