#//////////////////////////////////////////////////////////////////////////
# 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".
#
#  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

# 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

scrub:
	-rm Do178Builder Do178Builder-utility/Do178Builder-utility
	-rm `find . -name "*.o"`
	-rm *.exe *.ow
		
install: Do178Builder
	cp Do178Builder /usr/local/bin
	chmod ugo+x /usr/local/bin/Do178Builder	
	
snapshot: scrub
	tar --directory=.. --exclude="*/.snprj" --exclude="*/CVS" -czf ${WEBSITE}/Do178Builder-${DATE}-source.tar.gz Do178Builder-source
	ls -l ${WEBSITE}/*source.tar.gz -t -r
	
snapshot32:
	zip -j Do178Builder-${DATE}-win32.zip .template*xml Release/*exe 
	
.phony:	utilities
utilities:
	cd Do178Builder-utility && \
	gcc -g -O0 -I /usr/include/libxml2 \
		Do178Builder-utility.c \
		WalkNodes.c  \
		Trace.c \
		TraceDesign.c \
		TraceDesignFull.c \
		TraceSystem.c \
		TraceImplement.c \
		TraceVerify.c \
		-lxml2 && \
	mv a.out Do178Builder-utility
	-cp -a Do178Builder-utility/Do178Builder-utility ${HOME}/bin
ifdef MINGW
	cd Do178Builder-utility && \
	${MINGW} -g -O0 -I /usr/include/libxml2 Do178Builder-utility.c WalkNodes.c -lxml2 && \
	mv a.out Do178Builder-utility.exe
endif
	
%.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.

# Here's a target that will set up the computer with the necessary cross-compiler
# and libraries for performing the cross-compilation.  Info on setting up the
# cross-compilation came from pages on the www.wxWidgets.org wiki titled
# "Cross-Compiling Under Linux" and "Install The Mingw Cross-Compiler".  
# Basically, I used those instructions as-is, except that the specific
# package versions listed in those wiki articles didn't work for me, so I'm
# using much more up-to-date packages.  The URLs for the download obviously
# are likely to change over time, but they're the ones I used.
#
# What you get from this is a C cross-compiler (i386-mingw32-gcc), a C++
# cross-compiler (i386-mingw32-g++), assembler (i386-mingw32-as), linker
# (i386-mingw32-ld), etc., as well as libxml2 and wxWidgets.
# 
# I think the only dependency that can kill you if you stick to the versions
# shown is the native version of gcc that you're using.  I was using 
# gcc 4.0.2 when I wrote this.

# 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

BINUTILS_VERSION=2.18
GCC_VERSION=3.4.6
LIBXML2_VERSION=2.6.31
WXVER=2.8
WXWIDGETS_VERSION=${WXVER}.7
MINGW_RUNTIME_VERSION=3.14
W32API_VERSION=3.11
PCRE_VERSION=7.6

BINUTILS_TARBALL=binutils-${BINUTILS_VERSION}.tar.bz2
GCC_TARBALL=gcc-${GCC_VERSION}.tar.bz2
LIBXML2_TARBALL=libxml2-${LIBXML2_VERSION}.tar.gz
MINGW_RUNTIME_TARBALL=mingw-runtime-${MINGW_RUNTIME_VERSION}.tar.gz
W32API_TARBALL=w32api-${W32API_VERSION}.tar.gz
WXWIDGETS_TARBALL=wxWidgets-${WXWIDGETS_VERSION}.tar.gz
PCRE_TARBALL=pcre-${PCRE_VERSION}.tar.bz2

${BINUTILS_TARBALL}:
	wget ftp://ftp.gnu.org/gnu/binutils/${BINUTILS_TARBALL}

${GCC_TARBALL}:
	wget ftp://ftp.gnu.org/gnu/gcc/gcc-${GCC_VERSION}/${GCC_TARBALL}

${LIBXML2_TARBALL}:
	wget ftp://xmlsoft.org/libxml2/${LIBXML2_TARBALL}

${MINGW_RUNTIME_TARBALL}:
	wget http://downloads.sourceforge.net/mingw/${MINGW_RUNTIME_TARBALL}

${W32API_TARBALL}:
	wget http://downloads.sourceforge.net/mingw/${W32API_TARBALL}

${WXWIDGETS_TARBALL}:
	wget http://prdownloads.sourceforge.net/wxwindows/${WXWIDGETS_TARBALL}

${PCRE_TARBALL}:
	wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/${PCRE_TARBALL}

# Wants to be done as root.
setup-for-cross-compilation: \
		${BINUTILS_TARBALL} \
		${GCC_TARBALL} \
		${LIBXML2_TARBALL} \
		${MINGW_RUNTIME_TARBALL} \
		${W32API_TARBALL} \
		${WXWIDGETS_TARBALL} \
		${PCRE_TARBALL}
	-mkdir ${INSTALL_DIR}
	tar -C ${INSTALL_DIR} -xzf ${MINGW_RUNTIME_TARBALL}
	tar -C ${INSTALL_DIR} -xzf ${W32API_TARBALL}
	-mkdir ${BUILD_DIR}
	# Build binutils
	tar -C ${BUILD_DIR} --bzip2 -xf ${CURRENT_DIR}/${BINUTILS_TARBALL}
	cd ${BUILD_DIR}/binutils-${BINUTILS_VERSION} &&
	./configure --prefix=${PREFIX_DIR} --target=${TARGET}
	make -C ${BUILD_DIR}/binutils-${BINUTILS_VERSION}
	make -C ${BUILD_DIR}/binutils-${BINUTILS_VERSION} install
	# Build gcc
	tar -C ${BUILD_DIR} --bzip2 -xf ${CURRENT_DIR}/${GCC_TARBALL}
	cd ${BUILD_DIR}/gcc-${GCC_VERSION} &&
	rm -rf libjava libobjc gcc/java gcc/objc
	cd ${BUILD_DIR}/gcc-${GCC_VERSION} &&
	./configure --prefix=${PREFIX_DIR} --target=${TARGET}
	make -C ${BUILD_DIR}/binutils-${BINUTILS_VERSION} LANGUAGES="c c++"
	make -C ${BUILD_DIR}/binutils-${BINUTILS_VERSION} LANGUAGES="c c++" install
	# Build libxml2.  This is needed only for Do178Builder-utility.exe.
	tar -C ${BUILD_DIR} -xzf ${CURRENT_DIR}/${LIBXML2_TARBALL}
	cd ${BUILD_DIR}/libxml2-${LIBXML2_VERSION} &&
	./configure --prefix=${PREFIX_DIR} --target=${TARGET} --without-python
	make -C ${BUILD_DIR}/libxml2-${LIBXML2_VERSION}
	make -C ${BUILD_DIR}/libxml2-${LIBXML2_VERSION} install
	# Build wxWidgets.  This is needed only for Do178Builder.
	tar -C ${BUILD_DIR} -xzf ${CURRENT_DIR}/${WXWIDGETS_TARBALL}
	cd ${BUILD_DIR}/wxWidgets-${WXWIDGETS_VERSION} &&
	./configure --prefix=${INSTALL_DIR} --host=${TARGET} --build=i686-linux \
		--with-msw --disable-shared --enable-monolithic
	make -C ${BUILD_DIR}/wxWidgets-${WXWIDGETS_VERSION}
	make -C ${BUILD_DIR}/wxWidgets-${WXWIDGETS_VERSION} install
	# Set up shared libraries.
	/sbin/ldconfig
	# Build PCRE.  This is a subsitute for the regular-expressions
	# functionality omitted from mingw32, and is used only for 
	# Do178Builder-utility.
	tar -C ${BUILD_DIR} --bzip2 -xf ${CURRENT_DIR}/${PCRE_TARBALL}
	cd ${BUILD_DIR}/pcre-${PCRE_VERSION} &&
	./configure --enable-newline-is-anycrlf \
		--prefix=${PREFIX_DIR} \
		--target=${TARGET} \
		--host=${TARGET}
	make -C ${BUILD_DIR}/pcre-${PCRE_VERSION}
	make -C ${BUILD_DIR}/pcre-${PCRE_VERSION} install
	ln --symbolic ${INSTALL_DIR}/include/pcreposix.h ${INSTALL_DIR}/include/regex.h
	# Clear out the garbage.
	strip ${PREFIX_DIR}/bin/${TARGET}-*
	strip ${INSTALL_DIR}/bin/*
	rm ${BUILD_DIR} -rf

# Below are targets to actually perform the cross compilation.
cross-compile: Do178Builder.exe Do178Builder-utility.exe

Do178Builder-utility.exe:
	cd Do178Builder-utility && \
	i386-mingw32-gcc -g -O0 -I ${INSTALL_DIR}/include/libxml2 \
		Do178Builder-utility.c \
		WalkNodes.c  \
		Trace.c \
		TraceDesign.c \
		TraceDesignFull.c \
		TraceSystem.c \
		TraceImplement.c \
		TraceVerify.c \
		-lxml2 -lpcreposix && \
	mv a.exe Do178Builder-utility.exe

LIBSUFFIX=${WXVER}-${TARGET}
WXCONFIG_LIBS_OLD=\
	-L/usr/local/${TARGET}/lib \
	-lwx_msw_xrc-${LIBSUFFIX} \
	-lwx_msw_qa-${LIBSUFFIX} \
	-lwx_msw_html-${LIBSUFFIX} \
	-lwx_msw_adv-${LIBSUFFIX} \
	-lwx_msw_richtext-${LIBSUFFIX} \
	-lwx_msw_core-${LIBSUFFIX} \
	-lwx_base_xml-${LIBSUFFIX} \
	-lwx_base_net-${LIBSUFFIX} \
	-lwx_base-${LIBSUFFIX}
WXCONFIG_LIBS=\
	-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 -\)
	
Do178Builder.exe: \
	Do178Builder.ow \
	Do178Builder_wdr.ow \
	XmlEditorDialogClass.ow \
	SearchBoxClass.ow \
	LoadXmlFile.ow \
	crc32.ow \
	${OLDDIR}/lib/libold.ow
	${TARGET}-gcc -g -o $@ $^ ${WXCONFIG_LIBS} 

%.ow:	%.cpp Do178Builder.h Do178Builder_wdr.h		
	${TARGET}-gcc \
		-I${INSTALL_DIR}/lib/wx/include/${TARGET}-msw-ansi-release-${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 \
		-Wl,--subsystem,windows -mwindows -DHAVE_W32API_H \
		-I${OLDDIR}/include -I${OLDDIR}/lib -Wall -c -g -o $@ $<
		
%.ow:	%.c
	${TARGET}-gcc -I${OLDDIR}/include -I${OLDDIR}/lib -Wall -c -g -o $@ $<

