Skip to content

Commit

Permalink
Merge pull request #13 from automerge/small-binaries
Browse files Browse the repository at this point in the history
Rebuild with new smaller shared libraries
  • Loading branch information
ConradIrwin authored Mar 22, 2023
2 parents 5c3bfd4 + 085490d commit 552165d
Show file tree
Hide file tree
Showing 17 changed files with 73 additions and 24 deletions.
2 changes: 1 addition & 1 deletion deps/automerge-rs
Binary file removed deps/darwin_amd64/libautomerge_core.a
Binary file not shown.
Binary file removed deps/darwin_arm64/libautomerge_core.a
Binary file not shown.
13 changes: 13 additions & 0 deletions deps/deps.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Package deps exists because shared libraries must be in the same directory
// as a go package for `go mod vendor` to work.
// (c.f. https://github.com/golang/go/issues/26366#issuecomment-405683150)
package deps

/*
#cgo LDFLAGS: -L${SRCDIR}
#cgo darwin,arm64 LDFLAGS: -lautomerge_core_darwin_arm64
#cgo darwin,amd64 LDFLAGS: -lautomerge_core_darwin_amd64
#cgo linux,arm64 LDFLAGS: -lautomerge_core_linux_arm64 -lm
#cgo linux,amd64 LDFLAGS: -lautomerge_core_linux_amd64 -lm
*/
import "C"
Binary file added deps/libautomerge_core_darwin_amd64.a
Binary file not shown.
Binary file added deps/libautomerge_core_darwin_arm64.a
Binary file not shown.
Binary file added deps/libautomerge_core_linux_amd64.a
Binary file not shown.
Binary file added deps/libautomerge_core_linux_arm64.a
Binary file not shown.
Binary file removed deps/linux_amd64/libautomerge_core.a
Binary file not shown.
Binary file removed deps/linux_arm64/libautomerge_core.a
Binary file not shown.
33 changes: 17 additions & 16 deletions deps/rebuild.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@ then
exit 1
fi

which cargo || (
which cargo || which cmake || (
echo "Missing dependencies!"
echo "See https://github.com/automerge/automerge-rs for instructions on setting up the build environment."
exit 1
)

# [email protected] is broken on my machine: https://github.com/cross-rs/cross/issues/1214
which cross || cargo install [email protected]

deps="$(realpath "$(dirname "$0")")"
automerge_c="$deps/automerge-rs/rust/automerge-c"

Expand All @@ -33,24 +30,28 @@ elif [[ "$1" != "" ]]; then
fi

mkdir -p "$deps/build"
mkdir -p "$deps/darwin_arm64"
mkdir -p "$deps/darwin_amd64"
mkdir -p "$deps/linux_arm64"
mkdir -p "$deps/linux_amd64"

CBINDGEN_TARGET_DIR="$deps/.." cargo build -r --manifest-path="$automerge_c/Cargo.toml" --target aarch64-apple-darwin --target-dir "$deps/build"
cp "$deps/build/aarch64-apple-darwin/release/libautomerge_core.a" "$deps/darwin_arm64/"
cmake -B "$deps/build" -S "$automerge_c"
cmake --build "$deps/build"
cp "$deps/build/include/automerge-c/automerge.h" "$deps/.."
cp "$deps/build/Cargo/target/aarch64-apple-darwin/release/libautomerge_core.a" "$deps/libautomerge_core_darwin_arm64.a"

if [[ "$1" == "local" ]]; then
exit
fi

cargo build -r --manifest-path="$automerge_c/Cargo.toml" --target x86_64-apple-darwin --target-dir "$deps/build"
cp "$deps/build/x86_64-apple-darwin/release/libautomerge_core.a" "$deps/darwin_amd64/"
# [email protected] is broken on my machine: https://github.com/cross-rs/cross/issues/1214
which cross || cargo install [email protected]

function build() {
target="$1"
output="$2"

cross build -r --manifest-path="$automerge_c/Cargo.toml" --target aarch64-unknown-linux-gnu --target-dir "$deps/build"
cp "$deps/build/aarch64-unknown-linux-gnu/release/libautomerge_core.a" "$deps/linux_arm64/"
RUSTFLAGS="-C panic=abort" cross +nightly build --release --manifest-path="$automerge_c/Cargo.toml" -Z build-std=std,panic_abort --target "$target" --target-dir "$deps/build"

cross build -r --manifest-path="$automerge_c/Cargo.toml" --target x86_64-unknown-linux-gnu --target-dir "$deps/build"
cp "$deps/build/x86_64-unknown-linux-gnu/release/libautomerge_core.a" "$deps/linux_amd64/"
cp "$deps/build/$target/release/libautomerge_core.a" "$deps/$output"
}

build x86_64-apple-darwin libautomerge_core_darwin_amd64.a
build aarch64-unknown-linux-gnu libautomerge_core_linux_arm64.a
build x86_64-unknown-linux-gnu libautomerge_core_linux_amd64.a
2 changes: 1 addition & 1 deletion deps/test_all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set -e
set -x

cd "$(dirname "$(go env GOMOD)")"
GOOS=darwin GOARCH=arm64 go test
GOOS=darwin GOARCH=arm64 go test ./...
CGO_ENABLED=1 GOOS=darwin GOARCH=amd64 go test
docker run --platform linux/arm64 -v "$(realpath):/automerge" -it --workdir /automerge golang go test
docker run --platform linux/amd64 -v "$(realpath):/automerge" -it --workdir /automerge golang go test
23 changes: 23 additions & 0 deletions deps/vendor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package deps

import "os"
import "os/exec"
import "testing"

func TestVendored(t *testing.T) {
os.Chdir("vendor_test")

run := func(c string, args ...string) {
cmd := exec.Command(c, args...)
output, err := cmd.CombinedOutput()
if err != nil {
os.Stdout.Write(output)
t.Fatal(err)
}
}

run("go", "mod", "tidy")
run("go", "mod", "vendor")
run("go", "test")
run("git", "checkout", "go.mod")
}
2 changes: 2 additions & 0 deletions deps/vendor_test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vendor/
go.sum
6 changes: 6 additions & 0 deletions deps/vendor_test/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
go 1.19

module github.com/automerge/vendor-test

require github.com/automerge/automerge-go v0.0.0
replace github.com/automerge/automerge-go => ../../
8 changes: 8 additions & 0 deletions deps/vendor_test/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package main

import "github.com/automerge/automerge-go"
import "testing"

func TestWorking(t *testing.T) {
automerge.New()
}
8 changes: 2 additions & 6 deletions result.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
package automerge

/*
#cgo LDFLAGS: -lautomerge_core
#cgo darwin,arm64 LDFLAGS: -L${SRCDIR}/deps/darwin_arm64
#cgo darwin,amd64 LDFLAGS: -L${SRCDIR}/deps/darwin_amd64
#cgo linux,arm64 LDFLAGS: -L${SRCDIR}/deps/linux_arm64 -lm
#cgo linux,amd64 LDFLAGS: -L${SRCDIR}/deps/linux_amd64 -lm
#include "automerge.h"
*/
import "C"
import (
"fmt"
"runtime"

_ "github.com/automerge/automerge-go/deps"
)

// result wraps an AMresult, and arranges for it to be AMfree'd after
Expand Down

0 comments on commit 552165d

Please sign in to comment.