#---------------------------------------------------------------------------------
# Clear the implicit built in rules
#---------------------------------------------------------------------------------
.SUFFIXES:
#---------------------------------------------------------------------------------

# Directories.
OBJ_DIR			:= obj-host
LIB_DIR			:= lib
BIN_DIR			:= .
SRC_DIR			:= src
INCLUDE_DIR		:= include

# Tools.
PIPE_TO_SED := 2>&1 | sed "s/:\([0-9]*\):/\(\1\) :/"

SYMLINKS=src/machdep src/osdep src/gfxdep src/joydep src/guidep src/sounddep src/threaddep


# Source files.
SRCS := $(wildcard $(SRC_DIR)/*.c $(SRC_DIR)/caps/*.c $(SRC_DIR)/dms/*.c $(SRC_DIR)/keymap/*.c) \
	$(SRC_DIR)/machdep/support.c $(SRC_DIR)/gfxdep/rawkeys.c  $(SRC_DIR)/guidep/gui-sdl.c $(SRC_DIR)/sounddep/sound.c \
	$(SRC_DIR)/gfxdep/sdlgfx.c $(SRC_DIR)/guidep/menu.c $(SRC_DIR)/osdep/main.c $(SRC_DIR)/threaddep/thread.c \
	$(SRC_DIR)/gfxdep/sdlkeys.c $(SRC_DIR)/joydep/joystick.c $(SRC_DIR)/osdep/memory.c


SRCS := $(filter-out src/compemu_raw_x86.c src/compemu_fpp.c src/compemu_support.c src/svgancui.c src/build68k.c src/catweasel.c \
	src/cdrom.c src/blkdev-libscg.c src/blkdev.c src/blitops.c src/akiko.c src/filesys_bootrom.c \
	src/genblitter.c src/gencomp.c src/gencpu.c \
	src/genlinetoscr.c src/compemu.c src/compstbl.c \
	src/enforcer.c src/ar.c src/debug.c \
	src/readdisk.c src/gengenblitter.c src/scsiemul.c src/tui.c src/linetoscr.c, $(SRCS))

# Library object files.
OBJS	:= $(subst $(SRC_DIR),$(OBJ_DIR),$(SRCS:.c=.o)) $(OBJ_DIR)/guidep/VirtualKeyboard.o

# Test source files.
# It can be useful to switch this variable around to select individual tests which are problematic.
TEST_SRCS	:= $(TEST_SRC_DIR)/showfont.c

# What's a full build?
all: uae-host.elf

# How to delete the intermediate files.
clean:
	@echo Cleaning $(OBJ_DIR)
	@rm -rf $(OBJS) $(OBJ_DIR) src/machdep src/target.h src/md-fpp.h src/sysconfig.h $(SYMLINKS)

$(OBJ_DIR):
	install -d $@

src/sysconfig.h: src/sysconfig.h.host
	cp $< $@

src/machdep:
	rm -f $@
	cd src && ln -s md-i386-gcc machdep

src/osdep:
	rm -f $@
	cd src && ln -s od-generic osdep

src/gfxdep:
	rm -f $@
	cd src && ln -s gfx-sdl gfxdep

src/joydep:
	rm -f $@
	cd src && ln -s jd-sdl joydep

src/guidep:
	rm -f $@
	cd src && ln -s gui-sdl guidep

src/sounddep:
	rm -f $@
	cd src && ln -s sd-sdl sounddep

src/threaddep:
	rm -f $@
	cd src && ln -s td-sdl threaddep

src/target.h:
	rm -f $@
	cd src && ln -s targets/t-unix.h target.h

src/md-fpp.h:
	rm -f $@
	cd src && ln -s include/fpp-ieee.h md-fpp.h

%.h: %.h.wii
	cp $< $@

%.c: %.c.wii
	cp $< $@

# How to build a library.
$(LIB_DIR)/libSDL_ttf.a: $(OBJS)
	@echo Archiving $@
	@-mkdir -p $(dir $@)
	@powerpc-gekko-ar crs $@ $(OBJS)
	@echo ----

# How to build a DOL.
$(BIN_DIR)/%.dol: %.elf
	@echo Creating DOL $@
	@-mkdir -p $(dir $@)
	#powerpc-gekko-objcopy -O binary $< $@
	elf2dol $< $@
	@echo ----

# Compilation flags.
COMMON_FLAGS	:= -g -Wall $(MACHDEP)
INCLUDES	:= -Isrc/md-generic/ -Isrc/include -Isrc -I/usr/include/SDL
DEFINES		:=  -DOS_WITHOUT_MEMORY_MANAGEMENT -DSAVESTATE -DUSE_SDL -DSUPPORT_THREADS -DCPUEMU_0 -DCPUEMU_5 -DCPUEMU_6 \
		    -DFPUEMU -DAGA -DAUTOCONFIG -DFILESYS
CFLAGS		:= $(COMMON_FLAGS) $(INCLUDES) $(DEFINES)

#unused defines; -DFDI2RAW 
# Test link flags.
LDFLAGS			:= $(COMMON_FLAGS) -L$(LIB_DIR) -lz -lSDL_ttf -lSDL -lfreetype -lm

# How to link an ELF.
uae-host.elf: $(SYMLINKS) src/target.h src/md-fpp.h src/sysconfig.h $(OBJS)
	@echo Linking $@
	@-mkdir -p $(dir $@)
	g++ -o $@ $(OBJS) $(LDFLAGS)

# How to compile C file (SDL library).
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
	@echo Compiling $<
	@-mkdir -p $(dir $@)
	gcc $(CFLAGS) -c $< -o $@ $(PIPE_TO_SED)

$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp
	@echo Compiling $<
	@-mkdir -p $(dir $@)
	g++ $(CFLAGS) -c $< -o $@ $(PIPE_TO_SED)
