Merge pull request #515 from xushiwei/q

build: support rpath
This commit is contained in:
xushiwei
2024-07-15 11:14:13 +08:00
committed by GitHub
2 changed files with 23 additions and 4 deletions

View File

@@ -1,2 +1,2 @@
export LLVM_DIR=/opt/homebrew/Cellar/llvm@17/17.0.6 export LLVM_DIR=$(llvm-config --prefix)
clang -L$LLVM_DIR/lib -lclang -lc++ -I$LLVM_DIR/include astdump.cpp clang -L$LLVM_DIR/lib -lclang -lc++ -I$LLVM_DIR/include astdump.cpp

View File

@@ -177,7 +177,7 @@ func Do(args []string, conf *Config) {
env := llvm.New("") env := llvm.New("")
os.Setenv("PATH", env.BinDir()+":"+os.Getenv("PATH")) // TODO(xsw): check windows os.Setenv("PATH", env.BinDir()+":"+os.Getenv("PATH")) // TODO(xsw): check windows
ctx := &context{env, progSSA, prog, dedup, patches, make(map[string]none), initial, mode} ctx := &context{env, progSSA, prog, dedup, patches, make(map[string]none), initial, mode, 0}
pkgs := buildAllPkgs(ctx, initial, verbose) pkgs := buildAllPkgs(ctx, initial, verbose)
var llFiles []string var llFiles []string
@@ -232,6 +232,7 @@ type context struct {
built map[string]none built map[string]none
initial []*packages.Package initial []*packages.Package
mode Mode mode Mode
nLibdir int
} }
func buildAllPkgs(ctx *context, initial []*packages.Package, verbose bool) (pkgs []*aPackage) { func buildAllPkgs(ctx *context, initial []*packages.Package, verbose bool) (pkgs []*aPackage) {
@@ -273,7 +274,13 @@ func buildAllPkgs(ctx *context, initial []*packages.Package, verbose bool) (pkgs
expd := "" expd := ""
altParts := strings.Split(param, ";") altParts := strings.Split(param, ";")
for _, param := range altParts { for _, param := range altParts {
expd = strings.TrimSpace(env.ExpandEnv(strings.TrimSpace(param))) param = strings.TrimSpace(param)
if strings.ContainsRune(param, '$') {
expd = strings.TrimSpace(env.ExpandEnv(param))
ctx.nLibdir++
} else {
expd = param
}
if len(expd) > 0 { if len(expd) > 0 {
break break
} }
@@ -291,6 +298,7 @@ func buildAllPkgs(ctx *context, initial []*packages.Package, verbose bool) (pkgs
command = " -l " + lib command = " -l " + lib
if dir != "" { if dir != "" {
command += " -L " + dir[:len(dir)-1] command += " -L " + dir[:len(dir)-1]
ctx.nLibdir++
} }
} }
if err := clangCheck.CheckLinkArgs(command); err != nil { if err := clangCheck.CheckLinkArgs(command); err != nil {
@@ -317,10 +325,12 @@ func linkMainPkg(ctx *context, pkg *packages.Package, pkgs []*aPackage, llFiles
if app == "" { if app == "" {
app = filepath.Join(conf.BinPath, name+conf.AppExt) app = filepath.Join(conf.BinPath, name+conf.AppExt)
} }
args := make([]string, 0, len(pkg.Imports)+len(llFiles)+10) args := make([]string, 0, len(pkg.Imports)+len(llFiles)+16)
args = append( args = append(
args, args,
"-o", app, "-o", app,
"-rpath", "@loader_path",
"-rpath", "@loader_path/../lib",
"-fuse-ld=lld", "-fuse-ld=lld",
"-Wno-override-module", "-Wno-override-module",
// "-O2", // FIXME: This will cause TestFinalizer in _test/bdwgc.go to fail on macOS. // "-O2", // FIXME: This will cause TestFinalizer in _test/bdwgc.go to fail on macOS.
@@ -391,6 +401,15 @@ func linkMainPkg(ctx *context, pkg *packages.Package, pkgs []*aPackage, llFiles
} }
}() }()
// add rpath
exargs := make([]string, 0, ctx.nLibdir<<1)
for _, arg := range args {
if strings.HasPrefix(arg, "-L") {
exargs = append(exargs, "-rpath", arg[2:])
}
}
args = append(args, exargs...)
// TODO(xsw): show work // TODO(xsw): show work
if verbose { if verbose {
fmt.Fprintln(os.Stderr, "clang", args) fmt.Fprintln(os.Stderr, "clang", args)