193 lines
4.4 KiB
Go
193 lines
4.4 KiB
Go
/*
|
|
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
package build
|
|
|
|
import (
|
|
"fmt"
|
|
"go/token"
|
|
"log"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
|
|
"golang.org/x/tools/go/packages"
|
|
"golang.org/x/tools/go/ssa"
|
|
|
|
"github.com/goplus/llgo/cl"
|
|
"github.com/goplus/llgo/x/clang"
|
|
|
|
llssa "github.com/goplus/llgo/ssa"
|
|
)
|
|
|
|
type Mode int
|
|
|
|
const (
|
|
ModeBuild Mode = iota
|
|
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 (
|
|
loadFiles = packages.NeedName | packages.NeedFiles | packages.NeedCompiledGoFiles
|
|
loadImports = loadFiles | packages.NeedImports
|
|
loadTypes = loadImports | packages.NeedTypes | packages.NeedTypesSizes
|
|
loadSyntax = loadTypes | packages.NeedSyntax | packages.NeedTypesInfo
|
|
)
|
|
|
|
func Do(args []string, conf *Config) {
|
|
flags, patterns, verbose := parseArgs(args)
|
|
cfg := &packages.Config{
|
|
Mode: loadSyntax | packages.NeedDeps | packages.NeedExportFile,
|
|
BuildFlags: flags,
|
|
}
|
|
|
|
if patterns == nil {
|
|
patterns = []string{"."}
|
|
}
|
|
initial, err := packages.Load(cfg, patterns...)
|
|
check(err)
|
|
|
|
// Create SSA-form program representation.
|
|
ssaProg, pkgs, errPkgs := allPkgs(initial, ssa.SanityCheckFunctions)
|
|
ssaProg.Build()
|
|
for _, errPkg := range errPkgs {
|
|
log.Println("cannot build SSA for package", errPkg)
|
|
}
|
|
|
|
llssa.Initialize(llssa.InitAll)
|
|
if verbose {
|
|
llssa.SetDebug(llssa.DbgFlagAll)
|
|
cl.SetDebug(cl.DbgFlagAll)
|
|
}
|
|
|
|
prog := llssa.NewProgram(nil)
|
|
mode := conf.Mode
|
|
for _, pkg := range pkgs {
|
|
buildPkg(prog, pkg, mode)
|
|
}
|
|
|
|
if mode == ModeInstall {
|
|
for _, pkg := range initial {
|
|
if pkg.Name == "main" {
|
|
linkMainPkg(pkg, conf)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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
|
|
fmt.Fprintln(os.Stderr, pkgPath)
|
|
if pkgPath == "unsafe" { // TODO(xsw): remove this special case
|
|
return
|
|
}
|
|
ret, err := cl.NewPackage(prog, aPkg.SSA, pkg.Syntax)
|
|
check(err)
|
|
if mode == ModeInstall {
|
|
file := pkg.ExportFile + ".ll"
|
|
os.WriteFile(file, []byte(ret.String()), 0644)
|
|
}
|
|
}
|
|
|
|
type aPackage struct {
|
|
*packages.Package
|
|
SSA *ssa.Package
|
|
}
|
|
|
|
func allPkgs(initial []*packages.Package, mode ssa.BuilderMode) (prog *ssa.Program, all []aPackage, errs []*packages.Package) {
|
|
var fset *token.FileSet
|
|
if len(initial) > 0 {
|
|
fset = initial[0].Fset
|
|
}
|
|
|
|
prog = ssa.NewProgram(fset, mode)
|
|
packages.Visit(initial, nil, func(p *packages.Package) {
|
|
if p.Types != nil && !p.IllTyped {
|
|
ssaPkg := prog.CreatePackage(p.Types, p.Syntax, p.TypesInfo, true)
|
|
all = append(all, aPackage{p, ssaPkg})
|
|
} else {
|
|
errs = append(errs, p)
|
|
}
|
|
})
|
|
return
|
|
}
|
|
|
|
func parseArgs(args []string) (flags, patterns []string, verbose bool) {
|
|
for i, arg := range args {
|
|
if !strings.HasPrefix(arg, "-") {
|
|
flags, patterns = args[:i], args[i:]
|
|
return
|
|
}
|
|
if arg == "-v" {
|
|
verbose = true
|
|
}
|
|
}
|
|
flags = args
|
|
return
|
|
}
|
|
|
|
func check(err error) {
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// -----------------------------------------------------------------------------
|