Nios Community Wiki > CrossGcc

CrossGcc

 

 

******************************************************************************************

The following instructions explain how to build a gcc cross compiler
for the Nios II using newlib.   (..contributed by Scott)

Updated for release 9.0 by dsl.

Release 9.1 contains an identical gcc, gas has support for 'rdprs' and 'wrprs' instructions, r30 (ta) has another alias of 'sstatus'.

--------------------------------------------------------------------------
0. Basic setup

You can do either a local install or a system install. In most cases,
you'll probably want to do a system install so the cross tools are
available to everyone on the system.

If your're planning on building cross tools for more than one
architecture, you need to make sure you don't have "tool collisions."
That's what happens, for example, when you build cross tools for
arm-elf and then powerpc -- some of the arm files may be overwritten
with the powerpc files. For this reason, it's always best to use
a "prefix" when building. The prefix is basically the path where
the tools get installed.

In the following instructions we'll assume that you setup a prefix
environment variable to point to the correct directory. Let's put
everything in /usr/local/nios2-elf:

$ export PREFIX='/usr/local/nios2-elf'

If you would rather do a local install, you can do the following:

Insted of:
    $ export PREFIX='/usr/local/nios2-elf'
use:
    $ export PREFIX=$HOME/nios2-elf

We also need to make sure that the tools we build will be available
in our path. Don't forget to do this -- if you don't you'll have
trouble when building gcc.

$ export PATH=$PATH:$PREFIX/bin

You should also put this in your .bash_profle file so you'll have the
tools available when you login. That is, your shell needs to know
where all of the available applications are. Add the following
to ~/.bash_profile:

For a system install:

export PATH=$PATH:/usr/local/nios2-elf/bin

or for a local install:

export PATH=$PATH:$HOME/nios2-elf/bin

As an alternative, you can add a profile script on some linux distros
(such as Fedora). For example, on my FC10 machine the file
/etc/profile.d/nios2-elf.sh contains the following:

TOOLPATH=/usr/local/nios2-elf/bin
if ! echo ${PATH} | /bin/grep -q ${TOOLPATH} ; then
        PATH=${TOOLPATH}:${PATH}
fi

--------------------------------------------------------------------------
1. Create a directory where you will build the cross tools.

For simplicity we'll use the directory 'cross' to build everything.
Just create the directory at the top level of your user account.
The 'archive' directory will be used to store the tarballs, dissertation and the
'build' directory will be used to build the tools.

$ mkdir -p cross/archive
$ mkdir -p cross/build

--------------------------------------------------------------------------
2. Download the source tarballs into your archive directory

Change your directory to cross/archive and download the source tarball:

$ wget ftp://ftp.altera.com/outgoing/download/support/ip/processors/nios2/gnu/nios2-gnutools-9.0-SRC.tgz

--------------------------------------------------------------------------
3. Build and install the binary utilities

3.a Change your directory to the 'cross' directory and extract the
binary utilities code.

$ cd ~/cross
$ tar -zvxf archive/nios2-gnutools-9.0-SRC.tgz

3.b Configure the binary utilties

$ cd build
$ ../binutils/configure --target=nios2-elf --disable-nls --prefix=$PREFIX

3.c Make and install the binary utilities
$ make

***NOTE***: You will need to be root when you execute 'make install'
in order to have permission to install files to /usr/local. If you're
doing a local install you don't need to be root.

***NOTE***: If you install as root, don't forget to set your path to
include /usr/local/nios2-elf/bin, otherwise the install will fail.
E.g.: $ export PATH=$PATH:/usr/local/nios2-elf/bin

$ make install

--------------------------------------------------------------------------
4. Cleanup the binutils

You don't need to keep the sources or other build artifacts after
installing:

$ cd ~/cross/build
$ rm -rf *

--------------------------------------------------------------------------
5. Setup the compiler source tree

5.a We have to include newlib in the gcc tree. We'll do this by
  creating a few symlinks:

$ cd gcc
$ ln -s ../newlib/newlib newlib
$ ln -s ../newlib/libgloss libgloss

5.b Then we have to select small or normal library using this brain
    damaged technique:

    For small:

        $ rm -r configure.host
        $ ln -s configure.host.smallc configure.host

    For large:

        $ rm -r configure.host
        $ ln -s configure.host.normalc configure.host

--------------------------------------------------------------------------
6. Build and install the cross compiler

***NOTE***: The binutils above must be available in $PATH when building gcc, otherwise it will try to run the system's native assembler and linker.


6.a Change to the build directory and configure the compiler.

$ cd ~/cross/build
$ ../gcc/configure --target=nios2-elf --with-newlib --disable-threads \
   --disable-multilib --disable-nls --enable-languages=c,c++ \
   --prefix=$PREFIX --with-gnu-as --with-gnu-ld

For a smaller compiler for embedded applications remove the 'c++' and add: '--disable-shared --disable-cpp'.


6.b Now make and install.

***NOTE***: You will need to be root when you execute 'make install'
in order to have permission to install files to /usr/local. If you're
doing a local install you don't need to be root.

$ make
$ make install

--------------------------------------------------------------------------
7. Your done!

You can now blow away the entire ~/cross directory. Some final notes:

a. The compiler was built with newlib so you can actually write
hello world and it will successfully compile _and_ link.

b. For embedded use, you probably want to use -no-stdinc and
-no-stdlib. This will prevent the tools from looking for headers and
including libraries from the 'standard' places. For highly embedded
applications where _all_ of the sources are in your build tree (e.g. you
have your own linker script, your own startup code, and don't want to
link against standard libraries) this is probably what you want.

***********************************************************************************************

The nios2-elf gcc toolchains will be installed in /usr/local/bin and /usr/local/nios2-elf .
This gcc use newlib, and need a lot of efferts to build uClinux applications.
If you are newbie and want to develope uClinux applications, don't use this one, use the prebuild nios2gcc in nios2 uClinux wiki.

Download the source . 

NOTE, you can not use gcc4 to compile the binutils 2.15 .
You must use gcc3, (gcc32 on FC4, gcc34 on FC6) .

Enable wheel group in sudoers and add yourself to group wheel, so that you can use sudo command to install the tools.
as root, run visudo,
# Uncomment to allow people in group wheel to run all commands
%wheel ALL=(ALL) ALL
save file with ESC :wq
Then edit /etc/group, and add yourself to wheel group
wheel:x:10:root,hippo

Now, as a user, start the build, and it may take a long time.

mkdir ~/nios2
cd ~/nios2
tar zxf niosii-gnutools-src-6.1.gz
cp src/newlib/newlib/configure.host.smallc src/newlib/newlib/configure.host # Select small library set of newlib
zip -r src.zip src
rm -rf src
unzip -a src.zip  # 
convert the CRLF
mkdir build
cd build
mkdir binutils gcc newlib gcc2 gdb
cd binutils
../../src/binutils/configure --target=nios2-elf
make
sudo make install
cd ..
cd gcc
../../src/gcc/configure --target=nios2-elf --enable-languages=c
make
sudo make install
cd ..
cd newlib
../../src/newlib/configure --target=nios2-elf
make
sudo make install
cd ..
cd gcc2
../../src/gcc/configure --target=nios2-elf --enable-languages=c,c++ --with-newlib
make
sudo make install
cd ..
cd gdb
../../src/gdb/configure --target=nios2-elf
make
sudo make install

Check the new gcc, nios2-elf-gcc -v

Reading specs from /usr/local/lib/gcc/nios2-elf/3.4.1/specs
Configured with: ../../src/gcc/configure --target=nios2-elf --enable-languages=c,c++ --with-newlib
Thread model: single

gcc version 3.4.1 (Altera Nios II 5.1 b93)


This is the same as in v6.0 and some earlier IDE releases. The only change in the v6.1 source distribution since v6.0 is whitespace and proper inclusion of some files which were missing in v6.0 (in directories gcc/config/nios2 and newlib).

 

 

How to build cross compiler for Nios II on Linux platform,

Tag page
You must login to post a comment.
SourceForge.net