// 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
}
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
}
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)
gofiles = append(gofiles, outGo...)
}
+ // Sanity check only, since Package.load already checked as well.
if len(gofiles) == 0 {
return &load.NoGoError{Package: a.Package}
}