LLVM Cross-Compiler: Difference between revisions

From OSDev.wiki
Jump to navigation Jump to search
[unchecked revision][unchecked revision]
Content added Content deleted
No edit summary
m (Bot: Replace deprecated source tag with syntaxhighlight)
 
(7 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{In_Progress}}
{{rating|3}}
{{rating|3}}


Line 19: Line 20:
** GNU Binutils (a fairly recent version of Binutils, try 2.21 or above if you get assembly compilation errors)
** GNU Binutils (a fairly recent version of Binutils, try 2.21 or above if you get assembly compilation errors)
** GIT or SVN (if building from sources)
** GIT or SVN (if building from sources)
** Curl (if building from source using the linked article)


== Building ==
== Building ==
Line 28: Line 30:
For GIT, in bash:
For GIT, in bash:


<source lang="bash">
<syntaxhighlight lang="bash">
mkdir crossllvm
mkdir crossllvm
cd crossllvm
cd crossllvm
Line 36: Line 38:
cd ../projects
cd ../projects
git clone http://llvm.org/git/compiler-rt.git
git clone http://llvm.org/git/compiler-rt.git
</syntaxhighlight>
</source>


For SVN, in bash:
For SVN, in bash:


<source lang="bash">
<syntaxhighlight lang="bash">
mkdir crossllvm
mkdir crossllvm
cd crossllvm
cd crossllvm
Line 48: Line 50:
cd ../projects
cd ../projects
svn co http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
svn co http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
</syntaxhighlight>
</source>


=== Building from sources (Possibly Unstable) ===
=== Building from sources (Possibly Unstable) ===
After having checked out the sources (see above), in bash, from the "crossllvm" directory:
After having checked out the sources (see above), in bash, from the "crossllvm" directory:
<source lang="bash">
<syntaxhighlight lang="bash">
mkdir build
mkdir build
cd build
cd build
Line 58: Line 60:
make
make
make install
make install
</syntaxhighlight>
</source>


Note: This Autoconf build has been removed from current versions of the LLVM build. All new builds must use CMake.
Or with CMake:


Or with CMake (adjust the source path as needed):
<source lang="bash">

<syntaxhighlight lang="bash">
mkdir build
mkdir build
cd build
cd build
cmake -DCMAKE_BUILD_TYPE:STRING=Release
cmake -DCMAKE_BUILD_TYPE:STRING=Release ../llvm/
make
make
make install
make install
</syntaxhighlight>
</source>


Now you have got LLVM and Clang built!
Now you have got LLVM and Clang built!


Alternatively, a faster and more up-to-date way for building from source (using ninja) is available [http://atta-metta.net/blog/2013/05/23/compiling-llvm-trunk/ here]. The linked article was written in May of 2013, so all of the article's provided custom patches are no longer required. Assuming you are using recent versions (released during or after June 2013) of compiler-rt, lldb, and libc++ then you can safely skip installing the provided patches within the article and proceed with compiling using the unmodified content of the git repos the article instructs you to download.
A faster and more up-to-date way is posted [http://exocortex.madfire.net/post/2013/05/23/compiling-llvm-trunk/ here]. It uses ninja for much faster building.


A Clang cross-compiler is generated by [https://github.com/berkus/metta/blob/develop/build_toolchain.sh this buildscript]. Dissect it to learn more. It uses some gcc and binutils to provide a fully working toolchain.
A Clang cross-compiler is generated by [https://github.com/berkus/metta/blob/develop/build_toolchain.sh this buildscript]. Dissect it to learn more. It uses some gcc and binutils to provide a fully working toolchain.
Line 78: Line 82:
=== Debian ===
=== Debian ===
Open a terminal
Open a terminal
<source lang="bash">
<syntaxhighlight lang="bash">
sudo apt-get install clang
sudo apt-get install clang
</syntaxhighlight>
</source>
Note, you might have to disable host functionality. See Useful Flags
Note, you might have to disable host functionality. See Useful Flags


== Building libc++ ==
== Building libc++ ==


<source lang="bash">
<syntaxhighlight lang="bash">
$ git clone http://llvm.org/git/libcxx.git
$ git clone http://llvm.org/git/libcxx.git
$ export TRIPLE=-apple-
$ export TRIPLE=-apple-
$ cd libcxx/lib
$ cd libcxx/lib
$ ./buildit
$ ./buildit
</syntaxhighlight>
</source>


Built libc++ is not installed by default, you can use make install or copy files manually.
Built libc++ is not installed by default, you can use make install or copy files manually.
Line 99: Line 103:


For example, for cross compiling to ARM, you can use
For example, for cross compiling to ARM, you can use
<source lang="bash">
<syntaxhighlight lang="bash">
-march=armv7-a -mfloat-abi=soft -ccc-host-triple arm-elf
-march=armv7-a -mfloat-abi=soft -ccc-host-triple arm-elf
</syntaxhighlight>
</source>
Since 3.1, it can be shortened to
Since 3.1, it can be shortened to
<source lang="bash">
<syntaxhighlight lang="bash">
-target armv7--eabi -mcpu=cortex-a9
-target armv7--eabi -mcpu=cortex-a9
</syntaxhighlight>
</source>
An example for compiling to a generic X86 ELF target would be:
<syntaxhighlight lang="bash">
--target=i686-pc-none-elf -march=i686
</syntaxhighlight>


=== Useful Flags ===
=== Useful Flags ===
Line 119: Line 127:
==== -nostdinc++ ====
==== -nostdinc++ ====
Makes sure the standard C++ library headers are not included. This makes sense if you build a custom version of libc++ and want to avoid including system one.
Makes sure the standard C++ library headers are not included. This makes sense if you build a custom version of libc++ and want to avoid including system one.

== Using system roots ==
Due to Clang's dependency on ld, you may get the error message "/usr/bin/ld: this linker was not configured to use sysroots". If you receive this error, you'll probably have to compile binutils with sysroot support. There is information how to do this [[GCC Cross-Compiler#Binutils]] - note the "--with-sysroot" flag.


== TODO ==
== TODO ==
Line 124: Line 135:
TODO: describe non-svn build from released tarballs.
TODO: describe non-svn build from released tarballs.
TODO: [http://www.embtoolkit.org EmbToolkit project] recently added support to clang/llvm based cross compiler, ddd note about how it works.
TODO: [http://www.embtoolkit.org EmbToolkit project] recently added support to clang/llvm based cross compiler, ddd note about how it works.

[[Category:Compilers]]

Latest revision as of 04:36, 9 June 2024

This page is a work in progress.
This page may thus be incomplete. Its content may be changed in the near future.
Difficulty level

Advanced

Introduction

NOTE: This article is not complete yet.
Having working and production ready llvm/clang cross-compiler involves much more work than just what is explained here
(such that having llvm/clang use correct target associated sysroot, static linker, C/C++ libraries, etc.)
See clang Universal driver.
For working and production ready clang/llvm cross-compiler use dedicated tools (such as EmbToolkit) generating one for you.

About

Generally speaking, a cross-compiler is a compiler that runs on platform A (the host), but generates executables for platform B (the target). These two platforms may (but do not need to) differ in CPU, operating system, and/or executable format.

Clang (and llvm) are host cross compilers. They by default have the capability to cross compile, but also produce host binaries. See Usage

Requirements

  • A host system with a working compiler (can be GCC, Clang, etc).
  • A bash shell or comparable environment. If you are not using a bash shell, you might have to modify some of the command lines below. If you have just installed the basic Cygwin package, you have to run the setup.exe again and install the following packages:
    • GNU Make or CMake
    • GNU Binutils (a fairly recent version of Binutils, try 2.21 or above if you get assembly compilation errors)
    • GIT or SVN (if building from sources)
    • Curl (if building from source using the linked article)

Building

Checking out sources

Clang/LLVM sources can be checked out either with GIT or SVN.

For GIT, in bash:

mkdir crossllvm
cd crossllvm
git clone http://llvm.org/git/llvm.git
cd llvm/tools
git clone http://llvm.org/git/clang.git
cd ../projects
git clone http://llvm.org/git/compiler-rt.git

For SVN, in bash:

mkdir crossllvm
cd crossllvm
svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm
cd llvm/tools
svn co http://llvm.org/svn/llvm-project/cfe/trunk clang
cd ../projects
svn co http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt

Building from sources (Possibly Unstable)

After having checked out the sources (see above), in bash, from the "crossllvm" directory:

mkdir build
cd build
../llvm/configure --enable-optimized
make
make install

Note: This Autoconf build has been removed from current versions of the LLVM build. All new builds must use CMake.

Or with CMake (adjust the source path as needed):

mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE:STRING=Release ../llvm/
make
make install

Now you have got LLVM and Clang built!

Alternatively, a faster and more up-to-date way for building from source (using ninja) is available here. The linked article was written in May of 2013, so all of the article's provided custom patches are no longer required. Assuming you are using recent versions (released during or after June 2013) of compiler-rt, lldb, and libc++ then you can safely skip installing the provided patches within the article and proceed with compiling using the unmodified content of the git repos the article instructs you to download.

A Clang cross-compiler is generated by this buildscript. Dissect it to learn more. It uses some gcc and binutils to provide a fully working toolchain.

Debian

Open a terminal

sudo apt-get install clang

Note, you might have to disable host functionality. See Useful Flags

Building libc++

$ git clone http://llvm.org/git/libcxx.git
$ export TRIPLE=-apple-
$ cd libcxx/lib
$ ./buildit

Built libc++ is not installed by default, you can use make install or copy files manually.


Usage

After building you will have a compiler able to output multiple output formats regardless of your current platform.

For example, for cross compiling to ARM, you can use

-march=armv7-a -mfloat-abi=soft -ccc-host-triple arm-elf

Since 3.1, it can be shortened to

-target armv7--eabi -mcpu=cortex-a9

An example for compiling to a generic X86 ELF target would be:

--target=i686-pc-none-elf -march=i686

Useful Flags

Some usefull flags for OS development.

-ffreestanding

Indicated that the file should be compiled for a freestanding enviroment (like a kernel), not a hosted (userspace), environment.

-fno-builtin

Disable special handling and optimizations of builtin functions like strlen and malloc.

-nostdlib

Disables standard library

-nostdinc

Makes sure the standard library headers are not included.

-nostdinc++

Makes sure the standard C++ library headers are not included. This makes sense if you build a custom version of libc++ and want to avoid including system one.

Using system roots

Due to Clang's dependency on ld, you may get the error message "/usr/bin/ld: this linker was not configured to use sysroots". If you receive this error, you'll probably have to compile binutils with sysroot support. There is information how to do this GCC Cross-Compiler#Binutils - note the "--with-sysroot" flag.

TODO

TODO: describe non-svn build from released tarballs.
TODO: EmbToolkit project recently added support to clang/llvm based cross compiler, ddd note about how it works.