#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1
export DH_OPTIONS

VERSION		= $(shell sed -ne '/released/{s/^.*GCC //;s/ release.*$$//;p;q}' ChangeLog)

#LTO_OPT := -flto -ffat-lto-objects

include /usr/share/cdbs/1/class/mingw64.mk

gcc_env_vars = 	CFLAGS="-O2 $(LTO_OPT) -DWINPTHREAD_STATIC" \
		CFLAGS_FOR_TARGET="-DWINPTHREAD_STATIC -DGC_NOT_DLL" \
		CXXFLAGS_FOR_TARGET="-mthreads -DWINPTHREAD_STATIC -DGC_NOT_DLL" \
		CPPFLAGS_FOR_TARGET="-DGC_NOT_DLL" \
		LDFLAGS_FOR_TARGET="-Wl,--enable-runtime-pseudo-reloc-v2 \
                $(LTO_OPT) -Wl,--exclude-libs,libpthread.a"

gcc_confopts =	--prefix=/usr \
		--build=$(DEB_BUILD_GNU_TYPE) \
		--host=$(DEB_HOST_GNU_TYPE) \
		--with-gnu-as --with-gnu-ld --verbose \
		--without-newlib \
		--disable-multilib \
		--enable-cxx-flags=-DWINPTHREAD_STATIC \
		--with-system-zlib \
		--disable-nls --without-included-gettext \
		--disable-win32-registry \
		--enable-version-specific-runtime-libs \
		--enable-languages="c,c++,fortran" \
		--without-x \
		--enable-hash-synchronization \
		--enable-fully-dynamic-strings \
		--enable-libgcj \
		--enable-java-gc-boehm \
		--disable-libgcj-debug \
                --enable-decimal-float=yes \
		--enable-threads=posix
                #--enable-fixed-point 

gcc_confopts32 = --with-dwarf2 --enable-sjlj-exceptions=no
configure: configure-stamp
configure-stamp:
	dh_testdir
	
	mkdir -p build/64
	cd build/64 && $(gcc_env_vars) $(CURDIR)/configure \
		--target=$(MINGW64_TARGET) \
		--with-sysroot=$(MINGW64_SYSROOT) \
		$(gcc_confopts)
	
	mkdir -p build/32
	cd build/32 && $(gcc_env_vars) $(CURDIR)/configure \
		--target=$(MINGW32_TARGET) \
		--with-sysroot=$(MINGW32_SYSROOT) \
		$(gcc_confopts) $(gcc_confopts32)
	touch configure-stamp

build: build-stamp
build-stamp: configure-stamp
	
	cd build/64 && $(MAKE) $(MAKEOPTS) all
	cd build/32 && $(MAKE) $(MAKEOPTS) all
	
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -rf build-stamp configure-stamp build debian/tmp
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_installdirs
	
	rm -rf $(BUILD_ROOT)
	
	mkdir -p $(BUILD_ROOT)
	cd build/64 && $(MAKE) $(MAKEOPTS) DESTDIR=$(CURDIR)/$(BUILD_ROOT)/64 install 
	cd build/32 && $(MAKE) $(MAKEOPTS) DESTDIR=$(CURDIR)/$(BUILD_ROOT)/32 install 
	
	# These files are in conflict with system files
	rm -rf $(BUILD_ROOT)/32/usr/share/info
	rm -f  $(BUILD_ROOT)/32/usr/lib/libiberty*
	rm -f  $(BUILD_ROOT)/32/usr/share/man/man7/*
	rm -f  $(BUILD_ROOT)/32/usr/bin/vxaddr2line
	rm -rf $(BUILD_ROOT)/64/usr/share/info
	rm -f  $(BUILD_ROOT)/64/usr/lib/libiberty*
	rm -f  $(BUILD_ROOT)/64/usr/share/man/man7/*
	rm -f  $(BUILD_ROOT)/64/usr/bin/vxaddr2line
	
	mkdir -p $(BUILD_ROOT)/32/lib
	ln -sf ../usr/bin/$(MINGW32_TARGET)-cpp $(BUILD_ROOT)/32/lib/$(MINGW32_TARGET)-cpp
	mkdir -p $(BUILD_ROOT)/64/lib
	ln -sf ../usr/bin/$(MINGW64_TARGET)-cpp $(BUILD_ROOT)/64/lib/$(MINGW64_TARGET)-cpp
	
	rm -f $(BUILD_ROOT)/32/usr/lib/libcc1*
	rm -f $(BUILD_ROOT)/64/usr/lib/libcc1*
	
	# ====================== BEGIN JAVA STUFF =========================
	#mkdir -p $(BUILD_ROOT)/32/lib/gcc/$(MINGW32_TARGET)/$(VERSION)/
	#cp debian/ecj.jar $(BUILD_ROOT)/32/lib/gcc/$(MINGW32_TARGET)/$(VERSION)/
	#gcj debian/ecj.jar --main=org.eclipse.jdt.internal.compiler.batch.GCCMain -o $(BUILD_ROOT)/32/lib/gcc/$(MINGW32_TARGET)/$(VERSION)/ecj1
	#
	#mkdir -p $(BUILD_ROOT)/32$(MINGW32_SYSROOT)mingw/lib
	#mv $(BUILD_ROOT)/32/usr/lib/gcj-$(VERSION)-12 $(BUILD_ROOT)/32$(MINGW32_SYSROOT)mingw/lib
	#mv $(BUILD_ROOT)/32/usr/lib/pkgconfig $(BUILD_ROOT)/32$(MINGW32_SYSROOT)mingw/lib
	#mv $(BUILD_ROOT)/32/usr/lib/security $(BUILD_ROOT)/32$(MINGW32_SYSROOT)mingw/lib
	#mv $(BUILD_ROOT)/32/usr/lib/*properties $(BUILD_ROOT)/32$(MINGW32_SYSROOT)mingw/lib
	#
	#mkdir -p $(BUILD_ROOT)/32$(MINGW32_SYSROOT)mingw/share
	#mv $(BUILD_ROOT)/32/usr/share/java $(BUILD_ROOT)/32$(MINGW32_SYSROOT)mingw/share
	#(cd $(BUILD_ROOT)/32$(MINGW32_SYSROOT)mingw/share/java; for i in libgcj libgcj-tools; do ln -s $$i-$(VERSION).jar $$i.jar; done)
	#
	#mkdir -p $(BUILD_ROOT)/64/lib/gcc/$(MINGW64_TARGET)/$(VERSION)/
	#cp debian/ecj.jar $(BUILD_ROOT)/64/lib/gcc/$(MINGW64_TARGET)/$(VERSION)/
	#gcj debian/ecj.jar --main=org.eclipse.jdt.internal.compiler.batch.GCCMain -o $(BUILD_ROOT)/64/lib/gcc/$(MINGW64_TARGET)/$(VERSION)/ecj1
	#
	#mkdir -p $(BUILD_ROOT)/64$(MINGW64_SYSROOT)mingw/lib
	#mv $(BUILD_ROOT)/64/usr/lib/gcj-$(VERSION)-12 $(BUILD_ROOT)/64$(MINGW64_SYSROOT)mingw/lib
	#mv $(BUILD_ROOT)/64/usr/lib/pkgconfig $(BUILD_ROOT)/64$(MINGW64_SYSROOT)mingw/lib
	#mv $(BUILD_ROOT)/64/usr/lib/security $(BUILD_ROOT)/64$(MINGW64_SYSROOT)mingw/lib
	#mv $(BUILD_ROOT)/64/usr/lib/*properties $(BUILD_ROOT)/64$(MINGW64_SYSROOT)mingw/lib
	#
	#mkdir -p $(BUILD_ROOT)/64$(MINGW64_SYSROOT)mingw/share
	#mv $(BUILD_ROOT)/64/usr/share/java $(BUILD_ROOT)/64$(MINGW64_SYSROOT)mingw/share
	#(cd $(BUILD_ROOT)/64$(MINGW64_SYSROOT)mingw/share/java; for i in libgcj libgcj-tools; do ln -s $$i-$(VERSION).jar $$i.jar; done)
	#
	# ====================== END JAVA STUFF =========================
	
	# We will, instead, use the DLLs from the native build
	#find $(BUILD_ROOT) -name \*.dll -exec rm {} +
	
	#mkdir -p $(BUILD_ROOT)/64/usr/lib/gcc/$(MINGW64_TARGET)/$(VERSION)
	#cp build/64/$(MINGW64_TARGET)/libgcc/shlib/libgcc_s.a $(BUILD_ROOT)/64/usr/lib/gcc/$(MINGW64_TARGET)/$(VERSION)/
	#cp build/64/$(MINGW64_TARGET)/libgcc/libgcc_eh.a $(BUILD_ROOT)/64/usr/lib/gcc/$(MINGW64_TARGET)/$(VERSION)/
	#cp build/64/$(MINGW64_TARGET)/libgcc/libgcc.a $(BUILD_ROOT)/64/usr/lib/gcc/$(MINGW64_TARGET)/$(VERSION)/
	#cp build/64/$(MINGW64_TARGET)/libstdc++-v3/src/.libs/libstdc++.a $(BUILD_ROOT)/64/usr/lib/gcc/$(MINGW64_TARGET)/$(VERSION)/
	
	# The system does not install DLL or their stub libraries
	#for lib in $$(find build/64 -name '*.dll.a'); do \
	#	cp $$lib $(BUILD_ROOT)/64/usr/lib/gcc/$(MINGW64_TARGET)/$(VERSION)/ ; \
	#done
	#mkdir -p $(BUILD_ROOT)/64/usr/$(MINGW64_TARGET)/sys-root/mingw/bin
	#for dll in $$(find build/64 -name '*.dll'); do \
	#	cp $$dll $(BUILD_ROOT)/64/usr/$(MINGW64_TARGET)/sys-root/mingw/bin ; \
	#done
	
	#mkdir -p $(BUILD_ROOT)/32/usr/lib/gcc/$(MINGW32_TARGET)/$(VERSION)
	#cp build/32/$(MINGW32_TARGET)/libgcc/shlib/libgcc_s.a $(BUILD_ROOT)/32/usr/lib/gcc/$(MINGW32_TARGET)/$(VERSION)/
	#cp build/32/$(MINGW32_TARGET)/libgcc/libgcc_eh.a $(BUILD_ROOT)/32/usr/lib/gcc/$(MINGW32_TARGET)/$(VERSION)/
	#cp build/32/$(MINGW32_TARGET)/libgcc/libgcc.a $(BUILD_ROOT)/32/usr/lib/gcc/$(MINGW32_TARGET)/$(VERSION)/
	#cp build/32/$(MINGW32_TARGET)/libstdc++-v3/src/.libs/libstdc++.a $(BUILD_ROOT)/32/usr/lib/gcc/$(MINGW32_TARGET)/$(VERSION)/
	
	# Same as above where the DLL and stub libraries are not copied to destinations
	#for lib in $$(find build/32 -name '*.dll.a'); do \
	#	cp $$lib $(BUILD_ROOT)/32/usr/lib/gcc/$(MINGW32_TARGET)/$(VERSION)/ ; \
	#done
	#mkdir -p $(BUILD_ROOT)/32/usr/$(MINGW32_TARGET)/sys-root/mingw/bin
	#for dll in $$(find build/32 -name '*.dll'); do \
	#	cp $$dll $(BUILD_ROOT)/32/usr/$(MINGW32_TARGET)/sys-root/mingw/bin ; \
	#done
	

	mv -f  $(BUILD_ROOT)/64/usr/lib/gcc/$(MINGW64_TARGET)/lib/* $(BUILD_ROOT)/64/usr/lib/gcc/$(MINGW64_TARGET)/$(VERSION)/
	rm -rf  $(BUILD_ROOT)/64/usr/lib/gcc/$(MINGW64_TARGET)/lib

	mv -f  $(BUILD_ROOT)/32/usr/lib/gcc/$(MINGW32_TARGET)/lib/* \
	$(BUILD_ROOT)/32/usr/lib/gcc/$(MINGW32_TARGET)/$(VERSION)/
	rm -rf  $(BUILD_ROOT)/32/usr/lib/gcc/$(MINGW32_TARGET)/lib

	rm -f $(BUILD_ROOT)/64/usr/bin/$(MINGW64_TARGET)-aot-compile
	rm -f $(BUILD_ROOT)/64/usr/bin/$(MINGW64_TARGET)-rebuild-gcj-db
	rm -f $(BUILD_ROOT)/64/usr/share/man/man1/$(MINGW64_TARGET)-aot-compile.1*
	rm -f $(BUILD_ROOT)/64/usr/share/man/man1/$(MINGW64_TARGET)-gjdoc.1*
	rm -f $(BUILD_ROOT)/64/usr/share/man/man1/$(MINGW64_TARGET)-rebuild-gcj-db.1*
	rm -f $(BUILD_ROOT)/64/usr/share/man/man3/$(MINGW64_TARGET)-ffi.3*
	rm -f $(BUILD_ROOT)/64/usr/share/man/man3/$(MINGW64_TARGET)-ffi_call.3*
	rm -f $(BUILD_ROOT)/64/usr/share/man/man3/$(MINGW64_TARGET)-ffi_prep_cif*
	rm -f $(BUILD_ROOT)/32/usr/bin/$(MINGW32_TARGET)-aot-compile
	rm -f $(BUILD_ROOT)/32/usr/bin/$(MINGW32_TARGET)-rebuild-gcj-db
	rm -f $(BUILD_ROOT)/32/usr/share/man/man1/$(MINGW32_TARGET)-aot-compile.1*
	rm -f $(BUILD_ROOT)/32/usr/share/man/man1/$(MINGW32_TARGET)-gjdoc.1*
	rm -f $(BUILD_ROOT)/32/usr/share/man/man1/$(MINGW32_TARGET)-rebuild-gcj-db.1*
	rm -f $(BUILD_ROOT)/32/usr/share/man/man3/$(MINGW32_TARGET)-ffi.3*
	rm -f $(BUILD_ROOT)/32/usr/share/man/man3/$(MINGW32_TARGET)-ffi_call.3*
	rm -f $(BUILD_ROOT)/32/usr/share/man/man3/$(MINGW32_TARGET)-ffi_prep_cif*
	
	rm -rf $(BUILD_ROOT)/64/usr/share/python
	rm -rf $(BUILD_ROOT)/32/usr/share/python
	
	find $(BUILD_ROOT) -name \*.py -exec rm {} +
	
binary-indep: build install
	#dh_testdir -i
	#dh_testroot -i
	#dh_movefiles -i
	#dh_installdocs -i
	#dh_installexamples -i
	#dh_install -i
	#dh_link -i
	#dh_fixperms -i
	#dh_makeshlibs -i
	#dh_installdeb -i
	#dh_shlibdeps -i
	#dh_installchangelogs ChangeLog
	#dh_gencontrol -i
	#dh_md5sums -i
	#dh_builddeb -i

binary-arch: build install
	dh_testdir -a
	dh_testroot -a
	dh_movefiles -a
	dh_installdocs -a
	dh_installexamples -a
	dh_install -a
	dh_link -a
	dh_fixperms -a
	dh_makeshlibs -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_installchangelogs ChangeLog
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-arch binary-indep
.PHONY: build clean binary-indep binary-arch binary install configure
