#
# Makefile for modified UAE 68000 CPU core
#
# by James Hammons
# (C) 2011 Underground Software
#
# This makefile is released under the GPLv3 or later
#

ifeq ("$(V)","1")
Q :=
else
Q := @
endif

# Cross compilation using MXE
CROSS = i686-pc-mingw32-
#CROSS = i686-w64-mingw32-
#CROSS = x86_64-pc-msys-

CC      := $(CROSS)gcc
LD      := $(CROSS)gcc
AR      := $(CROSS)ar
HOSTCC  := gcc

ARFLAGS := -rs
GCC_DEPS = -MMD
INCS    := -I. -I./obj `$(CROSS)sdl-config --cflags`

OBJS = \
	obj/cpustbl.o \
	obj/cpudefs.o \
	obj/cpuemu.o \
	obj/cpuextra.o \
	obj/readcpu.o \
	obj/m68kinterface.o \
	obj/m68kdasm.o

# Targets for convenience sake, not "real" targets
.PHONY: clean

all: obj obj/libm68k.a
	@echo "Done!"

# Library rules (might not be cross-platform compatible)
obj/libm68k.a: $(OBJS)
	$(Q)$(AR) $(ARFLAGS) obj/libm68k.a $(OBJS)

obj:
	@mkdir ./obj

# Main source compilation (implicit rules)...

obj/%.o: %.c
	@echo -e "\033[01;33m***\033[00;32m Compiling $<...\033[00m"
	$(Q)$(CC) $(GCC_DEPS) $(CFLAGS) $(INCS) -c $< -o $@

obj/%.o: obj/%.c
	@echo -e "\033[01;33m***\033[00;32m Compiling $<...\033[00m"
	$(Q)$(CC) $(GCC_DEPS) $(CFLAGS) $(INCS) -c $< -o $@

# Generated code

obj/cpuemu.c: obj/gencpu obj/cpustbl.c
obj/cpustbl.c: obj/gencpu
	@echo -e "\033[01;33m***\033[00;32m Generating cpuemu.c...\033[00m"
	@cd obj && ./gencpu

obj/gencpu: obj/cpudefs.c
	@echo -e "\033[01;33m***\033[00;32m Generating gencpu...\033[00m"
	$(Q)$(HOSTCC) $(GCC_DEPS) $(CFLAGS) gencpu.c readcpu.c obj/cpudefs.c -o obj/gencpu -I. -I./obj

obj/cpudefs.c: obj/build68k
	@echo -e "\033[01;33m***\033[00;32m Generating cpudefs.c...\033[00m"
	$(Q)obj/build68k < table68k > obj/cpudefs.c

obj/build68k: build68k.c
	@echo -e "\033[01;33m***\033[00;32m Compiling $< ...\033[00m"
	$(Q)$(HOSTCC) $(GCC_DEPS) $(CFLAGS) build68k.c -o obj/build68k

clean:
	@echo -ne "\033[01;33m***\033[00;32m Cleaning out the garbage...\033[00m"
	@-rm -rf ./obj
	@echo "done!"

# Pull in dependencies autogenerated by gcc's -MMD switch
# The "-" in front is there just in case they haven't been created yet

-include obj/*.d
