From 9cffed97ac4e6c2683923f1cfd7c5c864064aa57 Mon Sep 17 00:00:00 2001 From: siq1 Date: Thu, 22 Aug 2024 03:35:05 +0800 Subject: [PATCH] add multiple platform support in go wrapper, and add tests for that --- .github/workflows/tests.yml | 10 ++++++++++ ecgo/compile/wrapper/wrapper.go | 20 +++++++++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 9ad66fd..987365b 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -30,6 +30,16 @@ jobs: with: workspaces: "expander_compiler -> expander_compiler/target" - run: cargo test --manifest-path=expander_compiler/Cargo.toml + Test-go-mac: + runs-on: macos-latest + steps: + - uses: styfle/cancel-workflow-action@0.11.0 + - uses: actions/checkout@v4 + - name: Setup Go 1.21.x + uses: actions/setup-go@v5 + with: + go-version: '1.21.x' + - run: cd ecgo && go test ./test/ Test-go-linux: runs-on: ubuntu-latest steps: diff --git a/ecgo/compile/wrapper/wrapper.go b/ecgo/compile/wrapper/wrapper.go index fa87eef..fad3365 100644 --- a/ecgo/compile/wrapper/wrapper.go +++ b/ecgo/compile/wrapper/wrapper.go @@ -79,10 +79,24 @@ func getUrl(url string) ([]byte, error) { return body, nil } +func getLibName() string { + switch runtime.GOOS { + case "darwin": + if runtime.GOARCH == "arm64" { + return "libec_go_lib.dylib" + } + case "linux": + if runtime.GOARCH == "amd64" { + return "libec_go_lib.so" + } + } + panic(fmt.Sprintf("unsupported platform %s %s", runtime.GOOS, runtime.GOARCH)) +} + func downloadLib(path string) { log := logger.Logger() log.Info().Msg("Downloading rust libs ...") - err := downloadFile("https://github.com/PolyhedraZK/ExpanderCompilerCollection/raw/rust-built-libs/libec_go_lib.so", path) + err := downloadFile("https://github.com/PolyhedraZK/ExpanderCompilerCollection/raw/rust-built-libs/"+getLibName(), path) if err != nil { os.Remove(path) panic(err) @@ -143,11 +157,11 @@ func initCompilePtr() { return } curDir := currentFileDirectory() - soPath := filepath.Join(curDir, "libec_go_lib.so") + soPath := filepath.Join(curDir, getLibName()) updateLib(soPath) handle := C.dlopen(C.CString(soPath), C.RTLD_LAZY) if handle == nil { - panic("failed to load libec_go_lib.so") + panic("failed to load libec_go_lib") } compilePtr = C.dlsym(handle, C.CString("compile")) if compilePtr == nil {