diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d81b3918..6d93249b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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: diff --git a/.gitignore b/.gitignore index d2b38d5d..9960f900 100644 --- a/.gitignore +++ b/.gitignore @@ -105,7 +105,8 @@ docs/_build/ .python-version # Go -go/iroh-node-go +iroh-node-go +/iroh-go/iroh/ffi/ # Python -python/iroh-data \ No newline at end of file +python/iroh-data diff --git a/README.md b/README.md index c476e98e..399d8e27 100644 --- a/README.md +++ b/README.md @@ -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 +``` + +### 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 ``` @@ -69,15 +144,23 @@ go where `/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 ``` + + +### 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. diff --git a/build_go_linux.sh b/build_go_linux.sh new file mode 100755 index 00000000..3807d5bd --- /dev/null +++ b/build_go_linux.sh @@ -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 /\/\*\n#cgo CFLAGS: -I.\/ffi\n#cgo LDFLAGS: -liroh -L.\/ffi\n#include \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 diff --git a/build_go_mac.sh b/build_go_mac.sh new file mode 100755 index 00000000..5c48c0a3 --- /dev/null +++ b/build_go_mac.sh @@ -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 /\/\*\n#cgo CFLAGS: -I.\/ffi\n#cgo LDFLAGS: -liroh -L.\/ffi\n#include \n\*\//" $IROH_GO_FILE diff --git a/go/blob_test.go b/iroh-go/blob_test.go similarity index 99% rename from go/blob_test.go rename to iroh-go/blob_test.go index de78ebef..5c9d307c 100644 --- a/go/blob_test.go +++ b/iroh-go/blob_test.go @@ -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" ) diff --git a/go/doc_test.go b/iroh-go/doc_test.go similarity index 99% rename from go/doc_test.go rename to iroh-go/doc_test.go index 77bedc8b..5d379e55 100644 --- a/go/doc_test.go +++ b/iroh-go/doc_test.go @@ -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" ) diff --git a/go/go.mod b/iroh-go/go.mod similarity index 63% rename from go/go.mod rename to iroh-go/go.mod index 4bccd046..d71a3770 100644 --- a/go/go.mod +++ b/iroh-go/go.mod @@ -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 ) diff --git a/go/go.sum b/iroh-go/go.sum similarity index 88% rename from go/go.sum rename to iroh-go/go.sum index 8cf66553..fa4b6e68 100644 --- a/go/go.sum +++ b/iroh-go/go.sum @@ -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= diff --git a/go/iroh/iroh.c b/iroh-go/iroh/iroh.c similarity index 100% rename from go/iroh/iroh.c rename to iroh-go/iroh/iroh.c diff --git a/go/iroh/iroh.go b/iroh-go/iroh/iroh.go similarity index 99% rename from go/iroh/iroh.go rename to iroh-go/iroh/iroh.go index ffd31c35..8d8080d1 100644 --- a/go/iroh/iroh.go +++ b/iroh-go/iroh/iroh.go @@ -1,6 +1,10 @@ package iroh -// #include +/* +#cgo CFLAGS: -I./ffi +#cgo LDFLAGS: -liroh -L./ffi +#include +*/ import "C" import ( diff --git a/go/iroh/iroh.h b/iroh-go/iroh/iroh.h similarity index 100% rename from go/iroh/iroh.h rename to iroh-go/iroh/iroh.h diff --git a/go/key_test.go b/iroh-go/key_test.go similarity index 95% rename from go/key_test.go rename to iroh-go/key_test.go index 4bb9c909..cc40794a 100644 --- a/go/key_test.go +++ b/iroh-go/key_test.go @@ -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" ) diff --git a/go/lib_test.go b/iroh-go/lib_test.go similarity index 95% rename from go/lib_test.go rename to iroh-go/lib_test.go index 22b037e8..a2f06cea 100644 --- a/go/lib_test.go +++ b/iroh-go/lib_test.go @@ -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" ) diff --git a/go/main.go b/iroh-go/main.go similarity index 97% rename from go/main.go rename to iroh-go/main.go index a7a10234..3e4cdfd1 100644 --- a/go/main.go +++ b/iroh-go/main.go @@ -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() { diff --git a/go/net_test.go b/iroh-go/net_test.go similarity index 98% rename from go/net_test.go rename to iroh-go/net_test.go index 441c71be..475b7f6e 100644 --- a/go/net_test.go +++ b/iroh-go/net_test.go @@ -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" ) diff --git a/go/node_test.go b/iroh-go/node_test.go similarity index 97% rename from go/node_test.go rename to iroh-go/node_test.go index b4dd8ec4..40185850 100644 --- a/go/node_test.go +++ b/iroh-go/node_test.go @@ -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" ) diff --git a/make_go.sh b/make_go.sh deleted file mode 100755 index ea8f7589..00000000 --- a/make_go.sh +++ /dev/null @@ -1,5 +0,0 @@ -set -eu - -cargo build -rm -rf ./go/iroh/* -uniffi-bindgen-go ./src/iroh.udl --out-dir ./go diff --git a/src/node.rs b/src/node.rs index df6c51c8..67ed3dcb 100644 --- a/src/node.rs +++ b/src/node.rs @@ -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)?;