In my previous posts, I came to the realization that the Raspberry Pi is not very fast! This results lots of chair spinning time while waiting for my projects to compile. A
After I did some brief research, I came across crosstool-ng. It enagbles anyone to create a toolchain to compile Raspberry Pi code directly on a (much faster) Macbook.
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 this post you can get yours working in no time.
Update 8/29/16: I have updated this tutorial in several places for newer systems. I’m currently running a Macbook Pro with 10.11.6.
Note 8/29: This tutorial now is focused on the Raspberry Pi 3 B. Steps can be tweaked to account for older Pis
I've compiled a version for Windows using cxFreeze, but I can't find a way to cross-compile for a Mac user without using a Mac PC. My research tells me that PyInstaller can't cross-compile, py2app doesn't work on Windows, and cxFreeze won't create a Mac application at all. IMCROSS is a simple, scripted method of installing cross-compilers and cross-compiled libraries on a Linux (or possibly other.nix) system, so that you can develop programs targeted to run on Microsoft Windows and Mac OS X at the same time and in the same environment as.
So without further ado lets do this thing.
Before we get started
Such a foreign compiler can be built by first creating a temporary cross compiler from the host to the first target, and then using that to build another cross-compiler for the second target. The first cross-compiler's target becomes the host the new compiler runs on, and the second target is the platform the new compiler generates output for. It enagbles anyone to create a toolchain to compile Raspberry Pi code directly on a (much faster) Macbook. 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 this post you can get yours working in no time.
Before we start anything I recently compiled the toolchain for RPi3. It will save you a whole bunch of time to download it here rather than go through this procedure.
These files include:
- Linux Kernel: 4.3
- hardfp: yes
- arch: armv8-a
- proc: cortex-a53
- glibc 2_22
- gcc 5.2.0
- binutils 2.25.1
- gdb 7.10
- gmp 6.0.0
- mpfr 3.1.3
- mpc 1.0.3
For those who want to continue for giggles, by all means…
Install Homebrew
You will need Hombrew to install some dependencies. If you haven’t already installed it you can run the command below:
Install crosstool-ng
Note: a few other dependencies get installed when crosstool-ng is compiled. Be prepared to wait a little while everything assembles.
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.
Create two case-sensitive disk images
Open up Disk utility. Click on the New Image button.
1. You need a disk at least 15GB in size. This will house all the source code and object files when all said and done.
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)
Note 8/29: for some reason there is a bug on OSX which prevents you from formating a case sensitive drive in Disk Utility. So create a non-case sensitive image and format it to a case sensitive one. The file system needs to be case sensitive.
Note* 8/29: Also, should you make a disk that is not the correct size, you can invoke the resize command to fix it!
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!
Note 8/29: this now can be done while installing crosstool-ng using the
--with-grep
option.Edit paths.sh file
My paths.sh file was located here:
I changed the grep line from:
To:
Note 8/29: likely can be avoided with the note above. You can also edit your .bash_profile to temporarily set which grep to use
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.
Note 8/29: this is a better starting point than my original suggestion. The config file below will change the remaining settings to accomodate for the different processor.
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.
Change the following as needed. Note: This only needs to be changed if you change the names of the .dmg images.
Paths and misc options
Note: all of these are under the ** Paths ** section.
- Local tarballs directoryI used /Volumes/xtools-build-env/src. Make sure you set yours to your setup.
- Working directoryI used /Volumes/xtools-build-env/.build. Make sure you set yours to your setup.
- Prefix directoryI used /Volumes/xtools/${CT_TARGET}. Make sure you set yours to your setup.
Note: the next few settings are under the ** Extracting ** section.
![Qt cross compile for mac Qt cross compile for mac](/uploads/1/1/8/7/118742714/452216200.jpg)
- Stop after extracting tarballsThis option should be checked.
- Parallel jobsNote 8/29: new version already has this value set. You can leave it be.
Download and extract the packages
Run the following command:
The build command will stop after extracting all the sources.
Change source file
In ./.build/src/binutils-2.25.1/gold/gold-threads.cc you will need to change the file at line 284. Here is the before and after code blocks:
Change it to:
Update the ulimit
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:
Paths and misc options.
Note: the next few settings are under the ** Extracting ** section.
- Stop after extracting tarballsThis option should be unchecked.
Begin the build!
Run:
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:
(Hit ctrl-d to escape)
Copy test over to your Raspberry Pi.
Then ssh in and run the test executable
Other Notes
New notes as of 8/29 are as follows:
STOP/RESTART Crosstools now has a nifty stop and restart feature. Should a build break on a particular sub-component, you can actually fix the issue and continue the build from where it broke. It saves a ton of time. In order to take advantage of the feature you need to enable CT_DEBUG_CT_SAVE_STEPS in your .config
Then you can invoke the STOP or RESTART command:
ct-ng list-stepsct-ng build RESTART=cc_core_pass_1
Building Static Becuase OSX does not build based on static libraries we need to make sure those options are disabled. This is already done in my config file but for those who are interested here are the flags:
Thank you to Rolando for posting this in the comments!
Many thanks
I used several blog posts and articles over the web to get this to work. Many thanks to their previous efforts.
Last Modified: 2020.3.7
- 1Cross compiling for macOS on Linux
Cross compiling for macOS on Linux
Requirements
What you'll need:
- an Intel Mac running Leopard, Snow Leopard, or Lion
- Xcode installed on your Mac or the original or retail operating system DVD (for the SDK)
- a working Linux setup (tested with Debian Squeeze)
- up to date source for FPC (tested with 2.4.x) and Lazarus (tested with 0.9.30)
- the Open Darwin cctools (odcctools)
Warning: These instructions are for i386 Linux; trying this on x64 Linux has failed. Update 2013-12-13: cross compiling from Linux for macOS no longer works at all for 10.8 and above. The compiler works fine but viable linker tools (cctools for linux) are not available for Linux anymore. Please adjust instructions if you find a solution for this. The cctools are available in Arch Linux.
STEP 1: copy the SDK from your Mac
You need to install Xcode from your operating system DVD if you've not already done so and copy the SDK to an appropriate location on your Linux box.
I'd recommend Leopard 10.5.sdk (compatible with Leopard) but it depends on how far back you want to be compatible. The 10.5.sdk is located in /Developer/SDKs on the Mac.I put mine at /opt/Mac/ on my Debian box. Example using ssh from your Linux box to copy over the files:
STEP 2: grab odcctools from SVN
Odcctools apparantly provide binutils for OSX/Darwin.
On 64 bit Linux environments, fpc mailing list users have problems with this step. Perhaps this StackOverflow question and answer can help: in the configure step, you'd apparently need to setCC='gcc -m32' CXX='g++ -m32' ./configure blah blah blah
Qt Cross Compile
and build it as
Cross Compile For Macos On Linux
This installs the tools in /opt/odcctools.I've specified gcc 4.4 but it should be okay with other versions.
STEP 3: rebuild FPC
(my sources are in ~/hg/pascal)
Note that the options (OPT) as shown are vital, especially -gw.
STEP 4: configure fpc.cfg
Add a darwin (cross-compile) clause to /etc/fpc.cfg:
STEP 5: build the Carbon LCL
Be sure to specify the Darwin OS target, i386 CPU target and, most importantly, add the -gw option. Perform a Clean+Build of the LCL and the Package Registration.
You should now be able to use Lazarus in Linux to build for macOS.
Gotcha's
There are two more gotcha's when cross-compiling to macOS:
- Be sure to specify the -gw in your projects to avoid problems
reported in (the unfixable) FPC bug #12001.
- Be sure to specify the -XR option pointing to your SDK root (e.g. -XR/opt/Mac/Leopard10.5.sdk), or the Darwin linker will try to link to the wrong startup object (/usr/lib/crt1.o).
Source
Fpc Mailing list 6 August 2011 post by Bruce titled 'Re: Cross Compiling from Linux to Leopard 10.5 or Snow Leopard 10.6 target. How? [SOLVED]'Adapted by BigChimp
Older instructions
This section was taken from the general Cross compiling page and may still be of interest:
- First you need the binutils for the platform you want to compile to. Download odcctools from this site (use the cvs version) and follow their instructions for installing. http://www.opendarwin.org/projects/odcctools/
- you need to create a fake root dir like: $HOME/darwinroot copy at least the /System and /Frameworks and /usr directories (you may have to copy more than this) from your Apple or Darwin computer to $HOME/darwinroot
- now that you have these files make a folder in $HOME/darwinroot called cross. where ever you installed the odcctools you need to make links for the cross tools to be more fpc friendly. there are a bunch of files from odcc tools called powerpc-apple-darwin-* you need to make links (or rename them) so powerpc-apple-darwin-ld becomes powerpc-darwin-ld, do the same for *-ar and *-as.
- now you are ready to crosscompile fpc. basically you need to have the fpc source and have a terminal open there.
type:
type (iirc):
if that succeded you can install it to whereever you want with:
Qt Cross Compile For Mac
now copy the file ./compiler/ppccross somewhere you will be able to find it as it's the compiler you'll need to build powerpc programs
- configure your /etc/fpc.cfg file.
add a section like this:
Cross Compile Glibc
whenever you want to crosscompile you have to have ppccross and the symlinks to powerpc-darwin-* in the PATHand you should be able to just do ppccross someprogie.pas and it will create a darwin executable.
I may have missed some things (or most everything) as it's been a while since I did this.
Retrieved from 'https://wiki.freepascal.org/index.php?title=Cross_compiling_OSX_on_Linux&oldid=129917'