docs/overview.txt
author "Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
Sat Sep 15 21:44:18 2007 +0000 (2007-09-15)
changeset 391 11172b754564
parent 389 0361a83180a2
child 436 ecbb620acaa0
permissions -rw-r--r--
Further improve the architecture-specific framework.
Apply this framework into building of glibc and gcc.

(Whoo! 500th commit! Yeah!)
     1 File.........: overview.txt
     2 Content......: Overview of how crosstool-NG works.
     3 Copyrigth....: (C) 2007 Yann E. MORIN <yann.morin.1998@anciens.enib.fr>
     4 License......: Creative Commons Attribution Share Alike (CC-by-sa), v2.5
     5 
     6 ________________
     7                /
     8 Introduction  /
     9 _____________/
    10 
    11 crosstool-NG aims at building toolchains. Toolchains are an essential component
    12 in a software development project. It will compile, assemble and link the code
    13 that is being developped. Some pieces of the toolchain will eventually end up
    14 in the resulting binary/ies: static libraries are but an example.
    15 
    16 So, a toolchain is a very sensitive piece of software, as any bug in one of the
    17 components, or a poorly configured component, can lead to execution problems,
    18 ranging from poor performance, to applications ending unexpectedly, to
    19 mis-behaving software (which more than often is hard to detect), to hardware
    20 damage, or even to human risks (which is more than regretable).
    21 
    22 Toolchains are made of different piece of software, each being quite complex
    23 and requiring specially crafted options to build and work seamlessly. This
    24 is usually not that easy, even in the not-so-trivial case of native toolchains.
    25 The work reaches a higher degree of complexity when it comes to cross-
    26 compilation, where it can become quite a nightmare...
    27 
    28 Some cross-toolchains exist on the internet, and can be used for general
    29 development, but they have a number of limitations:
    30   - they can be general purpose, in that they are configured for the majority:
    31     no optimisation for your specific target,
    32   - they can be prepared for a specific target and thus are not easy to use,
    33     nor optimised for, or even supporting your target,
    34   - they often are using ageing components (compiler, C library, etc...) not
    35     supporting special features of your shiny new processor;
    36 On the other side, these toolchain offer some advantages:
    37   - they are ready to use and quite easy to install and setup,
    38   - they are proven if used by a wide community.
    39 
    40 But once you want to get all the juice out of your specific hardware, you will
    41 want to build your own toolchain. This is where crosstool-NG comes into play.
    42 
    43 There are also a number of tools that builds toolchains for specific needs,
    44 which is not really scalable. Examples are:
    45   - buildroot (buildroot.uclibc.org) whose main puprpose is to build root file
    46     systems, hence the name. But once you have your toolchain with buildroot,
    47     part of it is installed in the root-to-be, so if you want to build a whole
    48     new root, you either have to save the existing one as a template and
    49     restore it later, or restart again from scratch. This is not convenient,
    50   - ptxdist (www.pengutronix.de/software/ptxdist), whose purpose is very
    51     similar to buildroot,
    52   - other projects (openembeded.org for example), which is again used to
    53     build root file systems.
    54 
    55 crosstool-NG is really targetted at building toolchains, and only toolchains.
    56 It is then up to you to use it the way you want.
    57 
    58 ___________
    59           /
    60 History  /
    61 ________/
    62 
    63 crosstool was first 'conceived' by Dan Kegel, which offered it to the community,
    64 as a set of scripts, a repository of patches, and some pre-configured, general
    65 purpose setup files to be used to configure crosstool. This is available at
    66 http://www.kegel.com/crosstool, and the subversion repository is hosted on
    67 google at http://code.google.com/p/crosstool/.
    68 
    69 At the time of writing, crosstool only supports building with one C library,
    70 namely glibc, and one C compiler, gcc; it is cripled with historical support
    71 for legacy components, and is some kind of a mess to upgrade. Also, submited
    72 patches take a loooong time before they are integrated mainline.
    73 
    74 I once managed to add support for uClibc-based toolchains, but it did not make
    75 into mainline, mostly because I don't have time to port the patch forward to
    76 the new versions, due in part to the big effort it was taking.
    77 
    78 So I decided to clean up crosstool in the state it was, re-order the things
    79 in place, and add appropriate support for what I needed, that is uClibc
    80 support. That was a disaster, as inclusion into mainline is slow as hell,
    81 and the changes were so numerous.
    82 
    83 The only option left to me was rewrite crosstool from scratch. I decided to go
    84 this way, and name the new implementation crosstool-NG, standing for crosstool
    85 Next Generation, as many other comunity projects do, and as a wink at the TV
    86 series "Star Trek: The Next Generation". ;-)
    87 
    88 
    89 ___________________________
    90                           /
    91 Installing crosstool-NG  /
    92 ________________________/
    93 
    94 There are two ways you can use crosstool-NG:
    95  - build and install it, then get rid of the sources like you'd do for most
    96    programs,
    97  - or only build it and run from the source directory.
    98 
    99 The former should be used if you got crosstool-NG from a packaged tarball, see
   100 "Install method", below, while the latter is most usefull for developpers that
   101 checked the code out from SVN, and want to submit patches, see "The Hacker's
   102 way", below.
   103 
   104 Install method |
   105 ---------------+
   106 
   107 If you go for the install, then you just follow the classical, but yet easy
   108 ./configure way:
   109   ./configure --prefix=/some/place
   110   make
   111   make install
   112   export PATH="${PATH}:/some/place/bin"
   113 
   114 You can then get rid of crosstool-NG source. Next create a directory to serve
   115 as a working place, cd in there and run:
   116   ct-ng help
   117 
   118 See below for complete usage.
   119 
   120 The Hacker's way |
   121 -----------------+
   122 
   123 If you go the hacker's way, then the usage is a bit different, although very
   124 simple:
   125   ./configure --local
   126   make
   127 
   128 Now, *do not* remove crosstool-NG sources. They are needed to run crosstool-NG!
   129 Stay in the directory holding the sources, and run:
   130   ./ct-ng help
   131 
   132 See below for complete usage.
   133 
   134 Now, provided you checked-out the code, you can send me your interesting changes
   135 by running:
   136   svn diff
   137 
   138 and mailing me the result! :-P
   139 
   140 ____________________________
   141                            /
   142 Configuring crosstool-NG  /
   143 _________________________/
   144 
   145 crosstool-NG is configured by a configurator presenting a menu-stuctured set of
   146 options. These options let you specify the way you want your toolchain built,
   147 where you want it installed, what architecture and specific processor it
   148 will support, the version of the components you want to use, etc... The
   149 value for those options are then stored in a configuration file.
   150 
   151 The configurator works the same way you configure your Linux kernel.It is
   152 assumed you now how to handle this.
   153 
   154 To enter the menu, type:
   155   ct-ng menuconfig
   156 
   157 Almost every config item has a help entry. Read them carefully.
   158 
   159 String and number options can refer to environment variables. In such a case,
   160 you must use the shell syntax: ${VAR}. You shall neither single- nor double-
   161 quote the string/number options.
   162 
   163 There are three environment variables that are computed by crosstool-NG, and
   164 that you can use:
   165 
   166 CT_TARGET:
   167   It represents the target tuple you are building for. You can use it for
   168   example in the installation/prefix directory, such as:
   169     /opt/x-tools/${CT_TARGET}
   170 
   171 CT_TOP_DIR:
   172   The top directory where crosstool-NG is running. You shouldn't need it in
   173   most cases. There is one case where you may need it: if you have local
   174   patches and you store them in your running directory, you can refer to them
   175   by using CT_TOP_DIR, such as:
   176     ${CT_TOP_DIR}/patches.myproject
   177 
   178 CT_VERSION:
   179   The version of crosstool-NG you are using. Not much use for you, but it's
   180   there if you need it.
   181 
   182 
   183 Interesting config options |
   184 ---------------------------*
   185 
   186 CT_LOCAL_TARBALLS_DIR:
   187   If you already have some tarballs in a direcotry, enter it here. That will
   188   speed up the retrieving phase, where crosstool-NG would otherwise download
   189   those tarballs.
   190 
   191 CT_PREFIX_DIR:
   192   This is where the toolchain will be installed in (and for now, where it
   193   will run from). Common use it to add the target tuple in the directory
   194   path, such as (see above):
   195     /opt/x-tools/${CT_TARGET}
   196 
   197 CT_TARGET_VENDOR:
   198   An identifier for your toolchain, will take place in the vendor part of the
   199   target tuple. It shall *not* contain spaces or dashes. Usually, keep it
   200   to a one-word string, or use underscores to separate words if you need.
   201   Avoid dots, commas, and special characters.
   202 
   203 CT_TARGET_ALIAS:
   204   An alias for the toolchian. It will be used as a prefix to the toolchain
   205   tools. For example, you will have ${CT_TARGET_ALIAS}-gcc
   206 
   207 Also, if you think you don't see enough versions, you can try to enable one of
   208 those:
   209 
   210 CT_OBSOLETE:
   211   Show obsolete versions or tools. Most of the time, you don't want to base
   212   your toolchain on too old a version (of gcc, for example). But at times, it
   213   can come handy to use such an old version for regression tests. Those old
   214   versions are hidden behind CT_OBSOLETE.
   215 
   216 CT_EXPERIMENTAL:
   217   Show experimental versions or tools. Again, you might not want to base your
   218   toolchain on too recent tools (eg. gcc) for production. But if you need a
   219   feature present only in a recent version, or a new tool, you can find them
   220   hidden behind CT_EXPERIMENTAL.
   221 
   222 CT_BROKEN:
   223   Show broken versions or tools. Some usefull tools are currently broken: they
   224   won't compile, run, or worse, cause defects when running. But if you are
   225   brave enough, you can try and debug them. They are hidden behind CT_BROKEN,
   226   which itself is hidden behind EXPERIMENTAL.
   227 
   228 Re-building an existing toolchain |
   229 ----------------------------------+
   230 
   231 If you have an existing toolchain, you can re-use the options used to build it
   232 to create a new toolchain. That needs a very little bit of effort on your side
   233 but is quite easy. The options to build a toolchain are saved in the build log
   234 file that is saved within the toolchain. crosstool-NG can extract those options
   235 to recreate a new configuration:
   236   ct-ng extractconfig </path/to/your/build.log
   237 
   238 will extract those options, prompt you for the new ones, which you can later
   239 edit with menuconfig.
   240 
   241 Of course, if your build log was compressed, you'd have to use something like:
   242   bzcat /path/to/your/build.log.bz2 |ct-ng extractconfig
   243 
   244 ________________________
   245                        /
   246 Running crosstool-NG  /
   247 _____________________/
   248 
   249 To build the toolchain, simply type:
   250   ct-ng build
   251 
   252 This will use the above configuration to retrieve, extract and patch the
   253 components, build, install and eventually test your newly built toolchain.
   254 
   255 You are then free to add the toolchain /bin directory in your PATH to use
   256 it at will.
   257 
   258 In any case, you can get some terse help. Just type:
   259   ct-ng help
   260 or:
   261   man 1 ct-ng
   262 
   263 
   264 Stoping and restarting a build |
   265 -------------------------------*
   266 
   267 If you want to stop the build after a step you are debugging, you can pass the
   268 variable STOP to make:
   269   ct-ng STOP=some_step
   270 
   271 Conversely, if you want to restart a build at a specific step you are
   272 debugging, you can pass the RESTART variable to make:
   273   ct-ng RESTART=some_step
   274 
   275 Alternatively, you can call make with the name of a step to just do that step:
   276   ct-ng libc_headers
   277 is equivalent to:
   278   ct-ng RESTART=libs_headers STOP=libc_headers
   279 
   280 The shortcuts +step_name and step_name+ allow to respectively stop or restart
   281 at that step. Thus:
   282   ct-ng +libc_headers        and:    ct-ng libc_headers+
   283 are equivalent to:
   284   ct-ng STOP=libc_headers    and:    ct-ng RESTART=libc_headers
   285 
   286 To obtain the list of acceptable steps, please call:
   287   ct-ng liststeps
   288 
   289 Note that in order to restart a build, you'll have to say 'Y' to the config
   290 option CT_DEBUG_CT_SAVE_STEPS, and that the previous build effectively went
   291 that far.
   292 
   293 
   294 Testing all toolchains at once |
   295 -------------------------------*
   296 
   297 You can test-build all samples; simply call:
   298   ct-ng regtest
   299 
   300 
   301 Overriding the number of // jobs |
   302 ---------------------------------*
   303 
   304 If you want to override the number of jobs to run in // (the -j option to
   305 make), you can either re-enter the menuconfig, or simply add it on the command
   306 line, as such:
   307   ct-ng build.4
   308 
   309 which tells crosstool-NG to override the number of // jobs to 4.
   310 
   311 You can see the actions that support overriding the number of // jobs in
   312 the help menu. Those are the ones with [.#] after them (eg. build[.#] or
   313 regtest[.#], and so on...).
   314 
   315 _______________________
   316                       /
   317 Using the toolchain  /
   318 ____________________/
   319 
   320 Using the toolchain is as simple as adding the toolchain's bin directory in
   321 your PATH, such as:
   322   export PATH="${PATH}:/your/toolchain/path/bin"
   323 
   324 and then using the target tuple to tell the build systems to use your
   325 toolchain:
   326   ./configure --target=your-target-tuple
   327 or
   328   make CC=your-target-tuple-gcc
   329 or
   330   make CROSS_COMPILE=your-target-tuple-
   331 and so on...
   332 
   333 When your root directory is ready, it is still missing some important bits: the
   334 toolchain's libraries. To populate your root directory with those libs, just
   335 run:
   336   your-target-tuple-populate -s /your/root -d /your/root-populated
   337 
   338 This will copy /your/root into /your/root-populated, and put the needed and only
   339 the needed libraries there. Thus you don't polute /your/root with any cruft that
   340 would no longer be needed should you have to remove stuff. /your/root always
   341 contains only those things you install in it.
   342 
   343 You can then use /your/root-populated to build up your file system image, a
   344 tarball, or to NFS-mount it from your target, or whatever you need.
   345 
   346 populate accepts the following options:
   347 
   348  -s [src_dir]
   349     Use 'src_dir' as the 'source', un-populated root directory
   350 
   351  -d [dst_dir]
   352     Put the 'destination', populated root directory in 'dst_dir'
   353 
   354  -f
   355     Remove 'dst_dir' if it previously existed
   356 
   357  -v
   358     Be verbose, and tell what's going on (you can see exactly where libs are
   359     coming from).
   360 
   361  -h
   362     Print the help
   363 
   364 ___________________
   365                   /
   366 Toolchain types  /
   367 ________________/
   368 
   369 There are four kinds of toolchains you could encounter.
   370 
   371 First off, you must understand the following: when it comes to compilers there
   372 are up to four machines involved:
   373   1) the machine configuring the toolchain components: the config machine
   374   2) the machine building the toolchain components:    the build machine
   375   3) the machine running the toolchain:                the host machine
   376   4) the machine the toolchain is generating code for: the target machine
   377 
   378 We can most of the time assume that the config machine and the build machine
   379 are the same. Most of the time, this will be true. The only time it isn't
   380 is if you're using distributed compilation (such as distcc). Let's forget
   381 this for the sake of simplicity.
   382 
   383 So we're left with three machines:
   384  - build
   385  - host
   386  - target
   387 
   388 Any toolchain will involve those three machines. You can be as pretty sure of
   389 this as "2 and 2 are 4". Here is how they come into play:
   390 
   391 1) build == host == target
   392     This is a plain native toolchain, targetting the exact same machine as the
   393     one it is built on, and running again on this exact same machine. You have
   394     to build such a toolchain when you want to use an updated component, such
   395     as a newer gcc for example.
   396     crosstool-NG calls it "native".
   397 
   398 2) build == host != target
   399     This is a classic cross-toolchain, which is expected to be run on the same
   400     machine it is compiled on, and generate code to run on a second machine,
   401     the target.
   402     crosstool-NG calls it "cross".
   403 
   404 3) build != host == target
   405     Such a toolchain is also a native toolchain, as it targets the same machine
   406     as it runs on. But it is build on another machine. You want such a
   407     toolchain when porting to a new architecture, or if the build machine is
   408     much faster than the host machine.
   409     crosstool-NG calls it "cross-native".
   410 
   411 4) build != host != target
   412     This one is called a canadian-toolchain (*), and is tricky. The three
   413     machines in play are different. You might want such a toolchain if you
   414     have a fast build machine, but the users will use it on another machine,
   415     and will produce code to run on a third machine.
   416     crosstool-NG calls it "canadian".
   417 
   418 crosstool-NG can build all these kinds of toolchains (or is aiming at it,
   419 anyway!)
   420 
   421 (*) The term Canadian Cross came about because at the time that these issues
   422     were all being hashed out, Canada had three national political parties.
   423     http://en.wikipedia.org/wiki/Cross_compiler
   424 
   425 _____________
   426             /
   427 Internals  /
   428 __________/
   429 
   430 Internally, crosstool-NG is script-based. To ease usage, the frontend is
   431 Makefile-based.
   432 
   433 Makefile front-end |
   434 -------------------*
   435 
   436 The entry point to crosstool-NG is the Makefile script "ct-ng". Calling this
   437 script with an action will act exactly as if the Makefile was in the current
   438 working directory and make was called with the action as rule. Thus:
   439   ct-ng menuconfig
   440 
   441 is equivalent to having the Makefile in CWD, and calling:
   442   make menuconfig
   443 
   444 Having ct-ng as it is avoids copying the Makefile everywhere, and acts as a
   445 traditional command.
   446 
   447 ct-ng loads sub- Makefiles from the library directory $(CT_LIB_DIR), as set up
   448 at configuration time with ./configure.
   449 
   450 ct-ng also search for config files, sub-tools, samples, scripts and patches in
   451 that library directory.
   452 
   453 Because of a stupid make behavior/bug I was unable to track down, implicit make
   454 rules are disabled: installing with --local would triger those rules, and mconf
   455 was unbuildable.
   456 
   457 Kconfig parser |
   458 ---------------*
   459 
   460 The kconfig language is a hacked version, vampirised from the toybox project
   461 by Rob LANDLEY (http://www.landley.net/code/toybox/), itself coming from the
   462 Linux kernel (http://www.kernel.org/), and (heavily) adapted to my needs.
   463 
   464 The kconfig parsers (conf and mconf) are not installed pre-built, but as
   465 source files. Thus you can have the directory where crosstool-NG is installed,
   466 exported (via NFS or whatever) and have clients with different architectures
   467 use the same crosstool-NG installation, and most notably, the same set of
   468 patches.
   469 
   470 Architecture-specific |
   471 ----------------------*
   472 
   473 An architecture is defined by:
   474 
   475  - a human-readable name, in lower case letters, with numbers as appropriate.
   476    The underscore is allowed. Eg.: arm, x86_64
   477  - a boolean kconfig option named after the architecture (in capital letters
   478    if possible) prefixed with "ARCH_". Eg.: ARCH_ARM, ARCH_x86_64
   479  - a directory in "arch/" named after the architecture, with the same letters
   480    as above. Eg.: arch/arm, arch/x86_64
   481    This directory contains:
   482    - a configuration file in kconfig syntax, named "config.in", which may be
   483      empty. Eg.: arch/arm/config.in
   484    - a function script in bash-3.0 syntax, named "functions", which shall
   485      follow the API defined below. Eg.: arch/arm/functions
   486 
   487 The "functions" file API:
   488  > the function "CT_DoArchValues"
   489    + parameters: none
   490    + environment:
   491       - all variables from the ".config" file,
   492       - the two variables "target_endian_eb" and "target_endian_el" which are
   493         the endianness suffixes
   494    + return value: 0 upon success, !0 upon failure
   495    + provides:
   496      - mandatory
   497      - the environment variable CT_TARGET_ARCH
   498      - contains:
   499        the architecture part of the target tuple.
   500        Eg.: "armeb" for big endian ARM
   501             "i386" for an i386
   502    + provides:
   503      - optional
   504      - the environment variable CT_TARGET_SYS
   505      - contain:
   506        the sytem part of the target tuple.
   507        Eg.: "gnu" for glibc on most architectures
   508             "gnueabi" for glibc on an ARM EABI
   509      - defaults to:
   510        - for glibc-based toolchain: "gnu"
   511        - for uClibc-based toolchain: "uclibc"
   512    + provides:
   513      - optional
   514      - the environment variable CT_KERNEL_ARCH
   515      - contains:
   516        the architecture name as understandable by the Linux kernel build
   517        system.
   518        Eg.: "arm" for an ARM
   519             "powerpc" for a PowerPC
   520             "i386" for an x86
   521      - defaults to:
   522        ${CT_ARCH}
   523    + provides:
   524      - optional
   525      - the environment variables to configure the cross-gcc
   526        - CT_ARCH_WITH_ARCH
   527        - CT_ARCH_WITH_ABI
   528        - CT_ARCH_WITH_CPU
   529        - CT_ARCH_WITH_TUNE
   530        - CT_ARCH_WITH_FPU
   531        - CT_ARCH_WITH_FLOAT
   532      - contain (defaults):
   533        - CT_ARCH_WITH_ARCH    : the gcc ./configure switch to select architecture level         ( "--with-arch=${CT_ARCH_ARCH}"       )
   534        - CT_ARCH_WITH_ABI     : the gcc ./configure switch to select ABI level                  ( "--with-abi=${CT_ARCH_ARCH}"        )
   535        - CT_ARCH_WITH_CPU     : the gcc ./configure switch to select CPU instruction set        ( "--with-cpu=${CT_ARCH_ARCH}"        )
   536        - CT_ARCH_WITH_TUNE    : the gcc ./configure switch to select scheduling                 ( "--with-tune=${CT_ARCH_ARCH}"       )
   537        - CT_ARCH_WITH_FPU     : the gcc ./configure switch to select FPU type                   ( "--with-fpu=${CT_ARCH_ARCH}"        )
   538        - CT_ARCH_WITH_FLOAT   : the gcc ./configure switch to select floating point arithmetics ( "--with-float=soft" or /empty/      )
   539    + provides:
   540      - optional
   541      - the environment variables to pass to the cross-gcc to build target binaries
   542        - CT_ARCH_ARCH_CFLAG
   543        - CT_ARCH_ABI_CFLAG
   544        - CT_ARCH_CPU_CFLAG
   545        - CT_ARCH_TUNE_CFLAG
   546        - CT_ARCH_FPU_CFLAG
   547        - CT_ARCH_FLOAT_CFLAG
   548        - CT_ARCH_ENDIAN_CFLAG
   549      - contain (defaults):
   550        - CT_ARCH_ARCH_CFLAG   : the gcc switch to select architecture level                     ( "-march=${CT_ARCH_ARCH}"            )
   551        - CT_ARCH_ABI_CFLAG    : the gcc switch to select ABI level                              ( "-mabi=${CT_ARCH_AABI}"             )
   552        - CT_ARCH_CPU_CFLAG    : the gcc switch to select CPU instruction set                    ( "-mcpu=${CT_ARCH_CPU}"              )
   553        - CT_ARCH_TUNE_CFLAG   : the gcc switch to select scheduling                             ( "-mtune=${CT_ARCH_TUNE}"            )
   554        - CT_ARCH_FPU_CFLAG    : the gcc switch to select FPU type                               ( "-mfpu=${CT_ARCH_FPU}"              )
   555        - CT_ARCH_FLOAT_CFLAG  : the gcc switch to choose floating point arithmetics             ( "-msoft-float" or /empty/           )
   556        - CT_ARCH_ENDIAN_CFLAG : the gcc switch to choose big or little endian                   ( "-mbig-endian" or "-mlittle-endian" )
   557      - default to:
   558        see above.
   559      
   560 
   561 Build scripts |
   562 --------------*
   563 
   564 To Be Written later...