DEV Community

loading...

Test LLVM Go binding in travis

dannypsnl profile image 林子篆 Originally published at dannypsnl.github.io on ・2 min read

Basically, the problem is because I use Go binding of LLVM, then it hard to use preinstalled LLVM.

This article is about how I solve the problem step by step.

First we need to install dependencies by apt-get(travis use ubuntu 14.04 if you do not spec anything)

dist: trusty
addons:
  apt:
    sources:
      - ubuntu-toolchain-r-test
      - llvm-toolchain-trusty
    packages:
      - g++-4.9
      - clang-8
      - clang-tools-8
      - libclang-common-8-dev
      - libclang-8-dev
      - libclang1-8
      - libllvm8
      - llvm-8
      - llvm-8-dev
      - llvm-8-runtime
      - libfuzzer-8-dev
      - lldb-8
      - lld-8

Now we get LLVM 8 on Travis.

Then we need to setup environment variables:

env:
  - CC=clang-8 CGO_CXXFLAGS=-std=c++11 CGO_LDFLAGS_ALLOW='Wl,(search_paths_first|headerpad_max_install_names)' CGO_CPPFLAGS="`llvm-config-8 --cppflags`" CGO_LDFLAGS="`llvm-config-8 --ldflags --libs --system-libs all`"

llvm-config is a helper tool of LLVM that would generate the meta data for your program, e.g. llvm-config --cppflags contains include path to installed headers path of LLVM.

At here the suffix -8 is because it installed like that.

But if you compile your Go binding now, you would get some what run_build_sh is undefined.

This is because at llvm.org/llvm/bindings/go/llvm/llvm_dep.go contains a type check bound to make sure type run_build_sh generated.It can only be generated by run llvm.org/llvm/bindings/go/build.sh

But if we dig into build flow, we will find llvm.org/llvm/tools/llvm-go/llvm-go.go generated the thing!It works like:

go build $GOPATH/src/llvm.org/llvm/tools/llvm-go/llvm-go.go
./llvm-go print-config $GOPATH/src/llvm.org/llvm/bindings/go/llvm/llvm_config.go

Now we can work with latest version of LLVM basically, but if you install some older LLVM in your machine, it won’t compile!

Because Go binding would expect some variable, but C didn’t have one(old version).

The solution is go to http://releases.llvm.org to download spec version(should be the same as installed in your system) to get binding source.

Use LLVM 6.0 to be example:

mkdir -p $GOPATH/src/llvm.org && cd $GOPATH/src/llvm.org
curl -XGET http://releases.llvm.org/6.0.0/llvm-6.0.0.src.tar.xz >> llvm.tar.xz
tar -xf llvm.tar.xz
mv llvm-6.0.0.src llvm

Now, you can test your code by preinstalled LLVM!

Discussion (1)

Collapse
dannypsnl profile image
林子篆 Author

Strongly recommend github.com/llir/llvm for golang llvm user now

Forem Open with the Forem app