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

Commit

Permalink
Merge pull request #52 from karalabe/keep-cgo-deps
Browse files Browse the repository at this point in the history
Detect and keep CGO dependencies from subfolders.
  • Loading branch information
imikushin authored Oct 25, 2016
2 parents 98d9612 + 39f6a11 commit 2634e3b
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions trash.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/sha1"
"encoding/hex"
"fmt"
"go/ast"
"go/parser"
"go/token"
"os"
Expand Down Expand Up @@ -459,6 +460,8 @@ func listImports(rootPackage, libRoot, pkg string) <-chan util.Packages {
}
go func() {
defer close(sch)

// Gather all the Go imports
ps, err := parser.ParseDir(token.NewFileSet(), pkgPath, noVendoredTests, parser.ImportsOnly)
if err != nil {
if os.IsNotExist(err) {
Expand Down Expand Up @@ -486,6 +489,50 @@ func listImports(rootPackage, libRoot, pkg string) <-chan util.Packages {
}
}
}
// Gather all the CGO imports
ps, err = parser.ParseDir(token.NewFileSet(), pkgPath, noVendoredTests, parser.ParseComments)
if err != nil {
if os.IsNotExist(err) {
logrus.Debugf("listImports, pkgPath does not exist: %s", err)
} else {
logrus.Errorf("Error parsing comments, pkgPath: '%s', err: '%s'", pkgPath, err)
}
return
}
logrus.Infof("Collecting CGO imports for package '%s'", pkg)
for _, p := range ps {
for _, f := range p.Files {
// Drill down to locate C preable definitions
for _, decl := range f.Decls {
d, ok := decl.(*ast.GenDecl)
if !ok {
continue
}
for _, spec := range d.Specs {
s, ok := spec.(*ast.ImportSpec)
if !ok || s.Path.Value != `"C"` {
continue
}
cg := s.Doc
if cg == nil && len(d.Specs) == 1 {
cg = d.Doc
}
if cg != nil {
// Extract any includes from the preamble
for _, line := range strings.Split(cg.Text(), "\n") {
if line = strings.TrimSpace(line); strings.HasPrefix(line, "#include \"") {
if includePath := filepath.Dir(line[10 : len(line)-1]); includePath != "." {
if _, err := os.Stat(filepath.Join(pkgPath, includePath)); !os.IsNotExist(err) {
sch <- filepath.Clean(filepath.Join(pkg, includePath))
}
}
}
}
}
}
}
}
}
}()
lnc := util.MergeStrChans(sch, util.OneStr(pkg))
return chanPackagesFromLines(lnc)
Expand Down

0 comments on commit 2634e3b

Please sign in to comment.