#//////////////////////////////////////////////////////////////////////////
# 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.
#
#  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
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).
# Make sure that $PREFIX_DIR/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 $PREFIX_DIR/$TARGET/lib and $PREFIX_DIR/$TARGET/include.
PREFIX_DIR=${HOME}/IMCROSS
TARGET=i386-mingw32
INSTALL_DIR=${PREFIX_DIR}/${TARGET}
BUILD_DIR=temp-cross-compiler-build
CURRENT_DIR:=$(shell pwd)
WXDIR:=${CURRENT_DIR}/wx2

# 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

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 Do178Builder Others

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

.phony:	snapshot	
snapshot: snap-win snap-source
	ls -l ${WEBSITE}/*source.tar.gz  ${WEBSITE}/*.zip -t -r

.phony: snap-win
snap-win:
	make clean
	zip ${WEBSITE}/Do178Builder-${DATE}-win32.zip *.exe .template*.xml

.phony: snap-source
snap-source:
	make scrub
	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
	
snapshot32:
	zip -j Do178Builder-${DATE}-win32.zip .template*xml Release/*exe 
	
.phony: Others
Others:
	-make utilities
	-make Do178Builder.exe
	-make Do178Builder-utility.exe	
	
.phony:	utilities
utilities:
	cd Do178Builder-utility && \
	gcc -g -O0 -o Do178Builder-utility -I /usr/include/libxml2 \
		Do178Builder-utility.c \
		WalkNodes.c  \
		Trace.c \
		TraceDesign.c \
		TraceDesignFull.c \
		TraceSystem.c \
		TraceImplement.c \
		TraceVerify.c \
		-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 Do178Builder-utility.exe

Do178Builder-utility.exe:
	cd Do178Builder-utility && \
	${TARGET}-gcc -O3 -o ${CURRENT_DIR}/$@ -DPCRE_STATIC -DLIBXML_STATIC \
		-I ${INSTALL_DIR}/include/libxml2 \
		`${INSTALL_DIR}/bin/pcre-config --cflags-posix` \
		Do178Builder-utility.c \
		WalkNodes.c  \
		Trace.c \
		TraceDesign.c \
		TraceDesignFull.c \
		TraceSystem.c \
		TraceImplement.c \
		TraceVerify.c \
		`${INSTALL_DIR}/bin/pcre-config --libs-posix` \
		-lxml2 -lwsock32
	${TARGET}-strip $@

LIBSUFFIX=${WXVER}-${TARGET}

ifndef dontdothis
# This is the setting used.
WXCONFIG_LIBS=\
	 -Wl,--subsystem,windows -mwindows -mthreads \
	-L/usr/local/${TARGET}/lib \
	-Xlinker -\( \
	-lwx_msw-${LIBSUFFIX} \
	-lwxzlib-${LIBSUFFIX} \
	-lwxtiff-${LIBSUFFIX} \
	-lwxregex-${LIBSUFFIX} \
	-lwxpng-${LIBSUFFIX} \
	-lwxjpeg-${LIBSUFFIX} \
	-lwxexpat-${LIBSUFFIX} \
	-lpcre -lstdc++ -lgcc -lodbc32 -lwsock32 \
	-lwinspool -lwinmm -lshell32 -lcomctl32 -lctl3d32 -lodbc32 -ladvapi32 \
	-lodbc32 -lwsock32 -lopengl32 -lglu32 -lole32 -loleaut32 -luuid \
	-Xlinker -\)
else
# This setting isn't used.  It should work, but doesn't.
WXCONFIG_LIBS:=$(shell ${INSTALL_DIR}/bin/wx-config --static=yes --libs)
endif

ifdef dothis
# I used to use this setting, but don't any longer.
WXCONFIG_CXXFLAGS:=\
	-I${INSTALL_DIR}/lib/wx/include/${TARGET}-msw-ansi-release-static-${WXVER} \
	-I${INSTALL_DIR}/include/wx-${WXVER} \
	-DSTRICT \
	-DWINVER=0x0400 -D__WIN95__ -D__GNUWIN32__ -D__WIN32__ \
	-D__WXMSW__ -D__WINDOWS__ \
	-D_X86_=1 -DWIN32 -D_WIN32 \
	-DWIN32_LEAN_AND_MEAN \
	-DHAVE_W32API_H \
	-I${OLDDIR}/include -I${OLDDIR}/lib
else
# This is the setting used.	
WXCONFIG_CXXFLAGS:=$(shell ${INSTALL_DIR}/bin/wx-config --static=yes --cxxflags)
endif

ifdef dothis
# I used to use this setting, but don't any longer.
WXCONFIG_CFLAGS:=-I${OLDDIR}/include -I${OLDDIR}/lib
else
# This is the setting used.
WXCONFIG_CFLAGS:=$(shell ${INSTALL_DIR}/bin/wx-config --static=yes --cflags) -I${OLDDIR}/include -I${OLDDIR}/lib
endif

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

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

