Skip to content

Build Golang Binding

Claudia Misale edited this page Feb 24, 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 to fix some absolute paths in order to avoid touch stuff from 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 I had anyway paths messing up with my local disk. 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 built already as we will use it from Go.

In resources/hlapi/bindings/go there are two files

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

The main.go is our testing environment. The fluxcli folder contains reapi_cli.go package, which builds the fluxcli binding package that we import in main.go. There is also some dummy code for testing. It contains the implementation of the same reapi_cli function names we find in the reapi_cli.h, which are actually called through the access to 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, as well as creating objects defined in the C library (like reapi_cli_ctx) that we can access in Go:

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. 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 avoid 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