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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
|
// Copyright 2015 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cc
// This file generates the final rules for compiling all C/C++. All properties related to
// compiling should have been translated into builderFlags or another argument to the Transform*
// functions.
import (
"android/soong/android"
"fmt"
"runtime"
"strconv"
"path/filepath"
"strings"
"github.com/google/blueprint"
)
const (
objectExtension = ".o"
staticLibraryExtension = ".a"
)
var (
pctx = android.NewPackageContext("android/soong/cc")
cc = pctx.StaticRule("cc",
blueprint.RuleParams{
Depfile: "${out}.d",
Deps: blueprint.DepsGCC,
Command: "$relPwd $ccCmd -c $cFlags -MD -MF ${out}.d -o $out $in",
CommandDeps: []string{"$ccCmd"},
Description: "cc $out",
},
"ccCmd", "cFlags")
ld = pctx.StaticRule("ld",
blueprint.RuleParams{
Command: "$ldCmd ${ldDirFlags} ${crtBegin} @${out}.rsp " +
"${libFlags} ${crtEnd} -o ${out} ${ldFlags}",
CommandDeps: []string{"$ldCmd"},
Description: "ld $out",
Rspfile: "${out}.rsp",
RspfileContent: "${in}",
},
"ldCmd", "ldDirFlags", "crtBegin", "libFlags", "crtEnd", "ldFlags")
partialLd = pctx.StaticRule("partialLd",
blueprint.RuleParams{
Command: "$ldCmd -nostdlib -Wl,-r ${in} -o ${out} ${ldFlags}",
CommandDeps: []string{"$ldCmd"},
Description: "partialLd $out",
},
"ldCmd", "ldFlags")
ar = pctx.StaticRule("ar",
blueprint.RuleParams{
Command: "rm -f ${out} && $arCmd $arFlags $out @${out}.rsp",
CommandDeps: []string{"$arCmd"},
Description: "ar $out",
Rspfile: "${out}.rsp",
RspfileContent: "${in}",
},
"arCmd", "arFlags")
darwinAr = pctx.StaticRule("darwinAr",
blueprint.RuleParams{
Command: "rm -f ${out} && ${macArPath} $arFlags $out $in",
CommandDeps: []string{"${macArPath}"},
Description: "ar $out",
},
"arFlags")
darwinAppendAr = pctx.StaticRule("darwinAppendAr",
blueprint.RuleParams{
Command: "cp -f ${inAr} ${out}.tmp && ${macArPath} $arFlags ${out}.tmp $in && mv ${out}.tmp ${out}",
CommandDeps: []string{"${macArPath}", "${inAr}"},
Description: "ar $out",
},
"arFlags", "inAr")
darwinStrip = pctx.StaticRule("darwinStrip",
blueprint.RuleParams{
Command: "${macStripPath} -u -r -o $out $in",
CommandDeps: []string{"${macStripPath}"},
Description: "strip $out",
})
prefixSymbols = pctx.StaticRule("prefixSymbols",
blueprint.RuleParams{
Command: "$objcopyCmd --prefix-symbols=${prefix} ${in} ${out}",
CommandDeps: []string{"$objcopyCmd"},
Description: "prefixSymbols $out",
},
"objcopyCmd", "prefix")
stripPath = pctx.SourcePathVariable("stripPath", "build/soong/scripts/strip.sh")
strip = pctx.StaticRule("strip",
blueprint.RuleParams{
Depfile: "${out}.d",
Deps: blueprint.DepsGCC,
Command: "CROSS_COMPILE=$crossCompile $stripPath ${args} -i ${in} -o ${out} -d ${out}.d",
CommandDeps: []string{"$stripPath"},
Description: "strip $out",
},
"args", "crossCompile")
emptyFile = pctx.StaticRule("emptyFile",
blueprint.RuleParams{
Command: "rm -f $out && touch $out",
Description: "empty file $out",
})
copyGccLibPath = pctx.SourcePathVariable("copyGccLibPath", "build/soong/scripts/copygcclib.sh")
copyGccLib = pctx.StaticRule("copyGccLib",
blueprint.RuleParams{
Depfile: "${out}.d",
Deps: blueprint.DepsGCC,
Command: "$copyGccLibPath $out $ccCmd $cFlags -print-file-name=${libName}",
CommandDeps: []string{"$copyGccLibPath", "$ccCmd"},
Description: "copy gcc $out",
},
"ccCmd", "cFlags", "libName")
)
func init() {
// We run gcc/clang with PWD=/proc/self/cwd to remove $TOP from the
// debug output. That way two builds in two different directories will
// create the same output.
if runtime.GOOS != "darwin" {
pctx.StaticVariable("relPwd", "PWD=/proc/self/cwd")
} else {
// Darwin doesn't have /proc
pctx.StaticVariable("relPwd", "")
}
}
type builderFlags struct {
globalFlags string
asFlags string
cFlags string
conlyFlags string
cppFlags string
ldFlags string
libFlags string
yaccFlags string
nocrt bool
toolchain Toolchain
clang bool
stripKeepSymbols bool
stripKeepMiniDebugInfo bool
stripAddGnuDebuglink bool
}
// Generate rules for compiling multiple .c, .cpp, or .S files to individual .o files
func TransformSourceToObj(ctx android.ModuleContext, subdir string, srcFiles android.Paths,
flags builderFlags, deps android.Paths) (objFiles android.Paths) {
objFiles = make(android.Paths, len(srcFiles))
cflags := flags.globalFlags + " " + flags.cFlags + " " + flags.conlyFlags
cppflags := flags.globalFlags + " " + flags.cFlags + " " + flags.cppFlags
asflags := flags.globalFlags + " " + flags.asFlags
if flags.clang {
cflags += " ${noOverrideClangGlobalCflags}"
cppflags += " ${noOverrideClangGlobalCflags}"
} else {
cflags += " ${noOverrideGlobalCflags}"
cppflags += " ${noOverrideGlobalCflags}"
}
for i, srcFile := range srcFiles {
objFile := android.ObjPathWithExt(ctx, srcFile, subdir, "o")
objFiles[i] = objFile
var moduleCflags string
var ccCmd string
switch srcFile.Ext() {
case ".S", ".s":
ccCmd = "gcc"
moduleCflags = asflags
case ".c":
ccCmd = "gcc"
moduleCflags = cflags
case ".cpp", ".cc":
ccCmd = "g++"
moduleCflags = cppflags
default:
ctx.ModuleErrorf("File %s has unknown extension", srcFile)
continue
}
if flags.clang {
switch ccCmd {
case "gcc":
ccCmd = "clang"
case "g++":
ccCmd = "clang++"
default:
panic("unrecoginzied ccCmd")
}
ccCmd = "${clangBin}/" + ccCmd
} else {
ccCmd = gccCmd(flags.toolchain, ccCmd)
}
ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Rule: cc,
Output: objFile,
Input: srcFile,
OrderOnly: deps,
Args: map[string]string{
"cFlags": moduleCflags,
"ccCmd": ccCmd,
},
})
}
return objFiles
}
// Generate a rule for compiling multiple .o files to a static library (.a)
func TransformObjToStaticLib(ctx android.ModuleContext, objFiles android.Paths,
flags builderFlags, outputFile android.ModuleOutPath) {
arCmd := gccCmd(flags.toolchain, "ar")
arFlags := "crsPD"
ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Rule: ar,
Output: outputFile,
Inputs: objFiles,
Args: map[string]string{
"arFlags": arFlags,
"arCmd": arCmd,
},
})
}
// Generate a rule for compiling multiple .o files to a static library (.a) on
// darwin. The darwin ar tool doesn't support @file for list files, and has a
// very small command line length limit, so we have to split the ar into multiple
// steps, each appending to the previous one.
func TransformDarwinObjToStaticLib(ctx android.ModuleContext, objFiles android.Paths,
flags builderFlags, outputPath android.ModuleOutPath) {
arFlags := "cqs"
if len(objFiles) == 0 {
dummy := android.PathForModuleOut(ctx, "dummy"+objectExtension)
dummyAr := android.PathForModuleOut(ctx, "dummy"+staticLibraryExtension)
ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Rule: emptyFile,
Output: dummy,
})
ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Rule: darwinAr,
Output: dummyAr,
Input: dummy,
Args: map[string]string{
"arFlags": arFlags,
},
})
ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Rule: darwinAppendAr,
Output: outputPath,
Input: dummy,
Args: map[string]string{
"arFlags": "d",
"inAr": dummyAr.String(),
},
})
return
}
// ARG_MAX on darwin is 262144, use half that to be safe
objFilesLists, err := splitListForSize(objFiles.Strings(), 131072)
if err != nil {
ctx.ModuleErrorf("%s", err.Error())
}
outputFile := outputPath.String()
var in, out string
for i, l := range objFilesLists {
in = out
out = outputFile
if i != len(objFilesLists)-1 {
out += "." + strconv.Itoa(i)
}
if in == "" {
ctx.Build(pctx, blueprint.BuildParams{
Rule: darwinAr,
Outputs: []string{out},
Inputs: l,
Args: map[string]string{
"arFlags": arFlags,
},
})
} else {
ctx.Build(pctx, blueprint.BuildParams{
Rule: darwinAppendAr,
Outputs: []string{out},
Inputs: l,
Args: map[string]string{
"arFlags": arFlags,
"inAr": in,
},
})
}
}
}
// Generate a rule for compiling multiple .o files, plus static libraries, whole static libraries,
// and shared libraires, to a shared library (.so) or dynamic executable
func TransformObjToDynamicBinary(ctx android.ModuleContext,
objFiles, sharedLibs, staticLibs, lateStaticLibs, wholeStaticLibs, deps android.Paths,
crtBegin, crtEnd android.OptionalPath, groupLate bool, flags builderFlags, outputFile android.WritablePath) {
var ldCmd string
if flags.clang {
ldCmd = "${clangBin}/clang++"
} else {
ldCmd = gccCmd(flags.toolchain, "g++")
}
var ldDirs []string
var libFlagsList []string
if len(flags.libFlags) > 0 {
libFlagsList = append(libFlagsList, flags.libFlags)
}
if len(wholeStaticLibs) > 0 {
if ctx.Host() && ctx.Darwin() {
libFlagsList = append(libFlagsList, android.JoinWithPrefix(wholeStaticLibs.Strings(), "-force_load "))
} else {
libFlagsList = append(libFlagsList, "-Wl,--whole-archive ")
libFlagsList = append(libFlagsList, wholeStaticLibs.Strings()...)
libFlagsList = append(libFlagsList, "-Wl,--no-whole-archive ")
}
}
libFlagsList = append(libFlagsList, staticLibs.Strings()...)
if groupLate && len(lateStaticLibs) > 0 {
libFlagsList = append(libFlagsList, "-Wl,--start-group")
}
libFlagsList = append(libFlagsList, lateStaticLibs.Strings()...)
if groupLate && len(lateStaticLibs) > 0 {
libFlagsList = append(libFlagsList, "-Wl,--end-group")
}
for _, lib := range sharedLibs {
dir, file := filepath.Split(lib.String())
if !strings.HasPrefix(file, "lib") {
panic("shared library " + lib.String() + " does not start with lib")
}
if !strings.HasSuffix(file, flags.toolchain.ShlibSuffix()) {
panic("shared library " + lib.String() + " does not end with " + flags.toolchain.ShlibSuffix())
}
libFlagsList = append(libFlagsList,
"-l"+strings.TrimSuffix(strings.TrimPrefix(file, "lib"), flags.toolchain.ShlibSuffix()))
ldDirs = append(ldDirs, dir)
}
deps = append(deps, sharedLibs...)
deps = append(deps, staticLibs...)
deps = append(deps, lateStaticLibs...)
deps = append(deps, wholeStaticLibs...)
if crtBegin.Valid() {
deps = append(deps, crtBegin.Path(), crtEnd.Path())
}
ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Rule: ld,
Output: outputFile,
Inputs: objFiles,
Implicits: deps,
Args: map[string]string{
"ldCmd": ldCmd,
"ldDirFlags": ldDirsToFlags(ldDirs),
"crtBegin": crtBegin.String(),
"libFlags": strings.Join(libFlagsList, " "),
"ldFlags": flags.ldFlags,
"crtEnd": crtEnd.String(),
},
})
}
// Generate a rule for compiling multiple .o files to a .o using ld partial linking
func TransformObjsToObj(ctx android.ModuleContext, objFiles android.Paths,
flags builderFlags, outputFile android.WritablePath) {
var ldCmd string
if flags.clang {
ldCmd = "${clangBin}clang++"
} else {
ldCmd = gccCmd(flags.toolchain, "g++")
}
ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Rule: partialLd,
Output: outputFile,
Inputs: objFiles,
Args: map[string]string{
"ldCmd": ldCmd,
"ldFlags": flags.ldFlags,
},
})
}
// Generate a rule for runing objcopy --prefix-symbols on a binary
func TransformBinaryPrefixSymbols(ctx android.ModuleContext, prefix string, inputFile android.Path,
flags builderFlags, outputFile android.WritablePath) {
objcopyCmd := gccCmd(flags.toolchain, "objcopy")
ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Rule: prefixSymbols,
Output: outputFile,
Input: inputFile,
Args: map[string]string{
"objcopyCmd": objcopyCmd,
"prefix": prefix,
},
})
}
func TransformStrip(ctx android.ModuleContext, inputFile android.Path,
outputFile android.WritablePath, flags builderFlags) {
crossCompile := gccCmd(flags.toolchain, "")
args := ""
if flags.stripAddGnuDebuglink {
args += " --add-gnu-debuglink"
}
if flags.stripKeepMiniDebugInfo {
args += " --keep-mini-debug-info"
}
if flags.stripKeepSymbols {
args += " --keep-symbols"
}
ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Rule: strip,
Output: outputFile,
Input: inputFile,
Args: map[string]string{
"crossCompile": crossCompile,
"args": args,
},
})
}
func TransformDarwinStrip(ctx android.ModuleContext, inputFile android.Path,
outputFile android.WritablePath) {
ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Rule: darwinStrip,
Output: outputFile,
Input: inputFile,
})
}
func CopyGccLib(ctx android.ModuleContext, libName string,
flags builderFlags, outputFile android.WritablePath) {
ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Rule: copyGccLib,
Output: outputFile,
Args: map[string]string{
"ccCmd": gccCmd(flags.toolchain, "gcc"),
"cFlags": flags.globalFlags,
"libName": libName,
},
})
}
func gccCmd(toolchain Toolchain, cmd string) string {
return filepath.Join(toolchain.GccRoot(), "bin", toolchain.GccTriple()+"-"+cmd)
}
func splitListForSize(list []string, limit int) (lists [][]string, err error) {
var i int
start := 0
bytes := 0
for i = range list {
l := len(list[i])
if l > limit {
return nil, fmt.Errorf("list element greater than size limit (%d)", limit)
}
if bytes+l > limit {
lists = append(lists, list[start:i])
start = i
bytes = 0
}
bytes += l + 1 // count a space between each list element
}
lists = append(lists, list[start:])
totalLen := 0
for _, l := range lists {
totalLen += len(l)
}
if totalLen != len(list) {
panic(fmt.Errorf("Failed breaking up list, %d != %d", len(list), totalLen))
}
return lists, nil
}
|