Skip to content

Cross Compiler

Guillaume PERCEPIED edited this page May 15, 2024 · 17 revisions

Cross Compiler

To compile OliOS (and any OS), we need to have a cross compilers (a compiler able to compile our OS from our host OS without using any dependencies from the host OS. That's why we need to setup a "standalone" compiling toolchain. The cross compiler toolchain needs those three tools: Binutils, Binutils GDB and GCC

All the information that you will find on this wiki page comes from the osdev wiki, specifically this page. This wiki is a short sum up of the steps I did for OliOS, you will find a lot more information on this wiki.

All those steps are for a Linux host, Fedora in my case

First you need to install required dependencies:

sudo dnf install gcc gcc-c++ make bison flex gmp-devel libmpc-devel mpfr-devel texinfo isl-devel

Then, you need to prepare your folders. Don't try to install your cross compiler in /usr/bin, this is not meant to replace your local compiler at all. It will not be compatible as we will target a different platform (yours!)

For example, you can create an "opt" folder in your home, with a cross directory (where your cross compiler bin will be) and a src directory where you will clone the binutils, binutils gdb and gcc sources:

mkdir ~/opt

mkdir ~/opt/cross

mkdir ~/opt/src

Then you can clone the different sources:

cd ~/opt/src

git clone git://sourceware.org/git/binutils

git clone git://sourceware.org/git/binutils-gdb.git

git clone git://gcc.gnu.org/git/gcc.git

Before compiling all those nice stuff, prepare some variables to ease the process (update accordingly to your preferences). This will install everything in your ~/opt/cross with the i686-elf target (If you don't know what it is, stop there and read more on the osdev wiki):

export PREFIX="$HOME/opt/cross"

export TARGET=i686-elf

export PATH="$PREFIX/bin:$PATH"

Binutils

To compile binutils from sources do those steps, and verify that each step are done sucessfully and not stopped by an error:

cd ~/opt/src

mkdir build-binutils && cd build-binutils

../binutils/configure --target=$TARGET --prefix="$PREFIX" --with-sysroot --disable-nls --disable-werror

make

make install

Binutils GDB

To compile binutils GDB from sources do those steps, and verify that each step are done sucessfully and not stopped by an error:

../gdb/configure --target=$TARGET --prefix="$PREFIX" --disable-werror

make all-gdb

make install-gdb

GCC

Check first that your new bin directory is NOT in your $PATH variable, if your path appears in it it's not good, remove it before continuing:

echo $PATH | grep "opt/cross"

cd ~/opt/src

mkdir build-gcc && cd build-gcc

../gcc/configure --target=$TARGET --prefix="$PREFIX" --disable-nls --enable-languages=c,c++ --without-headers

make all-gcc

make all-target-libgcc

make install-gcc

make install-target-libgcc

Check that everything compiled correctly

~/opt/cross/bin/$TARGET-gcc --version

This should give you a nice message:

i686-elf-gcc (GCC) 15.0.0 20240513 (experimental)
Copyright (C) 2024 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Ease of use

You could add this to your .bashrc and call your new cross compiler with $TARGET-gcc:

export PATH="$HOME/opt/cross/bin:$PATH"

But I prefer a simple alias in .bashrc:

alias oli_i686_gcc="~/opt/cross/bin/i686-elf-gcc"
alias oli_i686_gpp="~/opt/cross/bin/i686-elf-g++"
alias oli_i686_gdb="~/opt/cross/bin/i686-elf-gdb"

So when I type oli_ and then tab/tab it I have all my commands:

dummy@dummy:~/opt/src/build-gcc$ oli_i686_g

oli_i686_gcc oli_i686_gdb oli_i686_gpp

dummy@dummy:~/opt/src/build-gcc$ oli_i686_gcc --version

i686-elf-gcc (GCC) 15.0.0 20240513 (experimental)
Copyright (C) 2024 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Clone this wiki locally