#
# 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) 2002,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 -L/usr/lib -nostdlib -no-undefined
CPPFLAGS += -I../include

SOURCES = config.c loadecoff.c loadelf.c file.c main.c malloc.c msdos.c \
	readext2blocks.c readisoblocks.c start.S stringops.c
OBJS = $(patsubst %.S,%.o,$(patsubst %.c,%.o,$(SOURCES)))
LIBS = -lext2fs -lcom_err

.PHONY = all install clean reallyclean

all: delo.2nd

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

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

delo.2nd: $(OBJS)
	$(LD) $(LDFLAGS) -o $@ $^ $(LIBS)

install: delo.2nd
	$(INSTALL) -d -m 0755 /boot
	$(INSTALL) -m 0644 delo.2nd /boot

tags:
	ctags *.h *.c

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

reallyclean: clean
	-rm -f delo.2nd

# 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
