.DELETE_ON_ERROR:

#########################################################################
#									#
#									#
#  Makefile for the QEMU version of Xinu, using as much as possible	#
#			from the Galileo code				#
#									#
#  includes: .deps and .defs						#
#									#
#									#
#########################################################################

QEMU_CMD      = qemu-system-i386

# Set COMPILER_ROOT based on the existence of pre-built binaries
TOOLCHAIN_BIN := $(CURDIR)/.toolchain/bin

USE_PREBUILT := $(wildcard $(TOOLCHAIN_BIN)/i686-elf-gcc)
ifdef XINU_SKIP_PREBUILT_TOOLCHAIN
USE_PREBUILT :=
endif

ifeq ($(USE_PREBUILT),)
	# Use system compiler
	COMPILER_ROOT ?= i686-linux-gnu-
else
	# Use the pre-built binaries
	COMPILER_ROOT := $(TOOLCHAIN_BIN)/i686-elf-
	export PATH := $(TOOLCHAIN_BIN):$(PATH)
endif

PNAME		=	QEMU
PLAT		=	Platform_$(PNAME)
TOPDIR		=	..

CC		=	${COMPILER_ROOT}gcc
LD		=	${COMPILER_ROOT}ld
OBJCOPY		=	${COMPILER_ROOT}objcopy
XINU		=	$(TOPDIR)/compile/xinu.elf
XINUBIN		=	$(TOPDIR)/compile/xinu.bin
XINUXBIN	=	$(TOPDIR)/compile/xinu
MAKEXBIN 	=	$(TOPDIR)/compile/bin/buildxbin
BUILDMAKE	=	$(TOPDIR)/compile/bin/build-make

MAKEDEP		=	$(CC) -M -MG

DEPSFILE	=	.deps
DEFSFILE	=	.defs
VERSIONFILE	=	version
LDSCRIPT	=	ld.script
MKVERS		=	$(TOPDIR)/compile/bin/mkvers
REBUILDFLAGS	=	-s $(TOPDIR)/system  debug.c 	\
			-s $(TOPDIR)/lib			\
			-s $(TOPDIR)/device/tty			\
			-s $(TOPDIR)/device/nam			\
			-s $(TOPDIR)/device/eth			\
			-s $(TOPDIR)/device/rds			\
			-s $(TOPDIR)/device/ram			\
			-s $(TOPDIR)/device/lfs			\
			-s $(TOPDIR)/device/rfs			\
			-s $(TOPDIR)/device/pipe		\
			-s $(TOPDIR)/net  'arp_dump*'  		\
				'dhcp_dump*'  pxe.c		\
			-s $(TOPDIR)/shell  'xsh_rdstest*'

INCLUDE		=	-I$(TOPDIR)/include

# Amount to move loaded image down in memory

BRELOC  =	0x150000
BOOTPLOC=	0x150000

DEFS		= -DBRELOC=${BRELOC} -DBOOTPLOC=${BOOTPLOC} -DBSDURG -DVERSION=\""`cat $(VERSIONFILE)`"\"

# Compiler flags
CFLAGS  = -march=i586 -m32 -fno-builtin -fno-stack-protector -nostdlib -c -Wall -O0 ${DEFS} ${INCLUDE}
SFLAGS  = ${INCLUDE}

# Loader flags
LDFLAGS = -dn -m elf_i386 -Map xinu.map -T $(LDSCRIPT)

all:	xinu

#--------------------------------------------------------------------------------
# Handle generation of a new version string when initialize is recompiled
#--------------------------------------------------------------------------------

newversion:
	@echo creating new version
	@$(MKVERS) $(PNAME) > $(VERSIONFILE)

#-------------------------------------------------------------------------------------------
# Skip remaking .defs when the goal is `clean` or `setup` - gawk binary might not exist yet
#-------------------------------------------------------------------------------------------

ifneq ($(MAKECMDGOALS),setup)
ifneq ($(MAKECMDGOALS),clean)
-include $(DEFSFILE)
endif
endif

#--------------------------------------------------------------------------------
# Add files that require special rules and place start.o at the front
#--------------------------------------------------------------------------------
LD_LIST = binaries/start.o $(filter-out binaries/start.o,$(OBJ_FILES))

#------------------------------------------------------------
# Rules for files that need special handling
#------------------------------------------------------------

# Define variables for the Configuration file and generated files

CONFFILE	= $(TOPDIR)/config/Configuration
CONFH		= $(TOPDIR)/include/conf.h
CONFC		= $(TOPDIR)/system/conf.c

# Define variables for the config program and its sources

CONFPGM		= $(TOPDIR)/config/config
CONFL		= $(TOPDIR)/config/config.l
CONFY		= $(TOPDIR)/config/config.y

# Set up the required build directory structure

BLDDIRS	= binaries
export

#--------------------------------------------------------------------------------
# Specific additional rules and exceptions
#--------------------------------------------------------------------------------

# The following inserts 'echo' when run on xinu00
ECHO$(MACHINE) = echo; echo "On QEMU, the following command would be run:  "

xinu:	check-toolchain Makefile rebuild $(BLDDIRS) $(DEFSFILE) $(DEPSFILE) $(CONFH) $(CONFC) $(LD_LIST)
	@echo;echo 'Loading object files to produce GRUB bootable xinu'
	@$(LD) $(LDFLAGS) $(LD_LIST) -o $(XINU)
	

$(BLDDIRS): 
	@mkdir -p $(BLDDIRS)

objects: $(LD_LIST)

$(CONFH): $(CONFFILE) $(CONFPGM)
	@echo making $(CONFH)
	@make configure

$(CONFC): $(CONFFILE) $(CONFPGM)
	@echo making $(CONFC)
	@make configure

$(CONFPGM): $(CONFL) $(CONFY)
	@echo making the config program
	@make -C $(TOPDIR)/config clean all install

configure:
	@echo forcing a rebuild of conf.h and conf.c
	@make newversion
	@(cd $(TOPDIR)/config; make install)

clean:
	@echo removing .o files
	@rm -f binaries/*.o
	@echo   removing configuration files ...
	@rm -f $(CONFH) $(CONFC)
	@(cd $(TOPDIR)/config; make clean)
	@echo removing xinu ...
	@rm -f $(XINU)
	@rm -f $(XINUXBIN)

#--------------------------------------------------------------------------------
# Locations of source directories and exceptions (.c and .[sS] files to exclude)
#--------------------------------------------------------------------------------
	
$(DEFSFILE):
	@rm -f $(DEFSFILE)
	@echo "" > $(DEFSFILE)
	@make rebuild

$(DEPSFILE):
	@rm -f $(DEPSFILE)
	@echo "" > $(DEPSFILE)
	@make depend

rebuild: $(CONFC)
	@echo Rebuilding the $(DEFSFILE) file
	@$(BUILDMAKE) $(REBUILDFLAGS) > $(DEFSFILE)

defclean:
	rm -f $(DEFSFILE)
	echo "" > $(DEFSFILE)

depend: $(DEFSFILE)
	@echo;echo Making all dependencies in $(DEPSFILE)
	@$(MAKEDEP) ${INCLUDE} ${SRC_FULL} > $(DEPSFILE)
	@echo;echo Finished making dependencies

depclean:
	rm -f $(DEPSFILE)
	echo "" > $(DEPSFILE)

#-------------------------------------------------------
# Include .deps unless we are running `setup` or `clean`
#-------------------------------------------------------

ifneq ($(MAKECMDGOALS),setup)
ifneq ($(MAKECMDGOALS),clean)
-include $(DEPSFILE)
endif
endif

#-------------------------------------------
# Following are specific to the QEMU version
#-------------------------------------------
setup:
	@chmod +x setup-toolchain.sh
	@./setup-toolchain.sh $(if $(FORCE),--force)

check-toolchain:
	@command -v $(CC) >/dev/null 2>&1 || { \
		echo "error: compiler '$(CC)' not found."; \
		echo "  If you're using pre-built binaries, run 'make setup' first."; \
		echo "  If you're compiling natively, make sure it's installed and on your PATH."; \
		exit 1; \
		}

run:
	$(QEMU_CMD) -nographic \
		-kernel $(XINU) \
		-netdev user,id=mynetdev \
		-device e1000-82545em,netdev=mynetdev
