提交 5c625055 编写于 作者: chai2010's avatar chai2010

优化命令行, 去掉对 clang 依赖

上级 f185a08b
......@@ -19,32 +19,25 @@ jobs:
uses: actions/setup-go@v2
with:
go-version: 1.17
- name: Setup Wasmer
uses: wasmerio/setup-wasmer@v1
- name: Set up Clang
uses: egor-tensin/setup-clang@v1
with:
version: latest
platform: x64
- run: wasmer -V
- run: go version
- run: go env
- run: clang --version
- run: go test -v -coverprofile=profile.cov ./...
- run: go install
- run: wa -h
- run: wa hello.wa
- run: wa run _examples/hello
- run: wa run _examples/prime
- run: wa run hello.wa
- run: wa wasm hello.wa
- run: wa build -o a.out.wat hello.wa
- run: wasmer a.out.wat
- uses: shogo82148/actions-goveralls@v1
......@@ -74,11 +67,11 @@ jobs:
- run: go install
- run: wa -h
#- run: wa hello.wa
#- run: wa run _examples/hello
#- run: wa run _examples/prime
- run: wa hello.wa
- run: wa run _examples/prime
- run: wa run hello.wa
- run: wa wasm hello.wa
- run: wa build -o a.out.wat hello.wa
- run: wasmer a.out.wat
......@@ -105,9 +98,9 @@ jobs:
- run: go install
- run: wa -h
#- run: wa hello.wa
#- run: wa run _examples/hello
#- run: wa run _examples/prime
- run: wa hello.wa
- run: wa run _examples/prime
- run: wa run hello.wa
- run: wa wasm hello.wa
- run: wa build -o a.out.wat hello.wa
- run: wasmer a.out.wat
......@@ -30,6 +30,7 @@ func RunWasm(filename string) (stdoutStderr []byte, err error) {
if stdoutStderr, err = RunWat2Wasm(filename, "-o", dst); err != nil {
return stdoutStderr, err
}
defer os.Remove(dst)
}
wasmBytes, err := os.ReadFile(dst)
......
......@@ -7,8 +7,9 @@ import (
"fmt"
"os"
"os/exec"
"runtime"
"runtime/debug"
"strings"
"time"
"github.com/wa-lang/wa/internal/3rdparty/cli"
"github.com/wa-lang/wa/internal/app"
......@@ -25,15 +26,20 @@ func main() {
return info.Main.Version
}
}
return "(devel)"
return "devel:" + time.Now().Format("2006-01-02+15:04:05")
}()
cliApp.Flags = []cli.Flag{
&cli.StringFlag{Name: "os", Usage: "set target OS", Value: runtime.GOOS},
&cli.StringFlag{Name: "arch", Usage: "set target Arch", Value: runtime.GOARCH},
&cli.StringFlag{Name: "clang", Usage: "set clang"},
&cli.BoolFlag{Name: "debug", Aliases: []string{"d"}, Usage: "set debug mode"},
&cli.StringFlag{Name: "trace", Aliases: []string{"t"}, Usage: "set trace mode (*|app|compiler|loader)"},
&cli.BoolFlag{
Name: "debug",
Aliases: []string{"d"},
Usage: "set debug mode",
},
&cli.StringFlag{
Name: "trace",
Aliases: []string{"t"},
Usage: "set trace mode (*|app|compiler|loader)",
},
}
cliApp.Before = func(c *cli.Context) error {
......@@ -101,39 +107,50 @@ func main() {
return nil
},
},
{
Name: "fmt",
Usage: "format Wa package sources",
Action: func(c *cli.Context) error {
waApp := app.NewApp(build_Options(c))
err := waApp.Fmt(c.Args().First())
if err != nil {
fmt.Println(err)
os.Exit(1)
}
return nil
},
},
{
Name: "run",
Usage: "compile and run Wa program",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "html",
Usage: "output html",
},
},
Action: func(c *cli.Context) error {
if c.NArg() == 0 {
fmt.Fprintf(os.Stderr, "no input file")
os.Exit(1)
}
waApp := app.NewApp(build_Options(c))
data, err := waApp.Run(c.Args().First(), nil)
if len(data) != 0 {
fmt.Print(string(data))
ctx := app.NewApp(build_Options(c))
output, err := ctx.WASM(c.Args().First())
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if errx, ok := err.(*exec.ExitError); ok {
os.Exit(errx.ExitCode())
outfile := "a.out.wat"
if !c.Bool("debug") {
defer os.Remove(outfile)
}
if err = os.WriteFile(outfile, []byte(output), 0666); err != nil {
fmt.Println(err)
os.Exit(1)
}
stdoutStderr, err := app.RunWasm(outfile)
if err != nil {
if len(stdoutStderr) > 0 {
fmt.Println(string(stdoutStderr))
}
fmt.Println(err)
os.Exit(1)
}
if len(stdoutStderr) > 0 {
fmt.Println(string(stdoutStderr))
}
return nil
},
},
......@@ -146,40 +163,46 @@ func main() {
Aliases: []string{"o"},
Usage: "set output file",
},
&cli.BoolFlag{
Name: "html",
Usage: "output html",
},
},
Action: func(c *cli.Context) error {
outfile := c.String("output")
if c.NArg() == 0 {
fmt.Fprintf(os.Stderr, "no input file")
os.Exit(1)
}
opt := build_Options(c)
ctx := app.NewApp(build_Options(c))
output, err := ctx.WASM(c.Args().First())
if err != nil {
fmt.Println(err)
os.Exit(1)
}
outfile := c.String("output")
if outfile == "" {
outfile = "a.out"
if opt.TargetOS == "windows" {
outfile += ".exe"
if outfile != "" && outfile != "-" {
if !strings.HasSuffix(outfile, ".wat") {
outfile += ".wat"
}
err := os.WriteFile(outfile, []byte(output), 0666)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
} else {
fmt.Println(string(output))
}
waApp := app.NewApp(opt)
data, err := waApp.Build(c.Args().First(), nil, outfile)
if len(data) != 0 {
fmt.Print(string(data))
}
if errx, ok := err.(*exec.ExitError); ok {
os.Exit(errx.ExitCode())
}
if err != nil {
fmt.Println(err)
}
return nil
},
},
{
Name: "lex",
Usage: "lex Wa source code and print token list",
Hidden: true,
Name: "lex",
Usage: "lex Wa source code and print token list",
Action: func(c *cli.Context) error {
if c.NArg() == 0 {
fmt.Fprintf(os.Stderr, "no input file")
......@@ -196,8 +219,9 @@ func main() {
},
},
{
Name: "ast",
Usage: "parse Wa source code and print ast",
Hidden: true,
Name: "ast",
Usage: "parse Wa source code and print ast",
Action: func(c *cli.Context) error {
if c.NArg() == 0 {
fmt.Fprintf(os.Stderr, "no input file")
......@@ -214,8 +238,9 @@ func main() {
},
},
{
Name: "ssa",
Usage: "print Wa ssa code",
Hidden: true,
Name: "ssa",
Usage: "print Wa ssa code",
Action: func(c *cli.Context) error {
if c.NArg() == 0 {
fmt.Fprintf(os.Stderr, "no input file")
......@@ -232,8 +257,9 @@ func main() {
},
},
{
Name: "cir",
Usage: "print cir code",
Hidden: true,
Name: "cir",
Usage: "print cir code",
Action: func(c *cli.Context) error {
if c.NArg() == 0 {
fmt.Fprintf(os.Stderr, "no input file")
......@@ -250,8 +276,9 @@ func main() {
},
},
{
Name: "asm",
Usage: "parse Wa and print ouput assembly code",
Hidden: true,
Name: "asm",
Usage: "parse Wa and print ouput assembly code",
Action: func(c *cli.Context) error {
if c.NArg() == 0 {
fmt.Fprintf(os.Stderr, "no input file")
......@@ -268,76 +295,23 @@ func main() {
},
},
{
Name: "wasm",
Usage: "parse Wa and print ouput WebAssembly text",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "output",
Aliases: []string{"o"},
Usage: "set output file",
Value: "a.out.wat",
},
&cli.BoolFlag{
Name: "run",
Usage: "run wat or wasm",
},
},
Name: "test",
Usage: "test packages",
Action: func(c *cli.Context) error {
outfile := c.String("output")
if c.NArg() == 0 {
fmt.Fprintf(os.Stderr, "no input file")
os.Exit(1)
}
ctx := app.NewApp(build_Options(c))
output, err := ctx.WASM(c.Args().First())
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if outfile != "" && outfile != "-" {
err := os.WriteFile(outfile, []byte(output), 0666)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
} else {
if c.Bool("run") {
outfile = "a.out.wat"
err := os.WriteFile(outfile, []byte(output), 0666)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
} else {
fmt.Println(string(output))
}
}
if c.Bool("run") {
stdoutStderr, err := app.RunWasm(outfile)
if err != nil {
if len(stdoutStderr) > 0 {
fmt.Println(string(stdoutStderr))
}
fmt.Println(err)
os.Exit(1)
}
if len(stdoutStderr) > 0 {
fmt.Println(string(stdoutStderr))
}
}
fmt.Println("TODO")
return nil
},
},
{
Name: "test",
Usage: "test packages",
Name: "fmt",
Usage: "format Wa package sources",
Action: func(c *cli.Context) error {
fmt.Println("TODO")
waApp := app.NewApp(build_Options(c))
err := waApp.Fmt(c.Args().First())
if err != nil {
fmt.Println(err)
os.Exit(1)
}
return nil
},
},
......@@ -376,9 +350,6 @@ func main() {
func build_Options(c *cli.Context) *app.Option {
return &app.Option{
Debug: c.Bool("debug"),
TargetOS: c.String("os"),
TargetArch: c.String("arch"),
Clang: c.String("clang"),
Debug: c.Bool("debug"),
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册