-
Notifications
You must be signed in to change notification settings - Fork 9
/
gcc.go
238 lines (216 loc) · 5.15 KB
/
gcc.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// 27 september 2014
package main
import (
"flag"
"runtime"
)
type GCCBase struct {
CC string
CXX string
LD string
RC string
ArchFlag []string
}
func (g *GCCBase) buildRegularFile(cc string, std string, cflags []string, filename string) (stages []Stage, object string) {
object = objectName(filename, ".o")
line := []string{
cc,
filename,
"-c",
std,
"-Wall",
"-Wextra",
// for the case where we are implementing an interface and are not using some parameter
"-Wno-unused-parameter",
}
if g.ArchFlag != nil {
line = append(line, g.ArchFlag...)
}
line = append(line, cflags...)
if *debug {
line = append(line, "-g")
}
line = append(line, "-o", object)
s := &Step{
Name: "Compiled " + filename,
Line: line,
}
stages = []Stage{
Stage{s},
}
return stages, object
}
func (g *GCCBase) BuildCFile(filename string, cflags []string) (stages []Stage, object string) {
return g.buildRegularFile(
g.CC,
"--std=c99", // I refuse to support C11.
cflags,
filename)
}
func (g *GCCBase) BuildCXXFile(filename string, cflags []string) (stages []Stage, object string) {
g.LD = g.CXX
return g.buildRegularFile(
g.CXX,
"--std=c++11",
cflags,
filename)
}
// apart from needing -lobjc at link time, Objective-C/C++ are identical to C/C++; the --std flags are the same (thanks Beelsebob in irc.freenode.net/#macdev)
// TODO provide -lobjc?
func (g *GCCBase) BuildMFile(filename string, cflags []string) (stages []Stage, object string) {
return g.BuildCFile(filename, cflags)
}
func (g *GCCBase) BuildMMFile(filename string, cflags []string) (stages []Stage, object string) {
return g.BuildCXXFile(filename, cflags)
}
func (g *GCCBase) BuildRCFile(filename string, cflags []string) (stages []Stage, object string) {
if g.RC == "" {
fail("LLVM/clang does not come with a Windows resource compiler (if this message appears in other situations in error, contact andlabs)")
}
object = objectName(filename, ".o")
line := append([]string{
g.RC,
filename,
object,
}, cflags...)
s := &Step{
Name: "Compiled " + filename,
Line: line,
}
stages = []Stage{
Stage{s},
}
return stages, object
}
func (g *GCCBase) Link(objects []string, ldflags []string, libs []string) *Step {
if g.LD == "" {
g.LD = g.CC
}
target := targetName()
for i := 0; i < len(libs); i++ {
libs[i] = "-l" + libs[i]
}
line := []string{
g.LD,
}
if g.ArchFlag != nil {
line = append(line, g.ArchFlag...)
}
line = append(line, objects...)
line = append(line, ldflags...)
line = append(line, libs...)
if *debug {
line = append(line, "-g")
}
line = append(line, "-o", target)
return &Step{
Name: "Linked " + target,
Line: line,
}
}
// TODO:
// - MinGW static libgcc/libsjlj/libwinpthread/etc.
// - -static-libgcc
var triplet = flag.String("triplet", "", "gcc/clang target triplet to use; see README")
var garchs = map[string]string{
"386": "i686",
"amd64": "x86_64",
}
// 386 and amd64 are commonly configured using multilib rather than targets
// everything else will be a nil slice
var garchflags = map[string][]string{
"386": []string{"-m32"},
"amd64": []string{"-m64"},
}
type GCC struct {
*GCCBase
}
func isMultilib() bool {
if *targetOS == runtime.GOOS && runtime.GOOS != "windows" {
// MinGW for Windows is not multilib
// assume everything else is
if runtime.GOARCH == "386" || runtime.GOARCH == "amd64" {
if *targetArch == "386" || *targetArch == "amd64" {
// multilib only
return true
}
}
}
return false
}
func (g *GCC) Prepare() {
garch := garchs[*targetArch]
prefix := ""
if *triplet != "" {
prefix = *triplet + "-"
goto out
}
// set this before any of the following in case target == host
g.ArchFlag = garchflags[*targetArch]
if *targetOS == runtime.GOOS && *targetArch == runtime.GOARCH {
return
}
if isMultilib() {
return
}
switch *targetOS {
case "windows":
prefix = garch + "-w64-mingw32-"
case "linux":
// TODO abi override
prefix = garch + "-linux-gnu-"
default:
fail("Sorry, cross-compiling for gcc on %s requires specifying an explicit target triple with -target", *targetOS)
}
out:
g.CC = prefix + g.CC
g.CXX = prefix + g.CXX
g.RC = prefix + g.RC
}
type Clang struct {
*GCCBase
}
// see the LLVM source; file include/llvm/ADT/Triple.h (thanks jroelofs in irc.oftc.net/#llvm)
var clangOS = map[string]string{
"windows": "win32",
"linux": "linux",
"darwin": "darwin",
"freebsd": "freebsd",
"openbsd": "openbsd",
"netbsd": "netbsd",
"dragonfly": "dragonfly",
"solaris": "solaris",
}
func (g *Clang) Prepare() {
if *triplet != "" {
g.ArchFlag = []string{"-target", *triplet}
return
}
// set this before any of the following in case target == host
g.ArchFlag = garchflags[*targetArch]
if *targetOS == runtime.GOOS && *targetArch == runtime.GOARCH {
return
}
if isMultilib() {
return
}
// clang makes the job easier
// TODO abi override
g.ArchFlag = append(g.ArchFlag, "-target", garchs[*targetArch] + "-" + clangOS[*targetOS])
}
func init() {
toolchains["gcc"] = &GCC{
GCCBase: &GCCBase{
CC: "gcc",
CXX: "g++",
RC: "windres",
},
}
toolchains["clang"] = &Clang{
GCCBase: &GCCBase{
CC: "clang",
CXX: "clang++",
// no RC in clang
},
}
}