1
0
mirror of https://github.com/golang/go synced 2024-11-18 17:54:57 -07:00

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:
Michael Matloob 2018-08-07 15:25:39 -04:00
parent 201986631b
commit 62181fabb0
3 changed files with 21 additions and 10 deletions

View File

@ -66,10 +66,10 @@ import (
"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.
//
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")
if err != nil {
return nil, err
@ -81,7 +81,7 @@ func ProcessCgoFiles(bp *build.Package, fset *token.FileSet, DisplayPath func(pa
pkgdir = DisplayPath(pkgdir)
}
cgoFiles, cgoDisplayFiles, err := RunCgo(bp, pkgdir, tmpdir, false)
cgoFiles, cgoDisplayFiles, err := Run(bp, pkgdir, tmpdir, false)
if err != nil {
return nil, err
}
@ -104,15 +104,20 @@ func ProcessCgoFiles(bp *build.Package, fset *token.FileSet, DisplayPath func(pa
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
// 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:
// 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)
_, cgoexeCFLAGS, _, _ := cflags(bp, false)

View File

@ -755,7 +755,7 @@ func (conf *Config) parsePackageFiles(bp *build.Package, which rune) ([]*ast.Fil
// Preprocess CgoFiles and parse the outputs (sequentially).
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 {
errs = append(errs, err)
} else {

View File

@ -72,20 +72,24 @@ func golistPackagesFallback(ctx context.Context, cfg *raw.Config, words ...strin
}
compiledGoFiles := absJoin(p.Dir, p.GoFiles)
// Use a function to simplify control flow. It's just a bunch of gotos.
var cgoErrors []error
processCgo := func() bool {
// Suppress any cgo errors. Any relevant errors will show up in typechecking.
// TODO(matloob): Skip running cgo if Mode < LoadTypes.
if tmpdir == "" {
if tmpdir, err = ioutil.TempDir("", "gopackages"); err != nil {
cgoErrors = append(cgoErrors, err)
return false
}
}
outdir := filepath.Join(tmpdir, strings.Replace(p.ImportPath, "/", "_", -1))
if err := os.Mkdir(outdir, 0755); err != nil {
cgoErrors = append(cgoErrors, err)
return false
}
files, _, err := runCgo(p.Dir, outdir, cfg.Env)
if err != nil {
cgoErrors = append(cgoErrors, err)
return false
}
compiledGoFiles = append(compiledGoFiles, files...)
@ -98,10 +102,11 @@ func golistPackagesFallback(ctx context.Context, cfg *raw.Config, words ...strin
ID: id,
Name: p.Name,
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),
PkgPath: pkgpath,
Imports: importMap(p.Imports),
// TODO(matloob): set errors on the Package to cgoErrors
})
if cfg.Tests {
testID := fmt.Sprintf("%s [%s.test]", id, id)
@ -113,10 +118,11 @@ func golistPackagesFallback(ctx context.Context, cfg *raw.Config, words ...strin
ID: testID,
Name: p.Name,
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),
PkgPath: pkgpath,
Imports: importMap(append(p.Imports, p.TestImports...)),
// TODO(matloob): set errors on the Package to cgoErrors
})
}
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)...)
}
}
return cgo.RunCgo(bp, pkgdir, tmpdir, true)
return cgo.Run(bp, pkgdir, tmpdir, true)
}