From d7ac5ce8a3d0cc0a77e4ea5dc7ef1aa4b39db2f5 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Thu, 5 Oct 2017 19:30:51 -0400 Subject: [PATCH] cmd/go: hoist C++, Objective-C, and Fortran checks earlier Today they happen during the build phase; they should happen during the load phase instead, along with the C check. Change-Id: I6074a995b8e29275549aafa574511b735642d85b Reviewed-on: https://go-review.googlesource.com/69051 Run-TryBot: Russ Cox TryBot-Result: Gobot Gobot Reviewed-by: David Crawshaw --- src/cmd/go/internal/load/pkg.go | 30 +++++++++++++++++++++++------- src/cmd/go/internal/work/build.go | 19 +------------------ 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/src/cmd/go/internal/load/pkg.go b/src/cmd/go/internal/load/pkg.go index 4bbf3e65ea..1d89512b66 100644 --- a/src/cmd/go/internal/load/pkg.go +++ b/src/cmd/go/internal/load/pkg.go @@ -1086,12 +1086,31 @@ func (p *Package) load(stk *ImportStack, bp *build.Package, err error) { // code; see issue #16050). } - // The gc toolchain only permits C source files with cgo. - if len(p.CFiles) > 0 && !p.UsesCgo() && !p.UsesSwig() && cfg.BuildContext.Compiler == "gc" { + setError := func(msg string) { p.Error = &PackageError{ ImportStack: stk.Copy(), - Err: fmt.Sprintf("C source files not allowed when not using cgo or SWIG: %s", strings.Join(p.CFiles, " ")), + Err: msg, } + } + + // The gc toolchain only permits C source files with cgo or SWIG. + if len(p.CFiles) > 0 && !p.UsesCgo() && !p.UsesSwig() && cfg.BuildContext.Compiler == "gc" { + setError(fmt.Sprintf("C source files not allowed when not using cgo or SWIG: %s", strings.Join(p.CFiles, " "))) + return + } + + // C++, Objective-C, and Fortran source files are permitted only with cgo or SWIG, + // regardless of toolchain. + if len(p.CXXFiles) > 0 && !p.UsesCgo() && !p.UsesSwig() { + setError(fmt.Sprintf("C++ source files not allowed when not using cgo or SWIG: %s", strings.Join(p.CXXFiles, " "))) + return + } + if len(p.MFiles) > 0 && !p.UsesCgo() && !p.UsesSwig() { + setError(fmt.Sprintf("Objective-C source files not allowed when not using cgo or SWIG: %s", strings.Join(p.MFiles, " "))) + return + } + if len(p.FFiles) > 0 && !p.UsesCgo() && !p.UsesSwig() { + setError(fmt.Sprintf("Fortran source files not allowed when not using cgo or SWIG: %s", strings.Join(p.FFiles, " "))) return } @@ -1100,10 +1119,7 @@ func (p *Package) load(stk *ImportStack, bp *build.Package, err error) { if other := foldPath[fold]; other == "" { foldPath[fold] = p.ImportPath } else if other != p.ImportPath { - p.Error = &PackageError{ - ImportStack: stk.Copy(), - Err: fmt.Sprintf("case-insensitive import collision: %q and %q", p.ImportPath, other), - } + setError(fmt.Sprintf("case-insensitive import collision: %q and %q", p.ImportPath, other)) return } diff --git a/src/cmd/go/internal/work/build.go b/src/cmd/go/internal/work/build.go index e20696120d..4fc39f9411 100644 --- a/src/cmd/go/internal/work/build.go +++ b/src/cmd/go/internal/work/build.go @@ -1438,24 +1438,6 @@ func (b *Builder) build(a *Action) (err error) { return fmt.Errorf("missing or invalid package binary for binary-only package %s", a.Package.ImportPath) } - // Return an error if the package has CXX files but it's not using - // cgo nor SWIG, since the CXX files can only be processed by cgo - // and SWIG. - if len(a.Package.CXXFiles) > 0 && !a.Package.UsesCgo() && !a.Package.UsesSwig() { - return fmt.Errorf("can't build package %s because it contains C++ files (%s) but it's not using cgo nor SWIG", - a.Package.ImportPath, strings.Join(a.Package.CXXFiles, ",")) - } - // Same as above for Objective-C files - if len(a.Package.MFiles) > 0 && !a.Package.UsesCgo() && !a.Package.UsesSwig() { - return fmt.Errorf("can't build package %s because it contains Objective-C files (%s) but it's not using cgo nor SWIG", - a.Package.ImportPath, strings.Join(a.Package.MFiles, ",")) - } - // Same as above for Fortran files - if len(a.Package.FFiles) > 0 && !a.Package.UsesCgo() && !a.Package.UsesSwig() { - return fmt.Errorf("can't build package %s because it contains Fortran files (%s) but it's not using cgo nor SWIG", - a.Package.ImportPath, strings.Join(a.Package.FFiles, ",")) - } - defer func() { if err != nil && err != errPrintedOutput { err = fmt.Errorf("go build %s: %v", a.Package.ImportPath, err) @@ -1558,6 +1540,7 @@ func (b *Builder) build(a *Action) (err error) { gofiles = append(gofiles, outGo...) } + // Sanity check only, since Package.load already checked as well. if len(gofiles) == 0 { return &load.NoGoError{Package: a.Package} } -- 2.48.1