build: mark need runtime

This commit is contained in:
xushiwei
2024-04-29 00:49:17 +08:00
parent c30ed1b3c8
commit 769b93a277
2 changed files with 27 additions and 3 deletions

View File

@@ -132,6 +132,14 @@ func Do(args []string, conf *Config) {
} }
} }
func setNeedRuntime(pkg *packages.Package) {
pkg.Module = nil // just use pkg.Module to mark it needs runtime
}
func isNeedRuntime(pkg *packages.Package) bool {
return pkg.Module == nil
}
func buildAllPkgs(prog llssa.Program, initial []*packages.Package, mode Mode, verbose bool) { func buildAllPkgs(prog llssa.Program, initial []*packages.Package, mode Mode, verbose bool) {
// Create SSA-form program representation. // Create SSA-form program representation.
ssaProg, pkgs, errPkgs := allPkgs(initial, ssa.SanityCheckFunctions) ssaProg, pkgs, errPkgs := allPkgs(initial, ssa.SanityCheckFunctions)
@@ -141,6 +149,9 @@ func buildAllPkgs(prog llssa.Program, initial []*packages.Package, mode Mode, ve
} }
for _, pkg := range pkgs { for _, pkg := range pkgs {
buildPkg(prog, pkg, mode, verbose) buildPkg(prog, pkg, mode, verbose)
if prog.NeedRuntime() {
setNeedRuntime(pkg.Package)
}
} }
} }
@@ -156,14 +167,18 @@ func linkMainPkg(pkg *packages.Package, runtimeFiles []string, conf *Config, mod
args[0] = "-o" args[0] = "-o"
args[1] = app args[1] = app
args[2] = "-Wno-override-module" args[2] = "-Wno-override-module"
if runtimeFiles != nil { needRuntime := false
args = append(args, runtimeFiles...)
}
packages.Visit([]*packages.Package{pkg}, nil, func(p *packages.Package) { packages.Visit([]*packages.Package{pkg}, nil, func(p *packages.Package) {
if p.PkgPath != "unsafe" { // TODO(xsw): maybe can remove this special case if p.PkgPath != "unsafe" { // TODO(xsw): maybe can remove this special case
args = append(args, p.ExportFile+".ll") args = append(args, p.ExportFile+".ll")
if !needRuntime {
needRuntime = isNeedRuntime(p)
}
} }
}) })
if needRuntime && runtimeFiles != nil {
args = append(args, runtimeFiles...)
}
// TODO(xsw): show work // TODO(xsw): show work
// fmt.Fprintln(os.Stderr, "clang", args) // fmt.Fprintln(os.Stderr, "clang", args)

View File

@@ -126,6 +126,8 @@ type aProgram struct {
uintptrTy Type uintptrTy Type
intTy Type intTy Type
f64Ty Type f64Ty Type
needRuntime bool
} }
// A Program presents a program. // A Program presents a program.
@@ -154,10 +156,16 @@ func (p Program) SetRuntime(runtime any) {
} }
} }
// NeedRuntime returns if the current package needs runtime.
func (p Program) NeedRuntime() bool {
return p.needRuntime
}
func (p Program) runtime() *types.Package { func (p Program) runtime() *types.Package {
if p.rt == nil { if p.rt == nil {
p.rt = p.rtget() p.rt = p.rtget()
} }
p.needRuntime = true
return p.rt return p.rt
} }
@@ -197,6 +205,7 @@ func (p Program) NewPackage(name, pkgPath string) Package {
// mod.Finalize() // mod.Finalize()
fns := make(map[string]Function) fns := make(map[string]Function)
gbls := make(map[string]Global) gbls := make(map[string]Global)
p.needRuntime = false
return &aPackage{mod, fns, gbls, p} return &aPackage{mod, fns, gbls, p}
} }