# =============================================================================
# \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

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

LIB_TARGET := $(BUILD_DIR)/libutils.a

# =============================================================================
# Documentation generation
# =============================================================================

DOCCONF := doxygen.config
DOCDIR  := doc

# =============================================================================
# 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

# =============================================================================
# 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)

doc: $(DOCCONF)
	doxygen $<

$(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

$(BUILD_DIR):
	$(MKDIR) $@

clean:
	$(RM) $(BUILD_DIR)

clean-doc:
	$(RM) $(DOCDIR)

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

distclean: clean clean-doc clean-log
