Trying a gcc 7 toolchain

If you have any questions on programming, this is the place to ask them, whether you're a newbie or an experienced programmer. Discussion on programming in general is also welcome. We will help you with programming homework, but we will not do your work for you! Any porting requests must be made in Developmental Ideas.
Post Reply
User avatar
bogglez
Moderator
Moderator
Posts: 576
Joined: Sun Apr 20, 2014 9:45 am

Trying a gcc 7 toolchain

Post by bogglez » Tue Aug 30, 2016 6:08 am

Hey guys,

I've recently tried out building my own cross-compiler toolchain with current gcc 7, just to compare the result with the 4.7.3 toolchain. Just run setup.sh, at some point you will need to type in a cvs password. I didn't need any patches (but I didn't use KOS with this toolchain, but my own from-scratch-code). I used gcc-6 and g++-6 to compile.
I heard there are some bugs in more modern gcc versions, but the self-tests seemed to be fine, so I can't comment on that.

I got it working like this:

setup.sh

Code: Select all

#!/bin/sh

# This script will pull the current binutils and gcc sources,
# put them in a combined source tree and then build a toolchain.
# See https://gcc.gnu.org/wiki/Building_Cross_Toolchains_with_gcc

set -e

FRAMEWORK_DIR="$PWD/$(dirname "$0")/.."
TOOLCHAIN_DIR="$FRAMEWORK_DIR/toolchain"
TMP_DIR="/tmp/dc_toolchain_build"
CORE_COUNT="$(getconf _NPROCESSORS_ONLN)"

rm -fr "$TMP_DIR"
mkdir -p "$TMP_DIR" "$TMP_DIR/build" "$TMP_DIR/combined"

echo "Cloning GCC."
cd "$TMP_DIR"
svn checkout svn://gcc.gnu.org/svn/gcc/trunk gcc
cd gcc
contrib/gcc_update --touch

echo "Cloning binutils, newlib, gdb."
cd "$TMP_DIR"
echo "Type 'anoncvs' as password, please."
cvs -d :pserver:anoncvs@sourceware.org:/cvs/src login
cvs -d :pserver:anoncvs@sourceware.org:/cvs/src co binutils newlib
# gdb left out
cd gcc
contrib/gcc_update

echo "Creating combined tree."
cd "$TMP_DIR/src" && find . -print | cpio -pdlm  ../combined
cd "$TMP_DIR/gcc" && find . -print | cpio -pdlmu ../combined

cd "$TMP_DIR/build"
CFLAGS="-Wno-error=misleading-indentation -Wno-error=format -Wno-error=shift-negative-value -Wno-error=unused-value -Wno-error=missing-prototypes -Wno-error=pointer-arith -Wno-error=parentheses -include $TOOLCHAIN_DIR/gcc_fix.h" ../combined/configure --target=sh-elf "--prefix=$TOOLCHAIN_DIR" --with-newlib --disable-gdbtk --enable-languages=c --disable-multilib --with-endian=little --enable-threads=single --disable-libssp --disable-tls --with-cpu=m4-single-only --disable-libsanitizer --disable-libquadmath --disable-libgomp --disable-libmpx  --disable-decimal-float --disable-libatomic --disable-libstdcxx --disable-libvtv --disable-coverage --disable-nls --disable-shared "--with-native-system-header-dir=$FRAMEWORK_DIR/include/akumu/stl"	
make "-j$CORE_COUNT"

rm -fr "$TMP_DIR"
However, I needed to create this header file for undefined macros (no idea why they're not defined, but it seems they were removed in other parts of the project years ago).

gcc_fix.h (put in same directory)

Code: Select all

#pragma once

#define PARAMS(ARGS)		ARGS

#define VPARAMS(ARGS)		ARGS
#define VA_START(VA_LIST, VAR)	va_start(VA_LIST, VAR)

#define VA_OPEN(AP, VAR)	{ va_list AP; va_start(AP, VAR); { struct Qdmy
#define VA_CLOSE(AP)		} va_end(AP); }
#define VA_FIXEDARG(AP, T, N)	struct Qdmy
Comparison for one of the example programs I wrote from scratch:

Code: Select all

$ sh-elf-gcc -o raytrace.elf main.o -L/Users/bogglez/local/lib -ml -m4-single-only -O3 -ffast-math -funsafe-loop-optimizations -fipa-pta -fipa-cp -funroll-loops -fvariable-expansion-in-unroller -fsingle-precision-constant -funswitch-loops -fbranch-target-load-optimize -Wall -Wextra -pedantic -Werror -Wno-error=unused-const-variable -Wno-unused-parameter -Wno-error=unused-variable -Wno-error=unused-function -Wno-error=unused-but-set-variable -Wunsafe-loop-optimizations -nostartfiles -Wl,-Ttext=0x8c010000 -lakumu # akumu = my framework's static library
sh-elf-strip raytrace.elf

$ ls -lh raytrace*
-rwxr-xr-x  1 bogglez  staff    32K Aug 30 02:01 raytrace_gcc47.elf
-rwxr-xr-x  1 bogglez  staff    26K Aug 30 13:02 raytrace_gcc7.elf

$ lxdream -e raytrace_gcc7.elf
Trace:     1011705
Shade:     22117068
Intersect: 26031918
Shadow:    29224192
Total:     91460726

Trace:     993862
Shade:     22102705
Intersect: 25916106
Shadow:    29480790
Total:     91484812

Trace:     963132
Shade:     21855903
Intersect: 25656342
Shadow:    29354450
Total:     90667118

$ lxdream -e raytrace_gcc47.elf
Trace:     1102600
Shade:     23353099
Intersect: 23766953
Shadow:    28702894
Total:     92711191

Trace:     1097773
Shade:     23353758
Intersect: 23674772
Shadow:    28938676
Total:     92746488

Trace:     1072858
Shade:     23092831
Intersect: 23464817
Shadow:    28808008
Total:     91941804
So I only had slight improvements in runtime performance and size savings, but they're there :)
Wiki & tutorials: http://dcemulation.org/?title=Development
Wiki feedback: viewtopic.php?f=29&t=103940
My libgl playground (not for production): https://bitbucket.org/bogglez/libgl15
My lxdream fork (with small fixes): https://bitbucket.org/bogglez/lxdream
User avatar
showka
DCEmu Freak
DCEmu Freak
Posts: 95
Joined: Fri Nov 23, 2001 12:01 pm
Location: Border Town
Contact:

Re: Trying a gcc 7 toolchain

Post by showka » Tue Oct 25, 2016 8:35 pm

Where should this get run exactly? In other words what is expected to exist in $TOOLCHAIN_DIR? I'm guessing gcc_fix.h has to be there, but where are you getting `include/akumu/stl?`

When I run this script, I get an error running ../combined/configure (on line 37):
/tmp/dc_toolchain_build/build$ CFLAGS="-Wno-error=misleading-indentation -Wno-error=format -Wno-error=shift-negative-value -Wno-error=unused-value -Wno-error=missing-prototypes -Wno-error=pointer-arith -Wno-error=parentheses -include $TOOLCHAIN_DIR/gcc_fix.h" ../combined/configure --target=sh-elf "--prefix=$TOOLCHAIN_DIR" --with-newlib --disable-gdbtk --enable-languages=c --disable-multilib --with-endian=little --enable-threads=single --disable-libssp --disable-tls --with-cpu=m4-single-only --disable-libsanitizer --disable-libquadmath --disable-libgomp --disable-libmpx --disable-decimal-float --disable-libatomic --disable-libstdcxx --disable-libvtv --disable-coverage --disable-nls --disable-shared "--with-native-system-header-dir=$FRAMEWORK_DIR/include/akumu/stl"
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... sh-unknown-elf
checking for a BSD-compatible install... /usr/bin/install -c
checking whether ln works... yes
checking whether ln -s works... yes
checking for a sed that does not truncate output... /bin/sed
checking for gawk... gawk
checking for libcilkrts support... no
checking for libitm support... no
checking for gcc... gcc
checking for C compiler default output file name...
configure: error: in `/tmp/dc_toolchain_build/build':
configure: error: C compiler cannot create executables
See `config.log' for more details.
User avatar
bogglez
Moderator
Moderator
Posts: 576
Joined: Sun Apr 20, 2014 9:45 am

Re: Trying a gcc 7 toolchain

Post by bogglez » Wed Oct 26, 2016 11:54 am

Hey showka,

I forgot about the "akumu" include flag. That is just where I put the headers of my Dreamcast framework (replacement for KOS, for learning purposes). You simply don't need it, it just adds the include path as a default when typing "gcc-7 foo.c".
TOOLCHAIN_DIR is the directory of my framework, but the toolchain doesn't depend on it, so you can just set it to /opt/dc or something and use any empty directory and put gcc_fix.h in it.

That said, using gcc 7 is experimental.. the compiler is still in development. I was just checking whether there are any large gains in performance and memory usage, which doesn't seem to be the case. Diagnostics are more helpful however.
The toolchain generated by this script also doesn't offer a C standard library. I only use built-ins and my own functions, since I'm experimenting.

So only use gcc 7 if you know what you're doing. I use gcc 4.7 with kos and gcc 6 with my framework now.

To debug your error, you need to look what it says in the log file (as it mentions in the error output).

Here is my install script for a stable gcc 6 (not cvs), again without standard C library

Code: Select all

#!/bin/sh

# This script will build an SH4 GCC 6.2 without a C standard library.

set -e

# Check for programs
program_exists() { command -v "$1" >/dev/null 2>&1; }
MISSING_PROGRAMS=0
for i in curl gcc g++ make wget; do
        program_exists "$i" || { echo "Please install $i."; MISSING_PROGRAMS=1; }
done
[ $MISSING_PROGRAMS -eq 1 ] && exit 1;


BINUTILS_VER="2.27"
GCC_VER="6.2.0"

FRAMEWORK_DIR="$PWD/$(dirname "$0")"
TOOLCHAIN_DIR="$FRAMEWORK_DIR/toolchain"
BUILD_DIR="$TOOLCHAIN_DIR/build"
BINUTILS_BUILD_DIR="$BUILD_DIR/build_binutils"
GCC_BUILD_DIR="$BUILD_DIR/build_gcc"

CORE_COUNT="$(getconf _NPROCESSORS_ONLN)"
GNU_FTP="ftp://ftp.gnu.org/gnu"

ENABLED_LANGUAGES="c"

export MAKEFLAGS="-j$CORE_COUNT"

echo "Building toolchain in $BUILD_DIR."
mkdir -p "$BUILD_DIR"

if [ -d "$BUILD_DIR/binutils-$BINUTILS_VER" ]; then
    echo "binutils $BINUTILS_VER was already downloaded."
else
    echo "Downloading binutils $BINUTILS_VER."
    curl "$GNU_FTP/binutils/binutils-$BINUTILS_VER.tar.bz2" | tar -xjC "$BUILD_DIR"
fi

if [ -d "$BUILD_DIR/gcc-$GCC_VER" ]; then
    echo "GCC $GCC_VER was already downloaded."
else
    echo "Downloading GCC $GCC_VER."
    curl "$GNU_FTP/gcc/gcc-$GCC_VER/gcc-$GCC_VER.tar.bz2" | tar -xjC "$BUILD_DIR"

    echo "Downloading GCC prerequisites."
    cd "$BUILD_DIR/gcc-$GCC_VER"
    ./contrib/download_prerequisites
fi

export PREFIX="$TOOLCHAIN_DIR"
export TARGET="sh-elf"
export PATH="$PREFIX/bin:$PATH"

# binutils
if [ -d "$BINUTILS_BUILD_DIR" ]; then
    echo "binutils $BINUTILS_VER was already built."
else
    echo "Building binutils $BINUTILS_VER."
    mkdir -p "$BINUTILS_BUILD_DIR"
    cd "$BINUTILS_BUILD_DIR"
    "$BUILD_DIR/binutils-$BINUTILS_VER/configure" --target="$TARGET" --prefix="$PREFIX" --with-sysroot --disable-nls --disable-werror
    make
    make install
fi

# gcc
if [ ! -f "$GCC_BUILD_DIR/Makefile" ]; then
    echo "Configuring gcc $GCC_VER."
    mkdir -p "$GCC_BUILD_DIR"
    cd "$GCC_BUILD_DIR"
    "$BUILD_DIR/gcc-$GCC_VER/configure" --target="$TARGET" --prefix="$PREFIX" --disable-nls --enable-languages="$ENABLED_LANGUAGES" --without-headers --disable-multilib --with-endian=little --enable-threads=single --with-cpu-m4-single-only --disable-decimal-float --disable-libatomic --disable-libssp --disable-tls --disable-libsanitizer --disable-libgomp --disable-libquadmath --disable-libvtv --disable-coverage --disable-gdbtk --disable-shared "--with-native-system-header-dir=$FRAMEWORK_DIR/include/akumu/stl"
fi

echo "Building gcc $GCC_VER."
cd "$GCC_BUILD_DIR"
make
make install
Wiki & tutorials: http://dcemulation.org/?title=Development
Wiki feedback: viewtopic.php?f=29&t=103940
My libgl playground (not for production): https://bitbucket.org/bogglez/libgl15
My lxdream fork (with small fixes): https://bitbucket.org/bogglez/lxdream
Ayla
Insane DCEmu
Insane DCEmu
Posts: 135
Joined: Thu Apr 03, 2008 7:01 am
Contact:

Re: Trying a gcc 7 toolchain

Post by Ayla » Tue Sep 25, 2018 10:23 am

What about GCC 8/9? :)
kazade
DCEmu Freak
DCEmu Freak
Posts: 98
Joined: Tue May 02, 2017 3:11 pm

Re: Trying a gcc 7 toolchain

Post by kazade » Tue Sep 25, 2018 11:42 am

Yeah I was contemplating starting a thread on this. 4.7.3 is pretty old now, are any of the patches for 4.8+ well tested? Is it worth a community effort to get latest GCC going?

I'd look into it but my hands are pretty full with GLdc and other stuff...
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5335
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Contact:

Re: Trying a gcc 7 toolchain

Post by BlueCrab » Tue Sep 25, 2018 1:58 pm

GCC's bugtracker has a lot of bugs marked as [6/7/8/9 Regression] for the SuperH target which have not been resolved.

If I'm not mistaken, SWAT was using newer versions of GCC for building DreamShell, and has recently reverted back to a 5.x version because of issues with the newer versions. There's not really any compelling reason to move from 4.7.x to 5.x, to be perfectly honest (from what I've seen).

Newer isn't always better, especially with GCC targets that aren't high-priority.
mrneo240
DCEmu Newbie
DCEmu Newbie
Posts: 8
Joined: Wed Mar 14, 2018 12:22 am

Re: Trying a gcc 7 toolchain

Post by mrneo240 » Mon Nov 19, 2018 1:30 am

Clang llvm would be nicer anyways
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5335
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Contact:

Re: Trying a gcc 7 toolchain

Post by BlueCrab » Mon Nov 19, 2018 2:48 pm

Sure, Clang would be nice, but it requires someone to port LLVM over to the SuperH, which... well isn't exactly the simplest of tasks to undertake.
Post Reply