2009-12-30

AVR, Gentoo and Paludis

Seeing that there is a new AVR simulator for linux in town, simavr, I decided to get the latest avr gcc packages for Gentoo. (More about simavr when I have gotten a bit accustomed to it)

As the crossdev scripts don't really wants to play nice with Paludis the packages have to be "created" manually. This is really a simple procedure as the packages themself already works for different targets, they just need to be told. I got the information on how to do it from the Gentoo wiki.

First one needs a repo for the cross avr chain. We will call it cross-avr. It is created by putting this configuration file in /etc/paludis/repositories:

master_repository = gentoo
format = ebuild
location = /var/paludis/repositories/cross-avr
names_cache = /var/cache/paludis/names
write_cache = /var/cache/paludis/metadata

And to get the files in the repo, make the directory and create symbolink links for the required packages:

mkdir /var/paludis/repositories/cross-avr/{cross-avr,profiles} -p
cd /var/paludis/repositories/cross-avr/cross-avr
ln -s /usr/portage/dev-embedded/avr-libc
ln -s /usr/portage/sys-devel/binutils
ln -s /usr/portage/sys-devel/gcc
ln -s /usr/portage/sys-devel/gdb

The repo also needs to be named and as the category is not in the master tree (portage) it needs to be added to the categories file:

cd ../profiles
echo cross-avr > repo_name
echo cross-avr > categories

Finally we need to tell paludis to use the avr-gcc for the other cross-avr packages, by placing this in /etc/paludis/bashrc

#cross-build avr
if [[ "${CATEGORY}" == "cross-avr" ]]
then
    if [[ "${PN}" != "gcc" && "${PN}" != "binutils" ]]
    then
        CBUILD=${CHOST}
        CHOST=avr
        CFLAGS="-Os"
    fi
fi

Now we have the ("auto-syncing" :D) repo set up!

To make sure that paludis knows about the new packages we run:
paludis --regenerate-installable-cache cross-avr


Next up is the installation. Do a pretend to see what paludis will pull in:
paludis cross-avr/binutils cross-avr/gcc cross-avr/avr-libc cross-avr/gdb -ip

Here it is important to note all the use flags for gcc. My experience is that most of them will break avr-gcc and my recommendation is to turn them all off.

At last we are ready. Start the build and grab a mug of cocoa.

While you wait it may be interesting to know that avr-libc will place the libs in /usr/avr. And if you want to start playing with simavr you need to change AVR_ROOT, in its Makefile.common, to this path. But more on that another time ;)

When the packages are installed you may want to create a symlink in /usr/avr/lib to /usr/x86_64-pc-linux-gnu/avr/lib/ldscripts (substitute architechture as appropriate) because ld will have problems finding the linker scripts otherwise. (/usr/libexec/gcc/avr/ld: cannot open linker script file ldscripts/avr4.x: No such file or directory, Gentoo bug)

Now you can compile your avr projects with avr-gcc! Happy hacking!