|
| 1 | +# Building TinyGo |
| 2 | + |
| 3 | +TinyGo depends on LLVM and libclang, which are both big C++ libraries. There are |
| 4 | +two ways these can be linked: dynamically and statically. The default is dynamic |
| 5 | +linking because it is fast and works almost out of the box on Debian-based |
| 6 | +systems with the right libraries installed. |
| 7 | + |
| 8 | +This guide describes how to statically link TinyGo against LLVM and libclang so |
| 9 | +that the binary can be easily moved between systems. |
| 10 | + |
| 11 | +## Dependencies |
| 12 | + |
| 13 | +LLVM and Clang are both quite light on dependencies, requiring only standard |
| 14 | +build tools to be built. Go is of course necessary to build TinyGo itself. |
| 15 | + |
| 16 | + * Go (1.11+) |
| 17 | + * [dep](https://golang.github.io/dep/) |
| 18 | + * Standard build tools (gcc/clang) |
| 19 | + * git or subversion |
| 20 | + * CMake |
| 21 | + * [Ninja](https://ninja-build.org/) or make (preferably Ninja) |
| 22 | + |
| 23 | +The rest of this guide assumes you're running Linux, but it should be equivalent |
| 24 | +on a different system like Mac. |
| 25 | + |
| 26 | +## Download the source |
| 27 | + |
| 28 | +The first step is to get the source code. Place it in some directory, assuming |
| 29 | +`$HOME/src` here, but you can pick a different one of course: |
| 30 | + |
| 31 | + git clone -b release_70 https://github.com/llvm-mirror/llvm.git $HOME/src/llvm |
| 32 | + git clone -b release_70 https://github.com/llvm-mirror/clang.git $HOME/src/llvm/tools/clang |
| 33 | + go get -d github.com/tinygo-org/tinygo |
| 34 | + cd $HOME/go/src/github.com/tinygo-org/tinygo |
| 35 | + dep ensure -vendor-only # download dependencies |
| 36 | + |
| 37 | +Note that Clang must be placed inside the tools subdirectory of LLVM to be |
| 38 | +automatically built with the rest of the system. |
| 39 | + |
| 40 | +## Build LLVM and Clang |
| 41 | + |
| 42 | +Building LLVM is quite easy compared to some other software packages. However, |
| 43 | +the default configuration is _not_ optimized for distribution. It is optimized |
| 44 | +for development, meaning that binaries produce accurate error messages at the |
| 45 | +cost of huge binaries and slow compiles. |
| 46 | + |
| 47 | +Before configuring, you may want to set the following environment variables to |
| 48 | +speed up the build. Most Linux distributions ship with GCC as the default |
| 49 | +compiler, but Clang is significantly faster and uses much less memory while |
| 50 | +producing binaries that are about as fast. |
| 51 | + |
| 52 | + export CC=clang |
| 53 | + export CXX=clang++ |
| 54 | + |
| 55 | +Make a build directory. LLVM requires out-of-tree builds: |
| 56 | + |
| 57 | + mkdir $HOME/src/llvm-build |
| 58 | + cd $HOME/src/llvm-build |
| 59 | + |
| 60 | +Configure LLVM with CMake: |
| 61 | + |
| 62 | + cmake -G Ninja ../llvm "-DLLVM_TARGETS_TO_BUILD=X86;ARM;AArch64" "-DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD=AVR;WebAssembly" -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_ASSERTIONS=OFF -DLIBCLANG_BUILD_STATIC=ON |
| 63 | + |
| 64 | +You can also choose a different build system than Ninja, but Ninja is fast. |
| 65 | + |
| 66 | +There are various options you can tune here, but the options given above are |
| 67 | +preferable for releases. Here is what they do: |
| 68 | + |
| 69 | + * `LLVM_TARGETS_TO_BUILD` and `LLVM_EXPERIMENTAL_TARGETS_TO_BUILD`: the |
| 70 | + targets that are natively supported by the LLVM code generators. The targets |
| 71 | + listed here are the ones supported by TinyGo. Note that LLVM is a cross |
| 72 | + compiler by default, unlike some other compilers. |
| 73 | + * `CMAKE_BUILD_TYPE`: the default is Debug, which produces large inefficient |
| 74 | + binaries that are easy to debug. We want small and fast binaries. |
| 75 | + * `LLVM_ENABLE_ASSERTIONS`: the default is ON, which greatly slows down LLVM |
| 76 | + and is only really useful during development. Disable them here. |
| 77 | + * `LIBCLANG_BUILD_STATIC`: unlike LLVM, libclang is built as a shared library |
| 78 | + by default. We want a static library for easy distribution. |
| 79 | + |
| 80 | +Now build it: |
| 81 | + |
| 82 | + ninja # or make, if you choose make in the previous step |
| 83 | + |
| 84 | +This can take over an hour depending on the speed of your system. |
| 85 | + |
| 86 | +## Build TinyGo |
| 87 | + |
| 88 | +Now that you have a working version of LLVM, build TinyGo using it. You need to |
| 89 | +specify the directories to the LLVM build directory and to the Clang source. |
| 90 | + |
| 91 | + cd $HOME/go/src/github.com/tinygo-org/tinygo |
| 92 | + make static LLVM_BUILDDIR=$HOME/src/llvm-build CLANG_SRC=$HOME/src/llvm/tools/clang |
| 93 | + |
| 94 | +## Verify TinyGo |
| 95 | + |
| 96 | +Try running TinyGo: |
| 97 | + |
| 98 | + ./build/tinygo help |
| 99 | + |
| 100 | +Also, make sure the `tinygo` binary really is statically linked. Check this |
| 101 | +using `ldd`: |
| 102 | + |
| 103 | + ldd ./build/tinygo |
| 104 | + |
| 105 | +The result should not contain libclang or libLLVM. |
0 commit comments