build: make llgen, cltest, gentests call build.Do

This commit is contained in:
Li Jie
2024-11-26 11:20:15 +08:00
parent 25bc84817a
commit d879d0d924
11 changed files with 87 additions and 172 deletions

View File

@@ -53,6 +53,7 @@ const (
ModeInstall
ModeRun
ModeCmpTest
ModeGen
)
const (
@@ -119,7 +120,7 @@ const (
loadSyntax = loadTypes | packages.NeedSyntax | packages.NeedTypesInfo
)
func Do(args []string, conf *Config) {
func Do(args []string, conf *Config) ([]Package, error) {
flags, patterns, verbose := ParseArgs(args, buildFlags)
cl.EnableDebugSymbols(IsDebugEnabled())
flags = append(flags, "-tags", "llgo")
@@ -159,7 +160,6 @@ func Do(args []string, conf *Config) {
}
initial, err := packages.LoadEx(dedup, sizes, cfg, patterns...)
check(err)
mode := conf.Mode
if len(initial) == 1 && len(initial[0].CompiledGoFiles) > 0 {
if mode == ModeBuild {
@@ -167,11 +167,10 @@ func Do(args []string, conf *Config) {
}
} else if mode == ModeRun {
if len(initial) > 1 {
fmt.Fprintln(os.Stderr, "cannot run multiple packages")
return nil, fmt.Errorf("cannot run multiple packages")
} else {
fmt.Fprintln(os.Stderr, "no Go files in matched packages")
return nil, fmt.Errorf("no Go files in matched packages")
}
return
}
altPkgPaths := altPkgs(initial, llssa.PkgRuntime)
@@ -203,12 +202,21 @@ func Do(args []string, conf *Config) {
ctx := &context{env, cfg, progSSA, prog, dedup, patches, make(map[string]none), initial, mode, 0}
pkgs := buildAllPkgs(ctx, initial, verbose)
if mode == ModeGen {
for _, pkg := range pkgs {
if pkg.Package == initial[0] {
return []*aPackage{pkg}, nil
}
}
return nil, fmt.Errorf("initial package not found")
}
dpkg := buildAllPkgs(ctx, altPkgs[noRt:], verbose)
var linkArgs []string
for _, pkg := range dpkg {
linkArgs = append(linkArgs, pkg.LinkArgs...)
}
if mode != ModeBuild {
nErr := 0
for _, pkg := range initial {
@@ -220,6 +228,7 @@ func Do(args []string, conf *Config) {
os.Exit(nErr)
}
}
return dpkg, nil
}
func setNeedRuntimeOrPyInit(pkg *packages.Package, needRuntime, needPyInit bool) {
@@ -571,6 +580,8 @@ type aPackage struct {
LinkArgs []string
}
type Package = *aPackage
func allPkgs(ctx *context, initial []*packages.Package, verbose bool) (all []*aPackage, errs []*packages.Package) {
prog := ctx.progSSA
built := ctx.built

View File

@@ -16,41 +16,8 @@
package llgen
import (
"os"
"github.com/goplus/llgo/cl"
"github.com/goplus/llgo/internal/build"
"github.com/goplus/llgo/internal/mod"
llssa "github.com/goplus/llgo/ssa"
)
func Init() {
llssa.Initialize(llssa.InitAll)
llssa.SetDebug(llssa.DbgFlagAll)
cl.SetDebug(cl.DbgFlagAll)
cl.EnableDebugSymbols(build.IsDebugEnabled())
}
func PkgPath(dir string) string {
_, pkgPath, err := mod.Load(dir)
check(err)
return pkgPath
}
func Do(pkgPath, inFile, outFile string) {
ret := genFrom(inFile, pkgPath)
err := os.WriteFile(outFile, []byte(ret), 0644)
check(err)
}
func check(err error) {
if err != nil {
panic(err)
}
}
var (
Verbose = true
)

View File

@@ -17,107 +17,30 @@
package llgen
import (
"go/ast"
"go/types"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/goplus/llgo/cl"
"github.com/goplus/llgo/internal/build"
"github.com/goplus/llgo/internal/packages"
"golang.org/x/tools/go/ssa"
"golang.org/x/tools/go/ssa/ssautil"
llssa "github.com/goplus/llgo/ssa"
)
const (
loadFiles = packages.NeedName | packages.NeedFiles | packages.NeedCompiledGoFiles
loadImports = loadFiles | packages.NeedImports
loadTypes = loadImports | packages.NeedTypes | packages.NeedTypesSizes
loadSyntax = loadTypes | packages.NeedSyntax | packages.NeedTypesInfo
)
func initRtAndPy(prog llssa.Program, cfg *packages.Config) {
var pkgRtAndPy []*packages.Package
load := func() []*packages.Package {
if pkgRtAndPy == nil {
var err error
pkgRtAndPy, err = packages.LoadEx(nil, prog.TypeSizes, cfg, llssa.PkgRuntime, llssa.PkgPython)
check(err)
}
return pkgRtAndPy
}
prog.SetRuntime(func() *types.Package {
rt := load()
return rt[0].Types
})
prog.SetPython(func() *types.Package {
rt := load()
return rt[1].Types
})
}
func GenFrom(fileOrPkg string) string {
return genFrom(fileOrPkg, "")
pkg, err := genFrom(fileOrPkg)
check(err)
return pkg.LPkg.String()
}
func genFrom(fileOrPkg string, pkgPath string) string {
prog := llssa.NewProgram(nil)
cfg := &packages.Config{
Mode: loadSyntax | packages.NeedDeps,
BuildFlags: []string{"-tags", "llgo"},
func genFrom(pkgPath string) (build.Package, error) {
conf := &build.Config{
Mode: build.ModeGen,
AppExt: build.DefaultAppExt(),
}
dedup := packages.NewDeduper()
dedup.SetPkgPath(func(path, name string) string {
if path == "command-line-arguments" {
if pkgPath != "" {
path = pkgPath
} else {
path = name
}
}
return path
})
dedup.SetPreload(func(pkg *types.Package, files []*ast.File) {
cl.ParsePkgSyntax(prog, pkg, files)
})
initial, err := packages.LoadEx(dedup, prog.TypeSizes, cfg, fileOrPkg)
check(err)
buildMode := ssa.SanityCheckFunctions | ssa.InstantiateGenerics
if build.IsDebugEnabled() {
buildMode |= ssa.GlobalDebug
pkgs, err := build.Do([]string{pkgPath}, conf)
if err != nil {
return nil, err
}
if !build.IsOptimizeEnabled() {
buildMode |= ssa.NaiveForm
}
_, pkgs := ssautil.AllPackages(initial, buildMode)
pkg := initial[0]
ssaPkg := pkgs[0]
ssaPkg.Build()
initRtAndPy(prog, cfg)
if Verbose {
ssaPkg.WriteTo(os.Stderr)
}
ret, err := cl.NewPackage(prog, ssaPkg, pkg.Syntax)
check(err)
if prog.NeedPyInit { // call PyInit if needed
ret.PyInit()
}
return ret.String()
return pkgs[0], nil
}
func DoFile(fileOrPkg, outFile string) {
@@ -126,9 +49,12 @@ func DoFile(fileOrPkg, outFile string) {
check(err)
}
func SmartDoFile(inFile string, pkgPath ...string) {
func SmartDoFile(pkgPath ...string) {
pkg, err := genFrom(pkgPath[0])
check(err)
const autgenFile = "llgo_autogen.ll"
dir, _ := filepath.Split(inFile)
dir, _ := filepath.Split(pkg.GoFiles[0])
absDir, _ := filepath.Abs(dir)
absDir = filepath.ToSlash(absDir)
fname := autgenFile
@@ -142,10 +68,8 @@ func SmartDoFile(inFile string, pkgPath ...string) {
return // skip to gen
}
if len(pkgPath) > 0 {
Do(pkgPath[0], inFile, outFile)
} else {
DoFile(inFile, outFile)
if err = os.WriteFile(outFile, []byte(pkg.LPkg.String()), 0644); err != nil {
panic(err)
}
if false && fname == autgenFile {
genZip(absDir, "llgo_autogen.lla", autgenFile)