Files
llgo/internal/build/build_install.go

149 lines
3.6 KiB
Go
Raw Normal View History

2024-04-24 07:55:51 +08:00
/*
* 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 (
2024-04-24 11:13:17 +08:00
"fmt"
"go/token"
2024-04-24 07:55:51 +08:00
"log"
"os"
"strings"
"golang.org/x/tools/go/packages"
"golang.org/x/tools/go/ssa"
"github.com/goplus/llgo/cl"
llssa "github.com/goplus/llgo/ssa"
2024-04-24 11:49:43 +08:00
"github.com/goplus/llgo/x/clang"
2024-04-24 07:55:51 +08:00
)
type Mode int
const (
ModeBuild Mode = iota
ModeInstall
)
// -----------------------------------------------------------------------------
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, mode Mode) {
2024-04-24 14:27:14 +08:00
flags, patterns, verbose := parseArgs(args)
2024-04-24 07:55:51 +08:00
cfg := &packages.Config{
2024-04-25 00:14:02 +08:00
Mode: loadSyntax | packages.NeedDeps | packages.NeedExportFile,
2024-04-24 07:55:51 +08:00
BuildFlags: flags,
}
if patterns == nil {
patterns = []string{"."}
}
initial, err := packages.Load(cfg, patterns...)
check(err)
// Create SSA-form program representation.
2024-04-25 00:14:02 +08:00
ssaProg, pkgs, errPkgs := allPkgs(initial, ssa.SanityCheckFunctions)
ssaProg.Build()
2024-04-24 11:13:17 +08:00
for _, errPkg := range errPkgs {
log.Println("cannot build SSA for package", errPkg)
}
2024-04-24 07:55:51 +08:00
llssa.Initialize(llssa.InitAll)
2024-04-24 14:27:14 +08:00
if verbose {
llssa.SetDebug(llssa.DbgFlagAll)
cl.SetDebug(cl.DbgFlagAll)
}
2024-04-24 11:13:17 +08:00
2024-04-24 07:55:51 +08:00
prog := llssa.NewProgram(nil)
2024-04-24 11:49:43 +08:00
llFiles := make([]string, 0, len(pkgs))
2024-04-24 11:13:17 +08:00
for _, pkg := range pkgs {
2024-04-24 11:49:43 +08:00
llFiles = buildPkg(llFiles, prog, pkg, mode)
}
if mode == ModeInstall {
2024-04-25 00:14:02 +08:00
// TODO(xsw): show work
// fmt.Fprintln(os.Stderr, "clang", llFiles)
2024-04-24 11:49:43 +08:00
err = clang.New("").Exec(llFiles...)
check(err)
2024-04-24 11:13:17 +08:00
}
}
2024-04-24 11:49:43 +08:00
func buildPkg(llFiles []string, prog llssa.Program, pkg aPackage, mode Mode) []string {
2024-04-24 11:13:17 +08:00
pkgPath := pkg.PkgPath
fmt.Fprintln(os.Stderr, pkgPath)
if pkgPath == "unsafe" { // TODO(xsw): remove this special case
2024-04-24 11:49:43 +08:00
return llFiles
2024-04-24 11:13:17 +08:00
}
ret, err := cl.NewPackage(prog, pkg.SSA, pkg.Syntax)
check(err)
if mode == ModeInstall {
2024-04-24 11:49:43 +08:00
file := pkg.ExportFile + ".ll"
os.WriteFile(file, []byte(ret.String()), 0644)
llFiles = append(llFiles, file)
2024-04-24 11:13:17 +08:00
}
2024-04-24 11:49:43 +08:00
return llFiles
2024-04-24 11:13:17 +08:00
}
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
2024-04-24 07:55:51 +08:00
}
2024-04-24 11:13:17 +08:00
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
2024-04-24 07:55:51 +08:00
}
2024-04-24 14:27:14 +08:00
func parseArgs(args []string) (flags, patterns []string, verbose bool) {
2024-04-24 07:55:51 +08:00
for i, arg := range args {
if !strings.HasPrefix(arg, "-") {
2024-04-24 14:27:14 +08:00
flags, patterns = args[:i], args[i:]
return
}
if arg == "-v" {
verbose = true
2024-04-24 07:55:51 +08:00
}
}
2024-04-24 14:27:14 +08:00
flags = args
return
2024-04-24 07:55:51 +08:00
}
func check(err error) {
if err != nil {
panic(err)
}
}
// -----------------------------------------------------------------------------