Skip to content

Build Golang Binding

Claudia Misale edited this page Feb 25, 2021 · 9 revisions

In this page I am showing the steps I do to build the Go binding on Mac.

Code can be found https://github.com/cmisale/flux-sched.git, in gobind-dev branch.

Disclaimer

Please keep in mind that there are a few hacks I needed to do in order to make multiple versions of Go coexist, and I needed to fix some absolute paths in order to avoid touching stuff on my local disk. For some reason, even if I export Go env variables, go env doesn't show up the right ones. So, I need to export the correct paths almost every time I need to run a go command.
We need to find a more elegant way to build the binding. I gave go modules a try, but paths were anyway messed up. Help/Feedback is welcome, of course.

Build Flux-Sched

First step is to build and run the flux-sched docker container

cd flux-sched
./src/test/docker/docker-run-checks.sh -I --   ## this will run a container with all the dependencies, mounting the current folder
./autogen
./configure
make 

Build the Binding

Install Golang binaries

Since I am running on Mac, I need to download the binaries for Linux to be able to build the binding in the container. You can find information here https://golang.org/doc/install.

I extracted the directory in my flux-sched home directory.

Binding internals

To build the binding, we need the C binding to be already built, as we will use it from Go.

In resources/hlapi/bindings/go there are a few files

$ cd resource/hlapi/bindings/go/
$ tree
.
├── main
│   └── main.go
└── src
    └── fluxcli
        ├── reapi_cli.go
        └── reapi_cli_test.go

The main.go is our development environment. The fluxcli folder contains the reapi_cli.go file, which is the root for the fluxcli package that we can import in main.go. There is also some dummy code for testing in reapi_cli_test.go. The fluxcli package contains the implementation of the same reapi_cli function names we find in the reapi_cli.h, which are actually called in Go by accessing the reapi_cli.la library in resource/hlapi/bindings/c/.libs.

For example

func NewReapiCli() *ReapiCtx {
	return (*ReapiCtx)(C.reapi_cli_new())
}

with C. syntax, we can access the functions defined reapi_cli library, and we can create objects defined in the library (like reapi_cli_ctx) that we can access in Go, as well as C types. For instance, here we create a typedef for an easy access to the reapi_cli_ctx_t struct defined in the reapi_cli library:

type (
	ReapiCtx C.struct_reapi_cli_ctx_t
)

Install the binding

First, we need to build the cli package. Notice that we need to specify the absolute path. I am also using absolute path for the Go binary. This should work fine if you are installing the Go binaries in flux-sched home folder, otherwise remember to fix the path in the command.

Since with this docker container we are mounting a local volume, I am seeing GOPATH being messed up and I need to set it by hand. To build the package, we need superpowers to avoid an failed to initialize build cache at /Users/cmisale/.cache/go-build: mkdir /Users: permission denied error... local disk stuff.

cd resource/hlapi/bindings/go/src/fluxcli
 
sudo CGO_CFLAGS="-I/usr/src/resource/hlapi/bindings/c -I/usr/include" CGO_LDFLAGS="-L/usr/src/resource/hlapi/bindings/c/.libs -lreapi_cli -lstdc++" GOPATH="/usr/src/resource/hlapi/bindings/go" /usr/src/golang/go/bin/go install 

## try the test..
CGO_CFLAGS="-I/usr/src/resource/hlapi/bindings/c -I/usr/include" CGO_LDFLAGS="-L/usr/src/resource/hlapi/bindings/c/.libs -lreapi_cli -lstdc++" GOPATH="/usr/src/resource/hlapi/bindings/go" /usr/src/golang/go/bin/go test
PASS
ok  	fluxcli	0.007s

Let's try out the main program. It is creating a cli context struct and prints it. Again, to build it I need to specify where Flux components are.

cd ../../main

CGO_CFLAGS="-I/usr/src/resource/hlapi/bindings/c -I/usr/include" CGO_LDFLAGS="-L/usr/src/resource/hlapi/bindings/c/.libs -lreapi_cli -lstdc++" GOPATH="/usr/src/resource/hlapi/bindings/go" /usr/src/golang/go/bin/go build -o main

./main
really nothing  &{}
Clone this wiki locally