Skip to content

Commit

Permalink
rework build
Browse files Browse the repository at this point in the history
Signed-off-by: Roman Volosatovs <[email protected]>
  • Loading branch information
rvolosatovs committed Sep 19, 2024
1 parent a72248b commit 2e73cfa
Show file tree
Hide file tree
Showing 24 changed files with 198 additions and 72 deletions.
18 changes: 0 additions & 18 deletions build.go

This file was deleted.

10 changes: 0 additions & 10 deletions build_dev.go

This file was deleted.

5 changes: 5 additions & 0 deletions gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//go:build !dev

//go:generate go run ./internal/generate

package west
5 changes: 5 additions & 0 deletions gen_dev.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//go:build dev

//go:generate go run -tags=dev ./internal/generate

package west
22 changes: 22 additions & 0 deletions internal/generate/build.go
Original file line number Diff line number Diff line change
@@ -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...)...)
}
22 changes: 22 additions & 0 deletions internal/generate/build_dev.go
Original file line number Diff line number Diff line change
@@ -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...)...)
}
77 changes: 77 additions & 0 deletions internal/generate/west.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
7 changes: 7 additions & 0 deletions internal/generate/west_android_arm64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "path/filepath"

const CARGO_TARGET = "aarch64-linux-android"

var LibTargetDir = filepath.Join(LibDir, "aarch64-android")
7 changes: 7 additions & 0 deletions internal/generate/west_darwin_amd64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "path/filepath"

const CARGO_TARGET = "x86_64-apple-darwin"

var LibTargetDir = filepath.Join(LibDir, "x86_64-darwin")
7 changes: 7 additions & 0 deletions internal/generate/west_darwin_arm64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "path/filepath"

const CARGO_TARGET = "aarch64-apple-darwin"

var LibTargetDir = filepath.Join(LibDir, "aarch64-darwin")
7 changes: 7 additions & 0 deletions internal/generate/west_linux_amd64.go
Original file line number Diff line number Diff line change
@@ -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")
7 changes: 7 additions & 0 deletions internal/generate/west_linux_arm64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "path/filepath"

const CARGO_TARGET = "aarch64-unknown-linux-gnu"

var LibTargetDir = filepath.Join(LibDir, "aarch64-linux")
7 changes: 7 additions & 0 deletions internal/generate/west_linux_riscv64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "path/filepath"

const CARGO_TARGET = "riscv64gc-unknown-linux-gnu"

var LibTargetDir = filepath.Join(LibDir, "riscv64-linux")
7 changes: 7 additions & 0 deletions internal/generate/west_windows_amd64.go
Original file line number Diff line number Diff line change
@@ -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")
7 changes: 7 additions & 0 deletions internal/generate/west_windows_arm64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "path/filepath"

const CARGO_TARGET = "aarch64-pc-windows-gnullvm"

var LibTargetDir = filepath.Join(LibDir, "aarch64-windows")
15 changes: 11 additions & 4 deletions west.go
Original file line number Diff line number Diff line change
@@ -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 <stdlib.h>
import "C"
Expand Down
5 changes: 0 additions & 5 deletions west_android_arm64.go

This file was deleted.

5 changes: 0 additions & 5 deletions west_darwin_amd64.go

This file was deleted.

5 changes: 0 additions & 5 deletions west_darwin_arm64.go

This file was deleted.

5 changes: 0 additions & 5 deletions west_linux_amd64.go

This file was deleted.

5 changes: 0 additions & 5 deletions west_linux_arm64.go

This file was deleted.

5 changes: 0 additions & 5 deletions west_linux_riscv64.go

This file was deleted.

5 changes: 0 additions & 5 deletions west_windows_amd64.go

This file was deleted.

5 changes: 0 additions & 5 deletions west_windows_arm64.go

This file was deleted.

0 comments on commit 2e73cfa

Please sign in to comment.