#//////////////////////////////////////////////////////////////////////////
# Filename:	Makefile
# Author:	Ron Burkey
# Purpose:	Makefile for Do178Builder.  The assumption is that GNU
#		make and gcc (or mingw32) are used.
# 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.
#		01/29/09 RSB	All of the stuff related to setting up a
#				cross-compilation environment has been 
#				removed, as this stuff is now in a separate
#				project (www.sandroid.org/imcross).  Added
#				the targets 'default' and 'all', and changed
#				the 'clean' target to conform somewhat better
#				to most people's expectations.  The default
#				target now builds the utilities, which it 
#				didn't used to.  Moreover, the utility program
#				targets have been rationalized so that that
#				work in a much more reasonable fashion now.
#		01/31/09 RSB	Fixed the Windows IMCROSS cross-build.
#
#  Copyright 2001-2003,2005,2006,2008-2009 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

# Here's a list of reasonable build targets:
#	make [WXVER=ver]	Clean build of Do178Builder and 
#				Do178Builder-utility for Linux.
#				If WXVER=ver isn't present, the default
#				version of wxWidgets installed on your
#				computer is used.  If present, ver should
#				be something like 2.8, 2.6, 2.4, etc.,
#				and needs to correspond to a version
#				actually present on your computer.
#	make [WXVER=ver] all	Same, but builds Windows .exe versions
#				of the programs as well.  The method 
#				assumes that the IMCROSS cross-compilation
#				environment (www.sandroid.org/imcross) is
#				installed, that within IMCROSS the option
#				to install wxWidgets hasn't been disabled,
#				that the installation directory of IMCROSS
#				is ~/IMCROSS/, and that 
#				~/IMCROSS/i386-mingw32/bin/ is in your
#				PATH (probably at the end).  If the IMCROSS
#				installation directory is different, you
#				can change it by adding PREFIX_DIR=something
#				to the 'make' command line.  The default
#				corresponds to PREFIX_DIR=${HOME}/IMCROSS.
#	make clean		Removes all already-built object files and
#				executable files.
#	sudo make [WXVER=ver] [PREFIX_DIR=something] install
#	make snapshot		Build all Linux and Windows targets cleanly
#				and create the snapshot tarballs and 
#				zipfiles for the website.

#//////////////////////////////////////////////////////////////////////////
#			   *** IMPORTANT NOTE ***
#	In versions prior to 01/29/2009, would not build with a Unicode
#	build of wxWidgets, though I never knew this until now.
#	In later versions, will build (I hope) with any non-Unicode
#	build of wxWidgets 2.4 or later, but will also build with
#	Unicode builds of wxWidgets 2.8.4 or later.  Wrong versions
#	of wxWidgets are detected at compile-time by statements within
#	Do178Builder.h.  At this writing, I've tested this statement
#	only with the following wxWidgets versions:
#		wxWidgets 2.8.7.1	Unicode	Linux
#		wxWidgets 2.8.0		ANSI	Windows
#		wxWidgets 2.6.3		ANSI	Linux
#		wxWidgets 2.4		ANSI	Linux
#//////////////////////////////////////////////////////////////////////////

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

# Some stuff related to cross-compilation for Windows from Linux.  This assumes
# use of the IMCROSS cross-compilation environment (www.sandroid.org/imcross).
# Note that all IMCROSS tools are located explicitly under the paths defined 
# below, and do not need to appear in your PATH environment variable.
ifndef PREFIX_DIR
PREFIX_DIR=${HOME}/IMCROSS
endif
TARGET=i386-mingw32
INSTALL_DIR=${PREFIX_DIR}/${TARGET}
BUILD_DIR=temp-cross-compiler-build
CURRENT_DIR:=$(shell pwd)
WXDIR:=${CURRENT_DIR}/wx2

# Stuff related to Do178Builder-utility
UDIR=Do178Builder-utility/
UTIL=${UDIR}Do178Builder-utility

# If MINGW is defined, then the 'utilities' target will try to build
# exe files for all utility programs.  It won't work, but it will try!
#MINGW=/usr/local/cross-tools/bin/i386-mingw32msvc-gcc

default: scrub Do178Builder ${UTIL}

all:	default Others

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

scrub:
	-rm Do178Builder ${UTIL}
	-rm `find . -name "*.o"`
	-rm `find . -name "*.exe"`
	-rm `find . -name "*.ow"`
		
install: Do178Builder ${UTIL}
	cp Do178Builder ${UTIL} /usr/local/bin
	chmod ugo+x /usr/local/bin/Do178Builder*

.phony:	snapshot	
snapshot: snap-source snap-win
	ls -ltr ${WEBSITE}/*win32.zip ${WEBSITE}/*source.tar.gz

.phony: snap-win
snap-win:
	make clean Others
	-rm ${WEBSITE}/Do178Builder-${DATE}-win32.zip
	zip -j ${WEBSITE}/Do178Builder-${DATE}-win32.zip *.exe ${UTIL}.exe .template*.xml
	ls -l ${WEBSITE}/*win32.zip

.phony: snap-source
snap-source:
	make clean
	-rm ${WEBSITE}/Do178Builder-${DATE}-source.tar.gz
	tar --directory=.. --exclude="*/.snprj" --exclude="*/CVS" --exclude="*.tar.bz2" \
		--exclude="*.tar.gz" --exclude="setup_*" --exclude="${BUILD_DIR}*" \
		--exclude="*~" --exclude="*.bak" --exclude=SNDB4 \
		-czf ${WEBSITE}/Do178Builder-${DATE}-source.tar.gz Do178Builder-source
	ls -l ${WEBSITE}/*source.tar.gz

.phony: Others
Others:
	-make Do178Builder.exe
	-make ${UTIL}.exe	

UTILITY_SOURCE=$(wildcard ${UDIR}*.c)
UTILITY_HEADERS=$(wildcard ${UDIR}*.h)
${UTIL}: ${UTILITY_SOURCE} ${UTILITY_HEADERS}
	gcc -g -O0 -o $@ \
		-I ${UDIR} -I /usr/include/libxml2 \
		${UTILITY_SOURCE} -lxml2
	
%.o:	%.cpp Do178Builder.h Do178Builder_wdr.h		
	gcc `${WXCONFIG} ${WXVERSION} --cppflags` -I${OLDDIR}/include -I${OLDDIR}/lib -Wall -c -g -o $@ $<
		
%.o:	%.c
	gcc -I${OLDDIR}/include -I${OLDDIR}/lib -Wall -c -g -o $@ $<

# Stuff above this line is for compiling on Linux, or generically *nix.
#/////////////////////////////////////////////////////////////////////////////	
# Stuff below this line is for cross-compiling a Windows version of the
# software from Linux.

# Below are targets to actually perform the cross compilation.
cross-compile: Do178Builder.exe ${UTIL}.exe

${UTIL}.exe: ${UTILITY_SOURCE} ${UTILITY_HEADERS}
	${INSTALL_DIR}/bin/gcc -O3 -o $@ -DPCRE_STATIC -DLIBXML_STATIC \
		-I ${UDIR} \
		`${INSTALL_DIR}/bin/xml2-config --cflags` \
		`${INSTALL_DIR}/bin/pcre-config --cflags-posix` \
		${UTILITY_SOURCE} \
		`${INSTALL_DIR}/bin/xml2-config --libs` \
		`${INSTALL_DIR}/bin/pcre-config --libs-posix` \
		-lwsock32
	${INSTALL_DIR}/bin/strip $@

WXCONFIG_LIBS:=\
	$(shell ${INSTALL_DIR}/bin/pcre-config --libs-posix) \
	$(shell ${INSTALL_DIR}/bin/wx-config --static=yes --libs) \
	-lstdc++
WXCONFIG_CFLAGS:=\
	$(shell ${INSTALL_DIR}/bin/pcre-config --cflags-posix) \
	$(shell ${INSTALL_DIR}/bin/wx-config --static=yes --cflags) \
	-I${OLDDIR}/include -I${OLDDIR}/lib
WXCONFIG_CXXFLAGS:=\
	$(shell ${INSTALL_DIR}/bin/wx-config --static=yes --cxxflags)

Do178Builder.exe: \
		Do178Builder.ow \
		Do178Builder_wdr.ow \
		XmlEditorDialogClass.ow \
		SearchBoxClass.ow \
		LoadXmlFile.ow \
		crc32.ow \
		${OLDDIR}/lib/libold.ow
	${INSTALL_DIR}/bin/gcc -o $@ $^ ${WXCONFIG_LIBS} 
	${INSTALL_DIR}/bin/strip $@

%.ow:	%.cpp Do178Builder.h Do178Builder_wdr.h		
	${INSTALL_DIR}/bin/gcc ${WXCONFIG_CXXFLAGS} -Wall -c -O3 -o $@ $<
		
%.ow:	%.c
	${INSTALL_DIR}/bin/gcc ${WXCONFIG_CFLAGS} -Wall -c -O3 -o $@ $<

