Skip to content

Commit d7ac5ce

Browse files
committed
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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: David Crawshaw <[email protected]>
1 parent a212083 commit d7ac5ce

File tree

2 files changed

+24
-25
lines changed

2 files changed

+24
-25
lines changed

src/cmd/go/internal/load/pkg.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,12 +1086,31 @@ func (p *Package) load(stk *ImportStack, bp *build.Package, err error) {
10861086
// code; see issue #16050).
10871087
}
10881088

1089-
// The gc toolchain only permits C source files with cgo.
1090-
if len(p.CFiles) > 0 && !p.UsesCgo() && !p.UsesSwig() && cfg.BuildContext.Compiler == "gc" {
1089+
setError := func(msg string) {
10911090
p.Error = &PackageError{
10921091
ImportStack: stk.Copy(),
1093-
Err: fmt.Sprintf("C source files not allowed when not using cgo or SWIG: %s", strings.Join(p.CFiles, " ")),
1092+
Err: msg,
10941093
}
1094+
}
1095+
1096+
// The gc toolchain only permits C source files with cgo or SWIG.
1097+
if len(p.CFiles) > 0 && !p.UsesCgo() && !p.UsesSwig() && cfg.BuildContext.Compiler == "gc" {
1098+
setError(fmt.Sprintf("C source files not allowed when not using cgo or SWIG: %s", strings.Join(p.CFiles, " ")))
1099+
return
1100+
}
1101+
1102+
// C++, Objective-C, and Fortran source files are permitted only with cgo or SWIG,
1103+
// regardless of toolchain.
1104+
if len(p.CXXFiles) > 0 && !p.UsesCgo() && !p.UsesSwig() {
1105+
setError(fmt.Sprintf("C++ source files not allowed when not using cgo or SWIG: %s", strings.Join(p.CXXFiles, " ")))
1106+
return
1107+
}
1108+
if len(p.MFiles) > 0 && !p.UsesCgo() && !p.UsesSwig() {
1109+
setError(fmt.Sprintf("Objective-C source files not allowed when not using cgo or SWIG: %s", strings.Join(p.MFiles, " ")))
1110+
return
1111+
}
1112+
if len(p.FFiles) > 0 && !p.UsesCgo() && !p.UsesSwig() {
1113+
setError(fmt.Sprintf("Fortran source files not allowed when not using cgo or SWIG: %s", strings.Join(p.FFiles, " ")))
10951114
return
10961115
}
10971116

@@ -1100,10 +1119,7 @@ func (p *Package) load(stk *ImportStack, bp *build.Package, err error) {
11001119
if other := foldPath[fold]; other == "" {
11011120
foldPath[fold] = p.ImportPath
11021121
} else if other != p.ImportPath {
1103-
p.Error = &PackageError{
1104-
ImportStack: stk.Copy(),
1105-
Err: fmt.Sprintf("case-insensitive import collision: %q and %q", p.ImportPath, other),
1106-
}
1122+
setError(fmt.Sprintf("case-insensitive import collision: %q and %q", p.ImportPath, other))
11071123
return
11081124
}
11091125

src/cmd/go/internal/work/build.go

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,24 +1438,6 @@ func (b *Builder) build(a *Action) (err error) {
14381438
return fmt.Errorf("missing or invalid package binary for binary-only package %s", a.Package.ImportPath)
14391439
}
14401440

1441-
// Return an error if the package has CXX files but it's not using
1442-
// cgo nor SWIG, since the CXX files can only be processed by cgo
1443-
// and SWIG.
1444-
if len(a.Package.CXXFiles) > 0 && !a.Package.UsesCgo() && !a.Package.UsesSwig() {
1445-
return fmt.Errorf("can't build package %s because it contains C++ files (%s) but it's not using cgo nor SWIG",
1446-
a.Package.ImportPath, strings.Join(a.Package.CXXFiles, ","))
1447-
}
1448-
// Same as above for Objective-C files
1449-
if len(a.Package.MFiles) > 0 && !a.Package.UsesCgo() && !a.Package.UsesSwig() {
1450-
return fmt.Errorf("can't build package %s because it contains Objective-C files (%s) but it's not using cgo nor SWIG",
1451-
a.Package.ImportPath, strings.Join(a.Package.MFiles, ","))
1452-
}
1453-
// Same as above for Fortran files
1454-
if len(a.Package.FFiles) > 0 && !a.Package.UsesCgo() && !a.Package.UsesSwig() {
1455-
return fmt.Errorf("can't build package %s because it contains Fortran files (%s) but it's not using cgo nor SWIG",
1456-
a.Package.ImportPath, strings.Join(a.Package.FFiles, ","))
1457-
}
1458-
14591441
defer func() {
14601442
if err != nil && err != errPrintedOutput {
14611443
err = fmt.Errorf("go build %s: %v", a.Package.ImportPath, err)
@@ -1558,6 +1540,7 @@ func (b *Builder) build(a *Action) (err error) {
15581540
gofiles = append(gofiles, outGo...)
15591541
}
15601542

1543+
// Sanity check only, since Package.load already checked as well.
15611544
if len(gofiles) == 0 {
15621545
return &load.NoGoError{Package: a.Package}
15631546
}

0 commit comments

Comments
 (0)