# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as 
# published by the Free Software Foundation here:
# (http://www.gnu.org/licenses/gpl-2.0.html)
# 
# This program 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.

export SRCPKG = gcc
export DEPENDS = dash,glibc,gmp,libgcc,libstdc++,mpc,mpfr,zlib
export BUILDDEPS = gcc,libgcc-dev
export ARCH = x86_64
export URL = https://gcc.gnu.org/
REPO = dev
BRIEF = GNU compiler collection
DESC = The GNU Compiler Collection includes front ends for C, C++, \
Objective-C, Fortran, Java, Ada, and Go, as well as libraries for these \
languages (libstdc++, libgcj,...).
SLVER = 5

ARCHIVE := $(PWD)/SRC/$(shell ls SRC|egrep '(bz2|gz|tar|xz)$$'|tail -1)
TYPE := $(shell file -ib $(ARCHIVE)|cut -d';' -f1|tr -d '\n')
SRCDIR := $(shell tar -tf $(ARCHIVE)|head -1|sed 's/\/.*//')
PATCHDIR := $(PWD)/SRC/patches
BUILDDIR := $(PWD)/BUILD
MACHINE := $(gcc -dumpmachine)
export VERSION := $(shell echo $(SRCDIR)|egrep -o '\-[0-9].*'| \
	sed 's/^-//')-$(SLVER)

LIBGCC := $(PWD)/libgcc
LIBGCCROOT := $(LIBGCC)/ROOT
LIBGCCDEV := $(PWD)/libgcc-dev
LIBGCCDEVROOT := $(LIBGCCDEV)/ROOT
LIBSTDC := $(PWD)/libstdc++
LIBSTDCROOT := $(LIBSTDC)/ROOT

include /usr/share/sl/Makefile.snaplinux

$(SRCDIR)/configure: $(ARCHIVE)
	@echo "Extracting archive..."
	@if [ '$(TYPE)' == 'application/x-bzip2' ]; then \
		tar -jxf $(ARCHIVE); \
	elif [ '$(TYPE)' == 'application/x-gzip' ]; then \
		tar -zxf $(ARCHIVE); \
	elif [ '$(TYPE)' == 'application/x-tar' ]; then \
		tar -xf $(ARCHIVE); \
	elif [ '$(TYPE)' == 'application/x-xz' ]; then \
		tar -xf $(ARCHIVE); \
	else \
		echo 'Unable to determine archive type'; \
		exit 1; \
	fi
	@touch $(SRCDIR)/configure

$(BUILDDIR)/config.log: $(SRCDIR)/configure
	@cd $(SRCDIR) && \
	for patch in `find $(PATCHDIR) -name \*.patch|sort`; do \
		patch --verbose -Np1 -i $$patch; \
	done
	@if [ ! -d $(BUILDDIR) ]; then \
		mkdir -v $(BUILDDIR); \
	fi
	@cd $(BUILDDIR); \
	../$(SRCDIR)/configure \
		--prefix=/usr \
		--enable-languages=c,c++ \
		--disable-multilib \
		--disable-bootstrap \
		--with-system-zlib \
		--without-cloog \
		--without-isl \
		--build=x86_64-snap-linux-gnu \
		--host=x86_64-snap-linux-gnu \
		--target=x86_64-snap-linux-gnu

$(BUILDDIR)/gcc: $(BUILDDIR)/config.log
	@cd $(BUILDDIR) && make

$(ROOT): $(BUILDDIR)/gcc
	@if [ -d $(ROOT) ]; then \
		touch $(ROOT); \
	else \
		mkdir -v $(ROOT); \
	fi

	@cd $(BUILDDIR) && make install DESTDIR=$(ROOT) && \
	install -v -d -m 755 $(ROOT)/lib && \
	ln -sv /usr/bin/cpp $(ROOT)/lib/cpp && \
	ln -sv gcc $(ROOT)/usr/bin/cc && \
	install -v -dm755 $(ROOT)/usr/lib64/bfd-plugins && \
	ln -sfv ../../libexec/gcc/$(MACHINE)/$(VERSION)/liblto_plugin.so \
		$(ROOT)/usr/lib64/bfd-plugins && \
	mkdir -pv $(ROOT)/usr/share/gdb/auto-load/usr/lib && \
	mv -v $(ROOT)/usr/lib64/*gdb.py \
		$(ROOT)/usr/share/gdb/auto-load/usr/lib && \
	rm -v $(ROOT)/usr/share/info/dir

	@install -v -d $(LIBGCC)/SL && \
	install -v -d $(LIBGCCROOT) && \
	install -v -d $(LIBGCCDEV)/SL && \
	install -v -d $(LIBGCCDEVROOT) && \
	install -v -d $(LIBSTDC)/SL && \
	install -v -d $(LIBSTDCROOT) && \
	install -v -d -m 755 $(LIBGCCROOT)/usr/lib && \
	install -v -d -m 755 $(LIBGCCDEVROOT)/usr/lib && \
	install -v -d -m 755 $(LIBSTDCROOT)/usr/lib && \
	mv -v $(ROOT)/usr/lib64/libgcc_s.so* $(LIBGCCROOT)/usr/lib && \
	mv -v $(ROOT)/usr/lib64/libstdc++* $(LIBSTDCROOT)/usr/lib && \
	for file in `find $(ROOT)/usr/lib64 \( -type f -o -type l \) \
	-maxdepth 1`; do \
		path=`dirname $$file|sed "s[$(ROOT)[$(LIBGCCDEVROOT)["`; \
		mkdir -p $$path; \
		mv $$file $$path; \
	done && \
	find $(ROOT) -type d -empty -delete && \
	cd $(LIBGCC) && make -f ../Makefile.libgcc && mv *.slp ../ && \
	cd $(LIBGCCDEV) && make -f ../Makefile.libgcc-dev && mv *.slp ../ && \
	cd $(LIBSTDC) && make -f ../Makefile.libstdc++ && mv *.slp ../

test: $(ROOT)
	@cd $(BUILDDIR); \
	make -k check

clean:
	@rm -rvf $(ROOT) \
		$(LIBGCC) \
		$(LIBGCCDEV) \
		$(LIBSTDC) \
		$(SLINFO) \
		$(MANIFEST) \
		$(FILES) \
		$(SRCDIR) \
		$(BUILDDIR)
