Difference between revisions of "Bare Metal ARM Cortex-M GDC Cross Compiler"

From D Wiki
Jump to: navigation, search
(Undo revision 3652 by Verax (talk))
 
(25 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{ParentArticle|[[GDC]] > [[GDC/Cross Compiler|Cross Compiler]]}}
+
== Introduction ==
 +
These instructions will describe how to build a bare metal ARM Cortex-M (arm-none-eabi) GDC cross compiler for a Linux host.  All scripts were tested on an Arch Linux 64 host.  If you encounter any problems while following these instructions, you may be able to get help at the [http://forum.dlang.org/group/D.gnu D.GNU] forum.  If you can improve these instructions, please do so by making an account on this wiki and authoring changes.
 +
 
 +
A C library will not be built as this toolchain is intended for building all software in D.  The D Runtime and Phobos (the D standard library) also will not be built as those libraries have not yet been ported to the ARM Cortex-M platform.  However, this toolchain should give one the tools needed to do so.
  
== Introduction ==
+
As an alternative to these instructions, you may be able to use the pre-built ARM binaries at [http://gdcproject.org/downloads http://gdcproject.org/downloads].  All ARM GCC compilers can generate code for all ARM variants (the hard-float compiler can generate soft-float code and the soft-float compiler can generate hard-float code as well) as long as the appropriate platform compiler flags are used. The difference between the compilers are the bundled libraries (e.g. the bundled libgcc may not be compatible with the thumb targets).
These instructions will describe how to build a bare metal ARM Cortex-M (arm-none-eabi) GDC cross compiler for a Linux host.  All commands were tested on an Arch Linux 64 host.  If you encounter any problems while following these instructions, you may be able to get help at the [http://forum.dlang.org/group/D.gnu D.GNU] forumIf you can improve these instructions, please do so by making an account on this wiki and authoring changes.
 
  
A C library will not be built as this toolchain is intended for building all software in D. The D Runtime and Phobos (the D standard library) also will not be built as these libraries have not yet been ported to the ARM Cortex-M platform.  However, this toolchain should give one the tools needed to do so.
+
These instructions will use GCC 4.9.2 and the corresponding GDC branch from the [https://github.com/D-Programming-GDC/GDC GDC source code repository].
  
For now, these instructions will use GCC 4.8.2 and the corresponding GDC branch from the [https://github.com/D-Programming-GDC/GDC GDC source code repository].
+
'''WARNING''': The scripts in these instructions will create and remove several intermediate folders, so it is best if they are executed from within an empty folder to prevent removing or overwriting existing files and folders.
  
 
== Specifying a target and installation folder ==
 
== Specifying a target and installation folder ==
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
export TARGET=arm-none-eabi
 
export TARGET=arm-none-eabi
export PREFIX={installation folder}
+
export PREFIX={installation directory}
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Replace <code>{installation folder}</code> with the absolute path to the folder the toolchain should be installed in.
+
Replace <code>{installation directory}</code> with the absolute path to the folder the toolchain should be installed in.
  
 
== GNU Binutils ==
 
== GNU Binutils ==
 +
 +
For information on what GNU Binutils is and what features of the toolchain it provides see their website [http://www.gnu.org/software/binutils/ here].
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
# Delete existing binutils source archive and download a new one
 
# Delete existing binutils source archive and download a new one
 
#-------------------------------------------------------------------
 
#-------------------------------------------------------------------
export BINUTILS_NAME=binutils-2.24
+
export BINUTILS_NAME=binutils-2.25
 
export BINUTILS_SOURCE_ARCHIVE=$BINUTILS_NAME.tar.bz2
 
export BINUTILS_SOURCE_ARCHIVE=$BINUTILS_NAME.tar.bz2
 +
rm -f $BINUTILS_SOURCE_ARCHIVE
 +
rm -rf $BINUTILS_NAME
 
wget http://ftpmirror.gnu.org/binutils/$BINUTILS_SOURCE_ARCHIVE
 
wget http://ftpmirror.gnu.org/binutils/$BINUTILS_SOURCE_ARCHIVE
  
Line 31: Line 37:
 
#-------------------------------------------------------------------
 
#-------------------------------------------------------------------
 
export BINUTILS_BUILD_DIR=binutils-build
 
export BINUTILS_BUILD_DIR=binutils-build
 +
rm -rf $BINUTILS_BUILD_DIR
 
mkdir $BINUTILS_BUILD_DIR
 
mkdir $BINUTILS_BUILD_DIR
  
Line 48: Line 55:
 
cd ..
 
cd ..
 
</syntaxhighlight>
 
</syntaxhighlight>
 
For information on what GNU Binutils is and what features of the toolchain it provides see their website [http://www.gnu.org/software/binutils/ here].
 
  
 
== Download GDC ==
 
== Download GDC ==
Line 59: Line 64:
 
git clone https://github.com/D-Programming-GDC/GDC.git gdc
 
git clone https://github.com/D-Programming-GDC/GDC.git gdc
 
cd gdc
 
cd gdc
git checkout gdc-4.8
+
git checkout gdc-4.9
 
cd ..
 
cd ..
 
</syntaxhighlight>
 
</syntaxhighlight>
Line 67: Line 72:
 
# Delete existing GCC source archive and download a new one
 
# Delete existing GCC source archive and download a new one
 
#-------------------------------------------------------------------
 
#-------------------------------------------------------------------
export GCC_NAME=gcc-4.8.2
+
export GCC_MIRROR=http://ftpmirror.gnu.org/gcc
 +
export GCC_NAME=gcc-4.9.2
 
export GCC_SOURCE_ARCHIVE=$GCC_NAME.tar.bz2
 
export GCC_SOURCE_ARCHIVE=$GCC_NAME.tar.bz2
 
rm -f $GCC_SOURCE_ARCHIVE
 
rm -f $GCC_SOURCE_ARCHIVE
 
rm -rf $GCC_NAME
 
rm -rf $GCC_NAME
wget http://ftp.tsukuba.wide.ad.jp/software/gcc/releases/$GCC_NAME/$GCC_SOURCE_ARCHIVE
+
wget $GCC_MIRROR/$GCC_NAME/$GCC_SOURCE_ARCHIVE
  
 
# Extract GCC
 
# Extract GCC
Line 77: Line 83:
 
tar xjfv $GCC_SOURCE_ARCHIVE
 
tar xjfv $GCC_SOURCE_ARCHIVE
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
You may wish to use an alternate [https://gcc.gnu.org/mirrors.html GCC mirror].
  
 
== Add GDC to GCC ==
 
== Add GDC to GCC ==
Line 118: Line 126:
 
   --with-gnu-as            \
 
   --with-gnu-as            \
 
   --with-gnu-ld            \
 
   --with-gnu-ld            \
 +
  --with-cpu={your_cpu}    \
 +
  --with-tune={your_cpu}  \
 
   --with-mode=thumb        \
 
   --with-mode=thumb        \
 
   --without-headers
 
   --without-headers
Line 123: Line 133:
 
make -j4 all-target-libgcc
 
make -j4 all-target-libgcc
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
Adjust <code>--with-cpu</code> and <code>--with-tune</code> with your CPU variant (e.g. cortex-m3, cortex-m4, etc..).
  
 
<code>--disable-libphobos</code> disables the D Runtime and Phobos (the D standard library) as these have not yet been ported to the ARM Cortex-M platform.
 
<code>--disable-libphobos</code> disables the D Runtime and Phobos (the D standard library) as these have not yet been ported to the ARM Cortex-M platform.
  
 
<code>make all-gcc</code> and <code>make all-target-libgcc</code> are used instead of <code>make</code> in order to limit the build to just the necessary components.
 
<code>make all-gcc</code> and <code>make all-target-libgcc</code> are used instead of <code>make</code> in order to limit the build to just the necessary components.
 +
 +
Documentation on the various configure options can be found in GCC's documentation [http://gcc.gnu.org/install/configure.html here]
  
 
== Install the toolchain ==
 
== Install the toolchain ==
Line 136: Line 150:
  
 
== Testing the toolchain ==
 
== Testing the toolchain ==
The toolchain can be tested with the minimal semihosted "Hello World!" program documented [[Extremely_minimal_semihosted_"Hello_World" | here]].
+
The toolchain can be tested with the minimal semihosted "Hello World!" program documented [[Minimal_semihosted_ARM_Cortex-M_"Hello_World" | here]].
 +
 
 +
To get a list of compiler options specific to the target of your cross-compiler, use the command <code>arm-none-eabi-gdc --target-help</code>
  
 
== Additional Information ==
 
== Additional Information ==
These instructions were modeled after the instructions and build scripts at [https://launchpad.net/gcc-arm-embedded GNU Tools for ARM Embedded Processors]. The [http://wiki.osdev.org/GCC_Cross-Compiler GCC cross compiler instructions] on OSDev.org were also quite helpful. Documentation on the various configure options can be found in GCC's documentation [http://gcc.gnu.org/install/configure.html here]
+
These instructions were modeled after the instructions and build scripts at [https://launchpad.net/gcc-arm-embedded GNU Tools for ARM Embedded Processors]. The [http://wiki.osdev.org/GCC_Cross-Compiler GCC cross compiler instructions] on OSDev.org were also quite helpful.
 +
 
 +
== See also ==
 +
* [[GDC]]
 +
* [[GDC/Cross Compiler|GDC Cross Compiler]]
 +
 
 +
[[Category:GDC Compiler]]
 +
[[Category:ARM]]

Latest revision as of 19:04, 8 September 2015

Introduction

These instructions will describe how to build a bare metal ARM Cortex-M (arm-none-eabi) GDC cross compiler for a Linux host. All scripts were tested on an Arch Linux 64 host. If you encounter any problems while following these instructions, you may be able to get help at the D.GNU forum. If you can improve these instructions, please do so by making an account on this wiki and authoring changes.

A C library will not be built as this toolchain is intended for building all software in D. The D Runtime and Phobos (the D standard library) also will not be built as those libraries have not yet been ported to the ARM Cortex-M platform. However, this toolchain should give one the tools needed to do so.

As an alternative to these instructions, you may be able to use the pre-built ARM binaries at http://gdcproject.org/downloads. All ARM GCC compilers can generate code for all ARM variants (the hard-float compiler can generate soft-float code and the soft-float compiler can generate hard-float code as well) as long as the appropriate platform compiler flags are used. The difference between the compilers are the bundled libraries (e.g. the bundled libgcc may not be compatible with the thumb targets).

These instructions will use GCC 4.9.2 and the corresponding GDC branch from the GDC source code repository.

WARNING: The scripts in these instructions will create and remove several intermediate folders, so it is best if they are executed from within an empty folder to prevent removing or overwriting existing files and folders.

Specifying a target and installation folder

export TARGET=arm-none-eabi
export PREFIX={installation directory}

Replace {installation directory} with the absolute path to the folder the toolchain should be installed in.

GNU Binutils

For information on what GNU Binutils is and what features of the toolchain it provides see their website here.

# Delete existing binutils source archive and download a new one
#-------------------------------------------------------------------
export BINUTILS_NAME=binutils-2.25
export BINUTILS_SOURCE_ARCHIVE=$BINUTILS_NAME.tar.bz2
rm -f $BINUTILS_SOURCE_ARCHIVE
rm -rf $BINUTILS_NAME
wget http://ftpmirror.gnu.org/binutils/$BINUTILS_SOURCE_ARCHIVE

# Extract binutils
#-------------------------------------------------------------------
tar xjfv $BINUTILS_SOURCE_ARCHIVE

# Create binutils build directory
#-------------------------------------------------------------------
export BINUTILS_BUILD_DIR=binutils-build
rm -rf $BINUTILS_BUILD_DIR
mkdir $BINUTILS_BUILD_DIR

# Configure and build binutils
#-------------------------------------------------------------------
cd $BINUTILS_BUILD_DIR
../$BINUTILS_NAME/configure \
  --target=$TARGET   \
  --prefix=$PREFIX   \
  --disable-nls      \
  --disable-multilib \
  --with-gnu-as      \
  --with-gnu-ld      \
  --disable-libssp   
make -j4 all
make install
cd ..

Download GDC

# Download GDC
#-------------------------------------------------------------------
rm -rf gdc
mkdir gdc
git clone https://github.com/D-Programming-GDC/GDC.git gdc
cd gdc
git checkout gdc-4.9
cd ..

Download GCC

# Delete existing GCC source archive and download a new one
#-------------------------------------------------------------------
export GCC_MIRROR=http://ftpmirror.gnu.org/gcc
export GCC_NAME=gcc-4.9.2
export GCC_SOURCE_ARCHIVE=$GCC_NAME.tar.bz2
rm -f $GCC_SOURCE_ARCHIVE
rm -rf $GCC_NAME
wget $GCC_MIRROR/$GCC_NAME/$GCC_SOURCE_ARCHIVE

# Extract GCC
#-------------------------------------------------------------------
tar xjfv $GCC_SOURCE_ARCHIVE

You may wish to use an alternate GCC mirror.

Add GDC to GCC

# Add GDC to GCC
#-------------------------------------------------------------------
cd gdc
./setup-gcc.sh ../$GCC_NAME
cd ..

Build GCC

# Create GCC build directory
#-------------------------------------------------------------------
export GCC_BUILD_DIR=gcc-build
rm -rf $GCC_BUILD_DIR
mkdir $GCC_BUILD_DIR

# Configure and build GCC
#-------------------------------------------------------------------
cd $GCC_BUILD_DIR
../$GCC_NAME/configure --target=$TARGET --prefix=$PREFIX \
  --enable-languages=d     \
  --disable-bootstrap      \
  --disable-libssp         \
  --disable-libgomp        \
  --disable-libmudflap     \
  --disable-multilib       \
  --disable-libphobos      \
  --disable-decimal-float  \
  --disable-libffi         \
  --disable-libmudflap     \
  --disable-libquadmath    \
  --disable-libssp         \
  --disable-libstdcxx-pch  \
  --disable-nls            \
  --disable-shared         \
  --disable-threads        \
  --disable-tls            \
  --with-gnu-as            \
  --with-gnu-ld            \
  --with-cpu={your_cpu}    \
  --with-tune={your_cpu}   \
  --with-mode=thumb        \
  --without-headers
make -j4 all-gcc
make -j4 all-target-libgcc

Adjust --with-cpu and --with-tune with your CPU variant (e.g. cortex-m3, cortex-m4, etc..).

--disable-libphobos disables the D Runtime and Phobos (the D standard library) as these have not yet been ported to the ARM Cortex-M platform.

make all-gcc and make all-target-libgcc are used instead of make in order to limit the build to just the necessary components.

Documentation on the various configure options can be found in GCC's documentation here

Install the toolchain

make install-gcc
make install-target-libgcc
cd ..

Testing the toolchain

The toolchain can be tested with the minimal semihosted "Hello World!" program documented here.

To get a list of compiler options specific to the target of your cross-compiler, use the command arm-none-eabi-gdc --target-help

Additional Information

These instructions were modeled after the instructions and build scripts at GNU Tools for ARM Embedded Processors. The GCC cross compiler instructions on OSDev.org were also quite helpful.

See also