diff --git a/cmd/internal/build/build.go b/cmd/internal/build/build.go index 58657c1c..af53ab62 100644 --- a/cmd/internal/build/build.go +++ b/cmd/internal/build/build.go @@ -47,11 +47,7 @@ func runCmd(cmd *base.Command, args []string) { } conf := build.NewDefaultConf(build.ModeBuild) - conf.Tags = flags.Tags - conf.Verbose = flags.Verbose - conf.OutFile = flags.OutputFile - conf.Target = flags.Target - conf.AbiMode = build.AbiMode(flags.AbiMode) + flags.UpdateConfig(conf) args = cmd.Flag.Args() diff --git a/cmd/internal/flags/flags.go b/cmd/internal/flags/flags.go index 4159e58f..c61927c3 100644 --- a/cmd/internal/flags/flags.go +++ b/cmd/internal/flags/flags.go @@ -3,6 +3,7 @@ package flags import ( "flag" + "github.com/goplus/llgo/internal/build" "github.com/goplus/llgo/internal/buildenv" ) @@ -17,6 +18,9 @@ var BuildEnv string var Tags string var Target string var AbiMode int +var CheckLinkArgs bool +var CheckLLFiles bool +var GenLLFiles bool func AddBuildFlags(fs *flag.FlagSet) { fs.BoolVar(&Verbose, "v", false, "Verbose mode") @@ -25,8 +29,9 @@ func AddBuildFlags(fs *flag.FlagSet) { fs.StringVar(&Target, "target", "", "Target platform (e.g., rp2040, wasi)") if buildenv.Dev { fs.IntVar(&AbiMode, "abi", 2, "ABI mode (default 2). 0 = none, 1 = cfunc, 2 = allfunc.") - } else { - AbiMode = 2 + fs.BoolVar(&CheckLinkArgs, "check-linkargs", false, "check link args valid") + fs.BoolVar(&CheckLLFiles, "check-llfiles", false, "check .ll files valid") + fs.BoolVar(&GenLLFiles, "gen-llfiles", false, "generate .ll files for pkg export") } } @@ -35,3 +40,21 @@ var Gen bool func AddCmpTestFlags(fs *flag.FlagSet) { fs.BoolVar(&Gen, "gen", false, "Generate llgo.expect file") } + +func UpdateConfig(conf *build.Config) { + conf.Tags = Tags + conf.Verbose = Verbose + conf.Target = Target + switch conf.Mode { + case build.ModeBuild: + conf.OutFile = OutputFile + case build.ModeCmpTest: + conf.GenExpect = Gen + } + if buildenv.Dev { + conf.AbiMode = build.AbiMode(AbiMode) + conf.CheckLinkArgs = CheckLinkArgs + conf.CheckLLFiles = CheckLLFiles + conf.GenLL = GenLLFiles + } +} diff --git a/cmd/internal/install/install.go b/cmd/internal/install/install.go index 96fb7275..7c50c509 100644 --- a/cmd/internal/install/install.go +++ b/cmd/internal/install/install.go @@ -45,9 +45,7 @@ func runCmd(cmd *base.Command, args []string) { } conf := build.NewDefaultConf(build.ModeInstall) - conf.Tags = flags.Tags - conf.Verbose = flags.Verbose - conf.AbiMode = build.AbiMode(flags.AbiMode) + flags.UpdateConfig(conf) args = cmd.Flag.Args() _, err := build.Do(args, conf) diff --git a/cmd/internal/run/run.go b/cmd/internal/run/run.go index dc4e53e7..b6380ab9 100644 --- a/cmd/internal/run/run.go +++ b/cmd/internal/run/run.go @@ -68,11 +68,7 @@ func runCmdEx(cmd *base.Command, args []string, mode build.Mode) { } conf := build.NewDefaultConf(mode) - conf.Tags = flags.Tags - conf.Verbose = flags.Verbose - conf.GenExpect = flags.Gen - conf.Target = flags.Target - conf.AbiMode = build.AbiMode(flags.AbiMode) + flags.UpdateConfig(conf) args = cmd.Flag.Args() args, runArgs, err := parseRunArgs(args) diff --git a/cmd/internal/test/test.go b/cmd/internal/test/test.go index 557094c3..b05f1e69 100644 --- a/cmd/internal/test/test.go +++ b/cmd/internal/test/test.go @@ -27,10 +27,7 @@ func runCmd(cmd *base.Command, args []string) { } conf := build.NewDefaultConf(build.ModeTest) - conf.Tags = flags.Tags - conf.Verbose = flags.Verbose - conf.Target = flags.Target - conf.AbiMode = build.AbiMode(flags.AbiMode) + flags.UpdateConfig(conf) args = cmd.Flag.Args() _, err := build.Do(args, conf) diff --git a/internal/build/build.go b/internal/build/build.go index e2fa4a10..ecd94edb 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -71,21 +71,23 @@ const ( ) type Config struct { - Goos string - Goarch string - Target string // target name (e.g., "rp2040", "wasi") - takes precedence over Goos/Goarch - BinPath string - AppExt string // ".exe" on Windows, empty on Unix - OutFile string // only valid for ModeBuild when len(pkgs) == 1 - RunArgs []string // only valid for ModeRun - Mode Mode - AbiMode AbiMode - GenExpect bool // only valid for ModeCmpTest - Verbose bool - GenLL bool // generate pkg .ll files - Tags string - GlobalNames map[string][]string // pkg => names - GlobalDatas map[string]string // pkg.name => data + Goos string + Goarch string + Target string // target name (e.g., "rp2040", "wasi") - takes precedence over Goos/Goarch + BinPath string + AppExt string // ".exe" on Windows, empty on Unix + OutFile string // only valid for ModeBuild when len(pkgs) == 1 + RunArgs []string // only valid for ModeRun + Mode Mode + AbiMode AbiMode + GenExpect bool // only valid for ModeCmpTest + Verbose bool + GenLL bool // generate pkg .ll files + CheckLLFiles bool // check .ll files valid + CheckLinkArgs bool // check linkargs valid + Tags string + GlobalNames map[string][]string // pkg => names + GlobalDatas map[string]string // pkg.name => data } func NewDefaultConf(mode Mode) *Config { @@ -277,14 +279,12 @@ func Do(args []string, conf *Config) ([]Package, error) { output := conf.OutFile != "" ctx := &context{env: env, conf: cfg, progSSA: progSSA, prog: prog, dedup: dedup, patches: patches, built: make(map[string]none), initial: initial, mode: mode, - output: output, - needRt: make(map[*packages.Package]bool), - needPyInit: make(map[*packages.Package]bool), - buildConf: conf, - crossCompile: export, - isCheckEnabled: IsCheckEnabled(), - isCheckLinkArgsEnabled: IsCheckLinkArgsEnabled(), - cTransformer: cabi.NewTransformer(prog, conf.AbiMode), + output: output, + needRt: make(map[*packages.Package]bool), + needPyInit: make(map[*packages.Package]bool), + buildConf: conf, + crossCompile: export, + cTransformer: cabi.NewTransformer(prog, conf.AbiMode), } pkgs, err := buildAllPkgs(ctx, initial, verbose) check(err) @@ -374,9 +374,6 @@ type context struct { nLibdir int output bool - isCheckEnabled bool - isCheckLinkArgsEnabled bool - needRt map[*packages.Package]bool needPyInit map[*packages.Package]bool @@ -476,7 +473,7 @@ func buildAllPkgs(ctx *context, initial []*packages.Package, verbose bool) (pkgs ctx.nLibdir++ } } - if ctx.isCheckLinkArgsEnabled { + if ctx.buildConf.CheckLinkArgs { if err := ctx.compiler().CheckLinkArgs(pkgLinkArgs, isWasmTarget(ctx.buildConf.Goos)); err != nil { panic(fmt.Sprintf("test link args '%s' failed\n\texpanded to: %v\n\tresolved to: %v\n\terror: %v", param, expdArgs, pkgLinkArgs, err)) } @@ -868,7 +865,7 @@ func exportObject(ctx *context, pkgPath string, exportFile string, data []byte) if err != nil { return exportFile, err } - if ctx.isCheckEnabled { + if ctx.buildConf.CheckLLFiles { if msg, err := llcCheck(ctx.env, f.Name()); err != nil { fmt.Fprintf(os.Stderr, "==> lcc %v: %v\n%v\n", pkgPath, f.Name(), msg) } @@ -1010,11 +1007,9 @@ const llgoDebug = "LLGO_DEBUG" const llgoDbgSyms = "LLGO_DEBUG_SYMBOLS" const llgoTrace = "LLGO_TRACE" const llgoOptimize = "LLGO_OPTIMIZE" -const llgoCheck = "LLGO_CHECK" const llgoWasmRuntime = "LLGO_WASM_RUNTIME" const llgoWasiThreads = "LLGO_WASI_THREADS" const llgoStdioNobuf = "LLGO_STDIO_NOBUF" -const llgoCheckLinkArgs = "LLGO_CHECK_LINKARGS" const llgoFullRpath = "LLGO_FULL_RPATH" const defaultWasmRuntime = "wasmtime" @@ -1055,18 +1050,10 @@ func IsOptimizeEnabled() bool { return isEnvOn(llgoOptimize, true) } -func IsCheckEnabled() bool { - return isEnvOn(llgoCheck, false) -} - func IsWasiThreadsEnabled() bool { return isEnvOn(llgoWasiThreads, true) } -func IsCheckLinkArgsEnabled() bool { - return isEnvOn(llgoCheckLinkArgs, false) -} - func IsFullRpathEnabled() bool { return isEnvOn(llgoFullRpath, true) }