# Windows Makefile for SheepShaver

## System specific configuration
@SET_MAKE@
SHELL = /bin/sh

prefix = @prefix@
exec_prefix = @exec_prefix@
bindir = @bindir@
datadir = @datadir@
mandir = @mandir@
man1dir = $(mandir)/man1

DESTDIR =

SDL_CFLAGS = @SDL_CFLAGS@
SDL_LIBS = @SDL_LIBS@
WANT_GTK = @WANT_GTK@
GTK_CFLAGS = @GTK_CFLAGS@
GTK_LIBS = @GTK_LIBS@

SLIRP_CFLAGS = @SLIRP_CFLAGS@
SLIRP_SRCS = \
    ../slirp/bootp.c     ../slirp/ip_output.c  ../slirp/tcp_input.c  \
    ../slirp/cksum.c     ../slirp/mbuf.c       ../slirp/tcp_output.c \
    ../slirp/debug.c     ../slirp/misc.c       ../slirp/tcp_subr.c   \
    ../slirp/if.c        ../slirp/sbuf.c       ../slirp/tcp_timer.c  \
    ../slirp/ip_icmp.c   ../slirp/slirp.c      ../slirp/tftp.c       \
    ../slirp/ip_input.c  ../slirp/socket.c     ../slirp/udp.c
SLIRP_OBJS = $(SLIRP_SRCS:../slirp/%.c=$(OBJ_DIR)/slirp-%.o)

USE_BINCUE = @USE_BINCUE@

LN_S = @LN_S@
WINDRES = @WINDRES@
CC = @CC@
CXX = @CXX@
CFLAGS = @CFLAGS@ $(SDL_CFLAGS)
CXXFLAGS = @CXXFLAGS@ $(SDL_CFLAGS)
CPPFLAGS = @CPPFLAGS@ -I../include -I. -I../slirp
DEFS = @DEFS@
LDFLAGS = @LDFLAGS@ -Wl,-Bstatic
#TODO remove pthread part of that if irrelevant
LIBS = @LIBS@ -lws2_32 -lwsock32 -liphlpapi
CPUSRCS = @CPUSRCS@
EXTRASRCS = @EXTRASRCS@
PERL = @PERL@

USE_DYNGEN = @USE_DYNGEN@
USE_PREGENERATED_DYNGEN = @USE_PREGENERATED_DYNGEN@
DYNGENSRCS = @DYNGENSRCS@
DYNGEN_CC = $(CXX)
DYNGEN_OP_FLAGS = @DYNGEN_OP_FLAGS@

HOST_CC = gcc
HOST_CXX = g++
HOST_CFLAGS = -O2
HOST_CXXFLAGS = -O2
HOST_LDFLAGS =

## Files
XPLATSRCS = vm_alloc.cpp vm_alloc.h sigsegv.cpp sigsegv.h video_vosf.h video_blit.cpp video_blit.h

ROUTERSRCS =  router/arp.cpp router/dump.cpp router/dynsockets.cpp router/ftp.cpp \
	router/icmp.cpp router/mib/interfaces.cpp router/iphelp.cpp router/ipsocket.cpp \
	router/mib/mibaccess.cpp router/router.cpp router/tcp.cpp router/udp.cpp b2ether/packet32.cpp

SRCS = ../main.cpp main_windows.cpp ../prefs.cpp ../prefs_items.cpp prefs_windows.cpp \
	sys_windows.cpp cdenable/cache.cpp cdenable/eject_nt.cpp cdenable/ntcd.cpp \
    ../rom_patches.cpp ../rsrc_patches.cpp ../emul_op.cpp ../name_registry.cpp \
    ../macos_util.cpp ../timer.cpp timer_windows.cpp ../xpram.cpp xpram_windows.cpp \
    ../adb.cpp ../sony.cpp ../disk.cpp ../cdrom.cpp ../scsi.cpp ../dummy/scsi_dummy.cpp \
    ../gfxaccel.cpp ../video.cpp \
    ../SDL/video_sdl.cpp ../SDL/video_sdl2.cpp ../SDL/video_sdl3.cpp video_blit.cpp \
    ../audio.cpp ../SDL/audio_sdl.cpp ../SDL/audio_sdl3.cpp ../ether.cpp ether_windows.cpp \
    ../thunks.cpp ../serial.cpp serial_windows.cpp ../extfs.cpp extfs_windows.cpp \
    about_window_windows.cpp ../user_strings.cpp user_strings_windows.cpp \
    ../dummy/prefs_editor_dummy.cpp clip_windows.cpp util_windows.cpp \
    vm_alloc.cpp sigsegv.cpp posix_emu.cpp SheepShaver.rc \
	$(CPUSRCS) $(ROUTERSRCS) $(EXTRASRCS) $(SLIRP_OBJS)

UI_SRCS = ../prefs.cpp prefs_windows.cpp prefs_editor_gtk.cpp xpram_windows.cpp \
	../prefs_items.cpp ../user_strings.cpp user_strings_windows.cpp util_windows.cpp \
	../dummy/prefs_dummy.cpp \
	b2ether/packet32.cpp SheepShaverGUI.rc

UI_APP = SheepShaverGUI.exe

APP = SheepShaver.exe

PROGS = $(APP)

ifeq ($(WANT_GTK),yes)
PROGS += $(UI_APP)
endif

## Rules
.PHONY: clean distclean
.SUFFIXES:
.SUFFIXES: .c .cpp .S .o .h

all: $(PROGS)

$(XPLATSRCS): %: ../CrossPlatform/%
	$(LN_S) $< $@

OBJ_DIR = obj
$(OBJ_DIR)::
	@[ -d $(OBJ_DIR) ] || mkdir $(OBJ_DIR) > /dev/null 2>&1

define SRCS_LIST_TO_OBJS
	$(addprefix $(OBJ_DIR)/, $(addsuffix .o, $(foreach file, $(SRCS), \
	$(basename $(notdir $(file))))))
endef
OBJS = $(SRCS_LIST_TO_OBJS)

define UI_SRCS_LIST_TO_OBJS
	$(addprefix $(OBJ_DIR)/, $(addsuffix .o, $(foreach file, $(UI_SRCS), \
	$(basename $(notdir $(file))))))
endef
UI_OBJS = $(UI_SRCS_LIST_TO_OBJS)

define DYNGENSRCS_LIST_TO_OBJS
	$(addprefix $(OBJ_DIR)/, $(addsuffix .ho, $(foreach file, $(DYNGENSRCS), \
	$(basename $(notdir $(file))))))
endef
DYNGENOBJS = $(DYNGENSRCS_LIST_TO_OBJS)

SRC_PATHS += $(sort $(foreach file, $(SRCS), $(dir $(file))))
VPATH :=
VPATH += $(addprefix :, $(subst  ,:, $(filter-out $($(subst, :, ,$(VPATH))), $(SRC_PATHS))))

$(APP): $(XPLATSRCS) $(OBJ_DIR) $(OBJS)
	$(CXX) -o $(APP) $(LDFLAGS) $(OBJS) $(LIBS) $(SDL_LIBS)

$(UI_APP): $(XPLATSRCS) $(OBJ_DIR) $(UI_OBJS)
	$(CXX) -o $@ $(LDFLAGS) $(UI_OBJS) $(LIBS) -Wl,-Bdynamic $(GTK_LIBS) -Wl,-Bstatic -mwindows -static-libgcc

mostlyclean:
	rm -f $(APP) $(UI_APP) $(OBJ_DIR)/* core* *.core *~ *.bak

clean: mostlyclean dyngenclean
	rm -f $(XPLATSRCS)
dyngenclean:
	rm -f $(DYNGEN) basic-dyngen-ops.hpp ppc-dyngen-ops.hpp ppc-execute-impl.cpp

distclean: clean
	rm -rf $(OBJ_DIR)
	rm -rf autom4te.cache
	rm -f Makefile
	rm -f config.cache config.log config.status config.h

depend dep:
	makedepend $(CPPFLAGS)  # Truncate out previous rules
	# Manually process emitted obj targets to take out extra directory layers
	makedepend $(CPPFLAGS) -pobj/ -Y. $(SRCS) -f- 2>/dev/null | sed 's#^obj/.*/\(.*\.o\):#obj/\1:#' >> Makefile

$(OBJ_DIR)/%.ho : %.c
	$(HOST_CC) $(CPPFLAGS) $(DEFS) $(HOST_CFLAGS) -c $< -o $@
$(OBJ_DIR)/%.ho : %.cpp
	$(HOST_CXX) $(CPPFLAGS) $(DEFS) $(HOST_CXXFLAGS) -c $< -o $@
$(OBJ_DIR)/slirp-%.o : ../slirp/%.c
	$(CC) $(CPPFLAGS) $(DEFS) $(CFLAGS) $(SLIRP_CFLAGS) -c $< -o $@
$(OBJ_DIR)/%.o : %.c
	$(CC) $(CPPFLAGS) $(DEFS) $(CFLAGS) -c $< -o $@
$(OBJ_DIR)/%.o : %.cpp
	$(CXX) $(CPPFLAGS) $(DEFS) $(CXXFLAGS) -c $< -o $@
$(OBJ_DIR)/%.o : %.mm
	$(CXX) $(CPPFLAGS) $(DEFS) $(CXXFLAGS) -c $< -o $@
$(OBJ_DIR)/%.o : %.S
	$(CPP) $(CPPFLAGS) -D__ASSEMBLY__ $< -o $*.out.s
	$(AS) $(ASFLAGS) -o $@ $*.out.s
	rm $*.out.s
$(OBJ_DIR)/prefs_editor_gtk.o: prefs_editor_gtk.cpp
	$(CXX) -O2 -mms-bitfields $(CPPFLAGS) $(DEFS) $(GTK_CFLAGS) -c $< -o $@

# Windows resources
$(OBJ_DIR)/%.o: %.rc
	$(WINDRES) --include-dir ../Windows -i $< -o $@

# Kheperix CPU emulator
kpxsrcdir = ../kpx_cpu/src
GENEXECPL = $(kpxsrcdir)/cpu/ppc/genexec.pl
DYNGEN    = dyngen.exe

ifeq ($(USE_DYNGEN),yes)
DYNGENDEPS = basic-dyngen-ops.hpp ppc-dyngen-ops.hpp
dyngendeps: $(DYNGENDEPS)

###

$(OBJ_DIR)/basic-dyngen.o: basic-dyngen-ops.hpp
$(OBJ_DIR)/ppc-dyngen.o: ppc-dyngen-ops.hpp

ifeq ($(USE_PREGENERATED_DYNGEN),yes)

basic-dyngen-ops.hpp: cygwin_precompiled_dyngen/basic-dyngen-ops.hpp
	cp -f $< $@
ppc-dyngen-ops.hpp: cygwin_precompiled_dyngen/ppc-dyngen-ops.hpp
	cp -f $< $@

else
# Only GCC is supported for generating synthetic opcodes
$(DYNGEN): $(DYNGENOBJS)
	$(HOST_CXX) -o $@ $(LDFLAGS) $(DYNGENOBJS)

$(OBJ_DIR)/basic-dyngen-ops.o: $(kpxsrcdir)/cpu/jit/basic-dyngen-ops.cpp
	$(DYNGEN_CC) $(CPPFLAGS) $(DEFS) $(CXXFLAGS) $(DYNGEN_OP_FLAGS) -c $< -o $@
basic-dyngen-ops.hpp: $(OBJ_DIR)/basic-dyngen-ops.o $(DYNGEN)
	./$(DYNGEN) -o $@ $<

$(OBJ_DIR)/ppc-dyngen-ops.o: $(kpxsrcdir)/cpu/ppc/ppc-dyngen-ops.cpp basic-dyngen-ops.hpp
	$(DYNGEN_CC) $(CPPFLAGS) $(DEFS) $(CXXFLAGS) $(DYNGEN_OP_FLAGS) -c $< -o $@
ppc-dyngen-ops.hpp: $(OBJ_DIR)/ppc-dyngen-ops.o $(DYNGEN)
	./$(DYNGEN) -o $@ $<
endif

$(OBJ_DIR)/sheepshaver_glue.o $(OBJ_DIR)/ppc-cpu.o $(OBJ_DIR)/ppc-decode.o $(OBJ_DIR)/ppc-translate.o $(OBJ_DIR)/ppc-jit.o: basic-dyngen-ops.hpp ppc-dyngen-ops.hpp
endif

$(OBJ_DIR)/ppc-execute.o: ppc-execute-impl.cpp
ppc-execute-impl.cpp: $(kpxsrcdir)/cpu/ppc/ppc-decode.cpp $(GENEXECPL) $(DYNGENDEPS)
	$(CPP) $(CPPFLAGS) -DGENEXEC $< | $(PERL) $(GENEXECPL) > $@

# PowerPC CPU tester
TESTSRCS_ = mathlib/ieeefp.cpp mathlib/mathlib.cpp cpu/ppc/ppc-cpu.cpp cpu/ppc/ppc-decode.cpp cpu/ppc/ppc-execute.cpp cpu/ppc/ppc-translate.cpp test/test-powerpc.cpp $(MONSRCS) vm_alloc.cpp utils/utils-cpuinfo.cpp
ifeq ($(USE_DYNGEN),yes)
TESTSRCS_ += cpu/jit/jit-cache.cpp cpu/jit/basic-dyngen.cpp cpu/ppc/ppc-dyngen.cpp cpu/ppc/ppc-jit.cpp
endif
TESTSRCS  = $(TESTSRCS_:%.cpp=$(kpxsrcdir)/%.cpp)

define TESTSRCS_LIST_TO_OBJS
	$(addprefix $(OBJ_DIR)/, $(addsuffix .o, $(foreach file, $(TESTSRCS), \
	$(basename $(notdir $(file))))))
endef
TESTOBJS  = $(TESTSRCS_LIST_TO_OBJS)

$(OBJ_DIR)/test-powerpc.o: $(kpxsrcdir)/test/test-powerpc.cpp
	$(HOST_CXX) $(CPPFLAGS) $(DEFS) $(CXXFLAGS) -DEMU_KHEPERIX -c $< -o $@

test-powerpc.exe: $(TESTOBJS)
	$(HOST_CXX) -o $@ $(LDFLAGS) $(TESTOBJS) -mconsole

#-------------------------------------------------------------------------
# DO NOT DELETE THIS LINE -- make depend depends on it.
