Skip to content

Commit

Permalink
cmd/mecha: add ability to compile Zig modules to mecha build command
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <ron@hybridgroup.com>
  • Loading branch information
deadprogram committed Apr 1, 2024
1 parent 0995e2f commit 44bff01
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 6 deletions.
31 changes: 25 additions & 6 deletions cmd/mecha/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,33 +50,33 @@ GLOBAL OPTIONS:
--version, -v print the version
```

## New project
### New project

```bash
mecha new project example.com/myproject
```

## New project based on template
### New project based on template

```bash
mecha new project -t=blink example.com/myproject
```

## New module
### New module

```bash
mecha new module mymodule

```

## New module based on template
### New module based on template

```bash
mecha new module -t=blink mymodule

```

## Build modules in current project
### Build modules in current project

```bash
mecha build
Expand All @@ -88,8 +88,27 @@ or
mecha build modules
```

## Build application for current project
### Build application for current project

```bash
mecha build project
```

## Using `mecha` with Rust

If you want to use the `mecha` command with Rust, you will need to install Rust as follows.

- First install Rust by using the instructions here: https://www.rust-lang.org/tools/install

- Then, install the Rust `wasm32-unknown-unknown` target.

```bash
rustup target add wasm32-unknown-unknown
```
Any Rust modules in your project's `modules` directory should be automatically built when you run the `mecha build` command.

## Using `mecha` with Zig

If you want to use the `mecha` command with Zig, you will need to install Zig 0.11.0 by using the instructions here: https://ziglang.org/learn/getting-started/#installing-zig

Any Zig modules in your project's `modules` directory should be automatically built when you run the `mecha build` command.
40 changes: 40 additions & 0 deletions cmd/mecha/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@ func buildModules(cCtx *cli.Context) error {
continue
}

// check if there is a build.zig file in the directory
_, err = os.Stat(filepath.Join(wd, "modules", f.Name(), "build.zig"))
if err == nil {
if err := buildZigModule(filepath.Join(wd, "modules"), f.Name()); err != nil {
return err
}
continue
}

// no go.mod or Cargo.toml file found
fmt.Println("No module files found in", f.Name(), "skipping...")
}
Expand Down Expand Up @@ -176,6 +185,37 @@ func buildRustModule(modulesPath, name string) error {
return nil
}

func buildZigModule(modulesPath, name string) error {
s := spinner.New(spinner.CharSets[5], 100*time.Millisecond, spinner.WithWriter(os.Stdout))
s.Suffix = " Building Zig module " + name
s.FinalMSG = "Done.\n"
s.Start()
defer s.Stop()

fmt.Println("Building Zig module", name)
modulePath := filepath.Join(modulesPath, name)
os.Chdir(modulePath)
defer os.Chdir("../..")

cmd := exec.Command("zig", "build")

var stdoutBuf, stderrBuf bytes.Buffer
cmd.Stdout = io.MultiWriter(&spinWriter{s, os.Stdout, false}, &stdoutBuf)
cmd.Stderr = io.MultiWriter(&spinWriter{s, os.Stderr, false}, &stderrBuf)

if err := cmd.Run(); err != nil {
fmt.Printf("zig build error %s: %v\n", modulePath, err)
os.Exit(1)
}

if err := copyFile(filepath.Join(modulePath, "zig-out", "lib", name+".wasm"), filepath.Join(modulesPath, name+".wasm")); err != nil {
fmt.Printf("copy file error %s: %v\n", modulePath, err)
os.Exit(1)
}

return nil
}

// copyFile copies the given file or directory from src to dst. It can copy over
// a possibly already existing file (but not directory) at the destination.
func copyFile(src, dst string) error {
Expand Down

0 comments on commit 44bff01

Please sign in to comment.