|
| 1 | +#!/bin/bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +root_path=$(git rev-parse --show-toplevel) |
| 5 | + |
| 6 | +git clone --depth=1 --branch sdk-bindings --single-branch https://github.com/iotaledger/iota-rust-sdk.git "$root_path/clients/bindings/iota-rust-sdk" || true |
| 7 | + |
| 8 | +iota_binding_dir="$root_path/clients/bindings" |
| 9 | +iota_go_ffi_dir="$iota_binding_dir/iota_sdk_ffi" |
| 10 | +iota_go_ffi_file="$iota_go_ffi_dir/iota_sdk_ffi.go" |
| 11 | + |
| 12 | +iota_rust_sdk_path="$iota_binding_dir/iota-rust-sdk" |
| 13 | +go_ffi_dir="$iota_rust_sdk_path/bindings/go/iota_sdk_ffi/iota_sdk_ffi" |
| 14 | +go_ffi_file="$go_ffi_dir/iota_sdk_ffi.go" |
| 15 | + |
| 16 | +go_ffi_target_dir="$root_path/clients/bindings/iota-rust-sdk/target" |
| 17 | + |
| 18 | +cd "$iota_rust_sdk_path" |
| 19 | +cargo build --all-features -p iota-sdk-ffi --lib --release |
| 20 | + |
| 21 | +lib_base="$iota_rust_sdk_path/target/release/libiota_sdk_ffi" |
| 22 | +case "$(uname -s)" in |
| 23 | + Darwin*) ext="dylib" ;; |
| 24 | + Linux*) ext="so" ;; |
| 25 | + MINGW*|MSYS*|CYGWIN*) ext="dll" ;; |
| 26 | +*) echo "Unsupported OS: $(uname -s)"; exit 1 ;; |
| 27 | +esac |
| 28 | +lib_path="${lib_base}.${ext}" |
| 29 | + |
| 30 | +command -v uniffi-bindgen-go >/dev/null || { |
| 31 | +echo "uniffi-bindgen-go not found in PATH" >&2 |
| 32 | +exit 1 |
| 33 | +} |
| 34 | +[[ -f "$lib_path" ]] || { |
| 35 | +echo "Library not found: $lib_path" >&2 |
| 36 | +exit 1 |
| 37 | +} |
| 38 | + |
| 39 | +# Clean up old generated files before generating new ones |
| 40 | +rm -rf "$root_path/clients/bindings/iota_sdk_ffi" |
| 41 | + |
| 42 | +uniffi-bindgen-go --library "$lib_path" --out-dir "$iota_binding_dir" --no-format |
| 43 | +echo "Generated Go bindings to $iota_binding_dir using $lib_path" |
| 44 | + |
| 45 | +if [[ -f "$iota_go_ffi_file" ]]; then |
| 46 | + # Only replace if the placeholder include line exists |
| 47 | + |
| 48 | + if grep -qE '^// #include <iota_sdk_ffi\.h>$' "$iota_go_ffi_file"; then |
| 49 | + # Replace the single-line include with a full cgo block |
| 50 | + # Note: Escape $ in ${SRCDIR} as \${SRCDIR} so Perl doesn't interpolate it. |
| 51 | + perl -0777 -pe 's{// #include <iota_sdk_ffi\.h>}{/*\n#cgo darwin LDFLAGS: -L\${SRCDIR}/../iota-rust-sdk/target/release -liota_sdk_ffi -Wl,-rpath,\${SRCDIR}/../iota-rust-sdk/target/release\n#cgo linux LDFLAGS: -L\${SRCDIR}/../iota-rust-sdk/target/release -liota_sdk_ffi -Wl,-rpath,\${SRCDIR}/../iota-rust-sdk/target/release\n#cgo windows LDFLAGS: -L\${SRCDIR}/../iota-rust-sdk/target/release -liota_sdk_ffi\n#include "iota_sdk_ffi.h"\n*/}g' -i.bak "$iota_go_ffi_file" |
| 52 | + rm -f "$iota_go_ffi_file.bak" |
| 53 | + echo "Patched cgo block in: $iota_go_ffi_file" |
| 54 | + else |
| 55 | + echo "No placeholder include found; skipping patch: $iota_go_ffi_file" |
| 56 | + fi |
| 57 | +else |
| 58 | + echo "File not found: $iota_go_ffi_file" >&2 |
| 59 | + exit 1 |
| 60 | +fi |
| 61 | + |
| 62 | + |
| 63 | +# cp -R $go_ffi_target_dir $root_path/clients/bindings/iota_sdk_ffi |
| 64 | + |
| 65 | + |
| 66 | + |
0 commit comments