# Makefile
#
# Copyright (C) 2026 wolfSSL Inc.
#
# This file is part of wolfBoot.
#
# wolfBoot is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# wolfBoot is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA

CC=arm-none-eabi-gcc
OBJCOPY ?= arm-none-eabi-objcopy

CFLAGS := -mcpu=cortex-m33 -mthumb -mcmse -Os -ffreestanding -fdata-sections -ffunction-sections -g -ggdb
CFLAGS += -I. -I../common -I../../../include -DEMU_STM32
CFLAGS += -DIMAGE_HEADER_SIZE=$(IMAGE_HEADER_SIZE)
LDFLAGS := -nostdlib -T target.ld -Wl,-gc-sections
LDLIBS := -Wl,--start-group -lc -lm -lgcc -lnosys -Wl,--end-group
NSC_OBJ :=
ifeq ($(TZEN),1)
  CFLAGS += -DTZEN
  CFLAGS += -DWOLFCRYPT_SECURE_MODE
  NSC_OBJ := ../../../src/wolfboot_tz_nsc.o
endif

VPATH := ../common

COMMON_SRCS := emu_ivt.c emu_startup.c emu_syscalls.c emu_update.c emu_hal.c
LIBWOLFBOOT_SRC := ../../../src/libwolfboot.c
SRCS := $(COMMON_SRCS) uart.c
OBJS := $(SRCS:.c=.o) libwolfboot.o $(NSC_OBJ)

all: app.bin

app.elf: $(OBJS) target.ld
	$(CC) $(CFLAGS) $(OBJS) $(LDFLAGS) $(LDLIBS) -o $@

app.bin: app.elf
	$(OBJCOPY) -O binary $< $@

%.o: %.c
	$(CC) $(CFLAGS) -c $< -o $@

libwolfboot.o: $(LIBWOLFBOOT_SRC)
	$(CC) $(CFLAGS) -c $< -o $@

clean:
	rm -f $(SRCS:.c=.o) libwolfboot.o app.elf app.bin

.PHONY: all clean
