# This Makefile requires GNU make.

# If you get strange errors about `missing conditional' when
# running make, try `gmake' instead of `make'.

# DO NOT edit this file unless you're a z26 developer. All the
# configuration is done via an included config.mak.

# If you can't get any of the existing targets to build, instead of
# editing this file, you run `make custom', then edit config.mak.

# If you're able to get the program to compile and run successfully
# by changing config.mak, please send the authors a copy of it, along
# with the name of your operating system, so we can add it to the
# next release.

sinclude config.mak

# This is a sneaky way to have my cake and eat it too.

# The first time you run `make' after extracting the tarball, it will
# tell you to run e.g. `make windows' or `make linux'.

# The next time, you just run `make' to build the same target as you
# did last time.

# This is done by copying `config_$target.mak' to `config.mak'
# if it doesn't already exist (which it won't, the first time),
# or else using the existing config.mak if present.

ifeq ($(CONFIG), )
all:
	@echo "Targets are:" ; \
	echo ; \
	echo "	windows"; \
	echo "	linux"; \
	echo "	linux-static"; \
	echo "	freebsd"; \
	echo "	netbsd"; \
	echo "	beos"; \
	echo "	generic-unix"; \
	echo "	custom (if you've created config_custom.mak)"; \
	echo ; \
	echo "There may be other targets for use by the z26 developers."
else
all:
	@echo "Building for $(CONFIG)"
	$(MAKE) $(EXE)
endif

linux:
	cp conf/config_linux.mak config.mak
	$(MAKE) clean all

c_linux:
	cp conf/config_c_linux.mak config.mak
	$(MAKE) clean all

linux-static:
	cp conf/config_linux-static.mak config.mak
	$(MAKE) clean all

freebsd:
	cp conf/config_freebsd.mak config.mak
	$(MAKE) clean all

netbsd:
	cp conf/config_netbsd.mak config.mak
	$(MAKE) clean all

beos:
	cp conf/config_beos.mak config.mak
	$(MAKE) clean all

generic-sdl:
	cp conf/config_generic-sdl.mak config.mak
	$(MAKE) clean all

windows:
	cp conf/config_windows.mak config.mak
	$(MAKE) clean all

c_windows:
	cp conf/config_c_windows.mak config.mak
	$(MAKE) clean all

custom:
	cp conf/config_custom.mak config.mak
	$(MAKE) clean all

es:
	cp conf/config_es.mak config.mak
	$(MAKE) clean all

### No user-serviceable parts below (unless you know what you're doing!)

ifneq ($(CONFIG), )
# make sure everything's set:

ifeq ($(NASM), )
$(warning You didn't set NASM, using default)
NASM=nasm
endif

ifeq ($(NASMFLAGS), )
$(warning You didn't set NASMFLAGS, using default)
NASMFLAGS=-f elf
endif

ifeq ($(CC), )
$(warning You didn't set CC, using default)
CC=gcc
endif

# no defaults for SDLLIBS, SDLCFLAGS, CFLAGS, Z26OPTS
# This is reasonable for CFLAGS and Z26OPTS.

ifeq ($(SDLCFLAGS), )
$(warning You didn't set SDLCFLAGS, and there is no default.)
endif

ifeq ($(SDLLIBS), )
$(warning You didn't set SDLLIBS, and there is no default.)
endif

ifeq ($(INSTALL_TARGET), )
INSTALL_TARGET=install.unix
endif

ifeq ($(INSTALL_USER), )
INSTALL_USER=root
endif

ifeq ($(INSTALL_GROUP), )
INSTALL_GROUP=root
endif

ifeq ($(INSTALL_BINDIR), )
INSTALL_BINDIR=/usr/local/bin
endif

# Most platforms use this:
z26: z26.o z26core.o
	$(CC) z26.o z26core.o $(SDLLIBS) -o z26
	strip z26

z26c: z26.o
	$(CC) z26.o $(SDLLIBS) -o z26c
	strip z26c

# Windows uses this (and so will OS/2 if we ever support it):
z26.exe: z26.o z26core.o
	$(CC) z26.o z26core.o $(SDLLIBS) -o z26 
	strip z26.exe

z26c.exe: z26.o
	$(CC) z26.o $(SDLLIBS) -o z26c
	strip z26c.exe


# linux-static uses this:
z26-static: z26.o z26core.o
	$(CC) z26.o z26core.o -Wl,-static $(SDLLIBS) -o z26-static
	strip z26-static


z26.o:	z26.c z26.h \
	2600core.c         c_cpujam.c		guigame.c		palette.c     sdlvideo.c \
	c_banks.c          guiinterface.c	pcx.c			c_soundq.c \
	bintoasm.c		   gui_sort.c		c_pitfall2.c	c_starpath.c \
	carts.c            ct.c				guisound.c      position.c    text.c \
	c_catchuppixels.c  c_trace.c		guitiming.c     c_riot.c      c_tialine.c \
	genclr.c           guitweaks.c		sdlflip.c		c_tiasnd.c \
	globals.c          guiutil.c		sdlicon.c		c_tiawrite.c \
	cli.c              guiabout.c		guivideo.c      sdlmouse.c    trace.c \
	controls.c         gui.c			c_init.c        sdlpixcopy.c  tracex.c \
	c_cpu.c            guidebug.c		kidvid.c        sdlsound.c \
	c_cpuhand.c        guifilelist.c	mt.c            sdlsrv.c \
	conf/c_core.mak
	$(CC) -Wall $(Z26OPTS) $(C_CORE) $(CFLAGS) $(SDLCFLAGS) -c z26.c


z26core.o: z26core.asm \
	banks.asm    defs.asm    lincopy.asm	service.asm   tialine.asm \
	cpu.asm      extern.asm  main.asm	soundq.asm    tiasnd.asm    trace.asm \
	cpuhand.asm  head.asm    pitfall2.asm	starpath.asm  tiatab.asm \
	cpujam.asm   init.asm    riot.asm	tail.asm      tiawrite.asm \
	conf/c_core.mak
	$(NASM) $(NASMFLAGS) $(C_CORE) -o z26core.o z26core.asm


c_catchuppixels.c: catchuppixels.m4
	m4 catchuppixels.m4 > c_catchuppixels.c

c_cpu.c: cpu.m4
	m4 cpu.m4 > c_cpu.c


# INSTALL_TARGET is either install.unix or install.null
install: $(INSTALL_TARGET)

install.unix: all z26.man
	mkdir -p $(INSTALL_BINDIR) ;\
	install -m755 -o$(INSTALL_USER) -g$(INSTALL_GROUP) $(EXE) $(INSTALL_BINDIR)/;\

install.null:
	@echo "No install target for this platform."


endif

clean:
	rm -f core *.o z26 z26c z26-static z26.exe z26c.exe *~ *.bak

realclean: clean
	rm -f z26.cli z26.gui config.mak z26.log
