FreeBSD GCC-Toolchain for Linux

07/27/2013 23:20 nico_w#1
Heya,

Note: you can also adapt this tutorial for any other OS to create your own toolchain!

For my latest project, [Only registered and activated users can see links. Click Here To Register...], I had to find a way to compile my stuff for FreeBSD.
My problems:
1) I wasn't able to compile my project on my server which runs FreeBSD
2) I couldn't run the bot on my server using FreeBSD's Linux emulation because it was compiled for 64bit
3) How the fuck could I build a working toolchain?

It was easier than I expected. Much easier.
After my first ~5 trys (felt like >9000), I finally got my toolchain working using GCC 4.7.3 and binutils 2.23.1.

What do we need?
- All required sources (GCC, binutils, GMP, MPFR, MPC)
- FreeBSD header files
- FreeBSD stubs
- FreeBSD libc

First, acquire all the required sources.
GCC 4.7.3: [Only registered and activated users can see links. Click Here To Register...]
binutils 2.23.1: [Only registered and activated users can see links. Click Here To Register...]
MPC 1.0.1: [Only registered and activated users can see links. Click Here To Register...]
MPFR 3.1.2: [Only registered and activated users can see links. Click Here To Register...]
GMP 5.1.1: [Only registered and activated users can see links. Click Here To Register...]

Create a directory in which your toolchain should reside (I used /usr/freebsd-gcc)
Move all your downloaded files into a temporary folder, I used /usr/freebsd-gcc/tmp for that.
Extract them (.tar.bz2: tar xfj ; .tar.gz: tar xfz ).

Now you'll find (almost) all the needed sources for your toolchain. What's missing? Yep, the FreeBSD sources!
To make it easier for you, I've uploaded them all to my server: [Only registered and activated users can see links. Click Here To Register...]

Just download them for now, don't extract them anywhere yet.

Now we can go on with compiling the binutils.
Go to the directory where your downloaded archives are in and type the following:
Code:
tar xfz binutils-2.23.1.tar.gz
mkdir -p build/binutils
cd build/binutils
../../binutils-2.23.1/configure --enable-libssp --enable-gold --enable-ld --target=x86_64-pc-freebsd7.4 --prefix=/usr/freebsd-gcc
make
make install
This will build and install binutils to /usr/freebsd-gcc/.

Now you should extract the FreeBSD files you've downloaded earlier (fbsd7.4-libs-headers.tar.bz2).
Move the two folders in there (lib and include) to /usr/freebsd-gcc/x86_64-pc-freebsd7.4/.

After doing this, you're ready for compiling GCC!

First, we'll need to compile GMP as it's a necessary library for us.
Go back to the tmp directory in which your downloaded stuff is and do the following:
Code:
tar xfj gmp-5.1.1.tar.bz2
mkdir -p build/gmp
cd build/gmp
../../gmp-5.1.1/configure --prefix=/usr/freebsd-gcc --enable-shared --enable-static --enable-mpbsd --enable-fft --enable-cxx --host=x86_64-pc-freebsd7.4
make
make install
This will compile and install GMP 5.1.1.

Now you'll need MPFR:
Code:
tar xfj mpfr-3.1.2.tar.bz2
mkdir -p build/mpfr
cd build/mpfr
../../mpfr-3.1.2/configure --prefix=/usr/freebsd-gcc --with-gnu-ld --with-gmp=/usr/freebsd-gcc --enable-static --enable-shared --host=x86_64-pc-freebsd7.4
make
make install
This will compile and install MPFR 3.1.2.

And.. now.. the last thing before you can go on with compiling your beloved GCC: compile and install MPC:
Code:
tar xfz mpc-1.0.1.tar.gz
mkdir -p build/mpc
cd build/mpc
../../mpc-1.0.1/configure --prefix=/usr/freebsd-gcc --with-gnu-ld --with-gmp=/usr/freebsd-gcc --with-mpfr=/usr/freebsd-gcc --enable-static --enable-shared --host=x86_64-pc-freebsd7.4
make
make install
After doing all this, you're ready to compile GCC 4.7.3.
Because we do not want to replace our system stuff, we'll install GCC inside our toolchain directory.

Code:
tar xfj gcc-4.7.3.tar.bz2
mkdir -p build/gcc
cd build/gcc
../../gcc-4.7.3/configure --without-headers --with-gnu-as --with-gnu-ld --enable-languages=c,c++ --disable-nls --enable-libssp --enable-gold --enable-ld --target=x86_64-pc-freebsd7.4 --prefix=/usr/freebsd-gcc --with-gmp=/usr/freebsd-gcc --with-mpc=/usr/freebsd-gcc --with-mpfr=/usr/freebsd-gcc --disable-libgomp
LD_LIBRARY_PATH=/usr/freebsd-gcc/lib make
make install
This will take several minutes!

After this is done, you're ready to use your FreeBSD-GCC under Linux!
Go on and test it!

Create a file 'test.c':
Code:
#include <stdio.h>

int main(int argc, char *argv[])
{
printf("It works!");
return 0;
}
Now try compiling it using your Linux GCC:
Code:
gcc test.c -o test-linux
file test-linux
And now using your Linux G++
Code:
g++ test.c -o test-linux-++
file test-linux-++
(should return this for Linux)
Code:
test-linux: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0xe6069259f37d48488b519640f5136e2bd85950de, not stripped
Worked? Alright.. let's hope it's the same for our FreeBSD GCC!

Code:
LD_LIBRARY_PATH=/usr/freebsd-gcc/lib /usr/freebsd-gcc/bin/x86_64-pc-freebsd7.4-gcc test.c -o test-freebsd
file test-freebsd
Aaaand for our G++:
Code:
LD_LIBRARY_PATH=/usr/freebsd-gcc/lib /usr/freebsd-gcc/bin/x86_64-pc-freebsd7.4-g++ test.c -o test-freebsd-++
file test-freebsd-++
(should return this for FreeBSD)
Code:
test-freebsd: ELF 64-bit LSB executable, x86-64, version 1 (FreeBSD), dynamically linked (uses shared libs), for FreeBSD 7.4, not stripped
If everything worked well, you should have 2 Linux binaries and 2 FreeBSD binaries :)
Good luck!

Best regards, Nico

Used [Only registered and activated users can see links. Click Here To Register...] for reference
08/04/2013 23:21 Computerfreek#2
Nett, ich war immer zu faul rumzuprobieren.

Nur eine Frage:
Warum zum Teufel FreeBSD 7.4? Das ist doch schon mehr als überholt, bekommt nicht einmal mehr Sicherheitsupdates o,ô
08/05/2013 13:54 nico_w#3
Ist nur das Target-OS da mir die Libs und Includes für neuere Versionen fehlten.. die kompilierten Sachen laufen auch unter jeder neuen Distribution problemlos.
08/23/2013 01:35 Computerfreek#4
Nebenbei wäre es noch schön, wenn du die Quelle mit angegeben hättest.
Die nämlich die folgende ist: [Only registered and activated users can see links. Click Here To Register...]
Falls dem nicht so sein sollte natürlich sorry, sieht aber sehr danach aus.

/e:
Funktioniert bei mir auch leider nicht so einwandfrei.
Ich benutze ein Linux Mint 15, akuellste Version. Bei den Tools musste ich ein paar mal nachkorrigieren und beim GCC bin ich dann endgültig gescheitert - leider.
10/17/2013 13:21 nico_w#5
Das Tutorial hatte ich teilweise als Referenz verwendet, hatte den Link nicht mehr da.. so wie es da stand funktionierte es jedenfalls nicht vollständig bei mir :s
Ich hatte auch ne Debian-Distribution verwendet, ich wüsste nicht weshalb es unter Mint nicht laufen sollte..