# GBDK-2.0 Top level Makefile
# Michael Hope, Pascal Felber

.EXPORT_ALL_VARIABLES:

VERSION	= 2
PATCHLEVEL = 0
SUBLEVEL = 17

# Housekeeping
CONFIG_SHELL := $(shell if [ -x "$$BASH" ]; then echo $$BASH; \
	else if [ -x /bin/bash ]; then echo /bin/bash; \
	else echo sh; fi ; fi)

TOPDIR	:= $(shell if [ "$$PWD" != "" ]; then echo $$PWD; else pwd; fi)

# See if a targets already been defined
ifeq (.target,$(wildcard .target))
include .target
endif

Z80 		:= $(TOPDIR)/z80
LCC		:= lcc-4.1
CFLAGS		:= #-O2 -fomit-frame-pointer -Wall

# Set HOSTCC and HOSTLD to the name of your systems native compiler
HOSTCC	 	:= gcc
HOSTLD		:= gcc
HOSTE		:= 

# If INDEXLIB is uncommented, then link will build an in-memory index of 
# the library files on the first pass.  This gives a 20x speed up on
# low end systems (386/2M) (Honest!)
INDEXLIB	= y

ifdef INDEXLIB
CFLAGS		:= $(CFLAGS) -DINDEXLIB -DMLH_MAP
endif

SUBDIRS		:= as link

# LCC dir is the root of where LCC will expect to find it's bins, include
#  files and libraries

# Currently supported processors: gb, z80
# Currently supported targetos's: rrgb, gb
#PROCESSOR = gb
#TARGETOS = gb

ifndef PROCESSOR
		PROCESSOR	:= gb
		GAMEBOY		:= y
endif
ifndef TARGETOS
		TARGETOS	:= gb
endif

TARGET			= $(PROCESSOR)-$(TARGETOS)
TARGET_STRING		= \"$(TARGET)\"
SDK_VERSION		= $(VERSION).$(PATCHLEVEL).$(SUBLEVEL)
SDK_VERSION_STRING	= \"$(SDK_VERSION)\"
ifndef SDK_DIR
SDK_DIR			= $(HOME)
endif

BUILDDIR	:= $(TOPDIR)/build/$(TARGET)/$(SDK_VERSION)
DISTROOT	:= $(TOPDIR)/dist

############################################################
# Setup the compiler, flags and executable extension depending on the target
ifdef DOS
	CC		=  gcc-go32
	LD		=  gcc-go32
	HOSTFILE	=  $(Z80)/lcc/etc/gb-dos.c
	DESTDIR		=  \\\\SDK\\\\$(TARGET)\\\\$(VERSION)-$(PATCHLEVEL)-$(SUBLEVEL)\\\\
	DIST		:= $(DISTROOT)/SDK/$(TARGET)/$(VERSION)-$(PATCHLEVEL)-$(SUBLEVEL)
	ARCHIVE		:= $(TOPDIR)/SDK-$(TARGET)-$(SDK_VERSION).zip
	LCCDIR		=  \"$(TESTDIR)\"
	CFLAGS		:= $(CFLAGS) -DDOS -DWIN32 -D_P_WAIT=P_WAIT -D_spawnvp=spawnvp

	# libemu is the 387 math co-pro emulator
	LDFLAGS		= -lemu
	E		= .exe
else
	CC		=  $(HOSTCC)
	LD		=  $(HOSTLD)
	E		=  $(HOSTE)
	HOSTFILE	=  $(Z80)/lcc/etc/gb-unix.c
	DESTDIR		=  $(SDK_DIR)/SDK/$(TARGET)/$(SDK_VERSION)/
	DIST		:= $(DISTROOT)/SDK/$(TARGET)/$(SDK_VERSION)
	ARCHIVE		:= $(TOPDIR)/sdk-$(TARGET)-$(SDK_VERSION).tar
	CFLAGS		:= $(CFLAGS) -DUNIX
	LDFLAGS         = -lm
endif

LCCDIR		:= \"$(DESTDIR)\"

ifeq ($(PROCESSOR), gb)
	CFLAGS	:= $(CFLAGS) -DGAMEBOY
endif

############################################################

CFLAGS		:= $(CFLAGS) -DSDK -DLCCDIR=$(LCCDIR) -DTARGET=$(TARGET) -DSDK_VERSION_STRING=$(SDK_VERSION_STRING)\
			-DTARGET_STRING=$(TARGET_STRING)

all:
ifdef DOS
	echo "DOS=y" > $(TOPDIR)/.target
else
	echo "UNIX=y" > $(TOPDIR)/.target
endif
	$(MAKE) binary

binary: lcc-binary
	for i in $(SUBDIRS); do $(MAKE) -C $$i; done

# We copy the lcc distribution files to the build directory to avoid
# modifying the original lcc distribution
lcc-binary:
	mkdir -p $(BUILDDIR)/src/lcc
	cp $(LCC)/custom.mk $(BUILDDIR)/src/lcc
	cp -r $(LCC)/lburg $(BUILDDIR)/src/lcc
	cp -r $(LCC)/src $(BUILDDIR)/src/lcc
	cp -r $(LCC)/etc $(BUILDDIR)/src/lcc
	cp -r $(LCC)/cpp $(BUILDDIR)/src/lcc
	cp $(Z80)/lcc/makefile $(BUILDDIR)/src/lcc
	cp -r $(Z80)/lcc/lburg/* $(BUILDDIR)/src/lcc/lburg
	cp -r $(Z80)/lcc/src/* $(BUILDDIR)/src/lcc/src
	cp -r $(Z80)/lcc/etc/* $(BUILDDIR)/src/lcc/etc
	$(MAKE) -C $(BUILDDIR)/src/lcc CC=$(HOSTCC) LD=$(HOSTLD) CFLAGS= LDFLAGS= lburg
	$(MAKE) -C $(BUILDDIR)/src/lcc LCCDIR=$(LCCDIR) rcc lcc cpp

dosify:
ifdef DOS
	for i in $(DIST)/* $(DIST)/doc/* $(DIST)/include/* $(DIST)/lib/* $(DIST)/examples/* $(DIST)/examples/*/*; \
	 do ( \
	  if [ -f $$i ] && [ `expr $$i : '.*\.\(.*\)' \| $$i` != "gbr" ] && [ `expr $$i : '.*\.\(.*\)' \| $$i` != "pdf" ]; then \
	   unix2dos $$i $$i; \
	  fi ); \
	 done
endif

makedirs:
	rm -rf $(DIST)
	mkdir -p $(DIST)/bin
	mkdir -p $(DIST)/doc
	mkdir -p $(DIST)/include
	mkdir -p $(DIST)/lib
	mkdir -p $(DIST)/examples

copyfiles:
	cp -rf include-$(TARGETOS)/* $(DIST)/include
	cp -rf doc-$(TARGETOS)/* $(DIST)/doc
	cp -rf doc-common/* $(DIST)/doc
	cp -rf lib-$(TARGETOS)/* $(DIST)/lib
	cp -rf examples-$(TARGETOS)/* $(DIST)/examples
ifdef DOS
	cp gbdk.bat $(DIST)
	troff -man -a $(LCC)/doc/lcc.1 > $(DIST)/doc/lcc.doc
else
	cp $(LCC)/doc/lcc.1 $(DIST)/doc
endif
	chmod -R a+r $(DIST)/bin
	chmod -R a+r $(DIST)/doc
	chmod -R a+r $(DIST)/include
	chmod -R a+r $(DIST)/lib
	chmod -R a+r $(DIST)/examples

zdist:	dist
	rm -f $(ARCHIVE)
ifdef DOS
	cd $(DISTROOT); \
	zip -r $(ARCHIVE) *
else
	cd $(DISTROOT); \
	tar cf $(ARCHIVE) *
	gzip $(ARCHIVE)
endif

dist:	makedirs copyfiles dosify
	rm -rf bin.zip
	cp $(BUILDDIR)/lcc$E $(DIST)/bin
	cp $(BUILDDIR)/rcc$E $(DIST)/bin
	cp $(BUILDDIR)/cpp$E $(DIST)/bin
	cp as/as$E           $(DIST)/bin
	cp link/link$E	     $(DIST)/bin
	rm -rf `find "dist" -name CVS`
	rm -rf `find "dist" \( -name \*\~ -o -name \*\# -o -name \*\% \) -print`

clean:
	for i in $(SUBDIRS); do $(MAKE) -C $$i clean; done
	rm -rf $(BUILDDIR)
	rm -rf $(DIST)
	rm -f $(TOPDIR)/.target

install: dist
ifndef UNIX
	@echo "Panic: dont know how to install on a DOS system"
else
	mkdir -p $(DESTDIR)
	cd $(DIST); tar cf - * | (cd $(DESTDIR); tar xf - )
endif
