Skip to content

Commit

Permalink
build: rework
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 aa88a3b commit 8b1ba66
Show file tree
Hide file tree
Showing 26 changed files with 269 additions and 96 deletions.
94 changes: 70 additions & 24 deletions .github/workflows/west.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ jobs:
strategy:
matrix:
target:
- aarch64-unknown-linux-musl
- aarch64-linux-android
- aarch64-unknown-linux-musl
- riscv64gc-unknown-linux-gnu
- x86_64-pc-windows-gnu
- x86_64-unknown-linux-musl
Expand All @@ -49,22 +49,24 @@ jobs:
matrix:
config:
- os: macos-13
lib: x86_64-darwin
target: x86_64-apple-darwin
sdk: 10.12

- os: macos-14
lib: aarch64-darwin
target: aarch64-apple-darwin
sdk: 11.0

name: west-${{ matrix.config.target }}
runs-on: ${{ matrix.config.os }}
steps:
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
- uses: ./.github/actions/install-nix
with:
cachixAuthToken: '${{ secrets.CACHIX_AUTH_TOKEN }}'
- run: nix build -L
- run: nix run -L --inputs-from . 'nixpkgs#coreutils' -- --coreutils-prog=cp -RLv ./result artifact
- run: rustup show
- uses: Swatinem/[email protected]
- run: cargo build -p west-sys --release --target ${{ matrix.config.target }}
env:
MACOSX_DEPLOYMENT_TARGET: ${{ matrix.config.sdk }}
- run: mkdir -p artifact/lib
- run: mv target/${{ matrix.config.target }}/release/libwest_sys.a artifact/lib/libwest_sys.a
- uses: actions/upload-artifact@v4
with:
name: west-${{ matrix.config.target }}
Expand Down Expand Up @@ -93,26 +95,43 @@ jobs:
- os: ubuntu-latest
lib: x86_64-linux
target: x86_64-unknown-linux-musl
shell: bash

- os: windows-latest
lib: x86_64-windows
target: x86_64-pc-windows-gnu
shell: msys2

- os: macos-13
lib: x86_64-darwin
target: x86_64-apple-darwin
shell: bash

- os: macos-14
lib: aarch64-darwin
target: aarch64-apple-darwin
shell: bash

name: test-release-${{ matrix.config.target }}
name: test-release (${{ matrix.config.os }})
needs:
- build-ffi
- build-ffi-darwin
- build-wasm
runs-on: ${{ matrix.config.os }}
defaults:
run:
shell: ${{ matrix.config.shell }} {0}
steps:
- uses: msys2/setup-msys2@v2
if: matrix.config.os == 'windows-latest'
with:
update: true
install: mingw-w64-x86_64-toolchain
msystem: MINGW64
path-type: inherit
- run: git config --global core.autocrlf input
if: matrix.config.os == 'windows-latest'

- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
- uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8
with:
Expand All @@ -124,46 +143,73 @@ jobs:
- run: mv lib/libwest_sys.a "lib/${{ matrix.config.lib }}/libwest.a"
- uses: actions/setup-go@v5
with:
go-version-file: 'go.mod'
go-version-file: go.mod
if: matrix.config.os != 'windows-latest'
- run: rustup show
if: matrix.config.os != 'windows-latest'
- uses: Swatinem/[email protected]
if: matrix.config.os != 'windows-latest'
- uses: cargo-bins/[email protected]
- run: cargo binstall -y [email protected]
- run: go generate ./tests/go/...
- run: go test ./...
- run: go test -failfast ./...
env:
GOGC: 1
WEST_LOG: trace
continue-on-error: ${{ matrix.config.os == 'ubuntu-latest' || matrix.config.os == 'windows-latest' }} # TODO: remove
continue-on-error: ${{ matrix.config.os == 'ubuntu-latest' }} # TODO: remove
- run: git diff --exit-code

test-dev:
strategy:
matrix:
os:
- ubuntu-latest
- windows-latest
- macos-13
- macos-14

name: test-dev-${{ matrix.os }}
runs-on: ${{ matrix.os }}
config:
- os: ubuntu-latest
shell: bash

- os: windows-latest
shell: msys2

- os: macos-13
shell: bash

- os: macos-14
shell: bash

runs-on: ${{ matrix.config.os }}
defaults:
run:
shell: ${{ matrix.config.shell }} {0}
steps:
- uses: msys2/setup-msys2@v2
if: matrix.config.os == 'windows-latest'
with:
update: true
install: mingw-w64-x86_64-toolchain
msystem: MINGW64
path-type: inherit
- run: git config --global core.autocrlf input
if: matrix.config.os == 'windows-latest'

- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
- uses: actions/setup-go@v5
with:
go-version-file: 'go.mod'
go-version-file: go.mod

- run: rustup set default-host x86_64-pc-windows-gnu
if: matrix.config.os == 'windows-latest'
- run: rustup show

- uses: Swatinem/[email protected]
- uses: cargo-bins/[email protected]
- run: cargo binstall -y [email protected]
- run: cargo test --workspace --all-targets
if: matrix.config.os != 'windows-latest'
- run: go generate -tags=dev ./...
- run: go test -tags=dev ./...
- run: go test -failfast -tags=dev ./...
env:
GOGC: 1
WEST_LOG: trace
continue-on-error: ${{ matrix.os == 'ubuntu-latest' || matrix.os == 'windows-latest' }} # TODO: remove
continue-on-error: ${{ matrix.config.os == 'ubuntu-latest' }} # TODO: remove
- run: git diff --exit-code

gofmt:
Expand All @@ -172,7 +218,7 @@ jobs:
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
- uses: actions/setup-go@v5
with:
go-version-file: 'go.mod'
go-version-file: go.mod
- run: gofmt -w -s **/*.go
- run: git diff --exit-code

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/target
*.wasm
*.a
*.lib
west_bindings_test.go
/go/internal/tests/*/bindings/*
19 changes: 0 additions & 19 deletions build.go

This file was deleted.

11 changes: 0 additions & 11 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")
Loading

0 comments on commit 8b1ba66

Please sign in to comment.