blob: b5f4c5c0b3b0ab61b5f4944bfb06fbbbb003d713 [file] [log] [blame]
Colin Cross3f40fa42015-01-30 17:27:36 -08001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package cc
16
17// This file generates the final rules for compiling all C/C++. All properties related to
18// compiling should have been translated into builderFlags or another argument to the Transform*
19// functions.
20
21import (
Colin Cross0af4b842015-04-30 16:36:18 -070022 "fmt"
Colin Crossb98c8b02016-07-29 13:44:28 -070023 "path/filepath"
Colin Cross0af4b842015-04-30 16:36:18 -070024 "runtime"
25 "strconv"
Colin Cross3f40fa42015-01-30 17:27:36 -080026 "strings"
Colin Crossed4cf0b2015-03-26 14:43:45 -070027
28 "github.com/google/blueprint"
Colin Crossb98c8b02016-07-29 13:44:28 -070029
30 "android/soong/android"
31 "android/soong/cc/config"
Colin Cross3f40fa42015-01-30 17:27:36 -080032)
33
34const (
Dan Albertc3144b12015-04-28 18:17:56 -070035 objectExtension = ".o"
Colin Cross3f40fa42015-01-30 17:27:36 -080036 staticLibraryExtension = ".a"
37)
38
39var (
Jayant Chowdharya3bb1b32017-11-01 11:12:15 -070040 abiCheckAllowFlags = []string{
41 "-allow-extensions",
42 "-allow-unreferenced-changes",
43 "-allow-unreferenced-elf-symbol-changes",
44 }
45)
46
47var (
Colin Cross635c3b02016-05-18 15:37:25 -070048 pctx = android.NewPackageContext("android/soong/cc")
Colin Cross3f40fa42015-01-30 17:27:36 -080049
Colin Cross9d45bb72016-08-29 16:14:13 -070050 cc = pctx.AndroidGomaStaticRule("cc",
Colin Cross3f40fa42015-01-30 17:27:36 -080051 blueprint.RuleParams{
52 Depfile: "${out}.d",
53 Deps: blueprint.DepsGCC,
Alistair Strachan777475c2016-08-26 12:55:49 -070054 Command: "$relPwd ${config.CcWrapper}$ccCmd -c $cFlags -MD -MF ${out}.d -o $out $in",
Dan Willemsenc94a7682015-11-17 15:27:28 -080055 CommandDeps: []string{"$ccCmd"},
Colin Cross3f40fa42015-01-30 17:27:36 -080056 },
Dan Willemsen322a0a62015-11-17 15:19:46 -080057 "ccCmd", "cFlags")
Colin Cross3f40fa42015-01-30 17:27:36 -080058
Colin Cross9d45bb72016-08-29 16:14:13 -070059 ld = pctx.AndroidStaticRule("ld",
Colin Cross3f40fa42015-01-30 17:27:36 -080060 blueprint.RuleParams{
Dan Albertce2b8392016-07-21 13:16:49 -070061 Command: "$ldCmd ${crtBegin} @${out}.rsp " +
Colin Cross28344522015-04-22 13:07:53 -070062 "${libFlags} ${crtEnd} -o ${out} ${ldFlags}",
Dan Willemsenc94a7682015-11-17 15:27:28 -080063 CommandDeps: []string{"$ldCmd"},
Colin Cross7d21c442015-03-30 17:47:53 -070064 Rspfile: "${out}.rsp",
65 RspfileContent: "${in}",
Colin Cross3f40fa42015-01-30 17:27:36 -080066 },
Dan Albertce2b8392016-07-21 13:16:49 -070067 "ldCmd", "crtBegin", "libFlags", "crtEnd", "ldFlags")
Colin Cross3f40fa42015-01-30 17:27:36 -080068
Colin Cross9d45bb72016-08-29 16:14:13 -070069 partialLd = pctx.AndroidStaticRule("partialLd",
Colin Cross3f40fa42015-01-30 17:27:36 -080070 blueprint.RuleParams{
Colin Cross41280a42015-11-23 14:01:42 -080071 Command: "$ldCmd -nostdlib -Wl,-r ${in} -o ${out} ${ldFlags}",
Dan Willemsenc94a7682015-11-17 15:27:28 -080072 CommandDeps: []string{"$ldCmd"},
Colin Cross3f40fa42015-01-30 17:27:36 -080073 },
Colin Cross41280a42015-11-23 14:01:42 -080074 "ldCmd", "ldFlags")
Colin Cross3f40fa42015-01-30 17:27:36 -080075
Colin Cross9d45bb72016-08-29 16:14:13 -070076 ar = pctx.AndroidStaticRule("ar",
Colin Cross3f40fa42015-01-30 17:27:36 -080077 blueprint.RuleParams{
Colin Cross7d21c442015-03-30 17:47:53 -070078 Command: "rm -f ${out} && $arCmd $arFlags $out @${out}.rsp",
Dan Willemsenc94a7682015-11-17 15:27:28 -080079 CommandDeps: []string{"$arCmd"},
Colin Cross7d21c442015-03-30 17:47:53 -070080 Rspfile: "${out}.rsp",
81 RspfileContent: "${in}",
Colin Cross3f40fa42015-01-30 17:27:36 -080082 },
83 "arCmd", "arFlags")
84
Colin Cross9d45bb72016-08-29 16:14:13 -070085 darwinAr = pctx.AndroidStaticRule("darwinAr",
Colin Cross0af4b842015-04-30 16:36:18 -070086 blueprint.RuleParams{
Colin Crossb98c8b02016-07-29 13:44:28 -070087 Command: "rm -f ${out} && ${config.MacArPath} $arFlags $out $in",
88 CommandDeps: []string{"${config.MacArPath}"},
Colin Cross0af4b842015-04-30 16:36:18 -070089 },
Colin Crossb8ecdfe2016-05-03 15:10:29 -070090 "arFlags")
Colin Cross0af4b842015-04-30 16:36:18 -070091
Colin Cross9d45bb72016-08-29 16:14:13 -070092 darwinAppendAr = pctx.AndroidStaticRule("darwinAppendAr",
Colin Cross0af4b842015-04-30 16:36:18 -070093 blueprint.RuleParams{
Colin Crossb98c8b02016-07-29 13:44:28 -070094 Command: "cp -f ${inAr} ${out}.tmp && ${config.MacArPath} $arFlags ${out}.tmp $in && mv ${out}.tmp ${out}",
95 CommandDeps: []string{"${config.MacArPath}", "${inAr}"},
Colin Cross0af4b842015-04-30 16:36:18 -070096 },
Colin Crossb8ecdfe2016-05-03 15:10:29 -070097 "arFlags", "inAr")
98
Colin Cross9d45bb72016-08-29 16:14:13 -070099 darwinStrip = pctx.AndroidStaticRule("darwinStrip",
Colin Crossb8ecdfe2016-05-03 15:10:29 -0700100 blueprint.RuleParams{
Colin Crossa24166b2016-08-01 15:42:38 -0700101 Command: "${config.MacStripPath} -u -r -o $out $in",
102 CommandDeps: []string{"${config.MacStripPath}"},
Colin Crossb8ecdfe2016-05-03 15:10:29 -0700103 })
Colin Cross0af4b842015-04-30 16:36:18 -0700104
Colin Cross9d45bb72016-08-29 16:14:13 -0700105 prefixSymbols = pctx.AndroidStaticRule("prefixSymbols",
Colin Crossbfae8852015-03-26 14:44:11 -0700106 blueprint.RuleParams{
107 Command: "$objcopyCmd --prefix-symbols=${prefix} ${in} ${out}",
Dan Willemsenc94a7682015-11-17 15:27:28 -0800108 CommandDeps: []string{"$objcopyCmd"},
Colin Crossbfae8852015-03-26 14:44:11 -0700109 },
110 "objcopyCmd", "prefix")
111
Nan Zhang43a485c2017-03-27 14:27:58 -0700112 _ = pctx.SourcePathVariable("stripPath", "build/soong/scripts/strip.sh")
Colin Cross665dce92016-04-28 14:50:03 -0700113
Colin Cross9d45bb72016-08-29 16:14:13 -0700114 strip = pctx.AndroidStaticRule("strip",
Colin Cross665dce92016-04-28 14:50:03 -0700115 blueprint.RuleParams{
116 Depfile: "${out}.d",
117 Deps: blueprint.DepsGCC,
118 Command: "CROSS_COMPILE=$crossCompile $stripPath ${args} -i ${in} -o ${out} -d ${out}.d",
119 CommandDeps: []string{"$stripPath"},
Colin Cross665dce92016-04-28 14:50:03 -0700120 },
121 "args", "crossCompile")
122
Colin Cross9d45bb72016-08-29 16:14:13 -0700123 emptyFile = pctx.AndroidStaticRule("emptyFile",
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700124 blueprint.RuleParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700125 Command: "rm -f $out && touch $out",
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700126 })
127
Nan Zhang43a485c2017-03-27 14:27:58 -0700128 _ = pctx.SourcePathVariable("copyGccLibPath", "build/soong/scripts/copygcclib.sh")
Colin Cross3f40fa42015-01-30 17:27:36 -0800129
Colin Cross9d45bb72016-08-29 16:14:13 -0700130 copyGccLib = pctx.AndroidStaticRule("copyGccLib",
Colin Cross3f40fa42015-01-30 17:27:36 -0800131 blueprint.RuleParams{
132 Depfile: "${out}.d",
133 Deps: blueprint.DepsGCC,
134 Command: "$copyGccLibPath $out $ccCmd $cFlags -print-file-name=${libName}",
Dan Willemsenc94a7682015-11-17 15:27:28 -0800135 CommandDeps: []string{"$copyGccLibPath", "$ccCmd"},
Colin Cross3f40fa42015-01-30 17:27:36 -0800136 },
137 "ccCmd", "cFlags", "libName")
Colin Cross26c34ed2016-09-30 17:10:16 -0700138
Nan Zhang43a485c2017-03-27 14:27:58 -0700139 _ = pctx.SourcePathVariable("tocPath", "build/soong/scripts/toc.sh")
Colin Cross26c34ed2016-09-30 17:10:16 -0700140
141 toc = pctx.AndroidStaticRule("toc",
142 blueprint.RuleParams{
143 Depfile: "${out}.d",
144 Deps: blueprint.DepsGCC,
145 Command: "CROSS_COMPILE=$crossCompile $tocPath -i ${in} -o ${out} -d ${out}.d",
146 CommandDeps: []string{"$tocPath"},
147 Restat: true,
148 },
149 "crossCompile")
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700150
151 clangTidy = pctx.AndroidStaticRule("clangTidy",
152 blueprint.RuleParams{
153 Command: "rm -f $out && ${config.ClangBin}/clang-tidy $tidyFlags $in -- $cFlags && touch $out",
154 CommandDeps: []string{"${config.ClangBin}/clang-tidy"},
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700155 },
156 "cFlags", "tidyFlags")
Colin Cross91e90042016-12-02 17:13:24 -0800157
Nan Zhang43a485c2017-03-27 14:27:58 -0700158 _ = pctx.SourcePathVariable("yasmCmd", "prebuilts/misc/${config.HostPrebuiltTag}/yasm/yasm")
Colin Cross91e90042016-12-02 17:13:24 -0800159
160 yasm = pctx.AndroidStaticRule("yasm",
161 blueprint.RuleParams{
Dan Willemsen1d3e5452017-08-22 20:53:45 -0700162 Command: "$yasmCmd $asFlags -o $out $in && $yasmCmd $asFlags -M $in >$out.d",
Colin Cross91e90042016-12-02 17:13:24 -0800163 CommandDeps: []string{"$yasmCmd"},
Dan Willemsen1d3e5452017-08-22 20:53:45 -0700164 Depfile: "$out.d",
165 Deps: blueprint.DepsGCC,
Colin Cross91e90042016-12-02 17:13:24 -0800166 },
167 "asFlags")
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800168
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700169 windres = pctx.AndroidStaticRule("windres",
170 blueprint.RuleParams{
171 Command: "$windresCmd $flags -I$$(dirname $in) -i $in -o $out",
172 CommandDeps: []string{"$windresCmd"},
173 },
174 "windresCmd", "flags")
175
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800176 _ = pctx.SourcePathVariable("sAbiDumper", "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/header-abi-dumper")
177
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700178 // -w has been added since header-abi-dumper does not need to produce any sort of diagnostic information.
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800179 sAbiDump = pctx.AndroidStaticRule("sAbiDump",
180 blueprint.RuleParams{
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700181 Command: "rm -f $out && $sAbiDumper -o ${out} $in $exportDirs -- $cFlags -w -isystem ${config.RSIncludePath}",
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800182 CommandDeps: []string{"$sAbiDumper"},
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800183 },
184 "cFlags", "exportDirs")
185
186 _ = pctx.SourcePathVariable("sAbiLinker", "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/header-abi-linker")
187
188 sAbiLink = pctx.AndroidStaticRule("sAbiLink",
189 blueprint.RuleParams{
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700190 Command: "$sAbiLinker -o ${out} $symbolFilter -arch $arch -api $api $exportedHeaderFlags @${out}.rsp ",
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800191 CommandDeps: []string{"$sAbiLinker"},
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800192 Rspfile: "${out}.rsp",
193 RspfileContent: "${in}",
194 },
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700195 "symbolFilter", "arch", "api", "exportedHeaderFlags")
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800196
197 _ = pctx.SourcePathVariable("sAbiDiffer", "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/header-abi-diff")
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700198
Jayant Chowdhary219139d2017-11-27 14:52:21 -0800199 sAbiDiff = pctx.AndroidRuleFunc("sAbiDiff",
200 func(config android.Config) (blueprint.RuleParams, error) {
201
202 commandStr := "($sAbiDiffer $allowFlags -lib $libName -arch $arch -check-all-apis -o ${out} -new $in -old $referenceDump)"
203 distDir := config.ProductVariables.DistDir
204 if distDir != nil && *distDir != "" {
205 distAbiDiffDir := *distDir + "/abidiffs/"
206 commandStr += " || (mkdir -p " + distAbiDiffDir + " && cp ${out} " + distAbiDiffDir + " && exit 1)"
207 }
208 return blueprint.RuleParams{
209 Command: commandStr,
210 CommandDeps: []string{"$sAbiDiffer"},
211 }, nil
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800212 },
Jayant Chowdharya3bb1b32017-11-01 11:12:15 -0700213 "allowFlags", "referenceDump", "libName", "arch")
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700214
215 unzipRefSAbiDump = pctx.AndroidStaticRule("unzipRefSAbiDump",
216 blueprint.RuleParams{
217 Command: "gunzip -c $in > $out",
218 })
Colin Cross3f40fa42015-01-30 17:27:36 -0800219)
220
Dan Willemsen322a0a62015-11-17 15:19:46 -0800221func init() {
222 // We run gcc/clang with PWD=/proc/self/cwd to remove $TOP from the
223 // debug output. That way two builds in two different directories will
224 // create the same output.
225 if runtime.GOOS != "darwin" {
226 pctx.StaticVariable("relPwd", "PWD=/proc/self/cwd")
227 } else {
228 // Darwin doesn't have /proc
229 pctx.StaticVariable("relPwd", "")
230 }
231}
232
Colin Cross3f40fa42015-01-30 17:27:36 -0800233type builderFlags struct {
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700234 globalFlags string
235 arFlags string
236 asFlags string
237 cFlags string
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700238 toolingCFlags string // A separate set of Cflags for clang LibTooling tools
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700239 conlyFlags string
240 cppFlags string
241 ldFlags string
242 libFlags string
243 yaccFlags string
244 protoFlags string
245 tidyFlags string
246 sAbiFlags string
247 yasmFlags string
248 aidlFlags string
249 rsFlags string
250 toolchain config.Toolchain
251 clang bool
252 tidy bool
253 coverage bool
254 sAbiDump bool
Colin Cross665dce92016-04-28 14:50:03 -0700255
Colin Crossc3199482017-03-30 15:03:04 -0700256 systemIncludeFlags string
257
Colin Cross18c0c5a2016-12-01 14:45:23 -0800258 groupStaticLibs bool
259
Colin Cross665dce92016-04-28 14:50:03 -0700260 stripKeepSymbols bool
261 stripKeepMiniDebugInfo bool
262 stripAddGnuDebuglink bool
Colin Cross3f40fa42015-01-30 17:27:36 -0800263}
264
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700265type Objects struct {
Dan Willemsen581341d2017-02-09 16:16:31 -0800266 objFiles android.Paths
267 tidyFiles android.Paths
268 coverageFiles android.Paths
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800269 sAbiDumpFiles android.Paths
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700270}
271
272func (a Objects) Copy() Objects {
273 return Objects{
Dan Willemsen581341d2017-02-09 16:16:31 -0800274 objFiles: append(android.Paths{}, a.objFiles...),
275 tidyFiles: append(android.Paths{}, a.tidyFiles...),
276 coverageFiles: append(android.Paths{}, a.coverageFiles...),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800277 sAbiDumpFiles: append(android.Paths{}, a.sAbiDumpFiles...),
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700278 }
279}
280
281func (a Objects) Append(b Objects) Objects {
282 return Objects{
Dan Willemsen581341d2017-02-09 16:16:31 -0800283 objFiles: append(a.objFiles, b.objFiles...),
284 tidyFiles: append(a.tidyFiles, b.tidyFiles...),
285 coverageFiles: append(a.coverageFiles, b.coverageFiles...),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800286 sAbiDumpFiles: append(a.sAbiDumpFiles, b.sAbiDumpFiles...),
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700287 }
288}
289
Colin Cross3f40fa42015-01-30 17:27:36 -0800290// Generate rules for compiling multiple .c, .cpp, or .S files to individual .o files
Colin Cross635c3b02016-05-18 15:37:25 -0700291func TransformSourceToObj(ctx android.ModuleContext, subdir string, srcFiles android.Paths,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700292 flags builderFlags, deps android.Paths) Objects {
Colin Cross581c1892015-04-07 16:50:10 -0700293
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700294 objFiles := make(android.Paths, len(srcFiles))
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700295 var tidyFiles android.Paths
296 if flags.tidy && flags.clang {
297 tidyFiles = make(android.Paths, 0, len(srcFiles))
298 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800299 var coverageFiles android.Paths
300 if flags.coverage {
301 coverageFiles = make(android.Paths, 0, len(srcFiles))
302 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800303
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700304 commonFlags := strings.Join([]string{
Colin Crossc3199482017-03-30 15:03:04 -0700305 flags.globalFlags,
306 flags.systemIncludeFlags,
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700307 }, " ")
308
309 toolingCflags := strings.Join([]string{
310 commonFlags,
311 flags.toolingCFlags,
312 flags.conlyFlags,
313 }, " ")
314
315 cflags := strings.Join([]string{
316 commonFlags,
Colin Crossc3199482017-03-30 15:03:04 -0700317 flags.cFlags,
318 flags.conlyFlags,
319 }, " ")
320
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700321 toolingCppflags := strings.Join([]string{
322 commonFlags,
323 flags.toolingCFlags,
324 flags.cppFlags,
325 }, " ")
326
Colin Crossc3199482017-03-30 15:03:04 -0700327 cppflags := strings.Join([]string{
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700328 commonFlags,
Colin Crossc3199482017-03-30 15:03:04 -0700329 flags.cFlags,
330 flags.cppFlags,
331 }, " ")
332
333 asflags := strings.Join([]string{
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700334 commonFlags,
Colin Crossc3199482017-03-30 15:03:04 -0700335 flags.asFlags,
336 }, " ")
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700337
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800338 var sAbiDumpFiles android.Paths
339 if flags.sAbiDump && flags.clang {
340 sAbiDumpFiles = make(android.Paths, 0, len(srcFiles))
341 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800342
Dan Willemsenbe03f342016-03-03 17:21:04 -0800343 if flags.clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700344 cflags += " ${config.NoOverrideClangGlobalCflags}"
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700345 toolingCflags += " ${config.NoOverrideClangGlobalCflags}"
Colin Crossb98c8b02016-07-29 13:44:28 -0700346 cppflags += " ${config.NoOverrideClangGlobalCflags}"
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700347 toolingCppflags += " ${config.NoOverrideClangGlobalCflags}"
Dan Willemsenbe03f342016-03-03 17:21:04 -0800348 } else {
Colin Crossb98c8b02016-07-29 13:44:28 -0700349 cflags += " ${config.NoOverrideGlobalCflags}"
350 cppflags += " ${config.NoOverrideGlobalCflags}"
Dan Willemsenbe03f342016-03-03 17:21:04 -0800351 }
352
Colin Cross3f40fa42015-01-30 17:27:36 -0800353 for i, srcFile := range srcFiles {
Dan Willemsen21ec4902016-11-02 20:43:13 -0700354 objFile := android.ObjPathWithExt(ctx, subdir, srcFile, "o")
Colin Cross3f40fa42015-01-30 17:27:36 -0800355
356 objFiles[i] = objFile
357
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700358 switch srcFile.Ext() {
359 case ".asm":
Colin Crossae887032017-10-23 17:16:14 -0700360 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700361 Rule: yasm,
362 Description: "yasm " + srcFile.Rel(),
363 Output: objFile,
364 Input: srcFile,
365 OrderOnly: deps,
Colin Cross91e90042016-12-02 17:13:24 -0800366 Args: map[string]string{
367 "asFlags": flags.yasmFlags,
368 },
369 })
370 continue
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700371 case ".rc":
Colin Crossae887032017-10-23 17:16:14 -0700372 ctx.Build(pctx, android.BuildParams{
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700373 Rule: windres,
374 Description: "windres " + srcFile.Rel(),
375 Output: objFile,
376 Input: srcFile,
377 OrderOnly: deps,
378 Args: map[string]string{
379 "windresCmd": gccCmd(flags.toolchain, "windres"),
380 "flags": flags.toolchain.WindresFlags(),
381 },
382 })
383 continue
Colin Cross91e90042016-12-02 17:13:24 -0800384 }
385
Colin Cross3f40fa42015-01-30 17:27:36 -0800386 var moduleCflags string
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700387 var moduleToolingCflags string
Colin Cross3f40fa42015-01-30 17:27:36 -0800388 var ccCmd string
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700389 tidy := flags.tidy && flags.clang
Dan Willemsen581341d2017-02-09 16:16:31 -0800390 coverage := flags.coverage
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800391 dump := flags.sAbiDump && flags.clang
Colin Cross3f40fa42015-01-30 17:27:36 -0800392
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700393 switch srcFile.Ext() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800394 case ".S", ".s":
395 ccCmd = "gcc"
396 moduleCflags = asflags
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700397 tidy = false
Dan Willemsen581341d2017-02-09 16:16:31 -0800398 coverage = false
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800399 dump = false
Colin Cross3f40fa42015-01-30 17:27:36 -0800400 case ".c":
401 ccCmd = "gcc"
402 moduleCflags = cflags
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700403 moduleToolingCflags = toolingCflags
Colin Cross9978ffe2016-12-01 15:31:22 -0800404 case ".cpp", ".cc", ".mm":
Colin Cross3f40fa42015-01-30 17:27:36 -0800405 ccCmd = "g++"
406 moduleCflags = cppflags
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700407 moduleToolingCflags = toolingCppflags
Colin Cross3f40fa42015-01-30 17:27:36 -0800408 default:
409 ctx.ModuleErrorf("File %s has unknown extension", srcFile)
410 continue
411 }
412
413 if flags.clang {
414 switch ccCmd {
415 case "gcc":
416 ccCmd = "clang"
417 case "g++":
418 ccCmd = "clang++"
419 default:
420 panic("unrecoginzied ccCmd")
421 }
Colin Cross67a5c132017-05-09 13:45:28 -0700422 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800423
Colin Cross67a5c132017-05-09 13:45:28 -0700424 ccDesc := ccCmd
425
426 if flags.clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700427 ccCmd = "${config.ClangBin}/" + ccCmd
Colin Cross3f40fa42015-01-30 17:27:36 -0800428 } else {
429 ccCmd = gccCmd(flags.toolchain, ccCmd)
430 }
431
Dan Willemsen581341d2017-02-09 16:16:31 -0800432 var implicitOutputs android.WritablePaths
433 if coverage {
434 gcnoFile := android.ObjPathWithExt(ctx, subdir, srcFile, "gcno")
435 implicitOutputs = append(implicitOutputs, gcnoFile)
436 coverageFiles = append(coverageFiles, gcnoFile)
437 }
438
Colin Crossae887032017-10-23 17:16:14 -0700439 ctx.Build(pctx, android.BuildParams{
Dan Willemsen581341d2017-02-09 16:16:31 -0800440 Rule: cc,
Colin Cross67a5c132017-05-09 13:45:28 -0700441 Description: ccDesc + " " + srcFile.Rel(),
Dan Willemsen581341d2017-02-09 16:16:31 -0800442 Output: objFile,
443 ImplicitOutputs: implicitOutputs,
444 Input: srcFile,
445 OrderOnly: deps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800446 Args: map[string]string{
Colin Cross28344522015-04-22 13:07:53 -0700447 "cFlags": moduleCflags,
448 "ccCmd": ccCmd,
Colin Cross3f40fa42015-01-30 17:27:36 -0800449 },
450 })
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700451
452 if tidy {
Dan Willemsen21ec4902016-11-02 20:43:13 -0700453 tidyFile := android.ObjPathWithExt(ctx, subdir, srcFile, "tidy")
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700454 tidyFiles = append(tidyFiles, tidyFile)
455
Colin Crossae887032017-10-23 17:16:14 -0700456 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700457 Rule: clangTidy,
458 Description: "clang-tidy " + srcFile.Rel(),
459 Output: tidyFile,
460 Input: srcFile,
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700461 // We must depend on objFile, since clang-tidy doesn't
462 // support exporting dependencies.
463 Implicit: objFile,
464 Args: map[string]string{
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700465 "cFlags": moduleToolingCflags,
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700466 "tidyFlags": flags.tidyFlags,
467 },
468 })
469 }
470
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800471 if dump {
472 sAbiDumpFile := android.ObjPathWithExt(ctx, subdir, srcFile, "sdump")
473 sAbiDumpFiles = append(sAbiDumpFiles, sAbiDumpFile)
474
Colin Crossae887032017-10-23 17:16:14 -0700475 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700476 Rule: sAbiDump,
477 Description: "header-abi-dumper " + srcFile.Rel(),
478 Output: sAbiDumpFile,
479 Input: srcFile,
480 Implicit: objFile,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800481 Args: map[string]string{
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700482 "cFlags": moduleToolingCflags,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800483 "exportDirs": flags.sAbiFlags,
484 },
485 })
486 }
487
Colin Cross3f40fa42015-01-30 17:27:36 -0800488 }
489
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700490 return Objects{
Dan Willemsen581341d2017-02-09 16:16:31 -0800491 objFiles: objFiles,
492 tidyFiles: tidyFiles,
493 coverageFiles: coverageFiles,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800494 sAbiDumpFiles: sAbiDumpFiles,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700495 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800496}
497
498// Generate a rule for compiling multiple .o files to a static library (.a)
Colin Cross635c3b02016-05-18 15:37:25 -0700499func TransformObjToStaticLib(ctx android.ModuleContext, objFiles android.Paths,
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700500 flags builderFlags, outputFile android.ModuleOutPath, deps android.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800501
Dan Willemsen581341d2017-02-09 16:16:31 -0800502 if ctx.Darwin() {
503 transformDarwinObjToStaticLib(ctx, objFiles, flags, outputFile, deps)
504 return
505 }
506
Colin Cross3f40fa42015-01-30 17:27:36 -0800507 arCmd := gccCmd(flags.toolchain, "ar")
508 arFlags := "crsPD"
Vishwath Mohan83d9f712017-03-16 11:01:23 -0700509 if flags.arFlags != "" {
510 arFlags += " " + flags.arFlags
511 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800512
Colin Crossae887032017-10-23 17:16:14 -0700513 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700514 Rule: ar,
515 Description: "static link " + outputFile.Base(),
516 Output: outputFile,
517 Inputs: objFiles,
518 Implicits: deps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800519 Args: map[string]string{
520 "arFlags": arFlags,
521 "arCmd": arCmd,
522 },
523 })
524}
525
Colin Cross0af4b842015-04-30 16:36:18 -0700526// Generate a rule for compiling multiple .o files to a static library (.a) on
527// darwin. The darwin ar tool doesn't support @file for list files, and has a
528// very small command line length limit, so we have to split the ar into multiple
529// steps, each appending to the previous one.
Dan Willemsen581341d2017-02-09 16:16:31 -0800530func transformDarwinObjToStaticLib(ctx android.ModuleContext, objFiles android.Paths,
Colin Cross5b529592017-05-09 13:34:34 -0700531 flags builderFlags, outputFile android.ModuleOutPath, deps android.Paths) {
Colin Cross0af4b842015-04-30 16:36:18 -0700532
Colin Cross0af4b842015-04-30 16:36:18 -0700533 arFlags := "cqs"
534
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700535 if len(objFiles) == 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700536 dummy := android.PathForModuleOut(ctx, "dummy"+objectExtension)
537 dummyAr := android.PathForModuleOut(ctx, "dummy"+staticLibraryExtension)
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700538
Colin Crossae887032017-10-23 17:16:14 -0700539 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700540 Rule: emptyFile,
541 Description: "empty object file",
542 Output: dummy,
543 Implicits: deps,
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700544 })
545
Colin Crossae887032017-10-23 17:16:14 -0700546 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700547 Rule: darwinAr,
548 Description: "empty static archive",
549 Output: dummyAr,
550 Input: dummy,
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700551 Args: map[string]string{
552 "arFlags": arFlags,
553 },
554 })
555
Colin Crossae887032017-10-23 17:16:14 -0700556 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700557 Rule: darwinAppendAr,
558 Description: "static link " + outputFile.Base(),
559 Output: outputFile,
560 Input: dummy,
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700561 Args: map[string]string{
562 "arFlags": "d",
563 "inAr": dummyAr.String(),
564 },
565 })
566
567 return
568 }
569
Colin Cross0af4b842015-04-30 16:36:18 -0700570 // ARG_MAX on darwin is 262144, use half that to be safe
Colin Cross5b529592017-05-09 13:34:34 -0700571 objFilesLists, err := splitListForSize(objFiles, 131072)
Colin Cross0af4b842015-04-30 16:36:18 -0700572 if err != nil {
573 ctx.ModuleErrorf("%s", err.Error())
574 }
575
Colin Cross5b529592017-05-09 13:34:34 -0700576 var in, out android.WritablePath
Colin Cross0af4b842015-04-30 16:36:18 -0700577 for i, l := range objFilesLists {
578 in = out
579 out = outputFile
580 if i != len(objFilesLists)-1 {
Colin Cross5b529592017-05-09 13:34:34 -0700581 out = android.PathForModuleOut(ctx, outputFile.Base()+strconv.Itoa(i))
Colin Cross0af4b842015-04-30 16:36:18 -0700582 }
583
Colin Crossae887032017-10-23 17:16:14 -0700584 build := android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700585 Rule: darwinAr,
586 Description: "static link " + out.Base(),
587 Output: out,
588 Inputs: l,
589 Implicits: deps,
Colin Cross5b529592017-05-09 13:34:34 -0700590 Args: map[string]string{
591 "arFlags": arFlags,
592 },
Colin Cross0af4b842015-04-30 16:36:18 -0700593 }
Colin Cross5b529592017-05-09 13:34:34 -0700594 if i != 0 {
595 build.Rule = darwinAppendAr
596 build.Args["inAr"] = in.String()
597 }
Colin Crossae887032017-10-23 17:16:14 -0700598 ctx.Build(pctx, build)
Colin Cross0af4b842015-04-30 16:36:18 -0700599 }
600}
601
Colin Cross3f40fa42015-01-30 17:27:36 -0800602// Generate a rule for compiling multiple .o files, plus static libraries, whole static libraries,
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700603// and shared libraries, to a shared library (.so) or dynamic executable
Colin Cross635c3b02016-05-18 15:37:25 -0700604func TransformObjToDynamicBinary(ctx android.ModuleContext,
605 objFiles, sharedLibs, staticLibs, lateStaticLibs, wholeStaticLibs, deps android.Paths,
606 crtBegin, crtEnd android.OptionalPath, groupLate bool, flags builderFlags, outputFile android.WritablePath) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800607
608 var ldCmd string
609 if flags.clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700610 ldCmd = "${config.ClangBin}/clang++"
Colin Cross3f40fa42015-01-30 17:27:36 -0800611 } else {
612 ldCmd = gccCmd(flags.toolchain, "g++")
613 }
614
Colin Cross3f40fa42015-01-30 17:27:36 -0800615 var libFlagsList []string
616
Colin Cross16b23492016-01-06 14:41:07 -0800617 if len(flags.libFlags) > 0 {
618 libFlagsList = append(libFlagsList, flags.libFlags)
619 }
620
Colin Cross3f40fa42015-01-30 17:27:36 -0800621 if len(wholeStaticLibs) > 0 {
Dan Willemsen490fd492015-11-24 17:53:15 -0800622 if ctx.Host() && ctx.Darwin() {
Colin Cross635c3b02016-05-18 15:37:25 -0700623 libFlagsList = append(libFlagsList, android.JoinWithPrefix(wholeStaticLibs.Strings(), "-force_load "))
Colin Cross0af4b842015-04-30 16:36:18 -0700624 } else {
625 libFlagsList = append(libFlagsList, "-Wl,--whole-archive ")
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700626 libFlagsList = append(libFlagsList, wholeStaticLibs.Strings()...)
Colin Cross0af4b842015-04-30 16:36:18 -0700627 libFlagsList = append(libFlagsList, "-Wl,--no-whole-archive ")
628 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800629 }
630
Colin Cross7a7cf972016-12-05 18:47:39 -0800631 if flags.groupStaticLibs && !ctx.Darwin() && len(staticLibs) > 0 {
Colin Cross18c0c5a2016-12-01 14:45:23 -0800632 libFlagsList = append(libFlagsList, "-Wl,--start-group")
633 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700634 libFlagsList = append(libFlagsList, staticLibs.Strings()...)
Colin Cross7a7cf972016-12-05 18:47:39 -0800635 if flags.groupStaticLibs && !ctx.Darwin() && len(staticLibs) > 0 {
Colin Cross18c0c5a2016-12-01 14:45:23 -0800636 libFlagsList = append(libFlagsList, "-Wl,--end-group")
637 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800638
Stephen Hines10347862016-07-18 15:54:54 -0700639 if groupLate && !ctx.Darwin() && len(lateStaticLibs) > 0 {
Dan Willemsenedc385f2015-07-08 13:02:23 -0700640 libFlagsList = append(libFlagsList, "-Wl,--start-group")
641 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700642 libFlagsList = append(libFlagsList, lateStaticLibs.Strings()...)
Stephen Hines10347862016-07-18 15:54:54 -0700643 if groupLate && !ctx.Darwin() && len(lateStaticLibs) > 0 {
Dan Willemsenedc385f2015-07-08 13:02:23 -0700644 libFlagsList = append(libFlagsList, "-Wl,--end-group")
645 }
646
Colin Cross3f40fa42015-01-30 17:27:36 -0800647 for _, lib := range sharedLibs {
Dan Albert9840e1b2016-07-21 08:47:33 -0700648 libFlagsList = append(libFlagsList, lib.String())
Colin Cross3f40fa42015-01-30 17:27:36 -0800649 }
650
Colin Cross3f40fa42015-01-30 17:27:36 -0800651 deps = append(deps, staticLibs...)
Colin Cross3075ad02015-03-17 10:47:08 -0700652 deps = append(deps, lateStaticLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800653 deps = append(deps, wholeStaticLibs...)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700654 if crtBegin.Valid() {
655 deps = append(deps, crtBegin.Path(), crtEnd.Path())
Colin Cross3f40fa42015-01-30 17:27:36 -0800656 }
657
Colin Crossae887032017-10-23 17:16:14 -0700658 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700659 Rule: ld,
660 Description: "link " + outputFile.Base(),
661 Output: outputFile,
662 Inputs: objFiles,
663 Implicits: deps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800664 Args: map[string]string{
Dan Albertce2b8392016-07-21 13:16:49 -0700665 "ldCmd": ldCmd,
666 "crtBegin": crtBegin.String(),
667 "libFlags": strings.Join(libFlagsList, " "),
668 "ldFlags": flags.ldFlags,
669 "crtEnd": crtEnd.String(),
Colin Cross3f40fa42015-01-30 17:27:36 -0800670 },
671 })
672}
673
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800674// Generate a rule to combine .dump sAbi dump files from multiple source files
675// into a single .ldump sAbi dump file
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700676func TransformDumpToLinkedDump(ctx android.ModuleContext, sAbiDumps android.Paths, soFile android.Path,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800677 symbolFile android.OptionalPath, apiLevel, baseName, exportedHeaderFlags string) android.OptionalPath {
678 outputFile := android.PathForModuleOut(ctx, baseName+".lsdump")
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700679 var symbolFilterStr string
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800680 var linkedDumpDep android.Path
681 if symbolFile.Valid() {
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700682 symbolFilterStr = "-v " + symbolFile.Path().String()
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800683 linkedDumpDep = symbolFile.Path()
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700684 } else {
685 linkedDumpDep = soFile
686 symbolFilterStr = "-so " + soFile.String()
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800687 }
Colin Crossae887032017-10-23 17:16:14 -0700688 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700689 Rule: sAbiLink,
690 Description: "header-abi-linker " + outputFile.Base(),
691 Output: outputFile,
692 Inputs: sAbiDumps,
693 Implicit: linkedDumpDep,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800694 Args: map[string]string{
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700695 "symbolFilter": symbolFilterStr,
696 "arch": ctx.Arch().ArchType.Name,
697 "api": apiLevel,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800698 "exportedHeaderFlags": exportedHeaderFlags,
699 },
700 })
701 return android.OptionalPathForPath(outputFile)
702}
703
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700704func UnzipRefDump(ctx android.ModuleContext, zippedRefDump android.Path, baseName string) android.Path {
705 outputFile := android.PathForModuleOut(ctx, baseName+"_ref.lsdump")
Colin Crossae887032017-10-23 17:16:14 -0700706 ctx.Build(pctx, android.BuildParams{
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700707 Rule: unzipRefSAbiDump,
708 Description: "gunzip" + outputFile.Base(),
709 Output: outputFile,
710 Input: zippedRefDump,
711 })
712 return outputFile
713}
714
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800715func SourceAbiDiff(ctx android.ModuleContext, inputDump android.Path, referenceDump android.Path,
716 baseName string) android.OptionalPath {
717 outputFile := android.PathForModuleOut(ctx, baseName+".abidiff")
Colin Crossae887032017-10-23 17:16:14 -0700718 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700719 Rule: sAbiDiff,
720 Description: "header-abi-diff " + outputFile.Base(),
721 Output: outputFile,
722 Input: inputDump,
723 Implicit: referenceDump,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800724 Args: map[string]string{
725 "referenceDump": referenceDump.String(),
Jayant Chowdharyf54e0a72017-05-22 10:53:24 -0700726 "libName": baseName,
727 "arch": ctx.Arch().ArchType.Name,
Jayant Chowdharya3bb1b32017-11-01 11:12:15 -0700728 "allowFlags": strings.Join(abiCheckAllowFlags, " "),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800729 },
730 })
731 return android.OptionalPathForPath(outputFile)
732}
733
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700734// Generate a rule for extracting a table of contents from a shared library (.so)
735func TransformSharedObjectToToc(ctx android.ModuleContext, inputFile android.Path,
Colin Cross26c34ed2016-09-30 17:10:16 -0700736 outputFile android.WritablePath, flags builderFlags) {
737
738 crossCompile := gccCmd(flags.toolchain, "")
739
Colin Crossae887032017-10-23 17:16:14 -0700740 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700741 Rule: toc,
742 Description: "generate toc " + inputFile.Base(),
743 Output: outputFile,
744 Input: inputFile,
Colin Cross26c34ed2016-09-30 17:10:16 -0700745 Args: map[string]string{
746 "crossCompile": crossCompile,
747 },
748 })
749}
750
Colin Cross3f40fa42015-01-30 17:27:36 -0800751// Generate a rule for compiling multiple .o files to a .o using ld partial linking
Colin Cross635c3b02016-05-18 15:37:25 -0700752func TransformObjsToObj(ctx android.ModuleContext, objFiles android.Paths,
753 flags builderFlags, outputFile android.WritablePath) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800754
Colin Cross41280a42015-11-23 14:01:42 -0800755 var ldCmd string
756 if flags.clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700757 ldCmd = "${config.ClangBin}/clang++"
Colin Cross41280a42015-11-23 14:01:42 -0800758 } else {
759 ldCmd = gccCmd(flags.toolchain, "g++")
760 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800761
Colin Crossae887032017-10-23 17:16:14 -0700762 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700763 Rule: partialLd,
764 Description: "link " + outputFile.Base(),
765 Output: outputFile,
766 Inputs: objFiles,
Colin Cross3f40fa42015-01-30 17:27:36 -0800767 Args: map[string]string{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700768 "ldCmd": ldCmd,
Colin Cross41280a42015-11-23 14:01:42 -0800769 "ldFlags": flags.ldFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800770 },
771 })
772}
773
Colin Crossbfae8852015-03-26 14:44:11 -0700774// Generate a rule for runing objcopy --prefix-symbols on a binary
Colin Cross635c3b02016-05-18 15:37:25 -0700775func TransformBinaryPrefixSymbols(ctx android.ModuleContext, prefix string, inputFile android.Path,
776 flags builderFlags, outputFile android.WritablePath) {
Colin Crossbfae8852015-03-26 14:44:11 -0700777
778 objcopyCmd := gccCmd(flags.toolchain, "objcopy")
779
Colin Crossae887032017-10-23 17:16:14 -0700780 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700781 Rule: prefixSymbols,
782 Description: "prefix symbols " + outputFile.Base(),
783 Output: outputFile,
784 Input: inputFile,
Colin Crossbfae8852015-03-26 14:44:11 -0700785 Args: map[string]string{
786 "objcopyCmd": objcopyCmd,
787 "prefix": prefix,
788 },
789 })
790}
791
Colin Cross635c3b02016-05-18 15:37:25 -0700792func TransformStrip(ctx android.ModuleContext, inputFile android.Path,
793 outputFile android.WritablePath, flags builderFlags) {
Colin Cross665dce92016-04-28 14:50:03 -0700794
795 crossCompile := gccCmd(flags.toolchain, "")
796 args := ""
797 if flags.stripAddGnuDebuglink {
798 args += " --add-gnu-debuglink"
799 }
800 if flags.stripKeepMiniDebugInfo {
801 args += " --keep-mini-debug-info"
802 }
803 if flags.stripKeepSymbols {
804 args += " --keep-symbols"
805 }
806
Colin Crossae887032017-10-23 17:16:14 -0700807 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700808 Rule: strip,
809 Description: "strip " + outputFile.Base(),
810 Output: outputFile,
811 Input: inputFile,
Colin Cross665dce92016-04-28 14:50:03 -0700812 Args: map[string]string{
813 "crossCompile": crossCompile,
814 "args": args,
815 },
816 })
817}
818
Colin Cross635c3b02016-05-18 15:37:25 -0700819func TransformDarwinStrip(ctx android.ModuleContext, inputFile android.Path,
820 outputFile android.WritablePath) {
Colin Crossb8ecdfe2016-05-03 15:10:29 -0700821
Colin Crossae887032017-10-23 17:16:14 -0700822 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700823 Rule: darwinStrip,
824 Description: "strip " + outputFile.Base(),
825 Output: outputFile,
826 Input: inputFile,
Colin Crossb8ecdfe2016-05-03 15:10:29 -0700827 })
828}
829
Dan Willemsen581341d2017-02-09 16:16:31 -0800830func TransformCoverageFilesToLib(ctx android.ModuleContext,
831 inputs Objects, flags builderFlags, baseName string) android.OptionalPath {
832
833 if len(inputs.coverageFiles) > 0 {
834 outputFile := android.PathForModuleOut(ctx, baseName+".gcnodir")
835
836 TransformObjToStaticLib(ctx, inputs.coverageFiles, flags, outputFile, nil)
837
838 return android.OptionalPathForPath(outputFile)
839 }
840
841 return android.OptionalPath{}
842}
843
Colin Cross635c3b02016-05-18 15:37:25 -0700844func CopyGccLib(ctx android.ModuleContext, libName string,
845 flags builderFlags, outputFile android.WritablePath) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800846
Colin Crossae887032017-10-23 17:16:14 -0700847 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700848 Rule: copyGccLib,
849 Description: "copy gcc library " + libName,
850 Output: outputFile,
Colin Cross3f40fa42015-01-30 17:27:36 -0800851 Args: map[string]string{
852 "ccCmd": gccCmd(flags.toolchain, "gcc"),
853 "cFlags": flags.globalFlags,
854 "libName": libName,
855 },
856 })
857}
858
Colin Crossb98c8b02016-07-29 13:44:28 -0700859func gccCmd(toolchain config.Toolchain, cmd string) string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800860 return filepath.Join(toolchain.GccRoot(), "bin", toolchain.GccTriple()+"-"+cmd)
861}
Colin Cross0af4b842015-04-30 16:36:18 -0700862
Colin Cross5b529592017-05-09 13:34:34 -0700863func splitListForSize(list android.Paths, limit int) (lists []android.Paths, err error) {
Colin Cross0af4b842015-04-30 16:36:18 -0700864 var i int
865
866 start := 0
867 bytes := 0
868 for i = range list {
Colin Cross5b529592017-05-09 13:34:34 -0700869 l := len(list[i].String())
Colin Cross0af4b842015-04-30 16:36:18 -0700870 if l > limit {
871 return nil, fmt.Errorf("list element greater than size limit (%d)", limit)
872 }
873 if bytes+l > limit {
874 lists = append(lists, list[start:i])
875 start = i
876 bytes = 0
877 }
878 bytes += l + 1 // count a space between each list element
879 }
880
881 lists = append(lists, list[start:])
882
883 totalLen := 0
884 for _, l := range lists {
885 totalLen += len(l)
886 }
887 if totalLen != len(list) {
888 panic(fmt.Errorf("Failed breaking up list, %d != %d", len(list), totalLen))
889 }
890 return lists, nil
891}