Skip to content

Commit

Permalink
test: cgo + zigcc cross compile
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy committed Sep 14, 2024
1 parent ec2d9af commit 9b4b114
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 22 deletions.
32 changes: 29 additions & 3 deletions .github/workflows/go-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ jobs:
uses: actions/setup-go@v2
with:
go-version: 1.22
- uses: korandoru/setup-zig@v1
with:
zig-version: master
- name: Ready msys2
uses: msys2/setup-msys2@v2
with:
Expand All @@ -49,3 +46,32 @@ jobs:
if: matrix.os == 'windows-latest'
- name: Go code test
run: go test ./...

cross-compile-test:
runs-on: macos-12
defaults:
run:
working-directory: "go"
steps:
- name: Git checkout
uses: actions/checkout@v2
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.22
- uses: korandoru/setup-zig@v1
with:
zig-version: master
- name: Set output
id: macos_sdk
run: echo "path=$(xcrun --show-sdk-path)" >> $GITHUB_OUTPUT
- name: Go cross compile test
run: |
export SDK_PATH=$(xcrun --show-sdk-path)
CGO_ENABLED=1 GOOS=windows GOARCH=amd64 CC='zig c++ -target x86_64-windows-gnu' go build ./...
CGO_ENABLED=1 GOOS=linux GOARCH=amd64 CC='zig c++ -target x86_64-linux-musl' go build ./...
CGO_ENABLED=1 GOOS=linux GOARCH=arm64 CC='zig c++ -target x86_64-linux-musl' go build ./...
CGO_ENABLED=1 GOOS=darwin GOARCH=amd64 CC='zig c++ -target x86_64-macos-none -F'"${SDK_PATH}"'/System/Library/Frameworks' go build ./...
CGO_ENABLED=1 GOOS=darwin GOARCH=arm64 CC='zig c++ -target x86_64-macos-none -F'"${SDK_PATH}"'/System/Library/Frameworks' go build ./...
env:
SDK_PATH: ${{ steps.macos_sdk.outputs.path }}
29 changes: 10 additions & 19 deletions go/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,16 @@ package plugin
/*
#include <stdint.h>
#include <stdlib.h>
char* kcl_go_capi_InvokeJsonProxy(
#ifndef KCL_API_EXTERN
#if defined(_WIN32) && !defined(__MINGW32__) && !defined(LIBKCL_STATIC)
#define KCL_API_EXTERN __declspec(dllimport)
#else
#define KCL_API_EXTERN
#endif
#endif
KCL_API_EXTERN char* kcl_go_capi_InvokeJsonProxy(
char* method,
char* args_json,
char* kwargs_json
Expand All @@ -26,24 +35,6 @@ import (

const CgoEnabled = true

//export kcl_go_capi_InvokeJsonProxy
func kcl_go_capi_InvokeJsonProxy(_method, _args_json, _kwargs_json *C.char) (result_json *C.char) {
var method, args_json, kwargs_json string

if _method != nil {
method = C.GoString(_method)
}
if _args_json != nil {
args_json = C.GoString(_args_json)
}
if _kwargs_json != nil {
kwargs_json = C.GoString(_kwargs_json)
}

result := InvokeJson(method, args_json, kwargs_json)
return c_String_new(result)
}

func GetInvokeJsonProxyPtr() uint64 {
ptr := uint64(C.kcl_go_capi_getInvokeJsonProxyPtr())
return ptr
Expand Down
24 changes: 24 additions & 0 deletions go/plugin/proxy_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//go:build linux || darwin
// +build linux darwin

package plugin

import "C"

//export kcl_go_capi_InvokeJsonProxy
func kcl_go_capi_InvokeJsonProxy(_method, _args_json, _kwargs_json *C.char) (result_json *C.char) {
var method, args_json, kwargs_json string

if _method != nil {
method = C.GoString(_method)
}
if _args_json != nil {
args_json = C.GoString(_args_json)
}
if _kwargs_json != nil {
kwargs_json = C.GoString(_kwargs_json)
}

result := InvokeJson(method, args_json, kwargs_json)
return c_String_new(result)
}
23 changes: 23 additions & 0 deletions go/plugin/proxy_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//go:build windows
// +build windows

package plugin

import "C"

func kcl_go_capi_InvokeJsonProxy(_method, _args_json, _kwargs_json *C.char) (result_json *C.char) {
var method, args_json, kwargs_json string

if _method != nil {
method = C.GoString(_method)
}
if _args_json != nil {
args_json = C.GoString(_args_json)
}
if _kwargs_json != nil {
kwargs_json = C.GoString(_kwargs_json)
}

result := InvokeJson(method, args_json, kwargs_json)
return c_String_new(result)
}

0 comments on commit 9b4b114

Please sign in to comment.