go/internal/cgo: simplify names of ProcessCgoFiles and RunCgo
Also collect errors so we can set them on the Package (once golang.org/cl/128120 is in). Change-Id: I2950405404f060312813e4aa27393496078a3b7e Reviewed-on: https://go-review.googlesource.com/128357 Run-TryBot: Michael Matloob <matloob@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Alan Donovan <adonovan@google.com>
This commit is contained in:
parent
201986631b
commit
62181fabb0
|
@ -66,10 +66,10 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ProcessCgoFiles invokes the cgo preprocessor on bp.CgoFiles, parses
|
// ProcessFiles invokes the cgo preprocessor on bp.CgoFiles, parses
|
||||||
// the output and returns the resulting ASTs.
|
// the output and returns the resulting ASTs.
|
||||||
//
|
//
|
||||||
func ProcessCgoFiles(bp *build.Package, fset *token.FileSet, DisplayPath func(path string) string, mode parser.Mode) ([]*ast.File, error) {
|
func ProcessFiles(bp *build.Package, fset *token.FileSet, DisplayPath func(path string) string, mode parser.Mode) ([]*ast.File, error) {
|
||||||
tmpdir, err := ioutil.TempDir("", strings.Replace(bp.ImportPath, "/", "_", -1)+"_C")
|
tmpdir, err := ioutil.TempDir("", strings.Replace(bp.ImportPath, "/", "_", -1)+"_C")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -81,7 +81,7 @@ func ProcessCgoFiles(bp *build.Package, fset *token.FileSet, DisplayPath func(pa
|
||||||
pkgdir = DisplayPath(pkgdir)
|
pkgdir = DisplayPath(pkgdir)
|
||||||
}
|
}
|
||||||
|
|
||||||
cgoFiles, cgoDisplayFiles, err := RunCgo(bp, pkgdir, tmpdir, false)
|
cgoFiles, cgoDisplayFiles, err := Run(bp, pkgdir, tmpdir, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -104,15 +104,20 @@ func ProcessCgoFiles(bp *build.Package, fset *token.FileSet, DisplayPath func(pa
|
||||||
|
|
||||||
var cgoRe = regexp.MustCompile(`[/\\:]`)
|
var cgoRe = regexp.MustCompile(`[/\\:]`)
|
||||||
|
|
||||||
// RunCgo invokes the cgo preprocessor on bp.CgoFiles and returns two
|
// Run invokes the cgo preprocessor on bp.CgoFiles and returns two
|
||||||
// lists of files: the resulting processed files (in temporary
|
// lists of files: the resulting processed files (in temporary
|
||||||
// directory tmpdir) and the corresponding names of the unprocessed files.
|
// directory tmpdir) and the corresponding names of the unprocessed files.
|
||||||
//
|
//
|
||||||
// runCgo is adapted from (*builder).cgo in
|
// Run is adapted from (*builder).cgo in
|
||||||
// $GOROOT/src/cmd/go/build.go, but these features are unsupported:
|
// $GOROOT/src/cmd/go/build.go, but these features are unsupported:
|
||||||
// Objective C, CGOPKGPATH, CGO_FLAGS.
|
// Objective C, CGOPKGPATH, CGO_FLAGS.
|
||||||
//
|
//
|
||||||
func RunCgo(bp *build.Package, pkgdir, tmpdir string, useabs bool) (files, displayFiles []string, err error) {
|
// If useabs is set to true, absolute paths of the bp.CgoFiles will be passed in
|
||||||
|
// to the cgo preprocessor. This in turn will set the // line comments
|
||||||
|
// referring to those files to use absolute paths. This is needed for
|
||||||
|
// go/packages using the legacy go list support so it is able to find
|
||||||
|
// the original files.
|
||||||
|
func Run(bp *build.Package, pkgdir, tmpdir string, useabs bool) (files, displayFiles []string, err error) {
|
||||||
cgoCPPFLAGS, _, _, _ := cflags(bp, true)
|
cgoCPPFLAGS, _, _, _ := cflags(bp, true)
|
||||||
_, cgoexeCFLAGS, _, _ := cflags(bp, false)
|
_, cgoexeCFLAGS, _, _ := cflags(bp, false)
|
||||||
|
|
||||||
|
|
|
@ -755,7 +755,7 @@ func (conf *Config) parsePackageFiles(bp *build.Package, which rune) ([]*ast.Fil
|
||||||
|
|
||||||
// Preprocess CgoFiles and parse the outputs (sequentially).
|
// Preprocess CgoFiles and parse the outputs (sequentially).
|
||||||
if which == 'g' && bp.CgoFiles != nil {
|
if which == 'g' && bp.CgoFiles != nil {
|
||||||
cgofiles, err := cgo.ProcessCgoFiles(bp, conf.fset(), conf.DisplayPath, conf.ParserMode)
|
cgofiles, err := cgo.ProcessFiles(bp, conf.fset(), conf.DisplayPath, conf.ParserMode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errs = append(errs, err)
|
errs = append(errs, err)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -72,20 +72,24 @@ func golistPackagesFallback(ctx context.Context, cfg *raw.Config, words ...strin
|
||||||
}
|
}
|
||||||
compiledGoFiles := absJoin(p.Dir, p.GoFiles)
|
compiledGoFiles := absJoin(p.Dir, p.GoFiles)
|
||||||
// Use a function to simplify control flow. It's just a bunch of gotos.
|
// Use a function to simplify control flow. It's just a bunch of gotos.
|
||||||
|
var cgoErrors []error
|
||||||
processCgo := func() bool {
|
processCgo := func() bool {
|
||||||
// Suppress any cgo errors. Any relevant errors will show up in typechecking.
|
// Suppress any cgo errors. Any relevant errors will show up in typechecking.
|
||||||
// TODO(matloob): Skip running cgo if Mode < LoadTypes.
|
// TODO(matloob): Skip running cgo if Mode < LoadTypes.
|
||||||
if tmpdir == "" {
|
if tmpdir == "" {
|
||||||
if tmpdir, err = ioutil.TempDir("", "gopackages"); err != nil {
|
if tmpdir, err = ioutil.TempDir("", "gopackages"); err != nil {
|
||||||
|
cgoErrors = append(cgoErrors, err)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
outdir := filepath.Join(tmpdir, strings.Replace(p.ImportPath, "/", "_", -1))
|
outdir := filepath.Join(tmpdir, strings.Replace(p.ImportPath, "/", "_", -1))
|
||||||
if err := os.Mkdir(outdir, 0755); err != nil {
|
if err := os.Mkdir(outdir, 0755); err != nil {
|
||||||
|
cgoErrors = append(cgoErrors, err)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
files, _, err := runCgo(p.Dir, outdir, cfg.Env)
|
files, _, err := runCgo(p.Dir, outdir, cfg.Env)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
cgoErrors = append(cgoErrors, err)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
compiledGoFiles = append(compiledGoFiles, files...)
|
compiledGoFiles = append(compiledGoFiles, files...)
|
||||||
|
@ -98,10 +102,11 @@ func golistPackagesFallback(ctx context.Context, cfg *raw.Config, words ...strin
|
||||||
ID: id,
|
ID: id,
|
||||||
Name: p.Name,
|
Name: p.Name,
|
||||||
GoFiles: absJoin(p.Dir, p.GoFiles, p.CgoFiles),
|
GoFiles: absJoin(p.Dir, p.GoFiles, p.CgoFiles),
|
||||||
CompiledGoFiles: compiledGoFiles, // TODO(matloob): Use cgo-processed Go files instead of p.GoFiles
|
CompiledGoFiles: compiledGoFiles,
|
||||||
OtherFiles: absJoin(p.Dir, p.SFiles, p.CFiles),
|
OtherFiles: absJoin(p.Dir, p.SFiles, p.CFiles),
|
||||||
PkgPath: pkgpath,
|
PkgPath: pkgpath,
|
||||||
Imports: importMap(p.Imports),
|
Imports: importMap(p.Imports),
|
||||||
|
// TODO(matloob): set errors on the Package to cgoErrors
|
||||||
})
|
})
|
||||||
if cfg.Tests {
|
if cfg.Tests {
|
||||||
testID := fmt.Sprintf("%s [%s.test]", id, id)
|
testID := fmt.Sprintf("%s [%s.test]", id, id)
|
||||||
|
@ -113,10 +118,11 @@ func golistPackagesFallback(ctx context.Context, cfg *raw.Config, words ...strin
|
||||||
ID: testID,
|
ID: testID,
|
||||||
Name: p.Name,
|
Name: p.Name,
|
||||||
GoFiles: absJoin(p.Dir, p.GoFiles, p.CgoFiles, p.TestGoFiles),
|
GoFiles: absJoin(p.Dir, p.GoFiles, p.CgoFiles, p.TestGoFiles),
|
||||||
CompiledGoFiles: append(compiledGoFiles, absJoin(p.Dir, p.TestGoFiles)...), // TODO(matloob): Can there be cgo files in the tests?
|
CompiledGoFiles: append(compiledGoFiles, absJoin(p.Dir, p.TestGoFiles)...),
|
||||||
OtherFiles: absJoin(p.Dir, p.SFiles, p.CFiles),
|
OtherFiles: absJoin(p.Dir, p.SFiles, p.CFiles),
|
||||||
PkgPath: pkgpath,
|
PkgPath: pkgpath,
|
||||||
Imports: importMap(append(p.Imports, p.TestImports...)),
|
Imports: importMap(append(p.Imports, p.TestImports...)),
|
||||||
|
// TODO(matloob): set errors on the Package to cgoErrors
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
if len(p.XTestGoFiles) > 0 {
|
if len(p.XTestGoFiles) > 0 {
|
||||||
|
@ -259,5 +265,5 @@ func runCgo(pkgdir, tmpdir string, env []string) (files, displayfiles []string,
|
||||||
bp.CgoLDFLAGS = append(bp.CgoLDFLAGS, strings.Fields(v)...)
|
bp.CgoLDFLAGS = append(bp.CgoLDFLAGS, strings.Fields(v)...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return cgo.RunCgo(bp, pkgdir, tmpdir, true)
|
return cgo.Run(bp, pkgdir, tmpdir, true)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue