Skip to content

Commit

Permalink
Fix go mod vendor
Browse files Browse the repository at this point in the history
The actual fix is relatively simple (we moved the shared libraries to
/deps and made /deps a go package). To prevent accidental breakage in
the future I also added a test.
  • Loading branch information
ConradIrwin committed Mar 22, 2023
1 parent 4629528 commit 085490d
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 16 deletions.
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"
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
13 changes: 4 additions & 9 deletions deps/rebuild.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,11 @@ 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"

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/darwin_arm64"
cp "$deps/build/Cargo/target/aarch64-apple-darwin/release/libautomerge_core.a" "$deps/libautomerge_core_darwin_arm64.a"

if [[ "$1" == "local" ]]; then
exit
Expand All @@ -53,10 +49,9 @@ function build() {

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"

mkdir -p "$deps/$output"
cp "$deps/build/$target/release/libautomerge_core.a" "$deps/$output"
}

build x86_64-apple-darwin darwin_amd64
build aarch64-unknown-linux-gnu linux_arm64
build x86_64-unknown-linux-gnu linux_amd64
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 085490d

Please sign in to comment.