################################################################################
#
# toolchain.cygclass - for building and using toolchains
#
# Part of cygport - Cygwin packaging application
# Copyright (C) 2006-2020 Cygport authors
# Provided by the Cygwin project <https://cygwin.com/>
#
# cygport 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 3 of the License, or
# (at your option) any later version.
#
# cygport 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 cygport.  If not, see <https://www.gnu.org/licenses/>.
#
################################################################################

#****h* Cygclasses/toolchain.cygclass
#  SYNOPSIS
#  TOOLCHAIN_TARGET="..."
#  inherit toolchain
#  DESCRIPTION
#  The GNU toolchain, consisting of Binutils, the GCC compilers, and the GDB
#  debugger, are used to build code in C and related languages into an executable
#  or library, either for the system on which it runs (a "native" compiler) or
#  for another system (a "cross" compiler).
#
#  This cygclass assists in building both native- and cross-compilers which will
#  run on Cygwin.
#  INHERITS
#  autotools.cygclass
#  SEE ALSO
#  cross.cygclass
#****

#****v* toolchain.cygclass/TOOLCHAIN_TARGET
#  DESCRIPTION
#  The CPU-VENDOR-OS triplet, or customary abbreviation thereof, describing the
#  compiler's "target" system; for example:
#  * i686-pc-linux-gnu: i686 GNU/Linux
#  * x86_64-w64-mingw32: Win64
#  * arm-eabi [arm-unknown-eabi]: Embedded ARM ELF system (e.g. with Newlib)
#  * avr [avr-unknown-none]: Atmel AVR bare metal (e.g. Arduino)
#  Note that while the 'unknown' vendor tag is usually omitted from embedded
#  systems, particularly in conjunction with a 'none' OS tag, as above, this
#  does not apply to e.g. 'x86_64-unknown-linux-gnu' and other systems.
#
#  The keyword "native" is also accepted, which will cause a host-native
#  Cygwin compiler to be built, and TOOLCHAIN_TARGET to be redefined
#  accordingly.
#  NOTES
#  * TOOLCHAIN_TARGET must be defined before inherit()ing toolchain.cygclass.
#****
if ! defined TOOLCHAIN_TARGET
then
	error "TOOLCHAIN_TARGET must be defined before 'inherit toolchain'"
fi
case ${TOOLCHAIN_TARGET} in
[Nn][Aa][Tt][Ii][Vv][Ee])
	inform "Building native toolchain for ${CTARGET} host"
	TOOLCHAIN_TARGET=${CTARGET}
	TOOLCHAIN_SYSROOT=
	;;
*)	__check_triplet ${TOOLCHAIN_TARGET}
	CTARGET=${TOOLCHAIN_TARGET}
	if __target_is_embedded
	then
		TOOLCHAIN_SYSROOT="/usr/${CTARGET}"
	else
		TOOLCHAIN_SYSROOT="/usr/${CTARGET}/sys-root"
	fi
	;;
esac

#****d* toolchain.cygclass/TOOLCHAIN_SYSROOT
#  DESCRIPTION
#  GCC cross-compilers provide the concept of a sysroot: a directory which
#  acts as the virtual root of the target filesystem, resulting in
#  TOOLCHAIN_SYSROOT/$prefix/include and TOOLCHAIN_SYSROOT/$prefix/lib being in
#  the default search paths without requiring -I or -L flags.
#
#  This means that software natively compiled for the host system can be unpacked
#  into the sysroot without needing to mangle paths.  This is especially
#  useful when bootstrapping a new toolchain; e.g. for Linux, one can just download
#  glibc and kernel headers packages from a Linux distro, unpack them into the
#  TOOLCHAIN_SYSROOT, and use those to build a cross-GCC for the first time.
#****

#****d* toolchain.cygclass/TOOLCHAIN_PREFIX
#  DESCRIPTION
#  The $prefix directory under the sysroot.  This definition is meant to
#  be used in configure or cmake arguments to avoid dealing with different
#  prefixes on various systems.
#****
TOOLCHAIN_PREFIX="${TOOLCHAIN_SYSROOT}$(__target_prefix)"

#****d* toolchain.cygclass/TOOLCHAIN_BINDIR
#  DESCRIPTION
#  The $prefix/bin directory under the sysroot.  This definition is meant to
#  be used in configure or cmake arguments to avoid dealing with different
#  prefixes on various systems.
#****
TOOLCHAIN_BINDIR="${TOOLCHAIN_PREFIX}/bin"

#****d* toolchain.cygclass/TOOLCHAIN_INCLUDEDIR
#  DESCRIPTION
#  The $prefix/include directory under the sysroot.  This definition is
#  meant to be used in configure or cmake arguments to avoid dealing with
#  different prefixes on various systems.
#****
TOOLCHAIN_INCLUDEDIR="${TOOLCHAIN_PREFIX}/include"

#****d* toolchain.cygclass/TOOLCHAIN_LIBDIR
#  DESCRIPTION
#  The $prefix/lib directory under the sysroot.  This definition is meant to
#  be used in configure or cmake arguments to avoid dealing with different
#  prefixes on various systems.
#****
TOOLCHAIN_LIBDIR="${TOOLCHAIN_PREFIX}/lib"

#****d* toolchain.cygclass/TOOLCHAIN_DATADIR
#  DESCRIPTION
#  The $prefix/share directory under the sysroot.  This definition is meant to
#  be used in configure or cmake arguments to avoid dealing with different
#  prefixes on various systems.
#****
TOOLCHAIN_DATADIR="${TOOLCHAIN_PREFIX}/share"

# A normal cross-compiler uses the stage1 xgcc to cross-compile
# libgcc/libstdc++/etc.  However, in a "Canadian cross" scenario,
# the stage1 compiler will not run on the build system, as it is itself
# cross-compiled, so you also need a $target toolchain to build
# libgcc and friends instead.  Of course, if $target is the same
# as the build system (a "crossback"), then the question is moot.
if test ${CHOST} != ${CBUILD} && test ${CTARGET} != ${CBUILD} \
   && (! check_target_tool gcc || ! check_target_tool strip)
then
	error "This package requires ${CTARGET} binutils and gcc"
fi

#****C* toolchain.cygclass/toolchain_compile
#  SYNOPSIS
#  toolchain_compile [CONFIGURE_ARGS]
#  DESCRIPTION
#  Configures the toolchain package with the necessary options for the given
#  TOOLCHAIN_TARGET, then runs cygmake to build.
#****
toolchain_compile() {
	local sysroot

	if test ${CTARGET} != ${CHOST} && ! __target_is_embedded
	then
		sysroot="--with-sysroot=${TOOLCHAIN_SYSROOT} --with-build-sysroot=${TOOLCHAIN_SYSROOT}"
	fi

	cygconf ${sysroot} ${@}
	cygmake
}

#****o* toolchain.cygclass/src_compile (toolchain)
#  NOTE
#  An autoreconf stage is not run by default, because GNU toolchain packages
#  require specific versions of the autotools which do not match the latest
#  versions in the distribution.  They also do not recurse with AC_CONFIG_SUBDIRS.
#  DEFINITION
src_compile() {
	cd ${B}
	toolchain_compile
}
#****

readonly -f toolchain_compile
