GNAT Cross-Compiler: Difference between revisions

m
Bot: Replace deprecated source tag with syntaxhighlight
[unchecked revision][unchecked revision]
(Initial checkin)
 
m (Bot: Replace deprecated source tag with syntaxhighlight)
 
(4 intermediate revisions by 2 users not shown)
Line 68:
The following sets up the base variables used within our build script. The <tt>BUILD_TARGET</tt> variable specifies the [[Target_Triplet|target triplet]] for our build. This is a specific string format used by GNU tools to specify the target system for a build. The <tt>BUILD_PREFIX</tt> variable provides the installation directory prefix for our cross-compiler installation. <tt>${HOME}/opt/cross/</tt> is a common location for installing cross-compilers. Any sensible location can be used here, however sticking to established conventions is recommended for new developers. The <tt>HOST</tt> variable provides the target triplet that identifies our host system.
 
<sourcesyntaxhighlight lang="bash">
#!/usr/bin/env bash
 
Line 96:
# Adjust this as necessary for the target system.
local_lib_dir="/usr/local"
</syntaxhighlight>
</source>
 
This guide assumes that the source for the build prerequisites is stored in the location <tt>${HOME}/src</tt> with another directory <tt>${HOME}/src/build</tt> used as an intermediate build directory prior to installation. If necessary, these directories can be substituted for others in the scripts below by modifying these variables.
 
<sourcesyntaxhighlight lang="bash">
# The directory where the source directories are located.
source_dir="${HOME}/src"
# The directory to use as storage for the intermediate build dirs.
build_dir="${HOME}/src/build"
</syntaxhighlight>
</source>
 
The below variables define the version of each dependency to build. These correspond to the source directories contained within the parent source directory listed above. These are defined here to facilitate easily changing the versions of the various dependencies in a single place.
 
<sourcesyntaxhighlight lang="bash">
# The versions of each dependency to build.
binutils_version="2.32"
gcc_version="8.3.0"
</syntaxhighlight>
</source>
 
=== Binutils ===
Line 119:
The following code details the process for building [https://www.gnu.org/software/binutils/ GNU binutils], which is a required component in the cross-compiler build. Binutils provides utilities such as <tt>as</tt>, <tt>ld</tt>, and <tt>objdump</tt> built for the target architecture.
 
<sourcesyntaxhighlight lang="bash">
binutils_dir="binutils-${binutils_version}"
 
Line 143:
make -j${concurrency} || exit 1
make -j${concurrency} install || exit 1
</syntaxhighlight>
</source>
 
Detailed explanations for the selected configuration options can be seen below:
Line 171:
|-
| --with-sysroot
| Enable [[sysroot]] support in the cross-compiler. NotUsing passingthe abare directoryargument here causeswithout thea directory specified will configurationdefault to an empty directory. Supplying this argument will allow support for integrating a custom sysroot at a later point.
|}
 
Line 180:
In order to successfully create a build of GCC with Ada support, an initial GCC cross-compiler for the target architecture is required. This initial bootstrapping build will be built with only C support. This will be used to bootstrap our final GCC build with GNAT support.
 
<sourcesyntaxhighlight lang="bash">
gcc_dir="gcc-${gcc_version}"
 
Line 205:
make -j${concurrency} all-gcc || exit 1
make -j${concurrency} install-gcc || exit 1
</syntaxhighlight>
</source>
 
Detailed explanations for the configuration options specific to this step can be seen below:
Line 239:
The code below shows the recipe for the final GCC build which will add Ada support.
 
<sourcesyntaxhighlight lang="bash">
gcc_dir="gcc-${gcc_version}"
 
Line 268:
make -j${concurrency} -C gcc cross-gnattools ada.all.cross || exit 1
make -j${concurrency} install-strip-gcc install-target-libgcc || exit 1
</syntaxhighlight>
</source>
 
 
Line 289:
In order to use the newly built cross-compiler its binaries must be visible on the system's <tt>PATH</tt>. On a Linux system this can be facilitated by adding the following line to the current user's <tt>~/.bashrc</tt> file, or the equivalent file on the host system:
 
<sourcesyntaxhighlight lang="bash">
# Adjust the following to reflect the location of the installed cross-compiler build.
export PATH="${HOME}/opt/cross/i686-elf:$PATH"
</syntaxhighlight>
</source>
 
== GPRbuild Integration ==
Line 298:
[https://docs.adacore.com/gprbuild-docs/html/gprbuild_ug.html GPRbuild] is a build tool designed for instrumenting the compilation of GNAT projects. It is written and maintained by AdaCore and is installed alongside their GNAT compiler or can be built from source. Its source code is publicly available on [https://github.com/AdaCore/gprbuild AdaCore's Github profile]. Its function is analogous to that of GNU Make. It is capable of compiling projects featuring a variety of source languages such as Ada, C and Assembly, among others. Modern versions of GNAT do not natively feature the capability to compile GNAT project files, and will require the use of GPRbuild.
 
GPRbuild will not automatically recognise the existence of the new cross-compiler toolchain. Additional steps are necessary before GPRbuild will recognise our target architecture as being valid. This is accomplished by adding configuration information about the newly built toolchain to GPRbuild's 'knowledge base' linker configuration file. This file instructs GPRbuild how to link executables and libraries using the various toolchains supported by the host system. If GPRbuild was installed as part of an AdaCore GNAT installation, this configuration file will be located at <tt>${prefix}/share/gprconfig/linker.xml></tt>, where <tt>${prefix}</tt> is the location of the GNAT install directory. The easiest way to add a new target architecture is to duplicate an existing toolchain's target configuration. Inside the linker configuration file, search for an existing configuration such as <tt>leon-elf</tt>, and duplicate each entry for the existing configuration. modifying it to reflect the newly built <tt>i686-elf</tt>, or similar, target.
 
''Note: GPRbuild uses regular expressions to pattern match the target triplets.''
Line 304:
A truncated example of the new configuration entries can be seen below:
 
<sourcesyntaxhighlight lang="xml">
<configuration>
<targets>
Line 329:
</config>
</configuration>
</syntaxhighlight>
</source>
 
Once each entry has been duplicated and modified to reference the newly built toolchain, paying special attention to preserve any regex special characters, the newly built toolchain should be visible to GPRbuild.