#############################################################################
#                  HAL  Makefile for x86_64                                 #
#############################################################################

-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/ioc_ata.o     \
               $(HAL_ARCH)/build/drivers/pic_apic.o    \
               $(HAL_ARCH)/build/drivers/txt_rs232.o

CORE_OBJS    = \
              $(HAL_ARCH)/build/core/hal_boot.o         \
              $(HAL_ARCH)/build/core/hal_smpboot.o      \
              $(HAL_ARCH)/build/core/hal_init.o         \
              $(HAL_ARCH)/build/core/hal_cpu.o          \
              $(HAL_ARCH)/build/core/hal_kentry.o       \
              $(HAL_ARCH)/build/core/hal_acpi.o         \
              $(HAL_ARCH)/build/core/hal_apic.o         \
              $(HAL_ARCH)/build/core/x86_printf.o       \
              $(HAL_ARCH)/build/core/hal_drivers.o      \
              $(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_exception.o    \
              $(HAL_ARCH)/build/core/hal_interrupt.o    \
              $(HAL_ARCH)/build/core/hal_syscall.o      \


HAL_INCLUDE =     -I$(KERNEL) \
                 -Idrivers \
                 -I$(HAL)/generic             \
                 -Icore    \
                 -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

# 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:       drivers/%.c \
                                drivers/%.h \
                                $(KERNEL)/kernel_config.h             \
                                core/hal_kernel_types.h
	$(CC) $(HAL_INCLUDE) $(CFLAGS)  -c -o $@ $<

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

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

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

$(HAL_ARCH)/build/core/hal_init.o:    core/hal_init.c         \
                                core/hal_boot.h         \
                                core/hal_multiboot.h    \
                                core/hal_segmentation.h \
                                core/hal_acpi.h         \
                                core/hal_apic.h         \
                                core/hal_internal.h     \
                                $(KERNEL)/kernel_config.h               \
                                core/hal_kernel_types.h
	$(CC) $(HAL_INCLUDE) $(CFLAGS) -c -o $@ $<

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

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

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

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

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

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