# =============================================================================
# \file      Makefile
# \author    Cesar Fuguet
# \date      July 9, 2014
# =============================================================================
include ../common.mk

# =============================================================================
# Include files paths
# =============================================================================

CONFDIR ?= output
CONF    := $(addprefix $(CONFDIR)/config/, hard_config.h)

INCLUDE += -I$(CONFDIR)/config

# =============================================================================
# Utils library
# =============================================================================

LIB_SRCS   := stdio.c \
              string.c \
              tty.c \
              simhelper.c \
			  xcu.c

LIB_OBJS   := $(addprefix $(BUILD_DIR)/,\
                  $(subst .c,.o, $(notdir $(LIB_SRCS))))

LIB_TARGET := $(BUILD_DIR)/libutils.a

# =============================================================================
# Object files
# =============================================================================

S_SRCS := reset.S
C_SRCS := main.c

OBJS   := $(addprefix $(BUILD_DIR)/,\
              $(subst .c,.o, $(notdir $(C_SRCS))) \
              $(subst .S,.o, $(notdir $(S_SRCS))))

TARGET := $(BUILD_DIR)/soft.elf

# =============================================================================
# Test variables
# =============================================================================
PLATFORM  := ../../../../../platforms/tsar_generic_iob
SIMULATOR := $(PLATFORM)/simul.x

# =============================================================================
# Makefile rules
# =============================================================================

all: $(TARGET)

# Specific dependencies
# NOTE: normal prerequisites | order-only prerequisites
# SEE: http://www.gnu.org/software/make/manual/make.html#Prerequisite-Types

$(LIB_OBJS): $(CONF) | $(BUILD_DIR)
$(OBJS): $(CONF) | $(BUILD_DIR)

$(LIB_TARGET): $(LIB_OBJS)
	$(AR) rcs $@ $(LIB_OBJS)

$(TARGET): $(OBJS) ../soft.ld $(LIB_TARGET)
	$(LD) -o $@ -T ../soft.ld $(OBJS) -L$(BUILD_DIR) -lutils
	$(DU) -D $@ > $@.txt

mkconfig $(SIMULATOR) $(CONF): $(PLATFORM)/scripts/onerun.py
	./$< -o $(CONFDIR) -p $(PLATFORM) -x3 -y3 -n1 -c

$(BUILD_DIR):
	$(MKDIR) $@

clean:
	$(RM) $(BUILD_DIR)

clean-log:
	$(RM) $(CONFDIR)

.PHONY: $(SIMULATOR) mkconfig

distclean: clean clean-log
