Skip to content
This repository was archived by the owner on Mar 25, 2024. It is now read-only.

Commit 2634e3b

Browse files
authored
Merge pull request #52 from karalabe/keep-cgo-deps
Detect and keep CGO dependencies from subfolders.
2 parents 98d9612 + 39f6a11 commit 2634e3b

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

trash.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"crypto/sha1"
55
"encoding/hex"
66
"fmt"
7+
"go/ast"
78
"go/parser"
89
"go/token"
910
"os"
@@ -459,6 +460,8 @@ func listImports(rootPackage, libRoot, pkg string) <-chan util.Packages {
459460
}
460461
go func() {
461462
defer close(sch)
463+
464+
// Gather all the Go imports
462465
ps, err := parser.ParseDir(token.NewFileSet(), pkgPath, noVendoredTests, parser.ImportsOnly)
463466
if err != nil {
464467
if os.IsNotExist(err) {
@@ -486,6 +489,50 @@ func listImports(rootPackage, libRoot, pkg string) <-chan util.Packages {
486489
}
487490
}
488491
}
492+
// Gather all the CGO imports
493+
ps, err = parser.ParseDir(token.NewFileSet(), pkgPath, noVendoredTests, parser.ParseComments)
494+
if err != nil {
495+
if os.IsNotExist(err) {
496+
logrus.Debugf("listImports, pkgPath does not exist: %s", err)
497+
} else {
498+
logrus.Errorf("Error parsing comments, pkgPath: '%s', err: '%s'", pkgPath, err)
499+
}
500+
return
501+
}
502+
logrus.Infof("Collecting CGO imports for package '%s'", pkg)
503+
for _, p := range ps {
504+
for _, f := range p.Files {
505+
// Drill down to locate C preable definitions
506+
for _, decl := range f.Decls {
507+
d, ok := decl.(*ast.GenDecl)
508+
if !ok {
509+
continue
510+
}
511+
for _, spec := range d.Specs {
512+
s, ok := spec.(*ast.ImportSpec)
513+
if !ok || s.Path.Value != `"C"` {
514+
continue
515+
}
516+
cg := s.Doc
517+
if cg == nil && len(d.Specs) == 1 {
518+
cg = d.Doc
519+
}
520+
if cg != nil {
521+
// Extract any includes from the preamble
522+
for _, line := range strings.Split(cg.Text(), "\n") {
523+
if line = strings.TrimSpace(line); strings.HasPrefix(line, "#include \"") {
524+
if includePath := filepath.Dir(line[10 : len(line)-1]); includePath != "." {
525+
if _, err := os.Stat(filepath.Join(pkgPath, includePath)); !os.IsNotExist(err) {
526+
sch <- filepath.Clean(filepath.Join(pkg, includePath))
527+
}
528+
}
529+
}
530+
}
531+
}
532+
}
533+
}
534+
}
535+
}
489536
}()
490537
lnc := util.MergeStrChans(sch, util.OneStr(pkg))
491538
return chanPackagesFromLines(lnc)

0 commit comments

Comments
 (0)