test crosscompile feature with goloader
This commit is contained in:
parent
31d3dadc52
commit
e2557da13f
8 changed files with 74 additions and 46 deletions
|
@ -1,5 +1,11 @@
|
|||
This branch uses unofficial goloader implementation, see https://pkg.go.dev/github.com/eh-steve/goloader, https://github.com/eh-steve/goloader and https://git.aliberksandikci.com.tr/Liderahenk/ahenk-docs/src/branch/main/dev/resources.md#goloader-plugin-logic for more information
|
||||
|
||||
|
||||
---
|
||||
#### When building for windows:
|
||||
- change `.vscode/settings.json` to `"go.buildFlags": ["-tags=windows"],`
|
||||
- change `cmd/go-loader-test/plugin-manager.go` to `BuildEnv: []string{"GOOS=windows"},`
|
||||
|
||||
---
|
||||
# Pros
|
||||
- Fast, Lightweight
|
||||
|
|
|
@ -10,9 +10,9 @@ func PluginManager(params ...string) {
|
|||
conf := jit.BuildConfig{
|
||||
KeepTempFiles: false, // Files are copied/written to a temp dir to ensure it is writable. This retains the temporary copies
|
||||
ExtraBuildFlags: []string{"-x"}, // Flags passed to go build command
|
||||
BuildEnv: []string{"GOOS=windows"}, // Env vars to set for go build toolchain
|
||||
BuildEnv: []string{"GOOS=darwin"}, // Env vars to set for go build toolchain
|
||||
TmpDir: "/tmp/inner", // To control where temporary files are copied
|
||||
DebugLog: true, //
|
||||
DebugLog: false, //
|
||||
}
|
||||
|
||||
loadable, err := jit.BuildGoPackage(conf, "plugins/tmptest/")
|
||||
|
@ -26,12 +26,6 @@ func PluginManager(params ...string) {
|
|||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
// defer func() {
|
||||
// err = module.Unload()
|
||||
// if err != nil {
|
||||
// panic(err)
|
||||
// }
|
||||
// }()
|
||||
switch f := symbols["MyFunc"].(type) {
|
||||
case func([]byte) (interface{}, error):
|
||||
result, err := f([]byte(`{"k":"v"}`))
|
||||
|
@ -48,4 +42,39 @@ func PluginManager(params ...string) {
|
|||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
|
||||
loadable2, err := jit.BuildGoPackage(conf, "plugins/addsymbol/")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
module2, err := loadable2.Load()
|
||||
// module.SymbolsByPkg is a map[string]map[string]interface{} of all packages and their exported functions and global vars
|
||||
symbols2 := module2.SymbolsByPkg[loadable2.ImportPath]
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
switch f := symbols2["Symbol"].(type) {
|
||||
case func(a, b string) (interface{}, error):
|
||||
result, err := f("a", "b")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println(result)
|
||||
default:
|
||||
fmt.Println(f)
|
||||
panic("Function signature was not what was expected")
|
||||
}
|
||||
|
||||
err = module2.Unload()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,26 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"plugin"
|
||||
|
||||
"git.aliberksandikci.com.tr/asandikci/go-loader-test/pkg/utils"
|
||||
)
|
||||
|
||||
func LoadPlugin(plugName string, chn chan interface{}) {
|
||||
|
||||
plug, err := plugin.Open(PluginDir + plugName + ".so")
|
||||
utils.Check(err)
|
||||
|
||||
symPlug, err := plug.Lookup(utils.FirstUpperEN(plugName) + "Connect")
|
||||
utils.Check(err)
|
||||
|
||||
var plugOut interface{}
|
||||
plugOut, ok := symPlug.(interface{})
|
||||
if !ok {
|
||||
fmt.Println("unexpected type from module symbol")
|
||||
os.Exit(1)
|
||||
}
|
||||
chn <- plugOut
|
||||
}
|
BIN
go-loader-test
Executable file
BIN
go-loader-test
Executable file
Binary file not shown.
5
plugins/addsymbol/symbol.go
Normal file
5
plugins/addsymbol/symbol.go
Normal file
|
@ -0,0 +1,5 @@
|
|||
package addsymbol
|
||||
|
||||
func Symbol(a, b string) (interface{}, error) {
|
||||
return symbol(a, b), nil
|
||||
}
|
7
plugins/addsymbol/symbol_linux.go
Normal file
7
plugins/addsymbol/symbol_linux.go
Normal file
|
@ -0,0 +1,7 @@
|
|||
// go:build linux && !windows
|
||||
|
||||
package addsymbol
|
||||
|
||||
func symbol(a, b string) string {
|
||||
return a + "+" + b + "=linux"
|
||||
}
|
7
plugins/addsymbol/symbol_windows.go
Normal file
7
plugins/addsymbol/symbol_windows.go
Normal file
|
@ -0,0 +1,7 @@
|
|||
// go:build windows
|
||||
|
||||
package addsymbol
|
||||
|
||||
func symbol(a, b string) string {
|
||||
return a + "-" + b + "=windows"
|
||||
}
|
|
@ -1,11 +1,11 @@
|
|||
package mypackage
|
||||
|
||||
func Info() map[string]interface{} {
|
||||
return map[string]interface{}{
|
||||
"name": "tmptest",
|
||||
"version": "1.0.2",
|
||||
"support": "any",
|
||||
"description": "Temporary testing",
|
||||
"developer": "asandikci@aliberksandikci.com.tr",
|
||||
}
|
||||
}
|
||||
// func Info() map[string]interface{} {
|
||||
// return map[string]interface{}{
|
||||
// "name": "tmptest",
|
||||
// "version": "1.0.2",
|
||||
// "support": "any",
|
||||
// "description": "Temporary testing",
|
||||
// "developer": "asandikci@aliberksandikci.com.tr",
|
||||
// }
|
||||
// }
|
||||
|
|
Loading…
Reference in a new issue