# Template file for 'gcc6'
# old isl is necessary
_isl_version=0.16
pkgname=gcc6
version=6.5.0
revision=1
_majorver="${version%%.*}"
_minorver="${version%.*}"
create_wrksrc=yes
hostmakedepends="automake libtool perl flex texinfo zip unzip"
makedepends="gmp-devel mpfr-devel libmpc-devel zlib-devel"
depends="binutils"
short_desc="GNU C Compiler (6.x)"
maintainer="Orphaned <orphan@voidlinux.org>"
license="GFDL-1.2-or-later, GPL-3.0-or-later, LGPL-2.1-or-later, EPL"
homepage="http://gcc.gnu.org"
distfiles="
 ${GNU_SITE}/gcc/gcc-${version}/gcc-${version}.tar.xz
 ${SOURCEFORGE_SITE}/libisl/isl-${_isl_version}.tar.bz2
 https://sourceware.org/pub/java/ecj-4.9.jar"
checksum="
 7ef1796ce497e89479183702635b14bb7a46b53249209a5e0f999bebf4740945
 3899e68047d0a18db5a7d73bdabcbe9246865e73e86efbd95387817d342fd554
 9506e75b862f782213df61af67338eb7a23c35ff425d328affc65585477d34cd"
skip_extraction="ecj-4.9.jar"
nopie=yes
lib32disabled=yes
nocross=yes
python_version=2 #unverified

subpackages="gcc6-gcj gcc6-gcj-ecj libgcj-devel libgcj gcc6-gcj-jdk-compat"

case "$XBPS_TARGET_MACHINE" in
	i686) _triplet="i686-pc-linux-gnu";;
	i686-musl) _triplet="i686-linux-musl";;
	x86_64) _triplet="x86_64-unknown-linux-gnu";;
	x86_64-musl) _triplet="x86_64-linux-musl";;
	armv5tel) _triplet="arm-linux-gnueabi";;
	armv5tel-musl) _triplet="arm-linux-musleabi";;
	armv6l) _triplet="arm-linux-gnueabihf";;
	armv7l) _triplet="armv7l-linux-gnueabihf";;
	armv6l-musl) _triplet="arm-linux-musleabihf";;
	armv7l-musl) _triplet="armv7l-linux-musleabihf";;
	aarch64) _triplet="aarch64-linux-gnu";;
	aarch64-musl) _triplet="aarch64-linux-musl";;
	ppc) _triplet="powerpc-linux-gnu";;
	ppc-musl) _triplet="powerpc-linux-musl";;
	ppc64le) _triplet="powerpc64le-linux-gnu";;
	ppc64le-musl) _triplet="powerpc64le-linux-musl";;
	ppc64) _triplet="powerpc64-linux-gnu";;
	ppc64-musl) _triplet="powerpc64-linux-musl";;
	mips-musl) _triplet="mips-linux-musl";;
	mipshf-musl) _triplet="mips-linux-muslhf";;
	mipsel-musl) _triplet="mipsel-linux-musl";;
	mipselhf-musl) _triplet="mipsel-linux-muslhf";;
esac

case "$XBPS_TARGET_MACHINE" in
	*-musl) depends+=" musl-devel";;
	*) depends+=" glibc-devel";;
esac

post_extract() {
	mv gcc-${version}/* .
	mv isl-${_isl_version} isl
	# https://gcc.gnu.org/ml/java/2008-04/msg00027.html
	cp ${XBPS_SRCDISTDIR}/${pkgname}-${version}/ecj-*.jar ${wrksrc}/ecj.jar
}

pre_configure() {
	# _FORTIFY_SOURCE needs an optimization level.
	sed -i "/ac_cpp=/s/\$CPPFLAGS/\$CPPFLAGS -O2/" {gcc,libiberty}/configure

	# old isl normally tries to call aclocal-1.14 to reconfigure itself
	pushd ${wrksrc}/isl
	autoreconf -if
	popd
}

do_configure() {
	local _args _hash

	_hash=gnu
	case "$XBPS_TARGET_MACHINE" in
		mipselhf-musl) _args+=" --with-arch=mips32r2 --with-float=hard"; _hash=sysv;;
		mipsel-musl) _args+=" --with-arch=mips32r2 --with-float=soft"; _hash=sysv;;
		mipshf-musl) _args+=" --with-arch=mips32r2 --with-float=hard";;
		mips-musl) _args+=" --with-arch=mips32r2 --with-float=soft";;
		armv5*) _args+=" --with-arch=armv5te --with-float=soft";;
		armv6l*) _args+=" --with-arch=armv6 --with-fpu=vfp --with-float=hard";;
		armv7l*) _args+=" --with-arch=armv7-a --with-fpu=vfpv3 --with-float=hard";;
		aarch64*) _args+=" --with-arch=armv8-a";;
		ppc) _args+=" --enable-secureplt";;
		ppc-musl) _args+=" --enable-secureplt --disable-decimal-float";;
		ppc64*)
			_args+=" --with-abi=elfv2 --enable-secureplt"
			sed -i 's/lib64/lib/' gcc/config/rs6000/linux64.h
			;;
		ppc64*-musl)
			_args+=" --with-abi=elfv2 --enable-secureplt --disable-decimal-float"
			sed -i 's/lib64/lib/' gcc/config/rs6000/linux64.h
			;;
		x86_64*)
			sed -i 's/lib64/lib/' gcc/config/i386/linux64.h
			;;
	esac

	export CFLAGS="${CFLAGS/-D_FORTIFY_SOURCE=2/}"
	export CXXFLAGS="${CXXFLAGS/-D_FORTIFY_SOURCE=2/}"

	# Disable explicit -fno-PIE, gcc will figure this out itself.
	export CFLAGS="${CFLAGS//-fno-PIE/}"
	export CXXFLAGS="${CXXFLAGS//-fno-PIE/}"
	export LDFLAGS="${LDFLAGS//-no-pie/}"

	_args+=" --build=${_triplet}"
	_args+=" --program-suffix=-${_majorver}"
	_args+=" --enable-version-specific-runtime-libs"
	_args+=" --prefix=/usr"
	_args+=" --mandir=/usr/share/man"
	_args+=" --infodir=/usr/share/info"
	_args+=" --libexecdir=/usr/lib"
	_args+=" --libdir=/usr/lib"
	_args+=" --with-gxx-include-dir=/usr/include/c++/${version}"
	_args+=" --enable-languages=c,c++,java,lto"
	_args+=" --enable-java-gc=boehm"
	_args+=" --with-jvm-root=/usr/lib/jvm/java-1.5-gcj"
	_args+=" --enable-fast-character"
	_args+=" --disable-fixed-point"
	_args+=" --enable-threads=posix"
	_args+=" --enable-__cxa_atexit"
	_args+=" --disable-multilib"
	_args+=" --enable-lto"
	_args+=" --disable-bootstrap"
	_args+=" --with-system-zlib"
	_args+=" --enable-shared"
	_args+=" --enable-linker-build-id"
	_args+=" --disable-werror"
	_args+=" --disable-nls"
	_args+=" --enable-default-pie"
	_args+=" --enable-default-ssp"
	_args+=" --enable-checking=release"
	_args+=" --disable-libstdcxx-pch"
	_args+=" --with-isl"
	_args+=" --with-linker-hash-style=$_hash"
	_args+=" --disable-libunwind-exceptions"
	_args+=" --disable-target-libiberty"
	_args+=" --disable-libquadmath"
	_args+=" --disable-libsanitizer"
	_args+=" --disable-vtable-verify"
	_args+=" --disable-libitm"
	_args+=" --disable-libgomp"
	_args+=" --disable-libatomic"
	_args+=" --disable-libmpx"
	_args+=" --disable-libmudflap"
	_args+=" --disable-libssp"

	if [ "$XBPS_TARGET_LIBC" = "musl" ]; then
		_args+=" --disable-symvers"
		_args+=" libat_cv_have_ifunc=no"
	fi

	mkdir -p build
	cd build

	CONFIG_SHELL=/bin/bash ${wrksrc}/configure ${_args}
}

do_build() {
	cd build
	make ${makejobs}
}

do_install() {
	cd build
	if [ "$XBPS_TARGET_WORDSIZE" = "64" ]; then
		vmkdir usr/lib
		ln -sfr ${DESTDIR}/usr/lib ${DESTDIR}/usr/lib64
	fi
	mkdir -p ${DESTDIR}/usr/include/c++/${version}
	make DESTDIR=${DESTDIR} install

	# Make version a symlink of major version to make all versions
	# from the same series work automagically.
	mv ${DESTDIR}/usr/lib/gcc/${_triplet}/${version} \
		${DESTDIR}/usr/lib/gcc/${_triplet}/${_minorver}
	ln -sfr ${DESTDIR}/usr/lib/gcc/${_triplet}/${_minorver} \
		${DESTDIR}/usr/lib/gcc/${_triplet}/${version}

	# Ditto for c++ headers.
	mv ${DESTDIR}/usr/include/c++/${version} \
		${DESTDIR}/usr/include/c++/${_minorver}
	ln -sfr ${DESTDIR}/usr/include/c++/${_minorver} \
		${DESTDIR}/usr/include/c++/${version}

	if [ -e ${DESTDIR}/usr/lib64 ]; then
		rm -f ${DESTDIR}/usr/lib64
	fi

	# Remove python stuff
	rm -rf ${DESTDIR}/usr/share/gcc-${version}/python

	# Avoid conflict with gcc
	mv ${DESTDIR}/usr/lib/libcc1* ${DESTDIR}/usr/lib/gcc/${_triplet}/${_minorver}

	# Version info files to avoid conflicts with gcc
	for f in cp-tools cpp cppinternals gcc gccinstall gccint gcj; do
		if [ -f ${DESTDIR}/usr/share/info/${f}.info ]; then
			mv ${DESTDIR}/usr/share/info/${f}.info \
			   ${DESTDIR}/usr/share/info/${f}-${_majorver}.info
		fi
	done

	# Conflicting manpages, provided by gcc
	rm -rf ${DESTDIR}/usr/share/man/man7

	# Provided by main libgcc
	rm -f ${DESTDIR}/usr/lib/gcc/${_triplet}/${_minorver}/libgcc_s.so*

	# Gcj libs need to be available in primary libdir
	mv ${DESTDIR}/usr/lib/gcc/${_triplet}/${_minorver}/libgcj*.so* \
	   ${DESTDIR}/usr/lib
	mv ${DESTDIR}/usr/lib/gcc/${_triplet}/${_minorver}/libgij.so* \
	   ${DESTDIR}/usr/lib

	# Move pkg-config file into a path pkg-config knows about
	mkdir -p ${DESTDIR}/usr/lib/pkgconfig
	mv ${DESTDIR}/usr/lib/gcc/${_triplet}/${_minorver}/pkgconfig/*.pc \
	   ${DESTDIR}/usr/lib/pkgconfig
	rmdir ${DESTDIR}/usr/lib/gcc/${_triplet}/${_minorver}/pkgconfig

	# Build ecj - needed for compat package
	${DESTDIR}/usr/bin/gcj-6 -Wl,-Bsymbolic -findirect-dispatch \
	 -o ${DESTDIR}/usr/bin/ecj-gcj -lgcj \
	 --main=org.eclipse.jdt.internal.compiler.batch.Main \
	 ${DESTDIR}/usr/share/java/ecj.jar
}

gcc6-gcj_package() {
	lib32disabled=yes
	depends="gcc6>=${_minorver} libgcj-devel>=${_minorver}"
	short_desc+=" - Java compiler frontend"
	pkg_install() {
		for f in gcj jv-convert gjarsigner gjar grmic jcf-dump \
			gtnameserv gcjh gnative2ascii gserialver gkeytool \
			gij grmiregistry grmid gorbd gcj-dbtool gjavah \
			gc-analyze gappletviewer rebuild-gcj-db aot-compile; do
			vmove usr/bin/${f}-${_majorver}
		done
		for f in gcj jv-convert gjarsigner gjar grmic jcf-dump gjdoc \
			gtnameserv gcjh gnative2ascii gserialver gkeytool \
			gij grmiregistry grmid gorbd gcj-dbtool gjavah \
			gc-analyze gappletviewer rebuild-gcj-db aot-compile; do
			if [ -f ${DESTDIR}/usr/share/man/man1/${f}-${_majorver}.1 ]; then
				vmove usr/share/man/man1/${f}-${_majorver}.1
			fi
		done
		vmove usr/bin/${_triplet}-gcj-${_majorver}
		vmove usr/lib/gcc/${_triplet}/${_minorver}/jc1
		vmove usr/lib/gcc/${_triplet}/${_minorver}/jvgenmain
		vmove usr/lib/gcc/${_triplet}/${_minorver}/ecj1
		vmove usr/share/info/gcj-${_majorver}.info
		touch ${PKGDESTDIR}/usr/share/info/dir
	}
}

gcc6-gcj-ecj_package() {
	short_desc+=" - Eclipse Java Compiler frontend"
	pkg_install() {
		vmove usr/bin/ecj-gcj
	}
}

gcc6-gcj-jdk-compat_package() {
	lib32disabled=yes
	depends="gcc6-gcj gcc6-gcj-ecj fastjar"
	short_desc+=" - JDK compatiblity layer"
	pkg_install() {
		local jvm_dir=$PKGDESTDIR/usr/lib/jvm
		local jdk_dir=$jvm_dir/java-1.5-gcj
		local jdk_bin_dir=$jdk_dir/bin
		local jdk_lib_dir=$jdk_dir/lib
		local jdk_inc_dir=$jdk_dir/include
		local jre_dir=$jdk_dir/jre
		local jre_bin_dir=$jre_dir/bin
		local jre_lib_dir=$jre_dir/lib

		local arch=
		case "$XBPS_TARGET_MACHINE" in
			i686*) arch=i386 ;;
			x86_64*) arch=amd64 ;;
			arm*) arch=arm ;;
			aarch64*) arch=aarch64 ;;
			ppc64le*) arch=ppc64le ;;
			ppc64*) arch=ppc64 ;;
			ppc*) arch=ppc ;;
			mipsel*) arch=mipsel ;;
			mips*) arch=mips ;;
		esac

		mkdir -p $jdk_bin_dir $jdk_lib_dir
		ln -sfr ${PKGDESTDIR}/usr/bin/gij-${_majorver} $jdk_bin_dir/java
		ln -sfr ${PKGDESTDIR}/usr/bin/fastjar $jdk_bin_dir/fastjar
		ln -sfr ${PKGDESTDIR}/usr/bin/grmic-${_majorver} $jdk_bin_dir/rmic
		ln -sfr ${PKGDESTDIR}/usr/bin/gjavah-${_majorver} $jdk_bin_dir/javah
		ln -sfr ${PKGDESTDIR}/usr/bin/gappletviewer-${_majorver} $jdk_bin_dir/appletviewer
		ln -sfr ${PKGDESTDIR}/usr/bin/gjarsigner-${_majorver} $jdk_bin_dir/jarsigner
		ln -sfr ${PKGDESTDIR}/usr/bin/grmiregistry-${_majorver} $jdk_bin_dir/rmiregistry
		ln -sfr ${PKGDESTDIR}/usr/bin/gkeytool-${_majorver} $jdk_bin_dir/keytool
		ln -sfr ${PKGDESTDIR}/usr/bin/gjar-${_majorver} $jdk_bin_dir/gjar
		ln -sfr ${PKGDESTDIR}/usr/bin/gnative2ascii-${_majorver} $jdk_bin_dir/gnative2ascii
		ln -sfr ${PKGDESTDIR}/usr/bin/ecj-gcj $jdk_bin_dir/javac
		ln -sfr ${PKGDESTDIR}/usr/share/java/libgcj-tools-${version}.jar $jdk_lib_dir/tools.jar

		# symlink headers for jni.h and so on, don't do the whole directory
		# otherwise undesirable gcc include files might leak into the builds
		mkdir -p $jdk_inc_dir
		ln -sfr ${PKGDESTDIR}/usr/lib/gcc/${_triplet}/${_minorver}/include/gcj $jdk_inc_dir/gcj
		ln -sfr ${PKGDESTDIR}/usr/lib/gcc/${_triplet}/${_minorver}/include/jawt.h $jdk_inc_dir/jawt.h
		ln -sfr ${PKGDESTDIR}/usr/lib/gcc/${_triplet}/${_minorver}/include/jawt_md.h $jdk_inc_dir/jawt_md.h
		ln -sfr ${PKGDESTDIR}/usr/lib/gcc/${_triplet}/${_minorver}/include/jni.h $jdk_inc_dir/jni.h
		ln -sfr ${PKGDESTDIR}/usr/lib/gcc/${_triplet}/${_minorver}/include/jni_md.h $jdk_inc_dir/jni_md.h
		ln -sfr ${PKGDESTDIR}/usr/lib/gcc/${_triplet}/${_minorver}/include/jvmpi.h $jdk_inc_dir/jvmpi.h

		mkdir -p $jre_bin_dir $jre_lib_dir/$arch
		ln -sfr ${PKGDESTDIR}/usr/bin/gij-${_majorver} $jre_bin_dir/java
		ln -sfr ${PKGDESTDIR}/usr/bin/grmiregistry-${_majorver} $jre_bin_dir/rmiregistry
		ln -sfr ${PKGDESTDIR}/usr/bin/gkeytool-${_majorver} $jre_bin_dir/keytool
		ln -sfr ${PKGDESTDIR}/usr/share/java/libgcj-${version}.jar $jre_lib_dir/rt.jar
		ln -sfr ${PKGDESTDIR}/usr/share/java/libgcj-tools-${version}.jar $jre_lib_dir/tools.jar
		ln -sfr ${PKGDESTDIR}/usr/lib/gcc/${_triplet}/${_minorver}/gcj-$version-*/libjvm.so $jre_lib_dir/$arch
		ln -sfr ${PKGDESTDIR}/usr/lib/gcc/${_triplet}/${_minorver}/gcj-$version-*/libjavamath.so $jre_lib_dir/$arch
		ln -sfr ${PKGDESTDIR}/usr/lib/gcc/${_triplet}/${_minorver}/gcj-$version-*/classmap.db $jre_lib_dir/$arch
	}
}

libgcj-devel_package() {
	lib32disabled=yes
	depends="libgcj-${version}_${revision}"
	short_desc+=" - Java library - development files"
	pkg_install() {
		vmove usr/include/c++/${_minorver}/gnu
		vmove usr/include/c++/${_minorver}/java
		vmove usr/include/c++/${_minorver}/javax
		vmove usr/include/c++/${_minorver}/sun
		vmove usr/include/c++/${_minorver}/gcj
		vmove usr/include/c++/${_minorver}/org
		vmove "usr/lib/gcc/${_triplet}/${_minorver}/plugin/include/java"
		vmove "usr/lib/gcc/${_triplet}/${_minorver}/include/jni*"
		vmove "usr/lib/gcc/${_triplet}/${_minorver}/include/gcj*"
		vmove "usr/lib/gcc/${_triplet}/${_minorver}/include/jvm*"
		vmove "usr/lib/gcc/${_triplet}/${_minorver}/include/jawt*"
		vmove usr/lib/pkgconfig/libgcj-${_majorver}.pc
	}
}

libgcj_package() {
	lib32disabled=yes
	short_desc+=" - Java libraries"
	pkg_install() {
		vmove "usr/lib/libgcj*.so*"
		vmove "usr/lib/libgij.so*"
		vmove usr/lib/gcc/${_triplet}/${_minorver}/gcj-${version}-*
		vmove usr/lib/gcc/${_triplet}/${_minorver}/logging.properties
		vmove usr/lib/gcc/${_triplet}/${_minorver}/libgcj.spec
		vmove usr/lib/gcc/${_triplet}/${_minorver}/security/classpath.security
		vmove usr/share/java
	}
}
