#
# Makefile for libkern.a
#
TARGET = libkern.a

CC = gcc -pipe
DEFINITIONS = -DMULTITOS -DVERBOSE_BOOT -DTRAPS_PRIVATE -DCRYPTO_CODE
WARN = -Wall -Wmissing-prototypes -Winline
OPT = -fomit-frame-pointer -O2
CFLAGS = -mshort $(OPT) $(DEFINITIONS) $(WARN) -I. -I../..
RANLIB = ranlib
CRLF = crlf -s

#
# C source files
#
%.o: %.c
	@echo "$(CC) $(CFLAGS) $(MODEL) -c $< -o $@"; \
	$(CC) -Wp,-MD,.deps/$(@:.o=.P) $(CFLAGS) $(MODEL) -c $< -o $@

#
# files
#
CFILES = \
	atol.c \
	bzero.c \
	ctype.c \
	dostime.c \
	ksprintf.c \
	ksprintf_old.c \
	memchr.c \
	ms_time.c \
	strcat.c \
	strcmp.c \
	strcpy.c \
	stricmp.c \
	stricmp_16.c \
	strlen.c \
	strlwr.c \
	strncmp.c \
	strncpy.c \
	strncpy_f.c \
	strnicmp.c \
	strnicmp_16.c \
	strrchr.c \
	strrev.c \
	strtonumber.c \
	strupr.c \
	tolower.c \
	toupper.c \
	unix2xbios.c \
	unixtime.c \
	vsprintf.c

OBJS =	$(CFILES:.c=.o)

DEPENDENCIES = $(addprefix ./.deps/, $(patsubst %.c,%.P,$(CFILES)))

#
# main target
#
$(TARGET): asm-files $(OBJS)
	$(RM) $@
	$(AR) rc $@ $(OBJS) $(shell for file in `cat asm/asm-files` ; do echo asm/$$file ; done)
	$(RANLIB) $@

asm-files:
	cd asm; \
	$(MAKE) $@\
	CC="$(CC)" \
	AS="$(AS)" \
	CFLAGS="$(CFLAGS) -I.." \
	ASFLAGS="$(ASFLAGS)" \
	DEFINITIONS="$(DEFINITIONS)" \
	RANLIB=$(RANLIB) \
	ATRANS=$(ATRANS) \
	CRLF="$(CRLF)"


#
# macros for cleaning up
#
clean:
	$(RM) $(OBJS) $(TARGET)
	cd asm; $(MAKE) $@

distclean:
	$(MAKE) clean
	$(RM) -r .deps
	$(RM) *~
	cd asm; $(MAKE) $@

.PHONY: help all build buildtime boot clean distclean dist

#
# dependencies
#
DEPS_MAGIC := $(shell mkdir ./.deps > /dev/null 2>&1 || :)

-include $(DEPENDENCIES)
