########################################################################
#                                                                      #
# Standard rulesets for use when compiling the ELKS kernel.            #
#                                                                      #
########################################################################

ifndef TOPDIR
$(error TOPDIR is not defined)
endif

include $(TOPDIR)/Make.defs

########################################################################
#
# This file expects the following variables to have been defined before
# it is included into any other Makefile on this system:
#
#	BASEDIR 	The relative path from the directory the Makefile
#			is in to the base directory. This is used quite
#			extensively through these definitions, so must be
#			correct.
#
#	USEBCC		Whether to compile using ia16-elf-gcc or host GCC.
#			If this is set to 'N' it compiles using GCC, otherwise
#			it compiles using ia16-elf-gcc.
#
#	CLEANDEP	Any files in the current directory that the
#			`make dep` command is dependent on.
#
#	CLEANME 	Any files in the current directory to be removed
#			by the `make clean` command that are not in the
#			standard list of files matching "*~",
#			"*.a", "*.o", "*.map" or "*.tmp" or named "core".
#			Also removed are any files matching "*.s" for
#			which a "*.c" or "*.S" file also exists.
#
#	DEPEND		Any files in the current directory that the
#			`make dep` command is dependent on.
#
#	DISTFILES	Any files in the current directory that are to
#			be included in a distribution made with the
#			`make dist` command that are not in the standard
#			list. This standard list consists of those files
#			NOT removed by `make clean` that match any of
#			the patterns "*.?", "*.cc", "*.html", "*.in",
#			"*.sh", "*.tk", "*.txt", "Makefile*" or "README*".
#
#	NOINDENT	Any files in the current directory that are NOT
#			to be processed in a `make indent` run. Usually,
#			these files include sections of assembly code,
#			which gets mangled by the `indent` command.
#
# No other variables should be defined before including this file.
#
#########################################################################
# State the current version of this system.

VERSION     = 0	# (0-255)
PATCHLEVEL  = 9	# (0-255)
SUBLEVEL    = 2	# (0-255)
PRE         =

#########################################################################
# Specify the architecture we will use.

ARCH		= i86

#########################################################################
# Specify the target for the netboot image.

TARGET_NB_IMAGE	= $(TOPDIR)/elks/nbImage

#########################################################################
#									#
#   From here onwards, it is not normally necessary to edit this file   #
#									#
#########################################################################
# Define variables directly dependant on the current version number.

ifeq ($(PRE), )

DIST		?= $(shell printf '%u.%u.%u' \
			$(VERSION) $(PATCHLEVEL) $(SUBLEVEL))

else

DIST		?= $(shell printf '%u.%u.%u%s' \
			$(VERSION) $(PATCHLEVEL) $(SUBLEVEL) $(PRE))

endif

#########################################################################
# Specify the various directories.

ARCH_DIR	= arch/$(ARCH)

INCLUDES = -I$(TOPDIR)/include -I$(TOPDIR)/elks/include

#########################################################################
# Specify the standard definitions to be given to system programs.

CCDEFS = -D__KERNEL__

#########################################################################
# Set the target environment and the compiler to use

# Not linked to any target C library ('freestanding')
# Use ES only for segment ('elks')
# Small memory model: CS != DS == SS
# No relocation stuff related to MSDOS 'MZ' executable

CROSS_CC = ia16-elf-gcc
CROSS_CFLAGS = -ffreestanding -fno-inline -melks -mcmodel=small -msegment-relocation-stuff

#########################################################################
# Define architecture-specific flags.

#MK_ARCH	= $(shell grep '^CONFIG_ARCH_' $(TOPDIR)/.config \
			| cut -d = -f 1 | cut -d _ -f 3- | tr A-Z a-z)
#ifeq ($(MK_ARCH), ibmpc)
    ARCH_AS	=
    ARCH_CC	=
    ARCH_LD	=
#endif

#########################################################################
# Define CPU-specific flags.

#MK_CPU		= $(shell grep '^CONFIG_CPU_' $(TOPDIR)/.config \
			| cut -d = -f 1 | cut -d _ -f 3- | tr A-Z a-z)
#ifeq ($(MK_CPU), 8086)
    CPU_AS	= -mtune=i8086
ifdef CONFIG_ARCH_SWAN
    CPU_CC	= -march=v30mz -mtune=v30mz
else
    CPU_CC	= -mtune=i8086
endif
    CPU_LD	=
#endif

#ifeq ($(MK_CPU), 80186)
#    CPU_AS	= -mtune=i186
#    CPU_CC	= -mtune=i186
#    CPU_LD	=
#endif

#ifeq ($(MK_CPU), 80286)
#    CPU_AS	= -mtune=i286
#    CPU_CC	= -mtune=i286
#    CPU_LD	=
#endif

#########################################################################
# Export all variables.

.EXPORT_ALL_VARIABLES:

#########################################################################
# Specify the tools to use, with their flags.

#########################################################################
# Common definitions

CC_PROTO	= gcc $(INCLUDES) -M -MG $(CCDEFS)
CFLBASE 	= $(INCLUDES) $(CCDEFS)
CPP		= $(CC) $(INCLUDES) -E $(CCDEFS) -ansi
LINT		= splint
LINTLEVEL	= -weak
LCFLAGS 	= $(INCLUDES) $(CCDEFS)
LHFLAGS 	= $(LCFLAGS) -exportfcn -declundef -fcnuse -fielduse \
		  -exportheader -varuse -exporttype -exportconst \
		  -exportvar -typeuse -enummemuse -constuse

ifneq ($(USEBCC), N)

#########################################################################
# Definitions using ia16-elf-gcc compiler

AS      = ia16-elf-as
ASFLAGS = $(CPU_AS) $(ARCH_AS) --32-segelf
CC      = $(CROSS_CC)
CFLAGS  = $(CROSS_CFLAGS) $(CPU_CC) $(ARCH_CC) $(CFLBASE) -Os
CFLAGS += -fno-delete-null-pointer-checks -Wnull-dereference
CFLAGS += -Wall -W -Wmissing-prototypes -Wpointer-arith -Wreturn-type
CFLAGS += -Wredundant-decls -Wuninitialized -Winline -Wswitch -Wshadow
CFLAGS += -Wchar-subscripts -Wwrite-strings
CFLAGS += -Wdiscarded-qualifiers -Wcast-align -Wcast-qual

# uncomment for strong checking, won't work with CONFIG_FARTEXT_KERNEL
#CFLAGS += -std=gnu99 -Wpedantic -D__STRICT_ANSI__

# TODO: remove/add the following
#CFLAGS += -Wbad-function-cast
CFLAGS += -Wno-sign-compare
CFLAGS += -Wno-cast-qual
CFLAGS += -Wno-unused-parameter
CFLAGS += -Wno-missing-prototypes
CFLAGS += -Wno-empty-body
#CFLAGS += -Wstrict-prototypes
#CFLAGS += -Wconversion
CFLAGS += -Wextra -Wtype-limits
#CFLAGS += -fno-defer-pop

LD      = ia16-elf-ld.gold
LDFLAGS = $(CPU_LD) -s
POSTLINK = elf2elks
NM      = ia16-elf-nm
AR      = ia16-elf-ar

INCLUDES += -I$(TOPDIR)/libc/include

else

#########################################################################
# Definitions using GCC host compiler

AS      = as
ASFLAGS =
CC      = gcc
CFLAGS  = -Wall $(CFLBASE)
LD      = ld
LDFLAGS =
AR      = ar

endif

#########################################################################
# general construction rules

.S.s: ;

.S.o:
	$(CC) -E -traditional $(INCLUDES) $(CCDEFS) -o $*.tmp $<
	$(AS) $(ASFLAGS) -o $*.o $*.tmp
	rm $*.tmp

.s.o:
	$(AS) $(ASFLAGS) -o $*.o $<

ifneq ($(USEBCC), N)
.c.o:
	$(CC) $(CFLAGS) -c -o $*.o $<
endif

#########################################################################
# Default target, to allow standard targets to be included. This simply
# allows `make elks` to be called from any directory in the tree, and
# results in `make all` being called in the root directory of the tree.

elks:
	${MAKE} -C $(BASEDIR) all

#########################################################################
# Standard commands.

dokclean: $(CLEANDEP)
	rm -f *~ *.a *.map *.o *.tmp $(CLEANME)
	@for DIR in */ ; do \
		if [ -f "$$DIR/Makefile" -a "$$DIR" != "tools" ]; then \
			${MAKE} -C "$$DIR" dokclean ; \
		fi ; \
	done

doclean: $(CLEANDEP)
	rm -f *~ *.a *.bak *.lint *.map *.o *.tmp core $(CLEANME)
#	@for FILE in *.s ; do \
#		BASE=`basename "$$FILE" .s` ; \
#		if [ -f "$$BASE.c" -o -f "$$BASE.S" ]; then \
#			echo rm -f "$$FILE" ; \
#			rm -f "$$FILE" ; \
#		fi ; \
#	done
	@for DIR in */ ; do \
		if [ -f "$$DIR/Makefile" ]; then \
			${MAKE} -C "$$DIR" doclean ; \
		fi ; \
	done

dodep:	$(DEPEND)
	@if \ls *.c > /dev/null 2>&1 ; then \
		${MAKE} mkdep ; \
	fi
	@for DIR in */ ; do \
		if [ -f "$$DIR/Makefile" ]; then \
			${MAKE} -C "$$DIR" dodep ; \
		fi ; \
	done

mkdep:
	@sed '/\#\#\# Dependencies:/q' < Makefile > make.tmp
	@echo >> make.tmp
	@for FILE in *.c ; do \
		$(CC_PROTO) "$${FILE}" >> make.tmp ; \
		printf '\n\n' >> make.tmp ; \
	done
	@if ! diff Makefile make.tmp > /dev/null ; then \
		echo -n 'Assigning dependencies: ' ; \
		mv -fv make.tmp Makefile ; \
	else \
		rm -f make.tmp ; \
	fi

nodep:
	sed '/\#\#\# Dependencies:/q' < Makefile > make.tmp
	@if ! diff Makefile make.tmp > /dev/null ; then \
		echo -n 'Removing dependencies: ' ; \
		mv -fv make.tmp Makefile ; \
	else \
		rm -f make.tmp ; \
	fi
	@for DIR in */ ; do \
		if [ -f "$$DIR/Makefile" ]; then \
			${MAKE} -C "$$DIR" nodep ; \
		fi ; \
	done

doindent:
	$(BASEDIR)/scripts/reindent $(NOINDENT)

indent:	doindent
	@for DIR in */ ; do \
		if [ -f "$$DIR/Makefile" ]; then \
			${MAKE} -C "$$DIR" indent ; \
		fi ; \
	done

#########################################################################
# Specify the directory we are to create the distribution copy of this
# directory in, in such a way that no . or .. directories occur. This
# also makes this an absolute path, but that fact is not important.

#MYDIR		= $(shell pwd | sed 's:^$(TOPDIR):.:')
#DISTDIR 	= $(shell $(BASEDIR)/scripts/setdir $(TOPDIR)/elks-$(DIST)/$(MYDIR))
#MKDIST	= $(shell export allow_null_glob_expansion=Y ; \
	  echo *.? *.cc *.html *.in *.png *.sh *.tk *.txt Makefile* README*)

# Sub-command used by the `make dist` command in the toplevel Makefile.
# NOTE: disabled as slows general make times greatly

#mkdist:
#	mkdir -m 755 -p $(DISTDIR)
#	cp -pf $(MKDIST) $(DISTFILES) $(DISTDIR)
#	@for DIR in */ ; do \
#		if [ -f "$${DIR}/Makefile" ]; then \
#			${MAKE} -C "$${DIR}" mkdist ; \
#		fi ; \
#	done

#########################################################################
# End of standard rules file.
