diff --git a/build.go b/build.go deleted file mode 100644 index 5572a83..0000000 --- a/build.go +++ /dev/null @@ -1,18 +0,0 @@ -//go:build !dev - -//go:generate cargo build -p west-sys --release -//go:generate cargo build -p west-passthrough --target wasm32-unknown-unknown --release -//go:generate wasm-tools component new target/wasm32-unknown-unknown/release/west_passthrough.wasm -o lib/passthrough.wasm - -package west - -// #cgo LDFLAGS: -lwest -// #cgo android,arm64 LDFLAGS: -L${SRCDIR}/lib/aarch64-android -// #cgo darwin,amd64 LDFLAGS: -L${SRCDIR}/lib/x86_64-darwin -// #cgo darwin,arm64 LDFLAGS: -L${SRCDIR}/lib/aarch64-darwin -// #cgo linux,amd64 LDFLAGS: -L${SRCDIR}/lib/x86_64-linux -// #cgo linux,arm64 LDFLAGS: -L${SRCDIR}/lib/aarch64-linux -// #cgo linux,riscv64 LDFLAGS: -L${SRCDIR}/lib/riscv64-linux -// #cgo windows,amd64 LDFLAGS: -L${SRCDIR}/lib/x86_64-windows -// #cgo windows,arm64 LDFLAGS: -L${SRCDIR}/lib/aarch64-windows -import "C" diff --git a/build_dev.go b/build_dev.go deleted file mode 100644 index 44032ae..0000000 --- a/build_dev.go +++ /dev/null @@ -1,10 +0,0 @@ -//go:build dev - -//go:generate cargo build -p west-sys -//go:generate cargo build -p west-passthrough --target wasm32-unknown-unknown -//go:generate wasm-tools component new target/wasm32-unknown-unknown/debug/west_passthrough.wasm -o lib/passthrough.wasm - -package west - -// #cgo LDFLAGS: -L${SRCDIR}/target/debug -lwest_sys -import "C" diff --git a/gen.go b/gen.go new file mode 100644 index 0000000..9bf662c --- /dev/null +++ b/gen.go @@ -0,0 +1,5 @@ +//go:build !dev + +//go:generate go run ./internal/generate + +package west diff --git a/gen_dev.go b/gen_dev.go new file mode 100644 index 0000000..73ac83d --- /dev/null +++ b/gen_dev.go @@ -0,0 +1,5 @@ +//go:build dev + +//go:generate go run -tags=dev ./internal/generate + +package west diff --git a/internal/generate/build.go b/internal/generate/build.go new file mode 100644 index 0000000..b41eae6 --- /dev/null +++ b/internal/generate/build.go @@ -0,0 +1,22 @@ +//go:build !dev + +package main + +import ( + "path/filepath" +) + +func TargetPath(target string) string { + if target == "" { + return filepath.Join(TargetDir, "release") + } + return filepath.Join(TargetDir, target, "release") +} + +func CargoBuild(target string, args ...string) error { + base := []string{"build", "--release"} + if target != "" { + base = append(base, "--target", target) + } + return RunCargo(append(base, args...)...) +} diff --git a/internal/generate/build_dev.go b/internal/generate/build_dev.go new file mode 100644 index 0000000..87253e0 --- /dev/null +++ b/internal/generate/build_dev.go @@ -0,0 +1,22 @@ +//go:build dev + +package main + +import ( + "path/filepath" +) + +func TargetPath(target string) string { + if target == "" { + return filepath.Join(TargetDir, "debug") + } + return filepath.Join(TargetDir, target, "debug") +} + +func CargoBuild(target string, args ...string) error { + base := []string{"build"} + if target != "" { + base = append(base, "--target", target) + } + return RunCargo(append(base, args...)...) +} diff --git a/internal/generate/west.go b/internal/generate/west.go new file mode 100644 index 0000000..019980f --- /dev/null +++ b/internal/generate/west.go @@ -0,0 +1,77 @@ +package main + +import ( + "flag" + "log" + "os" + "os/exec" + "path/filepath" + "runtime" +) + +var ( + Root = func() string { + _, f, _, _ := runtime.Caller(0) + return filepath.Join(f, "..", "..", "..") + }() + + TargetDir = filepath.Join(Root, "target") + LibDir = filepath.Join(Root, "lib") +) + +func init() { + log.SetFlags(0) + flag.Parse() +} + +func Run(cmd *exec.Cmd) error { + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + return cmd.Run() +} + +func RunCargo(args ...string) error { + return Run(exec.Command("cargo", args...)) +} + +func main() { + if err := Run(exec.Command( + "go", + "run", + "github.com/bytecodealliance/wasm-tools-go/cmd/wit-bindgen-go", + "generate", + "-w", + "imports", + "-o", + "bindings", + filepath.Join(Root, "wit"), + )); err != nil { + log.Fatalf("failed to generate WIT bindings: %s", err) + } + if err := CargoBuild(CARGO_TARGET, "-p", "west-sys"); err != nil { + log.Fatalf("failed to build FFI: %s", err) + } + libTarget := filepath.Join(LibTargetDir, "libwest.a") + if err := os.RemoveAll(libTarget); err != nil { + log.Fatalf("failed to remove FFI: %s", err) + } + if err := os.Link( + filepath.Join(TargetPath(CARGO_TARGET), "libwest_sys.a"), + libTarget, + ); err != nil { + log.Fatalf("failed to hard link FFI: %s", err) + } + if err := CargoBuild("wasm32-unknown-unknown", "-p", "west-passthrough"); err != nil { + log.Fatalf("failed to build passthrough component: %s", err) + } + if err := Run(exec.Command( + "wasm-tools", + "component", + "new", + filepath.Join(TargetPath("wasm32-unknown-unknown"), "west_passthrough.wasm"), + "-o", + filepath.Join(LibDir, "passthrough.wasm"), + )); err != nil { + log.Fatalf("failed to create passthrough component: %s", err) + } +} diff --git a/internal/generate/west_android_arm64.go b/internal/generate/west_android_arm64.go new file mode 100644 index 0000000..4efcd4a --- /dev/null +++ b/internal/generate/west_android_arm64.go @@ -0,0 +1,7 @@ +package main + +import "path/filepath" + +const CARGO_TARGET = "aarch64-linux-android" + +var LibTargetDir = filepath.Join(LibDir, "aarch64-android") diff --git a/internal/generate/west_darwin_amd64.go b/internal/generate/west_darwin_amd64.go new file mode 100644 index 0000000..13216fc --- /dev/null +++ b/internal/generate/west_darwin_amd64.go @@ -0,0 +1,7 @@ +package main + +import "path/filepath" + +const CARGO_TARGET = "x86_64-apple-darwin" + +var LibTargetDir = filepath.Join(LibDir, "x86_64-darwin") diff --git a/internal/generate/west_darwin_arm64.go b/internal/generate/west_darwin_arm64.go new file mode 100644 index 0000000..b59fc68 --- /dev/null +++ b/internal/generate/west_darwin_arm64.go @@ -0,0 +1,7 @@ +package main + +import "path/filepath" + +const CARGO_TARGET = "aarch64-apple-darwin" + +var LibTargetDir = filepath.Join(LibDir, "aarch64-darwin") diff --git a/internal/generate/west_linux_amd64.go b/internal/generate/west_linux_amd64.go new file mode 100644 index 0000000..944a4b1 --- /dev/null +++ b/internal/generate/west_linux_amd64.go @@ -0,0 +1,7 @@ +package main + +import "path/filepath" + +const CARGO_TARGET = "x86_64-unknown-linux-gnu" + +var LibTargetDir = filepath.Join(LibDir, "x86_64-linux") diff --git a/internal/generate/west_linux_arm64.go b/internal/generate/west_linux_arm64.go new file mode 100644 index 0000000..79fde29 --- /dev/null +++ b/internal/generate/west_linux_arm64.go @@ -0,0 +1,7 @@ +package main + +import "path/filepath" + +const CARGO_TARGET = "aarch64-unknown-linux-gnu" + +var LibTargetDir = filepath.Join(LibDir, "aarch64-linux") diff --git a/internal/generate/west_linux_riscv64.go b/internal/generate/west_linux_riscv64.go new file mode 100644 index 0000000..5cb5501 --- /dev/null +++ b/internal/generate/west_linux_riscv64.go @@ -0,0 +1,7 @@ +package main + +import "path/filepath" + +const CARGO_TARGET = "riscv64gc-unknown-linux-gnu" + +var LibTargetDir = filepath.Join(LibDir, "riscv64-linux") diff --git a/internal/generate/west_windows_amd64.go b/internal/generate/west_windows_amd64.go new file mode 100644 index 0000000..4184e50 --- /dev/null +++ b/internal/generate/west_windows_amd64.go @@ -0,0 +1,7 @@ +package main + +import "path/filepath" + +const CARGO_TARGET = "x86_64-pc-windows-gnu" + +var LibTargetDir = filepath.Join(LibDir, "x86_64-windows") diff --git a/internal/generate/west_windows_arm64.go b/internal/generate/west_windows_arm64.go new file mode 100644 index 0000000..feafa1a --- /dev/null +++ b/internal/generate/west_windows_arm64.go @@ -0,0 +1,7 @@ +package main + +import "path/filepath" + +const CARGO_TARGET = "aarch64-pc-windows-gnullvm" + +var LibTargetDir = filepath.Join(LibDir, "aarch64-windows") diff --git a/west.go b/west.go index 34e37e8..26b2799 100644 --- a/west.go +++ b/west.go @@ -1,9 +1,16 @@ -//go:generate go run github.com/bytecodealliance/wasm-tools-go/cmd/wit-bindgen-go generate -w imports -o bindings ./wit - package west -// #cgo !windows LDFLAGS: -lm -ldl -pthread -// #cgo windows LDFLAGS: -lws2_32 -lole32 -lntdll -lbcrypt -luserenv +// #cgo LDFLAGS: -lwest +// #cgo android,arm64 LDFLAGS: -L${SRCDIR}/lib/aarch64-android +// #cgo darwin,amd64 LDFLAGS: -L${SRCDIR}/lib/x86_64-darwin +// #cgo darwin,arm64 LDFLAGS: -L${SRCDIR}/lib/aarch64-darwin +// #cgo linux,amd64 LDFLAGS: -L${SRCDIR}/lib/x86_64-linux +// #cgo linux,arm64 LDFLAGS: -L${SRCDIR}/lib/aarch64-linux +// #cgo linux,riscv64 LDFLAGS: -L${SRCDIR}/lib/riscv64-linux +// #cgo windows,amd64 LDFLAGS: -L${SRCDIR}/lib/x86_64-windows +// #cgo windows,arm64 LDFLAGS: -L${SRCDIR}/lib/aarch64-windows +// #cgo !windows LDFLAGS: -lm -ldl -pthread +// #cgo windows LDFLAGS: -lws2_32 -lole32 -lntdll -lbcrypt -luserenv // #include "./include/west.h" // #include import "C" diff --git a/west_android_arm64.go b/west_android_arm64.go deleted file mode 100644 index a12e633..0000000 --- a/west_android_arm64.go +++ /dev/null @@ -1,5 +0,0 @@ -//go:build !dev - -//go:generate cp ./target/release/libwest_sys.a ./lib/aarch64-android/libwest.a - -package west diff --git a/west_darwin_amd64.go b/west_darwin_amd64.go deleted file mode 100644 index 1e5f1af..0000000 --- a/west_darwin_amd64.go +++ /dev/null @@ -1,5 +0,0 @@ -//go:build !dev - -//go:generate cp ./target/release/libwest_sys.a ./lib/x86_64-darwin/libwest.a - -package west diff --git a/west_darwin_arm64.go b/west_darwin_arm64.go deleted file mode 100644 index cf186ec..0000000 --- a/west_darwin_arm64.go +++ /dev/null @@ -1,5 +0,0 @@ -//go:build !dev - -//go:generate cp ./target/release/libwest_sys.a ./lib/aarch64-darwin/libwest.a - -package west diff --git a/west_linux_amd64.go b/west_linux_amd64.go deleted file mode 100644 index c513a93..0000000 --- a/west_linux_amd64.go +++ /dev/null @@ -1,5 +0,0 @@ -//go:build !dev - -//go:generate cp ./target/release/libwest_sys.a ./lib/x86_64-linux/libwest.a - -package west diff --git a/west_linux_arm64.go b/west_linux_arm64.go deleted file mode 100644 index 3a24e15..0000000 --- a/west_linux_arm64.go +++ /dev/null @@ -1,5 +0,0 @@ -//go:build !dev - -//go:generate cp ./target/release/libwest_sys.a ./lib/aarch64-linux/libwest.a - -package west diff --git a/west_linux_riscv64.go b/west_linux_riscv64.go deleted file mode 100644 index 8bb832d..0000000 --- a/west_linux_riscv64.go +++ /dev/null @@ -1,5 +0,0 @@ -//go:build !dev - -//go:generate cp ./target/release/libwest_sys.a ./lib/riscv64-linux/libwest.a - -package west diff --git a/west_windows_amd64.go b/west_windows_amd64.go deleted file mode 100644 index 841669c..0000000 --- a/west_windows_amd64.go +++ /dev/null @@ -1,5 +0,0 @@ -//go:build !dev - -//go:generate cp ./target/release/libwest_sys.a ./lib/x86_64-windows/libwest.a - -package west diff --git a/west_windows_arm64.go b/west_windows_arm64.go deleted file mode 100644 index e4385e1..0000000 --- a/west_windows_arm64.go +++ /dev/null @@ -1,5 +0,0 @@ -//go:build !dev - -//go:generate cp ./target/release/libwest_sys.a ./lib/aarch64-windows/libwest.a - -package west