Makefile 18.6 KB
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
## ###################################################################
##
##     Copyright 2019-2020 NXP
##
##     Redistribution and use in source and binary forms, with or without modification,
##     are permitted provided that the following conditions are met:
##
##     o Redistributions of source code must retain the above copyright notice, this list
##       of conditions and the following disclaimer.
##
##     o Redistributions in binary form must reproduce the above copyright notice, this
##       list of conditions and the following disclaimer in the documentation and/or
##       other materials provided with the distribution.
##
##     o Neither the name of the copyright holder nor the names of its
##       contributors may be used to endorse or promote products derived from this
##       software without specific prior written permission.
##
##     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
##     ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
##     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
##     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
##     ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
##     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
##     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
##     ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
##     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
##     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
##
##
## ###################################################################

# This Makefile will build the System Controller and Test

SHELL=/bin/bash -o pipefail

# Configure Si Rev
DEF_SREV := B0
ifdef r
    R := $(r)
endif
LOWER_R := $(shell echo $(R) | tr A-Z a-z)
UPPER_R := $(shell echo $(R) | tr a-z A-Z)
export R LOWER_R UPPER_R

# Configure emul
ifdef z
    Z := $(z)
endif
ifndef Z
    Z = 0
endif
ifeq ($(Z),1)
	FLAGS += -DEMUL
endif

# Include config makefiles
HELP := ""
HELP_OPT := ""
MAKECONFIG := $(shell find makefiles -name Makefile.config)
MAKECONFIG += $(shell find makefiles -name Makefile.full)
include $(foreach makeconfig,$(MAKECONFIG),$(makeconfig))

# Default config
CONFIG ?= mx8qm
DEVICE ?= ALL
OUT ?= build_api
HW ?= SIMU
PROJ ?= ""
SREC_OFFSET ?= 0x00000000
DOXYGEN ?= doxygen
DOX_OUT = $(OUT)/docs
FLAGS += -DCPU_$(DEVICE) $(OPT) -DECC_CLEAN

# Configure verbose logging

ifdef v
    V := $(v)
endif
ifeq ($(V),1)
    AT :=
else
    AT := @
endif
export AT

# Configure forced error
ifdef f
    F := $(f)
endif
ifndef F
    F = 0
endif
ifeq ($(F),1)
	FLAGS += -DFORCE_COMPILE_ERROR
endif
ifeq ($(F),2)
	FLAGS += -DFORCE_TEST_ERROR
endif

# Configure board
ifdef b
    B := $(b)
endif
ifdef ALIAS_B
ifeq ($(B), $(ALIAS_B))
	override B = $(MAP_B)
endif
endif
ifeq ($(HW), SIMU)
    override B := none
endif
ifndef DEF_B
    DEF_B := val
endif
ifndef B
    B := $(DEF_B)
endif
LOWER_B := $(shell echo $(B) | tr A-Z a-z)
export B
export LOWER_B
ifeq ($(LOWER_B),none)
	BOARD = none
else
	BOARD = $(CONFIG)_$(LOWER_B)
endif
FLAGS += -DBOARD_$(shell echo $(B) | tr a-z A-Z)

# Configure SS
ifdef c
    C := $(c)
endif
ifndef C
    C := ALL
endif
LOWER_C := $(shell echo $(C) | tr A-Z a-z)
UPPER_C := $(shell echo $(C) | tr a-z A-Z)
export C LOWER_C UPPER_C

# Configure debug
ifdef d
    D := $(d)
endif
ifndef D
    D = 1
endif
ifeq ($(D),1)
	DEBUG = -g -DDEBUG -Os
else
	DEBUG = -Os
endif
ifeq ($(COVERAGE),1)
	DEBUG = -DGCOV -fprofile-arcs -ftest-coverage
	LDFLAGS = -lgcov --coverage
endif
ifdef dl
    DL := $(dl)
endif
ifdef DL
	FLAGS += -DDL=$(DL)
endif
ifdef tl
    TL := $(tl)
endif
ifdef TL
	FLAGS += -DTL=$(TL)
endif

#configure LTO (link time optimization)
ifdef lto
    LTO := $(lto)
endif
ifndef lto
    LTO = 0
endif
ifeq ($(LTO),1)
    CFLAG_LTO = -flto
	FLAGS += -DLTO
endif

# Configure tests
ifdef t
    T := $(t)
endif
ifndef T
    T := NONE
else
	FLAGS += -DHAS_TEST
endif
LOWER_T := $(shell echo $(T) | tr A-Z a-z)
UPPER_T := $(shell echo $(T) | tr a-z A-Z)
export T LOWER_T UPPER_T
ifeq ($(LOWER_T),all)
	FLAGS += -DTEST_ALL
endif
ifeq ($(LOWER_T),boottime)
	FLAGS += -DTEST_BOOTTIME
endif
FLAGS += -DTEST=$(LOWER_T)

# Configure XRDC
ifdef x
    X := $(x)
endif
ifndef X
    X = 1
endif
ifeq ($(X),1)
	FLAGS += -DXRDC_SUPPORT
endif
ifeq ($(X),2)
	FLAGS += -DXRDC_SUPPORT -DXRDC_NOCHECK
endif

# Configure UART
ifdef u
    U := $(u)
endif
ifndef U
    U = 0
endif
ifeq ($(D),0)
	FLAGS += -DDEBUG_UART=0
else
	FLAGS += -DDEBUG_UART=$(U)
endif

# Configure MSI posted writes
ifdef w
    W := $(w)
endif
ifndef W
    W = 1
endif
FLAGS += -DPOST_WR_LIMIT=$(W)

# Configure AutoCal
ifdef ac
    AC := $(ac)
endif
ifndef AC
    AC = 0
endif
ifeq ($(AC),1)
	FLAGS += -DAUTOCAL
endif

# Configure manifest
MANIFEST := $(shell md5sum --status -c MANIFEST 1>&2 2> /dev/null; echo $$?)
ifeq ($(MANIFEST),1)
	FLAGS += -DDIRTY
endif

FLAGS += -DSREV_$(UPPER_R)

OUT := $(OUT)_$(LOWER_R)

# Configure monitor
ifdef m
    M := $(m)
endif
ifndef M
    M = 0
endif
ifndef EM
    EM = 0
endif
ifeq ($(M),1)
	FLAGS += -DMONITOR
	DCDFLAGS += -DMINIMIZE
endif
ifeq ($(EM),1)
	FLAGS += -DEXPORT_MONITOR
	DCDFLAGS += -DMINIMIZE
endif

ifdef NO_OVLY
	FLAGS += -DNO_OVLY
endif

ifdef NO_WDOG_DIS
	FLAGS += -DNO_WDOG_DIS
endif

# Check config
ifeq ($(D),0)
ifeq ($(M),1)
$(error M=1 and D=0 are mutually exclusive)
endif
endif

WARNS = -Wall -Wextra -Wno-missing-braces -Wno-missing-field-initializers \
    -Wfloat-equal -Wswitch-default -Wcast-align \
    -Wpointer-arith -Wredundant-decls -Wbad-function-cast -Wstrict-prototypes \
    -Wundef -Wcast-qual -Wshadow \
    -Wold-style-definition -Wno-unused-parameter -Werror

ifneq ($(HW), SIMU)
	WARNS += -Wstack-usage=1024
endif

# Set common flags
rootdir := $(CURDIR)
SRC = platform
DOC = doc
FLAGS += $(DEBUG) ${WARNS}
# Configure tools
ifeq ($(wildcard $(TOOLS)/srec/srec_cat),)
    SREC_CAT = srec_cat 
else
    SREC_CAT = $(TOOLS)/srec/srec_cat    
endif
ifeq ($(wildcard $(TOOLS)/cppcheck/cppcheck),)
    CPPCHECK = cppcheck   
else
    CPPCHECK = $(TOOLS)/cppcheck/cppcheck   
endif

# Configure compiler
ifeq ($(HW), SIMU)
    #GNU GCC
    AS		= as
    LD		= ld
    CC		= gcc
    CPP		= cpp
    AR		= ar
    NM		= nm
    LDR		= ldr
    STRIP	= strip
    OBJCOPY	= objcopy
    OBJDUMP	= objdump
    GCOV    = gcov

    INCLUDE = -I$(rootdir)/platform/ \
              -I$(rootdir)/platform/CMSIS/Include/ \
              -I$(rootdir)/platform/drivers/common/ \
              -I$(rootdir)/platform/devices/ \
			  -I$(rootdir)/platform/devices/$(DEVICE)/ \
			  -I$(rootdir)/platform/devices/MX8/ \
              -I$(rootdir)/platform/config/$(CONFIG)/ \
              -I$(rootdir)/platform/config/$(CONFIG)/$(UPPER_C) \
              -I$(rootdir)/platform/board/$(BOARD)

ifneq ($(COVERAGE),1)
ifneq ($(NO_ASAN),1)
	CFLAGS += -fsanitize=address -fno-omit-frame-pointer -fsanitize=undefined
	LDFLAGS += -lasan -lubsan
endif
endif

    CFLAGS += $(FLAGS) -std=gnu99 -m32 -fshort-enums
    LDFLAGS += -m32 -Wl,-lrt
    FLAGS += -DSIMU -DNO_DEVICE_ACCESS
    WARNS += -Wformat=2 -Wnested-externs
    TEST_CFLAGS := $(CFLAGS)
else
	CROSS_COMPILE = $(TOOLS)/gcc-arm-none-eabi-*/bin/arm-none-eabi-
    #GNU GCC
    AS		 = $(CROSS_COMPILE)as
    LD		 = $(CROSS_COMPILE)ld
    CC		 = $(CROSS_COMPILE)gcc
    CPP		 = $(CROSS_COMPILE)cpp
    AR		 = $(CROSS_COMPILE)ar
    NM		 = $(CROSS_COMPILE)nm
    LDR		 = $(CROSS_COMPILE)ldr
    STRIP	 = $(CROSS_COMPILE)strip
    OBJCOPY	 = $(CROSS_COMPILE)objcopy
    OBJDUMP	 = $(CROSS_COMPILE)objdump
    CPPCHECK = $(TOOLS)/cppcheck/cppcheck   

    SYSROOT  := $(shell $(CC) -print-sysroot)
    INCLUDE = -I$(SYSROOT)/usr/include \
              -I$(rootdir)/platform/ \
              -I$(rootdir)/platform/CMSIS/Include/ \
              -I$(rootdir)/platform/drivers/common/ \
              -I$(rootdir)/platform/devices/ \
			  -I$(rootdir)/platform/devices/$(DEVICE)/ \
			  -I$(rootdir)/platform/devices/MX8/ \
              -I$(rootdir)/platform/config/$(CONFIG)/ \
              -I$(rootdir)/platform/config/$(CONFIG)/$(UPPER_C) \
              -I$(rootdir)/platform/board/$(BOARD)
    ARCHFLAGS = -mcpu=cortex-m4 -mthumb -mfloat-abi=softfp -mfpu=fpv4-sp-d16 
    CFLAGS = $(INCLUDE) $(ARCHFLAGS) $(FLAGS) -ffunction-sections -std=c99 -mno-unaligned-access -pipe -fno-common -ffunction-sections -fdata-sections -ffreestanding -mapcs -std=c99 -fno-builtin -fshort-enums -DNDEBUG
    TEST_CFLAGS := $(CFLAGS)
    CFLAGS += $(CFLAG_LTO)
    AFLAGS = $(INCLUDE) $(ARCHFLAGS) $(FLAGS) -fno-common -ffunction-sections -fdata-sections -mno-unaligned-access -ffreestanding -fno-builtin -std=c99 -pipe
    LDFLAGS += -Wl,--defsym=__ram_vector_table__=1 -Wl,-Bstatic -Wl,--gc-sections -nostdlib
ifeq ($(D),1)
    LIB_C    := $(shell $(CC) $(ARCHFLAGS) --print-file-name=libc_nano.a)
    LIB_GCC  := $(shell $(CC) $(ARCHFLAGS) --print-file-name=libgcc.a)
	LCNANO := -lc_nano
else
	LCNANO :=
endif
    WARNS += -Wformat=0 -Wunreachable-code
endif

export $(CFLAGS)

# Define Components to build
SUBCOMPS := devices test

SUBCOMPS += main

ifeq ($(HW), REAL)
	SUBCOMPS += utilities
endif

# Define drivers, subsystems, and services to build for the SoC

include $(SRC)/config/$(CONFIG)/soc.bom

# Define drivers, subsystems, and services to build for the board

include $(SRC)/board/$(BOARD)/board.bom

SUBCOMPS += soc/$(SOC)

include $(foreach subcomp,$(SUBCOMPS),$(rootdir)/$(SRC)/$(subcomp)/Makefile)

ifeq ($(HW), REAL)
	DRV += $(DRV2)
endif

ifeq ($(DEVICE), ALL)
    DRV := $(shell find $(SRC)/drivers -name Makefile)
    SS := $(shell find $(SRC)/ss -name Makefile)
    BRD := $(shell find $(SRC)/board -name Makefile)
    SVC := $(shell find $(SRC)/svc -mindepth 1 -type d)
    DOX_API := $(foreach svc,$(SVC),$(svc)/api.h)
    include $(foreach drv,$(DRV),$(drv))
    include $(foreach ss,$(SS),$(ss))
    include $(foreach brd,$(BRD),$(brd))
    include $(foreach svc,$(SVC),$(svc)/Makefile)
else
    include $(foreach drv,$(DRV),$(SRC)/drivers/$(drv)/Makefile)
    include $(SRC)/ss/inf/Makefile
    include $(foreach ss,$(SS),$(rootdir)/$(SRC)/ss/$(ss)/Makefile)
    include $(SRC)/board/$(BOARD)/Makefile
    include $(foreach svc,$(SVC),$(rootdir)/$(SRC)/svc/$(svc)/Makefile)
    DOX_API := $(foreach svc,$(SVC),platform/svc/$(svc)/api.h)
    DOX_ADD := platform/config/$(CONFIG)
    DOX_ADD += $(foreach drv,$(DRV),platform/drivers/$(drv))
    DOX_ADD += $(foreach drv,$(DRV2),platform/drivers/$(drv))
	DOX_ADD += platform/drivers/snvs/doxygen//fsl_snvs.dox
	DOX_ADD += platform/drivers/snvs/fsl_snvs.h
	DOX_ADD += platform/drivers/seco/doxygen
	DOX_ADD += platform/drivers/seco/fsl_seco.h
    DOX_ADD += platform/ss/inf
    DOX_ADD += $(foreach ss,$(SS),platform/ss/$(ss))
    DOX_ADD += $(DOX_ADD_COMMON)
    DOX_ADD += platform/board/$(BOARD)
    DOX_ADD += $(foreach svc,$(SVC),platform/svc/$(svc))
endif

# Dox input files

DOX_SOC := doc/resources.md doc/clocks.md doc/controls.md doc/pads.md

ifdef EXP
	DOX_SOC += doc/memmap.md
	MD += doc/memmap.md
endif

ifeq ($(LOWER_T),all)
	TCM_LD = $(SRC)/devices/$(DEVICE)/linker/gcc/$(DEVICE)_overlay.ld
else
	TCM_LD = $(SRC)/devices/$(DEVICE)/linker/gcc/$(DEVICE)_tcm.ld
endif

ifdef NO_OVLY
	TCM_LD = $(SRC)/devices/$(DEVICE)/linker/gcc/$(DEVICE)_tcm.ld
endif

FUSE_DIRNAME=../test_files
ifneq "$(wildcard $(FUSE_DIRNAME) )" ""
	FIRST_R := $(shell echo $(UPPER_R) | cut -c1-1)
	FUSEFILES += $(shell find ../test_files/$(CONFIG)/fuses -name "*$(FIRST_R).txt")
	FUSELOGS += $(FUSEFILES:../test_files/$(CONFIG)/%=$(OUT)/%)
endif

CONFIG_DIRNAME=../test_files
ifneq "$(wildcard $(CONFIG_DIRNAME) )" ""
	CONFIGFILES += $(shell find ../test_files/$(CONFIG)/configs -name "*.txt")
	CONFIGLOGS += $(CONFIGFILES:../test_files/$(CONFIG)/%=$(OUT)/%)
endif

# Build rules

all : help

ifndef NO_IPC
    OBJS += ${RPCS} ${RPCL}
endif

CONFIGH+=$(SRC)/config/$(CONFIG)/pads.h
CONFIGH+=$(SRC)/config/$(CONFIG)/pad_data.h
CONFIGH+=$(SRC)/config/$(CONFIG)/pad_priority.h
TPROTOH+=$(SRC)/test/$(CONFIG)/test_all.c $(SRC)/test/common/test.h $(SRC)/devices/$(DEVICE)/linker/gcc/$(DEVICE)_overlay.ld
PAD_MD += $(SRC)/config/pad.txt
MEMMAP_MD += $(SRC)/devices/$(DEVICE)/memmap_md.txt

BIH+=$(SRC)/main/build_info.h

INCH+= $(SRC)/config/$(CONFIG)/all_api.h $(SRC)/config/$(CONFIG)/all_svc.h \
	$(SRC)/config/$(CONFIG)/all_ss.h $(SRC)/config/$(CONFIG)/all_config.h \
	$(SRC)/config/$(CONFIG)/ss_ver.h

MD+=doc/resources.md doc/clocks.md doc/controls.md doc/pads.md

.SECONDARY : ${INCH} ${INFH} ${CONFIGH} ${RPCH} ${RPCC} ${RPCHDR} ${RSRC_MD} ${CLK_MD} $(CTRL_MD) $(MEMMAP_MD) $(TPROTOH)

.INTERMEDIATE : ${MD} $(SRC)/test/board_system_config.c

.PRECIOUS : ${DCDH}

GCOV_OUT := $(OBJS:.o=.gcov)

# Include config makefiles
MAKERULE := $(shell find makefiles -name Makefile.rules)
include $(foreach makerule,$(MAKERULE),$(makerule))

$(OUT)/sc : ${OBJS}
	${CC} -o "$@" ${OBJS} ${LDFLAGS}

-include $(OBJS:.o=.d)

.SUFFIXES :

# Include pkit makefiles
MAKERULE2 := $(shell find makefiles -name Makefile.rules2)
include $(foreach makerule,$(MAKERULE2),$(makerule))

$(OUT)/board/board_common.o : $(SRC)/board/board_common.c ${INCH} ${INFH} ${CONFIGH} ${BIH} ${DCDH} $(SRC)/test/common/test.h
	@echo "Compiling $<"
	$(AT)${CC} ${CFLAGS} ${INCLUDE} -c $< -o $@

$(SRC)/board/$(CONFIG)_$(B)/dcd/%.h : $(SRC)/board/$(CONFIG)_$(B)/dcd/%.cfg
	@echo "Generating $@"
	$(AT)rm -f $(*F).temp
	$(AT)${CPP} -P -I$(SRC)/devices $(DCDFLAGS) $< $(*F).temp
	$(AT)sed -i -f bin/dcd.sed $(*F).temp
	$(AT)echo "#ifndef $(subst .,_,$(*F))_H" > $@
	$(AT)echo "#define $(subst .,_,$(*F))_H" >> $@
	$(AT)cat $(*F).temp >> $@
	$(AT)echo '#endif' >> $@
	$(AT)rm -f $(*F).temp

$(SRC)/board/$(CONFIG)_$(B)/dcd/dcd.h : $(SRC)/board/$(CONFIG)_$(B)/dcd/$(DDR_CON).h
	@echo "Generating $@ from $<"
	$(AT)rm -f $@
	$(AT)echo "#ifndef DCD_H" > $@
	$(AT)echo "#define DCD_H" >> $@
	$(AT)echo "#include \"$(DDR_CON).h\"" >> $@
	$(AT)echo '#endif' >> $@

$(SRC)/board/$(CONFIG)_$(B)/dcd/%_retention.h : $(SRC)/board/$(CONFIG)_$(B)/dcd/%.cfg
	@echo "Generating $@"
	$(AT)rm -f $(*F)_ret.temp
	$(AT)${CPP} -P -I$(SRC)/devices $(DCDFLAGS) $< $(*F)_ret.temp
	$(AT)sed -i -f bin/retention.sed $(*F)_ret.temp
	$(AT)echo "#ifndef $(subst .,_,$(*F))_RET_H" > $@
	$(AT)echo "#define $(subst .,_,$(*F))_RET_H" >> $@
	$(AT)cat $(*F)_ret.temp >> $@
	$(AT)echo '#endif' >> $@
	$(AT)rm -f $(*F)_ret.temp

$(SRC)/board/$(CONFIG)_$(B)/dcd/dcd_retention.h : $(SRC)/board/$(CONFIG)_$(B)/dcd/$(DDR_CON)_retention.h
	@echo "Generating $@ from $<"
	$(AT)rm -f $@
	$(AT)echo "#ifndef DDR_RETENTION_H" > $@
	$(AT)echo "#define DDR_RETENTION_H" >> $@
	$(AT)echo "#include \"$(DDR_CON)_retention.h\"" >> $@
	$(AT)echo '#endif' >> $@

$(OUT)/fuses/%.txt : $(OUT)/ $(OUT)/sc
	$(AT)mkdir -p $(OUT)/fuses
	$(OUT)/sc -r 0 -f ../test_files/$(CONFIG)/fuses/$*.txt 2> $(OUT)/fuses/$*.txt
	$(OUT)/sc -r 1 -f ../test_files/$(CONFIG)/fuses/$*.txt 2> $(OUT)/fuses/$*.txt

$(OUT)/configs/%.txt : $(OUT)/ $(OUT)/sc
	$(AT)mkdir -p $(OUT)/configs
	$(OUT)/sc -r 0 -c ../test_files/$(CONFIG)/configs/$*.txt 2> $(OUT)/configs/$*.txt
	$(OUT)/sc -r 1 -c ../test_files/$(CONFIG)/configs/$*.txt 2> $(OUT)/configs/$*.txt

$(OUT)/%.gcov : $(FUSELOGS) $(CONFIGLOGS)
	@echo "Coverage $@"
	$(AT)${GCOV} -p platform/$*.c -o $(OUT)/$*


$(OUT)/%.o : $(SRC)/%.S
	@echo "Assembling $<"
	$(AT)${CC} ${AFLAGS} ${INCLUDE} -c $< -o $@

$(OUT)/scfw.bin : $(OUT)/scfw.elf
	@echo "Objcopy $@ ...."
	$(AT)$(OBJCOPY) -O binary --gap-fill 0x0 $(OUT)/scfw.elf $(OUT)/scfw.bin --strip-debug

$(OUT)/scfw_tcm.bin : $(OUT)/scfw_tcm.elf
	@echo "Objcopy $@ ...."
	$(AT)$(OBJCOPY) -O binary --gap-fill 0x0 -R .test* $(OUT)/scfw_tcm.elf $(OUT)/scfw_tcm.bin --strip-debug

$(OUT)/scfw_tests.bin : $(OUT)/scfw_tcm.elf
	@echo "Objcopy $@ ...."
	$(AT)$(OBJCOPY) -O binary --gap-fill 0x0 -j .test* $(OUT)/scfw_tcm.elf $(OUT)/scfw_tests.bin --strip-debug

$(OUT)/scfw.hex : $(OUT)/scfw.srec
	@echo "Generating hex $@ ...."
	@-rm -f $(OUT)/scfw.hex
	$(AT)$(SREC_CAT) $(OUT)/scfw.srec -byte_swap 4 -crop 0x00000000 0xffffffff -offset $(SREC_OFFSET) -Line_Length=82 -output $(OUT)/scfw.hex -vmem 32

$(OUT)/scfw_tcm.hex : $(OUT)/scfw_tcm.srec
	@echo "Generating hex $@ ...."
	$(AT)$(SREC_CAT) $(OUT)/scfw_tcm.srec -byte_swap 4 -crop 0x00000000 0xffffffff -offset 0x11000000 -Line_Length=82 -output $(OUT)/scfw_tcm.hex -vmem 32

$(OUT)/scfw_tests.hex : $(OUT)/scfw_tests.srec
	@echo "Generating hex $@ ...."
	$(AT)$(SREC_CAT) $(OUT)/scfw_tests.srec -byte_swap 4 -crop 0x00000000 0xffffffff -offset 0x11000000 -Line_Length=82 -output $(OUT)/scfw_tests.hex -vmem 32

$(OUT)/scfw.srec : $(OUT)/scfw.elf
	@echo "Generating srec $@ ...."
	$(AT)-rm -f $(OUT)/scfw_fb.bin
	$(AT)$(OBJCOPY) -O srec --gap-fill 0x0 --pad-to 0x10000 $(OUT)/scfw.elf $(OUT)/scfw.srec --strip-debug

$(OUT)/scfw_tcm.srec : $(OUT)/scfw_tcm.elf
	@echo "Objcopy $@ ...."
	$(AT)$(OBJCOPY) -O srec --gap-fill 0x0 -R .test* --pad-to 0x30000 $(OUT)/scfw_tcm.elf $(OUT)/scfw_tcm.srec --strip-debug

$(OUT)/scfw_tests.srec : $(OUT)/scfw_tcm.elf
	@echo "Objcopy $@ ...."
	$(AT)$(OBJCOPY) -O srec --gap-fill 0x0 -j .test* --pad-to 0x30000 $(OUT)/scfw_tcm.elf $(OUT)/scfw_tests.srec --strip-debug

$(OUT)/scfw.elf : $(OUT)/devices/$(DEVICE)/gcc/startup_$(DEVICE).o $(SRC)/devices/$(DEVICE)/linker/gcc/$(DEVICE)_flash.ld
	@echo "Linking $@ ...."
	$(AT)$(CC) $(ARCHFLAGS) $(LDFLAGS) -o $@ -T $(SRC)/devices/$(DEVICE)/linker/gcc/$(DEVICE)_flash.ld $(OUT)/devices/$(DEVICE)/gcc/startup_$(DEVICE).o -lgcc -Wl,-Map=$(OUT)/scfw.map

$(OUT)/scfw_tcm.elf : $(OBJS) $(TCM_LD)
	@echo "Linking $@ ...."
	$(AT)$(CC) $(ARCHFLAGS) $(LDFLAGS) -o $@ -T $(TCM_LD) $(OBJS) $(LCNANO) -lgcc -Wl,-Map=$(OUT)/scfw_tcm.map

$(OUT)/ : FORCE
	$(AT)-rm -f log.txt
	$(AT)-mkdir -p $(DIRS)

FORCE:

help :
	@/bin/echo -e "Usage: make TARGET OPTIONS\n"
	@/bin/echo -e "Targets:"
	@/bin/echo -e "\thelp           : display help text"
	@/bin/echo -e "\tclean          : remove all build files\n"
	@/bin/echo -en $(HELP)
	@/bin/echo -e "\nOptions:"
	@/bin/echo -e "\tV=0            : quite output (default)"
	@/bin/echo -e "\tV=1            : verbose output"
	@/bin/echo -e "\tD=0            : configure for no debug"
	@/bin/echo -e "\tD=1            : configure for debug (default)"
	@/bin/echo -e "\tDL=<level>     : configure debug level (0-5)"
	@/bin/echo -e "\tB=<board>      : configure board (default=val)"
	@/bin/echo -e "\tU=<uart>       : configure debug UART (default=0)"
	@/bin/echo -e "\tDDR_CON=<file> : specify DDR config file"
	@/bin/echo -e "\tR=<srev>       : silicon revision (default=A0)"
	@/bin/echo -e "\tM=0            : no debug monitor (default)"
	@/bin/echo -e "\tM=1            : include debug monitor"
	@/bin/echo -e "\tLTO=0          : build without link-time optimization (default)"
	@/bin/echo -e "\tLTO=1          : build with link-time optimization"
	@/bin/echo -e "\tT=<test>       : run tests rather than boot next core"
	@/bin/echo -en $(HELP_OPT)