This repository has been archived by the owner on Feb 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
compile.go
281 lines (246 loc) · 7.98 KB
/
compile.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Copyright 2018 Schibsted
package buildbuild
import (
"bytes"
"errors"
"fmt"
"os"
"os/exec"
"path"
"regexp"
"strings"
)
type CompileGeneralSrcFunc func(g *GeneralDesc, srcdir, src, srcbase string)
type CompileLinkerSrcFunc func(l *LinkDesc, srcdir, src, srcbase string)
type CompileSpecialSrcFunc func(desc Descriptor, tname, rule string, srcs []string, destdir, srcdir string, extraargs []string, options map[string]bool) Descriptor
var (
UnknownSourceExtension = errors.New("Invalid source extension (not all extensions can be used in all Descriptors)")
EnumWithoutGperf = errors.New("Extension .enum without .gperf.enum")
SourceNeedsLinker = errors.New("Incompatible source type for this descriptor")
)
func (ops *GlobalOps) DefaultCompiler() {
ops.CC = "cc"
ops.CXX = "c++"
ops.CompilerFlavor = "gcc"
}
// This is like AddTarget, but allow plugins to intercept in case they do some special processing.
// It also splits tname on comma and sets a multi target if more than one element.
func CompileSpecial(desc Descriptor, tname, rule string, srcs []string, destdir, srcdir string, extraargs []string, options map[string]bool) Descriptor {
if len(srcs) == 1 && srcs[0] == "" {
if options == nil {
options = make(map[string]bool)
}
options["emptysrcs"] = true
srcs = nil
}
if f := PluginSpecialSrcs[rule]; f != nil {
return f(desc, tname, rule, srcs, destdir, srcdir, extraargs, options)
}
tnames := strings.Split(tname, ",")
target := desc.AddTarget(tnames[0], rule, srcs, destdir, srcdir, extraargs, options)
if len(tnames) > 1 {
desc.AddMultiTarget(tnames, target)
}
return desc
}
// Sources in the srcs argument are compiled based on their file extension.
// What extensions are allowed depend on the descriptor. The only extension
// allowed anywhere is .in, which generates a file with the in.pl script.
//
// Descriptors that link a binary allow several more extensions:
// .c, .cc, .cxx, .ll, .yy, .gperf, .gperf.enum, .xs and .go
//
// Plugins might add additional extensions.
//
// The produced objects are put in the object directory of the descriptor,
// where they can be referenced by other arguments or used in the descriptor
// target.
//
// If a source generates a C file or similar, it is typically compiled all
// the way to an object file and then added to the linked objects.
// Other sources, e.g. in files, have to be used manually by other arguments
// or they will be discarded.
func CompileSrc(desc Descriptor, srcdir, src string) {
ext := path.Ext(src)
if len(ext) < 1 {
ext = "."
}
srcbase := strings.TrimSuffix(src, ext)
desc.CompileSrc(srcdir, src, srcbase, ext)
}
func (g *GeneralDesc) CompileSrc(srcdir, src, srcbase, ext string) {
if f := PluginGeneralExtensions[ext[1:]]; f != nil {
f(g, srcdir, src, srcbase)
return
}
switch ext[1:] {
case "in":
g.CompileIn(srcdir, src, srcbase)
default:
panic(&ParseError{UnknownSourceExtension, src, g.Builddesc})
}
}
func (l *LinkDesc) CompileSrc(srcdir, src, srcbase, ext string) {
if f := PluginLinkerExtensions[ext[1:]]; f != nil {
f(l, srcdir, src, srcbase)
return
}
switch ext[1:] {
case "c":
l.CompileC(srcdir, src, srcbase)
case "cc", "cxx":
l.CompileCXX(srcdir, src, srcbase)
case "yy":
l.CompileYY(srcdir, src, srcbase)
case "ll":
l.CompileLL(srcdir, src, srcbase)
case "gperf":
l.CompileGperf(srcdir, src, srcbase)
case "enum":
l.CompileEnum(srcdir, src, srcbase)
case "xs":
l.CompileXs(srcdir, src, srcbase)
case "go":
l.CompileGo(srcdir, src, srcbase)
default:
l.GeneralDesc.CompileSrc(srcdir, src, srcbase, ext)
return
}
}
// Redirected by test
var findCompilerRun = (*exec.Cmd).Run
func (ops *GlobalOps) FindCompilerCC() error {
if ops.didFindCompiler {
return nil
}
ops.didFindCompiler = true
candidates := ops.Config.Compiler
envcc := os.Getenv("CC")
if envcc != "" {
candidates = append(candidates, envcc)
}
candidates = append(candidates, "gcc", "clang", "cc")
ops.CompilerFlavor = ""
minGcc := comparableVersion("4.8")
minClang := comparableVersion("3.4")
versionRE := regexp.MustCompile(`(\S+) version ([0-9]+\.[0-9]+)`)
for _, cc := range candidates {
minv := ""
if col := strings.IndexRune(cc, ':'); col >= 0 {
minv = cc[col+1:]
cc = cc[:col]
}
ccv := append(strings.Split(cc, " "), "-v")
cmd := exec.Command(ccv[0], ccv[1:]...)
var output bytes.Buffer
cmd.Stdout = &output
cmd.Stderr = &output
err := findCompilerRun(cmd)
if err != nil {
continue
}
match := versionRE.FindStringSubmatch(output.String())
if match == nil {
continue
}
c := match[1]
v := comparableVersion(match[2])
if minv != "" && v < comparableVersion(minv) {
continue
}
if c == "gcc" && v < minGcc {
continue
}
if c == "clang" && v < minClang {
continue
}
ops.CompilerFlavor = c
ops.CC = cc
if idx := strings.Index(cc, "gcc"); idx >= 0 {
ops.CXX = cc[:idx] + "g++" + cc[idx+3:]
} else if idx := strings.Index(cc, "cc"); idx >= 0 {
ops.CXX = cc[:idx] + "c++" + cc[idx+2:]
} else if idx := strings.Index(cc, "-"); idx >= 0 { // clang-4.0 -> clang++-4.0
ops.CXX = cc[:idx] + "++" + cc[idx:]
} else {
ops.CXX = cc + "++"
}
if ops.Options.Debug {
fmt.Printf("Compilers detected: %s / %s (%s %s)\n", ops.CC, ops.CXX, c, v)
}
break
}
if ops.CompilerFlavor == "" {
return errors.New("Couldn't find a compatible compiler")
}
return nil
}
func comparableVersion(v string) string {
var vlong strings.Builder
for _, vpart := range strings.Split(v, ".") {
fmt.Fprintf(&vlong, "%4s", vpart)
}
return vlong.String()
}
func (l *LinkDesc) CompileC(srcdir, src, srcbase string) {
opts := map[string]bool{"incdeps": true}
l.AddTarget(srcbase+".o", "cc", []string{src}, "obj", srcdir, nil, opts)
if l.Picrules {
l.AddTarget(srcbase+".pic_o", "cc", []string{src}, "obj", srcdir, []string{"picflag=-fPIC"}, opts)
}
if l.NoAnalyse {
l.DontAnalyse[srcbase] = true
} else {
l.AddTarget(srcbase+".analyse", "cc_analyse", []string{src}, "obj", srcdir, nil, opts)
}
l.Objs = append(l.Objs, srcbase)
}
func (l *LinkDesc) CompileCXX(srcdir, src, srcbase string) {
l.Link = "linkxx"
opts := map[string]bool{"incdeps": true}
l.AddTarget(srcbase+".o", "cxx", []string{src}, "obj", srcdir, nil, opts)
if l.Picrules {
l.AddTarget(srcbase+".pic_o", "cxx", []string{src}, "obj", srcdir, []string{"picflag=-fPIC"}, opts)
}
if l.NoAnalyse {
l.DontAnalyse[srcbase] = true
} else {
l.AddTarget(srcbase+".analyse", "cxx_analyse", []string{src}, "obj", srcdir, nil, opts)
}
l.Objs = append(l.Objs, srcbase)
}
func (l *LinkDesc) CompileYY(srcdir, src, srcbase string) {
l.Link = "linkxx"
l.AddTarget(srcbase+".cc", "yaccxx", []string{src}, "obj", srcdir, nil, nil)
l.AddTarget(srcbase+".hh", "phony", []string{srcbase + ".cc"}, "obj", "", nil, nil)
l.Incdeps = append(l.Incdeps, srcbase+".hh")
l.CompileCXX(srcdir, srcbase+".cc", srcbase)
}
func (l *LinkDesc) CompileLL(srcdir, src, srcbase string) {
l.Link = "linkxx"
l.AddTarget(srcbase+".cc", "flexx", []string{src}, "obj", srcdir, nil, nil)
l.CompileCXX(srcdir, srcbase+".cc", srcbase)
}
func (l *LinkDesc) CompileGperf(srcdir, src, srcbase string) {
l.AddTarget(srcbase+".h", "gperf", []string{src}, "obj", srcdir, nil, nil)
l.Incdeps = append(l.Incdeps, srcbase+".h")
}
func (l *LinkDesc) CompileEnum(srcdir, src, srcbase string) {
l.AddTarget(srcbase, "gperf_enum", []string{src}, "obj", srcdir, nil, nil)
gp := strings.TrimSuffix(srcbase, ".gperf")
if gp == srcbase {
panic(&ParseError{EnumWithoutGperf, src, l.Builddesc})
}
l.CompileGperf("", srcbase, gp)
}
func (g *GeneralDesc) CompileIn(srcdir, src, srcbase string) {
g.AddTarget(srcbase, "in", []string{src}, "obj", srcdir, nil, nil)
}
func (l *LinkDesc) CompileXs(srcdir, src, srcbase string) {
l.AddTarget(srcbase+".c", "xs", []string{src}, "obj", srcdir, nil, nil)
l.CompileC("", srcbase+".c", srcbase)
}
func (l *LinkDesc) CompileGo(srcdir, src, srcbase string) {
l.AddTarget(path.Join("go", src), "copy_go", []string{src}, "obj", srcdir, nil, nil)
l.GoSrc = append(l.GoSrc, path.Join("$objdir/go", src))
}