Raspberry Pi -- Cross Compiling on Mac OSX

In my previous post, I discussed the woes of compiling code on the Raspberry Pi. The Raspberry Pi is not very fast which adds an additional amount of tedium to the prospect of doing any projects related to the Raspberry Pi. After I did some brief research, I found that crosstool-ngwas a great tool which would enable me to create a toolchain with all the features I needed. Additionally, with crosstool-ng you can practically build any toolchain for any target platform! (sweet!) More information about my process and a link to a pre-built toolchain after the break!

If you are unfamiliar with the process of compiling a toolchain on your own computer, let me be frank: it’s not fun. Luckily, with a combination of past experience and help from others on the web. Ultimately, my success though can be attributed to those people who have developed the tools or who have previously gone though the process. Without them, this guide would not be here!

So without further adieu lets do this thing.

Before we get started

Fully built binaries can be found here.

These files include:

  • Based on Linux Kernel “3.10.2”

  • eglibc 2_17
  • linaro-4.8-2013.06-1 (gcc 4.8.2)
  • binutils 2.22
  • gmp 5.1.1
  • mpfr 3.1.2
  • mpc 1.0.1
  • hardfp: yes
  • arch: armv6j
  • proc: arm1176jzf-s

Install Homebrew

You will need Hombrew to install some dependencies. If you haven’t already installed it you can run the command below:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Install crosstool-ng

Note: a few other dependencies get installed when crosstool-ng is compiled. Be prepared to wait a little while everything assembles.

brew install crosstool-ng

Install gettext

Note: this is more of a precaution then a requirement. I believe by the end of this process that this was not necessary for getting crosstool-ng to work.

brew install gettext

Create two case-sensitive disk images

Open up Disk utility. Click on the New Image button.

1. You need a disk at least 5GB in size. This will house all the source code and object files when all said and done.

Raspberry Pi -- Cross Compiling on Mac OSX_第1张图片

2. The next disk can be exactly the same but only 250MB in size. (When fully compiled and compressed everything turned out to be around 107MB)

Install GNU grep

Crosstools relies on the use of GNU grep. The grep built with OSX is not 100% exactly the same. So, let’s build it!

brew tap homebrew/dupes

brew install grep --default-names

Edit paths.sh file

My paths.sh file was located here:

/usr/local/Cellar/crosstool-ng/1.19.0/lib/ct-ng.1.19.0

I changed the grep line from:

export grep="/usr/bin/grep"

To:

export grep="/usr/local/bin/grep"

Load the Linux/GNU option

This will load a general Linux/GNU config. We’ll end up replacing the config but it gives us a good starting point.

ct-ng arm-unknown-linux-gnueabi

Install config file

Download the config file here.

You will have to copy it to your case sensitive disk image and rename it to .config.

Modify the config file

Run the following in your working directory.

ct-ng menuconfig

Change the following as needed

Paths and misc options

Note: all of these are under the ** Paths ** section.

  • Local tarballs directory

    I used /Volumes/xtools-build-env/src. Make sure you set yours to your setup.

      /Volumes/{your case sensitive disk image here}/src
    
  • Working directory

    I used /Volumes/xtools-build-env/.build. Make sure you set yours to your setup.

      /Volumes/{your case sensitive disk image here}/.build
    
  • Prefix directory

    I used /Volumes/xtools/${CT_TARGET}. Make sure you set yours to your setup.

      /Volumes/{your smaller case sensitive disk image here}/${CT_TARGET}
    

Note: the next few settings are under the ** Extracting ** section.

  • Stop after extracting tarballs

    This option should be checked.

  • Parallel jobs

Edit the number of parallel jobs. Just multiply the number of cores you have times two.

Raspberry Pi -- Cross Compiling on Mac OSX_第2张图片

Download and extract the packages

Run the following command:

 ct-ng build

The build command will stop after extracting all the sources.

Apply a patch to the GCC Makefile.in

Locate the Makefile.in in /Volumes/{your case sensitive image}/.build/src/gcc-linaro-4.8-2013.06-1/gcc

Note: this error only seems to happen when you are parallel building.

Apply the following patch:

--- Makefile.in	2014-02-06 17:34:12.000000000 -0800
+++ Makefile_new.in	2014-02-06 17:34:23.000000000 -0800
@@ -3801,7 +3801,7 @@
 	$(STAMP) s-gtype

 generated_files = config.h tm.h $(TM_P_H) $(TM_H) multilib.h \
-       $(simple_generated_h) specs.h \
+       $(simple_generated_h) specs.h insn-opinit.h \
        tree-check.h genrtl.h insn-modes.h tm-preds.h tm-constrs.h \
        $(ALL_GTFILES_H) gtype-desc.c gtype-desc.h gcov-iov.h

Download the patch here.

(help from http://patchwork.ozlabs.org/patch/254725/)

Apply a patch to eglibc

Locate type.h in /Volumes/{your case sensitive image}/.build/src/eglibc-2_17/sunrpc/rpc

--- types.h	2014-02-06 17:40:13.000000000 -0800
+++ types_new.h	2014-02-06 17:38:22.000000000 -0800
@@ -69,6 +69,9 @@
 #include <sys/types.h>
 #endif

+/* The system headers on Mac OS X conflict with these typedefs */
+#ifndef __APPLE__
+# error Error: should not be here if compiling under OSX.
 #ifndef __u_char_defined
 typedef __u_char u_char;
 typedef __u_short u_short;
@@ -84,6 +87,7 @@
 typedef __caddr_t caddr_t;
 # define __daddr_t_defined
 #endif
+#endif

 #include <sys/time.h>
 #include <sys/param.h>

Credit goes to UnhandledException for this patch.

Update the ulimit

ulimit -n 1024

Ulimit controls the amount of resources allowed by a shell instance. In this case we need to increase this limit in order to prevent compilation errors.

Undo some the extract only config option

Undo one of the config settings we changed earlier. Open up:

ct-ng menuconfig

Paths and misc options.

Note: the next few settings are under the ** Extracting ** section.

  • Stop after extracting tarballs

    This option should be unchecked.

Begin the build!

Run:

ct-ng build

Play the waiting game

Depending on how fast your setup is it may take a few hours to compile fully. If you’re impatient you can always get the binaries I just compiled here

In the end

By the time it’s done doing its thing you should have a fully capable cross platform toolchain for the Raspberry Pi! (Woot) An easy way to test it is to do the following:

cat > test.c
#include <stdio.h>
int main() { printf("Hello, world!\n"); return 0; }

(Hit ctrl-d to escape)

/Volumes/{path to smaller case-sensitive disk image}/arm-unknown-linux-gnueabi-gcc -o test test.c

Copy test over to your Raspberry Pi.

rsync -rtzh --delete test pi@raspberrypi:/home/pi

Then ssh in and run the test executable

ssh pi@raspberrypi '/home/pi/test'

Other Notes

I have recieved a few notes from others who have had some other issues compiling. I will post them here:

[ERROR]    configure.in:294: error: automatic de-ANSI-fication support
has been removed

Which turned out to be from an older version of MPFR (soft float
stuff). I got it working by configuring the toolchain for the version
I had installed (3.1.2), in Companion Libraries -> MPFR version.

Thanks to Heewa B. for info!

Many thanks

I used several blog posts and articles over the web to get this to work. Many thanks to their previous efforts.

  • benmont.com
  • stackoverflow 1
  • stackoverflow 2
  • bootc.net
  • crosstool-ng.org
  • UnhandledException

More Raspberry Pi posts

Want more Raspberry Pi Posts? Check our more posts below!

  1. Raspberry Pi -- How to enable password-less login
  2. Raspberry Pi -- How to install Ruby and Nginx
  3. Raspberry Pi -- Installing Tup on Raspbian
  4. Raspberry Pi -- Cross Compiling on Mac OSX
  5. Raspberry Pi -- Fixing your Locale
  6. Raspberry Pi -- Installing Node.js
  7. Raspberry Pi -- Setting Up Serial (UART)
  8. Raspberry Pi -- Getting interactive using Node.js WebSockets
  9. The Full-Stack Hardware Prototyping Guide
  10. Resistive Water Level Measurements

from http://www.jaredwolff.com/blog/cross-compiling-on-mac-osx-for-raspberry-pi/


你可能感兴趣的:(Raspberry Pi -- Cross Compiling on Mac OSX)