llgo install: /appName

This commit is contained in:
xushiwei
2024-04-25 00:53:42 +08:00
parent cbaf9e21b2
commit a2c6e5d7fc
3 changed files with 62 additions and 15 deletions

View File

@@ -33,5 +33,7 @@ func init() {
} }
func runCmd(cmd *base.Command, args []string) { func runCmd(cmd *base.Command, args []string) {
build.Do(args, build.ModeBuild) build.Do(args, &build.Config{
Mode: build.ModeBuild,
})
} }

View File

@@ -33,5 +33,6 @@ func init() {
} }
func runCmd(cmd *base.Command, args []string) { func runCmd(cmd *base.Command, args []string) {
build.Do(args, build.ModeInstall) conf := build.NewDefaultConf(build.ModeInstall)
build.Do(args, conf)
} }

View File

@@ -21,14 +21,18 @@ import (
"go/token" "go/token"
"log" "log"
"os" "os"
"path"
"path/filepath"
"runtime"
"strings" "strings"
"golang.org/x/tools/go/packages" "golang.org/x/tools/go/packages"
"golang.org/x/tools/go/ssa" "golang.org/x/tools/go/ssa"
"github.com/goplus/llgo/cl" "github.com/goplus/llgo/cl"
llssa "github.com/goplus/llgo/ssa"
"github.com/goplus/llgo/x/clang" "github.com/goplus/llgo/x/clang"
llssa "github.com/goplus/llgo/ssa"
) )
type Mode int type Mode int
@@ -38,6 +42,27 @@ const (
ModeInstall ModeInstall
) )
type Config struct {
BinPath string
AppSuffix string // ".exe" on Windows, empty on Unix
Mode Mode
}
func NewDefaultConf(mode Mode) *Config {
bin := os.Getenv("GOBIN")
if bin == "" {
bin = filepath.Join(runtime.GOROOT(), "bin")
}
conf := &Config{
BinPath: bin,
Mode: mode,
}
if runtime.GOOS == "windows" {
conf.AppSuffix = ".exe"
}
return conf
}
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
const ( const (
@@ -47,7 +72,7 @@ const (
loadSyntax = loadTypes | packages.NeedSyntax | packages.NeedTypesInfo loadSyntax = loadTypes | packages.NeedSyntax | packages.NeedTypesInfo
) )
func Do(args []string, mode Mode) { func Do(args []string, conf *Config) {
flags, patterns, verbose := parseArgs(args) flags, patterns, verbose := parseArgs(args)
cfg := &packages.Config{ cfg := &packages.Config{
Mode: loadSyntax | packages.NeedDeps | packages.NeedExportFile, Mode: loadSyntax | packages.NeedDeps | packages.NeedExportFile,
@@ -74,32 +99,51 @@ func Do(args []string, mode Mode) {
} }
prog := llssa.NewProgram(nil) prog := llssa.NewProgram(nil)
llFiles := make([]string, 0, len(pkgs)) mode := conf.Mode
for _, pkg := range pkgs { for _, pkg := range pkgs {
llFiles = buildPkg(llFiles, prog, pkg, mode) buildPkg(prog, pkg, mode)
} }
if mode == ModeInstall { if mode == ModeInstall {
// TODO(xsw): show work for _, pkg := range initial {
// fmt.Fprintln(os.Stderr, "clang", llFiles) if pkg.Name == "main" {
err = clang.New("").Exec(llFiles...) linkMainPkg(pkg, conf)
check(err) }
}
} }
} }
func buildPkg(llFiles []string, prog llssa.Program, pkg aPackage, mode Mode) []string { func linkMainPkg(pkg *packages.Package, conf *Config) {
name := path.Base(pkg.PkgPath)
args := make([]string, 2, len(pkg.Imports)+3)
args[0] = "-o"
args[1] = filepath.Join(conf.BinPath, name+conf.AppSuffix)
packages.Visit([]*packages.Package{pkg}, nil, func(p *packages.Package) {
if p.PkgPath != "unsafe" { // TODO(xsw): remove this special case
args = append(args, p.ExportFile+".ll")
}
})
// TODO(xsw): show work
// fmt.Fprintln(os.Stderr, "clang", args)
fmt.Fprintln(os.Stderr, args[1])
err := clang.New("").Exec(args...)
check(err)
}
func buildPkg(prog llssa.Program, aPkg aPackage, mode Mode) {
pkg := aPkg.Package
pkgPath := pkg.PkgPath pkgPath := pkg.PkgPath
fmt.Fprintln(os.Stderr, pkgPath) fmt.Fprintln(os.Stderr, pkgPath)
if pkgPath == "unsafe" { // TODO(xsw): remove this special case if pkgPath == "unsafe" { // TODO(xsw): remove this special case
return llFiles return
} }
ret, err := cl.NewPackage(prog, pkg.SSA, pkg.Syntax) ret, err := cl.NewPackage(prog, aPkg.SSA, pkg.Syntax)
check(err) check(err)
if mode == ModeInstall { if mode == ModeInstall {
file := pkg.ExportFile + ".ll" file := pkg.ExportFile + ".ll"
os.WriteFile(file, []byte(ret.String()), 0644) os.WriteFile(file, []byte(ret.String()), 0644)
llFiles = append(llFiles, file)
} }
return llFiles
} }
type aPackage struct { type aPackage struct {