go.tools/go/loader: apply DisplayPath to working directory of cgo tool.

This transforms the virtualized directory (build.Context).Dir to a physical one,
for proprietary build systems that distinguish them.

LGTM=gri
R=gri
CC=golang-codereviews
https://golang.org/cl/116230043
This commit is contained in:
Alan Donovan 2014-07-24 14:12:52 -04:00
parent 9ffbf29971
commit 935700a081
2 changed files with 11 additions and 6 deletions

View File

@ -65,14 +65,19 @@ import (
// processCgoFiles invokes the cgo preprocessor on bp.CgoFiles, parses // processCgoFiles 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, mode parser.Mode) ([]*ast.File, error) { func processCgoFiles(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
} }
defer os.RemoveAll(tmpdir) defer os.RemoveAll(tmpdir)
cgoFiles, cgoDisplayFiles, err := runCgo(bp, tmpdir) pkgdir := bp.Dir
if DisplayPath != nil {
pkgdir = DisplayPath(pkgdir)
}
cgoFiles, cgoDisplayFiles, err := runCgo(bp, pkgdir, tmpdir)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -103,7 +108,7 @@ var cgoRe = regexp.MustCompile(`[/\\:]`)
// $GOROOT/src/cmd/go/build.go, but these features are unsupported: // $GOROOT/src/cmd/go/build.go, but these features are unsupported:
// pkg-config, Objective C, CGOPKGPATH, CGO_FLAGS. // pkg-config, Objective C, CGOPKGPATH, CGO_FLAGS.
// //
func runCgo(bp *build.Package, tmpdir string) (files, displayFiles []string, err error) { func runCgo(bp *build.Package, pkgdir, tmpdir string) (files, displayFiles []string, err error) {
cgoCPPFLAGS, _, _, _ := cflags(bp, true) cgoCPPFLAGS, _, _, _ := cflags(bp, true)
_, cgoexeCFLAGS, _, _ := cflags(bp, false) _, cgoexeCFLAGS, _, _ := cflags(bp, false)
@ -137,10 +142,10 @@ func runCgo(bp *build.Package, tmpdir string) (files, displayFiles []string, err
cgoCPPFLAGS, cgoexeCFLAGS, bp.CgoFiles, cgoCPPFLAGS, cgoexeCFLAGS, bp.CgoFiles,
) )
if false { if false {
log.Printf("Running cgo for package %q: %s", bp.ImportPath, args) log.Printf("Running cgo for package %q: %s (dir=%s)", bp.ImportPath, args, pkgdir)
} }
cmd := exec.Command(args[0], args[1:]...) cmd := exec.Command(args[0], args[1:]...)
cmd.Dir = bp.Dir cmd.Dir = pkgdir
cmd.Stdout = os.Stderr cmd.Stdout = os.Stderr
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil { if err := cmd.Run(); err != nil {

View File

@ -682,7 +682,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 := processCgoFiles(bp, conf.fset(), conf.ParserMode) cgofiles, err := processCgoFiles(bp, conf.fset(), conf.DisplayPath, conf.ParserMode)
if err != nil { if err != nil {
errs = append(errs, err) errs = append(errs, err)
} else { } else {