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

-include params.mk

# Compilation flags.
CFLAGS = -Wall -ffreestanding -mno-gpopt -mips32 -g -O2 \
	 	 -fno-delete-null-pointer-checks
#	 	 -fno-delete-null-pointer-checks -std=c99

export
CC = mipsel-unknown-elf-gcc
AS = mipsel-unknown-elf-as
LD = mipsel-unknown-elf-ld
DU = mipsel-unknown-elf-objdump
AR = mipsel-unknown-elf-ar

LIBGCC = -L$(shell $(CC) -print-libgcc-file-name | sed 's/libgcc.a//')
# Default values for hardware parameters.
# These parameters should be defined in the 'params.mk' file.
ARCH		?= /users/alain/soc/tsar-trunk-svn-2013/platforms/tsar_generic_iob
X_SIZE		?= 2
Y_SIZE		?= 2
NB_PROCS	?= 2
NB_TTYS		?= 3
FBF_WIDTH	?= 256
IOC_TYPE	?= IOC_BDV


# Checking hardware platform definition.
ifeq ($(wildcard $(ARCH)),)
$(error Please define in ARCH parameter the path to the hardware platform!)
endif

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

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

# List of directories to be searched for included files
# when compiling for boot.elf generation
BOOT_INCLUDE = -Itools/bootloader_tsar  \
		       -Itools/arch_info        \
               -Ihal/tsar_mips32/core   \
               -I.

# Objects to be linked for the soclib drivers library
DRIVERS_OBJS = build/kernel/drivers/soclib_tty.o  \
               build/kernel/drivers/soclib_bdv.o  \
               build/kernel/drivers/soclib_hba.o  \
               build/kernel/drivers/soclib_mmc.o  \
               build/kernel/drivers/soclib_pic.o  \
               build/kernel/drivers/soclib_nic.o  \
               build/kernel/drivers/soclib_dma.o  \
               build/kernel/drivers/soclib_iob.o

# Objects to be linked for kernel.elf generation
KERN_OBJS	= build/kernel/kern/kernel_init.o     \
              build/kernel/kern/printk.o          \
              build/kernel/kern/thread.o          \
              build/kernel/kern/process.o         \
              build/kernel/kern/chdev.o           \
              build/kernel/kern/cluster.o         \
              build/kernel/kern/scheduler.o       \
              build/kernel/kern/core.o            \
              build/kernel/kern/dqdt.o            \
              build/kernel/kern/do_syscall.o      \
              build/kernel/kern/do_interrupt.o    \
              build/kernel/kern/do_exception.o    \
              build/kernel/kern/rpc.o             \
              build/kernel/kern/signal.o

HAL_OBJS    = build/kernel/hal/hal_special.o      \
              build/kernel/hal/hal_context.o      \
              build/kernel/hal/hal_atomic.o       \
              build/kernel/hal/hal_remote.o       \
              build/kernel/hal/hal_uspace.o       \
              build/kernel/hal/hal_irqmask.o      \
              build/kernel/hal/hal_gpt.o          \
              build/kernel/hal/hal_ppm.o          \
              build/kernel/hal/hal_exception.o    \
              build/kernel/hal/hal_interrupt.o    \
              build/kernel/hal/hal_syscall.o      \
              build/kernel/hal/hal_drivers.o      \
              build/kernel/hal/hal_kentry.o       \
              build/kernel/hal/hal_switch.o

DEV_OBJS    = build/kernel/devices/dev_txt.o      \
              build/kernel/devices/dev_ioc.o      \
              build/kernel/devices/dev_mmc.o      \
              build/kernel/devices/dev_nic.o      \
              build/kernel/devices/dev_pic.o      \
              build/kernel/devices/dev_dma.o      \
              build/kernel/devices/dev_fbf.o      \
              build/kernel/devices/dev_iob.o

MM_OBJS     = build/kernel/mm/ppm.o               \
              build/kernel/mm/vmm.o               \
              build/kernel/mm/vseg.o              \
              build/kernel/mm/page.o              \
              build/kernel/mm/kcm.o               \
              build/kernel/mm/khm.o               \
              build/kernel/mm/mapper.o            \
              build/kernel/mm/kmem.o

LIBK_OBJS   = build/kernel/libk/grdxt.o           \
              build/kernel/libk/bits.o            \
              build/kernel/libk/elf.o             \
              build/kernel/libk/string.o          \
              build/kernel/libk/ctype.o           \
              build/kernel/libk/rwlock.o          \
              build/kernel/libk/spinlock.o        \
              build/kernel/libk/barrier.o         \
              build/kernel/libk/remote_barrier.o  \
              build/kernel/libk/remote_spinlock.o \
              build/kernel/libk/remote_rwlock.o   \
              build/kernel/libk/remote_fifo.o     \
              build/kernel/libk/remote_mutex.o    \
              build/kernel/libk/remote_sem.o      \
              build/kernel/libk/remote_condvar.o  \
              build/kernel/libk/memcpy.o          \
              build/kernel/libk/htab.o            \
              build/kernel/libk/xhtab.o

SYS_OBJS_0  = build/kernel/syscalls/sys_thread_exit.o     \
              build/kernel/syscalls/sys_mmap.o            \
              build/kernel/syscalls/sys_thread_create.o   \
              build/kernel/syscalls/sys_thread_join.o     \
              build/kernel/syscalls/sys_thread_detach.o   \
              build/kernel/syscalls/sys_thread_yield.o    \
              build/kernel/syscalls/sys_sem.o             \
              build/kernel/syscalls/sys_condvar.o         \
              build/kernel/syscalls/sys_barrier.o         \
              build/kernel/syscalls/sys_mutex.o

SYS_OBJS_1  = build/kernel/syscalls/sys_thread_sleep.o    \
              build/kernel/syscalls/sys_thread_wakeup.o   \
              build/kernel/syscalls/sys_open.o            \
              build/kernel/syscalls/sys_creat.o           \
              build/kernel/syscalls/sys_read.o            \
              build/kernel/syscalls/sys_write.o           \
              build/kernel/syscalls/sys_lseek.o           \
              build/kernel/syscalls/sys_close.o           \
              build/kernel/syscalls/sys_unlink.o          \
              build/kernel/syscalls/sys_pipe.o

SYS_OBJS_2  = build/kernel/syscalls/sys_chdir.o           \
              build/kernel/syscalls/sys_mkdir.o           \
              build/kernel/syscalls/sys_mkfifo.o          \
              build/kernel/syscalls/sys_opendir.o         \
              build/kernel/syscalls/sys_readdir.o         \
              build/kernel/syscalls/sys_closedir.o        \
              build/kernel/syscalls/sys_getcwd.o          \
              build/kernel/syscalls/sys_clock.o           \
              build/kernel/syscalls/sys_alarm.o           \
              build/kernel/syscalls/sys_rmdir.o

SYS_OBJS_3  = build/kernel/syscalls/sys_utls.o            \
              build/kernel/syscalls/sys_chmod.o           \
              build/kernel/syscalls/sys_signal.o          \
              build/kernel/syscalls/sys_timeofday.o       \
              build/kernel/syscalls/sys_kill.o            \
              build/kernel/syscalls/sys_getpid.o          \
              build/kernel/syscalls/sys_fork.o            \
              build/kernel/syscalls/sys_exec.o            \
              build/kernel/syscalls/sys_stat.o            \
              build/kernel/syscalls/sys_trace.o

VFS_OBJS    = build/kernel/vfs/vfs.o              \
              build/kernel/vfs/fatfs.o            \
              build/kernel/vfs/devfs.o            \
              build/kernel/vfs/ramfs.o

# List of directories to be searched for included files
# when compiling for kernel.elf generation
KERNEL_INCLUDE = -Ikernel                \
                 -Ikernel/kern           \
                 -Ikernel/devices        \
                 -Ikernel/syscalls       \
                 -Ihal/tsar_mips32/drivers \
                 -Ikernel/syscalls       \
                 -Ikernel/libk           \
                 -Ikernel/mm             \
                 -Ikernel/vfs            \
                 -Ikernel/fatfs          \
                 -Ikernel/sysfs          \
                 -Ikernel/ramfs          \
                 -Ikernel/devfs          \
                 -Itools/arch_info       \
                 -Ihal/generic           \
                 -Ihal/tsar_mips32/core  \
                 -I.

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

# Virtual disk path
DISK_IMAGE	:= hdd/virt_hdd.dmg

# The Mtools used to build the FAT32 disk image perfom a few sanity checks, to
# make sure that the disk is indeed an MS-DOS disk. However, the size of the
# disk image used by ALMOS-VM is not MS-DOS compliant.
# Setting this variable prevents these checks.
MTOOLS_SKIP_CHECK := 1

# Rule to generate compile boot.elf, kernel.elf, init.elf and update virtual disk.
compile: dirs                     \
         build_disk               \
         hard_config.h            \
		 build/boot/boot.elf      \
         build/kernel/kernel.elf  \
         build/user/init/init.elf \
         list

# Rule to create the build directories.
dirs:
	@mkdir -p build/boot
	@mkdir -p build/kernel
	@mkdir -p build/kernel/kern
	@mkdir -p build/kernel/hal
	@mkdir -p build/kernel/devices
	@mkdir -p build/kernel/mm
	@mkdir -p build/kernel/libk
	@mkdir -p build/kernel/drivers
	@mkdir -p build/kernel/vfs
	@mkdir -p build/kernel/syscalls
	@mkdir -p build/user
	@mkdir -p build/user/init
	@mkdir -p build/user/ksh
	@mkdir -p hdd

# Rule to make a recursive list of the virtual disk content.
list:
	mdir -/ -b -i $(DISK_IMAGE) ::/

##############################################################
# Rule to copy the files generated by the virtual prototype
# from the virtual disk 'home' directory to the current directory.
extract:
	mcopy -o -i $(DISK_IMAGE) ::/home .

# Rules to delete all binary files from Unix File System
# without modifying the virtual disk.
clean:
	rm -f *.o *.elf *.bin *.txt core
	rm -f hard_config.h arch_info.xml arch_info.bin
	rm -rf build
	cd user/init   &&  $(MAKE) clean  &&  cd ../..

####################################################	
# Rule for building a new virtual disk from scratch.
# It creates the bin, bin/kernel, bin/init, and home directories.
# This requires the generic Linux/MacOS 'create_dmg' script, that should be
# placed in the same directory as this Makefile.
build_disk:
	rm -f $(DISK_IMAGE)
	./create_dmg  	create $(basename $(DISK_IMAGE))
	dd 				if=$(DISK_IMAGE) of=temp.dmg count=65536
	mv 				temp.dmg $(DISK_IMAGE)
	mmd 			-o -i $(DISK_IMAGE) ::/bin         || true
	mmd 			-o -i $(DISK_IMAGE) ::/bin/kernel  || true
	mmd 			-o -i $(DISK_IMAGE) ::/bin/init    || true
	mmd 			-o -i $(DISK_IMAGE) ::/home        || true
	mdir		 -/ -b -i $(DISK_IMAGE) ::/
	
##############################################################
# Rules to generate hardware description files (hard_config.h,
# arch_info.bin and arch_info.xml), and update the virtual disk.
hard_config.h: $(ARCH)/arch_info.py
	tools/arch_info/genarch.py	--arch=$(ARCH)			\
								--x_size=$(X_SIZE)		\
								--y_size=$(Y_SIZE)		\
								--nb_cores=$(NB_PROCS)	\
								--nb_ttys=$(NB_TTYS)	\
								--fbf_size=$(FBF_WIDTH)	\
								--ioc_type=$(IOC_TYPE)	\
								--hard=.				\
								--bin=.					\
								--xml=.					
	mcopy -o -i $(DISK_IMAGE) arch_info.bin ::/   || true
	mdir		 -/ -b -i $(DISK_IMAGE) ::/

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

build/boot/boot_bdv_driver.o:	tools/bootloader_tsar/boot_bdv_driver.c \
								tools/bootloader_tsar/boot_bdv_driver.h \
								tools/bootloader_tsar/boot_utils.h      \
								tools/bootloader_tsar/boot_config.h     \
								hal/tsar_mips32/core/hal_types.h        \
								hard_config.h
	$(CC) $(BOOT_INCLUDE) $(CFLAGS) -c -o $@ $<
	$(DU) -D $@ > $@.txt

build/boot/boot_hba_driver.o:	tools/bootloader_tsar/boot_hba_driver.c \
								tools/bootloader_tsar/boot_hba_driver.h \
								tools/bootloader_tsar/boot_utils.h      \
								tools/bootloader_tsar/boot_config.h     \
								hal/tsar_mips32/core/hal_types.h        \
								hard_config.h
	$(CC) $(BOOT_INCLUDE) $(CFLAGS) -c -o $@ $<
	$(DU) -D $@ > $@.txt

build/boot/boot_mmc_driver.o:	tools/bootloader_tsar/boot_mmc_driver.c \
								tools/bootloader_tsar/boot_mmc_driver.h \
								tools/bootloader_tsar/boot_utils.h      \
								tools/bootloader_tsar/boot_config.h     \
								hal/tsar_mips32/core/hal_types.h        \
								hard_config.h
	$(CC) $(BOOT_INCLUDE) $(CFLAGS) -c -o $@ $<
	$(DU) -D $@ > $@.txt

##############################
# Rule to compile boot_fat32.o
build/boot/boot_fat32.o:	 	tools/bootloader_tsar/boot_fat32.c      \
								tools/bootloader_tsar/boot_fat32.h      \
								tools/bootloader_tsar/boot_utils.h      \
								tools/bootloader_tsar/boot_config.h     \
								hal/tsar_mips32/core/hal_types.h        \
								hard_config.h			
	$(CC) $(BOOT_INCLUDE) $(CFLAGS) -c -o $@ $<
	$(DU) -D $@ > $@.txt

##############################
# Rule to compile boot_utils.o
build/boot/boot_utils.o:		tools/bootloader_tsar/boot_utils.c       \
								tools/bootloader_tsar/boot_utils.h       \
								tools/bootloader_tsar/boot_tty_driver.h  \
								hal/tsar_mips32/core/hal_types.h         \
								hard_config.h
	$(CC) $(BOOT_INCLUDE) $(CFLAGS) -c -o $@ $<
	$(DU) -D $@ > $@.txt

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

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

#####################################################################
# Rule to generate boot.elf and place it in sector #2 of virtual disk
build/boot/boot.elf:				$(BOOT_OBJS) \
									tools/bootloader_tsar/boot.ld					
	$(LD) -o $@ -T tools/bootloader_tsar/boot.ld $(BOOT_OBJS)
	$(DU) -D $@ > $@.txt
	dd if=build/boot/boot.elf of=$(DISK_IMAGE) seek=2 conv=notrunc


##############################
# rules to compile the drivers
build/kernel/drivers/%.o:       hal/tsar_mips32/drivers/%.c \
                                hal/tsar_mips32/drivers/%.h \
                                kernel_config.h             \
                                hal/tsar_mips32/core/hal_types.h
	$(CC) $(KERNEL_INCLUDE) $(CFLAGS)  -c -o $@ $<

#######################################
# Rules to generate kernel/kern objects
build/kernel/kern/%.o:		    kernel/kern/%.c             \
                                kernel/kern/%.h             \
                                kernel_config.h             \
                                hal/tsar_mips32/core/hal_types.h
	$(CC) $(KERNEL_INCLUDE) $(CFLAGS) -c -o $@ $<

######################################
# Rules to generate kernel/hal objects
build/kernel/hal/%.o:           hal/tsar_mips32/core/%.c    \
                                hal/generic/%.h             \
                                kernel_config.h             \
                                hal/tsar_mips32/core/hal_types.h
	$(CC) $(KERNEL_INCLUDE) $(CFLAGS) -c -o $@ $<

build/kernel/hal/hal_kentry.o:  hal/tsar_mips32/core/hal_kentry.S \
                                hal/tsar_mips32/core/hal_kentry.h \
                                kernel_config.h                   \
                                hal/tsar_mips32/core/hal_types.h
	$(CC) $(KERNEL_INCLUDE) $(CFLAGS) -c -o $@ $<

build/kernel/hal/hal_switch.o:  hal/tsar_mips32/core/hal_switch.S \
                                hal/tsar_mips32/core/hal_switch.h
	$(CC) $(KERNEL_INCLUDE) $(CFLAGS) -c -o $@ $<
	$(DU) -D $@ > $@.txt

######################################
# Rules to generate kernel/dev objects
build/kernel/devices/%.o:       kernel/devices/%.c          \
                                kernel/devices/%.h          \
                                kernel_config.h             \
                                hal/tsar_mips32/core/hal_types.h
	$(CC) $(KERNEL_INCLUDE) $(CFLAGS) -c -o $@ $<

#####################################
# Rules to generate kernel/mm objects
build/kernel/mm/%.o:            kernel/mm/%.c               \
                                kernel/mm/%.h               \
                                kernel_config.h             \
                                hal/tsar_mips32/core/hal_types.h
	$(CC) $(KERNEL_INCLUDE) $(CFLAGS) -c -o $@ $<

#######################################
# Rules to generate kernel/libk objects
build/kernel/libk/%.o:          kernel/libk/%.c             \
                                kernel/libk/%.h             \
                                kernel_config.h             \
                                hal/tsar_mips32/core/hal_types.h
	$(CC) $(KERNEL_INCLUDE) $(CFLAGS) -c -o $@ $<

###########################################
# Rules to generate kernel/syscalls objects
build/kernel/syscalls/%.o:      kernel/syscalls/%.c         \
                                kernel/syscalls/syscalls.h  \
                                kernel_config.h             \
                                hal/tsar_mips32/core/hal_types.h
	$(CC) $(KERNEL_INCLUDE) $(CFLAGS) -c -o $@ $<

#######################################
# Rules to generate kernel/vfs objects
build/kernel/vfs/%.o:           kernel/vfs/%.c              \
                                kernel/vfs/%.h              \
                                kernel_config.h             \
                                hal/tsar_mips32/core/hal_types.h
	$(CC) $(KERNEL_INCLUDE) $(CFLAGS) -c -o $@ $<

###########################################################
# Rule to generate kernel.elf and place it on virtual disk
build/kernel/kernel.elf:	    $(KERN_OBJS)                \
                                $(HAL_OBJS)                 \
                                $(DEV_OBJS)                 \
                                $(MM_OBJS)                  \
                                $(LIBK_OBJS)                \
                                $(DRIVERS_OBJS)             \
                                $(VFS_OBJS)                 \
                                $(SYS_OBJS_0)               \
                                $(SYS_OBJS_1)               \
                                $(SYS_OBJS_2)               \
                                $(SYS_OBJS_3)               \
								hal/tsar_mips32/kernel.ld
	$(LD) -o $@ -T hal/tsar_mips32/kernel.ld $(LIBGCC)      \
          $(KERN_OBJS) $(HAL_OBJS) $(DEV_OBJS) $(MM_OBJS)   \
          $(LIBK_OBJS) $(DRIVERS_OBJS) $(VFS_OBJS)          \
          $(SYS_OBJS_0) $(SYS_OBJS_1) $(SYS_OBJS_2)         \
          $(SYS_OBJS_3) -lgcc

	$(DU) -D $@ > $@.txt
	mcopy -o -i $(DISK_IMAGE) build/kernel/kernel.elf ::/bin/kernel

############################################################
# Rule to generate the init.elf and place it on virtual disk
build/user/init/init.elf : user/init/init.c 
	$(MAKE) -C user/init 
	mcopy -o -i $(DISK_IMAGE) user/init/init.elf ::/bin/init 
  

