Changes between Version 12 and Version 13 of CrossCompilation


Ignore:
Timestamp:
Mar 31, 2011 10:33:12 PM (4 years ago)
Author:
MtnViewMark
Comment:

major re-write and incorporation of work done on cross-compiling with 7.0

Legend:

Unmodified
Added
Removed
Modified
  • CrossCompilation

    v12 v13  
    11= Cross Compiling GHC =
    22
    3 As of this moment (GHC 6.12) GHC does not support cross-compilation.  There are reasons that we would like it to:
     3As of this moment (GHC 7.0) GHC does not support cross-compilation. This page is to gather information and plans it.
    44
    5  * [http://takeoffgw.sourceforge.net/ TakeoffGW] is a distribution of Unix tools for Windows, built by cross-compiling on a Linux machine.  They would like to be able to build and distribute GHC this way.  It might be useful for us to be able to cross-compile a Windows GHC from Linux too.
     5== General Problem ==
     6The most general case is a developer on build platform (B), wishing to build a GHC that runs on host platform (H) which produces code that runs on target platform (T). But, we need not handle such a general case: There are two common cases:
    67
    7  * We could build a 64-bit GHC on OS X, by cross-compiling using the 32-bit version.
     8 * Building a '''cross-compiler''': Create a compiler that runs on one platform, but targets another. Examples are building a GHC that:
     9   * runs on Mac OS X, but targets iOS
     10   * runs on x86_64 linux, but targets i386
     11   * runs on some existing GHC supported platform, but targets a smaller embedded platform
     12 * '''Cross-building''' a normal compiler: Build on one platform a compiler that that runs on, and targets another.  Examples:
     13   * [http://takeoffgw.sourceforge.net/ TakeoffGW] is a distribution of Unix tools for Windows, built by cross-compiling on a Linux machine.  They would like to be able to build and distribute GHC this way.  It might be useful for us to be able to cross-compile a Windows GHC from Linux too.
     14   * build a 64-bit GHC on OS X, by cross-compiling using the 32-bit version.
     15   * We could port to Win64 (#1884) by cross-compiling using a 32-bit Windows GHC.
     16   * Other porting tasks might be easier, given a suitable cross-compilation toolchain.
    817
    9  * We could port to Win64 (#1884) by cross-compiling using a 32-bit Windows GHC.
     18From the developer's perspective, these can be categorized as:
     19 * '''Cross-compiler''': B = H, and H ≠ T.
     20 * '''Cross-building''': B ≠ H, and H = T.
    1021
    11  * Other porting tasks might be easier, given a suitable cross-compilation toolchain.
     22It seems reasonable to limit ourselves to these two cases.
    1223
    13 By way of example, let's suppose we have an x86/Linux platform and we want to cross-compile to PPC64/OSX.  Then our build is going to look like this:
    1424
    15 || '''Compiler'''      || '''Runs on'''    || '''Generates code for''' ||
    16 || Stage 0       || x86/Linux  || x86/Linux          ||
    17 || Stage 1       || x86/Linux  || PPC64/OSX          ||
    18 || Stage 2       || PPC64/OSX  || PPC64/OSX          ||
     25== Meshing with GHC's 2-Stage Build ==
    1926
    20 Where stage 0 is the bootstrap compiler (the one you specify using `--with-ghc` when configuring the build), and stages 1 and 2 are the compilers being built.
     27The GHC build, in general, is a two stage process, involving three GHC compilers and two sets of build libraries:
    2128
    22 Now some general nomenclature:
     29 * '''Stage 0''': the GHC that is already on the build system (the one you specify using `--with-ghc` when configuring the build), comes with a set of built libs, could be older than the version of GHC being built
     30 * '''libs boot''': libs that the current version of GHC being built relies on that are either absent or too old in older versions of GHC that might be being used as Stage 0. These libs are built with Stage 0 GHC, and linked into the Stage 1 GHC being built.
     31 * '''Stage 1''': the first GHC built, compiled by the Stage 0 GHC, and linked with both libs from that GHC installation, and the boot libs.
     32 * '''libs install''': libs that will accompany the final compiler, built by the Stage 1 GHC
     33 * '''Stage 2''': the final GHC built, compiled by the Stage 1 GHC, and linked with only the install libs
    2334
    24  * '''Build platform''': the platform on which the software is being built
    25  * '''Host platform''': the platform on which the software will run
    26  * '''Target platform''': for a compiler, the platform on which the generated code will run
     35In summary:
    2736
    28 These correspond to CPP symbols that are defined when compiling both Haskell and C code:
     37||               ||'''Stage 0''' || '''libs boot''' || '''Stage 1''' || '''libs install''' || '''Stage 2''' ||
     38||'''built on''' || ---          || ''build''       || ''build''     || ''build''          || ''build''     ||
     39||'''runs on'''  ||''build''     || ''build''       || ''build''     || ''host''           || ''host''      ||
     40||'''targets'''  ||''build''     || ---             || ''host''      || ---                || ''target''    ||
    2941
    30  * ''xxx''`_BUILD_ARCH`, ''xxx''`_BUILD_OS`: the build platform
    31  * ''xxx''`_HOST_ARCH`, ''xxx''`_HOST_OS`: the host platform
    32  * ''xxx''`_TARGET_ARCH`, ''xxx''`_TARGET_OS`: the target platform
     42Because of the way the compiler is configured (same configuration for Stage 1 and Stage 2), and the way the install libraries are built (built with Stage 1, but shipped with Stage 2), you can see that both Stage 1 and Stage 2 compilers must target the same architecture. Furthermore, the build only allows for probing for the properties (word size, library function availability, etc...) of one platform. This means that the build isn't as general as one might expect, and what we really have is:
    3343
    34 The important thing to realise about the 2-stage bootstrap is that each stage has a different notion of build/host/target: these CPP symbols will map to different things when compiling stage 1 and stage 2.  Furthermore the RTS and libraries also have a notion of build and host (but not target: they don't generate code).
     44||               ||'''Stage 0''' || '''libs boot''' || '''Stage 1''' || '''libs install''' || '''Stage 2''' ||
     45||'''built on''' || ---          || ''build''       || ''build''     || ''build''          || ''build''     ||
     46||'''runs on'''  ||''build''     || ''build''       || ''build''     || ''target''         || ''target''    ||
     47||'''targets'''  ||''build''     || ---             || ''target''    || ---                || ''target''    ||
    3548
    36 The overall build has a build/host/target, supplied on the `configure` command line:
     49But this works out just fine for the two use cases we've identified:
    3750
    38   `$ ./configure --build=`''build''` --host=`''host''` --target=`''target''
     51 * '''Cross-compiler''': a Stage 1 compiler & libs install
     52 * '''Cross-building''': a Stage 2 compiler & libs install
    3953
    40 '''The host and target specified on the configure command line refer to the stage 2 compiler.'''  Specifically, here is how we map the platforms from the configure command line onto the platforms used by the different stages, and the RTS and libraries:
     54The build plan becomes:
    4155
    42 ||                      ||'''Overall build''' ||'''Stage 0''' || '''Stage 1''' || '''Stage 2'''   || '''libs-host''' || '''libs-target''' ||
    43 ||'''Build platform'''  ||''build''       ||--- || ''build'' || ''build''   || ''build''   || ''build'' ||
    44 ||'''Host platform'''   ||''host''        ||''build'' || ''build'' || ''host''    || ''host''    || ''target'' ||
    45 ||'''Target platform''' ||''target''      ||''build'' || ''host''  || ''target''  || ---       || --- ||
     56 * '''Cross-compiler'''
     57   * Developer configures with B = H, and H ≠ T:
     58       `$ ./configure --target=`''other-platform''
     59   * Build though Stage 1 and libs install
     60   * Package Stage 1 GHC and libs install as the desired cross-compiler
     61 * '''Cross-build'''
     62   * Developer configures with B ≠ H, and H = T
     63       `$ ./configure --host=`''other-platform''` --target=`''other-platform''
     64   * Internally, set H to B, so that we have B = H, and H ≠ T as required
     65   * Build through libs install and Stage 2
     66   * Package Stage 2 GHC and libs install as the desired cross-compiler
    4667
    47 Where '''libs-host''' refers to the libraries and RTS that we are building to link with the stage 2 compiler, and '''libs-target''' refers to the libraries and RTS that will be linked with binaries built by the stage 2 compiler to run on the target platform.
     68Thus, as far as the mechanics of the build are concerned, the two use cases are actually handled the same once the B/H/T variables are normalized. The only real difference is when to stop (before or after Stage 2), and which compiler gets bundled as the installed compiler (Stage 1 or Stage 2).
    4869
    49 In the special case where we are using cross compilation to bootstrap a new platform, as in the above example, we have ''host'' == ''target'':
     70From here on out, this page assumes that the B/H/T variables have been normalized. That is B = H, and H ≠ T.
    5071
    51 ||               ||'''Overall build'''||'''Stage 1'''||'''Stage 2'''||'''libs-host'''||
    52 ||'''Build platform''' ||''build''   ||''build''  ||''build''  ||''build''  ||
    53 ||'''Host platform'''  ||''target''  ||''build''  ||''target'' ||''target'' ||
    54 ||'''Target platform'''||''target''  ||''target'' ||''target'' ||         ||
     72== Tool-chains ==
    5573
    56 Note that with ''host'' == ''target'', '''libs-host''' == '''libs-target''', so we only need to build the RTS and libraries once (fortunately, because the GHC build system only supports building them once).
     74These kinds of builds need two tool-chains: One that runs on B/H, and compiles for B/H, the "host-tool-chain" or HT, and one that runs on B/H, but compiles for T, the "cross-tool-chain" or XT. The tool-chains include many programs needed: gcc, ld, nm, ar, as, ranlib, strip, and even ghc! The stage0 GHC is effectively part of the HT, and the stage1 we are building is going to become part of the XT. The tool-chain also includes a raft of information about the tools: does ar need ranlib, which extra ld flags need to be passed, etc.
    5775
    58 Suppose we wanted to build a cross-compiler to run on the current platform.  Then we could configure with ''build'' == ''host'', but ''target'' is different:
     76Even in a non-cross build, the current build system takes some care to achieve a limited form of tool-chain separation. In particular, when using the stage0 GHC, the build should be using the tool chain that that compiler is designed to work with -- which may not be the tool chain specified on the ./configure command line. This is only partially fulfilled. For example, while the build uses the stage0 GHC to compile C sources, so that the stage0 compatible gcc will be used, the build also uses other various tools ferreted out by ./configure (ar and ranlib for example).
    5977
    60 ||               ||'''Overall build'''||'''Stage 1'''||'''Stage 2'''||'''libs-host'''||'''libs-target'''
    61 ||'''Build platform''' ||''build''   ||''build''  ||''build''  ||''build'' ||''build'' ||
    62 ||'''Host platform'''  ||''build''   ||''build''  ||''build''  ||''build'' ||''target'' ||
    63 ||'''Target platform'''||''target''  ||''build''  ||''target'' ||          || ||
     78== Autoconf ==
    6479
    65 Note in this configuration that we need both '''libs-host''' and '''libs-target''', so currently the GHC build system does not support building this kind of cross-compiler.  Fortunately, most of the things you would want to do with this kind of cross-compiler are supported by the first kind, the only caveat is that you can't ''install'' a cross-compiler that way.
     80Autoconf offers only limited support for cross compiling. While it professes to know about three platforms, base, host, and target; it knows only about one tool-chain. It uses that tool-chain to determine two classes of information: Information about how to use the tool-chain itself, and information about the target of that tool-chain. Hence, in the cross-compilation case, it makes sense for ./configure to be told about XT.
    6681
    67 == Plan ==
     82Autoconf's concept and variable $cross_compiling only gets set if B ≠ H. This is correct from the standpoint of compiling a simple program (for which T is irrelevant). In our normalized version of B/H/T, B = H, so the logic of autoconf needs to be ammended ''(patch pending)''.
    6883
    69 Here is how it should work:
    70 
    71 {{{
    72 $ ./configure --build=<here> --host=<there> --target=<there>
    73 }}}
    74 
    75 note that we're cross-compiling from the ''build'' machine to the ''host'' machine.  The ''target'' machine is the same as the ''host'': the GHC that we're trying to create will generate binaries for ''host''.
    76 
    77 No doubt we'll also need to specify some additional configuration parameters to tell the build system where to find our cross-compilation tools.  Perhaps something like
     84This leaves us with the issue of how to tell it about parts of HT it can't infer from the stage0 compiler. We need a new set of variables that are how to compile, link and run things on the host, which if cross compiling need to be different. There needs to be some way to pass those on the configure line. Perhaps something like:
    7885
    7986{{{
     
    8592}}}
    8693
    87  * stage 1: runs on `build`, compiles for `host`
    88  * stage 2: runs on `host`, compiles for `host`
     94A tricky aspect is that some properities of the tool chain are probed by Autoconf ("is cc gcc?", "does ar need ranlib?"). These probes technically should be performed for each tool-chain. ''(partial patch pending)''
     95
     96
     97Both ./configure, cabal configure, and hsc2hs desire to run things built for T. If the XT contains an emulator, than this is possible. Two approaches need to be supported here:
     98 1. Autoconf can now descern many values without running code and configure.ac / aclocal.m4 scripts can be changed to avoid running in many cases. (For example in libraries/base I rewrote things to use AC_COMPUTE_INT rather than AC_RUN_IFELSE to find the sizes of htypes.)  ''(patch pending)''
     99 2. Plumb the need to call the emulator to run in the right places. An alternative is to use an alternate linker command that inserts the emulator into those build executables (but this is tricky as you don't want to use that link when building for the real target...)
     100
     101== Make Files ==
     102
     103The over all build sequencing needs to recognize the cross compilation configuration, and adjust build targets and final packaging to match.
     104
     105There are few other places where the make system needs to get fixed up to use the correct tool-chain at the right time ''(partial patche pending)''.
     106
     107There are a set of CPP symbols that are defined when compiling both Haskell and C code:
     108
     109 * ''xxx''`_BUILD_ARCH`, ''xxx''`_BUILD_OS`: the build platform
     110 * ''xxx''`_HOST_ARCH`, ''xxx''`_HOST_OS`: the host platform
     111 * ''xxx''`_TARGET_ARCH`, ''xxx''`_TARGET_OS`: the target platform
     112
     113There are also similar Make variables. These need to be normalized into something more rational:  At present there the usage is somewhat sloppy, since in most builds all three are the same.
     114
    89115
    90116== Things that probably need fixing ==
    91117
    92  * The configure script doesn't let you specify different `build`, `host`, and `target` right now
    93  * The build system has no distinction between the gcc used to compile from build->build and build->host.
    94118 * We can't build anything with stage2 when cross-compiling, e.g. Haddock and DPH must be disabled.
    95119
     120
    96121-----
    97 ''This is a collection of information from Mark Lentczner's work on cross-compilation. Once things are settled, this information should be merged with the above.''
     122== Status ==
    98123
    99 == General Problem ==
    100 The most general case is a user on build platform B, wishing to build a GHC that runs on host platform H which produces code that runs on target platform T. But, we need not handle such a general case, it seems reasonable to limit ourselves to the case where (from the users' perspective) B = H, and H ≠ T. That is, the user things "I want to build a GHC on my current system, that runs on my current system, and which produces code for some other, target system".
     124===  March 2011: Mark Lentczner ===
     125I actually have much of the above working. At this point I can build and link and run a stage1 cross-compiler. I have plumbed two tool-chains through the top level ./configure and make, and have gotten through configuring several libraries with the in-place cabal. I have patches almost ready to go for items above marked ''pending'', and will submit them soon, if this whole approach agrees with everyone.
    101126
    102 == High Level Approach ==
    103 In this scenario, we have two choices:
    104 1) Build a stage1 compiler that runs on and compiles for B/H, then use that to build a stage2 compiler that runs on B/H and compiles for T. In this case, the libraries rts built by stage1 compiler will be incompatible with the stage2 compiler - and so the libraries and rts for distribution would have to be built again.
    105 --or--
    106 2) Build a stage1 compiler that runs on B/H and compiles for T. Then the rts and library built by the stage1 compiler, are compatible with it, and together (stage1 compiler & libs/rts built by it) form the cross-compiler.
    107 
    108 Approach 2 is more in-line with the rest of the build system. Further, if the users's stage0 GHC is the same as the tree they are building in, it is arguable that the extra compiler build of option 1 is redundant (since the stage1 build in that option should be identical to the stage0 they started with!).
    109 
    110 == Tool-chains ==
    111 When building a cross compiler, we will need two tool-chains: One that runs on B/H, and compiles for B/H, the "host-tool-chain" or HT, and one that runs on B/H, but compiles for T, the "cross-tool-chain" or XT. The tool-chains include many programs needed: gcc, ld, nm, ar, as, ranlib, strip, and even ghc! The stage0 GHC is effectively part of the HT, and the stage1 we are building is going to become part of the XT. The tool-chain also includes a raft of information about the tools: does ar need ranlib, which extra ld flags need to be passed, etc.
    112 
    113 Even in a non-cross build, the current build system takes some care to achieve a limited form of tool-chain separation. In particular, when using the stage0 GHC, the build should be using the tool chain that that compiler is designed to work with -- which may not be the tool chain specified on the ./configure command line. This is only partially fulfilled. For example, while the build uses the stage0 GHC to compile C sources, so that the stage0 compatible gcc will be used, the build also other various tools ferreted out by ./configure (ar and ranlib for example).
    114 
    115 == Autoconf ==
    116 Autoconf offers only limited support for cross compiling. While it professes to know about three platforms, base, host, and target; it knows only about one tool-chain. It uses that tool-chain to determine two classes of information: Information about how to use the tool-chain itself, and information about the target of that tool-chain. Hence, in the cross-compilation case, it makes sense for ./configure to be told about XT.
    117 
    118 Autoconf's concept and variable $cross_compiling only gets set if B ≠ H. This is correct from the standpoint of compiling a simple program (for which T is irrelevant). From the user's perspective, B = H, so we need to augment the logic of autoconf here.
    119 
    120 This leaves us with the issue of how to tell it about parts of HT it can't infer from the stage0 compiler. We need a new set of variables that are how to compile, link and run things on the host, which if cross compiling need to be different. There needs to be some way to pass those on the configure line.
    121 A tricky aspect is that some properities of the tool chain are probed by Autoconf ("is cc gcc?", "does ar need ranlib?"). These probes technically should be performed for each tool-chain.
    122 
    123 Both ./configure, cabal configure, and hsc2hs desire to run things built for T. If the XT contains an emulator, than this is possible. Two approaches need to be taken here: 1) Autoconf can now descern many values without running code and configure.ac / aclocal.m4 scripts can be changed to avoid running in many cases. (For example in libraries/base I rewrote things to use AC_COMPUTE_INT rather than AC_RUN_IFELSE to find the sizes of htypes.) 2) Plumb the need to call the emulator to run in the right places. An alternative is to use an alternate linker command that inserts the emulator into those build executables (but this is tricky as you don't want to use that link when building for the real target...)
    124 
    125 == Status ==
    126 I actually have much of this working. At this point I can build and link and run a stage1 cross-compiler. I have plumbed two tool-chains through the top level ./configure and make, and have gotten through configuring several libraries with the in-place cabal.
    127 
    128 In general, the problems have all been in plumbing the concepts of XT vs. HT around the build system. While I've been able to fudge it for most of the components that use autoconf, I'm currently having trouble getting things right for "cabal configure" to work on the libs, as configured for the dist-install build phase. I'm worried that this might be hopeless without diving into cabal and teaching it about this kind of situation.
    129 
    130 A Wholly different approach might be to instead mirror the two tree style that porting uses. This, however, will still run into similar issues, since "target" tree still wouldn't really be running on the target.
     127In general, the problems have all been in plumbing the concepts of XT vs. HT around the build system. While I've been able to fudge it for most of the components though there are places where my work around is forced.
     128