# let the user have a default configuration (ie for PLATFORM_DIR and SOCLIB)
-include ./build.mk

MAKECMDGOALS ?= none
RAMDISK      ?= 0
SOCLIB       ?= 0

ifneq ($(MAKECMDGOALS),$(filter $(MAKECMDGOALS),clean distclean clean-doc doc))
  ifndef PLATFORM_DIR
    $(error please define PLATFORM_DIR 'make PLATFORM_DIR=foo')
  else
    ifeq ($(SOCLIB),1)
      ifeq ($(RAMDISK),1)
        DEFS+= -DUSE_RDK
      else
        DEFS+= -DUSE_BDV
      endif
      DTS=platform_soclib.dts
      $(info Make for $(PLATFORM_DIR), SocLib variant)
    else
      DEFS+= -DUSE_SPI
      DTS=platform_fpga.dts
      $(info Make for $(PLATFORM_DIR), FPGA variant)
    endif
  endif
endif

LD         := mipsel-unknown-elf-ld
CC         := mipsel-unknown-elf-gcc
AS         := mipsel-unknown-elf-as
DU         := mipsel-unknown-elf-objdump
RM         := rm -rf
ECHO       := @echo
MKDIR      := mkdir
DTC        := dtc
HEXDUMP    := hexdump
DOXYGEN    := doxygen

BUILD_DIR  := build
SRCS_DIR   := src
INCS_DIR   := include

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

INCLUDE    += -I. -I$(INCS_DIR) -I$(PLATFORM_DIR)

# =============================================================================
# Paths of sources in another directories
# =============================================================================

VPATH      += $(SRCS_DIR)
VPATH      += $(PLATFORM_DIR)

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

CFLAGS     := -Wall                \
              -mno-gpopt           \
              -ffreestanding       \
              -fomit-frame-pointer \
              -mips32              \
              -ggdb                \
              -mlong-calls         \
              -O2                  \
              -Werror

C_SRCS     := reset_elf_loader.c \
              reset_ioc.c        \
              reset_utils.c      \
              reset_tty.c        \
              reset_exception.c

ifeq ($(SOCLIB),0)
  C_SRCS   += sdcard.c spi.c
endif

S_SRCS     := reset.S

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

TARGET     := preloader.elf

USE_DT     ?= 1

all: $(TARGET)

$(BUILD_DIR)/version.o: $(BUILD_DIR) $(OBJS) version version.sh
	$(ECHO) "[version.sh]"
	./version.sh > $(BUILD_DIR)/version.c
	$(ECHO) "[   CC    ]     $(BUILD_DIR)/version.c"
	$(CC) $(DEFS) $(CFLAGS) $(INCLUDE) -c -o $@ $(BUILD_DIR)/version.c
	$(DU) -D $@ > $@.txt

$(TARGET): $(BUILD_DIR) $(BUILD_DIR)/version.o $(OBJS) $(PLATFORM_DIR)/ldscript $(BUILD_DIR)/platform.ld
	$(ECHO) "[   LD    ]     $@"
	$(LD) -o $@ -T $(PLATFORM_DIR)/ldscript $(OBJS) $(BUILD_DIR)/version.o
	$(DU) -D $@ > $@.txt

ifeq ($(USE_DT), 1)
$(BUILD_DIR)/platform.ld: $(BUILD_DIR)/platform.dtb
	$(ECHO) "[ HEXDUMP ]     $(notdir $<)"
	$(HEXDUMP) -v -e '"BYTE(0x" 1/1 "%02X" ")\n"' $< > $@
else
$(BUILD_DIR)/platform.ld:
	$(ECHO) "[  TOUCH  ]     $(notdir $@)"
	touch $@
endif

$(BUILD_DIR)/platform.dtb: $(DTS)
	$(ECHO) "[   DTC   ]     $(notdir $<)"
	${DTC} -O dtb -o $@ $< &> /dev/null

$(BUILD_DIR):
	$(MKDIR) $@

doc: Doxyfile
	$(DOXYGEN) Doxyfile

clean:
	$(RM) $(TARGET).txt $(TARGET) *~ $(BUILD_DIR)

clean-doc:
	$(RM) doc

distclean: clean clean-doc

# =============================================================================
# Implicit makefile rules

$(BUILD_DIR)/%.o: %.c
	$(ECHO) "[   CC    ]     $(notdir $<)"
	$(CC) $(DEFS) $(CFLAGS) $(INCLUDE) -c -o $@ $<
	$(DU) -D $@ > $@.txt

$(BUILD_DIR)/%.o: %.S
	$(ECHO) "[   AS    ]     $(notdir $<)"
	$(CC) $(DEFS) ${INCLUDE} -g -mips32 -c -o $@ $<
	$(DU) -D $@ > $@.txt

.SILENT:
