mirror of
https://gitee.com/wa-lang/wa.git
synced 2025-12-06 09:18:53 +08:00
简化命令行参数
This commit is contained in:
@@ -22,8 +22,8 @@ func MakeFlag_output() *cli.StringFlag {
|
||||
func MakeFlag_target() *cli.StringFlag {
|
||||
return &cli.StringFlag{
|
||||
Name: "target",
|
||||
Usage: fmt.Sprintf("set target os (%s)", strings.Join(config.WaOS_List, "|")),
|
||||
Value: config.WaOS_Default,
|
||||
Usage: fmt.Sprintf("set target type (%s)", strings.Join(config.WaOS_List, "|")),
|
||||
Value: "",
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,8 +16,7 @@ type Option struct {
|
||||
Debug bool
|
||||
WaBackend string
|
||||
BuilgTags []string
|
||||
TargetArch string
|
||||
TargetOS string
|
||||
Target string
|
||||
LD_StackSize int
|
||||
LD_MaxMemory int
|
||||
}
|
||||
@@ -34,12 +33,6 @@ func (opt *Option) Config() *config.Config {
|
||||
if len(opt.BuilgTags) > 0 {
|
||||
cfg.BuilgTags = append(cfg.BuilgTags, opt.BuilgTags...)
|
||||
}
|
||||
if opt.TargetArch != "" {
|
||||
cfg.WaArch = opt.TargetArch
|
||||
}
|
||||
if opt.TargetOS != "" {
|
||||
cfg.WaOS = opt.TargetOS
|
||||
}
|
||||
if opt.LD_StackSize != 0 {
|
||||
cfg.LDFlags.StackSize = opt.LD_StackSize
|
||||
}
|
||||
@@ -47,30 +40,12 @@ func (opt *Option) Config() *config.Config {
|
||||
cfg.LDFlags.MaxMemory = opt.LD_MaxMemory
|
||||
}
|
||||
|
||||
switch cfg.WaArch {
|
||||
case "wasm":
|
||||
cfg.WaSizes.MaxAlign = 8
|
||||
cfg.WaSizes.WordSize = 4
|
||||
case "wasm64":
|
||||
cfg.WaSizes.MaxAlign = 8
|
||||
cfg.WaSizes.WordSize = 8
|
||||
default:
|
||||
panic("unknown WaArch: " + cfg.WaArch)
|
||||
}
|
||||
cfg.WaSizes.MaxAlign = 8
|
||||
cfg.WaSizes.WordSize = 4
|
||||
|
||||
return cfg
|
||||
}
|
||||
|
||||
// 构建命令行程序对象
|
||||
func (opt *Option) Adjust() {
|
||||
if opt.TargetOS == "" {
|
||||
opt.TargetOS = config.WaOS_Default
|
||||
}
|
||||
if opt.TargetArch == "" {
|
||||
opt.TargetArch = config.WaArch_Default
|
||||
}
|
||||
}
|
||||
|
||||
func BuildOptions(c *cli.Context, waBackend ...string) *Option {
|
||||
opt := &Option{
|
||||
Debug: c.Bool("debug"),
|
||||
@@ -80,32 +55,26 @@ func BuildOptions(c *cli.Context, waBackend ...string) *Option {
|
||||
LD_MaxMemory: c.Int("ld-max-memory"),
|
||||
}
|
||||
|
||||
opt.TargetArch = "wasm"
|
||||
if len(waBackend) > 0 {
|
||||
opt.WaBackend = waBackend[0]
|
||||
}
|
||||
|
||||
if target := c.String("target"); target != "" && !config.CheckWaOS(target) {
|
||||
fmt.Printf("unknown target: %s\n", c.String("target"))
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
switch c.String("target") {
|
||||
case "", "wa", "walang":
|
||||
opt.TargetOS = config.WaOS_Default
|
||||
case config.WaOS_wasi:
|
||||
opt.TargetOS = config.WaOS_wasi
|
||||
case config.WaOS_wasm4:
|
||||
opt.TargetOS = config.WaOS_wasm4
|
||||
case config.WaOS_unknown:
|
||||
opt.TargetOS = config.WaOS_unknown
|
||||
case "":
|
||||
// read from default or wa.mod
|
||||
case config.WaOS_js:
|
||||
opt.TargetOS = config.WaOS_js
|
||||
opt.Target = config.WaOS_js
|
||||
case config.WaOS_wasi:
|
||||
opt.Target = config.WaOS_wasi
|
||||
case config.WaOS_wasm4:
|
||||
opt.Target = config.WaOS_wasm4
|
||||
case config.WaOS_unknown:
|
||||
opt.Target = config.WaOS_unknown
|
||||
default:
|
||||
fmt.Printf("unreachable: target: %s\n", c.String("target"))
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
opt.Adjust()
|
||||
return opt
|
||||
}
|
||||
|
||||
@@ -158,6 +158,9 @@ func BuildApp(opt *appbase.Option, input, outfile string) (mainFunc string, wasm
|
||||
fmt.Printf("%q is invalid wa moudle\n", input)
|
||||
os.Exit(1)
|
||||
}
|
||||
if opt.Target != "" {
|
||||
manifest.Pkg.Target = opt.Target
|
||||
}
|
||||
if err := manifest.Valid(); err != nil {
|
||||
fmt.Printf("%q is invalid wa module; %v\n", input, err)
|
||||
os.Exit(1)
|
||||
@@ -188,7 +191,7 @@ func BuildApp(opt *appbase.Option, input, outfile string) (mainFunc string, wasm
|
||||
}
|
||||
|
||||
// 生成 js 胶水代码
|
||||
if opt.TargetOS == config.WaOS_js {
|
||||
if manifest.Pkg.Target == config.WaOS_js {
|
||||
jsOutfile := appbase.ReplaceExt(outfile, ".wasm", ".js")
|
||||
jsOutput := compiler.GenJSBinding(filepath.Base(outfile))
|
||||
err = os.WriteFile(jsOutfile, []byte(jsOutput), 0666)
|
||||
@@ -223,7 +226,7 @@ func BuildApp(opt *appbase.Option, input, outfile string) (mainFunc string, wasm
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if opt.TargetOS == config.WaOS_wasm4 {
|
||||
if manifest.Pkg.Target == config.WaOS_wasm4 {
|
||||
icoOutfile := filepath.Join(filepath.Dir(outfile), "favicon.ico")
|
||||
w4JsOutfile := filepath.Join(filepath.Dir(outfile), "wasm4.js")
|
||||
w4CssOutfile := filepath.Join(filepath.Dir(outfile), "wasm4.css")
|
||||
|
||||
@@ -40,6 +40,10 @@ var CmdInit = &cli.Command{
|
||||
Name: "wasm4",
|
||||
Usage: "wasm4 game",
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: "wasi",
|
||||
Usage: "wasi example",
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: "update",
|
||||
Aliases: []string{"u"},
|
||||
@@ -48,7 +52,11 @@ var CmdInit = &cli.Command{
|
||||
},
|
||||
|
||||
Action: func(c *cli.Context) error {
|
||||
err := InitApp(c.String("name"), c.String("pkgpath"), c.Bool("p5"), c.Bool("wasm4"), c.Bool("update"))
|
||||
err := InitApp(
|
||||
c.String("name"), c.String("pkgpath"),
|
||||
c.Bool("p5"), c.Bool("wasm4"), c.Bool("wasi"),
|
||||
c.Bool("update"),
|
||||
)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
@@ -57,7 +65,7 @@ var CmdInit = &cli.Command{
|
||||
},
|
||||
}
|
||||
|
||||
func InitApp(name, pkgpath string, isP5App, isWasm4App, update bool) error {
|
||||
func InitApp(name, pkgpath string, isP5App, isWasm4App, isWasiApp, update bool) error {
|
||||
if name == "" {
|
||||
return fmt.Errorf("init failed: <%s> is empty", name)
|
||||
}
|
||||
@@ -75,18 +83,28 @@ func InitApp(name, pkgpath string, isP5App, isWasm4App, update bool) error {
|
||||
}
|
||||
}
|
||||
|
||||
if isP5App {
|
||||
isWasiApp = false
|
||||
isWasm4App = false
|
||||
}
|
||||
|
||||
os.MkdirAll(name, 0777)
|
||||
os.WriteFile(filepath.Join(name, ".gitignore"), []byte("/output\n"), 0666)
|
||||
|
||||
var info = struct {
|
||||
Name string
|
||||
Pkgpath string
|
||||
Year int
|
||||
IsP5App bool
|
||||
IsWasm4App bool
|
||||
IsWasiApp bool
|
||||
}{
|
||||
Name: name,
|
||||
Pkgpath: pkgpath,
|
||||
Year: time.Now().Year(),
|
||||
IsP5App: isP5App,
|
||||
IsWasm4App: isWasm4App,
|
||||
IsWasiApp: isWasiApp,
|
||||
}
|
||||
|
||||
appFS := waroot_GetExampleAppFS()
|
||||
|
||||
@@ -36,12 +36,12 @@ global smiley = [8]byte{
|
||||
|
||||
#wa:export update
|
||||
func Update {
|
||||
wasm4.SetDrawColors(2)
|
||||
wasm4.SetDrawColors(2, 0, 0, 0)
|
||||
wasm4.Text("Hello from Wa-lang!", 10, 10)
|
||||
|
||||
gamepad := wasm4.GetGamePad1()
|
||||
if gamepad&wasm4.BUTTON_1 != 0 {
|
||||
wasm4.SetDrawColors(4)
|
||||
wasm4.SetDrawColors(4, 0, 0, 0)
|
||||
}
|
||||
|
||||
wasm4.Blit(smiley[:], 76, 76, 8, 8, wasm4.BLIT_1BPP)
|
||||
|
||||
@@ -2,3 +2,4 @@
|
||||
|
||||
name = "{{.Name}}"
|
||||
pkgpath = "{{.Pkgpath}}"
|
||||
target = {{if .IsWasiApp}}"wasi"{{else if .IsWasm4App}}"wasm4"{{else}}"js"{{end}}
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"wa-lang.org/wa/internal/3rdparty/cli"
|
||||
"wa-lang.org/wa/internal/app/appbase"
|
||||
@@ -122,6 +123,12 @@ func CmdRunAction(c *cli.Context) error {
|
||||
fileHandler.ServeHTTP(w, req)
|
||||
}),
|
||||
)
|
||||
|
||||
go func() {
|
||||
time.Sleep(time.Second * 2)
|
||||
openBrowser(addr)
|
||||
}()
|
||||
|
||||
if err := http.ListenAndServe(addr, nil); err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
|
||||
@@ -34,7 +34,7 @@ func (p *Compiler) Compile(prog *loader.Program) (output string, err error) {
|
||||
p.prog = prog
|
||||
|
||||
// 不同平台 stack 大小不同
|
||||
stkSize := wasrc.GetStackSize(config.WaBackend_wat, p.prog.Cfg.WaOS)
|
||||
stkSize := wasrc.GetStackSize(config.WaBackend_wat, p.prog.Cfg.Target)
|
||||
p.module = wir.NewModule(stkSize)
|
||||
p.module.AddGlobal("$wa.runtime.closure_data", "", p.module.GenValueType_Ptr(p.module.VOID), false, nil)
|
||||
wir.SetCurrentModule(p.module)
|
||||
@@ -108,7 +108,7 @@ func (p *Compiler) Compile(prog *loader.Program) (output string, err error) {
|
||||
func (p *Compiler) CompileWsFiles(prog *loader.Program) {
|
||||
var sb strings.Builder
|
||||
|
||||
sb.WriteString(wasrc.GetBaseWsCode(config.WaBackend_wat, p.prog.Cfg.WaOS))
|
||||
sb.WriteString(wasrc.GetBaseWsCode(config.WaBackend_wat, p.prog.Cfg.Target))
|
||||
sb.WriteString("\n")
|
||||
|
||||
var pkgpathList = make([]string, 0, len(prog.Pkgs))
|
||||
@@ -147,7 +147,7 @@ func (p *Compiler) CompileWsFiles(prog *loader.Program) {
|
||||
func (p *Compiler) CompileWImportFiles(prog *loader.Program) string {
|
||||
var sb strings.Builder
|
||||
|
||||
sb.WriteString(wasrc.GetBaseImportCode(p.prog.Cfg.WaOS))
|
||||
sb.WriteString(wasrc.GetBaseImportCode(p.prog.Cfg.Target))
|
||||
sb.WriteString("\n")
|
||||
|
||||
var pkgpathList = make([]string, 0, len(prog.Pkgs))
|
||||
|
||||
@@ -26,10 +26,9 @@ type PkgVFS struct {
|
||||
|
||||
// 通用配置信息
|
||||
type Config struct {
|
||||
Target string // 目标平台
|
||||
WatOutput string // 输出的 wat 文件路径
|
||||
WaBackend string // 编译器后端
|
||||
WaArch string // 目标 CPU
|
||||
WaOS string // 目标 OS
|
||||
WaSizes StdSizes // 指针大小
|
||||
BuilgTags []string // 条件编译的标志
|
||||
UnitTest bool // 单元测试模式
|
||||
@@ -56,21 +55,6 @@ func DefaultConfig() *Config {
|
||||
p.WaBackend = WaBackend_Default
|
||||
}
|
||||
|
||||
if p.WaArch == "" {
|
||||
if s := os.Getenv("WAARCH"); s != "" {
|
||||
p.WaArch = s
|
||||
} else {
|
||||
p.WaArch = WaArch_Default
|
||||
}
|
||||
}
|
||||
if p.WaOS == "" {
|
||||
if s := os.Getenv("WAOS"); s != "" {
|
||||
p.WaOS = s
|
||||
} else {
|
||||
p.WaOS = WaOS_Default
|
||||
}
|
||||
}
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
|
||||
@@ -32,6 +32,7 @@ type Manifest struct {
|
||||
type Manifest_package struct {
|
||||
Name string `json:"name"` // 名字
|
||||
Pkgpath string `json:"pkgpath"` // 模块的导入路径
|
||||
Target string `json:"target"` // 目标平台
|
||||
Version string `json:"version"` // 版本
|
||||
Authors []string `json:"authors,omitempty"` // 作者
|
||||
Description string `json:"description,omitempty"` // 一句话简介
|
||||
|
||||
@@ -462,12 +462,12 @@ func (p *_Loader) ParseDir_wsFiles(pkgpath string) (files []*WsFile, err error)
|
||||
func (p *_Loader) ParseDir_hostImportFiles(pkgpath string) (files []*WhostFile, err error) {
|
||||
logger.Tracef(&config.EnableTrace_loader, "pkgpath: %v", pkgpath)
|
||||
|
||||
if p.cfg.WaOS == "" {
|
||||
if p.cfg.Target == "" && p.prog.Manifest.Pkg.Target == "" {
|
||||
panic("unreachable")
|
||||
}
|
||||
|
||||
var (
|
||||
extNames = []string{fmt.Sprintf(".import.%s", p.cfg.WaOS)}
|
||||
extNames = []string{fmt.Sprintf(".import.%s", p.GetTargetOS())}
|
||||
unitTestMode bool = false
|
||||
|
||||
filenames []string
|
||||
@@ -696,7 +696,7 @@ func (p *_Loader) isSelfPkg(pkgpath string) bool {
|
||||
func (p *_Loader) getSizes() types.Sizes {
|
||||
var zero config.StdSizes
|
||||
if p == nil || p.cfg.WaSizes == zero {
|
||||
return types.SizesFor(p.cfg.WaArch)
|
||||
return types.SizesFor(p.GetTargetArch())
|
||||
} else {
|
||||
return &types.StdSizes{
|
||||
WordSize: p.cfg.WaSizes.WordSize,
|
||||
@@ -745,7 +745,7 @@ func (p *_Loader) isSkipedAstFile(f *ast.File) (bool, error) {
|
||||
return false, err
|
||||
}
|
||||
ok := expr.Eval(func(tag string) bool {
|
||||
if tag == p.cfg.WaOS || tag == p.cfg.WaArch {
|
||||
if tag == p.GetTargetOS() || tag == p.GetTargetArch() {
|
||||
return true
|
||||
}
|
||||
for _, x := range p.cfg.BuilgTags {
|
||||
@@ -776,7 +776,7 @@ func (p *_Loader) isSkipedSouceFile(filename string, unitTestMode bool, extNames
|
||||
}
|
||||
}
|
||||
|
||||
if p.cfg.WaOS != "" {
|
||||
if p.GetTargetOS() != "" {
|
||||
var isTargetFile bool
|
||||
for _, ext := range extNames {
|
||||
for _, os := range config.WaOS_List {
|
||||
@@ -789,7 +789,7 @@ func (p *_Loader) isSkipedSouceFile(filename string, unitTestMode bool, extNames
|
||||
if isTargetFile {
|
||||
var shouldSkip = true
|
||||
for _, ext := range extNames {
|
||||
if strings.HasSuffix(filename, "_"+p.cfg.WaOS+ext) {
|
||||
if strings.HasSuffix(filename, "_"+p.GetTargetOS()+ext) {
|
||||
shouldSkip = false
|
||||
break
|
||||
}
|
||||
@@ -821,3 +821,17 @@ func (p *_Loader) isTestFile(filename string) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (p *_Loader) GetTargetOS() string {
|
||||
if s := p.cfg.Target; s != "" {
|
||||
return s
|
||||
}
|
||||
if s := p.prog.Manifest.Pkg.Target; s != "" {
|
||||
return s
|
||||
}
|
||||
return config.WaOS_Default
|
||||
}
|
||||
|
||||
func (p *_Loader) GetTargetArch() string {
|
||||
return config.WaArch_Default
|
||||
}
|
||||
|
||||
@@ -71,6 +71,17 @@ func loadProgramFileMeta(cfg *config.Config, filename string, src interface{}) (
|
||||
}
|
||||
}
|
||||
|
||||
if cfg.Target != "" {
|
||||
manifest.Pkg.Target = cfg.Target
|
||||
}
|
||||
if manifest.Pkg.Target == "" {
|
||||
manifest.Pkg.Target = config.WaOS_Default
|
||||
}
|
||||
|
||||
if cfg.Target != manifest.Pkg.Target {
|
||||
cfg.Target = manifest.Pkg.Target
|
||||
}
|
||||
|
||||
logger.Tracef(&config.EnableTrace_loader, "manifest: %s", manifest.JSONString())
|
||||
|
||||
// 构造入口文件
|
||||
@@ -164,6 +175,17 @@ func loadProgramMeta(cfg *config.Config, appPath string) (
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
if cfg.Target != "" {
|
||||
manifest.Pkg.Target = cfg.Target
|
||||
}
|
||||
if manifest.Pkg.Target == "" {
|
||||
manifest.Pkg.Target = config.WaOS_Default
|
||||
}
|
||||
|
||||
if cfg.Target != manifest.Pkg.Target {
|
||||
cfg.Target = manifest.Pkg.Target
|
||||
}
|
||||
|
||||
logger.Tracef(&config.EnableTrace_loader, "manifest: %s", manifest.JSONString())
|
||||
|
||||
vfs = new(config.PkgVFS)
|
||||
|
||||
@@ -2,3 +2,4 @@
|
||||
|
||||
name = "p5app"
|
||||
pkgpath = "p5app"
|
||||
target = "js"
|
||||
|
||||
@@ -2,3 +2,4 @@
|
||||
|
||||
name = "w42048"
|
||||
pkgpath = "w42048"
|
||||
target = "wasm4"
|
||||
|
||||
1
waroot/examples/w4-hello/.gitignore
vendored
Normal file
1
waroot/examples/w4-hello/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/output
|
||||
@@ -2,3 +2,4 @@
|
||||
|
||||
name = "w4app"
|
||||
pkgpath = "w4app"
|
||||
target = "wasm4"
|
||||
|
||||
1
waroot/examples/w4-snake/.gitignore
vendored
Normal file
1
waroot/examples/w4-snake/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/output
|
||||
@@ -2,3 +2,4 @@
|
||||
|
||||
name = "w4snake"
|
||||
pkgpath = "w4snake"
|
||||
target = "wasm4"
|
||||
|
||||
Reference in New Issue
Block a user