Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

write build_go_mac.sh file to set up go package so it can be installed more seamlessly #79

Merged
merged 17 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,18 @@ jobs:
env:
XDG_CACHE_HOME: /root/.cache
HOME: /root #added based on https://github.com/actions/setup-go/issues/116
- name: Build rust
run: cargo build
- name: install uniffi-bindgen
run: cargo install uniffi-bindgen-go --git https://github.com/NordSecurity/uniffi-bindgen-go --tag v0.2.0+v0.25.0
- name: Build iroh-go
run: ./build_go_linux.sh
- name: Go test
env:
XDG_CACHE_HOME: /root/.cache
HOME: /root #added based on https://github.com/actions/setup-go/issues/116
run: |
cd ./go && \
LD_LIBRARY_PATH="${LD_LIBRARY_PATH:-}:/root/actions-runner/_work/iroh-ffi/iroh-ffi/target/debug/" \
CGO_LDFLAGS="-liroh -L /root/actions-runner/_work/iroh-ffi/iroh-ffi/target/debug" \
cd ./iroh-go && \
LD_LIBRARY_PATH="${LD_LIBRARY_PATH:-}:./iroh/ffi" \
CGO_LDFLAGS="-liroh -L ./iroh/ffi" \
go test ./...

build_and_test_python:
Expand Down
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ docs/_build/
.python-version

# Go
go/iroh-node-go
iroh-node-go
/iroh-go/iroh/ffi/

# Python
python/iroh-data
python/iroh-data
91 changes: 87 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,83 @@ docker run --rm -v $(pwd):/mnt -w /mnt quay.io/pypa/manylinux2014_x86_64 /mnt/bu

## Go

### Running
### Mac

#### Building
Ensure you have golang & rust installed.

Install `uniffi-bindgen-go`:

```
cargo install uniffi-bindgen-go --git https://github.com/NordSecurity/uniffi-bindgen-go --tag v0.2.0+v0.25.0
```

Build the bindings:
```
./build_go_mac.sh
```

Or build in release mode:
```
./build_go_mac.sh release
```

#### Running
Once you've built the bindings, run go normally:
```
cd iroh-go
go test ./...
```

### Linux
Ensure you have golang & rust installed.

Install `uniffi-bindgen-go`:

```
cargo install uniffi-bindgen-go --git https://github.com/NordSecurity/uniffi-bindgen-go --tag v0.2.0+v0.25.0
```

Build the bindings:
```
./build_go_linux.sh
```

Or in release mode:
```
./build_go_linux.sh release
```

#### Running

If you've used the build script to build the go bindings, it will also place the files in the correct locations.

Add the following to let go know where the dynamically linked files are located:

```
cd iroh-go
LD_LIBRARY_PATH="${LD_LIBRARY_PATH:-}:.iroh/ffi" \
CGO_LDFLAGS="-liroh -L .iroh/ffi" \
go <actual go command to build or run>
```

### Windows

### Building
Ensure you have golang & rust installed.

Install `uniffi-bindgen-go`:

```
cargo install uniffi-bindgen-go --git https://github.com/NordSecurity/uniffi-bindgen-go --tag v0.2.0+v0.25.0
```

Build the bindings:
```
cargo build
```

### Running
To make sure everything go needs to find is included the following is needed

```
Expand All @@ -69,15 +144,23 @@ go <actual go command to build or run>

where `<binaries path` needs to be replaced with the absolute path to where the rust build output is. Eg `/<path to repo>/iroh-ffi/target/debug` in debug mode.

#### Running

### Updating the bindings
If you've used the build script to build the go bindings, it will also place the files in the correct locations.

Install `uniffi-bindgen-go`:
Add the following to let go know where the dynamically linked files are located:

```
cargo install uniffi-bindgen-go --git https://github.com/NordSecurity/uniffi-bindgen-go --tag v0.2.0+v0.25.0
cd iroh-go
LD_LIBRARY_PATH="${LD_LIBRARY_PATH:-}:.iroh/ffi" \
CGO_LDFLAGS="-liroh -L .iroh/ffi" \
go <actual go command to build or run>
```



### Updating the bindings

# Developers
Check our our [DEVELOPERS.md](DEVELOPERS.md) for guides on how to translate from the iroh rust API to the iroh FFI API, as well as how to set up testing for golang and python.

Expand Down
42 changes: 42 additions & 0 deletions build_go_linux.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
set -eu

MODE=""
DIR_NAME="debug"
if [ "$#" -eq 1 ]; then
if [[ $1 == "release" ]]; then
MODE="--release"
DIR_NAME="release"
elif [[ $1 != "debug" ]]; then
echo "Unknown mode '$1'. Options are 'release' and 'debug'. Defaults to 'debug'"
exit
fi
fi

# the path to the new folder we are including
GO_DIR="./iroh-go"
INCLUDE_PATH="${GO_DIR}/iroh/ffi"
IROH_GO_PATH="${GO_DIR}/iroh/*"
UDL_PATH="./src/iroh.udl"
IROH_GO_FILE="${GO_DIR}/iroh/iroh.go"

rm -rf $IROH_GO_PATH

# build iroh-ffi and save the assets to ./go/iroh/include
cargo build $MODE

uniffi-bindgen-go $UDL_PATH --out-dir $GO_DIR

# TODO why does this needs to exist twice? once in the path and the other in
# the "deps" directory?
# move needed files over
mkdir ${INCLUDE_PATH}
cp "target/${DIR_NAME}/libiroh.so" "${INCLUDE_PATH}/libiroh.so"
mkdir "${INCLUDE_PATH}/deps"
cp "${INCLUDE_PATH}/libiroh.so" "${INCLUDE_PATH}/deps/libiroh.so"

sed -i "s/\/\/ #include <iroh.h>/\/\*\n#cgo CFLAGS: -I.\/ffi\n#cgo LDFLAGS: -liroh -L.\/ffi\n#include <iroh.h>\n\*\//" $IROH_GO_FILE

# to run you need to let the linker know where the linked library files are:
# LD_LIBRARY_PATH="${LD_LIBRARY_PATH:-}:./iroh/ffi" \
# CGO_LDFLAGS="-liroh -L ./iroh/ffi" \
# go <actual go command to build or run>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this stay here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, unfortunately from my linux experimentation, it won't recognize the linked files, no matter where I put libiroh.so. Either the box I was using was ignoring it, or maybe something was wrong with how it was set up for dynamic linking, but I couldn't get it to recognize the cgo flag. I needed to still add the LD_LIBRARY_PATH manually. So until that's confirmed to work I want to leave these instructions in.

37 changes: 37 additions & 0 deletions build_go_mac.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
set -eu

MODE=""
DIR_NAME="debug"
if [ "$#" -eq 1 ]; then
if [[ $1 == "release" ]]; then
MODE="--release"
DIR_NAME="release"
elif [[ $1 != "debug" ]]; then
echo "Unknown mode '$1'. Options are 'release' and 'debug'. Defaults to 'debug'"
exit
fi
fi

# the path to the new folder we are including
GO_DIR="./iroh-go"
INCLUDE_PATH="${GO_DIR}/iroh/ffi"
IROH_GO_PATH="${GO_DIR}/iroh/*"
UDL_PATH="./src/iroh.udl"
IROH_GO_FILE="${GO_DIR}/iroh/iroh.go"

rm -rf $IROH_GO_PATH

# build iroh-ffi and save the assets to ./go/iroh/include
cargo build $MODE

# TODO why does this needs to exist twice? once in the path and the other in
# the "deps" directory?
# move needed files over
mkdir ${INCLUDE_PATH}
cp "target/${DIR_NAME}/libiroh.dylib" "${INCLUDE_PATH}/libiroh.dylib"
mkdir "${INCLUDE_PATH}/deps"
cp "${INCLUDE_PATH}/libiroh.dylib" "${INCLUDE_PATH}/deps/libiroh.dylib"

uniffi-bindgen-go $UDL_PATH --out-dir $GO_DIR

sed -i '' "s/\/\/ #include <iroh.h>/\/\*\n#cgo CFLAGS: -I.\/ffi\n#cgo LDFLAGS: -liroh -L.\/ffi\n#include <iroh.h>\n\*\//" $IROH_GO_FILE
3 changes: 2 additions & 1 deletion go/blob_test.go → iroh-go/blob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
"strconv"
"testing"

"github.com/n0-computer/iroh-ffi/iroh"
"github.com/n0-computer/iroh-ffi/iroh-go/iroh"

"github.com/stretchr/testify/assert"
)

Expand Down
3 changes: 2 additions & 1 deletion go/doc_test.go → iroh-go/doc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
"path/filepath"
"testing"

"github.com/n0-computer/iroh-ffi/iroh"
"github.com/n0-computer/iroh-ffi/iroh-go/iroh"

"github.com/stretchr/testify/assert"
)

Expand Down
5 changes: 3 additions & 2 deletions go/go.mod → iroh-go/go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
module github.com/n0-computer/iroh-ffi
module github.com/n0-computer/iroh-ffi/iroh-go

go 1.19

require github.com/stretchr/testify v1.8.4

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.8.4 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
1 change: 1 addition & 0 deletions go/go.sum → iroh-go/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
File renamed without changes.
6 changes: 5 additions & 1 deletion go/iroh/iroh.go → iroh-go/iroh/iroh.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package iroh

// #include <iroh.h>
/*
#cgo CFLAGS: -I./ffi
#cgo LDFLAGS: -liroh -L./ffi
#include <iroh.h>
*/
import "C"

import (
Expand Down
File renamed without changes.
3 changes: 2 additions & 1 deletion go/key_test.go → iroh-go/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ package main
import (
"testing"

"github.com/n0-computer/iroh-ffi/iroh"
"github.com/n0-computer/iroh-ffi/iroh-go/iroh"

"github.com/stretchr/testify/assert"
)

Expand Down
3 changes: 2 additions & 1 deletion go/lib_test.go → iroh-go/lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package main
import (
"testing"

"github.com/n0-computer/iroh-ffi/iroh"
"github.com/n0-computer/iroh-ffi/iroh-go/iroh"

"github.com/stretchr/testify/assert"
)

Expand Down
2 changes: 1 addition & 1 deletion go/main.go → iroh-go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"os"

"github.com/n0-computer/iroh-ffi/iroh"
"github.com/n0-computer/iroh-ffi/iroh-go/iroh"
)

func main() {
Expand Down
3 changes: 2 additions & 1 deletion go/net_test.go → iroh-go/net_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package main
import (
"testing"

"github.com/n0-computer/iroh-ffi/iroh"
"github.com/n0-computer/iroh-ffi/iroh-go/iroh"

"github.com/stretchr/testify/assert"
)

Expand Down
3 changes: 2 additions & 1 deletion go/node_test.go → iroh-go/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
"os"
"testing"

"github.com/n0-computer/iroh-ffi/iroh"
"github.com/n0-computer/iroh-ffi/iroh-go/iroh"

"github.com/stretchr/testify/assert"
)

Expand Down
5 changes: 0 additions & 5 deletions make_go.sh

This file was deleted.

1 change: 1 addition & 0 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ impl IrohNode {
// TODO: store and load keypair
let secret_key = SecretKey::generate();

tokio::fs::create_dir_all(&path).await?;
let docs_path = path.join("docs.db");
let docs = iroh::sync::store::fs::Store::new(&docs_path)?;

Expand Down
Loading