#!/bin/bash runit() { set -e trap 'previous_command=$this_command; this_command=$BASH_COMMAND' DEBUG trap 'echo FAILED COMMAND: $previous_command' EXIT #### TU NIE ZMIENIAJ vvv export TARGET="${1}" export PREFIX="`pwd`/rel-bin/${TARGET}" export PATH="${PREFIX}/bin:${PATH}" export OUT="`pwd`/out" if uname -s | grep -q -i "linux"; then export nproc="$(( `nproc --all` * 2 ))" elif uname -s | grep -q -i "darwin"; then export nproc="$(( `sysctl -n hw.ncpu` * 2 ))" else if command -v nproc >/dev/null; then export nproc="$(( `nproc --all` * 2 ))" else #domyślnie dwa rdzenie export nproc="2" fi fi export ARGS="--enable-multilib --enable-multiarch --enable-targets=all --disable-werror" if [ "${buildnewlib}" -ne "0" ]; then export EXTRA_ARGS="--with-newlib" fi case "${2}" in 64) export CC="cc -m64" export CXX="c++ -m64" ;; 32) export CC="cc -m32" export CXX="c++ -m32" ;; esac mkdir -pv out logs #### TU NIE ZMIENIAJ ^^^ download() { if [ ! -d gcc-dl ]; then git clone --depth=1 -b ${gcc_version} "git://github.com/gcc-mirror/gcc" gcc-dl || return ${?} fi if [ ! -d glibc-dl ]; then git clone --depth=1 -b ${glibc_version} "git://github.com/bminor/glibc" glibc-dl || return ${?} fi if [ ! -d linux-dl ]; then git clone --depth=1 -b ${linux_version} "git://github.com/torvalds/linux" linux-dl || return ${?} fi if [ ! -d binutils-gdb-dl ]; then git clone --depth=1 -b ${binutils_version} "git://github.com/bminor/binutils-gdb" binutils-gdb-dl || return ${?} fi if [ ! -d newlib-dl ]; then git clone --depth=1 -b ${newlib_version} "git://github.com/bminor/newlib" newlib-dl || return ${?} fi } build() { if [ "${buildnewlib}" -eq "0" ]; then make -C linux-dl ARCH="`echo ${TARGET} | cut -f 1 -d '-' | sed -e s/i.86/x86/ -e s/x86_64/x86/ -e s/sun4u/sparc64/ -e s/arm.*/arm/ -e s/sa110/arm/ -e s/s390x/s390/ -e s/parisc64/parisc/ -e s/ppc.*/powerpc/ -e s/mips.*/mips/ -e s/sh[234].*/sh/ -e s/aarch64.*/arm64/ -e s/riscv.*/riscv/`" INSTALL_HDR_PATH="${PREFIX}/${TARGET}" headers_install | tee logs/${TARGET}-linux-headers.log || return ${?} find ${PREFIX}/${TARGET} -name "..install.cmd" | xargs rm -rf || return ${?} find ${PREFIX}/${TARGET} -name ".install" | xargs rm -rf || return ${?} fi mkdir -pv build-binutils || return ${?} (cd build-binutils; ../binutils-gdb-dl/configure --target="${TARGET}" --prefix="${PREFIX}" --disable-nls --enable-obsolete --disable-debug ${ARGS}) | tee logs/${TARGET}-configure-binutils.log || return ${?} make -C build-binutils -j${nproc} | tee logs/${TARGET}-build-binutils.log || return ${?} make -C build-binutils -j${nproc} install | tee logs/${TARGET}-install-binutils.log || return ${?} if [ ! -e gcc-dl/.dl_ok ]; then (cd gcc-dl; contrib/download_prerequisites) | tee logs/${TARGET}-gcc-dl-prerequisites.log || return ${?} touch gcc-dl/.dl_ok || return ${?} fi mkdir -pv build-gcc || return ${?} (cd build-gcc; ../gcc-dl/configure --target="${TARGET}" --prefix="${PREFIX}" --enable-languages="c,c++" --with-pkgversion="*DANiO* GCC (`echo ${TARGET} | cut -f 1 -d '-'`)" ${ARGS} ${EXTRA_ARGS}) | tee logs/${TARGET}-configure-gcc.log || return ${?} make -C build-gcc -j${nproc} all-gcc | tee logs/${TARGET}-gcc-all-gcc.log || return ${?} make -C build-gcc -j${nproc} install-gcc | tee logs/${TARGET}-gcc-install-gcc.log || return ${?} if [ "${buildnewlib}" -ne "0" ]; then mkdir -pv build-newlib || return ${?} (cd build-newlib; ../newlib-dl/configure --prefix="${PREFIX}" --target="${TARGET}" ${ARGS}) | tee logs/${TARGET}-configure-newlib.log || return ${?} make -C build-newlib -j${nproc} | tee logs/${TARGET}-build-newlib.log || return ${?} make -C build-newlib -j${nproc} install | tee logs/${TARGET}-install-newlib.log || return ${?} #(cd gcc-dl; ln -svf ../newlib-dl/newlib newlib; ln -svf ../newlib-dl/libgloss libgloss) || return ${?} make -C build-gcc -j${nproc} all-target-libgcc | tee logs/${TARGET}-gcc-all-target-libgcc.log || return ${?} make -C build-gcc -j${nproc} install-target-libgcc | tee logs/${TARGET}-gcc-install-target-libgcc.log || return ${?} else mkdir -pv build-glibc || return ${?} env CC="${TARGET}-gcc" CXX="${TARGET}-g++" sh -c '(cd build-glibc; ../glibc-dl/configure --prefix="${PREFIX}/${TARGET}" --host="${TARGET}" --with-headers="${PREFIX}/${TARGET}/include" --with-pkgversion="*DANiO* GLIBC (`echo ${TARGET} | cut -f 1 -d '-'`)" --enable-add-ons ${ARGS})' | tee logs/${TARGET}-configure-glibc.log || return ${?} make -C build-glibc -j${nproc} install-bootstrap-headers=yes install-headers | tee logs/${TARGET}-bootstrap-headers-glibc.log || return ${?} make -C build-glibc -j${nproc} csu/subdir_lib | tee logs/${TARGET}-build-glibc-csu.log || return ${?} mkdir -pv ${PREFIX}/${TARGET}/lib || return ${?} cp -ravf build-glibc/csu/crt1.o build-glibc/csu/crti.o build-glibc/csu/crtn.o ${PREFIX}/${TARGET}/lib | tee logs/${TARGET}-install-csu.log || return ${?} #${TARGET}-gcc -nostdlib -nostartfiles -shared -x c /dev/null -o ${PREFIX}/${TARGET}/lib/libc.so || return ${?} touch ${PREFIX}/${TARGET}/include/gnu/stubs.h || return ${?} make -C build-gcc -j${nproc} all-target-libgcc | tee logs/${TARGET}-gcc-all-target-libgcc.log || return ${?} make -C build-gcc -j${nproc} install-target-libgcc | tee logs/${TARGET}-gcc-install-target-libgcc.log || return ${?} make -C build-glibc -j${nproc} | tee logs/${TARGET}-build-glibc.log || return ${?} make -C build-glibc -j${nproc} install | tee logs/${TARGET}-install-glibc.log || return ${?} fi make -C build-gcc -j${nproc} all-target-libstdc++-v3 | tee logs/${TARGET}-gcc-all-target-libstdc++-v3.log || return ${?} make -C build-gcc -j${nproc} install-target-libstdc++-v3 | tee logs/${TARGET}-gcc-install-target-libstdc++-v3.log || return ${?} } strip_n_pack() { #find ${PREFIX} -type f -not -path *${TARGET}/${TARGET}/* | xargs file | grep "ELF" | cut -f 1 -d ":" | xargs strip -S --strip-unneeded #for host find ${PREFIX} -type f | xargs file | grep "ELF" | cut -f 1 -d ":" | xargs strip -S --strip-unneeded #for host find ${PREFIX}/${TARGET} -type f -name "*.a" | xargs ${TARGET}-strip -S --strip-unneeded #for sysroot find ${PREFIX}/${TARGET} -type f -name "*.a" | xargs ${TARGET}-ranlib #for sysroot find ${PREFIX}/${TARGET} -type f | xargs file | grep "ELF" | cut -f 1 -d ":" | xargs ${TARGET}-strip -S --strip-unneeded #for sysroot if [ "${buildnewlib}" -ne "0" ]; then if [ "${1}" = "64" ]; then (cd rel-bin; tar -cJvf "${OUT}/${TARGET}-GCC_${gcc_version}-newlib-`date +"%d-%m-%Y"`-`uname -s`_x86-64.txz" ${TARGET}) | tee logs/${TARGET}-package.log || return ${?} else (cd rel-bin; tar -cJvf "${OUT}/${TARGET}-GCC_${gcc_version}-newlib-`date +"%d-%m-%Y"`-`uname -s`_x86-32.txz" ${TARGET}) | tee logs/${TARGET}-package.log || return ${?} fi else if [ "${1}" = "64" ]; then (cd rel-bin; tar -cJvf "${OUT}/${TARGET}-GCC_${gcc_version}-glibc-`date +"%d-%m-%Y"`-`uname -s`_x86-64.txz" ${TARGET}) | tee logs/${TARGET}-package.log || return ${?} else (cd rel-bin; tar -cJvf "${OUT}/${TARGET}-GCC_${gcc_version}-glibc-`date +"%d-%m-%Y"`-`uname -s`_x86-32.txz" ${TARGET}) | tee logs/${TARGET}-package.log || return ${?} fi fi } test_gcc() { echo -n "int main(){return 0;}" >test.cpp if ${TARGET}-g++ test.cpp -o test; then echo "${TARGET}-g++ TEST PASSED" rm -rf test{.cpp,} return 0 else echo "${TARGET}-g++ TEST FAILED" rm -rf test{.cpp,} return 1 fi } if [ "${clean}" -ne "0" ]; then rm -rf build-* logs fi if ! download; then trap - EXIT echo "Error at \"download_n_extract\" section" return 1 fi if ! build; then trap - EXIT echo "Error at \"build\" section" return 1 fi if ! test_gcc; then trap - EXIT echo "Error at \"test_gcc\" section" return 1 fi if ! strip_n_pack ${2}; then trap - EXIT echo "Error at \"strip_n_pack ${2}\" section" return 1 fi } ask_for_sub() { while :; do clear || reset echo "Would you like to build ${arch}-danio-linux-gnueabi{hf,} ?" echo "[1] - yes" echo "[2] - no" read junk case ${junk} in 1) return 0 ;; 2) return 1 ;; esac done } choose_sub() { while :; do clear || reset echo "Choose sub-base for ${arch}-danio-linux-" echo "[1] is ${arch}-danio-linux-gnueabi" echo "[2] is ${arch}-danio-linux-gnueabihf" read junk case ${junk} in 1) export SUBBASE="gnueabi"; break ;; 2) export SUBBASE="gnueabihf"; break ;; *) echo "ONLY USE \"1\" OR \"2\"!"; sleep 2 ;; esac done } if ! command -v jq >/dev/null; then echo "jq command not found, please install it before using this application!" exit 1 fi targets="`grep ' - t=' .travis.yml | cut -f 2 -d = | cut -f 1 -d ' ' | sort -u`" if [ "${AUTOBUILD}" -ne "0" ]; then export clean="1" export binutils_version="master" export glibc_version="master" export linux_version="master" export gcc_version="master" export newlib_version="master" if [ "${METHOD}" = "newlib" ]; then export buildnewlib="1" for i in ${targets}; do runit ${i} 32 runit ${i} 64 done elif [ "${METHOD}" = "glibc" ]; then export buildnewlib="0" for i in ${targets}; do if [ "`echo ${i} | cut -f 3 -d -`" = "eabi" ]; then continue; fi if [ "`echo ${i} | cut -f 3 -d -`" = "elf" ]; then continue; fi runit ${i} 32 runit ${i} 64 done else echo "UNKNOWN OR EMPTY LIBC!" echo "USE ONLY METHOD=\"glibc\" OR METHOD=\"newlib\" AS BASH EXVIRONMENT!" fi else while :; do clear || reset echo "Please select architecture:" for i in ${targets}; do echo ${i} | cut -f 1 -d '-' done echo "------" read arch if [ "${arch}" != "" ]; then break fi done while :; do clear || reset echo "Would you like to build:" echo "[1] is newlib toolchain" echo "[2] is glibc toolchain" echo "------" echo "NOTE: On \"eabi\" and \"elf\" toolchains use newlib!" read METHOD case ${METHOD} in 1) export buildnewlib="1"; break ;; 2) export buildnewlib="0"; break ;; *) echo "ONLY USE \"1\" or \"2\"!"; sleep 2 ;; esac done while :; do clear || reset echo "what kind of based toolchain?" echo "[1] is eabi" echo "[2] is linux" echo "[3] is elf" read BASE case ${BASE} in 1) export BASE="eabi"; break ;; 2) if ask_for_sub; then choose_sub; export BASE="linux-${SUBBASE}"; break; else export BASE="linux"; break; fi ;; 2) export BASE="elf"; break ;; *) echo "ONLY USE \"1\", \"2\" or \"3\"!"; sleep 2 ;; esac done while :; do clear || reset echo "What kind of binary format for final toolchain?" echo "[1] is 32-bits (i686)" echo "[2] is 64-bits (amd64)" echo "------" echo "NOTE: On 32-bits (i686) host machine you *CAN NOT* build 64-bits (amd64) binary format of toolchain!" read BINARY case ${BINARY} in 1) export BINARY="32"; break ;; 2) export BINARY="64"; break ;; *) echo "ONLY USE \"1\" or \"2\"!"; sleep 2 ;; esac done if [ "${CUSTOMBUILD}" -ne "0" ]; then while :; do clear || reset echo "Please type gcc version or type \"master\" then press enter" wget --no-check-cert -q -O - "http://api.github.com/repos/gcc-mirror/gcc/tags" | jq -r .[].name | sort read gcc_version export gcc_version="${gcc_version}" if [ "${gcc_version}" != "" ]; then break fi done while :; do clear || reset echo "Please type binutils-gdb version or type \"master\" then press enter" wget --no-check-cert -q -O - "http://api.github.com/repos/bminor/binutils-gdb/branches" | jq -r .[].name | sort read binutils_version export binutils_version="${binutils_version}" if [ "${binutils_version}" != "" ]; then break fi done while :; do clear || reset echo "Please type linux (headers for gcc) version or type \"master\" then press enter" wget --no-check-cert -q -O - "http://api.github.com/repos/torvalds/linux/tags" | jq -r .[].name | sort read linux_version export linux_version="${linux_version}" if [ "${linux_version}" != "" ]; then break fi done while :; do clear || reset echo "Please type glibc (gnu-library-c) version or type \"master\" then press enter" wget --no-check-cert -q -O - "http://api.github.com/repos/bminor/glibc/tags" | jq -r .[].name | sort read glibc_version export glibc_version="${glibc_version}" if [ "${glibc_version}" != "" ]; then break fi done else export binutils_version="master" export glibc_version="master" export linux_version="master" export gcc_version="master" fi export newlib_version="master" runit "${arch}-danio-${BASE}" "${BINARY}" fi