#############################################################################
#                  ALMOS-MKH Makefile for BOOTLOADER TSAR                   #
#############################################################################

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

-include ../../params-hard.mk

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

# Macros to be processed by the C preprocessor.
MACROS		 = -DARCHINFO_PATHNAME="\"arch_info.bin\""		\
			   -DKERNEL_PATHNAME="\"/bin/kernel/kernel.elf\""

# Objects to be linked for boot.elf generation
BOOT_OBJS 	= build/boot.o            \
		build/boot_fat32.o      \
		build/boot_utils.o      \
		build/boot_entry.o      \
              	build/boot_tty_driver.o \
              	build/boot_hba_driver.o \
		build/boot_bdv_driver.o \
		build/boot_mmc_driver.o	

# List of directories to be searched for included files
# when compiling for boot.elf generation
BOOT_INCLUDE = -I.  \
		-I../../tools/arch_info/        \
		-I$(HAL_ARCH)/core   \
		-I../.. \
		-I$(KERNEL)

# Rule to generate boot.elf, kernel.elf, ksh.elf, sort.elf and update virtual disk.
compile: dirs                     \
         build/boot.elf

# Rule to create the build directories.
dirs:
	@mkdir -p build

clean:
	rm -rf build

../../hard_config.h:
	$(MAKE) -C ../../ hard_config.h

################################################
# Rules to compile boot drivers used by boot.elf
build/boot_tty_driver.o:	boot_tty_driver.c \
								boot_tty_driver.h \
								boot_utils.h      \
								boot_config.h     \
								$(HAL_ARCH)/core/hal_kernel_types.h        \
								../../hard_config.h
	$(CC) $(BOOT_INCLUDE) $(CFLAGS) -c -o $@ $<
	$(DU) -D $@ > $@.txt

build/boot_bdv_driver.o:	boot_bdv_driver.c \
								boot_bdv_driver.h \
								boot_utils.h      \
								boot_config.h     \
								$(HAL_ARCH)/core/hal_kernel_types.h        \
								../../hard_config.h
	$(CC) $(BOOT_INCLUDE) $(CFLAGS) -c -o $@ $<
	$(DU) -D $@ > $@.txt

build/boot_hba_driver.o:	boot_hba_driver.c \
								boot_hba_driver.h \
								boot_utils.h      \
								boot_config.h     \
								$(HAL_ARCH)/core/hal_kernel_types.h        \
								../../hard_config.h
	$(CC) $(BOOT_INCLUDE) $(CFLAGS) -c -o $@ $<
	$(DU) -D $@ > $@.txt

build/boot_mmc_driver.o:	boot_mmc_driver.c \
								boot_mmc_driver.h \
								boot_utils.h      \
								boot_config.h     \
								$(HAL_ARCH)/core/hal_kernel_types.h        \
								../../hard_config.h
	$(CC) $(BOOT_INCLUDE) $(CFLAGS) -c -o $@ $<
	$(DU) -D $@ > $@.txt

##############################
# Rule to compile boot_fat32.o
build/boot_fat32.o: boot_fat32.c      \
								boot_fat32.h      \
								boot_utils.h      \
								boot_config.h     \
								$(HAL_ARCH)/core/hal_kernel_types.h        \
								../../hard_config.h			
	$(CC) $(BOOT_INCLUDE) $(CFLAGS) -c -o $@ $<
	$(DU) -D $@ > $@.txt

##############################
# Rule to compile boot_utils.o
build/boot_utils.o: boot_utils.c       \
								boot_utils.h       \
								boot_tty_driver.h  \
								$(HAL_ARCH)/core/hal_kernel_types.h         \
								../../hard_config.h
	$(CC) $(BOOT_INCLUDE) $(CFLAGS) -c -o $@ $<
	$(DU) -D $@ > $@.txt

########################
# Rule to compile boot.o
build/boot.o:				boot.c             \
								boot_utils.h       \
								boot_fat32.h       \
								boot_tty_driver.h  \
								boot_hba_driver.h  \
								boot_bdv_driver.h  \
								boot_mmc_driver.h  \
								boot_config.h      \
								$(HAL_ARCH)/core/hal_kernel_types.h         \
								../../hard_config.h				
	$(CC) $(BOOT_INCLUDE) $(CFLAGS) $(MACROS) -c -o $@ $<
	$(DU) -D $@ > $@.txt

##############################
# Rule to compile boot_entry.o
build/boot_entry.o: boot_entry.S   \
								boot_config.h  \
								../../hard_config.h
	$(CC) $(BOOT_INCLUDE) $(CFLAGS) -c -o $@ $<
	$(DU) -D $@ > $@.txt

#####################################################################
# Rule to generate boot.elf
build/boot.elf: $(BOOT_OBJS) boot.ld
	$(LD) -o $@ -T boot.ld $(BOOT_OBJS)
	$(DU) -D $@ > $@.txt
