Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| mips_cross_compiler [2013/04/03 11:27] – tracing beckmanf | mips_cross_compiler [2014/06/04 17:52] (current) – simple project added beckmanf | ||
|---|---|---|---|
| Line 13: | Line 13: | ||
| * [[http:// | * [[http:// | ||
| * [[http:// | * [[http:// | ||
| + | * and others... | ||
| - | === Compile and Run === | + | From a bare ubuntu 12.04 LTS you need to add the following packages: |
| + | |||
| + | < | ||
| + | sudo apt-get install build-essential m4 texinfo libncurses5-dev bzip2 | ||
| + | </ | ||
| + | |||
| + | |||
| + | Copy the script " | ||
| + | The click terminal which opens the terminal. To change the current directory to your home directory type | ||
| + | |||
| + | < | ||
| + | cd | ||
| + | </ | ||
| + | |||
| + | To see the full path of the current directory: | ||
| + | |||
| + | < | ||
| + | pwd | ||
| + | </ | ||
| + | |||
| + | List the directory contents with | ||
| + | |||
| + | < | ||
| + | ls -la | ||
| + | </ | ||
| + | |||
| + | Change the file attributes of " | ||
| + | |||
| + | < | ||
| + | chmod a+x build_mips.sh | ||
| + | </ | ||
| + | |||
| + | Look into the bash script with | ||
| + | |||
| + | < | ||
| + | less build_mips.sh | ||
| + | </ | ||
| + | |||
| + | Extend the search path by appending " | ||
| + | |||
| + | Log out and login again to make the PATH variable active. Check your PATH variable with | ||
| + | |||
| + | < | ||
| + | echo $PATH | ||
| + | </ | ||
| + | |||
| + | You should see the < | ||
| + | |||
| + | Run the build script with | ||
| + | |||
| + | < | ||
| + | ./ | ||
| + | </ | ||
| + | |||
| + | |||
| + | Here is the shell script for building the cross compiler chain. Handmade | ||
| + | stuff so you need to adapt. The script will build to a place in your home | ||
| + | directory. So first add this place to your PATH variable. | ||
| + | |||
| + | <code bash build_mips.sh> | ||
| + | #!/bin/bash -ev | ||
| + | # -e => exit on error | ||
| + | # -v => verbose output | ||
| + | |||
| + | # Mips Cross Compiler | ||
| + | |||
| + | # Base directory | ||
| + | mkdir -p mips | ||
| + | # src directory | ||
| + | mkdir -p mips/src | ||
| + | # build directory | ||
| + | mkdir -p mips/ | ||
| + | # original archives | ||
| + | mkdir -p mips/orig | ||
| + | |||
| + | # Set the destination | ||
| + | export MYMIPS=${HOME}/ | ||
| + | |||
| + | # Versions | ||
| + | GMPVERSION=" | ||
| + | PPLVERSION=" | ||
| + | BINUTILSVERSION=" | ||
| + | MPFRVERSION=" | ||
| + | MPCVERSION=" | ||
| + | ISLVERSION=" | ||
| + | CLOOGVERSION=" | ||
| + | GCCVERSION=" | ||
| + | NEWLIBVERSION=" | ||
| + | GDBVERSION=" | ||
| + | |||
| + | |||
| + | ######################################## | ||
| + | # Binutils | ||
| + | ######################################## | ||
| + | |||
| + | # Get the archives | ||
| + | cd mips/orig | ||
| + | if [ ! -e " | ||
| + | wget http:// | ||
| + | fi | ||
| + | |||
| + | # Unpack to source directory | ||
| + | cd ../src | ||
| + | if [ ! -d " | ||
| + | tar -xvjf ../ | ||
| + | fi | ||
| + | |||
| + | cd ../build | ||
| + | mkdir -p binutils | ||
| + | cd binutils | ||
| + | if [ ! -e " | ||
| + | ../ | ||
| + | --prefix=$MYMIPS | ||
| + | fi | ||
| + | |||
| + | if [ ! -e " | ||
| + | make | ||
| + | make install | ||
| + | fi | ||
| + | cd ../../.. | ||
| + | |||
| + | ######################################## | ||
| + | # GMP | ||
| + | ######################################## | ||
| + | |||
| + | # Get the archives | ||
| + | cd mips/orig | ||
| + | if [ ! -e " | ||
| + | wget ftp:// | ||
| + | fi | ||
| + | |||
| + | # Unpack to source directory | ||
| + | cd ../src | ||
| + | if [ ! -d " | ||
| + | tar -xjvf ../ | ||
| + | fi | ||
| + | |||
| + | # Build | ||
| + | cd ../build | ||
| + | mkdir -p gmp | ||
| + | cd gmp | ||
| + | if [ ! -e " | ||
| + | ../ | ||
| + | fi | ||
| + | if [ ! -e " | ||
| + | make | ||
| + | make install | ||
| + | fi | ||
| + | cd ../../.. | ||
| + | |||
| + | |||
| + | ######################################## | ||
| + | # PPL | ||
| + | ######################################## | ||
| + | |||
| + | # Get the archives | ||
| + | #cd mips/orig | ||
| + | #if [ ! -e " | ||
| + | # wget ftp:// | ||
| + | #fi | ||
| + | |||
| + | # Unpack to source directory | ||
| + | #cd ../src | ||
| + | #if [ ! -d " | ||
| + | #tar -xjvf ../ | ||
| + | #fi | ||
| + | |||
| + | #cd ../build | ||
| + | #mkdir -p ppl | ||
| + | #cd ppl | ||
| + | #if [ ! -e " | ||
| + | # | ||
| + | #fi | ||
| + | #if [ ! -e " | ||
| + | #make | ||
| + | #make install | ||
| + | #fi | ||
| + | #cd ../../.. | ||
| + | |||
| + | |||
| + | ###################################### | ||
| + | # MPFR library | ||
| + | ####################################### | ||
| + | |||
| + | # Get the archives | ||
| + | cd mips/orig | ||
| + | if [ ! -e " | ||
| + | wget ftp:// | ||
| + | fi | ||
| + | |||
| + | # Unpack to source directory | ||
| + | cd ../src | ||
| + | if [ ! -d " | ||
| + | tar -xvjf ../ | ||
| + | fi | ||
| + | |||
| + | cd ../build | ||
| + | mkdir -p mpfr | ||
| + | cd mpfr | ||
| + | if [ ! -e " | ||
| + | ../ | ||
| + | fi | ||
| + | |||
| + | if [ ! -e " | ||
| + | make | ||
| + | make install | ||
| + | fi | ||
| + | cd ../../.. | ||
| + | |||
| + | ###################################### | ||
| + | # MPC library | ||
| + | ####################################### | ||
| + | |||
| + | # Get the archives | ||
| + | cd mips/orig | ||
| + | if [ ! -e " | ||
| + | wget ftp:// | ||
| + | fi | ||
| + | |||
| + | # Unpack to source directory | ||
| + | cd ../src | ||
| + | if [ ! -d " | ||
| + | tar -xvzf ../ | ||
| + | fi | ||
| + | |||
| + | cd ../build | ||
| + | mkdir -p mpc | ||
| + | cd mpc | ||
| + | if [ ! -e " | ||
| + | ../ | ||
| + | fi | ||
| + | |||
| + | if [ ! -e " | ||
| + | make | ||
| + | make install | ||
| + | fi | ||
| + | cd ../../.. | ||
| + | |||
| + | |||
| + | |||
| + | ############## | ||
| + | # ISL | ||
| + | ############## | ||
| + | |||
| + | # Get the archives | ||
| + | cd mips/orig | ||
| + | if [ ! -e " | ||
| + | wget ftp:// | ||
| + | fi | ||
| + | |||
| + | |||
| + | # Unpack to source directory | ||
| + | cd ../src | ||
| + | if [ ! -d " | ||
| + | tar -xvjf ../ | ||
| + | fi | ||
| + | |||
| + | cd ../build | ||
| + | mkdir -p isl | ||
| + | cd isl | ||
| + | if [ ! -e " | ||
| + | ../ | ||
| + | fi | ||
| + | |||
| + | if [ ! -e " | ||
| + | make | ||
| + | make install | ||
| + | fi | ||
| + | cd ../../.. | ||
| + | |||
| + | ############## | ||
| + | # CLOOG | ||
| + | ############## | ||
| + | |||
| + | # Get the archives | ||
| + | cd mips/orig | ||
| + | if [ ! -e " | ||
| + | wget ftp:// | ||
| + | fi | ||
| + | |||
| + | |||
| + | # Unpack to source directory | ||
| + | cd ../src | ||
| + | if [ ! -d " | ||
| + | tar -xvzf ../ | ||
| + | fi | ||
| + | |||
| + | cd ../build | ||
| + | mkdir -p cloog | ||
| + | cd cloog | ||
| + | if [ ! -e " | ||
| + | ../ | ||
| + | fi | ||
| + | |||
| + | if [ ! -e " | ||
| + | make | ||
| + | make install | ||
| + | fi | ||
| + | cd ../../.. | ||
| + | |||
| + | |||
| + | ######################################## | ||
| + | # newlib | ||
| + | ######################################## | ||
| + | |||
| + | # Get the archives | ||
| + | cd mips/orig | ||
| + | |||
| + | if [ ! -e " | ||
| + | wget ftp:// | ||
| + | fi | ||
| + | |||
| + | # Unpack to source directory | ||
| + | cd ../src | ||
| + | if [ ! -d " | ||
| + | tar -xvzf ../ | ||
| + | fi | ||
| + | |||
| + | cd ../.. | ||
| + | |||
| + | ######################################## | ||
| + | # gcc first stage | ||
| + | ######################################## | ||
| + | |||
| + | # Get the archives | ||
| + | cd mips/orig | ||
| + | if [ ! -e " | ||
| + | wget ftp:// | ||
| + | fi | ||
| + | |||
| + | # Unpack to source directory | ||
| + | cd ../src | ||
| + | if [ ! -d " | ||
| + | tar -xvjf ../ | ||
| + | fi | ||
| + | |||
| + | cd ../build | ||
| + | mkdir -p gcc-stage1 | ||
| + | cd gcc-stage1 | ||
| + | if [ ! -e " | ||
| + | LDFLAGS=" | ||
| + | ../ | ||
| + | --prefix=$MYMIPS \ | ||
| + | --with-gmp=$MYMIPS \ | ||
| + | --with-mpfr=$MYMIPS \ | ||
| + | --with-mpc=$MYMIPS \ | ||
| + | --with-isl=$MYMIPS \ | ||
| + | --with-newlib --without-headers \ | ||
| + | --disable-shared --disable-threads --disable-libssp \ | ||
| + | --disable-libgomp --disable-libmudflap \ | ||
| + | --enable-languages=" | ||
| + | fi | ||
| + | |||
| + | if [ ! -e " | ||
| + | make all-gcc | ||
| + | make install-gcc | ||
| + | fi | ||
| + | cd ../../.. | ||
| + | |||
| + | ######################################## | ||
| + | # newlib | ||
| + | ######################################## | ||
| + | |||
| + | # Build | ||
| + | cd mips/ | ||
| + | mkdir -p newlib | ||
| + | cd newlib | ||
| + | if [ ! -e " | ||
| + | ../ | ||
| + | fi | ||
| + | |||
| + | if [ ! -e " | ||
| + | make | ||
| + | make install | ||
| + | fi | ||
| + | cd ../../.. | ||
| + | |||
| + | ######################################## | ||
| + | # gcc second stage | ||
| + | ######################################## | ||
| + | |||
| + | cd mips/ | ||
| + | mkdir -p gcc-stage2 | ||
| + | cd gcc-stage2 | ||
| + | if [ ! -e " | ||
| + | LDFLAGS=" | ||
| + | ../ | ||
| + | --prefix=$MYMIPS \ | ||
| + | --with-gmp=$MYMIPS \ | ||
| + | --with-mpfr=$MYMIPS \ | ||
| + | --with-mpc=$MYMIPS \ | ||
| + | --with-isl=$MYMIPS \ | ||
| + | --with-newlib | ||
| + | --disable-shared --disable-threads --disable-libssp \ | ||
| + | --disable-libgomp --disable-libmudflap \ | ||
| + | --enable-languages=" | ||
| + | fi | ||
| + | |||
| + | if [ ! -e " | ||
| + | make all | ||
| + | make install | ||
| + | fi | ||
| + | cd ../../.. | ||
| + | |||
| + | |||
| + | ######################################## | ||
| + | # GDB | ||
| + | ######################################## | ||
| + | |||
| + | # Get the archives | ||
| + | cd mips/orig | ||
| + | if [ ! -e " | ||
| + | wget ftp:// | ||
| + | fi | ||
| + | |||
| + | # Unpack to source directory | ||
| + | cd ../src | ||
| + | if [ ! -d " | ||
| + | tar -xvjf ../ | ||
| + | fi | ||
| + | |||
| + | cd ../build | ||
| + | mkdir -p gdb | ||
| + | cd gdb | ||
| + | if [ ! -e " | ||
| + | ../ | ||
| + | --enable-sim-trace \ | ||
| + | --enable-sim-stdio \ | ||
| + | --prefix=$MYMIPS | ||
| + | fi | ||
| + | |||
| + | if [ ! -e " | ||
| + | make | ||
| + | make install | ||
| + | fi | ||
| + | cd ../../.. | ||
| + | |||
| + | </ | ||
| + | |||
| + | ===== Compile and Run an example ===== | ||
| + | |||
| + | Here is an example mini c code to test the compiler. | ||
| + | |||
| + | <code c hello.c> | ||
| + | /* Hello world */ | ||
| + | #include < | ||
| + | |||
| + | int myfunc(int a, int b){ | ||
| + | int k; | ||
| + | k = a + b; | ||
| + | |||
| + | k += 7; | ||
| + | k *= 6; | ||
| + | return k; | ||
| + | } | ||
| + | |||
| + | int main(){ | ||
| + | int i, | ||
| + | j = 3; | ||
| + | m = 2; | ||
| + | |||
| + | for(i=0; | ||
| + | m += myfunc(i, | ||
| + | printf(" | ||
| + | } | ||
| + | return 0; | ||
| + | } | ||
| + | </ | ||
| Compile to Assembler for viewing | Compile to Assembler for viewing | ||
| - | mipsel-none-elf-gcc -S fir.c | + | < |
| + | mipsel-none-elf-gcc -S hello.c | ||
| + | </ | ||
| - | The output is fir.s which is the assembler code. | + | The output |
| Compile and link ready for simulation with instruction set simulator | Compile and link ready for simulation with instruction set simulator | ||
| - | mipsel-none-elf-gcc -o fir -Tidt.ld | + | < |
| + | mipsel-none-elf-gcc -o hello -Tidt.ld | ||
| + | </ | ||
| Run the code | Run the code | ||
| - | mipsel-none-elf-run | + | < |
| + | mipsel-none-elf-run | ||
| + | </ | ||
| === Analyze === | === Analyze === | ||
| - | Produce an annoted source code | + | Produce an annoted source code showing how often lines are executed. |
| - | gcc -fprofile-arcs -ftest-coverage -o fir fir.c | + | < |
| + | gcc -fprofile-arcs -ftest-coverage -o hello hello.c | ||
| + | ./hello | ||
| + | gcov hello.c | ||
| + | </ | ||
| - | ./fir | + | This produces the file hello.c.gcov showing the annotated source file. |
| - | gcov fir.c | + | === Tracing in instruction set simulator === |
| Run with instruction tracing in the simulator | Run with instruction tracing in the simulator | ||
| - | mipsel-none-elf-run --trace-insn=on --trace-file trace fir | + | < |
| + | mipsel-none-elf-gcc -Tidt.ld -o hello hello.c | ||
| + | mipsel-none-elf-run --trace-insn=on --trace-file trace hello | ||
| + | </ | ||
| + | |||
| + | ===== Test the compiler ===== | ||
| + | |||
| + | < | ||
| + | sudo apt-get install dejagnu | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | cd | ||
| + | cd mips/ | ||
| + | make check-gcc RUNTESTFLAGS=--target_board=mips-sim | ||
| + | </ | ||
| + | |||
| + | ===== Install git and download a simple project | ||
| + | |||
| + | Install git and download a simple project | ||
| + | |||
| + | < | ||
| + | sudo apt-get install git | ||
| + | cd | ||
| + | mkdir projects | ||
| + | cd projects | ||
| + | git clone https:// | ||
| + | </ | ||
| + | |||
| + | Now you have the simple project " | ||
| + | |||
| + | === Try the MIPS Cross Compiler === | ||
| + | |||
| + | Change to the src directory and compile the code with the cross compiler. | ||
| + | |||
| + | < | ||
| + | cd myfirst | ||
| + | cd src | ||
| + | mipsel-none-elf-gcc -S hello.c | ||
| + | less hello.s | ||
| + | </ | ||
| + | |||
| + | Now you have the compiled assembler code " | ||
| + | |||
| + | < | ||
| + | mipsel-none-elf-gcc -o hello -Tidt.ld hello.c | ||
| + | </ | ||
| + | |||
| + | Now you have the binary " | ||
| + | |||
| + | < | ||
| + | mipsel-none-elf-run hello | ||
| + | </ | ||
| + | |||
| + | This will run the binary with the mips instruction set simulator. You should see "Hello World" | ||
| + | |||
| + | |||
| + | |||
| + | ===== Open OCD ===== | ||
| + | |||
| + | [[dt_openocd]] | ||