| 1 |
Notes on setting up cross compiling in CMake. |
| 2 |
|
| 3 |
The Book Mastering CMake provides a chapter on this which discusses many of the |
| 4 |
issues you will encounter. There is also some information online at: |
| 5 |
http://www.cmake.org/Wiki/CMake_Cross_Compiling |
| 6 |
|
| 7 |
The key to the cross compiling is the toolchain file. This file specifies the |
| 8 |
compilers to use and the paths to look for libraries and headers. |
| 9 |
|
| 10 |
|
| 11 |
To set the compilers you simply do: |
| 12 |
|
| 13 |
set(CMAKE_C_COMPILER mpicc) |
| 14 |
|
| 15 |
for each compiler that you want to replace, just like you would on the command |
| 16 |
line if the configure check finds the wrong version etc. It is then important |
| 17 |
that you do not override this on the command line too. |
| 18 |
|
| 19 |
The most important part of the toolchain is setting up the paths that cmake |
| 20 |
should look for the libraries, headers and potentially even programs(though that |
| 21 |
is typically not the case). To do this you set the variable CMAKE_FIND_ROOT_PATH |
| 22 |
to the list of paths that you want cmake to use in any of the FIND_* commands. |
| 23 |
These paths are used as the prefix to the standard paths cmake uses for FIND_*. |
| 24 |
You will need to make sure that they follow the typical form or <given path>/lib |
| 25 |
and/or <given path>/include to ensure cmake will find things there properly. |
| 26 |
|
| 27 |
There is a way to allow people to specify paths that do not follow this standard |
| 28 |
that cmake expects, however, it does open up the possibility of allowing cmake |
| 29 |
to find incompatible/wrong libraries and headers. There are some variables that |
| 30 |
you can set to control where cmake will look for libraries and headers. The |
| 31 |
variables: |
| 32 |
|
| 33 |
CMAKE_FIND_ROOT_PATH_MODE_PROGRAM |
| 34 |
CMAKE_FIND_ROOT_PATH_MODE_LIBRARY |
| 35 |
CMAKE_FIND_ROOT_PATH_MODE_INCLUDE |
| 36 |
|
| 37 |
can be set to "NEVER, ONLY or BOTH. "NEVER" tells cmake to only use the host |
| 38 |
environment to find things. This is typically only used for finding programs |
| 39 |
since any executable that you need will most likely be run on the host machine |
| 40 |
and not the target machine. "Only" will allow cmake to only look in the |
| 41 |
specified root paths given in the toolchain file. This is the recommended and |
| 42 |
typical setup for libraries and includes. It is the safest way since it excludes |
| 43 |
any headers or libraries that may exist in the host machine's environment. |
| 44 |
"BOTH" will allow cmake to use the target(cross compile) environment _and_ the |
| 45 |
host environment. This can be useful, but it also has risk. Using BOTH allows |
| 46 |
for the possibility that cmake will find a library/header in the host |
| 47 |
environment when it should only come from the target environment. However, it |
| 48 |
does have a valid use. |
| 49 |
|
| 50 |
Apparently the value given to the "PATHS" variable for find_* commands are |
| 51 |
considered to be part of the host environment. This causes a problem in Trilinos |
| 52 |
especially for TPLs. When trying to find headers and libraries for a TPL we use |
| 53 |
the PATHS variable for the first attempt to find a TPL, this path is often |
| 54 |
specified by the user in the configure script. Since that PATHS variable is |
| 55 |
considered part of the host environment it is ignored in the case of cross |
| 56 |
compiling if we set the find setting to ONLY. To allow users to set the paths |
| 57 |
for TPLs in the way that we have encouraged them and in the way that they are |
| 58 |
familiar we have to use the BOTH setting for libraries and headers. |
| 59 |
|
| 60 |
It is possible to use TPL libraries and headers without using the "BOTH" |
| 61 |
setting, but it requires that the path for the TPL be specified in the toolchain |
| 62 |
and that the structure of that path take the form that cmake expects which is: |
| 63 |
|
| 64 |
<path given in toolchain> |
| 65 |
|----lib (where libraries are kept) |
| 66 |
|----include (where headers are kept) |
| 67 |
|
| 68 |
This may work in some cases, and if it is possible to do this it is the safest |
| 69 |
method. |
| 70 |
|
| 71 |
The example redstorm toolchain file had to use the "BOTH" setting for libraries |
| 72 |
and headers to allow some pre-existing TPL libraries for redstorm to be linked |
| 73 |
in. |
| 74 |
|
| 75 |
The last bit of what needs to be set up is the "tryrunresults" file. During a |
| 76 |
cross compile cmake make be asked to do a "try run" configure time check. Since |
| 77 |
we are compiling for a machine other than what we are currently building on |
| 78 |
cmake knows that it likely cannot run the executable so it doesn't even try. |
| 79 |
Instead what it does is generate a "tryrunresults" file that needs to be |
| 80 |
manually editted to set the variables to what the target machine would use. This |
| 81 |
can be a little confusing since it may not be clear what those variables should |
| 82 |
be set to on the target platform. Cmake does still build the executable that it |
| 83 |
was supposed to run so that you can move it over to the target machine and run |
| 84 |
it. You will then have to manually add the results of the executable to the |
| 85 |
tryrunresults file though. Once this file has been completely set up you simply |
| 86 |
have to tell cmake about it and it will use it to resolve any tryruns that it |
| 87 |
comes across during configure time. |
| 88 |
|
| 89 |
|
| 90 |
Now that we have both the toolchain and tryrunresults files all we need to do to |
| 91 |
start cross compiling is to make a configure script like normal and add the |
| 92 |
following to it: |
| 93 |
|
| 94 |
-D CMAKE_TOOLCHAIN_FILE=<toolchain file> \ |
| 95 |
-C <tryrunresults file> \ |
| 96 |
|
| 97 |
and then run the new configure script. |
| 98 |
|
| 99 |
|
| 100 |
you can look at in the redstorm directory for an example toolchain and try run |
| 101 |
file. |