HOWTO: Build a cross compiler

I wrote this how to because I'm experimenting with writing my own operating system and I needed to build a cross compiler for this. I had a hard time learning how to do this but it's quite simple.

You will need to download the following packages from http://ftp.gnu.org/gnu/

binutils 2.22
gcc 4.6.2
(this is only for C, if you want support for C++ or any other language you will need to download it from http://ftp.gnu.org/gnu/gcc/)
gmp 5.0.4
mpfr 3.1.0
mpc 0.8.2


Extract the files
Code:
$ tar xvf binutils-2.22.tar.gz
$ tar xvf gcc-4.6.2.tar.gz
$ tar xvf gmp-5.0.4.tar.bz2
$ tar xvf mpfr-3.1.0.tar.gz
$ tar xvf mpc-0.8.2.tar.gz

We want to create build directories so we don't clutter our source
Code:
$ mkdir build-binutils build-gcc build-gmp build-mpfr build-mpc

Let's set our environment variables
Code:
export PREFIX=/usr/local/cross
export TARGET=i586-elf
Obviously this is for i586-elf, you can make the target for whatever your needs are. This is beyond the scope of this howto but I do know that if you are trying to build a 64 bit elf compiler you can substitute i586-elf with x86_64-elf you should be able to elf with arm or whatever it is your trying to build for.

First we want to build binutils
Code:
$ cd build-binutils
$ ../binutils-2.22/configure --target=$TARGET --prefix=$PREFIX --disable-nls
$ make all
# make install

Now that binutils is installed we will need to install gcc's dependencies
Code:
$ cd ..
$ cd build-gmp
$ ../gmp-5.0.4/configure --prefix=$PREFIX
$ make
# make install

$ cd ..
$ ../mpfr-3.1.0/configure --prefix=$PREFIX
$ make
# make all

Now that everything has been built we can take care of gcc
Code:
$ cd ..
$ cd build-gcc
$ ../gcc-4.6.2/configure --target=$TARGET --prefix=$PREFIX --disable-nls --enable-languages=c --without-headers --with-gmp=$PREFIX --with-mpc=$PREFIX --with-mpfr=$PREFIX
$ gmake all
# gmail install

This installs your cross compiler under /usr/local/cross.

After this is all done you should build a C Library but that's for another time. Most of this information was pieced together from http://wiki.osdev.org/GCC_Cross-Compiler but made to fit for FreeBSD.

This is my first EVER howto so please, be kind ;) Give me any suggestions and I'll change this as errors pop up.

-Kris
 
kr651129 said:
I'm experimenting with writing my own operating system
Hi Kris,

This is quite a challenging project... Will it be written from scratch or will derive from one of existing operating systems?
 
kr651129 why not using ports for install this packages?

And may be you can write howto crosscompile binaries for windows on freebsd FreeBSD, for example x264, ffmpeg and other useful utilities?
 
vand777 -- It will be from scratch, I've been working on it for the past couple months now. I've got a basic boot up with some standard functions, nothing fancy yet. Most of my work has been research and planning so far.

ssh2 -- I didn't use the ports because I'm not used to them yet. It was easier for me just to compile everything. I was Ubuntu as my primary OS so there is a little bit of learning for me to do. Those are good ideas that I might look into in the future.
 
kr651129 said:
vand777 -- It will be from scratch, I've been working on it for the past couple months now. I've got a basic boot up with some standard functions, nothing fancy yet. Most of my work has been research and planning so far.

Thanks! And good luck!
 
ssh2 said:
kr651129 why not using ports for install this packages?

And may be you can write howto crosscompile binaries for windows on freebsd FreeBSD, for example x264, ffmpeg and other useful utilities?

The 'mingw' port is what you're looking for.
 
kr651129 said:
Most of this information was pieced together from http://wiki.osdev.org/GCC_Cross-Compiler but made to fit for FreeBSD.

It would actually be very helpful if you could point out what exactly it was that had to be "made to fit for FreeBSD", i.e. why the original tutorial didn't work for you. Because, that way, we (speaking for the people at osdev.org) could adapt our tutorial so a seperate "FreeBSD version" is not necessary.
 
gcc that I know of is found on GNU Linux (gnu.org) or free software foundation (FSF) (Savannah). The steps can get recursive if you're building for an all new OS / platform it depends :) The documentation for gcc fully describes how many times you may need to pre-re-compile gcc.

Both have copies of the latest GNU compilers (also fortran, binutils) and instructions how to build the set for _any unix / platform (this part you may need to hack in or start with a ./configure your OS distro has pre-hacked - only a few are provided pre-done).

However its instructions are more complicated than ./configure if you wish to build for another platform (see the instructions for compiling gcc).
 
It's a FreeBSD port, no need for all the voodoo.
Code:
[port]lang/gcc[/port]                  GNU Compiler Collection 4.6
[port]lang/gcc34[/port]                GNU Compiler Collection 3.4
[port]lang/gcc42[/port]                GNU Compiler Collection 4.2
[port]lang/gcc44[/port]                GNU Compiler Collection 4.4
[port]lang/gcc46[/port]                GNU Compiler Collection 4.6
[port]lang/gcc47[/port]                GNU Compiler Collection 4.7
[port]lang/gcc48[/port]                GNU Compiler Collection 4.8
 
Hi everyone,

I realize this thread is old, but I just came across it looking for help on this same issue i.e., Building a cross compiler in FreeBSD.
I too had some difficulty following the OSDev tutorial on building the cross-compiler in FreeBSD. I am fairly new to FreeBSD so I am sure this had a lot to do with it, however, the major issue I had was that binutils would not compile. To get around this, I hacked the Makefile and removed any references to building and or installing the documentation. Specifically info-recursive targets and related. However, seeing the last post in this thread, this may have been avoided if I had used gmake when building binutils instead of make. I also had to use gmake to build/compile gcc, which was missing from the OSDev tutorial.
One further issue I also had and am still confused about is the target triplet. Where can one find the possible options for the target triplet? I am specifically trying to build for a mips-freebsd target (which did not work). I eventually played around until mips-elf was accepted, but I am unsure whether the resulting executable will actually execute on the target as of yet.
I am also curious how the original poster's OS development is progressing? Any update to give or a separate thread detailing your work?

Thanks
 
Is this mailing list post below of use to you? There have been efforts toward integrating an external build toolchain. MIPS is not part of what is mentioned here yet but the work is in progress. It may be worthwhile to look through the mailing list archives for further discussion on the efforts and see if you can make use of any of the information.

https://lists.freebsd.org/pipermail/freebsd-current/2014-November/053577.html

Unfortunately, I was not able to get any help from that link, but thanks anyways.

I was successful in building a 'naked' cross-compiler by pretty much following the OP's instructions, however, I had to define some environment variables like CC, CPP and CXX as per the Mac OS X instructions on the OSDev tutorial. I also confirmed that everything should be built using gmake and not FreeBSD's make.
Unfortunately, the naked cross-compiler is kind of useless since I was not successful in building libgcc for mips. During the build an error was encountered and I could not figure out how to solve the problem.
One method of building a fully functional cross-compiler I found was to use the source tree using 'make kernel-toolchain'. This method definitely works, but then now I'm stuck with the system compiler which I believe is gcc 4.2.1. If anyone can explain how to build gcc 4.9.2 from source targeting a mips platform I would be extremely grateful.
 
this is from Linux From Scratch GNU/Linux x-lfs-2010 which i wrote myself

(it's a 100k script that can compile and install a whole distro non-stop, a custom LFS from a few squeeze .deb, if there are no broken URLs :))

this is is the dependancy file for compiling the 2nd chroot (after squeeze compiles the first it is discarded, the mini LFS then compiles and is overwritten by the fresh compiles, and whala, a bootable wholely compiles 'nix. but don't do it, it's for 2010 :) it is just a script that dl's .tar.gz, runs cc or ./configure or pmake, right down the list, until done. the only key: complete list of depends, several work-arounds for environmental differences, work-arounds when "non-default" compile options are opted for, that the single script handles on a per case basis.

MY POINT here is to show a FULLER dependency list for those attempting OP's TIPS (original poster) in freeBSD who may run into depends/version mismatch and need a fuller list - which GNU/Linux does not support like BSD World does. and that VERSIONS are important to the dependency. compiling options effect dependency: if you compile in pam support in gcc (usu. that's for libc, but lets play), then you'd have to have pam libs below too.

autotools-binutils.list
build up to and incl. gcc-4.4.5 and binutils (gcc uses binutils)
(gcc itself may take serveral minutes to build)
glibc-coreutils.list (autotools-binutils.list)
* after g-c, arg dochroot can make chroot2, the rest is built using chroot2
xorg-server.list (autotools-binutils.list)
build after coreutils up to and incl. Xorg
* builds in chroot2 (run by new glibc-2.11, gcc4, etc)
xfree86.list
use this instead of xorg-xserver.list for XFree86-4.8.0
before-x.list (glibc-coreutils.list)
libjpeg , or anythign in glib-pango one
after-x.list (xserver.list)
after Xorg, build stuff X11 needs (fb, xterm, many)
after-xfree86.list
use this instead of after-x.listfor XFree86-4.8.0
glib-pango.list
after all else, build glib+pango (they run under X11)
FIXME: add gtk to this ?? for firefox5 ??
xserver-only.list after-x.list [glib-pango.list]
try to build only Xorg using gcc/base already installed
note make sure your gtk is new enough, because it will want
gcc-4.4.5 to build if not
default: autotools-binutils.list preX11-coreutils.list
note: glibc (gnu libc) glib (gnome convenices over libc), gd (graphics)
for moi: firefox < gtk < glibc + gd + pango < X11 < libpng < zlib < libc


----------------------------------------------
### autotools-binutils.list - ending at binutils since that's linux specific

# do later if possible perl will pick up dependancy on old libs and configs
# we might not keep around. if you have no perl do now, again l8tr after libc
# really its needed before libc by X11 things if trying to build gd before libc
# perl-5.10.1

# optional starters , nls some want both first and last
# (all depends are circular, but nls is done like 3x though per run)

termcap-1.3.2
termutils-2.0
texinfo-4.13

# note change build.early.list and fix in glib-pango.list if you use 3.80
# make-3.80 works up until firefox-5.0 so use it until then
make-3.80

# ? flex wants GLIBC_2.7 if compiled with gcc4, so use old till later
# note m4 needs flex file that goes with flex in use
m4-1.4.13
bison-2.4.1
flex-2.5.35
# and if m4 depens on flex, be careful early on , synchronize them now
m4-1.4.13
flex-2.5.35
bison-2.4.1

# these hardcode env they are built in, recompile after gcc and glibc too
autoconf-2.68
automake-1.11
libtool-2.2.10

gmp-6.0.0
mpfr-3.1.2
mpc-1.0.2

# i think gcc says use sys if avail (HPUX) else build gcc's in
# ? was old attempt to fix __sync_fetch_and_add_4 not defined ?? (def in gcc4)
#libatomic-ops-7.4.2 # libunwind no longer uses
# suggest not using ipv6 enabled "debug tracing" as default, unix has unwind
# libunwind-1.0.1

# here maybe or later during X11 build after xorg,xserver,encodings-1.0.4
# gperf-3.0.4

gcc-4.4.5

# these hardcode env they are built in, recompile after gcc and glibc too
# NOTE: usually recompiling isnt necessary and is for a different purpose
autoconf-2.68
automake-1.11
libtool-2.2.10

# compile gcc again because path names will change and are hardcoded in gcc
# cpu support has changed and may alter avail. options and paths as well
# (we kinda wanted it a gcc built with up-to-date gcc anyway)
# NOTE: usually recompiling isnt necessary and is for a different purpose

gcc-4.4.5

# keep together same install dir, though 1.1.4 may not be needed today
zlib-1.1.4
zlib-1.1.4
zlib-1.2.7
 
above you saw repeats. that's no mistake. when you compile a next program you may very well need the holes that are filled by a next one compiled to fill holes in what you already compiled that need to be filled in the future :) (due to bad design: please avoid that if your making software, but GNU/Linux is racked full of it)

(in a few cases a 2nd build/install is to reverts file clobbering damage from later installs rather than to pick up holes, gnu softwares sometimes like to damage other wares so they stop working, very obstructive)

also: the above uses "default int'l support" in gcc and libc (libc not listed), assumes that's "good enough language" until after X is compiled. with intl'n support watch out (for linux anyway), it will spin your head, it's almost as if they dont' want anyone following their "lead" (which is full of USA software and others contributions! wow.)
 
NOTE: you may be able to build with gcc, but if you want to build "your own operating system" you need a LIBC (you plan to have a text terminal, and printf() right?). if you used linux libc you'd need a whole list of depends to get there. (LFS brand lfs would be suggested, the the above lists obviously show that's in the 2nd text list of things / order to build)

i suggest that make World, freeBSD, is the best free build system out there, it's a thing that doesn't need fixing. . if you want a custom LIBC only your own dev apps will be able to use them (noting there are a few alternate libc out there to link C apps against to find and xlfs builds one i believe)

but GCC is always a cross-compiler so is CLANG: developers only need to use them.

ALSO: only in strange cases does and end developer need to do so something like create a 64bit compiler using a 32bit one (or ARM from an intel). if your not doing that, then you don't need to "build a cross compiler" because you can just download the package.
 
excuse me. Apple dev (xcode) has a BSD-like userland and BSD like build system. i haven't tried it from scratch. but i believe it has a kind of "World build". i can't speak as to if that's better: but its' free. (some apple drivers for their pc's are non-free)

anywyay, for freeware - freeBSD is already your newOS under your total control that can cross-compile, if you want my opinion: you already got it!
 
Back
Top