##############################################################################
#                                                                            #
# Standard rulesets for use when compiling imported ELKS applications         #
# against Antler libc and the Antler IA-16 compiler runtime.                 #
#                                                                            #
# 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.

ANTLER_DIR=$(TOPDIR)
ELKSCMD_DIR=$(TOPDIR)/elkscmd

##############################################################################
#
ELKS_VERSION=Antler

##############################################################################
#
# 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 Antler using ia16-elf-gcc

ifeq ($(COMPILER), IA16)

CLBASE =  -mcmodel=small -mantler -mtune=i8086 -mno-protected-mode -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

GCC_INCLUDE_DIR := $(shell $(CC) -print-file-name=include)
ANTLER_CRT0 ?= $(TOPDIR)/libc/build/crt0.o
ANTLER_LIBC ?= $(TOPDIR)/libc/build/libc.a
ANTLER_LIBGCC ?= $(shell $(CC) -print-libgcc-file-name)

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

CFLAGS =  $(CLBASE) $(WARNINGS) $(LOCALFLAGS) $(INCLUDES)
ASFLAGS = -mtune=i8086 --32-segelf
LDFLAGS = $(CLBASE) -nostdlib -nostartfiles -nodefaultlibs
LDLIBS = $(ANTLER_CRT0) $(ANTLER_LIBC) $(ANTLER_LIBGCC)

endif

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

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

.PHONY: all clean install

%.s: %.S $(TOPDIR)/include/autoconf.h
	$(call quiet_cmd,CPP,$@)
	$(CC) -E -traditional $(INCLUDES) $(CCDEFS) -o $@ $<

%.o: %.S $(TOPDIR)/include/autoconf.h
	$(call quiet_cmd,AS,$@)
	$(CC) -E -traditional $(INCLUDES) $(CCDEFS) -o $*.tmp $<
	$(AS) $(ASFLAGS) -o $@ $*.tmp
	$(RM) $*.tmp

%.o: %.s
	$(call quiet_cmd,AS,$@)
	$(AS) $(ASFLAGS) -o $@ $<

%.o: %.c $(TOPDIR)/include/autoconf.h
	$(call quiet_cmd,CC,$@)
	$(CC) $(CFLAGS) -c -o $@ $<

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

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