#############################################################################
#                       ALMOS-MKH  Makefile for x86_64                      #
#############################################################################

-include params.mk

# Compilation flags.
CFLAGS = -Wall -ffreestanding -mno-red-zone -mno-mmx -mno-sse -mno-avx -g \
	 	 -fno-delete-null-pointer-checks -mcmodel=large

# Export all variables to applications sub-Makefile.
export
CC = gcc
AS = as
LD = ld
DU = objdump
AR = ar

# 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/kernel/kernel.elf	

# Objects to be linked for the soclib drivers library
DRIVERS_OBJS = build/kernel/drivers/ioc_ata.o     \
               build/kernel/drivers/pic_apic.o    \
               build/kernel/drivers/txt_rs232.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_boot.o         \
              build/kernel/hal/hal_smpboot.o      \
              build/kernel/hal/hal_init.o         \
              build/kernel/hal/hal_cpu.o          \
              build/kernel/hal/hal_kentry.o       \
              build/kernel/hal/hal_acpi.o         \
              build/kernel/hal/hal_apic.o         \
              build/kernel/hal/x86_printf.o       \
              build/kernel/hal/hal_drivers.o      \
              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

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/x86_64/drivers    \
                 -Ikernel/syscalls       \
                 -Ikernel/libk           \
                 -Ikernel/mm             \
                 -Ikernel/vfs            \
                 -Ikernel/fatfs          \
                 -Ikernel/sysfs          \
                 -Ikernel/ramfs          \
                 -Ikernel/devfs          \
                 -Itools/arch_info       \
                 -Ihal/generic           \
                 -Ihal/x86_64/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 compile kernel.elf, and update virtual disk.
compile: dirs			            \
		 hard_config.h              \
         build/kernel/kernel.elf    \
         list

# Rule to create the build directories.
dirs:
	@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 hdd

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

# Rule to make a file system check for the virtual disk.
fsck:
	fsck.msdos $(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
	rm -rf build

####################################################	
# Rule for building a new virtual disk from scratch.
# It creates the bin / 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) ::/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:
	echo "!!! UNUSED !!!" # XXX: cleanup later


##############################
# rules to compile the drivers
build/kernel/drivers/%.o:       hal/x86_64/drivers/%.c   \
                                hal/x86_64/drivers/%.h   \
                                kernel_config.h             \
                                hal/x86_64/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/x86_64/core/hal_types.h
	$(CC) $(KERNEL_INCLUDE) $(CFLAGS) -c -o $@ $<

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

build/kernel/hal/hal_boot.o:    hal/x86_64/core/hal_boot.S \
                                hal/x86_64/core/hal_boot.h        \
                                hal/x86_64/core/hal_multiboot.h   \
                                hal/x86_64/core/hal_segmentation.h \
                                kernel_config.h              \
                                hal/x86_64/core/hal_types.h
	$(CC) $(KERNEL_INCLUDE) $(CFLAGS) -c -o $@ $<

build/kernel/hal/hal_smpboot.o: hal/x86_64/core/hal_smpboot.S \
                                hal/x86_64/core/hal_boot.h        \
                                hal/x86_64/core/hal_multiboot.h   \
                                hal/x86_64/core/hal_segmentation.h \
                                kernel_config.h              \
                                hal/x86_64/core/hal_types.h
	$(CC) $(KERNEL_INCLUDE) $(CFLAGS) -c -o $@ $<

build/kernel/hal/hal_init.o:    hal/x86_64/core/hal_init.c         \
                                hal/x86_64/core/hal_boot.h         \
                                hal/x86_64/core/hal_multiboot.h    \
                                hal/x86_64/core/hal_segmentation.h \
                                hal/x86_64/core/hal_acpi.h         \
                                hal/x86_64/core/hal_apic.h         \
                                hal/x86_64/core/hal_internal.h     \
                                kernel_config.h               \
                                hal/x86_64/core/hal_types.h
	$(CC) $(KERNEL_INCLUDE) $(CFLAGS) -c -o $@ $<

build/kernel/hal/hal_cpu.o:     hal/x86_64/core/hal_cpu.S \
                                hal/x86_64/core/hal_boot.h        \
                                hal/x86_64/core/hal_segmentation.h \
                                kernel_config.h              \
                                hal/x86_64/core/hal_types.h
	$(CC) $(KERNEL_INCLUDE) $(CFLAGS) -c -o $@ $<

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

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

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

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

######################################
# Rules to generate kernel/dev objects
build/kernel/devices/%.o:       kernel/devices/%.c          \
                                kernel/devices/%.h          \
                                kernel_config.h             \
                                hal/x86_64/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/x86_64/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/x86_64/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/x86_64/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/x86_64/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/x86_64/kernel.ld
	$(LD) -o $@ -z max-page-size=0x1000 -T hal/x86_64/kernel.ld \
          $(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)

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

