From 05d2c14600526a6fe4fb9e87c1ccd3be93a86e72 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Tue, 22 Jan 2019 13:35:45 +0100 Subject: [PATCH] all: add static and release Makefile targets --- BUILDING.md | 105 ++++++++++++++++++++++++++++++++++++++ Gopkg.lock | 18 +++---- Makefile | 30 ++++++++++- loader/libclang.go | 2 - loader/libclang_config.go | 9 ++++ 5 files changed, 152 insertions(+), 12 deletions(-) create mode 100644 BUILDING.md create mode 100644 loader/libclang_config.go diff --git a/BUILDING.md b/BUILDING.md new file mode 100644 index 00000000..cd15d77d --- /dev/null +++ b/BUILDING.md @@ -0,0 +1,105 @@ +# Building TinyGo + +TinyGo depends on LLVM and libclang, which are both big C++ libraries. There are +two ways these can be linked: dynamically and statically. The default is dynamic +linking because it is fast and works almost out of the box on Debian-based +systems with the right libraries installed. + +This guide describes how to statically link TinyGo against LLVM and libclang so +that the binary can be easily moved between systems. + +## Dependencies + +LLVM and Clang are both quite light on dependencies, requiring only standard +build tools to be built. Go is of course necessary to build TinyGo itself. + + * Go (1.11+) + * [dep](https://golang.github.io/dep/) + * Standard build tools (gcc/clang) + * git or subversion + * CMake + * [Ninja](https://ninja-build.org/) or make (preferably Ninja) + +The rest of this guide assumes you're running Linux, but it should be equivalent +on a different system like Mac. + +## Download the source + +The first step is to get the source code. Place it in some directory, assuming +`$HOME/src` here, but you can pick a different one of course: + + git clone -b release_70 https://github.com/llvm-mirror/llvm.git $HOME/src/llvm + git clone -b release_70 https://github.com/llvm-mirror/clang.git $HOME/src/llvm/tools/clang + go get -d github.com/tinygo-org/tinygo + cd $HOME/go/src/github.com/tinygo-org/tinygo + dep ensure -vendor-only # download dependencies + +Note that Clang must be placed inside the tools subdirectory of LLVM to be +automatically built with the rest of the system. + +## Build LLVM and Clang + +Building LLVM is quite easy compared to some other software packages. However, +the default configuration is _not_ optimized for distribution. It is optimized +for development, meaning that binaries produce accurate error messages at the +cost of huge binaries and slow compiles. + +Before configuring, you may want to set the following environment variables to +speed up the build. Most Linux distributions ship with GCC as the default +compiler, but Clang is significantly faster and uses much less memory while +producing binaries that are about as fast. + + export CC=clang + export CXX=clang++ + +Make a build directory. LLVM requires out-of-tree builds: + + mkdir $HOME/src/llvm-build + cd $HOME/src/llvm-build + +Configure LLVM with CMake: + + 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 + +You can also choose a different build system than Ninja, but Ninja is fast. + +There are various options you can tune here, but the options given above are +preferable for releases. Here is what they do: + + * `LLVM_TARGETS_TO_BUILD` and `LLVM_EXPERIMENTAL_TARGETS_TO_BUILD`: the + targets that are natively supported by the LLVM code generators. The targets + listed here are the ones supported by TinyGo. Note that LLVM is a cross + compiler by default, unlike some other compilers. + * `CMAKE_BUILD_TYPE`: the default is Debug, which produces large inefficient + binaries that are easy to debug. We want small and fast binaries. + * `LLVM_ENABLE_ASSERTIONS`: the default is ON, which greatly slows down LLVM + and is only really useful during development. Disable them here. + * `LIBCLANG_BUILD_STATIC`: unlike LLVM, libclang is built as a shared library + by default. We want a static library for easy distribution. + +Now build it: + + ninja # or make, if you choose make in the previous step + +This can take over an hour depending on the speed of your system. + +## Build TinyGo + +Now that you have a working version of LLVM, build TinyGo using it. You need to +specify the directories to the LLVM build directory and to the Clang source. + + cd $HOME/go/src/github.com/tinygo-org/tinygo + make static LLVM_BUILDDIR=$HOME/src/llvm-build CLANG_SRC=$HOME/src/llvm/tools/clang + +## Verify TinyGo + +Try running TinyGo: + + ./build/tinygo help + +Also, make sure the `tinygo` binary really is statically linked. Check this +using `ldd`: + + ldd ./build/tinygo + +The result should not contain libclang or libLLVM. diff --git a/Gopkg.lock b/Gopkg.lock index e8858490..04084820 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -1,14 +1,6 @@ # This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. -[[projects]] - branch = "master" - digest = "1:747e7010f73dc26e5cb298444f6f1b4442ae155ab87e44080686b94a5f9d6c90" - name = "tinygo.org/x/go-llvm" - packages = ["."] - pruneopts = "UT" - revision = "8a0a627130a54562f55b1c2a9d7b8019ff14d9f1" - [[projects]] branch = "master" digest = "1:84316faef4ea12d34dde3b3e6dab682715a23b1c2bb8ab82cec9ab619766e214" @@ -21,12 +13,20 @@ pruneopts = "UT" revision = "3e7aa9e59977626dc60433e9aeadf1bb63d28295" +[[projects]] + branch = "master" + digest = "1:3611159788efdd4e0cfae18b6ebcccbad25a2815968b0e4323b42647d201031a" + name = "tinygo.org/x/go-llvm" + packages = ["."] + pruneopts = "UT" + revision = "f420620d1a0f54417a5712260153fe861780d030" + [solve-meta] analyzer-name = "dep" analyzer-version = 1 input-imports = [ - "tinygo.org/x/go-llvm", "golang.org/x/tools/go/ssa", + "tinygo.org/x/go-llvm", ] solver-name = "gps-cdcl" solver-version = 1 diff --git a/Makefile b/Makefile index 19fac130..cc78a296 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ all: tinygo tinygo: build/tinygo -.PHONY: all tinygo run-test run-blinky run-blinky2 clean fmt gen-device gen-device-nrf gen-device-avr +.PHONY: all tinygo static run-test run-blinky run-blinky2 clean fmt gen-device gen-device-nrf gen-device-avr TARGET ?= unix @@ -40,6 +40,15 @@ $(error Unknown target) endif +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 + +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++ + +# For static linking. +CGO_CPPFLAGS=$(shell $(LLVM_BUILDDIR)/bin/llvm-config --cppflags) -I$(abspath $(CLANG_SRC))/include +CGO_CXXFLAGS=-std=c++11 +CGO_LDFLAGS=-L$(LLVM_BUILDDIR)/lib $(CLANG_LIBS) $(shell $(LLVM_BUILDDIR)/bin/llvm-config --ldflags --libs --system-libs $(LLVM_COMPONENTS)) + run-test: build/test @@ -100,6 +109,25 @@ tinygo: @mkdir -p build go build -o build/tinygo . +static: + CGO_CPPFLAGS="$(CGO_CPPFLAGS)" CGO_CXXFLAGS="$(CGO_CXXFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" go build -o build/tinygo -tags byollvm . + +release: static gen-device + @mkdir -p build/release/tinygo/bin + @mkdir -p build/release/tinygo/lib/CMSIS/CMSIS + @mkdir -p build/release/tinygo/lib/compiler-rt/lib + @mkdir -p build/release/tinygo/lib/nrfx + @cp -p build/tinygo build/release/tinygo/bin + @cp -rp lib/CMSIS/CMSIS/Include build/release/tinygo/lib/CMSIS/CMSIS + @cp -rp lib/CMSIS/README.md build/release/tinygo/lib/CMSIS + @cp -rp lib/compiler-rt/lib/builtins build/release/tinygo/lib/compiler-rt/lib + @cp -rp lib/compiler-rt/LICENSE.TXT build/release/tinygo/lib/compiler-rt + @cp -rp lib/compiler-rt/README.txt build/release/tinygo/lib/compiler-rt + @cp -rp lib/nrfx/* build/release/tinygo/lib/nrfx + @cp -rp src build/release/tinygo/src + @cp -rp targets build/release/tinygo/targets + tar -czf build/release.tar.gz -C build/release tinygo + # Binary that can run on the host. build/%: src/examples/% src/examples/%/*.go build/tinygo src/runtime/*.go ./build/tinygo build $(TGOFLAGS) -size=short -o $@ $(subst src/,,$<) diff --git a/loader/libclang.go b/loader/libclang.go index d6370105..e92e31fa 100644 --- a/loader/libclang.go +++ b/loader/libclang.go @@ -9,8 +9,6 @@ import ( ) /* -#cgo CFLAGS: -I/usr/lib/llvm-7/include -#cgo LDFLAGS: -L/usr/lib/llvm-7/lib -lclang #include // if this fails, install libclang-7-dev #include diff --git a/loader/libclang_config.go b/loader/libclang_config.go new file mode 100644 index 00000000..e8494b91 --- /dev/null +++ b/loader/libclang_config.go @@ -0,0 +1,9 @@ +// +build !byollvm + +package loader + +/* +#cgo CFLAGS: -I/usr/lib/llvm-7/include +#cgo LDFLAGS: -L/usr/lib/llvm-7/lib -lclang +*/ +import "C"