###############################################################################
# This file is part of the IPCop Firewall.                                    #
#                                                                             #
# IPCop 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.                                         #
#                                                                             #
# IPCop 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 IPCop; if not, write to the Free Software                        #
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA    #
#                                                                             #
# Makefiles are based on LFSMake, which is                                    #
# Copyright (C) 2002 Rod Roard <rod@sunsetsystems.com>                        #
#                                                                             #
# Modifications by:                                                           #
# ??-12-2003 Mark Wormgoor < mark@wormgoor.com>                               #
#          - Modified Makefile for IPCop build                                #
#                                                                             #
# $Id: gcc 2175 2008-12-10 19:58:09Z gespinasse $
#                                                                             #
###############################################################################

###############################################################################
# Definitions
###############################################################################

include Config

PKG_NAME   = gcc
VER        = 4.2.4
HOST_ARCH  = all
OTHER_SRC  = yes

THISAPP    = $(PKG_NAME)-$(VER)
DL_FILE    = $(THISAPP).tar.bz2
DL_FROM    = $(URL_GNU)/gcc/gcc-$(VER)
DIR_APP    = $(DIR_SRC)/$(THISAPP)
CFLAGS     =
CXXFLAGS   =

ifeq "$(STAGE)" "toolchain"
	TARGET        = $(DIR_INFO)/$(STAGE_ORDER)_$(STAGE)/$(THISAPP)-pass$(PASS)
endif

ifeq "$(STAGE)" "base"
	TARGET        = $(DIR_INFO)/$(STAGE_ORDER)_$(STAGE)/$(THISAPP)
endif

ifeq "$(STAGE)" "ipcop"
	TARGET        = $(DIR_INFO)/$(STAGE_ORDER)_$(STAGE)/$(THISAPP)-cross-compile
endif

# sparc does not build on base stage without gcc bootstrap
ifeq "$(MACHINE)" "sparc"
	GCCBOOTSTRAP = 
else
	GCCBOOTSTRAP = --disable-bootstrap
endif
###############################################################################
# Top-level Rules
###############################################################################
objects = $(DL_FILE)

$(DL_FILE)				= $(DL_FROM)/$(DL_FILE)

$(DL_FILE)_MD5				= d79f553e7916ea21c556329eacfeaa16

install : $(TARGET)

check : $(patsubst %,$(DIR_CHK)/%,$(objects))

download :$(patsubst %,$(DIR_DL)/%,$(objects))

md5 : $(subst %,%_MD5,$(objects))

###############################################################################
# Downloading, checking, md5sum
###############################################################################

$(patsubst %,$(DIR_CHK)/%,$(objects)) :
	@$(CHECK)

$(patsubst %,$(DIR_DL)/%,$(objects)) :
	@$(LOAD)

$(subst %,%_MD5,$(objects)) :
	@$(MD5)

###############################################################################
# Installation Details
###############################################################################

$(TARGET) : $(patsubst %,$(DIR_DL)/%,$(objects))
	@$(PREBUILD)

ifeq "$(PASS)" "1"
	@rm -rf $(DIR_APP) $(DIR_SRC)/gcc-build && cd $(DIR_SRC) && tar jxf $(DIR_DL)/$(DL_FILE)
	@mkdir $(DIR_SRC)/gcc-build

	cd $(DIR_SRC)/gcc-build && CC="gcc -B/usr/bin/" ../$(THISAPP)/configure --prefix=/$(TOOLS_DIR) \
										--with-local-prefix=/$(TOOLS_DIR) \
										--disable-nls \
										--enable-shared \
										--enable-languages=c \
										--disable-multilib \
										--disable-bootstrap
	cd $(DIR_SRC)/gcc-build && make -j $(PARALLELISM)
	cd $(DIR_SRC)/gcc-build && make install
	ln -vs gcc /$(TOOLS_DIR)/bin/cc
endif

ifeq "$(PASS)" "2"
	@rm -rf $(DIR_APP) $(DIR_SRC)/gcc-build && cd $(DIR_SRC) && tar jxf $(DIR_DL)/$(DL_FILE)
	@mkdir $(DIR_SRC)/gcc-build

	cd $(DIR_APP) && sed -i 's@\./fixinc\.sh@-c true@' gcc/Makefile.in
	cd $(DIR_APP) && sed -i 's/^XCFLAGS =$$/& -fomit-frame-pointer/' gcc/Makefile.in
	# this looks (and is) ugly, see LFS book, chapter 5.11 GCC-4.2.3 - Pass 2 for info
	cd $(DIR_APP) && for file in $$(find gcc/config -name linux64.h -o -name linux.h -o -name sysv4.h); do \
				cp -uv $$file $$file.orig; \
				sed -e 's@/lib\(64\)\?\(32\)\?/ld@/$(TOOLS_DIR)&@g' \
					-e 's@/usr@/$(TOOLS_DIR)@g' $$file.orig > $$file; \
				echo -e "\n#undef STANDARD_INCLUDE_DIR\n#define STANDARD_INCLUDE_DIR 0" >> $$file; \
				touch $$file.orig; \
			done

	cd $(DIR_SRC)/gcc-build && ../$(THISAPP)/configure	--prefix=/$(TOOLS_DIR) \
								--with-local-prefix=/$(TOOLS_DIR) \
								--enable-clocale=gnu \
								--enable-shared \
								--enable-threads=posix \
								--enable-__cxa_atexit \
								--enable-languages=c,c++ \
								--disable-libstdcxx-pch \
								--disable-multilib
	cd $(DIR_SRC)/gcc-build && make -j $(PARALLELISM)

ifeq "$(RUNNING_TEST)" "yes"
	# Do not to stop on error in tests
	-cd $(DIR_SRC)/gcc-build; make -j 1 -k check &> $(DIR_TEST)/$(THISAPP)-$(STAGE_ORDER).log
	-cd $(DIR_SRC)/gcc-build; ../gcc-$(VER)/contrib/test_summary >> $(DIR_TEST)/$(THISAPP)-$(STAGE_ORDER).log
endif
	cd $(DIR_SRC)/gcc-build && make install
endif

ifeq "$(STAGE)" "base"
	@rm -rf $(DIR_APP) $(DIR_SRC)/gcc-build && cd $(DIR_SRC) && tar jxf $(DIR_DL)/$(DL_FILE)
	@mkdir $(DIR_SRC)/gcc-build

	cd $(DIR_APP) && sed -i 's/install_to_$$(INSTALL_DEST) //' libiberty/Makefile.in
	cd $(DIR_APP) && sed -i 's/^XCFLAGS =$$/& -fomit-frame-pointer/' gcc/Makefile.in
	cd $(DIR_APP) && sed -i 's@\./fixinc\.sh@-c true@' gcc/Makefile.in
	cd $(DIR_APP) && sed -i 's/@have_mktemp_command@/yes/' gcc/gccbug.in
	cd $(DIR_SRC)/gcc-build && ../$(THISAPP)/configure	--prefix=/usr \
								--libexecdir=/usr/lib \
								--enable-shared \
								--enable-threads=posix \
								--enable-__cxa_atexit \
								--enable-clocale=gnu \
								--enable-languages=c,c++ \
								$(GCCBOOTSTRAP)
	cd $(DIR_SRC)/gcc-build && make -j $(PARALLELISM)

ifeq "$(RUNNING_TEST)" "yes"
	# Do not to stop on error in tests
	-cd $(DIR_SRC)/gcc-build; make -j 1 -k check &> $(DIR_TEST)/$(THISAPP)-$(STAGE_ORDER).log
	-cd $(DIR_SRC)/gcc-build; ../gcc-$(VER)/contrib/test_summary >> $(DIR_TEST)/$(THISAPP)-$(STAGE_ORDER).log
endif

	cd $(DIR_SRC)/gcc-build && make install
	ln -sv ../usr/bin/cpp /lib
	ln -sv gcc /usr/bin/cc
endif

ifeq "$(STAGE)" "ipcop"
ifeq "$(MACHINE_REAL)" "sparc64"
	@rm -rf $(DIR_APP) $(DIR_SRC)/gcc-build && cd $(DIR_SRC) && tar jxf $(DIR_DL)/$(DL_FILE)
	@mkdir $(DIR_SRC)/gcc-build

	cd $(DIR_SRC)/gcc-build && ../$(THISAPP)/configure	--prefix=/usr/$(MACHINE_REAL)-linux \
								--disable-multilib \
								--disable-nls \
								--disable-shared \
								--disable-threads \
								--enable-languages=c \
								--host=$(MACHINE)-linux \
								--target=$(MACHINE_REAL)-linux
	cd $(DIR_SRC)/gcc-build && make all-gcc
	cd $(DIR_SRC)/gcc-build && make install-gcc
ifeq "$(RUNNING_TEST)" "yes"
	# How should we run these tests? They all fail actually
	# Do not to stop on error in tests
	-cd $(DIR_SRC)/gcc-build; make -j 1 -k check &> $(DIR_TEST)/$(THISAPP)-$(STAGE_ORDER).log
	-cd $(DIR_SRC)/gcc-build; ../gcc-$(VER)/contrib/test_summary >> $(DIR_TEST)/$(THISAPP)-$(STAGE_ORDER).log
endif
endif
endif
	@rm -rf $(DIR_APP) $(DIR_SRC)/gcc-build
	@$(POSTBUILD)
