DEV Community


Posted on • Originally published at on

Test LLVM Go binding in travis

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
      - ubuntu-toolchain-r-test
      - llvm-toolchain-trusty
      - 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
Enter fullscreen mode Exit fullscreen mode

Now we get LLVM 8 on Travis.

Then we need to setup environment variables:

  - 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`"
Enter fullscreen mode Exit fullscreen mode

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 contains a type check bound to make sure type run_build_sh generated.It can only be generated by run

But if we dig into build flow, we will find generated the thing!It works like:

go build $GOPATH/src/
./llvm-go print-config $GOPATH/src/
Enter fullscreen mode Exit fullscreen mode

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 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/ && cd $GOPATH/src/
curl -XGET >> llvm.tar.xz
tar -xf llvm.tar.xz
mv llvm-6.0.0.src llvm
Enter fullscreen mode Exit fullscreen mode

Now, you can test your code by preinstalled LLVM!

Top comments (1)

dannypsnl profile image

Strongly recommend for golang llvm user now