]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: hoist C++, Objective-C, and Fortran checks earlier
authorRuss Cox <rsc@golang.org>
Thu, 5 Oct 2017 23:30:51 +0000 (19:30 -0400)
committerRuss Cox <rsc@golang.org>
Wed, 11 Oct 2017 17:47:14 +0000 (17:47 +0000)
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 <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: David Crawshaw <crawshaw@golang.org>
src/cmd/go/internal/load/pkg.go
src/cmd/go/internal/work/build.go

index 4bbf3e65ea18d5ae205216637b335e7156fbb761..1d89512b669db0797fd8660103f4648f263097df 100644 (file)
@@ -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
        }
 
index e20696120d1a67ad57c89479e3391e233a995bac..4fc39f94119c94cc722c5e0e6bf258b27c7e007f 100644 (file)
@@ -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}
        }