#############################################################################
#                       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			        \
        hard_config.h           \
		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/rpc.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_vmm.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_thread_yield.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_cancel.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_exit.o            \
              build/kernel/syscalls/sys_munmap.o          \
              build/kernel/syscalls/sys_open.o            \
              build/kernel/syscalls/sys_mmap.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_isatty.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_wait.o 

SYS_OBJS_4  = build/kernel/syscalls/sys_get_config.o      \
              build/kernel/syscalls/sys_get_core.o        \
              build/kernel/syscalls/sys_get_cycle.o       \
              build/kernel/syscalls/sys_display.o         \
              build/kernel/syscalls/sys_thread_sleep.o    \
              build/kernel/syscalls/sys_thread_wakeup.o   \
              build/kernel/syscalls/sys_trace.o           \
              build/kernel/syscalls/sys_fg.o 

VFS_OBJS    = build/kernel/fs/vfs.o              \
              build/kernel/fs/fatfs.o            \
              build/kernel/fs/devfs.o            \
              build/kernel/fs/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/fs               \
                 -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 boot.elf, kernel.elf, ksh.elf, sort.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 \
         build/user/ksh/ksh.elf   \
         build/user/sort/sort.elf \
         build/user/pgcd/pgcd.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/fs
	@mkdir -p build/kernel/syscalls
	@mkdir -p build/user
	@mkdir -p build/user/ksh
	@mkdir -p build/user/sort
	@mkdir -p build/user/pgcd
	@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 ../..
	cd user/ksh   &&  $(MAKE) clean  &&  cd ../..
	cd user/sort  &&  $(MAKE) clean  &&  cd ../..
	cd user/pgcd  &&  $(MAKE) clean  &&  cd ../..

####################################################	
# Rule for building a new virtual disk from scratch.
# It creates the bin, bin/kernel, bin/user, 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/user    || 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/generic/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/fs objects
build/kernel/fs/%.o:            kernel/fs/%.c               \
                                kernel/fs/%.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
# TODO the syscalls grouped in SYS_OBJS_2 must be introduced
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)               \
                                $(SYS_OBJS_4)               \
								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) $(SYS_OBJS_4) -lgcc

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

###############################################################
# Rules to generate various user .elf and copy 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/user 
  
build/user/ksh/ksh.elf : user/ksh/ksh.c 
	$(MAKE) -C user/ksh 
	mcopy -o -i $(DISK_IMAGE) user/ksh/ksh.elf ::/bin/user 
  
build/user/sort/sort.elf : user/sort/sort.c 
	$(MAKE) -C user/sort 
	mcopy -o -i $(DISK_IMAGE) user/sort/sort.elf ::/bin/user 

build/user/pgcd/pgcd.elf : user/pgcd/pgcd.c 
	$(MAKE) -C user/pgcd 
	mcopy -o -i $(DISK_IMAGE) user/pgcd/pgcd.elf ::/bin/user 

