From 2916245115648f8cdf1b45b5db8dcb0c2267edbf Mon Sep 17 00:00:00 2001 From: Marc Nuri Date: Thu, 23 Jan 2025 15:56:33 +0100 Subject: [PATCH] feat(wasi): initial wasi compilation --- .gitignore | 1 + Makefile | 9 ++++++ native/wasm/main.go | 72 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 native/wasm/main.go diff --git a/.gitignore b/.gitignore index 849708fc..e094e31d 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ target/ *.so *.dylib *.h +*.wasm *.test diff --git a/Makefile b/Makefile index 9c885bf7..55b8b99b 100644 --- a/Makefile +++ b/Makefile @@ -42,6 +42,15 @@ build-native-cross-platform: go install src.techknowlogick.com/xgo@latest xgo $(COMMON_BUILD_ARGS) -out native/out/helm --targets */arm64,*/amd64 ./native +.PHONY: build-native-wasi +build-native-wasi: + #cd native && GOOS=wasip1 GOARCH=wasm go build -o ./out/helm.wasm ./wasm/main.go + # Andrea recommends using TinyGo + # Doesn't work, need to find the right combination of ENV variables + #cd native && GOOS=wasip1 GOARCH=wasm tinygo build -o ./out/helm.wasm + # Working Version: + cd native && tinygo build -target=wasi -o ./out/helm.wasm ./wasm/main.go + .PHONY: build-java build-java: mvn $(MAVEN_OPTIONS) clean verify diff --git a/native/wasm/main.go b/native/wasm/main.go new file mode 100644 index 00000000..3c87fc63 --- /dev/null +++ b/native/wasm/main.go @@ -0,0 +1,72 @@ +/* + * Copyright 2024 Marc Nuri + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package main + +/* +#include +#include + +typedef struct { + char* out; + char* err; + char* stdOut; + char* stdErr; +} Result; +*/ +import "C" +import ( + "fmt" + "strings" + "unsafe" +) + +//export ExportedFunction +func ExportedFunction() int64 { + fmt.Println("Hello") + return 0 +} + +//export CString +func CString() *C.char { + return C.CString("A C String") +} + +//export CFree +func CFree(pointer unsafe.Pointer) { + C.free(pointer) +} + +//export CExport +func CExport() *C.Result { + return &C.Result{ + out: toCString("out-string"), + err: toCString("err-string"), + stdOut: toCString("stdOut-string"), + stdErr: toCString("stdErr-string"), + } +} + +func toCString(str string) *C.char { + if len(strings.TrimSpace(str)) == 0 { + return nil + } + return C.CString(str) +} + +// Needed for compilation (only) +func main() { +}