| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 
 | 
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>How To Cross-Compile Clang/LLVM using Clang/LLVM — LLVM 13 documentation</title>
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    <link rel="stylesheet" href="_static/llvm-theme.css" type="text/css" />
    <script id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script>
    <script src="_static/jquery.js"></script>
    <script src="_static/underscore.js"></script>
    <script src="_static/doctools.js"></script>
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="next" title="How to Update Debug Info: A Guide for LLVM Pass Authors" href="HowToUpdateDebugInfo.html" />
    <link rel="prev" title="How to Cross Compile Compiler-rt Builtins For Arm" href="HowToCrossCompileBuiltinsOnArm.html" />
<style type="text/css">
  table.right { float: right; margin-left: 20px; }
  table.right td { border: 1px solid #ccc; }
</style>
  </head><body>
<div class="logo">
  <a href="index.html">
    <img src="_static/logo.png"
         alt="LLVM Logo" width="250" height="88"/></a>
</div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="HowToUpdateDebugInfo.html" title="How to Update Debug Info: A Guide for LLVM Pass Authors"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="HowToCrossCompileBuiltinsOnArm.html" title="How to Cross Compile Compiler-rt Builtins For Arm"
             accesskey="P">previous</a> |</li>
  <li><a href="https://llvm.org/">LLVM Home</a> | </li>
  <li><a href="index.html">Documentation</a>»</li>
          <li class="nav-item nav-item-1"><a href="UserGuides.html" accesskey="U">User Guides</a> »</li>
        <li class="nav-item nav-item-this"><a href="">How To Cross-Compile Clang/LLVM using Clang/LLVM</a></li> 
      </ul>
    </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<h3>Documentation</h3>
<ul class="want-points">
    <li><a href="https://llvm.org/docs/GettingStartedTutorials.html">Getting Started/Tutorials</a></li>
    <li><a href="https://llvm.org/docs/UserGuides.html">User Guides</a></li>
    <li><a href="https://llvm.org/docs/Reference.html">Reference</a></li>
</ul>
<h3>Getting Involved</h3>
<ul class="want-points">
    <li><a href="https://llvm.org/docs/Contributing.html">Contributing to LLVM</a></li>
    <li><a href="https://llvm.org/docs/HowToSubmitABug.html">Submitting Bug Reports</a></li>
    <li><a href="https://llvm.org/docs/GettingInvolved.html#mailing-lists">Mailing Lists</a></li>
    <li><a href="https://llvm.org/docs/GettingInvolved.html#irc">IRC</a></li>
    <li><a href="https://llvm.org/docs/GettingInvolved.html#meetups-and-social-events">Meetups and Social Events</a></li>
</ul>
<h3>Additional Links</h3>
<ul class="want-points">
    <li><a href="https://llvm.org/docs/FAQ.html">FAQ</a></li>
    <li><a href="https://llvm.org/docs/Lexicon.html">Glossary</a></li>
    <li><a href="https://llvm.org/pubs">Publications</a></li>
    <li><a href="https://github.com/llvm/llvm-project//">Github Repository</a></li>
</ul>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="_sources/HowToCrossCompileLLVM.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" />
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>$('#searchbox').show(0);</script>
        </div>
      </div>
    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="how-to-cross-compile-clang-llvm-using-clang-llvm">
<h1>How To Cross-Compile Clang/LLVM using Clang/LLVM<a class="headerlink" href="#how-to-cross-compile-clang-llvm-using-clang-llvm" title="Permalink to this headline">¶</a></h1>
<div class="section" id="introduction">
<h2>Introduction<a class="headerlink" href="#introduction" title="Permalink to this headline">¶</a></h2>
<p>This document contains information about building LLVM and
Clang on host machine, targeting another platform.</p>
<p>For more information on how to use Clang as a cross-compiler,
please check <a class="reference external" href="https://clang.llvm.org/docs/CrossCompilation.html">https://clang.llvm.org/docs/CrossCompilation.html</a>.</p>
<p>TODO: Add MIPS and other platforms to this document.</p>
</div>
<div class="section" id="cross-compiling-from-x86-64-to-arm">
<h2>Cross-Compiling from x86_64 to ARM<a class="headerlink" href="#cross-compiling-from-x86-64-to-arm" title="Permalink to this headline">¶</a></h2>
<p>In this use case, we’ll be using CMake and Ninja, on a Debian-based Linux
system, cross-compiling from an x86_64 host (most Intel and AMD chips
nowadays) to a hard-float ARM target (most ARM targets nowadays).</p>
<p>The packages you’ll need are:</p>
<blockquote>
<div><ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">cmake</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">ninja-build</span></code> (from backports in Ubuntu)</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">gcc-4.7-arm-linux-gnueabihf</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">gcc-4.7-multilib-arm-linux-gnueabihf</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">binutils-arm-linux-gnueabihf</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">libgcc1-armhf-cross</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">libsfgcc1-armhf-cross</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">libstdc++6-armhf-cross</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">libstdc++6-4.7-dev-armhf-cross</span></code></p></li>
</ul>
</div></blockquote>
<div class="section" id="configuring-cmake">
<h3>Configuring CMake<a class="headerlink" href="#configuring-cmake" title="Permalink to this headline">¶</a></h3>
<p>For more information on how to configure CMake for LLVM/Clang,
see <a class="reference internal" href="CMake.html"><span class="doc">Building LLVM with CMake</span></a>.</p>
<p>The CMake options you need to add are:</p>
<blockquote>
<div><ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">-DCMAKE_CROSSCOMPILING=True</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">-DCMAKE_INSTALL_PREFIX=<install-dir></span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">-DLLVM_TABLEGEN=<path-to-host-bin>/llvm-tblgen</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">-DCLANG_TABLEGEN=<path-to-host-bin>/clang-tblgen</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">-DLLVM_DEFAULT_TARGET_TRIPLE=arm-linux-gnueabihf</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">-DLLVM_TARGET_ARCH=ARM</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">-DLLVM_TARGETS_TO_BUILD=ARM</span></code></p></li>
</ul>
</div></blockquote>
<p>If you’re compiling with GCC, you can use architecture options for your target,
and the compiler driver will detect everything that it needs:</p>
<blockquote>
<div><ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">-DCMAKE_CXX_FLAGS='-march=armv7-a</span> <span class="pre">-mcpu=cortex-a9</span> <span class="pre">-mfloat-abi=hard'</span></code></p></li>
</ul>
</div></blockquote>
<p>However, if you’re using Clang, the driver might not be up-to-date with your
specific Linux distribution, version or GCC layout, so you’ll need to fudge.</p>
<p>In addition to the ones above, you’ll also need:</p>
<blockquote>
<div><ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">'-target</span> <span class="pre">arm-linux-gnueabihf'</span></code> or whatever is the triple of your cross GCC.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">'--sysroot=/usr/arm-linux-gnueabihf'</span></code>, <code class="docutils literal notranslate"><span class="pre">'--sysroot=/opt/gcc/arm-linux-gnueabihf'</span></code>
or whatever is the location of your GCC’s sysroot (where /lib, /bin etc are).</p></li>
<li><p>Appropriate use of <code class="docutils literal notranslate"><span class="pre">-I</span></code> and <code class="docutils literal notranslate"><span class="pre">-L</span></code>, depending on how the cross GCC is installed,
and where are the libraries and headers.</p></li>
</ul>
</div></blockquote>
<p>The TableGen options are required to compile it with the host compiler,
so you’ll need to compile LLVM (or at least <code class="docutils literal notranslate"><span class="pre">llvm-tblgen</span></code>) to your host
platform before you start. The CXX flags define the target, cpu (which in this case
defaults to <code class="docutils literal notranslate"><span class="pre">fpu=VFP3</span></code> with NEON), and forcing the hard-float ABI. If you’re
using Clang as a cross-compiler, you will <em>also</em> have to set <code class="docutils literal notranslate"><span class="pre">--sysroot</span></code>
to make sure it picks the correct linker.</p>
<p>When using Clang, it’s important that you choose the triple to be <em>identical</em>
to the GCC triple and the sysroot. This will make it easier for Clang to
find the correct tools and include headers. But that won’t mean all headers and
libraries will be found. You’ll still need to use <code class="docutils literal notranslate"><span class="pre">-I</span></code> and <code class="docutils literal notranslate"><span class="pre">-L</span></code> to locate
those extra ones, depending on your distribution.</p>
<p>Most of the time, what you want is to have a native compiler to the
platform itself, but not others. So there’s rarely a point in compiling
all back-ends. For that reason, you should also set the
<code class="docutils literal notranslate"><span class="pre">TARGETS_TO_BUILD</span></code> to only build the back-end you’re targeting to.</p>
<p>You must set the <code class="docutils literal notranslate"><span class="pre">CMAKE_INSTALL_PREFIX</span></code>, otherwise a <code class="docutils literal notranslate"><span class="pre">ninja</span> <span class="pre">install</span></code>
will copy ARM binaries to your root filesystem, which is not what you
want.</p>
</div>
<div class="section" id="hacks">
<h3>Hacks<a class="headerlink" href="#hacks" title="Permalink to this headline">¶</a></h3>
<p>There are some bugs in current LLVM, which require some fiddling before
running CMake:</p>
<ol class="arabic">
<li><p>If you’re using Clang as the cross-compiler, there is a problem in
the LLVM ARM back-end that is producing absolute relocations on
position-independent code (<code class="docutils literal notranslate"><span class="pre">R_ARM_THM_MOVW_ABS_NC</span></code>), so for now, you
should disable PIC:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>-DLLVM_ENABLE_PIC<span class="o">=</span>False
</pre></div>
</div>
<p>This is not a problem, since Clang/LLVM libraries are statically
linked anyway, it shouldn’t affect much.</p>
</li>
<li><p>The ARM libraries won’t be installed in your system.
But the CMake prepare step, which checks for
dependencies, will check the <em>host</em> libraries, not the <em>target</em>
ones. Below there’s a list of some dependencies, but your project could
have more, or this document could be outdated. You’ll see the errors
while linking as an indication of that.</p>
<p>Debian based distros have a way to add <code class="docutils literal notranslate"><span class="pre">multiarch</span></code>, which adds
a new architecture and allows you to install packages for those
systems. See <a class="reference external" href="https://wiki.debian.org/Multiarch/HOWTO">https://wiki.debian.org/Multiarch/HOWTO</a> for more info.</p>
<p>But not all distros will have that, and possibly not an easy way to
install them in any anyway, so you’ll have to build/download
them separately.</p>
<p>A quick way of getting the libraries is to download them from
a distribution repository, like Debian (<a class="reference external" href="http://packages.debian.org/jessie/">http://packages.debian.org/jessie/</a>),
and download the missing libraries. Note that the <code class="docutils literal notranslate"><span class="pre">libXXX</span></code>
will have the shared objects (<code class="docutils literal notranslate"><span class="pre">.so</span></code>) and the <code class="docutils literal notranslate"><span class="pre">libXXX-dev</span></code> will
give you the headers and the static (<code class="docutils literal notranslate"><span class="pre">.a</span></code>) library. Just in
case, download both.</p>
<p>The ones you need for ARM are: <code class="docutils literal notranslate"><span class="pre">libtinfo</span></code>, <code class="docutils literal notranslate"><span class="pre">zlib1g</span></code>,
<code class="docutils literal notranslate"><span class="pre">libxml2</span></code> and <code class="docutils literal notranslate"><span class="pre">liblzma</span></code>. In the Debian repository you’ll
find downloads for all architectures.</p>
<p>After you download and unpack all <code class="docutils literal notranslate"><span class="pre">.deb</span></code> packages, copy all
<code class="docutils literal notranslate"><span class="pre">.so</span></code> and <code class="docutils literal notranslate"><span class="pre">.a</span></code> to a directory, make the appropriate
symbolic links (if necessary), and add the relevant <code class="docutils literal notranslate"><span class="pre">-L</span></code>
and <code class="docutils literal notranslate"><span class="pre">-I</span></code> paths to <code class="docutils literal notranslate"><span class="pre">-DCMAKE_CXX_FLAGS</span></code> above.</p>
</li>
</ol>
</div>
<div class="section" id="running-cmake-and-building">
<h3>Running CMake and Building<a class="headerlink" href="#running-cmake-and-building" title="Permalink to this headline">¶</a></h3>
<p>Finally, if you’re using your platform compiler, run:</p>
<blockquote>
<div><div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>$ cmake -G Ninja <source-dir> <options above>
</pre></div>
</div>
</div></blockquote>
<p>If you’re using Clang as the cross-compiler, run:</p>
<blockquote>
<div><div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>$ <span class="nv">CC</span><span class="o">=</span><span class="s1">'clang'</span> <span class="nv">CXX</span><span class="o">=</span><span class="s1">'clang++'</span> cmake -G Ninja <source-dir> <options above>
</pre></div>
</div>
</div></blockquote>
<p>If you have <code class="docutils literal notranslate"><span class="pre">clang</span></code>/<code class="docutils literal notranslate"><span class="pre">clang++</span></code> on the path, it should just work, and special
Ninja files will be created in the build directory. I strongly suggest
you to run <code class="docutils literal notranslate"><span class="pre">cmake</span></code> on a separate build directory, <em>not</em> inside the
source tree.</p>
<p>To build, simply type:</p>
<blockquote>
<div><div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>$ ninja
</pre></div>
</div>
</div></blockquote>
<p>It should automatically find out how many cores you have, what are
the rules that needs building and will build the whole thing.</p>
<p>You can’t run <code class="docutils literal notranslate"><span class="pre">ninja</span> <span class="pre">check-all</span></code> on this tree because the created
binaries are targeted to ARM, not x86_64.</p>
</div>
<div class="section" id="installing-and-using">
<h3>Installing and Using<a class="headerlink" href="#installing-and-using" title="Permalink to this headline">¶</a></h3>
<p>After the LLVM/Clang has built successfully, you should install it
via:</p>
<blockquote>
<div><div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>$ ninja install
</pre></div>
</div>
</div></blockquote>
<p>which will create a sysroot on the install-dir. You can then tar
that directory into a binary with the full triple name (for easy
identification), like:</p>
<blockquote>
<div><div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>$ ln -sf <install-dir> arm-linux-gnueabihf-clang
$ tar zchf arm-linux-gnueabihf-clang.tar.gz arm-linux-gnueabihf-clang
</pre></div>
</div>
</div></blockquote>
<p>If you copy that tarball to your target board, you’ll be able to use
it for running the test-suite, for example. Follow the guidelines at
<a class="reference external" href="https://llvm.org/docs/lnt/quickstart.html">https://llvm.org/docs/lnt/quickstart.html</a>, unpack the tarball in the
test directory, and use options:</p>
<blockquote>
<div><div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>$ ./sandbox/bin/python sandbox/bin/lnt runtest nt <span class="se">\</span>
    --sandbox sandbox <span class="se">\</span>
    --test-suite <span class="sb">`</span><span class="nb">pwd</span><span class="sb">`</span>/test-suite <span class="se">\</span>
    --cc <span class="sb">`</span><span class="nb">pwd</span><span class="sb">`</span>/arm-linux-gnueabihf-clang/bin/clang <span class="se">\</span>
    --cxx <span class="sb">`</span><span class="nb">pwd</span><span class="sb">`</span>/arm-linux-gnueabihf-clang/bin/clang++
</pre></div>
</div>
</div></blockquote>
<p>Remember to add the <code class="docutils literal notranslate"><span class="pre">-jN</span></code> options to <code class="docutils literal notranslate"><span class="pre">lnt</span></code> to the number of CPUs
on your board. Also, the path to your clang has to be absolute, so
you’ll need the <cite>pwd</cite> trick above.</p>
</div>
</div>
</div>
            <div class="clearer"></div>
          </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="HowToUpdateDebugInfo.html" title="How to Update Debug Info: A Guide for LLVM Pass Authors"
             >next</a> |</li>
        <li class="right" >
          <a href="HowToCrossCompileBuiltinsOnArm.html" title="How to Cross Compile Compiler-rt Builtins For Arm"
             >previous</a> |</li>
  <li><a href="https://llvm.org/">LLVM Home</a> | </li>
  <li><a href="index.html">Documentation</a>»</li>
          <li class="nav-item nav-item-1"><a href="UserGuides.html" >User Guides</a> »</li>
        <li class="nav-item nav-item-this"><a href="">How To Cross-Compile Clang/LLVM using Clang/LLVM</a></li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        © Copyright 2003-2021, LLVM Project.
      Last updated on 2021-09-18.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 3.5.4.
    </div>
  </body>
</html>
 |