#############################################################################
#                  HAL Makefile for Tsar-MIPS32                             #
#############################################################################

-include ../../params-soft.mk
ifeq ($(ARCH_NAME),)
$(error Please define in ARCH_NAME parameter in params-soft.mk!)
endif

LIBGCC = -L$(shell $(CC) -print-libgcc-file-name | sed 's/libgcc.a//') #todo ?

# Rules that don't build target files
# always out-of-date, need to be regenerated everytime they are called
.PHONY: compile			        \
		dirs				    \
		clean				    \

DRIVERS_OBJS = $(HAL_ARCH)/build/drivers/soclib_tty.o  \
               $(HAL_ARCH)/build/drivers/soclib_mtty.o \
               $(HAL_ARCH)/build/drivers/soclib_bdv.o  \
               $(HAL_ARCH)/build/drivers/soclib_hba.o  \
               $(HAL_ARCH)/build/drivers/soclib_sdc.o  \
               $(HAL_ARCH)/build/drivers/soclib_spi.o  \
               $(HAL_ARCH)/build/drivers/soclib_mmc.o  \
               $(HAL_ARCH)/build/drivers/soclib_pic.o  \
               $(HAL_ARCH)/build/drivers/soclib_nic.o  \
               $(HAL_ARCH)/build/drivers/soclib_dma.o  \
               $(HAL_ARCH)/build/drivers/soclib_iob.o

CORE_OBJS    = $(HAL_ARCH)/build/core/hal_special.o      \
               $(HAL_ARCH)/build/core/hal_context.o      \
               $(HAL_ARCH)/build/core/hal_atomic.o       \
               $(HAL_ARCH)/build/core/hal_remote.o       \
               $(HAL_ARCH)/build/core/hal_uspace.o       \
               $(HAL_ARCH)/build/core/hal_irqmask.o      \
               $(HAL_ARCH)/build/core/hal_gpt.o          \
               $(HAL_ARCH)/build/core/hal_ppm.o          \
               $(HAL_ARCH)/build/core/hal_vmm.o          \
               $(HAL_ARCH)/build/core/hal_exception.o    \
               $(HAL_ARCH)/build/core/hal_interrupt.o    \
               $(HAL_ARCH)/build/core/hal_syscall.o      \
               $(HAL_ARCH)/build/core/hal_drivers.o      \
               $(HAL_ARCH)/build/core/hal_kentry.o       \
               $(HAL_ARCH)/build/core/hal_switch.o       \
               $(HAL_ARCH)/build/core/hal_user.o

HAL_INCLUDE =     -I$(KERNEL) \
                 -I$(HAL_ARCH)/drivers \
                 -I$(HAL)/generic             \
                 -I$(HAL_ARCH)/core    \
                 -I../../tools/arch_info         \
                 -I$(KERNEL)/kern \
                 -I$(KERNEL)/mm   \
                 -I$(KERNEL)/fs   \
                 -I$(KERNEL)/syscalls   \
                 -I$(SHARED_INCLUDE)  \
                 -I$(KERNEL)/devices    \
                 -I$(KERNEL)/libk \
                 -I../../ # for hard_config.h (needed by soclib_sdc_driver for SYS_CLK)

##############################
# Rule to generate .o for HAL.
compile: dirs $(CORE_OBJS) $(DRIVERS_OBJS)

########################################
# Rule to create the build directories.
dirs:
	@mkdir -p $(HAL_ARCH)/build
	@mkdir -p $(HAL_ARCH)/build/drivers
	@mkdir -p $(HAL_ARCH)/build/core


##############################
# rules to compile the drivers
$(HAL_ARCH)/build/drivers/%.o:  $(HAL_ARCH)/drivers/%.c      \
                                $(HAL_ARCH)/drivers/%.h      \
                                $(KERNEL)/kernel_config.h    \
                                $(HAL_ARCH)/core/hal_kernel_types.h
	$(CC) $(HAL_INCLUDE) $(CFLAGS)  -c -o $@ $<

######################################
# Rules to generate kernel/hal objects
$(HAL_ARCH)/build/core/%.o:     $(HAL_ARCH)/core/%.c         \
                                $(HAL)/generic/%.h           \
                                $(KERNEL)/kernel_config.h    \
                                $(HAL_ARCH)/core/hal_kernel_types.h
	$(CC) $(HAL_INCLUDE) $(CFLAGS) -c -o $@ $<

$(HAL_ARCH)/build/core/hal_kentry.o:  $(HAL_ARCH)/core/hal_kentry.S \
                                      $(HAL_ARCH)/core/hal_kentry.h \
                                      $(KERNEL)/kernel_config.h     \
                                      $(HAL_ARCH)/core/hal_kernel_types.h
	$(CC) $(HAL_INCLUDE) $(CFLAGS) -c -o $@ $<

$(HAL_ARCH)/build/core/hal_switch.o:  $(HAL_ARCH)/core/hal_switch.S \
                                      $(HAL)/generic/hal_switch.h
	$(CC) $(HAL_INCLUDE) $(CFLAGS) -c -o $@ $<
	$(DU) -D $@ > $@.txt

clean:
	rm -rf $(HAL_ARCH)/build/*
