#//////////////////////////////////////////////////////////////////////////
# Filename:	Makefile
# Author:	Ron Burkey
# Purpose:	Makefile for Do178Builder on a *nix development machine.  
#		The assumption is that GNU make and gcc (or mingw32) are used.
#		On Linux, if "I'm Cross!" (www.sandroid.org/imcross) is 
#		installed, then Windows executables are built in addition
#		to the Linux executables, and both Windows and Linux 
#		installer programs are created as well.
# Mods:		09/30/01 RSB	Created.
#		10/21/01 RSB	Was just a single gcc command that compiled
#				and linked everything, regardless of whether
#				any of it had changed.  Put into a little
#				less primitive form.
#		02/18/02 RSB	Changed optimization to -O0.
#		11/10/03 RSB	Added the 'install' target.
#		06/21/05 RSB	Added the 'snapshot' target.  There are some
#				other minor tweaks as well, because I was 
#				having a very difficult time compiling, but
#				those can basically be ignored.
#		07/05/06 RSB	Modified to take advantage of the fact that
#				libold source code is now included rather
#				than built as a separate library.
#		03/09/08 RSB	Accounted for moved "ftp" directory.
#		03/13/08 RSB	Added crc32.o to eliminate zlib.
#				Added Do178Builder-utility.c.
#		03/26/08 RSB	Changed to allow optional selection of the 
#				version of wxWidgets being used, if there 
#				are multiple versions installed.  For 
#				example, "make WXVER=2.8" or "make WXVER=2.6".
#				On my Fedora Core 5 system, wxWidgets 2.4
#				needs instead "make WXCONFIG=wx-2.4-config".
#		04/06/08 RSB	Added various cross-compilation-related
#				targets.
#		04/07/08 RSB	Lots of fixes and improvements to the cross-
#				compilation targets.  Now they're actually
#				tested.
#		04/24/08 RSB	The cross-compilation stuff has now been
#				spun off to a completely separate project
#				(www.sandroid.org/imcross), so I've 
#				removed it from this makefile.  This resulted
#				in a lot of cleanup in general.  Just doing
#				'make' on Linux now tries to build everything,
#				but fails gracefully (I hope) if "I'm Cross!"
#				isn't installed.  WXCONFIG removed.
#
#  Copyright 2001-2003,2005,2006,2008 Ronald S. Burkey
#
#  This file is part of Do178Builder.
#
#  Do178Builder 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.
#
#  Do178Builder 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 Do178Builder; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

#//////////////////////////////////////////////////////////////////////////

ifdef WXVER
WXVERSION=--version=${WXVER}
endif
DATE:=`date +%Y%m%d`
WEBSITE:=~/Projects/sandroid.org/public_html/birdsproject/wasftp.birdsproject
OLDDIR=old-0.16

# The purpose of the following is to make sure that the build aborts if there
# are any compiler warnings when the build is done on my machines.  If compiled
# elsewhere, warnings will be tolerated and the build won't abort.
ifeq (i_am_${USER},i_am_rburkey)
WERROR=-Werror
endif

# Some stuff related to cross-compilation for Windows from Linux.
# Make sure that /usr/local/bin is in your PATH at the time when you actually
# want to use it, or else it won't be found.  All of libraries and header files
# will be under /usr/local/i386-mingw32/lib and /usr/local/i386-mingw32/include.
PREFIX_DIR=/usr/local
TARGET=i386-mingw32
INSTALL_DIR=${PREFIX_DIR}/${TARGET}
BUILD_DIR=temp-cross-compiler-build
CURRENT_DIR:=$(shell pwd)
WXDIR:=${CURRENT_DIR}/wx2

.phony: default
default: Do178Builder Others

.phony: clean
clean:
	-rm Do178Builder Do178Builder-utility/Do178Builder-utility
	-rm `find . -name "*.o"`
	-rm `find . -name "*.exe"`
	-rm `find . -name "*.ow"`

.phony: install
install: Do178Builder utilities
	cp Do178Builder Do178Builder-utility/Do178Builder-utility /usr/local/bin
	chmod ugo+x /usr/local/bin/Do178Builder*

.phony: snapshot
snapshot: clean
	# Make the source tarball.
	tar --directory=.. --exclude="*/.snprj" --exclude="*/CVS" --exclude="*.tar.bz2" \
		--exclude="*.tar.gz" --exclude="setup_*" --exclude="${BUILD_DIR}*" \
		--exclude="*~" --exclude="*.bak" \
		-czf ${WEBSITE}/Do178Builder-${DATE}-source.tar.gz Do178Builder-source
	# Make the Win32 executable zipfile.
	${MAKE} default
	-rm ${WEBSITE}/DO178Builder-${DATE}-win32.zip
	zip -u ${WEBSITE}/DO178Builder-${DATE}-win32.zip *.exe .template*.xml
	# Now, tell us what the results were.
	ls -l ${WEBSITE}/*source.tar.gz ${WEBSITE}/*win32.zip -t -r

Do178Builder: \
	Do178Builder.o \
	Do178Builder_wdr.o \
	XmlEditorDialogClass.o \
	SearchBoxClass.o \
	LoadXmlFile.o \
	crc32.o \
	${OLDDIR}/lib/libold.o
	gcc ${EXTRALIBS} `wx-config ${WXVERSION} --libs` -g -o $@ $^

# On my computer, I force the following targets to build properly.  
# For other people, I let them fail gracefully, on the assumption that the
# primary interest is getting Do178Builder to build natively, and that the
# other stuff is just gravy.
.phony: Others
ifneq	(i_am_rburkey,i_am_${USER})
.ignore: Others
endif
Others:
	${MAKE} utilities
	${MAKE} Do178Builder.exe
	${MAKE} Do178Builder-utility.exe

.phony:	utilities
utilities:
	gcc -g -O0 -o Do178Builder-utility/Do178Builder-utility \
		`xml2-config --cflags` \
		Do178Builder-utility/*.c \
		`xml2-config --libs`

%.o:	%.cpp Do178Builder.h Do178Builder_wdr.h		
	gcc `wx-config ${WXVERSION} --cppflags` -I${OLDDIR}/include -I${OLDDIR}/lib -Wall ${WERROR} -c -o $@ $<

%.o:	%.c
	gcc -I${OLDDIR}/include -I${OLDDIR}/lib -Wall ${WERROR} -c -o $@ $<

# Stuff above this line is for compiling natively on Linux, or generically *nix.
#/////////////////////////////////////////////////////////////////////////////	
# Stuff below this line is for cross-compiling a Windows version of the
# software from Linux., assuming cross-compiler and cross-compiled libraries
# previously installed using the "I'm Cross!" project (www.sandroid.org/imcross).
# The build-methods are taken directly from the samples provided in "I'm Cross!"

Do178Builder-utility.exe:
	${TARGET}-gcc -O3 -o $@ \
		-DPCRE_STATIC `${INSTALL_DIR}/bin/pcre-config --cflags-posix` \
		-DLIBXML_STATIC `${INSTALL_DIR}/bin/xml2-config --cflags` \
		Do178Builder-utility/*.c \
		`${INSTALL_DIR}/bin/pcre-config --libs-posix` \
		`${INSTALL_DIR}/bin/xml2-config --libs` -lwsock32
	${TARGET}-strip $@

WX_EXTRA_LIBS= \
	-Xlinker -\( \
	-ladvapi32 -lctl3d32 -lglu32 -lodbc32 -lopengl32 -lstdc++ -lgcc \
	-Xlinker -\)

Do178Builder.exe: \
		Do178Builder.ow \
		Do178Builder_wdr.ow \
		XmlEditorDialogClass.ow \
		SearchBoxClass.ow \
		LoadXmlFile.ow \
		crc32.ow \
		${OLDDIR}/lib/libold.ow
	${TARGET}-gcc -o $@ $^ `${INSTALL_DIR}/bin/wx-config --static --libs` ${WX_EXTRA_LIBS}
	${TARGET}-strip $@

%.ow:	%.cpp Do178Builder.h Do178Builder_wdr.h		
	${TARGET}-gcc `${INSTALL_DIR}/bin/wx-config --static --cxxflags` -Wall ${WERROR} -c -O3 -o $@ $<

%.ow:	%.c
	${TARGET}-gcc -I${OLDDIR}/include -I${OLDDIR}/lib -Wall ${WERROR} -c -O3 -o $@ $<

