build: only limit run command to single package

This commit is contained in:
Li Jie
2025-01-16 15:08:13 +08:00
parent 946f304bb2
commit a07f711e22

View File

@@ -64,10 +64,6 @@ const (
debugBuild = packages.DebugPackagesLoad
)
func needLLFile(mode Mode) bool {
return mode != ModeBuild
}
type Config struct {
BinPath string
AppExt string // ".exe" on Windows, empty on Unix
@@ -169,13 +165,13 @@ func Do(args []string, conf *Config) ([]Package, error) {
initial, err := packages.LoadEx(dedup, sizes, cfg, patterns...)
check(err)
mode := conf.Mode
switch mode {
case ModeBuild:
if len(initial) == 1 && len(initial[0].CompiledGoFiles) > 0 {
mode = ModeInstall
}
case ModeRun:
if len(initial) > 1 {
if len(initial) > 1 {
switch mode {
case ModeBuild:
if conf.OutFile != "" {
return nil, fmt.Errorf("cannot build multiple packages with -o")
}
case ModeRun:
return nil, fmt.Errorf("cannot run multiple packages")
}
}
@@ -208,7 +204,8 @@ func Do(args []string, conf *Config) ([]Package, error) {
env := llvm.New("")
os.Setenv("PATH", env.BinDir()+":"+os.Getenv("PATH")) // TODO(xsw): check windows
ctx := &context{env, cfg, progSSA, prog, dedup, patches, make(map[string]none), initial, mode, 0}
output := mode != ModeBuild || conf.OutFile != ""
ctx := &context{env, cfg, progSSA, prog, dedup, patches, make(map[string]none), initial, mode, 0, output}
pkgs, err := buildAllPkgs(ctx, initial, verbose)
check(err)
if mode == ModeGen {
@@ -227,7 +224,7 @@ func Do(args []string, conf *Config) ([]Package, error) {
linkArgs = append(linkArgs, pkg.LinkArgs...)
}
if mode != ModeBuild {
if ctx.output {
for _, pkg := range initial {
if pkg.Name == "main" {
linkMainPkg(ctx, pkg, pkgs, linkArgs, conf, mode, verbose)
@@ -270,6 +267,7 @@ type context struct {
initial []*packages.Package
mode Mode
nLibdir int
output bool
}
func buildAllPkgs(ctx *context, initial []*packages.Package, verbose bool) (pkgs []*aPackage, err error) {
@@ -438,7 +436,7 @@ func linkMainPkg(ctx *context, pkg *packages.Package, pkgs []*aPackage, linkArgs
args = append(args, linkArgs...)
if needLLFile(mode) {
if ctx.output {
lpkg := aPkg.LPkg
os.WriteFile(pkg.ExportFile, []byte(lpkg.String()), 0644)
}
@@ -578,7 +576,7 @@ func buildPkg(ctx *context, aPkg *aPackage, verbose bool) (cgoLdflags []string,
check(err)
aPkg.LPkg = ret
cgoLdflags, err = buildCgo(ctx, aPkg, aPkg.Package.Syntax, externs, verbose)
if needLLFile(ctx.mode) {
if ctx.output {
pkg.ExportFile += ".ll"
os.WriteFile(pkg.ExportFile, []byte(ret.String()), 0644)
if debugBuild || verbose {