##############################################################################
#                                                                            #
# Standard rulesets for use when compiling ELKS applications.                #
#                                                                            #
# This file should be included in every Makefile below elkscmd/ via e.g.:    #
#   BASEDIR=..                                                               #
#   include $(BASEDIR)/Makefile-rules                                        #
#                                                                            #
##############################################################################

ifndef TOPDIR
$(error TOPDIR is not defined)
endif

include $(TOPDIR)/Make.defs

##############################################################################
#
# It is not normally necessary to make changes below this line.
#
# Specify directories.

ELKS_DIR=$(TOPDIR)/elks
ELKSCMD_DIR=$(TOPDIR)/elkscmd

##############################################################################
#
# Determine the ELKS kernel version.

E_V=$(shell grep -v '^\#' $(ELKS_DIR)/Makefile-rules \
		    | fgrep = | head -4 | tr '\#' = | cut -d = -f 2 ; )

ELKS_VERSION=$(shell printf '%s.%s.%s%s' $(E_V))

##############################################################################
#
# Compiler variables for programs to be compiled as host applications.

HOSTCC = gcc
HOSTCFLAGS = -O3

##############################################################################
#
# Default compiler.

ifeq ($(COMPILER), )
COMPILER = IA16
endif

##############################################################################
#
# Compiler variables for programs cross-compiled for ELKS using ia16-elf-gcc

ifeq ($(COMPILER), IA16)

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

CLBASE =  -mcmodel=small -melks-libc -mtune=i8086 -Wall -Os
CLBASE += -mno-segment-relocation-stuff
CLBASE += -fno-inline -fno-builtin-printf -fno-builtin-fprintf
#CLBASE += -mregparmcall
ifeq ($(CONFIG_APPS_FTRACE), y)
    CLBASE += -fno-omit-frame-pointer -fno-optimize-sibling-calls
    CLBASE += -finstrument-functions-simple -maout-symtab
endif

WARNINGS = -Wextra -Wtype-limits -Wno-unused-parameter -Wno-sign-compare -Wno-empty-body

# temporarily turn off typical non-K&R warnings for now
WARNINGS += -Wno-implicit-int
# temporarily turn off suggesting parenthesis around assignment used as truth value
WARNINGS += -Wno-parentheses

CC=ia16-elf-gcc
AS=ia16-elf-as
LD=ia16-elf-gcc

CFLAGS =  $(CLBASE) $(WARNINGS) $(LOCALFLAGS) $(INCLUDES)
ASFLAGS = -mtune=i8086 --32-segelf
LDFLAGS = $(CLBASE)
LDLIBS =

endif

##############################################################################
#
# Compiler variables for programs cross-compiled for ELKS using OpenWatcom C

ifeq ($(COMPILER), WATCOM)
CC = ewcc
LD = ewlink
CFLAGS = $(WARNINGS) $(LOCALFLAGS) $(INCLUDES)
LDFLAGS =
LDLIBS =

endif

###############################################################################
#
# Special libraries for some programs
TINYPRINTF=$(ELKSCMD_DIR)/lib/tiny_vfprintf.o

###############################################################################
#
# Standard compilation rules.

.PHONY: all clean install

.S.s:
	$(CC) -E -traditional $(INCLUDES) $(CCDEFS) -o $*.s $<

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

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

.c.o:
	$(CC) $(CFLAGS) -c -o $*.o $<

%.obj: %.c
	$(CC) $(CFLAGS) $<

###############################################################################
