#!/bin/bash
#CLFS	安装路径，与 sysroot 类似
#CLFS_HOST	当前宿主机的三元组，例如 x86_64-cross-linux-gnu
#CLFS_TARGET	目标架构，比如 x86_64-unknown-linux-gnu
#PREFIX_CROSS	最终工具链所在路径
set -e
KERNEL_PATH=$1

TARGETS="x86_64-glibc-gnuabi64"  #this name could change to release

#prefix="/opt/${target}"

#ROOTDIR="/opt/v1001"  # ROOTDIR could change to /opt/
ROOTDIR="$(dirname $(pwd))"  # ROOTDIR could change to /opt/


BUILD_DIR="Build_Output"  #when release BUILD_DIR=null

PREFIX="${ROOTDIR}/${TARGETS}"  #when release BUILD_DIR=null rootdir=/opt

PREFIX_CROSS="${PREFIX}"

SRCDIR="$(dirname $(pwd))/src"

OUTDIR="${PREFIX}"

CLFS="${OUTDIR}"

#CLFS_2="${ROOTDIR}/x86_64-glibc-cross-v1001"

export CLFS
export CLFS_HOST="$(echo ${MACHTYPE} | sed -e 's/-[^-]-/cross/')"
#export CLFS_HOST="x86_64-unknown-linux-gnu"
echo ${CLFS_HOST}
export CLFS_TARGET="x86_64-unknown-linux-gnu"
#export CLFS_TARGET32="$(echo ${CLFS_TARGET} | sed -e 's/64//gl')"

export BUILD32="-m32"
export BUILDN32="-mabi=n32"
export BUILD64="-m64"

export PATH="${PREFIX_CROSS}/bin:$PATH"


echo "CLFS $CLFS"
echo "PREFIX_CROSS $PREFIX_CROSS"

function fillvars ()
{
    sgccdir="${tmpdir}/sgcc/${target}"
    fgccdir="${outdir}/opt/${target}"
    arch="${target%%-}"
	echo ${arch}
    arch="${arch%-u*}"
	echo ${arch}
	echo ${target}
}

function stdbuild ()
{
    dir="$1"
    shift
    pushd "${SRCDIR}/${dir}"
    echo "stdbuild: $(pwd)"
    make distclean | cat
    ./configure "$@"
    make -j 20
    make install
    popd
}

function reqbuild ()
{
    stdbuild "$@" --prefix="${CLFS}/gcc_lib" --disable-shared
}

function build_binutils ()
{
    pushd "${SRCDIR}/binutils-2.27"
    rm -rf build
    mkdir build
    cd build
		#--with-lib-path="${PREFIX_CROSS}/lib/" \
    AR=ar AS=as ../configure  \
		--prefix="${PREFIX_CROSS}" \
		--host="${CLFS_HOST}" \
     		--target="${CLFS_TARGET}" \
		--with-sysroot='${exec_prefix}' \
		--disable-nls \
		--disable-static \
		--enable-64-bit-bfd \
		--disable-werror

		#--exec-prefix="${EPREFIX}" \
    make  -j 20

    make install
    popd
}

function build_stage_gcc ()
{
    pushd "${SRCDIR}/gcc-$ver_gcc"
	chmod +x configure
	chmod +x move-if-change
	chmod +x install-sh
	chmod +x libgcc -R
    rm -rf build
    mkdir -p build
    cd build
    make distclean | cat

    sed -i '/stack protector/a\gcc_cv_libc_provides_ssp=yes' ../gcc/configure  #run only once
	AR=ar  CFLAGS="-fPIC -fasynchronous-unwind-tables" \
    ../configure \
		 --prefix="${PREFIX_CROSS}" \
		 --build="${CLFS_HOST}" \
		 --host="${CLFS_HOST}" \
	     --target="${CLFS_TARGET}" \
		 --with-sysroot='${exec_prefix}' \
		 --with-native-system-headers-dir=${PREFIX_CROSS}/usr/include \
		 --with-local-prefix=${PREFIX_CROSS}/usr \
		 --disable-nls \
		 --disable-shared \
		 --with-gmp="${CLFS}/gcc_lib" \
		 --with-mpfr="${CLFS}/gcc_lib" \
		 --with-mpc="${CLFS}/gcc_lib" \
		 --without-headers \
		 --with-newlib \
		 --disable-decimal-float \
		 --disable-libgomp \
		 --disable-libmudflap \
		 --enable-libssp \
		 --disable-libatomic \
		 --disable-libitm \
		 --disable-libsanitizer \
		 --disable-libquadmath \
		 --disable-threads \
		 --disable-target-zlib \
		 --with-system-zlib \
		 --disable-multilib \
		 --enable-languages=c \
		 --enable-checking=release

		 #--exec-prefix="${EPREFIX}" \
    make V=1 all-gcc all-target-libgcc -j 24
    make install-gcc install-target-libgcc -j 24
    popd
}

function install_kernel_headers ()
{

    arch="${target%%-}"
    arch="${arch%-gl*}"
    pushd ${KERNEL_PATH}
#    make ARCH=${arch} INSTALL_HDR_PATH="${fgccdir}/usr32" headers_install
#    make ARCH=${arch} INSTALL_HDR_PATH="${fgccdir}/usrn32" headers_install
    make mrproper
    make ARCH=x86_64 headers_check
    make ARCH=x86_64 INSTALL_HDR_PATH="${CLFS}/kernel_header" headers_install
    popd
}
ver_gcc=10.4.0
function build_glibc64 ()
{
    pushd "${SRCDIR}/glibc-2.24"
    rm -rf build-64
    mkdir build-64
    cd build-64
    make distclean | cat
    BUILD_CC="gcc" CC="${CLFS_TARGET}-gcc" CFLAGS="-O2 -fPIC -fasynchronous-unwind-tables" \
    AR="${CLFS_TARGET}-ar" RANLIB="${CLFS_TARGET}-ranlib" \
    ../configure --prefix="/usr" \
		 --host=${CLFS_TARGET} \
		 --build=${CLFS_HOST} \
		 --disable-profile \
		 --enable-obsolete-rpc \
		 --disable-werror \
		 --with-binutils=${PREFIX_CROSS}/bin \
		 --with-headers=${CLFS}/kernel_header/include \
		 libc_cv_forced_unwind=yes \
		 libc_cv_ctors_header=yes \
		 libc_cv_c_cleanup=yes \
		 libc_cv_ssp=yes
	#	 --enable-stackguard-randomization \
	#	 libc_cv_predef_stack_protector=yes
		 #--exec-prefix="${EPREFIX}" \

	 #--enable-kernel=3.14.28 \
#	 --libdir="${PREFIX_CROSS}/usr/lib" \
	make V=1 -j 24
	make V=1 install  install_root=${PREFIX_CROSS}
    popd
}

function build_full_gcc ()
{
    pushd "${SRCDIR}/gcc-$ver_gcc"
    cp -rf ${CLFS}/kernel_header/include/* ${PREFIX_CROSS}/usr/include/
    rm -rf build
    mkdir -p build
    cd build
	AR=ar CFLAGS="-fPIC -fasynchronous-unwind-tables" \
    ../configure \
		 --prefix="${PREFIX_CROSS}" \
		 --build="${CLFS_HOST}" \
 		 --target="${CLFS_TARGET}" \
		 --host="${CLFS_HOST}" \
		 --with-sysroot='${exec_prefix}' \
		 --with-local-prefix=${PREFIX_CROSS}/usr \
		 --with-native-system-headers-dir=${PREFIX_CROSS}/usr/include \
		 --disable-nls \
		 --disable-static \
		 --enable-languages=c,c++ \
		 --enable-__cxa_atexit \
		 --enable-threads=posix \
		 --with-gmp="${CLFS}/gcc_lib" \
		 --with-mpfr="${CLFS}/gcc_lib" \
		 --with-mpc="${CLFS}/gcc_lib" \
		 --with-system-zlib \
		 --enable-checking=release \
		 --enable-libstdcxx-time \
		 --disable-multilib \
		 --enable-shared
		 #--exec-prefix="${EPREFIX}" \

    make V=1 AS_FOR_TARGET="${CLFS_TARGET}-as" \
			 LD_FOR_TARGET="${CLFS_TARGET}-ld" -j 24

    make V=1 install

    popd
}

#reqbuild gmp-6.0.0
#reqbuild mpfr-3.1.5 --with-gmp="${CLFS}/gcc_lib" --with-mpfr="${CLFS}/gcc_lib"
#reqbuild mpc-1.0.3 --with-gmp="${CLFS}/gcc_lib" --with-mpfr="${CLFS}/gcc_lib"

for target in ${TARGETS}; do
  #  install_kernel_headers
#    build_binutils
    build_stage_gcc
    build_glibc64
	  ln -s "${PREFIX}/usr/lib" "${PREFIX}/usr/lib64"
    build_full_gcc
done