Skip to content

Commit 05d2c14

Browse files
committed
all: add static and release Makefile targets
1 parent e7ad366 commit 05d2c14

File tree

5 files changed

+152
-12
lines changed

5 files changed

+152
-12
lines changed

BUILDING.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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.

Gopkg.lock

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Makefile

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
all: tinygo
44
tinygo: build/tinygo
55

6-
.PHONY: all tinygo run-test run-blinky run-blinky2 clean fmt gen-device gen-device-nrf gen-device-avr
6+
.PHONY: all tinygo static run-test run-blinky run-blinky2 clean fmt gen-device gen-device-nrf gen-device-avr
77

88
TARGET ?= unix
99

@@ -40,6 +40,15 @@ $(error Unknown target)
4040

4141
endif
4242

43+
LLVM_COMPONENTS = all-targets analysis asmparser asmprinter bitreader bitwriter codegen core coroutines debuginfodwarf executionengine instrumentation interpreter ipo irreader linker mc mcjit objcarcopts option profiledata scalaropts support target
44+
45+
CLANG_LIBS = -Wl,--start-group $(abspath $(LLVM_BUILDDIR))/lib/libclang.a -lclangAnalysis -lclangARCMigrate -lclangAST -lclangASTMatchers -lclangBasic -lclangCodeGen -lclangCrossTU -lclangDriver -lclangDynamicASTMatchers -lclangEdit -lclangFormat -lclangFrontend -lclangFrontendTool -lclangHandleCXX -lclangHandleLLVM -lclangIndex -lclangLex -lclangParse -lclangRewrite -lclangRewriteFrontend -lclangSema -lclangSerialization -lclangStaticAnalyzerCheckers -lclangStaticAnalyzerCore -lclangStaticAnalyzerFrontend -lclangTooling -lclangToolingASTDiff -lclangToolingCore -lclangToolingInclusions -lclangToolingRefactor -Wl,--end-group -lstdc++
46+
47+
# For static linking.
48+
CGO_CPPFLAGS=$(shell $(LLVM_BUILDDIR)/bin/llvm-config --cppflags) -I$(abspath $(CLANG_SRC))/include
49+
CGO_CXXFLAGS=-std=c++11
50+
CGO_LDFLAGS=-L$(LLVM_BUILDDIR)/lib $(CLANG_LIBS) $(shell $(LLVM_BUILDDIR)/bin/llvm-config --ldflags --libs --system-libs $(LLVM_COMPONENTS))
51+
4352

4453

4554
run-test: build/test
@@ -100,6 +109,25 @@ tinygo:
100109
@mkdir -p build
101110
go build -o build/tinygo .
102111

112+
static:
113+
CGO_CPPFLAGS="$(CGO_CPPFLAGS)" CGO_CXXFLAGS="$(CGO_CXXFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" go build -o build/tinygo -tags byollvm .
114+
115+
release: static gen-device
116+
@mkdir -p build/release/tinygo/bin
117+
@mkdir -p build/release/tinygo/lib/CMSIS/CMSIS
118+
@mkdir -p build/release/tinygo/lib/compiler-rt/lib
119+
@mkdir -p build/release/tinygo/lib/nrfx
120+
@cp -p build/tinygo build/release/tinygo/bin
121+
@cp -rp lib/CMSIS/CMSIS/Include build/release/tinygo/lib/CMSIS/CMSIS
122+
@cp -rp lib/CMSIS/README.md build/release/tinygo/lib/CMSIS
123+
@cp -rp lib/compiler-rt/lib/builtins build/release/tinygo/lib/compiler-rt/lib
124+
@cp -rp lib/compiler-rt/LICENSE.TXT build/release/tinygo/lib/compiler-rt
125+
@cp -rp lib/compiler-rt/README.txt build/release/tinygo/lib/compiler-rt
126+
@cp -rp lib/nrfx/* build/release/tinygo/lib/nrfx
127+
@cp -rp src build/release/tinygo/src
128+
@cp -rp targets build/release/tinygo/targets
129+
tar -czf build/release.tar.gz -C build/release tinygo
130+
103131
# Binary that can run on the host.
104132
build/%: src/examples/% src/examples/%/*.go build/tinygo src/runtime/*.go
105133
./build/tinygo build $(TGOFLAGS) -size=short -o $@ $(subst src/,,$<)

loader/libclang.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import (
99
)
1010

1111
/*
12-
#cgo CFLAGS: -I/usr/lib/llvm-7/include
13-
#cgo LDFLAGS: -L/usr/lib/llvm-7/lib -lclang
1412
#include <clang-c/Index.h> // if this fails, install libclang-7-dev
1513
#include <stdlib.h>
1614

loader/libclang_config.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// +build !byollvm
2+
3+
package loader
4+
5+
/*
6+
#cgo CFLAGS: -I/usr/lib/llvm-7/include
7+
#cgo LDFLAGS: -L/usr/lib/llvm-7/lib -lclang
8+
*/
9+
import "C"

0 commit comments

Comments
 (0)