Merge commit '6de3bdcdd9b3959d244e7b3b7e97fc4e3408cd78' into esp-llvm

# Conflicts:
#	internal/build/build.go
This commit is contained in:
Li Jie
2025-08-21 14:19:53 +08:00
204 changed files with 161734 additions and 494 deletions

View File

@@ -38,6 +38,7 @@ import (
"golang.org/x/tools/go/ssa"
"github.com/goplus/llgo/cl"
"github.com/goplus/llgo/internal/cabi"
"github.com/goplus/llgo/internal/clang"
"github.com/goplus/llgo/internal/crosscompile"
"github.com/goplus/llgo/internal/env"
@@ -63,6 +64,8 @@ const (
ModeGen
)
type AbiMode = cabi.Mode
const (
debugBuild = packages.DebugPackagesLoad
)
@@ -76,6 +79,7 @@ type Config struct {
OutFile string // only valid for ModeBuild when len(pkgs) == 1
RunArgs []string // only valid for ModeRun
Mode Mode
AbiMode AbiMode
GenExpect bool // only valid for ModeCmpTest
Verbose bool
GenLL bool // generate pkg .ll files
@@ -108,6 +112,7 @@ func NewDefaultConf(mode Mode) *Config {
Goarch: goarch,
BinPath: bin,
Mode: mode,
AbiMode: cabi.ModeAllFunc,
AppExt: DefaultAppExt(goos),
}
return conf
@@ -170,6 +175,9 @@ func Do(args []string, conf *Config) ([]Package, error) {
if conf.Tags != "" {
tags += "," + conf.Tags
}
if len(export.BuildTags) > 0 {
tags += "," + strings.Join(export.BuildTags, ",")
}
cfg := &packages.Config{
Mode: loadSyntax | packages.NeedDeps | packages.NeedModule | packages.NeedExportFile,
BuildFlags: []string{"-tags=" + tags},
@@ -269,12 +277,14 @@ func Do(args []string, conf *Config) ([]Package, error) {
output := conf.OutFile != ""
ctx := &context{env: env, conf: cfg, progSSA: progSSA, prog: prog, dedup: dedup,
patches: patches, built: make(map[string]none), initial: initial, mode: mode,
output: output,
needRt: make(map[*packages.Package]bool),
needPyInit: make(map[*packages.Package]bool),
buildConf: conf,
crossCompile: export,
isCheckEnable: IsCheckEnable(),
output: output,
needRt: make(map[*packages.Package]bool),
needPyInit: make(map[*packages.Package]bool),
buildConf: conf,
crossCompile: export,
isCheckEnabled: IsCheckEnabled(),
isCheckLinkArgsEnabled: IsCheckLinkArgsEnabled(),
cTransformer: cabi.NewTransformer(prog, conf.AbiMode),
}
pkgs, err := buildAllPkgs(ctx, initial, verbose)
check(err)
@@ -364,7 +374,8 @@ type context struct {
nLibdir int
output bool
isCheckEnable bool
isCheckEnabled bool
isCheckLinkArgsEnabled bool
needRt map[*packages.Package]bool
needPyInit map[*packages.Package]bool
@@ -372,6 +383,8 @@ type context struct {
buildConf *Config
crossCompile crosscompile.Export
cTransformer *cabi.Transformer
testFail bool
}
@@ -457,8 +470,10 @@ func buildAllPkgs(ctx *context, initial []*packages.Package, verbose bool) (pkgs
ctx.nLibdir++
}
}
if err := ctx.compiler().CheckLinkArgs(pkgLinkArgs, isWasmTarget(ctx.buildConf.Goos)); err != nil {
panic(fmt.Sprintf("test link args '%s' failed\n\texpanded to: %v\n\tresolved to: %v\n\terror: %v", param, expdArgs, pkgLinkArgs, err))
if ctx.isCheckLinkArgsEnabled {
if err := ctx.compiler().CheckLinkArgs(pkgLinkArgs, isWasmTarget(ctx.buildConf.Goos)); err != nil {
panic(fmt.Sprintf("test link args '%s' failed\n\texpanded to: %v\n\tresolved to: %v\n\terror: %v", param, expdArgs, pkgLinkArgs, err))
}
}
aPkg.LinkArgs = append(aPkg.LinkArgs, pkgLinkArgs...)
}
@@ -585,7 +600,19 @@ func linkMainPkg(ctx *context, pkg *packages.Package, pkgs []*aPackage, global l
objFiles = append(objFiles, export)
}
err = linkObjFiles(ctx, app, objFiles, linkArgs, verbose)
if IsFullRpathEnabled() {
exargs := make([]string, 0, ctx.nLibdir<<1)
// Treat every link-time library search path, specified by the -L parameter, as a runtime search path as well.
// This is to ensure the final executable can locate libraries with a relocatable install_name
// (e.g., "@rpath/libfoo.dylib") at runtime.
for _, arg := range linkArgs {
if strings.HasPrefix(arg, "-L") {
exargs = append(exargs, "-rpath", arg[2:])
}
}
linkArgs = append(linkArgs, exargs...)
}
err = linkObjFiles(ctx, app, objFiles, linkArgs, verbose)
check(err)
@@ -720,8 +747,7 @@ call i32 @setvbuf(ptr %stderr_ptr, ptr null, i32 2, %size_t 0)
startDefine := `
define weak void @_start() {
; argc = 0
%argc_val = icmp eq i32 0, 0
%argc = zext i1 %argc_val to i32
%argc = add i32 0, 0
; argv = null
%argv = inttoptr i64 0 to i8**
call i32 @main(i32 %argc, i8** %argv)
@@ -806,6 +832,9 @@ func buildPkg(ctx *context, aPkg *aPackage, verbose bool) error {
cl.SetDebug(0)
}
check(err)
ctx.cTransformer.TransformModule(ret.Path(), ret.Module())
aPkg.LPkg = ret
cgoLLFiles, cgoLdflags, err := buildCgo(ctx, aPkg, aPkg.Package.Syntax, externs, verbose)
if err != nil {
@@ -845,7 +874,7 @@ func exportObject(ctx *context, pkgPath string, exportFile string, data []byte)
if err != nil {
return exportFile, err
}
if ctx.isCheckEnable {
if ctx.isCheckEnabled {
if msg, err := llcCheck(ctx.env, f.Name()); err != nil {
fmt.Fprintf(os.Stderr, "==> lcc %v: %v\n%v\n", pkgPath, f.Name(), msg)
}
@@ -989,6 +1018,8 @@ const llgoCheck = "LLGO_CHECK"
const llgoWasmRuntime = "LLGO_WASM_RUNTIME"
const llgoWasiThreads = "LLGO_WASI_THREADS"
const llgoStdioNobuf = "LLGO_STDIO_NOBUF"
const llgoCheckLinkArgs = "LLGO_CHECK_LINKARGS"
const llgoFullRpath = "LLGO_FULL_RPATH"
const defaultWasmRuntime = "wasmtime"
@@ -1028,7 +1059,7 @@ func IsOptimizeEnabled() bool {
return isEnvOn(llgoOptimize, true)
}
func IsCheckEnable() bool {
func IsCheckEnabled() bool {
return isEnvOn(llgoCheck, false)
}
@@ -1036,6 +1067,14 @@ func IsWasiThreadsEnabled() bool {
return isEnvOn(llgoWasiThreads, true)
}
func IsCheckLinkArgsEnabled() bool {
return isEnvOn(llgoCheckLinkArgs, false)
}
func IsFullRpathEnabled() bool {
return isEnvOn(llgoFullRpath, true)
}
func WasmRuntime() string {
return defaultEnv(llgoWasmRuntime, defaultWasmRuntime)
}