This repository has been archived by the owner on Mar 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathgc.go
165 lines (148 loc) · 4.5 KB
/
gc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package gb
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/constabulary/gb/internal/version"
"github.com/pkg/errors"
)
// gc toolchain
type gcToolchain struct {
gc, cc, ld, as, pack string
}
func GcToolchain() func(c *Context) error {
return func(c *Context) error {
// TODO(dfc) this should come from the context, not the runtime.
goroot := runtime.GOROOT()
tooldir := filepath.Join(goroot, "pkg", "tool", c.gohostos+"_"+c.gohostarch)
exe := ""
if c.gohostos == "windows" {
exe += ".exe"
}
switch {
case version.Version > 1.5:
c.tc = &gcToolchain{
gc: filepath.Join(tooldir, "compile"+exe),
ld: filepath.Join(tooldir, "link"+exe),
as: filepath.Join(tooldir, "asm"+exe),
pack: filepath.Join(tooldir, "pack"+exe),
}
return nil
default:
return errors.Errorf("unsupported Go version: %v", runtime.Version())
}
}
}
func (t *gcToolchain) Asm(pkg *Package, ofile, sfile string) error {
args := []string{"-o", ofile, "-D", "GOOS_" + pkg.gotargetos, "-D", "GOARCH_" + pkg.gotargetarch}
switch {
case version.Version > 1.5:
odir := filepath.Join(filepath.Dir(ofile))
includedir := filepath.Join(runtime.GOROOT(), "pkg", "include")
args = append(args, "-I", odir, "-I", includedir)
default:
return errors.Errorf("unsupported Go version: %v", runtime.Version())
}
args = append(args, sfile)
if err := mkdir(filepath.Dir(ofile)); err != nil {
return errors.Errorf("gc:asm: %v", err)
}
var buf bytes.Buffer
err := runOut(&buf, pkg.Dir, nil, t.as, args...)
if err != nil {
fmt.Fprintf(os.Stderr, "# %s\n", pkg.ImportPath)
io.Copy(os.Stderr, &buf)
}
return err
}
func (t *gcToolchain) Ld(pkg *Package) error {
// to ensure we don't write a partial binary, link the binary to a temporary file in
// in the target directory, then rename.
dir := pkg.bindir()
if err := mkdir(dir); err != nil {
return err
}
tmp, err := ioutil.TempFile(dir, ".gb-link")
if err != nil {
return err
}
tmp.Close()
args := append(pkg.ldflags, "-o", tmp.Name())
for _, d := range pkg.includePaths() {
args = append(args, "-L", d)
}
args = append(args, "-extld", linkCmd(pkg, "CC", defaultCC))
args = append(args, "-buildmode", pkg.buildmode)
args = append(args, pkg.objfile())
var buf bytes.Buffer
if err = runOut(&buf, ".", nil, t.ld, args...); err != nil {
os.Remove(tmp.Name()) // remove partial file
fmt.Fprintf(os.Stderr, "# %s\n", pkg.ImportPath)
io.Copy(os.Stderr, &buf)
return err
}
return os.Rename(tmp.Name(), pkg.Binfile())
}
func (t *gcToolchain) Cc(pkg *Package, ofile, cfile string) error {
return errors.Errorf("gc %f does not support cc", version.Version)
}
func (t *gcToolchain) Pack(pkg *Package, afiles ...string) error {
args := []string{"r"}
args = append(args, afiles...)
dir := filepath.Dir(afiles[0])
var buf bytes.Buffer
err := runOut(&buf, dir, nil, t.pack, args...)
if err != nil {
fmt.Fprintf(os.Stderr, "# %s\n", pkg.ImportPath)
io.Copy(os.Stderr, &buf)
}
return err
}
func (t *gcToolchain) compiler() string { return t.gc }
func (t *gcToolchain) linker() string { return t.ld }
func (t *gcToolchain) Gc(pkg *Package, files []string) error {
outfile := pkg.objfile()
args := append(pkg.gcflags, "-p", pkg.ImportPath, "-pack")
args = append(args, "-o", outfile)
for _, d := range pkg.includePaths() {
args = append(args, "-I", d)
}
if pkg.Goroot && pkg.ImportPath == "runtime" {
// runtime compiles with a special gc flag to emit
// additional reflect type data.
args = append(args, "-+")
}
if pkg.complete() {
args = append(args, "-complete")
} else {
asmhdr := filepath.Join(filepath.Dir(outfile), pkg.Name, "go_asm.h")
args = append(args, "-asmhdr", asmhdr)
}
// If there are vendored components, create an -importmap to map the import statement
// to the vendored import path. The possibilities for abusing this flag are endless.
if pkg.Goroot {
for _, path := range pkg.Package.Imports {
if i := strings.LastIndex(path, "/vendor/"); i >= 0 {
args = append(args, "-importmap", path[i+len("/vendor/"):]+"="+path)
} else if strings.HasPrefix(path, "vendor/") {
args = append(args, "-importmap", path[len("vendor/"):]+"="+path)
}
}
}
args = append(args, files...)
if err := mkdir(filepath.Join(filepath.Dir(outfile), pkg.Name)); err != nil {
return errors.Wrap(err, "mkdir")
}
var buf bytes.Buffer
err := runOut(&buf, pkg.Dir, nil, t.gc, args...)
if err != nil {
fmt.Fprintf(os.Stderr, "# %s\n", pkg.ImportPath)
io.Copy(os.Stderr, &buf)
}
return err
}