# This is a Makefile for building the libraries in
# the subdirectories of $(KERNEL_HOME)/lib
# Zachary Anderson(zra)

include game.mk

CC = gcc
LD = ld
MAKE = make
CFLAGS = -nostdinc -fno-strict-aliasing -fno-builtin -Wall -gstabs+ -Werror
LDFLAGS = -static -Ttext 100000
INCLUDES = -I../410kern/inc -I../410kern/lib/inc -I../kern/inc
LIBS = ../410kern/lib

.PHONY: lib all clean apps

all: common.o apps nightdriver_game.o 410test.o

%.o: %.S
	$(CC) -c -o $@ $(CFLAGS) $(INCLUDES) $^

%.o: %.s
	@echo "You should use the .S file extension rather than .s"
	@echo ".s does not support precompiler directives (like #include)"
	@false

%.o: %.c
	$(CC) -c -o $@ $(CFLAGS) $(INCLUDES) $^

common.o: lib $(OBJS)
	$(LD) -r -static -L$(LIBS) -o $@ $(OBJS)

ifeq ($(strip $(NIGHTDRIVER_GAME_OBJS)),)
.PHONY: nightdriver_game.o
nightdriver_game.o:
	if [ -f $@ ]; then rm $@; fi
else
nightdriver_game.o: $(NIGHTDRIVER_GAME_OBJS)
	$(LD) -r -static -L$(LIBS) -o $@ $(NIGHTDRIVER_GAME_OBJS)
endif

ifeq ($(strip $(410_TEST_OBJS)),)
.PHONY: 410test.o
410test.o:
	if [ -f $@ ]; then rm $@; fi
else
410test.o: $(410_TEST_OBJS)
	$(LD) -r -static -L$(LIBS) -o $@ $(410_TEST_OBJS)
endif

lib:
	if [ -d lib ]; then $(MAKE) -C lib; fi

clean:
	rm -f $(OBJS) $(410_TEST_OBJS) $(NIGHTDRIVER_GAME_OBJS) *.a common.o nightdriver_game.o
	if [ -d lib ]; then $(MAKE) -C lib clean; fi

	find "." -regex '.*~' -type f -exec rm '{}' \;

