2012-05-04    Piotr Romaniuk, Ph.D.
What is a Toolchain?
If you read about building own cross-compiler you will find that this is not a trivial task. The build process is multistage and requires to build
a compiler for building a compiler. Although it sounds like 'snake eating its tail' it can be done.
Simplifying, the build of cross-compiler consists in making:
- cross-tools (like assembler, linker, etc.) - [binutils],
- first version of C cross-compiler - static version, without thread support,
- standard C library,
- final version of C cross-compiler.
In presented above sequence, consecutive stage is built by items created in previous stages. This process of building tools
is some kind of chain hence the name toolchain.
This term is ofter used to name all tools created at the end of build process.
If you want to learn more about building toolchain please read first chapters of: Cross Linux From Scratch.
This project deals with building the whole firmware image, including Linux kernel, filesystem preparation, bootloader and instalation details.
The first part is there making the toolchain, that is later used for building further components.
Although all phases are well described, this is hard way, because of lack of automatic methods (if you need to modify something you need to repeat the
sequence manually). You must remember that the goal of Cross Linux From Scratch project is presenting a reference, but not an environment for building.
Building the Toolchain
In order to build own toolchain I advise to use ready environment intended to that purpose, e.g.
crosstool-NG. In this case, building a toolchain consists in setting configurations and starting build process that will continue
automatically. Because of the complexity of this process you can expect that this take some time (usually tens of minutes).
Depending on operating system that you have on target machine, you can build under Linux (recommended) or under Windows using Cygwin.
Be aware of extra requirements when you work with Cygwin. Below on this page you may find a description.
According to manual from crosstool-NG site you need:
I. download selected version of package crosstool-NG from its site, e.g. crosstool-ng-1.14.1.tar.bz2
II. decompress the sources and enter that folder:
tar -xjf crosstool-ng-1.14.1.tar.bz2
cd crosstool-ng-1.14.1
III. configure package:
./configure --prefix=/some/folder/ctng-bin
where: prefix is a path selected by you, where crosstool-NG tool will be built (NOTE: this is not a toolchain yet)
  If you receive during configuration a message that some package is missing, you need install it and start configuration again.
(Cygwin required closing its console window while I was installing some packages).
IV. Build and installation crosstool-ng tool:
make
make install
V. add to system path crosstool-ng
export PATH="${PATH}:/some/folder/ctng-bin/bin
VI. enter the folder where toolchain will be built and its configuration will be stored:
cd /some/folder/development
When you complete above steps, crosstool-NG is ready for configuration the toolchain and staring the build after that.
In order to configure toolchain you need to execute:
ct-ng menuconfig
This will cause opening configuration menu:
Using that interface you should set toolchain options and save it on exit (by default it is written in .config file).
When you finish configuration you can start building by a command:
ct-ng build
Building process takes a lot of time, on my computer it took about 40 min. under Linux and 2h under Windows/Cygwin.
If you do not disable debug information in configuration options, the messages reporting completed phases will appear on console,
as well as progress indicator in a form of rotating bar.
Be aware that Crosstool-NG will download proper versions of packages, uncompress them, makes patching and compiles them.
Toolchain Configuration Options
Full set of options is very large, I will not explain them all, but only the most important:
Path and misc options
   - Prefix directory - the folder where built toolchain will be installed (by default it is in: ${HOME}/x-tools/${CT_TARGET} )
   - Number of parallel jobs - controls parallel built (speed vs. computer load)
   - Maximum log level - specifies how many details are reported during build process
Target options
   - Target Architecture - an architecture of processor on target platform (e.g.. x86, arm, etc.)
   - Bitness - 32/64 bits
   - Architecture level - version of processor, e.g. i686
   - Emit assembly for CPU - specifies what assembler should be used (like above)
   - Tune for CPU - code optimization for model (like above)
Operating system
   - Target OS - operating system that will work on target platform
       bare-metal - means without OS,
       linux - with Linuxem (if Linux is selected, kernel version must be specified; if it is not on the list, select custom_tarbal_directory
    
       and point out path to archive with sources (kernel can be downloaded from www.kernel.org).
C compiler
   C++ - mark if you need C++
C-library
   C library - type of standard C library ( size, speed vs. functionality )
     - uClibc - limited functionality, small size,
     - glibc - full GNU C Library - large, full functionality,
     - eglibc - version GNU C Library for embedded systems.
Hwo to Use Toolchain?
When the build process is finished, the toolchain is located in folder ${HOME}/x-tools/arch-vendor-kernel-system if you did not change it in configuration.
In order to use cross compiler you need to add it to system path, e.g.:
export PATH="${PATH}:/home/user/x-tools/i686-unknown-linux-gnu/bin"
in your makefiles that are intended for building target software you should use tools with prefix, e.g.:
CC=i686-unknown-linux-gnu-gcc
LD=i686-unknown-linux-gnu-gcc
AR=i686-unknown-linux-gnu-ar
AS=i686-unknown-linux-gnu-as
so during build cross-tools will be used and they generate the code binary compatible for target platform.
How to Build Toolchain under Cygwin?
When I was building crosstool-NG (1.14.1) and the toolchain I experienced following issues:
1. crosstool-NG requires case sensitive filesystem.
Unfortunatelly, NTFS used by Windows has disabled this option.
You can change it by a modification in Windows register and system restart:
   HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\kernel\obcaseinsensitive=0
aconring to   (1) Cygwin FAQ 4.30: Is Cygwin case-sensitive?
and:     (2) Using Cygwin, rozdział "Case sensitive filenames"
Warning:
You need to remember that this changes behavior not only files under Cygwin but whole filesystem.
It is possible that some software is assuming the case insensitivity, that will be not true after the change. In this case it can work strange.
In [2] there is a warning that you do this on your risk. The warning seems to be exaggerated. I change it permanently in my Windows 7 and I do not
see any problems. Nevertheless remember it.
2. Unnecessary elements in system path
When you start Cygwin console Windows system path (%PATH%) is copied to $PATH. Because it may point to other programs that use
another binary versions of Cygwin or MinGW, they will be detected by ./configure script. In order to avoid this issue, you need to shorten
the path in Cygwin. I advise to check it and then cut to necessary minimum part:
echo $PATH
export PATH=/usr/local/bin:/usr/bin
3. ncurses headers problem
Although ncurses headers are detected properly by configuration script, during the build you receive the message that they are missing.
Adding the path in kconfig/Makefile solve the problem:
# Build flags
CFLAGS = -DCONFIG_=\"CT_\" -DPACKAGE="\"crosstool-NG $(VERSION)\"" -I/usr/include/ncurses
4. Missing libintl library
Add library in kconfig/Makefile:
# Build flags
...
LDFLAGS = -lintl
5. ESCDELAY issue
The symbol seems to be undefined for ncurses in Cygwinie [?].
When I commented it in kconfig/nconf.c@1518 it stopped complaining:
notimeout(stdscr, FALSE);
/*ESCDELAY = 1;*/
6. Make in version newer than 3.81
In newer Make version (Cygwin can install package Make-v.3.82) small change has been introduced in makefiles interpretation.
It fixed some problem, that was previously ignored (it concerns how to Make behaves for mixed rules).
Although the correction is right, it cause the problem with build,
For now, I advise to install in Cygwin older version of Make, i.e. version 3.81 (you can select version in GUI setup.exe).
7. Much longer time of build than under Linux
Unfortunatelly, there is not much to do, you must be patient. You are not building the toolchain every day ;)
This issue is caused by extra load of open-file methods and fork in Cygwin according to Linux operations.
You need to remember that Cygwin is only some kind of envelope that provides a way for starting programs under Windows system in quasi Linux environment.