Skip to content

Commit

Permalink
build: support rpath
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Jul 15, 2024
1 parent 0b0cecc commit 2fcfac9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
2 changes: 1 addition & 1 deletion chore/_xtool/astdump/build.sh
Original file line number Diff line number Diff line change
@@ -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
25 changes: 22 additions & 3 deletions internal/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func Do(args []string, conf *Config) {
env := llvm.New("")
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)

var llFiles []string
Expand Down Expand Up @@ -232,6 +232,7 @@ type context struct {
built map[string]none
initial []*packages.Package
mode Mode
nLibdir int
}

func buildAllPkgs(ctx *context, initial []*packages.Package, verbose bool) (pkgs []*aPackage) {
Expand Down Expand Up @@ -273,7 +274,13 @@ func buildAllPkgs(ctx *context, initial []*packages.Package, verbose bool) (pkgs
expd := ""
altParts := strings.Split(param, ";")
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 {
break
}
Expand All @@ -291,6 +298,7 @@ func buildAllPkgs(ctx *context, initial []*packages.Package, verbose bool) (pkgs
command = " -l " + lib
if dir != "" {
command += " -L " + dir[:len(dir)-1]
ctx.nLibdir++
}
}
if err := clangCheck.CheckLinkArgs(command); err != nil {
Expand All @@ -317,10 +325,12 @@ func linkMainPkg(ctx *context, pkg *packages.Package, pkgs []*aPackage, llFiles
if app == "" {
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,
"-o", app,
"-rpath", "@loader_path",
"-rpath", "@loader_path/../lib",
"-fuse-ld=lld",
"-Wno-override-module",
// "-O2", // FIXME: This will cause TestFinalizer in _test/bdwgc.go to fail on macOS.
Expand Down Expand Up @@ -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
if verbose {
fmt.Fprintln(os.Stderr, "clang", args)
Expand Down

0 comments on commit 2fcfac9

Please sign in to comment.