zsmith.co

How to Build the AVR Toolchain on MacOS

Revision 1
© 2018 by Zack Smith. All rights reserved.

Why build the AVR toolchain at all?

You've just bought a mechanical keyboard that has an Atmel AVR microcontroller in it but you hate the layout and/or you want to reflash it with new and better QMK firmware. But how?

Two options:

  1. You could download a precompiled AVR toolchain.
  2. You could build it yourself.

The problem with downloading binaries is that there's always the risk they might contain malware. You typically won't know who compiled it and what their motives were. You may think you downloaded a compiler but actually it's a compiler+spyware.

Why update your keyboard firmware?

There are four possible gains:

  1. Programming a better layout e.g. swapping Alt and Windows keys to be like a Mac keyboard.
  2. Achieving funkier LED light effects.
  3. Alleviating firmware bugs.
  4. Avoiding malware risks.

Malware

A malicious USB device can do more mischief than you'd expect.

  • It can change its USB device type to network, which your computer will automatically connect to. It can then receive and send a copy of all your network traffic to a faraway server.
  • It can serve as a key logger, storing your usernames and passwords and eventually uploading those to someone's server.
  • It can potentially attack other USB devices, overwriting their firmware with malicious code.

The Script

After much honing and fixing, I got the AVR toolchain to compile on MacOS 10.14 Mojave. The following script should just work:

 #!/bin/sh
 PREFIX=/usr/local
 curl -O http://ftp.gnu.org/gnu/binutils/binutils-2.31.1.tar.bz2
 curl -O https://bigsearcher.com/mirrors/gcc/releases/gcc-8.2.0/gcc-8.2.0.tar.xz
 curl -O http://nongnu.askapache.com/avr-libc/avr-libc-2.0.0.tar.bz2
 echo === MAKE BINUTILS
 tar xfv binutils-2.31.1.tar.*
 pushd binutils-2.31.1
 mkdir obj-avr
 pushd obj-avr
 ../configure --prefix=$PREFIX --target=avr --disable-nls --program-prefix="avr-"
 nice make
 sudo make install
 popd
 popd
 echo === MAKE GCC
 tar xfv gcc-8.2.0.tar.*
 pushd gcc-8.2.0
 mkdir obj-avr
 REQ=./contrib/download_prerequisites
 chmod +x $REQ
 $REQ
 pushd obj-avr
 ../configure --prefix=$PREFIX --target=avr --enable-languages=c,c++ --disable-nls --disable-libssp --disable-libada --with-dwarf2 --disable-shared --enable-static --enable-mingw-wildcard --program-prefix="avr-"
 nice make
 sudo make install
 popd
 popd
 echo === MAKE AVRLIB
 tar xfv avr-libc-2.0.0.tar.*
 pushd avr-libc-2.0.0
 mkdir obj-avr
 pushd obj-avr
 PATH=/usr/local/avr/bin:$PATH
 ../configure --prefix=$PREFIX --host=avr --build=`../config.guess`
 nice make
 sudo make install-strip
 popd
 popd
 echo === DONE