|
| 1 | +*Concepts you may want to Google beforehand: cross-compiler* |
| 2 | + |
| 3 | +**Goal: Create a development environment to build your kernel** |
| 4 | + |
| 5 | +If you're using a Mac, you will need to do this process right away. Otherwise, it could have waited |
| 6 | +for a few more lessons. Anyway, you will need a cross-compiler one we jump to developing in a higher |
| 7 | +language, that is, C. [Read why](http://wiki.osdev.org/Why_do_I_need_a_Cross_Compiler%3F) |
| 8 | + |
| 9 | +I'll be adapting the instructions [at the OSDev wiki](http://wiki.osdev.org/GCC_Cross-Compiler). |
| 10 | + |
| 11 | + |
| 12 | +Required packages |
| 13 | +----------------- |
| 14 | + |
| 15 | +First, install the required packages. On linux, use your package distribution. On a Mac, [install brew](http://brew.sh/) if |
| 16 | +you didn't do it on lesson 00, and get those packages with `brew install` |
| 17 | + |
| 18 | +- gmp |
| 19 | +- mpfr |
| 20 | +- libmpc |
| 21 | +- gcc |
| 22 | + |
| 23 | +Yes, we will need `gcc` to build our cross-compiled `gcc`, especially on a Mac where gcc has been deprecated for `clang` |
| 24 | + |
| 25 | +Once installed, find where your packaged gcc is (remember, not clang) and export it. For example: |
| 26 | + |
| 27 | +``` |
| 28 | +export CC=/usr/local/bin/gcc-4.9 |
| 29 | +export LD=/usr/local/bin/gcc-4.9 |
| 30 | +``` |
| 31 | + |
| 32 | +We will need to build binutils and a cross-compiled gcc, and we will put them into `/usr/local/i386elfgcc`, so |
| 33 | +let's export some paths now. Feel free to change them to your liking. |
| 34 | + |
| 35 | +``` |
| 36 | +export PREFIX="/usr/local/i386elfgcc" |
| 37 | +export TARGET=i386-elf |
| 38 | +export PATH="$PREFIX/bin:$PATH" |
| 39 | +``` |
| 40 | + |
| 41 | +binutils |
| 42 | +-------- |
| 43 | + |
| 44 | +Rember: always be careful before pasting walls of text from the internet. I recommend copying line by line. |
| 45 | + |
| 46 | +```sh |
| 47 | +mkdir /tmp/src |
| 48 | +cd /tmp/src |
| 49 | +curl -O http://ftp.gnu.org/gnu/binutils/binutils-2.24.tar.gz # If the link 404's, look for a more recent version |
| 50 | +tar xf binutils-2.24.tar.gz |
| 51 | +mkdir binutils-build |
| 52 | +cd binutils-build |
| 53 | +../binutils-2.24/configure --target=$TARGET --enable-interwork --enable-multilib --disable-nls --disable-werror --prefix=$PREFIX 2>&1 | tee configure.log |
| 54 | +make all install 2>&1 | tee make.log |
| 55 | +``` |
| 56 | + |
| 57 | +gcc |
| 58 | +--- |
| 59 | +```sh |
| 60 | +cd /tmp/src |
| 61 | +curl -O http://mirror.bbln.org/gcc/releases/gcc-4.9.1/gcc-4.9.1.tar.bz2 |
| 62 | +tar xf gcc-4.9.1.tar.bz2 |
| 63 | +mkdir gcc-build |
| 64 | +cd gcc-build |
| 65 | +../gcc-4.9.1/configure --target=$TARGET --prefix="$PREFIX" --disable-nls --disable-libssp --enable-languages=c --without-headers |
| 66 | +make all-gcc |
| 67 | +make all-target-libgcc |
| 68 | +make install-gcc |
| 69 | +make install-target-libgcc |
| 70 | +``` |
| 71 | + |
| 72 | +That's it! You should have all the GNU binutils and the compiler at `/usr/local/i386elfgcc/bin`, prefixed by `i386-elf-` to avoid |
| 73 | +collisions with your system's compiler and binutils. |
| 74 | + |
| 75 | +You may want to add the `$PATH` to your `.bashrc`. From now on, on this tutorial, we will explicitly use the prefixes when using |
| 76 | +the cross-compiled gcc. |
0 commit comments