#
# set -o errexit -o noglob are assumed.
# Order: stage1_host, runtime_host, libstdcpp_v3_host, full_host, full
# Order: stage1_mingw32, native_mingw32
#

pkgp_gcc_build_dir() {
	PKG_BUILD_DIR="${1}-${2}-${PKG_TARGET}";
};

pkgp_gcc_setup_env() {
	local __="";
	export MAKE="make LIBTOOL=slibtool";
	export cbb_target="${PKG_TARGET}";
	unwind_midipix_h=$(readlink -f "${PKG_BASE_DIR}/cbb-gcc-${1}/gcc/unwind-midipix.h");
	case "${PKG_NAME}" in
	gcc_stage1_host)
		GCCTARGET_FLAGS="-DIN_TARGET_LIBRARY_BUILD --sysroot=${PREFIX_CROSS}";
		GCCTARGET_FLAGS="${GCCTARGET_FLAGS} --include=${unwind_midipix_h}";
		GCCTARGET_FLAGS="${GCCTARGET_FLAGS} -DTARGET_PERSONALITY_FUNCTION";
		GCCTARGET_FLAGS="${GCCTARGET_FLAGS} -DTARGET_PERSONALITY_FUNCTION_MIDIPIX";
		GCCTARGET_FLAGS="${GCCTARGET_FLAGS} ${DEFAULT_CFLAGS_DEBUG_RUNTIME}";
		export	cbb_neutral_libiberty=no						\
			cbb_ldflags_for_target=--sysroot="${PREFIX_CROSS}"			\
			cbb_sysroot_for_libgcc="${PREFIX_CROSS}"				\
			cbb_xgcc_for_specs="${PKG_BASE_DIR}/${PKG_BUILD_DIR}/gcc/xgcc"; ;;
	gcc_stage1_cross_mingw32)
		GCCTARGET_FLAGS="-DIN_TARGET_LIBRARY_BUILD --sysroot=${PREFIX_MINGW32} -midata";
		export	cbb_neutral_libiberty=no						\
			cbb_ldflags_for_target=--sysroot="${PREFIX_MINGW32}"			\
			cbb_sysroot_for_libgcc="${PREFIX_MINGW32}"				\
			cbb_xgcc_for_specs="${PKG_BASE_DIR}/${PKG_BUILD_DIR}/gcc/xgcc"; ;;
	gcc_stage1_native_mingw32)
		GCCTARGET_FLAGS="-DIN_TARGET_LIBRARY_BUILD --sysroot=${PKG_PREFIX} -midata";
		export	cbb_neutral_libiberty=no						\
			cbb_ldflags_for_target=--sysroot="${PKG_PREFIX}"			\
			cbb_sysroot_for_libgcc="${PKG_PREFIX}"					\
			cbb_xgcc_for_specs="${PKG_TARGET}-gcc"; ;;
	gcc_full)
		GCCTARGET_FLAGS="-DIN_TARGET_LIBRARY_BUILD --sysroot=${PKG_PREFIX}";
		GCCTARGET_FLAGS="${GCCTARGET_FLAGS} --include=${unwind_midipix_h}";
		GCCTARGET_FLAGS="${GCCTARGET_FLAGS} -DTARGET_PERSONALITY_FUNCTION";
		GCCTARGET_FLAGS="${GCCTARGET_FLAGS} -DTARGET_PERSONALITY_FUNCTION_MIDIPIX";
		GCCTARGET_FLAGS="${GCCTARGET_FLAGS} ${DEFAULT_CFLAGS_DEBUG_RUNTIME}";
		export	cbb_xgcc_for_specs="${PKG_TARGET}-gcc"					\
			cbb_ldflags_for_target=--sysroot="${PKG_PREFIX}"			\
			cbb_sysroot_for_libgcc="${PKG_PREFIX}"					\
			sysroot_headers_suffix="${PKG_PREFIX}"; ;;
	esac;
	GCCFLAGS="${PKG_CFLAGS_CONFIGURE} --include $(readlink -f "${PKG_BASE_DIR}/cbb-gcc-${1}/libc/cbb-musl-pe.h")";
	for __ in CFLAGS CFLAGS_FOR_BUILD CPPFLAGS_FOR_BUILD CXXFLAGS CXXFLAGS_FOR_BUILD; do
		export "${__}=${GCCFLAGS}";
	done;
	for __ in CFLAGS_FOR_TARGET CPPFLAGS_FOR_TARGET CXXFLAGS_FOR_TARGET LIBCFLAGS_FOR_TARGET XGCC_FLAGS_FOR_TARGET; do
		export "${__}=${GCCTARGET_FLAGS}";
	done;
	for __ in cbb_cflags_for_stage1 cbb_cflags_for_stage2 cbb_cflags_for_stage3 cbb_cflags_for_stage4; do
		export "${__}=${CFLAGS_FOR_BUILD}" ;
	done;
	export CFLAGS CXXFLAGS CFLAGS_FOR_BUILD CPPFLAGS_FOR_BUILD CXXFLAGS_FOR_BUILD CFLAGS_FOR_TARGET XGCC_FLAGS_FOR_TARGET CPPFLAGS_FOR_TARGET CXXFLAGS_FOR_TARGET LIBCFLAGS_FOR_TARGET;
};

pkgp_gcc_configure() {
	ex_rtl_fileop rm "${PKG_BASE_DIR}/${PKG_BUILD_DIR}";
	ex_rtl_fileop mkdir "${PKG_BASE_DIR}/${PKG_BUILD_DIR}";
	ex_rtl_fileop cd "${PKG_BASE_DIR}/${PKG_BUILD_DIR}";
	"../cbb-gcc-${PKG_VERSION}/configure" "${@}"\
		--disable-bootstrap			\
		--disable-libmudflap			\
		--disable-multilib			\
		--disable-nls				\
		--disable-obsolete			\
		--disable-symvers			\
		--enable-__cxa_atexit			\
		--enable-canonical-system-headers	\
		--enable-debug				\
		--enable-gnu-indirect-function		\
		--enable-gnu-unique-object		\
		--enable-initfini-array			\
		--enable-languages=c,c++,objc,lto	\
		--enable-libstdcxx-debug		\
		--enable-lto				\
		--enable-multiarch			\
		--enable-secureplt			\
		--enable-shared				\
		--enable-threads=posix			\
		--target="${PKG_TARGET}"		\
		--with-fpmath=sse;
	ex_pkg_state_set "${PKG_NAME}" configure -build;
};

pkg_gcc_stage1_host_all() {
	pkg_gcc_stage1_all "${@}";
};

pkg_gcc_stage1_cross_mingw32_all() {
	ex_rtl_fileop ln_symbolic . "${PKG_PREFIX}/x86_64-w64-mingw32/mingw";
	ex_rtl_fileop mkdir "${PKG_DESTDIR}/x86_64-w64-mingw32";
	ex_rtl_fileop ln_symbolic . "${PKG_DESTDIR}/x86_64-w64-mingw32/mingw";
	mkdir "${PKG_PREFIX}/x86_64-w64-mingw32/include";
	pkg_gcc_stage1_all "${@}";
};

pkg_gcc_stage1_native_mingw32_all() {
	ex_rtl_fileop ln_symbolic . "${PKG_PREFIX}/x86_64-w64-mingw32/mingw";
	ex_rtl_fileop mkdir "${PKG_DESTDIR}/x86_64-w64-mingw32";
	ex_rtl_fileop ln_symbolic . "${PKG_DESTDIR}/x86_64-w64-mingw32/mingw";
	mkdir "${PKG_PREFIX}/x86_64-w64-mingw32/include";
	pkg_gcc_stage1_all "${@}";
};

pkg_gcc_stage1_all() {
	local __="" _restart_at="${1}";
	case "${PKG_NAME}" in
	gcc_stage1_host)
		pkgp_gcc_build_dir "cbb-gcc-${PKG_VERSION}" cross;
		;;
	gcc_stage1_cross_mingw32)
		pkgp_gcc_build_dir "cbb-gcc-${PKG_VERSION}" mingw32;
		;;
	gcc_stage1_native_mingw32)
		pkgp_gcc_build_dir "cbb-gcc-${PKG_VERSION}" native;
		;;
	esac;
	# GCC, stage1.
	if ! ex_pkg_state_test "${PKG_NAME}" fetch "${_restart_at}"; then
		ex_rtl_fetch_urls_git "${PKG_BASE_DIR}" "cbb-gcc-${PKG_VERSION}=${PKG_GITROOT}/cbb/cbb-gcc-${PKG_VERSION}";
		ex_pkg_state_set "${PKG_NAME}" fetch -patch;
	fi;
	if ! ex_pkg_state_test "${PKG_NAME}" patch "${_restart_at}"; then
		if [ -e "${MIDIPIX_BUILD_PWD}/patches/${PKG_NAME}.local.patch" ]; then
			patch -b -d "${PKG_BASE_DIR}" -p0 < "${MIDIPIX_BUILD_PWD}/patches/${PKG_NAME}.local.patch";
		fi;
		ex_pkg_state_set "${PKG_NAME}" patch -configure;
	fi;
	pkgp_gcc_setup_env "${PKG_VERSION}";
	if ! ex_pkg_state_test "${PKG_NAME}" configure "${_restart_at}"; then
		case "${PKG_NAME}" in
		gcc_stage1_host)
			pkgp_gcc_configure				\
				--disable-sjlj-exceptions		\
				--prefix="${PREFIX}"			\
				--with-sysroot="${PREFIX_CROSS}"; ;;
		gcc_stage1_cross_mingw32)
			pkgp_gcc_configure				\
				--enable-sjlj-exceptions		\
				--prefix="${PREFIX}"			\
				--with-sysroot="${PREFIX_MINGW32}"	\
				--without-headers; ;;
		gcc_stage1_native_mingw32)
			pkgp_gcc_configure				\
				--build=x86_64-unknown-linux-gnu	\
				--enable-sjlj-exceptions		\
				--host=x86_64-nt64-midipix		\
				--prefix=/				\
				--with-elf="${PKG_PREFIX}"		\
				--with-gmp="${PKG_PREFIX}"		\
				--with-mpc="${PKG_PREFIX}"		\
				--with-mpfr="${PKG_PREFIX}"		\
				--with-sysroot=				\
				--without-headers; ;;
		esac;
	else
		ex_rtl_fileop cd "${PKG_BASE_DIR}/${PKG_BUILD_DIR}";
	fi;
	if ! ex_pkg_state_test "${PKG_NAME}" build "${_restart_at}"; then
		make ${PKG_MAKEFLAGS_BUILD} all-gcc;
		ex_pkg_state_set "${PKG_NAME}" build -install;
	fi;
	if ! ex_pkg_state_test "${PKG_NAME}" install_subdirs "${_restart_at}"; then
		pkg_install_subdirs;
		ex_pkg_state_set "${PKG_NAME}" install_subdirs -install_make;
	fi;
	if ! ex_pkg_state_test "${PKG_NAME}" install_make "${_restart_at}"; then
		case "${PKG_NAME}" in
		gcc_stage1_host)
			make ${PKG_MAKEFLAGS_BUILD} "prefix=${PKG_DESTDIR}" install-gcc; ;;
		gcc_stage1_cross_mingw32)
			make ${PKG_MAKEFLAGS_BUILD} "prefix=${PKG_DESTDIR}" install-gcc; ;;
		gcc_stage1_native_mingw32)
			make ${PKG_MAKEFLAGS_BUILD} "prefix=${PKG_DESTDIR}" install-gcc; ;;
		esac;
		__="$(uname -s)";
		if [ "${__#*CYGWIN*}" != "${__}" ]; then
			ex_rtl_fileop ln_symbolic "${PKG_DESTDIR}/libexec/gcc/${PKG_TARGET}/${PKG_VERSION}/liblto_plugin.dll.a"	\
				ex_rtl_fileop ln_symbolic "${PKG_DESTDIR}/libexec/gcc/${PKG_TARGET}/${PKG_VERSION}/liblto_plugin.so";
		fi;
		ex_pkg_state_set "${PKG_NAME}" install_make -install;
	fi;
	if ! ex_pkg_state_test "${PKG_NAME}" install "${_restart_at}"; then
		pkg_install;
		ex_pkg_state_set "${PKG_NAME}" install finish;
	fi;
};

pkg_gcc_runtime_host_all() {
	local _restart_at="${1}";
	local _vis_hide="vis_hide=-fvisibility=hidden";
	export MAKE="make LIBTOOL=slibtool ${_vis_hide}";
	pkgp_gcc_build_dir "cbb-gcc-${PKG_VERSION}" cross;
	ex_rtl_fileop cd "${PKG_BASE_DIR}/${PKG_BUILD_DIR}";
	# GCC, compiler runtime.
	if ! ex_pkg_state_test "${PKG_NAME}" build "${_restart_at}"; then
		make ${PKG_MAKEFLAGS_BUILD} all-target-libgcc ${_vis_hide};
		ex_pkg_state_set "${PKG_NAME}" build -install;
	fi;
	if ! ex_pkg_state_test "${PKG_NAME}" install_subdirs "${_restart_at}"; then
		pkg_install_subdirs;
		ex_pkg_state_set "${PKG_NAME}" install_subdirs -install_make;
	fi;
	if ! ex_pkg_state_test "${PKG_NAME}" install_make "${_restart_at}"; then
		make ${PKG_MAKEFLAGS_BUILD} "prefix=${PKG_DESTDIR}" install-target-libgcc;
		ex_pkg_state_set "${PKG_NAME}" install_make -install;
	fi;
	if ! ex_pkg_state_test "${PKG_NAME}" install "${_restart_at}"; then
		pkg_install;
		ex_pkg_state_set "${PKG_NAME}" install finish;
	fi;
};

pkg_gcc_libstdcpp_v3_host_all() {
	local _restart_at="${1}";
	export MAKE="make LIBTOOL=slibtool";
	pkgp_gcc_build_dir "cbb-gcc-${PKG_VERSION}" cross;
	ex_rtl_fileop cd "${PKG_BASE_DIR}/${PKG_BUILD_DIR}";
	# GCC, libstdc++-v3.
	if ! ex_pkg_state_test "${PKG_NAME}" build "${_restart_at}"; then
		make ${PKG_MAKEFLAGS_BUILD} all-target-libstdc++-v3;
		ex_pkg_state_set "${PKG_NAME}" build -install;
	fi;
	if ! ex_pkg_state_test "${PKG_NAME}" install_subdirs "${_restart_at}"; then
		pkg_install_subdirs;
		ex_pkg_state_set "${PKG_NAME}" install_subdirs -install_make;
	fi;
	if ! ex_pkg_state_test "${PKG_NAME}" install_make "${_restart_at}"; then
		make ${PKG_MAKEFLAGS_BUILD} "prefix=${PKG_DESTDIR}" install-target-libstdc++-v3;
		ex_pkg_state_set "${PKG_NAME}" install_make -install;
	fi;
	if ! ex_pkg_state_test "${PKG_NAME}" install "${_restart_at}"; then
		pkg_install;
		ex_pkg_state_set "${PKG_NAME}" install finish;
	fi;
};

pkg_gcc_full_host_all() {
	local _restart_at="${1}";
	export MAKE="make LIBTOOL=slibtool";
	pkgp_gcc_build_dir "cbb-gcc-${PKG_VERSION}" cross;
	ex_rtl_fileop cd "${PKG_BASE_DIR}/${PKG_BUILD_DIR}";
	# GCC, everything else.
	if ! ex_pkg_state_test "${PKG_NAME}" build "${_restart_at}"; then
		make ${PKG_MAKEFLAGS_BUILD};
		ex_pkg_state_set "${PKG_NAME}" build -install;
	fi;
	if ! ex_pkg_state_test "${PKG_NAME}" install_subdirs "${_restart_at}"; then
		pkg_install_subdirs;
		ex_pkg_state_set "${PKG_NAME}" install_subdirs -install_make;
	fi;
	if ! ex_pkg_state_test "${PKG_NAME}" install_make "${_restart_at}"; then
		make ${PKG_MAKEFLAGS_BUILD} "prefix=${PKG_DESTDIR}" install;
		ex_pkg_state_set "${PKG_NAME}" install_make -install;
	fi;
	if ! ex_pkg_state_test "${PKG_NAME}" install "${_restart_at}"; then
		pkg_install;
		ex_pkg_state_set "${PKG_NAME}" install finish;
	fi;
};

pkg_gcc_full_all() {
	local _restart_at="${1}" _configure_host="";
	local _vis_hide="vis_hide=-fvisibility=hidden";
	export MAKE="make LIBTOOL=slibtool ${_vis_hide}";
	# GCC, native.
	pkgp_gcc_build_dir "cbb-gcc-${PKG_VERSION}" native;
	if ! ex_pkg_state_test "${PKG_NAME}" fetch "${_restart_at}"; then
		ex_rtl_fetch_urls_git "${PKG_BASE_DIR}" "cbb-gcc-${PKG_VERSION}=${PKG_GITROOT}/cbb/cbb-gcc-${PKG_VERSION}";
		ex_pkg_state_set "${PKG_NAME}" fetch -patch;
	fi;
	if ! ex_pkg_state_test "${PKG_NAME}" patch "${_restart_at}"; then
		if [ -e "${MIDIPIX_BUILD_PWD}/patches/${PKG_NAME}.local.patch" ]; then
			patch -b -d "${PKG_BASE_DIR}" -p0 < "${MIDIPIX_BUILD_PWD}/patches/${PKG_NAME}.local.patch";
		fi;
		ex_pkg_state_set "${PKG_NAME}" patch -configure;
	fi;
	pkgp_gcc_setup_env "${PKG_VERSION}";
	if ! ex_pkg_state_test "${PKG_NAME}" configure "${_restart_at}"; then
		ex_rtl_fileop mkdir "${PKG_PREFIX}/${PKG_TARGET}";
		if [ \! -d "${PKG_PREFIX}/${PKG_TARGET}/usr" ]; then
			ex_rtl_fileop ln_symbolic . "${PKG_PREFIX}/${PKG_TARGET}/usr";
		fi;
		if [ "${ARCH}" = nt32 ]; then
			_configure_host="i686-nt32-midipix";
		else
			_configure_host="x86_64-nt64-midipix";
		fi;
		pkgp_gcc_configure				\
			--disable-sjlj-exceptions		\
			--host="${_configure_host}"		\
			--prefix=/				\
			--with-elf="${PKG_PREFIX}"		\
			--with-gmp="${PKG_PREFIX}"		\
			--with-mpc="${PKG_PREFIX}"		\
			--with-mpfr="${PKG_PREFIX}"		\
			--with-sysroot=;
	else
		ex_rtl_fileop cd "${PKG_BASE_DIR}/${PKG_BUILD_DIR}";
	fi;
	if ! ex_pkg_state_test "${PKG_NAME}" build "${_restart_at}"; then
		make ${PKG_MAKEFLAGS_BUILD} all-gcc;
		make ${PKG_MAKEFLAGS_BUILD} all-target-libgcc ${_vis_hide};
		make ${PKG_MAKEFLAGS_BUILD} all-target-libstdc++-v3;
		make ${PKG_MAKEFLAGS_BUILD} all;
		ex_pkg_state_set "${PKG_NAME}" build -install;
	fi;
	if ! ex_pkg_state_test "${PKG_NAME}" install_subdirs "${_restart_at}"; then
		pkg_install_subdirs;
		ex_pkg_state_set "${PKG_NAME}" install_subdirs -install_make;
	fi;
	if ! ex_pkg_state_test "${PKG_NAME}" install_make "${_restart_at}"; then
		make ${PKG_MAKEFLAGS_BUILD} "prefix=${PKG_DESTDIR}" install;
		ex_pkg_state_set "${PKG_NAME}" install_make -install;
	fi;
	if ! ex_pkg_state_test "${PKG_NAME}" install "${_restart_at}"; then
		pkg_install;
		ex_pkg_state_set "${PKG_NAME}" install finish;
	fi;
};

# vim:filetype=sh