blob: 873b117b3424d89d1a7e9f8c13ad4eec99ff261e [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 (
Colin Cross635c3b02016-05-18 15:37:25 -070040 pctx = android.NewPackageContext("android/soong/cc")
Colin Cross3f40fa42015-01-30 17:27:36 -080041
Colin Cross9d45bb72016-08-29 16:14:13 -070042 cc = pctx.AndroidGomaStaticRule("cc",
Colin Cross3f40fa42015-01-30 17:27:36 -080043 blueprint.RuleParams{
44 Depfile: "${out}.d",
45 Deps: blueprint.DepsGCC,
Alistair Strachan777475c2016-08-26 12:55:49 -070046 Command: "$relPwd ${config.CcWrapper}$ccCmd -c $cFlags -MD -MF ${out}.d -o $out $in",
Dan Willemsenc94a7682015-11-17 15:27:28 -080047 CommandDeps: []string{"$ccCmd"},
Colin Cross3f40fa42015-01-30 17:27:36 -080048 },
Dan Willemsen322a0a62015-11-17 15:19:46 -080049 "ccCmd", "cFlags")
Colin Cross3f40fa42015-01-30 17:27:36 -080050
Colin Cross9d45bb72016-08-29 16:14:13 -070051 ld = pctx.AndroidStaticRule("ld",
Colin Cross3f40fa42015-01-30 17:27:36 -080052 blueprint.RuleParams{
Dan Albertce2b8392016-07-21 13:16:49 -070053 Command: "$ldCmd ${crtBegin} @${out}.rsp " +
Colin Cross28344522015-04-22 13:07:53 -070054 "${libFlags} ${crtEnd} -o ${out} ${ldFlags}",
Dan Willemsenc94a7682015-11-17 15:27:28 -080055 CommandDeps: []string{"$ldCmd"},
Colin Cross7d21c442015-03-30 17:47:53 -070056 Rspfile: "${out}.rsp",
57 RspfileContent: "${in}",
Colin Cross3f40fa42015-01-30 17:27:36 -080058 },
Dan Albertce2b8392016-07-21 13:16:49 -070059 "ldCmd", "crtBegin", "libFlags", "crtEnd", "ldFlags")
Colin Cross3f40fa42015-01-30 17:27:36 -080060
Colin Cross9d45bb72016-08-29 16:14:13 -070061 partialLd = pctx.AndroidStaticRule("partialLd",
Colin Cross3f40fa42015-01-30 17:27:36 -080062 blueprint.RuleParams{
Colin Cross41280a42015-11-23 14:01:42 -080063 Command: "$ldCmd -nostdlib -Wl,-r ${in} -o ${out} ${ldFlags}",
Dan Willemsenc94a7682015-11-17 15:27:28 -080064 CommandDeps: []string{"$ldCmd"},
Colin Cross3f40fa42015-01-30 17:27:36 -080065 },
Colin Cross41280a42015-11-23 14:01:42 -080066 "ldCmd", "ldFlags")
Colin Cross3f40fa42015-01-30 17:27:36 -080067
Colin Cross9d45bb72016-08-29 16:14:13 -070068 ar = pctx.AndroidStaticRule("ar",
Colin Cross3f40fa42015-01-30 17:27:36 -080069 blueprint.RuleParams{
Colin Cross7d21c442015-03-30 17:47:53 -070070 Command: "rm -f ${out} && $arCmd $arFlags $out @${out}.rsp",
Dan Willemsenc94a7682015-11-17 15:27:28 -080071 CommandDeps: []string{"$arCmd"},
Colin Cross7d21c442015-03-30 17:47:53 -070072 Rspfile: "${out}.rsp",
73 RspfileContent: "${in}",
Colin Cross3f40fa42015-01-30 17:27:36 -080074 },
75 "arCmd", "arFlags")
76
Colin Cross9d45bb72016-08-29 16:14:13 -070077 darwinAr = pctx.AndroidStaticRule("darwinAr",
Colin Cross0af4b842015-04-30 16:36:18 -070078 blueprint.RuleParams{
Colin Crossb98c8b02016-07-29 13:44:28 -070079 Command: "rm -f ${out} && ${config.MacArPath} $arFlags $out $in",
80 CommandDeps: []string{"${config.MacArPath}"},
Colin Cross0af4b842015-04-30 16:36:18 -070081 },
Colin Crossb8ecdfe2016-05-03 15:10:29 -070082 "arFlags")
Colin Cross0af4b842015-04-30 16:36:18 -070083
Colin Cross9d45bb72016-08-29 16:14:13 -070084 darwinAppendAr = pctx.AndroidStaticRule("darwinAppendAr",
Colin Cross0af4b842015-04-30 16:36:18 -070085 blueprint.RuleParams{
Colin Crossb98c8b02016-07-29 13:44:28 -070086 Command: "cp -f ${inAr} ${out}.tmp && ${config.MacArPath} $arFlags ${out}.tmp $in && mv ${out}.tmp ${out}",
87 CommandDeps: []string{"${config.MacArPath}", "${inAr}"},
Colin Cross0af4b842015-04-30 16:36:18 -070088 },
Colin Crossb8ecdfe2016-05-03 15:10:29 -070089 "arFlags", "inAr")
90
Colin Cross9d45bb72016-08-29 16:14:13 -070091 darwinStrip = pctx.AndroidStaticRule("darwinStrip",
Colin Crossb8ecdfe2016-05-03 15:10:29 -070092 blueprint.RuleParams{
Colin Crossa24166b2016-08-01 15:42:38 -070093 Command: "${config.MacStripPath} -u -r -o $out $in",
94 CommandDeps: []string{"${config.MacStripPath}"},
Colin Crossb8ecdfe2016-05-03 15:10:29 -070095 })
Colin Cross0af4b842015-04-30 16:36:18 -070096
Colin Cross9d45bb72016-08-29 16:14:13 -070097 prefixSymbols = pctx.AndroidStaticRule("prefixSymbols",
Colin Crossbfae8852015-03-26 14:44:11 -070098 blueprint.RuleParams{
99 Command: "$objcopyCmd --prefix-symbols=${prefix} ${in} ${out}",
Dan Willemsenc94a7682015-11-17 15:27:28 -0800100 CommandDeps: []string{"$objcopyCmd"},
Colin Crossbfae8852015-03-26 14:44:11 -0700101 },
102 "objcopyCmd", "prefix")
103
Nan Zhang43a485c2017-03-27 14:27:58 -0700104 _ = pctx.SourcePathVariable("stripPath", "build/soong/scripts/strip.sh")
Colin Cross665dce92016-04-28 14:50:03 -0700105
Colin Cross9d45bb72016-08-29 16:14:13 -0700106 strip = pctx.AndroidStaticRule("strip",
Colin Cross665dce92016-04-28 14:50:03 -0700107 blueprint.RuleParams{
108 Depfile: "${out}.d",
109 Deps: blueprint.DepsGCC,
110 Command: "CROSS_COMPILE=$crossCompile $stripPath ${args} -i ${in} -o ${out} -d ${out}.d",
111 CommandDeps: []string{"$stripPath"},
Colin Cross665dce92016-04-28 14:50:03 -0700112 },
113 "args", "crossCompile")
114
Colin Cross9d45bb72016-08-29 16:14:13 -0700115 emptyFile = pctx.AndroidStaticRule("emptyFile",
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700116 blueprint.RuleParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700117 Command: "rm -f $out && touch $out",
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700118 })
119
Nan Zhang43a485c2017-03-27 14:27:58 -0700120 _ = pctx.SourcePathVariable("copyGccLibPath", "build/soong/scripts/copygcclib.sh")
Colin Cross3f40fa42015-01-30 17:27:36 -0800121
Colin Cross9d45bb72016-08-29 16:14:13 -0700122 copyGccLib = pctx.AndroidStaticRule("copyGccLib",
Colin Cross3f40fa42015-01-30 17:27:36 -0800123 blueprint.RuleParams{
124 Depfile: "${out}.d",
125 Deps: blueprint.DepsGCC,
126 Command: "$copyGccLibPath $out $ccCmd $cFlags -print-file-name=${libName}",
Dan Willemsenc94a7682015-11-17 15:27:28 -0800127 CommandDeps: []string{"$copyGccLibPath", "$ccCmd"},
Colin Cross3f40fa42015-01-30 17:27:36 -0800128 },
129 "ccCmd", "cFlags", "libName")
Colin Cross26c34ed2016-09-30 17:10:16 -0700130
Nan Zhang43a485c2017-03-27 14:27:58 -0700131 _ = pctx.SourcePathVariable("tocPath", "build/soong/scripts/toc.sh")
Colin Cross26c34ed2016-09-30 17:10:16 -0700132
133 toc = pctx.AndroidStaticRule("toc",
134 blueprint.RuleParams{
135 Depfile: "${out}.d",
136 Deps: blueprint.DepsGCC,
137 Command: "CROSS_COMPILE=$crossCompile $tocPath -i ${in} -o ${out} -d ${out}.d",
138 CommandDeps: []string{"$tocPath"},
139 Restat: true,
140 },
141 "crossCompile")
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700142
143 clangTidy = pctx.AndroidStaticRule("clangTidy",
144 blueprint.RuleParams{
145 Command: "rm -f $out && ${config.ClangBin}/clang-tidy $tidyFlags $in -- $cFlags && touch $out",
146 CommandDeps: []string{"${config.ClangBin}/clang-tidy"},
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700147 },
148 "cFlags", "tidyFlags")
Colin Cross91e90042016-12-02 17:13:24 -0800149
Nan Zhang43a485c2017-03-27 14:27:58 -0700150 _ = pctx.SourcePathVariable("yasmCmd", "prebuilts/misc/${config.HostPrebuiltTag}/yasm/yasm")
Colin Cross91e90042016-12-02 17:13:24 -0800151
152 yasm = pctx.AndroidStaticRule("yasm",
153 blueprint.RuleParams{
154 Command: "$yasmCmd $asFlags -o $out $in",
155 CommandDeps: []string{"$yasmCmd"},
Colin Cross91e90042016-12-02 17:13:24 -0800156 },
157 "asFlags")
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800158
159 _ = pctx.SourcePathVariable("sAbiDumper", "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/header-abi-dumper")
160
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700161 // -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 -0800162 sAbiDump = pctx.AndroidStaticRule("sAbiDump",
163 blueprint.RuleParams{
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700164 Command: "rm -f $out && $sAbiDumper -o ${out} $in $exportDirs -- $cFlags -w -isystem ${config.RSIncludePath}",
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800165 CommandDeps: []string{"$sAbiDumper"},
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800166 },
167 "cFlags", "exportDirs")
168
169 _ = pctx.SourcePathVariable("sAbiLinker", "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/header-abi-linker")
170
171 sAbiLink = pctx.AndroidStaticRule("sAbiLink",
172 blueprint.RuleParams{
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700173 Command: "$sAbiLinker -o ${out} $symbolFilter -arch $arch -api $api $exportedHeaderFlags @${out}.rsp ",
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800174 CommandDeps: []string{"$sAbiLinker"},
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800175 Rspfile: "${out}.rsp",
176 RspfileContent: "${in}",
177 },
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700178 "symbolFilter", "arch", "api", "exportedHeaderFlags")
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800179
180 _ = pctx.SourcePathVariable("sAbiDiffer", "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/header-abi-diff")
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700181
Jayant Chowdhary918b1d92017-04-18 10:44:00 -0700182 // Abidiff check turned on in advice-only mode. Builds will not fail on abi incompatibilties / extensions.
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800183 sAbiDiff = pctx.AndroidStaticRule("sAbiDiff",
184 blueprint.RuleParams{
Jayant Chowdharyf54e0a72017-05-22 10:53:24 -0700185 Command: "$sAbiDiffer -lib $libName -arch $arch -advice-only -o ${out} -new $in -old $referenceDump",
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800186 CommandDeps: []string{"$sAbiDiffer"},
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800187 },
Jayant Chowdharyf54e0a72017-05-22 10:53:24 -0700188 "referenceDump", "libName", "arch")
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700189
190 unzipRefSAbiDump = pctx.AndroidStaticRule("unzipRefSAbiDump",
191 blueprint.RuleParams{
192 Command: "gunzip -c $in > $out",
193 })
Colin Cross3f40fa42015-01-30 17:27:36 -0800194)
195
Dan Willemsen322a0a62015-11-17 15:19:46 -0800196func init() {
197 // We run gcc/clang with PWD=/proc/self/cwd to remove $TOP from the
198 // debug output. That way two builds in two different directories will
199 // create the same output.
200 if runtime.GOOS != "darwin" {
201 pctx.StaticVariable("relPwd", "PWD=/proc/self/cwd")
202 } else {
203 // Darwin doesn't have /proc
204 pctx.StaticVariable("relPwd", "")
205 }
206}
207
Colin Cross3f40fa42015-01-30 17:27:36 -0800208type builderFlags struct {
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700209 globalFlags string
210 arFlags string
211 asFlags string
212 cFlags string
213 toolingCFlags string // Seperate set of Cflags for clang LibTooling tools
214 conlyFlags string
215 cppFlags string
216 ldFlags string
217 libFlags string
218 yaccFlags string
219 protoFlags string
220 tidyFlags string
221 sAbiFlags string
222 yasmFlags string
223 aidlFlags string
224 rsFlags string
225 toolchain config.Toolchain
226 clang bool
227 tidy bool
228 coverage bool
229 sAbiDump bool
Colin Cross665dce92016-04-28 14:50:03 -0700230
Colin Crossc3199482017-03-30 15:03:04 -0700231 systemIncludeFlags string
232
Colin Cross18c0c5a2016-12-01 14:45:23 -0800233 groupStaticLibs bool
234
Colin Cross665dce92016-04-28 14:50:03 -0700235 stripKeepSymbols bool
236 stripKeepMiniDebugInfo bool
237 stripAddGnuDebuglink bool
Colin Cross3f40fa42015-01-30 17:27:36 -0800238}
239
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700240type Objects struct {
Dan Willemsen581341d2017-02-09 16:16:31 -0800241 objFiles android.Paths
242 tidyFiles android.Paths
243 coverageFiles android.Paths
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800244 sAbiDumpFiles android.Paths
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700245}
246
247func (a Objects) Copy() Objects {
248 return Objects{
Dan Willemsen581341d2017-02-09 16:16:31 -0800249 objFiles: append(android.Paths{}, a.objFiles...),
250 tidyFiles: append(android.Paths{}, a.tidyFiles...),
251 coverageFiles: append(android.Paths{}, a.coverageFiles...),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800252 sAbiDumpFiles: append(android.Paths{}, a.sAbiDumpFiles...),
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700253 }
254}
255
256func (a Objects) Append(b Objects) Objects {
257 return Objects{
Dan Willemsen581341d2017-02-09 16:16:31 -0800258 objFiles: append(a.objFiles, b.objFiles...),
259 tidyFiles: append(a.tidyFiles, b.tidyFiles...),
260 coverageFiles: append(a.coverageFiles, b.coverageFiles...),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800261 sAbiDumpFiles: append(a.sAbiDumpFiles, b.sAbiDumpFiles...),
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700262 }
263}
264
Colin Cross3f40fa42015-01-30 17:27:36 -0800265// Generate rules for compiling multiple .c, .cpp, or .S files to individual .o files
Colin Cross635c3b02016-05-18 15:37:25 -0700266func TransformSourceToObj(ctx android.ModuleContext, subdir string, srcFiles android.Paths,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700267 flags builderFlags, deps android.Paths) Objects {
Colin Cross581c1892015-04-07 16:50:10 -0700268
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700269 objFiles := make(android.Paths, len(srcFiles))
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700270 var tidyFiles android.Paths
271 if flags.tidy && flags.clang {
272 tidyFiles = make(android.Paths, 0, len(srcFiles))
273 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800274 var coverageFiles android.Paths
275 if flags.coverage {
276 coverageFiles = make(android.Paths, 0, len(srcFiles))
277 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800278
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700279 commonFlags := strings.Join([]string{
Colin Crossc3199482017-03-30 15:03:04 -0700280 flags.globalFlags,
281 flags.systemIncludeFlags,
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700282 }, " ")
283
284 toolingCflags := strings.Join([]string{
285 commonFlags,
286 flags.toolingCFlags,
287 flags.conlyFlags,
288 }, " ")
289
290 cflags := strings.Join([]string{
291 commonFlags,
Colin Crossc3199482017-03-30 15:03:04 -0700292 flags.cFlags,
293 flags.conlyFlags,
294 }, " ")
295
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700296 toolingCppflags := strings.Join([]string{
297 commonFlags,
298 flags.toolingCFlags,
299 flags.cppFlags,
300 }, " ")
301
Colin Crossc3199482017-03-30 15:03:04 -0700302 cppflags := strings.Join([]string{
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700303 commonFlags,
Colin Crossc3199482017-03-30 15:03:04 -0700304 flags.cFlags,
305 flags.cppFlags,
306 }, " ")
307
308 asflags := strings.Join([]string{
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700309 commonFlags,
Colin Crossc3199482017-03-30 15:03:04 -0700310 flags.asFlags,
311 }, " ")
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700312
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800313 var sAbiDumpFiles android.Paths
314 if flags.sAbiDump && flags.clang {
315 sAbiDumpFiles = make(android.Paths, 0, len(srcFiles))
316 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800317
Dan Willemsenbe03f342016-03-03 17:21:04 -0800318 if flags.clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700319 cflags += " ${config.NoOverrideClangGlobalCflags}"
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700320 toolingCflags += " ${config.NoOverrideClangGlobalCflags}"
Colin Crossb98c8b02016-07-29 13:44:28 -0700321 cppflags += " ${config.NoOverrideClangGlobalCflags}"
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700322 toolingCppflags += " ${config.NoOverrideClangGlobalCflags}"
Dan Willemsenbe03f342016-03-03 17:21:04 -0800323 } else {
Colin Crossb98c8b02016-07-29 13:44:28 -0700324 cflags += " ${config.NoOverrideGlobalCflags}"
325 cppflags += " ${config.NoOverrideGlobalCflags}"
Dan Willemsenbe03f342016-03-03 17:21:04 -0800326 }
327
Colin Cross3f40fa42015-01-30 17:27:36 -0800328 for i, srcFile := range srcFiles {
Dan Willemsen21ec4902016-11-02 20:43:13 -0700329 objFile := android.ObjPathWithExt(ctx, subdir, srcFile, "o")
Colin Cross3f40fa42015-01-30 17:27:36 -0800330
331 objFiles[i] = objFile
332
Colin Cross91e90042016-12-02 17:13:24 -0800333 if srcFile.Ext() == ".asm" {
334 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700335 Rule: yasm,
336 Description: "yasm " + srcFile.Rel(),
337 Output: objFile,
338 Input: srcFile,
339 OrderOnly: deps,
Colin Cross91e90042016-12-02 17:13:24 -0800340 Args: map[string]string{
341 "asFlags": flags.yasmFlags,
342 },
343 })
344 continue
345 }
346
Colin Cross3f40fa42015-01-30 17:27:36 -0800347 var moduleCflags string
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700348 var moduleToolingCflags string
Colin Cross3f40fa42015-01-30 17:27:36 -0800349 var ccCmd string
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700350 tidy := flags.tidy && flags.clang
Dan Willemsen581341d2017-02-09 16:16:31 -0800351 coverage := flags.coverage
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800352 dump := flags.sAbiDump && flags.clang
Colin Cross3f40fa42015-01-30 17:27:36 -0800353
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700354 switch srcFile.Ext() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800355 case ".S", ".s":
356 ccCmd = "gcc"
357 moduleCflags = asflags
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700358 tidy = false
Dan Willemsen581341d2017-02-09 16:16:31 -0800359 coverage = false
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800360 dump = false
Colin Cross3f40fa42015-01-30 17:27:36 -0800361 case ".c":
362 ccCmd = "gcc"
363 moduleCflags = cflags
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700364 moduleToolingCflags = toolingCflags
Colin Cross9978ffe2016-12-01 15:31:22 -0800365 case ".cpp", ".cc", ".mm":
Colin Cross3f40fa42015-01-30 17:27:36 -0800366 ccCmd = "g++"
367 moduleCflags = cppflags
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700368 moduleToolingCflags = toolingCppflags
Colin Cross3f40fa42015-01-30 17:27:36 -0800369 default:
370 ctx.ModuleErrorf("File %s has unknown extension", srcFile)
371 continue
372 }
373
374 if flags.clang {
375 switch ccCmd {
376 case "gcc":
377 ccCmd = "clang"
378 case "g++":
379 ccCmd = "clang++"
380 default:
381 panic("unrecoginzied ccCmd")
382 }
Colin Cross67a5c132017-05-09 13:45:28 -0700383 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800384
Colin Cross67a5c132017-05-09 13:45:28 -0700385 ccDesc := ccCmd
386
387 if flags.clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700388 ccCmd = "${config.ClangBin}/" + ccCmd
Colin Cross3f40fa42015-01-30 17:27:36 -0800389 } else {
390 ccCmd = gccCmd(flags.toolchain, ccCmd)
391 }
392
Dan Willemsen581341d2017-02-09 16:16:31 -0800393 var implicitOutputs android.WritablePaths
394 if coverage {
395 gcnoFile := android.ObjPathWithExt(ctx, subdir, srcFile, "gcno")
396 implicitOutputs = append(implicitOutputs, gcnoFile)
397 coverageFiles = append(coverageFiles, gcnoFile)
398 }
399
Colin Cross635c3b02016-05-18 15:37:25 -0700400 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Dan Willemsen581341d2017-02-09 16:16:31 -0800401 Rule: cc,
Colin Cross67a5c132017-05-09 13:45:28 -0700402 Description: ccDesc + " " + srcFile.Rel(),
Dan Willemsen581341d2017-02-09 16:16:31 -0800403 Output: objFile,
404 ImplicitOutputs: implicitOutputs,
405 Input: srcFile,
406 OrderOnly: deps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800407 Args: map[string]string{
Colin Cross28344522015-04-22 13:07:53 -0700408 "cFlags": moduleCflags,
409 "ccCmd": ccCmd,
Colin Cross3f40fa42015-01-30 17:27:36 -0800410 },
411 })
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700412
413 if tidy {
Dan Willemsen21ec4902016-11-02 20:43:13 -0700414 tidyFile := android.ObjPathWithExt(ctx, subdir, srcFile, "tidy")
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700415 tidyFiles = append(tidyFiles, tidyFile)
416
417 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700418 Rule: clangTidy,
419 Description: "clang-tidy " + srcFile.Rel(),
420 Output: tidyFile,
421 Input: srcFile,
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700422 // We must depend on objFile, since clang-tidy doesn't
423 // support exporting dependencies.
424 Implicit: objFile,
425 Args: map[string]string{
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700426 "cFlags": moduleToolingCflags,
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700427 "tidyFlags": flags.tidyFlags,
428 },
429 })
430 }
431
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800432 if dump {
433 sAbiDumpFile := android.ObjPathWithExt(ctx, subdir, srcFile, "sdump")
434 sAbiDumpFiles = append(sAbiDumpFiles, sAbiDumpFile)
435
436 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700437 Rule: sAbiDump,
438 Description: "header-abi-dumper " + srcFile.Rel(),
439 Output: sAbiDumpFile,
440 Input: srcFile,
441 Implicit: objFile,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800442 Args: map[string]string{
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700443 "cFlags": moduleToolingCflags,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800444 "exportDirs": flags.sAbiFlags,
445 },
446 })
447 }
448
Colin Cross3f40fa42015-01-30 17:27:36 -0800449 }
450
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700451 return Objects{
Dan Willemsen581341d2017-02-09 16:16:31 -0800452 objFiles: objFiles,
453 tidyFiles: tidyFiles,
454 coverageFiles: coverageFiles,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800455 sAbiDumpFiles: sAbiDumpFiles,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700456 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800457}
458
459// Generate a rule for compiling multiple .o files to a static library (.a)
Colin Cross635c3b02016-05-18 15:37:25 -0700460func TransformObjToStaticLib(ctx android.ModuleContext, objFiles android.Paths,
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700461 flags builderFlags, outputFile android.ModuleOutPath, deps android.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800462
Dan Willemsen581341d2017-02-09 16:16:31 -0800463 if ctx.Darwin() {
464 transformDarwinObjToStaticLib(ctx, objFiles, flags, outputFile, deps)
465 return
466 }
467
Colin Cross3f40fa42015-01-30 17:27:36 -0800468 arCmd := gccCmd(flags.toolchain, "ar")
469 arFlags := "crsPD"
Vishwath Mohan83d9f712017-03-16 11:01:23 -0700470 if flags.arFlags != "" {
471 arFlags += " " + flags.arFlags
472 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800473
Colin Cross635c3b02016-05-18 15:37:25 -0700474 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700475 Rule: ar,
476 Description: "static link " + outputFile.Base(),
477 Output: outputFile,
478 Inputs: objFiles,
479 Implicits: deps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800480 Args: map[string]string{
481 "arFlags": arFlags,
482 "arCmd": arCmd,
483 },
484 })
485}
486
Colin Cross0af4b842015-04-30 16:36:18 -0700487// Generate a rule for compiling multiple .o files to a static library (.a) on
488// darwin. The darwin ar tool doesn't support @file for list files, and has a
489// very small command line length limit, so we have to split the ar into multiple
490// steps, each appending to the previous one.
Dan Willemsen581341d2017-02-09 16:16:31 -0800491func transformDarwinObjToStaticLib(ctx android.ModuleContext, objFiles android.Paths,
Colin Cross5b529592017-05-09 13:34:34 -0700492 flags builderFlags, outputFile android.ModuleOutPath, deps android.Paths) {
Colin Cross0af4b842015-04-30 16:36:18 -0700493
Colin Cross0af4b842015-04-30 16:36:18 -0700494 arFlags := "cqs"
495
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700496 if len(objFiles) == 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700497 dummy := android.PathForModuleOut(ctx, "dummy"+objectExtension)
498 dummyAr := android.PathForModuleOut(ctx, "dummy"+staticLibraryExtension)
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700499
Colin Cross635c3b02016-05-18 15:37:25 -0700500 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700501 Rule: emptyFile,
502 Description: "empty object file",
503 Output: dummy,
504 Implicits: deps,
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700505 })
506
Colin Cross635c3b02016-05-18 15:37:25 -0700507 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700508 Rule: darwinAr,
509 Description: "empty static archive",
510 Output: dummyAr,
511 Input: dummy,
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700512 Args: map[string]string{
513 "arFlags": arFlags,
514 },
515 })
516
Colin Cross635c3b02016-05-18 15:37:25 -0700517 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700518 Rule: darwinAppendAr,
519 Description: "static link " + outputFile.Base(),
520 Output: outputFile,
521 Input: dummy,
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700522 Args: map[string]string{
523 "arFlags": "d",
524 "inAr": dummyAr.String(),
525 },
526 })
527
528 return
529 }
530
Colin Cross0af4b842015-04-30 16:36:18 -0700531 // ARG_MAX on darwin is 262144, use half that to be safe
Colin Cross5b529592017-05-09 13:34:34 -0700532 objFilesLists, err := splitListForSize(objFiles, 131072)
Colin Cross0af4b842015-04-30 16:36:18 -0700533 if err != nil {
534 ctx.ModuleErrorf("%s", err.Error())
535 }
536
Colin Cross5b529592017-05-09 13:34:34 -0700537 var in, out android.WritablePath
Colin Cross0af4b842015-04-30 16:36:18 -0700538 for i, l := range objFilesLists {
539 in = out
540 out = outputFile
541 if i != len(objFilesLists)-1 {
Colin Cross5b529592017-05-09 13:34:34 -0700542 out = android.PathForModuleOut(ctx, outputFile.Base()+strconv.Itoa(i))
Colin Cross0af4b842015-04-30 16:36:18 -0700543 }
544
Colin Cross5b529592017-05-09 13:34:34 -0700545 build := android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700546 Rule: darwinAr,
547 Description: "static link " + out.Base(),
548 Output: out,
549 Inputs: l,
550 Implicits: deps,
Colin Cross5b529592017-05-09 13:34:34 -0700551 Args: map[string]string{
552 "arFlags": arFlags,
553 },
Colin Cross0af4b842015-04-30 16:36:18 -0700554 }
Colin Cross5b529592017-05-09 13:34:34 -0700555 if i != 0 {
556 build.Rule = darwinAppendAr
557 build.Args["inAr"] = in.String()
558 }
559 ctx.ModuleBuild(pctx, build)
Colin Cross0af4b842015-04-30 16:36:18 -0700560 }
561}
562
Colin Cross3f40fa42015-01-30 17:27:36 -0800563// Generate a rule for compiling multiple .o files, plus static libraries, whole static libraries,
564// and shared libraires, to a shared library (.so) or dynamic executable
Colin Cross635c3b02016-05-18 15:37:25 -0700565func TransformObjToDynamicBinary(ctx android.ModuleContext,
566 objFiles, sharedLibs, staticLibs, lateStaticLibs, wholeStaticLibs, deps android.Paths,
567 crtBegin, crtEnd android.OptionalPath, groupLate bool, flags builderFlags, outputFile android.WritablePath) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800568
569 var ldCmd string
570 if flags.clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700571 ldCmd = "${config.ClangBin}/clang++"
Colin Cross3f40fa42015-01-30 17:27:36 -0800572 } else {
573 ldCmd = gccCmd(flags.toolchain, "g++")
574 }
575
Colin Cross3f40fa42015-01-30 17:27:36 -0800576 var libFlagsList []string
577
Colin Cross16b23492016-01-06 14:41:07 -0800578 if len(flags.libFlags) > 0 {
579 libFlagsList = append(libFlagsList, flags.libFlags)
580 }
581
Colin Cross3f40fa42015-01-30 17:27:36 -0800582 if len(wholeStaticLibs) > 0 {
Dan Willemsen490fd492015-11-24 17:53:15 -0800583 if ctx.Host() && ctx.Darwin() {
Colin Cross635c3b02016-05-18 15:37:25 -0700584 libFlagsList = append(libFlagsList, android.JoinWithPrefix(wholeStaticLibs.Strings(), "-force_load "))
Colin Cross0af4b842015-04-30 16:36:18 -0700585 } else {
586 libFlagsList = append(libFlagsList, "-Wl,--whole-archive ")
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700587 libFlagsList = append(libFlagsList, wholeStaticLibs.Strings()...)
Colin Cross0af4b842015-04-30 16:36:18 -0700588 libFlagsList = append(libFlagsList, "-Wl,--no-whole-archive ")
589 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800590 }
591
Colin Cross7a7cf972016-12-05 18:47:39 -0800592 if flags.groupStaticLibs && !ctx.Darwin() && len(staticLibs) > 0 {
Colin Cross18c0c5a2016-12-01 14:45:23 -0800593 libFlagsList = append(libFlagsList, "-Wl,--start-group")
594 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700595 libFlagsList = append(libFlagsList, staticLibs.Strings()...)
Colin Cross7a7cf972016-12-05 18:47:39 -0800596 if flags.groupStaticLibs && !ctx.Darwin() && len(staticLibs) > 0 {
Colin Cross18c0c5a2016-12-01 14:45:23 -0800597 libFlagsList = append(libFlagsList, "-Wl,--end-group")
598 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800599
Stephen Hines10347862016-07-18 15:54:54 -0700600 if groupLate && !ctx.Darwin() && len(lateStaticLibs) > 0 {
Dan Willemsenedc385f2015-07-08 13:02:23 -0700601 libFlagsList = append(libFlagsList, "-Wl,--start-group")
602 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700603 libFlagsList = append(libFlagsList, lateStaticLibs.Strings()...)
Stephen Hines10347862016-07-18 15:54:54 -0700604 if groupLate && !ctx.Darwin() && len(lateStaticLibs) > 0 {
Dan Willemsenedc385f2015-07-08 13:02:23 -0700605 libFlagsList = append(libFlagsList, "-Wl,--end-group")
606 }
607
Colin Cross3f40fa42015-01-30 17:27:36 -0800608 for _, lib := range sharedLibs {
Dan Albert9840e1b2016-07-21 08:47:33 -0700609 libFlagsList = append(libFlagsList, lib.String())
Colin Cross3f40fa42015-01-30 17:27:36 -0800610 }
611
Colin Cross3f40fa42015-01-30 17:27:36 -0800612 deps = append(deps, staticLibs...)
Colin Cross3075ad02015-03-17 10:47:08 -0700613 deps = append(deps, lateStaticLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800614 deps = append(deps, wholeStaticLibs...)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700615 if crtBegin.Valid() {
616 deps = append(deps, crtBegin.Path(), crtEnd.Path())
Colin Cross3f40fa42015-01-30 17:27:36 -0800617 }
618
Colin Cross635c3b02016-05-18 15:37:25 -0700619 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700620 Rule: ld,
621 Description: "link " + outputFile.Base(),
622 Output: outputFile,
623 Inputs: objFiles,
624 Implicits: deps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800625 Args: map[string]string{
Dan Albertce2b8392016-07-21 13:16:49 -0700626 "ldCmd": ldCmd,
627 "crtBegin": crtBegin.String(),
628 "libFlags": strings.Join(libFlagsList, " "),
629 "ldFlags": flags.ldFlags,
630 "crtEnd": crtEnd.String(),
Colin Cross3f40fa42015-01-30 17:27:36 -0800631 },
632 })
633}
634
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800635// Generate a rule to combine .dump sAbi dump files from multiple source files
636// into a single .ldump sAbi dump file
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700637func TransformDumpToLinkedDump(ctx android.ModuleContext, sAbiDumps android.Paths, soFile android.Path,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800638 symbolFile android.OptionalPath, apiLevel, baseName, exportedHeaderFlags string) android.OptionalPath {
639 outputFile := android.PathForModuleOut(ctx, baseName+".lsdump")
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700640 var symbolFilterStr string
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800641 var linkedDumpDep android.Path
642 if symbolFile.Valid() {
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700643 symbolFilterStr = "-v " + symbolFile.Path().String()
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800644 linkedDumpDep = symbolFile.Path()
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700645 } else {
646 linkedDumpDep = soFile
647 symbolFilterStr = "-so " + soFile.String()
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800648 }
649 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700650 Rule: sAbiLink,
651 Description: "header-abi-linker " + outputFile.Base(),
652 Output: outputFile,
653 Inputs: sAbiDumps,
654 Implicit: linkedDumpDep,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800655 Args: map[string]string{
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700656 "symbolFilter": symbolFilterStr,
657 "arch": ctx.Arch().ArchType.Name,
658 "api": apiLevel,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800659 "exportedHeaderFlags": exportedHeaderFlags,
660 },
661 })
662 return android.OptionalPathForPath(outputFile)
663}
664
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700665func UnzipRefDump(ctx android.ModuleContext, zippedRefDump android.Path, baseName string) android.Path {
666 outputFile := android.PathForModuleOut(ctx, baseName+"_ref.lsdump")
667 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
668 Rule: unzipRefSAbiDump,
669 Description: "gunzip" + outputFile.Base(),
670 Output: outputFile,
671 Input: zippedRefDump,
672 })
673 return outputFile
674}
675
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800676func SourceAbiDiff(ctx android.ModuleContext, inputDump android.Path, referenceDump android.Path,
677 baseName string) android.OptionalPath {
678 outputFile := android.PathForModuleOut(ctx, baseName+".abidiff")
679 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700680 Rule: sAbiDiff,
681 Description: "header-abi-diff " + outputFile.Base(),
682 Output: outputFile,
683 Input: inputDump,
684 Implicit: referenceDump,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800685 Args: map[string]string{
686 "referenceDump": referenceDump.String(),
Jayant Chowdharyf54e0a72017-05-22 10:53:24 -0700687 "libName": baseName,
688 "arch": ctx.Arch().ArchType.Name,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800689 },
690 })
691 return android.OptionalPathForPath(outputFile)
692}
693
Colin Cross26c34ed2016-09-30 17:10:16 -0700694// Generate a rule for extract a table of contents from a shared library (.so)
695func TransformSharedObjectToToc(ctx android.ModuleContext, inputFile android.WritablePath,
696 outputFile android.WritablePath, flags builderFlags) {
697
698 crossCompile := gccCmd(flags.toolchain, "")
699
700 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700701 Rule: toc,
702 Description: "generate toc " + inputFile.Base(),
703 Output: outputFile,
704 Input: inputFile,
Colin Cross26c34ed2016-09-30 17:10:16 -0700705 Args: map[string]string{
706 "crossCompile": crossCompile,
707 },
708 })
709}
710
Colin Cross3f40fa42015-01-30 17:27:36 -0800711// Generate a rule for compiling multiple .o files to a .o using ld partial linking
Colin Cross635c3b02016-05-18 15:37:25 -0700712func TransformObjsToObj(ctx android.ModuleContext, objFiles android.Paths,
713 flags builderFlags, outputFile android.WritablePath) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800714
Colin Cross41280a42015-11-23 14:01:42 -0800715 var ldCmd string
716 if flags.clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700717 ldCmd = "${config.ClangBin}/clang++"
Colin Cross41280a42015-11-23 14:01:42 -0800718 } else {
719 ldCmd = gccCmd(flags.toolchain, "g++")
720 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800721
Colin Cross635c3b02016-05-18 15:37:25 -0700722 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700723 Rule: partialLd,
724 Description: "link " + outputFile.Base(),
725 Output: outputFile,
726 Inputs: objFiles,
Colin Cross3f40fa42015-01-30 17:27:36 -0800727 Args: map[string]string{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700728 "ldCmd": ldCmd,
Colin Cross41280a42015-11-23 14:01:42 -0800729 "ldFlags": flags.ldFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800730 },
731 })
732}
733
Colin Crossbfae8852015-03-26 14:44:11 -0700734// Generate a rule for runing objcopy --prefix-symbols on a binary
Colin Cross635c3b02016-05-18 15:37:25 -0700735func TransformBinaryPrefixSymbols(ctx android.ModuleContext, prefix string, inputFile android.Path,
736 flags builderFlags, outputFile android.WritablePath) {
Colin Crossbfae8852015-03-26 14:44:11 -0700737
738 objcopyCmd := gccCmd(flags.toolchain, "objcopy")
739
Colin Cross635c3b02016-05-18 15:37:25 -0700740 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700741 Rule: prefixSymbols,
742 Description: "prefix symbols " + outputFile.Base(),
743 Output: outputFile,
744 Input: inputFile,
Colin Crossbfae8852015-03-26 14:44:11 -0700745 Args: map[string]string{
746 "objcopyCmd": objcopyCmd,
747 "prefix": prefix,
748 },
749 })
750}
751
Colin Cross635c3b02016-05-18 15:37:25 -0700752func TransformStrip(ctx android.ModuleContext, inputFile android.Path,
753 outputFile android.WritablePath, flags builderFlags) {
Colin Cross665dce92016-04-28 14:50:03 -0700754
755 crossCompile := gccCmd(flags.toolchain, "")
756 args := ""
757 if flags.stripAddGnuDebuglink {
758 args += " --add-gnu-debuglink"
759 }
760 if flags.stripKeepMiniDebugInfo {
761 args += " --keep-mini-debug-info"
762 }
763 if flags.stripKeepSymbols {
764 args += " --keep-symbols"
765 }
766
Colin Cross635c3b02016-05-18 15:37:25 -0700767 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700768 Rule: strip,
769 Description: "strip " + outputFile.Base(),
770 Output: outputFile,
771 Input: inputFile,
Colin Cross665dce92016-04-28 14:50:03 -0700772 Args: map[string]string{
773 "crossCompile": crossCompile,
774 "args": args,
775 },
776 })
777}
778
Colin Cross635c3b02016-05-18 15:37:25 -0700779func TransformDarwinStrip(ctx android.ModuleContext, inputFile android.Path,
780 outputFile android.WritablePath) {
Colin Crossb8ecdfe2016-05-03 15:10:29 -0700781
Colin Cross635c3b02016-05-18 15:37:25 -0700782 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700783 Rule: darwinStrip,
784 Description: "strip " + outputFile.Base(),
785 Output: outputFile,
786 Input: inputFile,
Colin Crossb8ecdfe2016-05-03 15:10:29 -0700787 })
788}
789
Dan Willemsen581341d2017-02-09 16:16:31 -0800790func TransformCoverageFilesToLib(ctx android.ModuleContext,
791 inputs Objects, flags builderFlags, baseName string) android.OptionalPath {
792
793 if len(inputs.coverageFiles) > 0 {
794 outputFile := android.PathForModuleOut(ctx, baseName+".gcnodir")
795
796 TransformObjToStaticLib(ctx, inputs.coverageFiles, flags, outputFile, nil)
797
798 return android.OptionalPathForPath(outputFile)
799 }
800
801 return android.OptionalPath{}
802}
803
Colin Cross635c3b02016-05-18 15:37:25 -0700804func CopyGccLib(ctx android.ModuleContext, libName string,
805 flags builderFlags, outputFile android.WritablePath) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800806
Colin Cross635c3b02016-05-18 15:37:25 -0700807 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700808 Rule: copyGccLib,
809 Description: "copy gcc library " + libName,
810 Output: outputFile,
Colin Cross3f40fa42015-01-30 17:27:36 -0800811 Args: map[string]string{
812 "ccCmd": gccCmd(flags.toolchain, "gcc"),
813 "cFlags": flags.globalFlags,
814 "libName": libName,
815 },
816 })
817}
818
Colin Crossb98c8b02016-07-29 13:44:28 -0700819func gccCmd(toolchain config.Toolchain, cmd string) string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800820 return filepath.Join(toolchain.GccRoot(), "bin", toolchain.GccTriple()+"-"+cmd)
821}
Colin Cross0af4b842015-04-30 16:36:18 -0700822
Colin Cross5b529592017-05-09 13:34:34 -0700823func splitListForSize(list android.Paths, limit int) (lists []android.Paths, err error) {
Colin Cross0af4b842015-04-30 16:36:18 -0700824 var i int
825
826 start := 0
827 bytes := 0
828 for i = range list {
Colin Cross5b529592017-05-09 13:34:34 -0700829 l := len(list[i].String())
Colin Cross0af4b842015-04-30 16:36:18 -0700830 if l > limit {
831 return nil, fmt.Errorf("list element greater than size limit (%d)", limit)
832 }
833 if bytes+l > limit {
834 lists = append(lists, list[start:i])
835 start = i
836 bytes = 0
837 }
838 bytes += l + 1 // count a space between each list element
839 }
840
841 lists = append(lists, list[start:])
842
843 totalLen := 0
844 for _, l := range lists {
845 totalLen += len(l)
846 }
847 if totalLen != len(list) {
848 panic(fmt.Errorf("Failed breaking up list, %d != %d", len(list), totalLen))
849 }
850 return lists, nil
851}