#//////////////////////////////////////////////////////////////////////////
#
# Filename:	Makefile 
# Purpose:	This is the makefile for GutenMark.  It will probably
#		work only with GNU make, and as far as compilers are 
#		concerned, is intended to be used with
#		GNU gcc on *NIX or on mingw32 on Win32.
# Mods:		09/01/01 RSB	Began.
#		09/03/01 RSB	Added AutoMarkWin
#		11/02/01 RSB	Name changed from AutoMark to 
#				GutenMark, and GPL status clarified.
#				gcc-compilation produces two 
#				identical files, GutenMark and 
#				GutenMark.exe.  This is a courtesy
#				thing for compiling with mingw32
#				in Win32.
#		11/22/01 RSB	Added MatchWordlists.  Also, reorganized
#				a lot, to use a single list of source files.
#				Added libz.  Borland compiler probably
#				doesn't work any longer.
#		11/28/01 RSB	Simplified somewhat.
#		11/29/01 RSB	Added regression test.
#		12/01/01 RSB	Final fixups for 12/01/01 release.
#		12/28/01 RSB	Added -funsigned_char to the compiler options.
#		01/21/04 RSB	Now automatically builds GutenUtilities.
#				Also, if mingw32 is installed (on Linux)
#				always tries to build Win32 versions in 
#				addition to the Linux versions ... but doesn't
#				abort on failure.
#		02/21/04 RSB	A "make clean" in Linux now removes GutenMark.exe.
#		04/20/08 RSB	Added 'snapshot' target.  
#		04/21/08 RSB	Corrected cross-compiler to account for IMCROSS 
#				project.
#		04/23/08 RSB	Altered the way the installers are built, to 
#				eliminate the Linux-only files in the Windows
#				install, and vice-versa.  The intaller programs
#				will thus be a little smaller now.
#		05/09/08 RSB	Added GutenMark-macosx target for a Mac OS X 
#				cross-compile, based on IMCROSS.  InstallJammer
#				doesn't work properly for Mac OS X yet, so I 
#				create a tarball for the Mac OS X platforms.
#		05/18/08 RSB	Created the Mac OS X application bundle.  Added
#				some overrides, in case someone actually wants
#				to perform the build on Mac OS X 10.5 rather 
#				than on Linux.
#		05/26/08 RSB	Modified according to Jason Pollock's 
#				suggestion, to use ${CC} rather than hard-coding
#				gcc as the compiler, and to have ${CC} default
#				to cc.
#		05/30/08 RSB	Fixed the PATH.  Also, added the ability to 
#				override the cross-compiled Mac executables if
#				executables that had actually been built natively
#				on the Mac were available: it's necessary to put
#				files called GutenMark-maconmac, GutenSplit-maconmac,
#				and GUItenMark-maconmac in this directory.
#				When building natively on Mac OS X using 
#				'make BuildOnMacOSX' using my account (only),
#				the executables are sent back to my Linux box
#				so that they can be incorporated into the 
#				Mac application bundle in preference to the 
#				cross-compiled ones.
#		06/01/2008 RSB	Replaced all ".phony" with ".PHONY", as I
#				am informed (thanks, Jason Pollock!), that
#				".phony" is not merely theoretically 
#				incorrect, but also doesn't work on all 
#				platforms.  Hopefully, fixed an abort that
#				could occur if MinGW wasn't present.  Fixed some
#				mistaken usage of PATH and some usage of 
#				"wx-config --static" that weren't as portable
#				as I had hoped.
#		03/09/09 RSB	Changed so that when *I* compile it, the
#				native-Linux version compiles for 32-bit 
#				CPU even if I'm using a 64-bit machine 
#				to compile it.  Also, the default target
#				now only compiles the native applications.
#				The new 'all-archs' target compiles for 
#				Linux, Win32, and Mac OS X.  So, the normal
#				native compile doesn't need IMCROSS.
#		05/10/09 RSB	Added the 'buildbox' target.
#
#  Copyright 2001,2004,2008-2009 Ronald S. Burkey <info@sandroid.org>
#
#  This file is part of GutenMark.
#
#  GutenMark is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  GutenMark is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with GutenMark; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#//////////////////////////////////////////////////////////////////////////

ifndef DARWIN_VERSION
DARWIN_VERSION=darwin9
endif

ifndef CC
CC=cc
endif

ifdef DEBUG
DEBUG_LOG=-DDEBUG_LOG
endif

ifeq "${USER}" "rburkey"
ARCH32=-m32
endif

IMCROSS_PATH=/home/${USER}/IMCROSS

# These are the target architectures used for Mac OS X.  
MAC_TARGETS=-arch ppc -arch i386 -I${IMCROSS_PATH}/mac/include

SUBMAKE:=${MAKE} "CC=${CC}" "IMCROSS_PATH=${IMCROSS_PATH}" "MAC_TARGETS=${MAC_TARGETS}"

# This is where software snapshots will be put. It's of no use to anyone 
# but me.
FTPSITE=/home/${USER}/Projects/sandroid.org/public_html/GutenMark/wasftp.GutenMark
WEBSITE=${FTPSITE}/SoftwareSnapshots
DATE:=`date +%Y%m%d`

SOURCES := $(wildcard *.c) $(wildcard libGutenSpell/*.c)

# The build is normally supposed to take place on a Linux box, even if 
# Win32 or Mac OS X executables are being built.  However, Those executables
# presently don't work on Intel-based Mac OS X 10.5, so it's possible that 
# someone might want to actually perform a build on a Mac OS X 10.5 system.
# The following step intercepts that case and substitutes different build
# rules.  The actual rules are stuck at the end of this file to get them
# out of the way of the "real" builds.
ifeq (darwin9.0,$OSTYPE)
.PHONY: OverrideDefault
OverrideDefault:  BuildOnMacOSX
endif

.PHONY: default
default: GutenMark
	${SUBMAKE} -C GutenUtilities
	-${SUBMAKE} -C GUItenMark

# 'buildbox' is like 'snapshot', except that the building is done on the build-box
# rather than on the local machine.  However, the files development-snapshot and
# binary installers so-created are still moved to their proper places on the local 
# machine within the mirror image of the website.
.PHONY: buildbox
buildbox: source-snapshot
	sh ./BuildBox.sh

# We need to override the PATH, in order to make sure that the various cross-compiler
# tools are located properly.  However, just changing the PATH variable here within
# the Makefile isn't enough; the PATH has to be set properly before 'make' is entered.
# Therefore, for various targets which require this, we have a special target that
# changes the PATH within the shell and then immediately does 'make' on those targets,
# which are renamed to be suffixed with '2'.  For example, if we do 'make default', 
# it actually changes the path and then does a 'make default2'. The targets for which
# this occurs are listed in the following variable.
PATHCHANGE_TARGETS=snapshot all-archs

.PHONY: ${PATHCHANGE_TARGETS}
${PATHCHANGE_TARGETS}:
	export PATH=/usr/local/bin:${PATH}:${IMCROSS_PATH}/bin:${IMCROSS_PATH}/installjammer && \
	${MAKE} $@2

.PHONY: all-archs2
all-archs2: GutenMark GutenMarkX.exe GutenMark-macosx
	${SUBMAKE} -C GutenUtilities all-archs
	${SUBMAKE} -C GUItenMark all-archs

.PHONY: snapshot2
snapshot2: source-snapshot all-archs2
	# Put stuff in the proper directory, which is a temporary directory we make
	# underneath InstallJammer, called InstallJammer/input.
	-rm InstallJammer/input InstallJammer/output -rf
	mkdir InstallJammer/input InstallJammer/output
	mkdir InstallJammer/input/binary
	mkdir InstallJammer/input/GUIConfigs
	mkdir InstallJammer/input/GutConfigs
	mkdir InstallJammer/input/Outputs
	cp \
		GUItenMark/*.jpg GUItenMark/*.png GUItenMark/*.ico \
		${FTPSITE}/Wordlists/* \
		GutenUtilities/espa~nol_filter \
		GutenUtilities/GutenSplit \
		GutenUtilities/GutenSplit.exe \
		GutenUtilities/GutenSplit-macosx \
		GutenUtilities/gztest \
		GutenUtilities/hk2_deutsch \
		GutenUtilities/names_english \
		GutenUtilities/NIMA \
		GutenUtilities/NoDups \
		GutenUtilities/norsk \
		GutenUtilities/string2line \
		GutenUtilities/USGS \
		GutenUtilities/utf8 \
		GutenUtilities/words197 \
		GUItenMark/GUItenMark \
		GUItenMark/GUItenMark.exe \
		GUItenMark/GUItenMark-macosx \
		GutenMark \
		GutenMark.exe \
		GutenMark-macosx \
		GutenMark.foreign.gz \
		GutenMark.native.gz \
		InstallJammer/input/binary
	# In the remainder of this rule, we create installers.
	cp GutenMark-install.cfg InstallJammer/input/GutConfigs/GutenMark.cfg
	# First, Linux ---------------------------------------------------
	# Before running the installer, we need to doctor the project
	# file a little to update the embedded version code.
	sed "s+PROGRAM_VERSION+${DATE}+" InstallJammer/InstallJammer-Linux.mpi >InstallJammer/temp.mpi
	# NOW run the installer builder.
	@echo PATH=${PATH}
	cd InstallJammer && installjammer --build temp.mpi
	cp InstallJammer/output/*Linux* ${WEBSITE}/GUItenMark-Linux-Install
	# Next, Windows --------------------------------------------------
	# Before running the installer, we need to doctor the project
	# file a little to update the embedded version code.
	sed "s+PROGRAM_VERSION+${DATE}+" InstallJammer/InstallJammer-Windows.mpi >InstallJammer/temp.mpi
	# NOW run the installer builder.
	cd InstallJammer && installjammer --build temp.mpi
	cp InstallJammer/output/*Setup.exe ${WEBSITE}/GUItenMark-Win32-setup.exe
	# Now, Mac OS X --------------------------------------------------
	# Redo all of the copying, since a different directory structure
	# is used to create the Apple application bundle, and since 
	# we don't run InstallJammer.  We create links for the executables 
	# the Resources directory of the application bundle, so that 
	# GutenMark and GutenSplit can be run from there.
	-mkdir temp
	cp -a GUItenMark.app temp
	cp GutenMark-install.cfg temp/GUItenMark.app/Contents/Resources/GutenMark.cfg
	cp -a ${FTPSITE}/Wordlists/* temp/GUItenMark.app/Contents/Resources
	cp -a GutenMark*gz temp/GUItenMark.app/Contents/Resources
	cp -a GutenUtilities/GutenSplit-macosx temp/GUItenMark.app/Contents/MacOS/GutenSplit
	cp -a GUItenMark/GUItenMark-macosx temp/GUItenMark.app/Contents/MacOS/GUItenMark
	cp -a GutenMark-macosx temp/GUItenMark.app/Contents/MacOS/GutenMark
	# If we've gone out of our way to procure executables *actually* built on Mac OS X
	# rather than cross-compiled, use them instead of the cross-compiled ones.
	-cp -a GutenSplit-maconmac temp/GUItenMark.app/Contents/MacOS/GutenSplit
	-cp -a GUItenMark-maconmac temp/GUItenMark.app/Contents/MacOS/GUItenMark
	-cp -a GutenMark-maconmac temp/GUItenMark.app/Contents/MacOS/GutenMark
	cp -a GUItenMark/*.jpg temp/GUItenMark.app/Contents/Resources
	cd temp/GUItenMark.app/Contents/Resources && ln --force --symbolic ../MacOS/* .
	-rm ${WEBSITE}/GUItenMark.app.tar.gz
	tar -C temp -czf ${WEBSITE}/GUItenMark.app.tar.gz GUItenMark.app
	# All done!
	ls -ltr ${WEBSITE}

# For generic *nix.  If the cc command isn't gcc, this might not work, 
# and I guess you'll want to substitute "gcc" for "cc".  The reason it's 
# set to "cc" rather than directly to "gcc" is for the benefit of MacOS-X. 
GutenMark: $(SOURCES) AutoMark.h
	${CC} ${ARCH32} -O3 -Wall -funsigned-char \
		-o GutenMark $(SOURCES) -lz
	strip $@
	./GutenMark bldhb10.txt bldhb10.html
	-diff bldhb10.html bldhb10.txt.html

# For making a software snapshot.
.PHONY: source-snapshot
source-snapshot:
	${MAKE} clean
	tar --directory=.. --exclude="*/.snprj" --exclude="*.proj" --exclude="*/CVS" \
		--exclude="*~" --exclude="*.bak" --exclude="InstallJammer/output" \
		--exclude="InstallJammer/build" --exclude="InstallJammer/temp.mpi" \
		--exclude="InstallJammer/build.log" --exclude="InstallJammer/input" \
		--exclude="*.tmp" --exclude="*.save" --exclude="*.save2" --exclude="temp" \
		-czf ${WEBSITE}/GutenMark_source_dev-${DATE}.tar.gz GutenMark-source

# Temporary target for making an installation zipfile.  Won't be used
# at all once there's an installer.
.PHONY: zip
zip:
	${MAKE} clean
	-rm ${WEBSITE}/GUItenMark-demo.zip
	cd ~ && \
	zip -o -r ${WEBSITE}/GUItenMark-demo.zip .GutenMark \
		-x ".GutenMark/GUIConfigs/*" -x ".GutenMark/Outputs/*"

Utilities:
	${SUBMAKE} -C GutenUtilities

# Builds a Win32 version of GutenMark, using mingw32 as installed on a Linux
# computer.  The assumption is that cross-compilers have been installed using 
# the IMCROSS ("I'm Cross!", www.sandroid.org/imcross) project.
# I've had to use a wrong target name to keep the rule from conflicting
# with a similar rule below (for ACTUALLY using mingw32 on Windows).  Therefore,
# this will attempt to build every time, whether or not it's up to date.
GutenMarkX.exe: $(SOURCES) AutoMark.h
	@echo PATH=${PATH}
	-i386-mingw32-gcc -O3 -Wall -funsigned-char -o GutenMark.exe $(SOURCES) -lz
	-i386-mingw32-strip GutenMark.exe

# And the same, but for Mac OS X.
GutenMark-macosx: $(SOURCES) AutoMark.h
	-powerpc-apple-${DARWIN_VERSION}-gcc \
		${MAC_TARGETS} -isysroot ${IMCROSS_PATH}/mac/SDKs/MacOSX10.4u.sdk \
		-O3 -Wall -funsigned-char \
		-o $@ $(SOURCES) -lz
	-powerpc-apple-${DARWIN_VERSION}-strip $@

debug: $(SOURCES) AutoMark.h
	cc -g -O0 -Wall -funsigned-char -DDEBUGGING \
		-o GutenMark $(SOURCES) -lz
	./GutenMark bldhb10.txt bldhb10.html
	-diff bldhb10.html bldhb10.txt.html

# For Win32 with mingw32.	
GutenMark.exe: $(SOURCES) AutoMark.h
	gcc -O3 -Wall -funsigned-char -I. -I.. -Izlib_w32 -Lzlib_w32 \
		-o GutenMark.exe $(SOURCES) -lz
	strip $@
	GutenMark bldhb10.txt bldhb10.html
	-fc bldhb10.html bldhb10.txt.html
	
clean:
	-rm InstallJammer/.GutenMark -rf
	-rm GutenMark GutenMark.exe GutenMark-macosx
	${SUBMAKE} -C GutenUtilities clean
	${SUBMAKE} -C GUItenMark clean


#################################################################################
# The following rules have nothing to do with the normal build process, which is
# Linux based.  However, they allow building *on* Mac OS X so that the programs
# could be run on that specific machine.  For this process to work, the GUItenMark 
# application bundle must already have been installed on the desktop.  What the 
# rule BuildOnMacOSX does is to build all of the executables, and then to replace 
# the executables that are already in the installed app bundle.
#
# Furthermore, wxWidget 2.8 must be installed.  This is the default on Mac OS X
# 10.5, but is NOT the case on 10.4 or prior.

PREP_BINARIES=strip $@
ifeq "${USER}" "rburkey"
GUITENMARK_TARGETS=${MAC_TARGETS}
endif

.PHONY: BuildOnMacOSX
BuildOnMacOSX:	${HOME}/Desktop/GUItenMark.app \
		GutenMark-maconmac \
		GutenSplit-maconmac \
		GUItenMark-maconmac
	# Patch the app bundle
	for n in GutenMark GutenSplit GUItenMark ; \
	do \
		cp $$n-maconmac ${HOME}/Desktop/GUItenMark.app/Contents/MacOS/$$n ; \
	done
ifeq "${USER}" "rburkey"
	# Phone home.
	scp -p GUItenMark-maconmac rburkey@192.168.254.250:Projects/GutenMark/GutenMark-source
endif

GutenMark-maconmac: $(SOURCES) AutoMark.h
	${CC} \
		-O3 -Wall -funsigned-char \
		-o $@ $(SOURCES) -lz
	${PREP_BINARIES}

GutenSplit-maconmac: GutenUtilities/GutenSplit.c
	${CC} -O2 -Wall -o $@ $^
	${PREP_BINARIES}

CSOURCE=GUItenMark/main.cpp GUItenMark/GUItenMark.cpp
CHEADERS=GUItenMark/GUItenMark.h
GUItenMark-maconmac: ${CSOURCE} ${CHEADERS} Makefile
	g++ \
		${DEBUG_LOG} ${GUITENMARK_TARGETS} \
		`wx-config --version=2.8 --cxxflags` \
		${CSOURCE} \
		`wx-config --version=2.8 --libs` \
		-o $@
	${PREP_BINARIES}


