Skip to content

Commit

Permalink
go: windows support for purego + macos-14 set go version
Browse files Browse the repository at this point in the history
  • Loading branch information
YOU54F committed Feb 29, 2024
1 parent bda532f commit e35ce30
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 9 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,8 @@ jobs:
run: rm 'C:\Windows\System32\bash.exe'
name: 'remove wsl bash'
- uses: actions/setup-go@v5
with:
go-version: '1.21'
- run: brew install protobuf
if: matrix.os == 'macos-14'
- run: make get_pact_ffi
Expand All @@ -583,6 +585,8 @@ jobs:
run: rm 'C:\Windows\System32\bash.exe'
name: 'remove wsl bash'
- uses: actions/setup-go@v5
with:
go-version: '1.21'
- run: brew install protobuf
if: matrix.os == 'macos-14'
- run: make get_pact_ffi
Expand Down
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -388,11 +388,11 @@ alpine_go:
alpine_go_purego:
docker run --platform=${DOCKER_DEFAULT_PLATFORM} -v ${PWD}:/app --rm alpine sh -c 'apk add make go && cd /app && CGO_ENABLED=0 make go_purego'
go_hello_ffi:
cd go && $(GO_CMD) build helloFfi.go
$(LOAD_PATH) go/helloFfi
cd go/cgo && $(GO_CMD) build helloFfi.go
$(LOAD_PATH) go/cgo/helloFfi
go_purego_hello_ffi:
cd go && $(GO_CMD) build helloFfiPureGo.go
$(LOAD_PATH) go/helloFfiPureGo
cd go/purego && $(GO_CMD) build
$(LOAD_PATH) go/purego/hello_ffi

go_purego: go_purego_hello_ffi
go: go_hello_ffi
Expand Down
9 changes: 4 additions & 5 deletions go/helloFfi.go → go/cgo/helloFfi.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package main

// #cgo CFLAGS: -g -I./..
// #cgo LDFLAGS: -lpact_ffi -L./..
// #include "../pact.h"
// #cgo CFLAGS: -g -I./../..
// #cgo LDFLAGS: -lpact_ffi -L./../..
// #include "../../pact.h"
import "C"
import (
"fmt"
Expand All @@ -17,10 +17,9 @@ func main() {
C.pactffi_log_message(C.CString("pact-go-ffi"), C.CString("INFO"), C.CString(fmt.Sprintf("hello from ffi version: %s", C.GoString(version))))
}


// https://github.com/golang/go/issues/51007

// * Executing task: cd go; go build
// * Executing task: cd go; go build

// # github.com/you54f/hello_ffi
// C:\Program Files\Go\pkg\tool\windows_amd64\link.exe: running gcc failed: exit status 1
Expand Down
File renamed without changes.
File renamed without changes.
73 changes: 73 additions & 0 deletions go/purego/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//go:build darwin || linux || windows

package main

import (
"fmt"
"os"
"path/filepath"
"runtime"

"github.com/ebitengine/purego"
)

func getSystemLibrary() string {
switch runtime.GOOS {
case "darwin":
return "libpact_ffi.dylib"
case "linux":
return "libpact_ffi.so"
case "windows":
return "pact_ffi.dll"
default:
panic(fmt.Errorf("GOOS=%s is not supported", runtime.GOOS))
}
}

func main() {
libpact_ffi, err := purego.Dlopen(filepath.Join(os.Getenv("PACT_LD_LIBRARY_PATH"), getSystemLibrary()), purego.RTLD_NOW|purego.RTLD_GLOBAL)
if err != nil {
panic(err)
}

// General Functions
var pactffi_version func() string
purego.RegisterLibFunc(&pactffi_version, libpact_ffi, "pactffi_version")

// General Check
print(pactffi_version())

// Logging Functions
var pactffi_log_to_stdout func(int) int
purego.RegisterLibFunc(&pactffi_log_to_stdout, libpact_ffi, "pactffi_log_to_stdout")
var pactffi_log_to_buffer func(int) int
purego.RegisterLibFunc(&pactffi_log_to_buffer, libpact_ffi, "pactffi_log_to_buffer")
var pactffi_log_to_file func(string, int) int
purego.RegisterLibFunc(&pactffi_log_to_file, libpact_ffi, "pactffi_log_to_file")

// Logging Check
pactffi_log_to_stdout(4)
// pactffi_log_to_file("pact.log", 5)

// Verifier Functions
var pactffi_verifier_new_for_application func(string, string) uintptr
purego.RegisterLibFunc(&pactffi_verifier_new_for_application, libpact_ffi, "pactffi_verifier_new_for_application")
var pactffi_verifier_shutdown func(uintptr)
purego.RegisterLibFunc(&pactffi_verifier_shutdown, libpact_ffi, "pactffi_verifier_shutdown")
var pactffi_verifier_set_provider_info func(uintptr, string, string, string, int, string)
purego.RegisterLibFunc(&pactffi_verifier_set_provider_info, libpact_ffi, "pactffi_verifier_set_provider_info")
var pactffi_verifier_execute func(uintptr) int
purego.RegisterLibFunc(&pactffi_verifier_execute, libpact_ffi, "pactffi_verifier_execute")
var pactffi_verifier_add_file_source func(uintptr, string)
purego.RegisterLibFunc(&pactffi_verifier_add_file_source, libpact_ffi, "pactffi_verifier_add_file_source")

// Verifier Check
var verifier_handle = pactffi_verifier_new_for_application("pact-purego", "0.0.1")
pactffi_verifier_set_provider_info(verifier_handle, "grpc-provider", "http", "localhost", 1234, "/")
pactffi_verifier_add_file_source(verifier_handle, "no_pact.json")
// pactffi_verifier_add_file_source(verifier_handle, "pact.json")
// pactffi_verifier_add_file_source(verifier_handle, "../pacts/Consumer-Alice Service.json")
// pactffi_verifier_add_file_source(verifier_handle, "../pacts/grpc-consumer-perl-area-calculator-provider.json")
pactffi_verifier_execute(verifier_handle)
pactffi_verifier_shutdown(verifier_handle)
}
10 changes: 10 additions & 0 deletions go/purego/main_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// SPDX-License-Identifier: Apache-2.0
//go:build darwin || linux

package main

import "github.com/ebitengine/purego"

func openLibrary(name string) (uintptr, error) {
return purego.Dlopen(name, purego.RTLD_NOW|purego.RTLD_GLOBAL)
}
9 changes: 9 additions & 0 deletions go/purego/main_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: Apache-2.0
package main

import "golang.org/x/sys/windows"

func openLibrary(name string) (uintptr, error) {
handle, err := windows.LoadLibrary(name)
return uintptr(handle), err
}

0 comments on commit e35ce30

Please sign in to comment.