Skip to content

Instantly share code, notes, and snippets.

@delicb
Last active March 29, 2021 23:43
Show Gist options
  • Save delicb/aebed97c6a2736b4fd0773a58985a210 to your computer and use it in GitHub Desktop.
Save delicb/aebed97c6a2736b4fd0773a58985a210 to your computer and use it in GitHub Desktop.
Getting OSO to work with Go on Apple M1 chip

Preparing OSO for ARM64

This is meant as a workaround for people using Apple M1 based computers and want to use OSO.

OSO provides Go package, but it depends on a core library, which is written in Rust in with which OSO communicates via C bindings.

First thing, clone OSO repository:

git clone https://github.com/osohq/oso.git
cd oso

Next, you will need Rust installed. We will do that via Brew.

brew install rust

This will install Rust and Cargo, Rust pacakge manager.

Next, build OSO. While in OSO repository, cloned above, execute

cargo build --release

This should produce file in target/release/libpolar.a. Now, we need to copy this file to a place where Go will find it. According to a source code, following command should do the trick:

cp target/release/libpolar.a languages/go/internal/ffi/native/macos 

One more step remains. File languages/go/internal/ffi/ffi.go references the library we just copied, but needs to be changed to reflect the fact we are using ARM. So, find a line

// #cgo darwin,amd64 LDFLAGS: ${SRCDIR}/native/macos/libpolar.a -ldl -lm

and replace it with

// #cgo darwin,arm64 LDFLAGS: ${SRCDIR}/native/macos/libpolar.a -ldl -lm

(notice amd64 -> arm64 change). In current version, this is the line 8, but it might change in the future.

Testing it all out

Now that we have all prepared, lets try it out.

You should be currently in cloned oso project. Lets remember the path to it and create a new Go project that is using local copy of the lib we just twaked.

OSO_PROJECT=$(pwd)
cd ..
mkdir test-oso
cd test-oso

go mod init github.com/<yourusername>/test-oso # TODO: replace with your username
go mod edit -replace github.com/osohq/go-oso=${OSO_PROJECT}/languages/go  # this will tell Go to use local copy
go get github.com/osohq/go-oso@v0.11.2  # add oso to requirements, 

Just create small example that imports OSO and build it. If it works, you should be fine. Create main.go with the content:

package main

import (
	"fmt"

	"github.com/osohq/go-oso"
)

func main() {
	o, _ := oso.NewOso()
	fmt.Println(o)
}

Then try to build it with

go build

If you do not get any errors, that should be it.

Thanks

Thanks to awesome OSO engineering crew for quick response and gudance on how to make this work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment