From 62181fabb05016ba7c8336c12935ee8579066575 Mon Sep 17 00:00:00 2001 From: Michael Matloob Date: Tue, 7 Aug 2018 15:25:39 -0400 Subject: [PATCH] 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 TryBot-Result: Gobot Gobot Reviewed-by: Alan Donovan --- go/internal/cgo/cgo.go | 17 +++++++++++------ go/loader/loader.go | 2 +- go/packages/golist/golist_fallback.go | 12 +++++++++--- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/go/internal/cgo/cgo.go b/go/internal/cgo/cgo.go index be03df7e..0f652ea6 100644 --- a/go/internal/cgo/cgo.go +++ b/go/internal/cgo/cgo.go @@ -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) diff --git a/go/loader/loader.go b/go/loader/loader.go index d6d2bbb4..c4566611 100644 --- a/go/loader/loader.go +++ b/go/loader/loader.go @@ -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 { diff --git a/go/packages/golist/golist_fallback.go b/go/packages/golist/golist_fallback.go index 0f8ac265..38b943a3 100644 --- a/go/packages/golist/golist_fallback.go +++ b/go/packages/golist/golist_fallback.go @@ -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) }