#
# This file is licensed under the terms of the GNU General Public License,
# version 2. See the file COPYING in the main directory for details.
#
#  Copyright (C) 2003  Thiemo Seufer <seufer@csv.ica.uni-stuttgart.de>
#

CC = gcc
INSTALL = install

CFLAGS = -mabi=32 -ffreestanding -Os -W -Wall #-DDEBUG
ASFLAGS = $(CFLAGS) -D__ASSEMBLY__
LDFLAGS = -static -T ld.script -nostdlib -no-undefined
CPPFLAGS += -I../include

# Gross hack! start.S must be first!
SOURCES = start.S main.c
OBJS = $(patsubst %.S,%.o,$(patsubst %.c,%.o,$(SOURCES)))

all: t-rex-loader

%.o: %.c
	$(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<

%.o: %.S
	$(CC) $(ASFLAGS) $(CPPFLAGS) -c -o $@ $<

t-rex-loader: $(OBJS)
	$(LD) $(LDFLAGS) -o $@ $^

install: t-rex-loader
	$(INSTALL) -d -m 0755 /usr/lib/delo/
	$(INSTALL) -m 0644 t-rex-loader /usr/lib/delo/

tags:
	ctags *.h *.c

clean:
	-rm -f $(OBJS) $(OBJS:.o=.d) core tags *~

reallyclean: clean
	-rm -f t-rex-loader

.PHONY = all install clean reallyclean

# Dependency handling
%.d: %.c
	@set -e; $(CC) -MM $(CPPFLAGS) $< \
	| sed 's/\($*\)\.o[ :]*/\1.o $@ : /g' > $@; \
	[ -s $@ ] || rm -f $@

%.d: %.S
	@set -e; $(CC) -MM $(CPPFLAGS) $< \
	| sed 's/\($*\)\.o[ :]*/\1.o $@ : /g' > $@; \
	[ -s $@ ] || rm -f $@

ifneq ($(MAKECMDGOALS),clean)
-include $(OBJS:.o=.d)
endif
