# Unix 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 =

CC = @CC@
CXX = @CXX@
CFLAGS = @CFLAGS@
CXXFLAGS = @CXXFLAGS@
CPPFLAGS = @CPPFLAGS@ -I../include -I. -I../CrossPlatform -I../slirp
DEFS = @DEFS@ -D_REENTRANT -DDATADIR=\"$(datadir)/$(APP)\"
LDFLAGS = @LDFLAGS@
LIBS = @LIBS@
SYSSRCS = @SYSSRCS@
CPUSRCS = @CPUSRCS@
MONSRCS = @MONSRCS@
PERL = @PERL@
USE_DYNGEN = @USE_DYNGEN@
DYNGENSRCS = @DYNGENSRCS@
DYNGEN_CC = @DYNGEN_CC@
DYNGEN_CFLAGS = @DYNGEN_CFLAGS@
DYNGEN_CXXFLAGS = @DYNGEN_CXXFLAGS@
DYNGEN_OP_FLAGS = @DYNGEN_OP_FLAGS@
BLESS = @BLESS@
EXEEXT = @EXEEXT@
KEYCODES = @KEYCODES@
INSTALL = @INSTALL@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_DATA = @INSTALL_DATA@

SLIRP_CFLAGS = @SLIRP_CFLAGS@
SLIRP_SRCS = @SLIRP_SRCS@
SLIRP_OBJS = $(SLIRP_SRCS:../slirp/%.c=obj/%.o)

STANDALONE_GUI = @STANDALONE_GUI@
GUI_CFLAGS = @GUI_CFLAGS@
GUI_LIBS = @GUI_LIBS@
GUI_SRCS = ../prefs.cpp prefs_unix.cpp prefs_editor_gtk.cpp ../prefs_items.cpp \
	../user_strings.cpp user_strings_unix.cpp xpram_unix.cpp sys_unix.cpp rpc_unix.cpp \
	../dummy/prefs_dummy.cpp

XPLAT_SRCS = ../CrossPlatform/vm_alloc.cpp ../CrossPlatform/sigsegv.cpp ../CrossPlatform/video_blit.cpp

# Append disassembler to dyngen, if available
ifneq (:no,$(MONSRCS):$(USE_DYNGEN))
DYNGENSRCS += $(filter %i386-dis.c,$(MONSRCS))
endif

## Files
SRCS = ../main.cpp main_unix.cpp ../prefs.cpp ../prefs_items.cpp prefs_unix.cpp sys_unix.cpp \
    ../rom_patches.cpp ../rsrc_patches.cpp ../emul_op.cpp ../name_registry.cpp \
    ../macos_util.cpp ../timer.cpp timer_unix.cpp ../xpram.cpp xpram_unix.cpp \
    ../adb.cpp ../sony.cpp ../disk.cpp ../cdrom.cpp ../scsi.cpp \
    ../gfxaccel.cpp ../video.cpp ../audio.cpp ../ether.cpp ../thunks.cpp \
    ../serial.cpp ../extfs.cpp disk_sparsebundle.cpp tinyxml2.cpp \
    about_window_unix.cpp ../user_strings.cpp user_strings_unix.cpp rpc_unix.cpp \
    sshpty.c strlcpy.c $(XPLAT_SRCS) $(SYSSRCS) $(CPUSRCS) $(MONSRCS) $(SLIRP_SRCS)
APP = SheepShaver
APP_EXE = $(APP)$(EXEEXT)
APP_APP = $(APP).app

PROGS = $(APP_EXE)
ifeq ($(STANDALONE_GUI),yes)
GUI_APP = SheepShaverGUI
GUI_APP_EXE = $(GUI_APP)$(EXEEXT)
GUI_APP_APP = $(GUI_APP).app
PROGS += $(GUI_APP_EXE)
else
CXXFLAGS += $(GUI_CFLAGS)
LIBS += $(GUI_LIBS)
endif

## Rules
.PHONY: modules install uninstall clean distclean depend dep
.SUFFIXES:
.SUFFIXES: .c .cpp .S .o .h

all: $(PROGS)

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 GUI_SRCS_LIST_TO_OBJS
	$(addprefix $(OBJ_DIR)/, $(addsuffix .guio, $(foreach file, $(GUI_SRCS), \
	$(basename $(notdir $(file))))))
endef
GUI_OBJS = $(GUI_SRCS_LIST_TO_OBJS)

define DYNGENSRCS_LIST_TO_OBJS
	$(addprefix $(OBJ_DIR)/, $(addsuffix .dgo, $(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_EXE): $(OBJ_DIR) $(OBJS)
	$(CXX) -o $@ $(LDFLAGS) $(OBJS) $(LIBS)
	$(BLESS) $(APP_EXE)

$(GUI_APP_EXE): $(OBJ_DIR) $(GUI_OBJS)
	$(CXX) -o $@ $(LDFLAGS) $(GUI_OBJS) $(GUI_LIBS)

$(APP)_app: $(APP) ../MacOSX/Info.plist ../MacOSX/$(APP).icns
	rm -rf $(APP_APP)/Contents
	mkdir -p $(APP_APP)/Contents
	./cpr.sh ../MacOSX/Info.plist $(APP_APP)/Contents/
	echo -n 'APPL????' > $(APP_APP)/Contents/PkgInfo
	mkdir -p $(APP_APP)/Contents/MacOS
	./cpr.sh $(APP) $(APP_APP)/Contents/MacOS/
	strip -x $(APP_APP)/Contents/MacOS/$(APP)
	mkdir -p $(APP_APP)/Contents/Resources
	mkdir -p $(APP_APP)/Contents/Resources/English.lproj
	./cpr.sh ../MacOSX/Launcher/English.lproj/VMSettingsWindow.nib $(APP_APP)/Contents/Resources/English.lproj
	./cpr.sh ../MacOSX/SheepShaver.icns $(APP_APP)/Contents/Resources/

$(GUI_APP)_app: $(GUI_APP) ../MacOSX/Info.plist ../MacOSX/$(APP).icns
	rm -rf $(GUI_APP_APP)/Contents
	mkdir -p $(GUI_APP_APP)/Contents
	sed -e "s/$(APP)/$(GUI_APP)/" < ../MacOSX/Info.plist > $(GUI_APP_APP)/Contents/Info.plist
	echo -n 'APPL????' > $(GUI_APP_APP)/Contents/PkgInfo
	mkdir -p $(GUI_APP_APP)/Contents/MacOS
	./cpr.sh $(GUI_APP) $(GUI_APP_APP)/Contents/MacOS/
	strip -x $(GUI_APP_APP)/Contents/MacOS/$(GUI_APP)
	mkdir -p $(GUI_APP_APP)/Contents/Resources
	./cpr.sh ../MacOSX/$(APP).icns $(GUI_APP_APP)/Contents/Resources/$(GUI_APP).icns

modules:
	cd Linux/NetDriver; make

install: $(PROGS) installdirs
	$(INSTALL_PROGRAM) $(APP_EXE) $(DESTDIR)$(bindir)/$(APP_EXE)
	if test -f "$(GUI_APP_EXE)"; then \
	  $(INSTALL_PROGRAM) $(GUI_APP_EXE) $(DESTDIR)$(bindir)/$(GUI_APP_EXE); \
	fi
	-$(INSTALL_DATA) $(APP).1 $(DESTDIR)$(man1dir)/$(APP).1
	$(INSTALL_DATA) $(KEYCODES) $(DESTDIR)$(datadir)/$(APP)/keycodes
	$(INSTALL_DATA) tunconfig $(DESTDIR)$(datadir)/$(APP)/tunconfig
	chmod 755 $(DESTDIR)$(datadir)/$(APP)/tunconfig

installdirs:
	$(SHELL) mkinstalldirs $(DESTDIR)$(bindir) $(DESTDIR)$(man1dir) $(DESTDIR)$(datadir)/$(APP)

uninstall:
	rm -f $(DESTDIR)$(bindir)/$(APP_EXE)
	rm -f $(DESTDIR)$(bindir)/$(GUI_APP_EXE)
	rm -f $(DESTDIR)$(man1dir)/$(APP).1
	rm -f $(DESTDIR)$(datadir)/$(APP)/keycodes
	rm -f $(DESTDIR)$(datadir)/$(APP)/tunconfig
	rmdir $(DESTDIR)$(datadir)/$(APP)

clean:
	rm -f $(PROGS) $(OBJ_DIR)/* core* *.core *~ *.bak ppc-execute-impl.cpp
	rm -f dyngen basic-dyngen-ops.hpp ppc-dyngen-ops.hpp ppc_asm.out.s
	rm -rf $(APP_APP) $(GUI_APP_APP)

distclean: clean
	rm -rf $(OBJ_DIR)
	rm -f Makefile
	rm -f config.cache config.log config.status config.h
	rm -f ../MacOSX/Info.plist

depend dep:
	makedepend $(CPPFLAGS) -Y. $(SRCS) 2>/dev/null

$(OBJ_DIR)/SDLMain.o : SDLMain.m
	$(CC) $(CPPFLAGS) $(DEFS) $(CFLAGS) -c $< -o $@
$(OBJ_DIR)/%.o : ../slirp/%.c
	$(CC) $(CPPFLAGS) $(DEFS) $(CFLAGS) $(SLIRP_CFLAGS) -c $< -o $@
$(OBJ_DIR)/%.o : %.c
	$(CC) $(CPPFLAGS) $(DEFS) $(CFLAGS) -c $< -o $@
$(OBJ_DIR)/DiskType.o : DiskType.m
	$(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)/%.guio : %.cpp
	$(CXX) $(CPPFLAGS) $(DEFS) $(CXXFLAGS) $(GUI_CFLAGS) -DSTANDALONE_GUI -c $< -o $@

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

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

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

$(OBJ_DIR)/%.dgo : %.c
	$(DYNGEN_CC) -xc $(CPPFLAGS) $(DEFS) $(DYNGEN_CFLAGS) -c $< -o $@
$(OBJ_DIR)/%.dgo : %.cpp
	$(DYNGEN_CC) $(CPPFLAGS) $(DEFS) $(DYNGEN_CXXFLAGS) -c $< -o $@

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

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

$(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
	$(CXX) $(CPPFLAGS) $(DEFS) $(CXXFLAGS) -DEMU_KHEPERIX -c $< -o $@

test-powerpc$(EXEEXT): $(TESTOBJS)
	$(CXX) -o $@ $(LDFLAGS) $(TESTOBJS) $(LIBS)

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