/
shlibdeps.go
58 lines (53 loc) · 1.22 KB
/
shlibdeps.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package build
import (
"errors"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
)
var lddRe = regexp.MustCompile(`^\t([^ ]+) => (/ro/[^/]+[^\s]+)`)
var errLddFailed = errors.New("ldd failed") // sentinel
type libDep struct {
pkg string
path string
basename string
}
func findShlibDeps(ldd, fn string, env []string) ([]libDep, error) {
cmd := exec.Command(ldd, fn)
cmd.Env = env
cmd.Stderr = os.Stderr
out, err := cmd.Output()
if err != nil {
// TODO: do not print an error for wrapper programs
log.Printf("TODO: exclude file %s: %v (out: %s)", fn, err, string(out))
return nil, errLddFailed // TODO: fix
return nil, err
}
var pkgs []libDep
for _, line := range strings.Split(strings.TrimSpace(string(out)), "\n") {
matches := lddRe.FindStringSubmatch(line)
if matches == nil {
continue
}
path, err := filepath.EvalSymlinks(matches[2])
if err != nil {
return nil, err
}
var pkg string
if strings.HasPrefix(path, "/ro/") {
pkg = strings.TrimPrefix(path, "/ro/")
if idx := strings.IndexByte(pkg, '/'); idx > -1 {
pkg = pkg[:idx]
}
}
pkgs = append(pkgs, libDep{
pkg: pkg,
path: path,
basename: filepath.Base(matches[2]),
})
}
return pkgs, nil
}