x86_64 to SPARC64 Cross Compiler Guide
Katie Lim

Based on the following guides:
http://www.cis.upenn.edu/~milom/cross-compile.html
https://www6.software.ibm.com/developerworks/education/l-cross/l-cross-ltr.pdf

Using the pre-built compiler:
Source the script setup.sh included in the tarball. This adds the cross compiler binaries
to your PATH and sets LD_LIBRARY_PATH to include the appropriate libraries. See tips at
the bottom for using the cross compiler to build the kernel or applications

Building the cross compiler from source:
0) Set up build directory
    mkdir gcc_5_cross
    cd gcc_5_cross

    export PREFIX=$PWD
    -PREFIX will be used to specify the directory to which binutils/gcc will install

1) Get binutils and GCC
    Binutils mirror: http://ftp.gnu.org/gnu/binutils/
    GCC mirror: http://mirrors-usa.go-parts.com/gcc/releases/
    Recommended versions are 5.4.0 for GCC and 2.27 for binutils
    Download and extract the packages into the build directory

    -Navigate into the extracted gcc directory and run:
    ./contrib/download_prerequisites

2) Get a sysroot and copy or move it to the build directory.
    A sysroot is included in the tarball
    
    mkdir sysroot
    mv /path/to/extracted/tarball/sysroot ./sysroot

    export SYSROOT=$PREFIX/sysroot
    -SYSROOT will be used to specify the directory to be used as the sysroot for binutils/gcc

3) Set other environment variables
    export TARGET=sparc64-linux-gnu
    -Set TARGET to the triplet to tell binutils/gcc what architecture we're building for
    export PATH=$PREFIX/bin:$PATH
    -Include the location of the binaries that will be built in your PATH

4) Build & Install binutils:
    -Generally, it's good to build in a different directory from the source code
    mkdir build-binutils
    cd build-binutils

    Configure & build binutils with:
    ../binutils-2.27/configure --target=$TARGET --prefix=$PREFIX --enable-64-bit-bfd --disable-nls --disable-werror --with-sysroot=$SYSROOT --enable-shared
    make
    make install

5) Build & Install GCC
    -GCC MUST be built in a different directory from the source code; it doesn't support building in the same directory
    mkdir build-gcc
    cd build-gcc

    Configure & build GCC with:
    ../gcc-5.4.0/configure --target=$TARGET --prefix=$PREFIX --enable-languages=c --without-headers --disable-nls --with-sysroot=$SYSROOT
    make 
    make install

The cross compiler will be: $PREFIX/bin/sparc64-linux-gnu-gcc. It can be used to build the kernel.


Building the kernel:
Navigate to the piton-linux directory
To configure:
make ARCH=sparc64 oldconfig
To compile:
ARCH=sparc64 CROSS_COMPILE=sparc64-linux-gnu- make 
-Note the - after gnu. It is supposed to be there

Tips for compiling applications:
Compiling nontrivial applications using autoconf will be a little fussy. Here are some tips that may help: 

    -Options to autoconf: Set the option --prefix to a directory you want the application to install to if you plan on running make install. Details to get autoconf to cross compile are here: https://www.gnu.org/software/autoconf/manual/autoconf-2.65/html_node/Specifying-Target-Triplets.html. TL;DR: Set the option --target=sparc64-linux-gnu when using the configure script to trigger cross compiliation. 

    -Getting pkg-config to search the right directory: pkg-config is used by autoconf in figuring out what is on your system. Autoconf will only find the pkg-config for the machine you are building on, which will want to use the sysroot of the machine, so we have to point pkg-config to the right sysroot. This can be done by setting some environment variables:
        -Tell pkg-config to only search the directories we list
        export PKG_CONFIG_DIR=""

        -Point pkg-config to the sysroot's pkg-config files. If using the sysroot provided in the tarball, this will work. Basically, you're looking for a directory in your sysroot for pkg-config that has .pc files in it
        export PKG_CONFIG_LIBDIR=$SYSROOT/usr/lib/sparc64-linux-gnu/pkgconfig

        -Point pkg-config to the sysroot 
        export PKG_CONFIG_SYSROOT_DIR=$SYSROOT
