Skip to content

Commit ecd7b7e

Browse files
author
Bryan C. Mills
committed
Revert "cmd/go: support cgo files in overlays"
This reverts CL 267197. Reason for revert: tests failing on windows-amd64-longtest builder (https://build.golang.org/log/83afde1aac3ea49debddb8996d089a741c3b1cf1). Change-Id: I0d7c706144e34b1715316ca2ee9662c901b7f8f5 Reviewed-on: https://go-review.googlesource.com/c/go/+/267357 Trust: Bryan C. Mills <[email protected]> Run-TryBot: Bryan C. Mills <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> TryBot-Result: Go Bot <[email protected]>
1 parent 393f2bb commit ecd7b7e

File tree

4 files changed

+12
-156
lines changed

4 files changed

+12
-156
lines changed

src/cmd/go/internal/work/exec.go

Lines changed: 7 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package work
88

99
import (
1010
"bytes"
11-
"cmd/go/internal/fsys"
1211
"context"
1312
"encoding/json"
1413
"errors"
@@ -2243,6 +2242,8 @@ func (b *Builder) ccompile(a *Action, p *load.Package, outfile string, flags []s
22432242
// when -trimpath is enabled.
22442243
if b.gccSupportsFlag(compiler, "-fdebug-prefix-map=a=b") {
22452244
if cfg.BuildTrimpath {
2245+
// TODO(#39958): handle overlays
2246+
22462247
// Keep in sync with Action.trimpath.
22472248
// The trimmed paths are a little different, but we need to trim in the
22482249
// same situations.
@@ -2312,8 +2313,7 @@ func (b *Builder) gccld(a *Action, p *load.Package, objdir, outfile string, flag
23122313

23132314
cmdargs := []interface{}{cmd, "-o", outfile, objs, flags}
23142315
dir := p.Dir
2315-
out, err := b.runOut(a, base.Cwd, b.cCompilerEnv(), cmdargs...)
2316-
2316+
out, err := b.runOut(a, dir, b.cCompilerEnv(), cmdargs...)
23172317
if len(out) > 0 {
23182318
// Filter out useless linker warnings caused by bugs outside Go.
23192319
// See also cmd/link/internal/ld's hostlink method.
@@ -2641,8 +2641,7 @@ func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgo
26412641
cgoLDFLAGS = append([]string{"-fsanitize=memory"}, cgoLDFLAGS...)
26422642
}
26432643

2644-
// Allows including _cgo_export.h, as well as the user's .h files,
2645-
// from .[ch] files in the package.
2644+
// Allows including _cgo_export.h from .[ch] files in the package.
26462645
cgoCPPFLAGS = append(cgoCPPFLAGS, "-I", objdir)
26472646

26482647
// cgo
@@ -2655,8 +2654,6 @@ func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgo
26552654
cfiles = append(cfiles, f+".cgo2.c")
26562655
}
26572656

2658-
hfiles := append([]string{}, p.HFiles...)
2659-
26602657
// TODO: make cgo not depend on $GOARCH?
26612658

26622659
cgoflags := []string{}
@@ -2701,38 +2698,7 @@ func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgo
27012698
cgoflags = append(cgoflags, "-exportheader="+objdir+"_cgo_install.h")
27022699
}
27032700

2704-
execdir := p.Dir
2705-
2706-
// If any of the Cgo, C, or H files are overlaid, copy them all to
2707-
// objdir to ensure that they refer to the right header files.
2708-
// TODO(#39958): Ideally, we'd always do this, but this could
2709-
// subtly break some cgo files that include .h files across directory
2710-
// boundaries, even though they shouldn't.
2711-
hasOverlay := false
2712-
cgoFileLists := [][]string{cgofiles, gccfiles, gxxfiles, mfiles, ffiles, hfiles}
2713-
OverlayLoop:
2714-
for _, fs := range cgoFileLists {
2715-
for _, f := range fs {
2716-
if _, ok := fsys.OverlayPath(mkAbs(p.Dir, f)); ok {
2717-
hasOverlay = true
2718-
break OverlayLoop
2719-
}
2720-
}
2721-
}
2722-
if hasOverlay {
2723-
execdir = objdir
2724-
for _, fs := range cgoFileLists {
2725-
for i := range fs {
2726-
opath, _ := fsys.OverlayPath(mkAbs(p.Dir, fs[i]))
2727-
fs[i] = objdir + filepath.Base(fs[i])
2728-
if err := b.copyFile(fs[i], opath, 0666, false); err != nil {
2729-
return nil, nil, err
2730-
}
2731-
}
2732-
}
2733-
}
2734-
2735-
if err := b.run(a, execdir, p.ImportPath, cgoenv, cfg.BuildToolexec, cgoExe, "-objdir", objdir, "-importpath", p.ImportPath, cgoflags, "--", cgoCPPFLAGS, cgoCFLAGS, cgofiles); err != nil {
2701+
if err := b.run(a, p.Dir, p.ImportPath, cgoenv, cfg.BuildToolexec, cgoExe, "-objdir", objdir, "-importpath", p.ImportPath, cgoflags, "--", cgoCPPFLAGS, cgoCFLAGS, cgofiles); err != nil {
27362702
return nil, nil, err
27372703
}
27382704
outGo = append(outGo, gofiles...)
@@ -2826,7 +2792,7 @@ func (b *Builder) dynimport(a *Action, p *load.Package, objdir, importGo, cgoExe
28262792
return err
28272793
}
28282794

2829-
linkobj := str.StringList(ofile, outObj, mkAbsFiles(p.Dir, p.SysoFiles))
2795+
linkobj := str.StringList(ofile, outObj, p.SysoFiles)
28302796
dynobj := objdir + "_cgo_.o"
28312797

28322798
// we need to use -pie for Linux/ARM to get accurate imported sym
@@ -2851,7 +2817,7 @@ func (b *Builder) dynimport(a *Action, p *load.Package, objdir, importGo, cgoExe
28512817
if p.Standard && p.ImportPath == "runtime/cgo" {
28522818
cgoflags = []string{"-dynlinker"} // record path to dynamic linker
28532819
}
2854-
return b.run(a, base.Cwd, p.ImportPath, b.cCompilerEnv(), cfg.BuildToolexec, cgoExe, "-dynpackage", p.Name, "-dynimport", dynobj, "-dynout", importGo, cgoflags)
2820+
return b.run(a, p.Dir, p.ImportPath, b.cCompilerEnv(), cfg.BuildToolexec, cgoExe, "-dynpackage", p.Name, "-dynimport", dynobj, "-dynout", importGo, cgoflags)
28552821
}
28562822

28572823
// Run SWIG on all SWIG input files.

src/cmd/go/internal/work/gc.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ func (a *Action) trimpath() string {
262262
if len(objdir) > 1 && objdir[len(objdir)-1] == filepath.Separator {
263263
objdir = objdir[:len(objdir)-1]
264264
}
265-
rewrite := ""
265+
rewrite := objdir + "=>"
266266

267267
rewriteDir := a.Package.Dir
268268
if cfg.BuildTrimpath {
@@ -271,7 +271,7 @@ func (a *Action) trimpath() string {
271271
} else {
272272
rewriteDir = a.Package.ImportPath
273273
}
274-
rewrite += a.Package.Dir + "=>" + rewriteDir + ";"
274+
rewrite += ";" + a.Package.Dir + "=>" + rewriteDir
275275
}
276276

277277
// Add rewrites for overlays. The 'from' and 'to' paths in overlays don't need to have
@@ -280,14 +280,11 @@ func (a *Action) trimpath() string {
280280
if fsys.OverlayFile != "" {
281281
for _, filename := range a.Package.AllFiles() {
282282
overlayPath, ok := fsys.OverlayPath(filepath.Join(a.Package.Dir, filename))
283-
rewrite += filepath.Join(objdir, filename) + "=>" + filepath.Join(rewriteDir, filename) + ";"
284283
if !ok {
285284
continue
286285
}
287-
rewrite += overlayPath + "=>" + filepath.Join(rewriteDir, filename) + ";"
286+
rewrite += ";" + overlayPath + "=>" + filepath.Join(rewriteDir, filename)
288287
}
289-
} else {
290-
rewrite += objdir + "=>"
291288
}
292289

293290
return rewrite

src/cmd/go/testdata/script/build_overlay.txt

Lines changed: 2 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
[short] skip
22

33
# Test building in overlays.
4-
# TODO(#39958): add a test case where the destination file in the replace map
4+
# TODO(matloob): add a test case where the destination file in the replace map
55
# isn't a go file. Either completely exclude that case in fs.IsDirWithGoFiles
66
# if the compiler doesn't allow it, or test that it works all the way.
7-
# TODO(#39958): add a test that both gc and gccgo assembly files can include .h
8-
# files.
97

108
# The main package (m) is contained in an overlay. It imports m/dir2 which has one
119
# file in an overlay and one file outside the overlay, which in turn imports m/dir,
@@ -31,18 +29,6 @@ exec ./print_trimpath_two_files$GOEXE
3129
stdout $WORK[/\\]gopath[/\\]src[/\\]m[/\\]printpath[/\\]main.go
3230
stdout $WORK[/\\]gopath[/\\]src[/\\]m[/\\]printpath[/\\]other.go
3331

34-
go build -overlay overlay.json -o main_cgo_replace$GOEXE ./cgo_hello_replace
35-
exec ./main_cgo_replace$GOEXE
36-
stdout '^hello cgo$'
37-
38-
go build -overlay overlay.json -o main_cgo_quote$GOEXE ./cgo_hello_quote
39-
exec ./main_cgo_quote$GOEXE
40-
stdout '^hello cgo$'
41-
42-
go build -overlay overlay.json -o main_cgo_angle$GOEXE ./cgo_hello_angle
43-
exec ./main_cgo_angle$GOEXE
44-
stdout '^hello cgo$'
45-
4632
# Run same tests but with gccgo.
4733
env GO111MODULE=off
4834
[!exec:gccgo] stop
@@ -60,19 +46,6 @@ go build -compiler=gccgo -overlay overlay.json -o print_trimpath_gccgo$GOEXE -tr
6046
exec ./print_trimpath_gccgo$GOEXE
6147
stdout ^\.[/\\]printpath[/\\]main.go
6248

63-
64-
go build -compiler=gccgo -overlay overlay.json -o main_cgo_replace_gccgo$GOEXE ./cgo_hello_replace
65-
exec ./main_cgo_replace_gccgo$GOEXE
66-
stdout '^hello cgo$'
67-
68-
go build -compiler=gccgo -overlay overlay.json -o main_cgo_quote_gccgo$GOEXE ./cgo_hello_quote
69-
exec ./main_cgo_quote_gccgo$GOEXE
70-
stdout '^hello cgo$'
71-
72-
go build -compiler=gccgo -overlay overlay.json -o main_cgo_angle_gccgo$GOEXE ./cgo_hello_angle
73-
exec ./main_cgo_angle_gccgo$GOEXE
74-
stdout '^hello cgo$'
75-
7649
-- m/go.mod --
7750
// TODO(matloob): how do overlays work with go.mod (especially if mod=readonly)
7851
module m
@@ -98,32 +71,9 @@ the actual code is in the overlay
9871
"dir/g.go": "overlay/dir_g.go",
9972
"dir2/i.go": "overlay/dir2_i.go",
10073
"printpath/main.go": "overlay/printpath.go",
101-
"printpath/other.go": "overlay2/printpath2.go",
102-
"cgo_hello_replace/cgo_header.h": "overlay/cgo_head.h",
103-
"cgo_hello_quote/cgo_hello.go": "overlay/cgo_hello_quote.go",
104-
"cgo_hello_quote/cgo_header.h": "overlay/cgo_head.h",
105-
"cgo_hello_angle/cgo_hello.go": "overlay/cgo_hello_angle.go",
106-
"cgo_hello_angle/cgo_header.h": "overlay/cgo_head.h"
74+
"printpath/other.go": "overlay2/printpath2.go"
10775
}
10876
}
109-
-- m/cgo_hello_replace/cgo_hello_replace.go --
110-
package main
111-
112-
// #include "cgo_header.h"
113-
import "C"
114-
115-
func main() {
116-
C.say_hello()
117-
}
118-
-- m/cgo_hello_replace/cgo_header.h --
119-
// Test that this header is replaced with one that has the proper declaration.
120-
void say_goodbye();
121-
122-
-- m/cgo_hello_replace/goodbye.c --
123-
#include <stdio.h>
124-
125-
void say_hello() { puts("hello cgo\n"); fflush(stdout); }
126-
12777
-- m/overlay/f.go --
12878
package main
12979

@@ -178,32 +128,3 @@ import "m/dir"
178128
func printMessage() {
179129
dir.PrintMessage()
180130
}
181-
-- m/overlay/cgo_hello_quote.go --
182-
package main
183-
184-
// #include "cgo_header.h"
185-
import "C"
186-
187-
func main() {
188-
C.say_hello()
189-
}
190-
-- m/overlay/cgo_hello_angle.go --
191-
package main
192-
193-
// #include <cgo_header.h>
194-
import "C"
195-
196-
func main() {
197-
C.say_hello()
198-
}
199-
-- m/overlay/cgo_head.h --
200-
void say_hello();
201-
-- m/cgo_hello_quote/hello.c --
202-
#include <stdio.h>
203-
204-
void say_hello() { puts("hello cgo\n"); fflush(stdout); }
205-
-- m/cgo_hello_angle/hello.c --
206-
#include <stdio.h>
207-
208-
void say_hello() { puts("hello cgo\n"); fflush(stdout); }
209-

src/cmd/go/testdata/script/build_trimpath_cgo.txt

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,38 +20,10 @@ go build -trimpath -o hello.exe .
2020
go run ./list-dwarf hello.exe
2121
! stdout gopath/src
2222

23-
24-
# Do the above, with the cgo (but not .c) sources in an overlay
25-
# Check that the source path appears when -trimpath is not used.
26-
mkdir $WORK/overlay
27-
cp hello.go $WORK/overlay/hello.go
28-
mkdir hello_overlay
29-
cp hello.c hello_overlay/hello.c
30-
go build -overlay overlay.json -o hello_overlay.exe ./hello_overlay
31-
grep -q gopath[/\\]src hello_overlay.exe
32-
! grep -q $WORK[/\\]overlay hello_overlay.exe
33-
go run ./list-dwarf hello_overlay.exe
34-
stdout gopath[/\\]src
35-
! stdout $WORK[/\\]overlay
36-
37-
# Check that the source path does not appear when -trimpath is used.
38-
go build -overlay overlay.json -trimpath -o hello_overlay.exe ./hello_overlay
39-
! grep -q gopath[/\\]src hello_overlay.exe
40-
! grep -q $WORK[/\\]overlay hello_overlay.exe
41-
go run ./list-dwarf hello_overlay.exe
42-
! stdout gopath/src
43-
! stdout $WORK[/\\]overlay
44-
4523
-- go.mod --
4624
module m
4725

4826
go 1.14
49-
-- overlay.json --
50-
{
51-
"Replace": {
52-
"hello_overlay/hello.go": "../../overlay/hello.go"
53-
}
54-
}
5527
-- hello.c --
5628
#include <stdio.h>
5729

0 commit comments

Comments
 (0)