Skip to content

Commit

Permalink
Initial version of Go bindings.
Browse files Browse the repository at this point in the history
This code is based on the existing LLVM Go bindings project hosted at:
https://github.com/go-llvm/llvm

Note that all contributors to the gollvm project have agreed to relicense
their changes under the LLVM license and submit them to the LLVM project.

Differential Revision: http://reviews.llvm.org/D5684

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@219976 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
pcc committed Oct 16, 2014
1 parent 86b3d8e commit 798ace2
Show file tree
Hide file tree
Showing 45 changed files with 4,426 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,9 @@ tools/lld
tools/polly
# Sphinx build tree, if building in-source dir.
docs/_build

#==============================================================================#
# Files created in tree by the Go bindings.
#==============================================================================#
bindings/go/llvm/llvm_config.go
bindings/go/llvm/workdir
1 change: 1 addition & 0 deletions Makefile.config.in
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ DOT := @DOT@
DOXYGEN := @DOXYGEN@
GROFF := @GROFF@
GZIPBIN := @GZIPBIN@
GO := @GO@
OCAMLC := @OCAMLC@
OCAMLOPT := @OCAMLOPT@
OCAMLDEP := @OCAMLDEP@
Expand Down
19 changes: 19 additions & 0 deletions autoconf/configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -1287,6 +1287,7 @@ AC_PATH_PROG(GROFF, [groff])
AC_PATH_PROG(GZIPBIN, [gzip])
AC_PATH_PROG(PDFROFF, [pdfroff])
AC_PATH_PROG(ZIP, [zip])
AC_PATH_PROG(GO, [go])
AC_PATH_PROGS(OCAMLC, [ocamlc])
AC_PATH_PROGS(OCAMLOPT, [ocamlopt])
AC_PATH_PROGS(OCAMLDEP, [ocamldep])
Expand Down Expand Up @@ -1867,6 +1868,11 @@ if test "$BINDINGS_TO_BUILD" = auto ; then
if test "x$OCAMLC" != x -a "x$OCAMLDEP" != x ; then
BINDINGS_TO_BUILD="ocaml $BINDINGS_TO_BUILD"
fi
if test "x$GO" != x ; then
if $GO run ${srcdir}/bindings/go/conftest.go ; then
BINDINGS_TO_BUILD="go $BINDINGS_TO_BUILD"
fi
fi
fi
AC_SUBST(BINDINGS_TO_BUILD,$BINDINGS_TO_BUILD)

Expand Down Expand Up @@ -1901,6 +1907,19 @@ for a_binding in $BINDINGS_TO_BUILD ; do
fi
fi
;;
go)
if test "x$GO" = x ; then
AC_MSG_WARN([--enable-bindings=go specified, but go not found. Try configure GO=/path/to/go])
binding_prereqs_failed=1
else
if $GO run ${srcdir}/bindings/go/conftest.go ; then
:
else
AC_MSG_WARN([--enable-bindings=go specified, but need at least Go 1.2. Try configure GO=/path/to/go])
binding_prereqs_failed=1
fi
fi
;;
esac
done
if test "$binding_prereqs_failed" = 1 ; then
Expand Down
6 changes: 5 additions & 1 deletion bindings/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ LEVEL := ..

include $(LEVEL)/Makefile.config

PARALLEL_DIRS = $(BINDINGS_TO_BUILD)
PARALLEL_DIRS =

ifneq (,$(filter ocaml,$(BINDINGS_TO_BUILD)))
PARALLEL_DIRS += ocaml
endif

include $(LEVEL)/Makefile.common
53 changes: 53 additions & 0 deletions bindings/go/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
This directory contains LLVM bindings for the Go programming language
(http://golang.org).

Prerequisites
-------------

* Go 1.2+.
* CMake (to build LLVM).

Using the bindings
------------------

The package path "llvm.org/llvm/bindings/go/llvm" can be used to
import the latest development version of LLVM from SVN. Paths such as
"llvm.org/llvm.v36/bindings/go/llvm" refer to released versions of LLVM.

It is recommended to use the "-d" flag with "go get" to download the
package or a dependency, as an additional step is required to build LLVM
(see "Building LLVM" below).

Building LLVM
-------------

The script "build.sh" in this directory can be used to build LLVM and prepare
it to be used by the bindings. If you receive an error message from "go build"
like this:

./analysis.go:4:84: fatal error: llvm-c/Analysis.h: No such file or directory
#include <llvm-c/Analysis.h> // If you are getting an error here read bindings/go/README.txt

or like this:

./llvm_dep.go:5: undefined: run_build_sh

it means that LLVM needs to be built or updated by running the script.

$ $GOPATH/src/llvm.org/llvm/bindings/go/build.sh

Any command line arguments supplied to the script are passed to LLVM's CMake
build system. A good set of arguments to use during development are:

$ $GOPATH/src/llvm.org/llvm/bindings/go/build.sh -DCMAKE_BUILD_TYPE=Debug -DLLVM_TARGETS_TO_BUILD=host -DBUILD_SHARED_LIBS=ON

Note that CMake keeps a cache of build settings so once you have built
LLVM there is no need to pass these arguments again after updating.

Alternatively, you can build LLVM yourself, but you must then set the
CGO_CPPFLAGS, CGO_CXXFLAGS and CGO_LDFLAGS environment variables:

$ export CGO_CPPFLAGS="`/path/to/llvm-build/bin/llvm-config --cppflags`"
$ export CGO_CXXFLAGS=-std=c++11
$ export CGO_LDFLAGS="`/path/to/llvm-build/bin/llvm-config --ldflags --libs --system-libs all`"
$ go build -tags byollvm
67 changes: 67 additions & 0 deletions bindings/go/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/bin/sh -xe

llvm_components="\
all-targets \
analysis \
asmparser \
asmprinter \
bitreader \
bitwriter \
codegen \
core \
debuginfo \
executionengine \
instrumentation \
interpreter \
ipo \
irreader \
linker \
mc \
mcjit \
objcarcopts \
option \
profiledata \
scalaropts \
support \
target \
"

if [ "$1" == "--print-components" ] ; then
echo $llvm_components
exit 0
fi

gollvmdir=$(dirname "$0")/llvm

workdir=$gollvmdir/workdir
llvmdir=$gollvmdir/../../..
llvm_builddir=$workdir/llvm_build

mkdir -p $llvm_builddir

cmake_flags="../../../../.. $@"
llvm_config="$llvm_builddir/bin/llvm-config"

if test -n "`which ninja`" ; then
# If Ninja is available, we can speed up the build by building only the
# required subset of LLVM.
(cd $llvm_builddir && cmake -G Ninja $cmake_flags)
ninja -C $llvm_builddir llvm-config
llvm_buildtargets="$($llvm_config --libs $llvm_components | sed -e 's/-l//g')"
ninja -C $llvm_builddir $llvm_buildtargets FileCheck
else
(cd $llvm_builddir && cmake $cmake_flags)
make -C $llvm_builddir -j4
fi

llvm_version="$($llvm_config --version)"
llvm_cflags="$($llvm_config --cppflags)"
llvm_ldflags="$($llvm_config --ldflags) $($llvm_config --libs $llvm_components) $($llvm_config --system-libs)"
if [ $(uname) != "Darwin" ]; then
# OS X doesn't like -rpath with cgo. See:
# https://code.google.com/p/go/issues/detail?id=7293
llvm_ldflags="-Wl,-rpath,$($llvm_config --libdir) $llvm_ldflags"
fi
sed -e "s#@LLVM_CFLAGS@#$llvm_cflags#g; s#@LLVM_LDFLAGS@#$llvm_ldflags#g" $gollvmdir/llvm_config.go.in > \
$gollvmdir/llvm_config.go
printf "package llvm\n\nconst Version = \"%s\"\n" "$llvm_version" > $gollvmdir/version.go
16 changes: 16 additions & 0 deletions bindings/go/conftest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import (
"go/build"
"os"
)

// Tests that the Go compiler is at least version 1.2.
func main() {
for _, tag := range build.Default.ReleaseTags {
if tag == "go1.2" {
os.Exit(0)
}
}
os.Exit(1)
}
Loading

0 comments on commit 798ace2

Please sign in to comment.