blob: b23755136c144d06877171f98e5dd023356b3d40 [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{
Dan Willemsen1d3e5452017-08-22 20:53:45 -0700154 Command: "$yasmCmd $asFlags -o $out $in && $yasmCmd $asFlags -M $in >$out.d",
Colin Cross91e90042016-12-02 17:13:24 -0800155 CommandDeps: []string{"$yasmCmd"},
Dan Willemsen1d3e5452017-08-22 20:53:45 -0700156 Depfile: "$out.d",
157 Deps: blueprint.DepsGCC,
Colin Cross91e90042016-12-02 17:13:24 -0800158 },
159 "asFlags")
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800160
161 _ = pctx.SourcePathVariable("sAbiDumper", "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/header-abi-dumper")
162
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700163 // -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 -0800164 sAbiDump = pctx.AndroidStaticRule("sAbiDump",
165 blueprint.RuleParams{
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700166 Command: "rm -f $out && $sAbiDumper -o ${out} $in $exportDirs -- $cFlags -w -isystem ${config.RSIncludePath}",
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800167 CommandDeps: []string{"$sAbiDumper"},
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800168 },
169 "cFlags", "exportDirs")
170
171 _ = pctx.SourcePathVariable("sAbiLinker", "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/header-abi-linker")
172
173 sAbiLink = pctx.AndroidStaticRule("sAbiLink",
174 blueprint.RuleParams{
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700175 Command: "$sAbiLinker -o ${out} $symbolFilter -arch $arch -api $api $exportedHeaderFlags @${out}.rsp ",
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800176 CommandDeps: []string{"$sAbiLinker"},
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800177 Rspfile: "${out}.rsp",
178 RspfileContent: "${in}",
179 },
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700180 "symbolFilter", "arch", "api", "exportedHeaderFlags")
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800181
182 _ = pctx.SourcePathVariable("sAbiDiffer", "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/header-abi-diff")
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700183
Jayant Chowdhary918b1d92017-04-18 10:44:00 -0700184 // Abidiff check turned on in advice-only mode. Builds will not fail on abi incompatibilties / extensions.
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800185 sAbiDiff = pctx.AndroidStaticRule("sAbiDiff",
186 blueprint.RuleParams{
Jayant Chowdharyf54e0a72017-05-22 10:53:24 -0700187 Command: "$sAbiDiffer -lib $libName -arch $arch -advice-only -o ${out} -new $in -old $referenceDump",
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800188 CommandDeps: []string{"$sAbiDiffer"},
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800189 },
Jayant Chowdharyf54e0a72017-05-22 10:53:24 -0700190 "referenceDump", "libName", "arch")
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700191
192 unzipRefSAbiDump = pctx.AndroidStaticRule("unzipRefSAbiDump",
193 blueprint.RuleParams{
194 Command: "gunzip -c $in > $out",
195 })
Colin Cross3f40fa42015-01-30 17:27:36 -0800196)
197
Dan Willemsen322a0a62015-11-17 15:19:46 -0800198func init() {
199 // We run gcc/clang with PWD=/proc/self/cwd to remove $TOP from the
200 // debug output. That way two builds in two different directories will
201 // create the same output.
202 if runtime.GOOS != "darwin" {
203 pctx.StaticVariable("relPwd", "PWD=/proc/self/cwd")
204 } else {
205 // Darwin doesn't have /proc
206 pctx.StaticVariable("relPwd", "")
207 }
208}
209
Colin Cross3f40fa42015-01-30 17:27:36 -0800210type builderFlags struct {
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700211 globalFlags string
212 arFlags string
213 asFlags string
214 cFlags string
215 toolingCFlags string // Seperate set of Cflags for clang LibTooling tools
216 conlyFlags string
217 cppFlags string
218 ldFlags string
219 libFlags string
220 yaccFlags string
221 protoFlags string
222 tidyFlags string
223 sAbiFlags string
224 yasmFlags string
225 aidlFlags string
226 rsFlags string
227 toolchain config.Toolchain
228 clang bool
229 tidy bool
230 coverage bool
231 sAbiDump bool
Colin Cross665dce92016-04-28 14:50:03 -0700232
Colin Crossc3199482017-03-30 15:03:04 -0700233 systemIncludeFlags string
234
Colin Cross18c0c5a2016-12-01 14:45:23 -0800235 groupStaticLibs bool
236
Colin Cross665dce92016-04-28 14:50:03 -0700237 stripKeepSymbols bool
238 stripKeepMiniDebugInfo bool
239 stripAddGnuDebuglink bool
Colin Cross3f40fa42015-01-30 17:27:36 -0800240}
241
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700242type Objects struct {
Dan Willemsen581341d2017-02-09 16:16:31 -0800243 objFiles android.Paths
244 tidyFiles android.Paths
245 coverageFiles android.Paths
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800246 sAbiDumpFiles android.Paths
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700247}
248
249func (a Objects) Copy() Objects {
250 return Objects{
Dan Willemsen581341d2017-02-09 16:16:31 -0800251 objFiles: append(android.Paths{}, a.objFiles...),
252 tidyFiles: append(android.Paths{}, a.tidyFiles...),
253 coverageFiles: append(android.Paths{}, a.coverageFiles...),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800254 sAbiDumpFiles: append(android.Paths{}, a.sAbiDumpFiles...),
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700255 }
256}
257
258func (a Objects) Append(b Objects) Objects {
259 return Objects{
Dan Willemsen581341d2017-02-09 16:16:31 -0800260 objFiles: append(a.objFiles, b.objFiles...),
261 tidyFiles: append(a.tidyFiles, b.tidyFiles...),
262 coverageFiles: append(a.coverageFiles, b.coverageFiles...),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800263 sAbiDumpFiles: append(a.sAbiDumpFiles, b.sAbiDumpFiles...),
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700264 }
265}
266
Colin Cross3f40fa42015-01-30 17:27:36 -0800267// Generate rules for compiling multiple .c, .cpp, or .S files to individual .o files
Colin Cross635c3b02016-05-18 15:37:25 -0700268func TransformSourceToObj(ctx android.ModuleContext, subdir string, srcFiles android.Paths,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700269 flags builderFlags, deps android.Paths) Objects {
Colin Cross581c1892015-04-07 16:50:10 -0700270
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700271 objFiles := make(android.Paths, len(srcFiles))
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700272 var tidyFiles android.Paths
273 if flags.tidy && flags.clang {
274 tidyFiles = make(android.Paths, 0, len(srcFiles))
275 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800276 var coverageFiles android.Paths
277 if flags.coverage {
278 coverageFiles = make(android.Paths, 0, len(srcFiles))
279 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800280
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700281 commonFlags := strings.Join([]string{
Colin Crossc3199482017-03-30 15:03:04 -0700282 flags.globalFlags,
283 flags.systemIncludeFlags,
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700284 }, " ")
285
286 toolingCflags := strings.Join([]string{
287 commonFlags,
288 flags.toolingCFlags,
289 flags.conlyFlags,
290 }, " ")
291
292 cflags := strings.Join([]string{
293 commonFlags,
Colin Crossc3199482017-03-30 15:03:04 -0700294 flags.cFlags,
295 flags.conlyFlags,
296 }, " ")
297
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700298 toolingCppflags := strings.Join([]string{
299 commonFlags,
300 flags.toolingCFlags,
301 flags.cppFlags,
302 }, " ")
303
Colin Crossc3199482017-03-30 15:03:04 -0700304 cppflags := strings.Join([]string{
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700305 commonFlags,
Colin Crossc3199482017-03-30 15:03:04 -0700306 flags.cFlags,
307 flags.cppFlags,
308 }, " ")
309
310 asflags := strings.Join([]string{
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700311 commonFlags,
Colin Crossc3199482017-03-30 15:03:04 -0700312 flags.asFlags,
313 }, " ")
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700314
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800315 var sAbiDumpFiles android.Paths
316 if flags.sAbiDump && flags.clang {
317 sAbiDumpFiles = make(android.Paths, 0, len(srcFiles))
318 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800319
Dan Willemsenbe03f342016-03-03 17:21:04 -0800320 if flags.clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700321 cflags += " ${config.NoOverrideClangGlobalCflags}"
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700322 toolingCflags += " ${config.NoOverrideClangGlobalCflags}"
Colin Crossb98c8b02016-07-29 13:44:28 -0700323 cppflags += " ${config.NoOverrideClangGlobalCflags}"
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700324 toolingCppflags += " ${config.NoOverrideClangGlobalCflags}"
Dan Willemsenbe03f342016-03-03 17:21:04 -0800325 } else {
Colin Crossb98c8b02016-07-29 13:44:28 -0700326 cflags += " ${config.NoOverrideGlobalCflags}"
327 cppflags += " ${config.NoOverrideGlobalCflags}"
Dan Willemsenbe03f342016-03-03 17:21:04 -0800328 }
329
Colin Cross3f40fa42015-01-30 17:27:36 -0800330 for i, srcFile := range srcFiles {
Dan Willemsen21ec4902016-11-02 20:43:13 -0700331 objFile := android.ObjPathWithExt(ctx, subdir, srcFile, "o")
Colin Cross3f40fa42015-01-30 17:27:36 -0800332
333 objFiles[i] = objFile
334
Colin Cross91e90042016-12-02 17:13:24 -0800335 if srcFile.Ext() == ".asm" {
336 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700337 Rule: yasm,
338 Description: "yasm " + srcFile.Rel(),
339 Output: objFile,
340 Input: srcFile,
341 OrderOnly: deps,
Colin Cross91e90042016-12-02 17:13:24 -0800342 Args: map[string]string{
343 "asFlags": flags.yasmFlags,
344 },
345 })
346 continue
347 }
348
Colin Cross3f40fa42015-01-30 17:27:36 -0800349 var moduleCflags string
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700350 var moduleToolingCflags string
Colin Cross3f40fa42015-01-30 17:27:36 -0800351 var ccCmd string
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700352 tidy := flags.tidy && flags.clang
Dan Willemsen581341d2017-02-09 16:16:31 -0800353 coverage := flags.coverage
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800354 dump := flags.sAbiDump && flags.clang
Colin Cross3f40fa42015-01-30 17:27:36 -0800355
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700356 switch srcFile.Ext() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800357 case ".S", ".s":
358 ccCmd = "gcc"
359 moduleCflags = asflags
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700360 tidy = false
Dan Willemsen581341d2017-02-09 16:16:31 -0800361 coverage = false
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800362 dump = false
Colin Cross3f40fa42015-01-30 17:27:36 -0800363 case ".c":
364 ccCmd = "gcc"
365 moduleCflags = cflags
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700366 moduleToolingCflags = toolingCflags
Colin Cross9978ffe2016-12-01 15:31:22 -0800367 case ".cpp", ".cc", ".mm":
Colin Cross3f40fa42015-01-30 17:27:36 -0800368 ccCmd = "g++"
369 moduleCflags = cppflags
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700370 moduleToolingCflags = toolingCppflags
Colin Cross3f40fa42015-01-30 17:27:36 -0800371 default:
372 ctx.ModuleErrorf("File %s has unknown extension", srcFile)
373 continue
374 }
375
376 if flags.clang {
377 switch ccCmd {
378 case "gcc":
379 ccCmd = "clang"
380 case "g++":
381 ccCmd = "clang++"
382 default:
383 panic("unrecoginzied ccCmd")
384 }
Colin Cross67a5c132017-05-09 13:45:28 -0700385 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800386
Colin Cross67a5c132017-05-09 13:45:28 -0700387 ccDesc := ccCmd
388
389 if flags.clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700390 ccCmd = "${config.ClangBin}/" + ccCmd
Colin Cross3f40fa42015-01-30 17:27:36 -0800391 } else {
392 ccCmd = gccCmd(flags.toolchain, ccCmd)
393 }
394
Dan Willemsen581341d2017-02-09 16:16:31 -0800395 var implicitOutputs android.WritablePaths
396 if coverage {
397 gcnoFile := android.ObjPathWithExt(ctx, subdir, srcFile, "gcno")
398 implicitOutputs = append(implicitOutputs, gcnoFile)
399 coverageFiles = append(coverageFiles, gcnoFile)
400 }
401
Colin Cross635c3b02016-05-18 15:37:25 -0700402 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Dan Willemsen581341d2017-02-09 16:16:31 -0800403 Rule: cc,
Colin Cross67a5c132017-05-09 13:45:28 -0700404 Description: ccDesc + " " + srcFile.Rel(),
Dan Willemsen581341d2017-02-09 16:16:31 -0800405 Output: objFile,
406 ImplicitOutputs: implicitOutputs,
407 Input: srcFile,
408 OrderOnly: deps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800409 Args: map[string]string{
Colin Cross28344522015-04-22 13:07:53 -0700410 "cFlags": moduleCflags,
411 "ccCmd": ccCmd,
Colin Cross3f40fa42015-01-30 17:27:36 -0800412 },
413 })
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700414
415 if tidy {
Dan Willemsen21ec4902016-11-02 20:43:13 -0700416 tidyFile := android.ObjPathWithExt(ctx, subdir, srcFile, "tidy")
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700417 tidyFiles = append(tidyFiles, tidyFile)
418
419 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700420 Rule: clangTidy,
421 Description: "clang-tidy " + srcFile.Rel(),
422 Output: tidyFile,
423 Input: srcFile,
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700424 // We must depend on objFile, since clang-tidy doesn't
425 // support exporting dependencies.
426 Implicit: objFile,
427 Args: map[string]string{
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700428 "cFlags": moduleToolingCflags,
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700429 "tidyFlags": flags.tidyFlags,
430 },
431 })
432 }
433
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800434 if dump {
435 sAbiDumpFile := android.ObjPathWithExt(ctx, subdir, srcFile, "sdump")
436 sAbiDumpFiles = append(sAbiDumpFiles, sAbiDumpFile)
437
438 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700439 Rule: sAbiDump,
440 Description: "header-abi-dumper " + srcFile.Rel(),
441 Output: sAbiDumpFile,
442 Input: srcFile,
443 Implicit: objFile,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800444 Args: map[string]string{
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700445 "cFlags": moduleToolingCflags,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800446 "exportDirs": flags.sAbiFlags,
447 },
448 })
449 }
450
Colin Cross3f40fa42015-01-30 17:27:36 -0800451 }
452
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700453 return Objects{
Dan Willemsen581341d2017-02-09 16:16:31 -0800454 objFiles: objFiles,
455 tidyFiles: tidyFiles,
456 coverageFiles: coverageFiles,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800457 sAbiDumpFiles: sAbiDumpFiles,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700458 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800459}
460
461// Generate a rule for compiling multiple .o files to a static library (.a)
Colin Cross635c3b02016-05-18 15:37:25 -0700462func TransformObjToStaticLib(ctx android.ModuleContext, objFiles android.Paths,
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700463 flags builderFlags, outputFile android.ModuleOutPath, deps android.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800464
Dan Willemsen581341d2017-02-09 16:16:31 -0800465 if ctx.Darwin() {
466 transformDarwinObjToStaticLib(ctx, objFiles, flags, outputFile, deps)
467 return
468 }
469
Colin Cross3f40fa42015-01-30 17:27:36 -0800470 arCmd := gccCmd(flags.toolchain, "ar")
471 arFlags := "crsPD"
Vishwath Mohan83d9f712017-03-16 11:01:23 -0700472 if flags.arFlags != "" {
473 arFlags += " " + flags.arFlags
474 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800475
Colin Cross635c3b02016-05-18 15:37:25 -0700476 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700477 Rule: ar,
478 Description: "static link " + outputFile.Base(),
479 Output: outputFile,
480 Inputs: objFiles,
481 Implicits: deps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800482 Args: map[string]string{
483 "arFlags": arFlags,
484 "arCmd": arCmd,
485 },
486 })
487}
488
Colin Cross0af4b842015-04-30 16:36:18 -0700489// Generate a rule for compiling multiple .o files to a static library (.a) on
490// darwin. The darwin ar tool doesn't support @file for list files, and has a
491// very small command line length limit, so we have to split the ar into multiple
492// steps, each appending to the previous one.
Dan Willemsen581341d2017-02-09 16:16:31 -0800493func transformDarwinObjToStaticLib(ctx android.ModuleContext, objFiles android.Paths,
Colin Cross5b529592017-05-09 13:34:34 -0700494 flags builderFlags, outputFile android.ModuleOutPath, deps android.Paths) {
Colin Cross0af4b842015-04-30 16:36:18 -0700495
Colin Cross0af4b842015-04-30 16:36:18 -0700496 arFlags := "cqs"
497
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700498 if len(objFiles) == 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700499 dummy := android.PathForModuleOut(ctx, "dummy"+objectExtension)
500 dummyAr := android.PathForModuleOut(ctx, "dummy"+staticLibraryExtension)
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700501
Colin Cross635c3b02016-05-18 15:37:25 -0700502 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700503 Rule: emptyFile,
504 Description: "empty object file",
505 Output: dummy,
506 Implicits: deps,
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700507 })
508
Colin Cross635c3b02016-05-18 15:37:25 -0700509 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700510 Rule: darwinAr,
511 Description: "empty static archive",
512 Output: dummyAr,
513 Input: dummy,
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700514 Args: map[string]string{
515 "arFlags": arFlags,
516 },
517 })
518
Colin Cross635c3b02016-05-18 15:37:25 -0700519 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700520 Rule: darwinAppendAr,
521 Description: "static link " + outputFile.Base(),
522 Output: outputFile,
523 Input: dummy,
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700524 Args: map[string]string{
525 "arFlags": "d",
526 "inAr": dummyAr.String(),
527 },
528 })
529
530 return
531 }
532
Colin Cross0af4b842015-04-30 16:36:18 -0700533 // ARG_MAX on darwin is 262144, use half that to be safe
Colin Cross5b529592017-05-09 13:34:34 -0700534 objFilesLists, err := splitListForSize(objFiles, 131072)
Colin Cross0af4b842015-04-30 16:36:18 -0700535 if err != nil {
536 ctx.ModuleErrorf("%s", err.Error())
537 }
538
Colin Cross5b529592017-05-09 13:34:34 -0700539 var in, out android.WritablePath
Colin Cross0af4b842015-04-30 16:36:18 -0700540 for i, l := range objFilesLists {
541 in = out
542 out = outputFile
543 if i != len(objFilesLists)-1 {
Colin Cross5b529592017-05-09 13:34:34 -0700544 out = android.PathForModuleOut(ctx, outputFile.Base()+strconv.Itoa(i))
Colin Cross0af4b842015-04-30 16:36:18 -0700545 }
546
Colin Cross5b529592017-05-09 13:34:34 -0700547 build := android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700548 Rule: darwinAr,
549 Description: "static link " + out.Base(),
550 Output: out,
551 Inputs: l,
552 Implicits: deps,
Colin Cross5b529592017-05-09 13:34:34 -0700553 Args: map[string]string{
554 "arFlags": arFlags,
555 },
Colin Cross0af4b842015-04-30 16:36:18 -0700556 }
Colin Cross5b529592017-05-09 13:34:34 -0700557 if i != 0 {
558 build.Rule = darwinAppendAr
559 build.Args["inAr"] = in.String()
560 }
561 ctx.ModuleBuild(pctx, build)
Colin Cross0af4b842015-04-30 16:36:18 -0700562 }
563}
564
Colin Cross3f40fa42015-01-30 17:27:36 -0800565// Generate a rule for compiling multiple .o files, plus static libraries, whole static libraries,
566// and shared libraires, to a shared library (.so) or dynamic executable
Colin Cross635c3b02016-05-18 15:37:25 -0700567func TransformObjToDynamicBinary(ctx android.ModuleContext,
568 objFiles, sharedLibs, staticLibs, lateStaticLibs, wholeStaticLibs, deps android.Paths,
569 crtBegin, crtEnd android.OptionalPath, groupLate bool, flags builderFlags, outputFile android.WritablePath) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800570
571 var ldCmd string
572 if flags.clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700573 ldCmd = "${config.ClangBin}/clang++"
Colin Cross3f40fa42015-01-30 17:27:36 -0800574 } else {
575 ldCmd = gccCmd(flags.toolchain, "g++")
576 }
577
Colin Cross3f40fa42015-01-30 17:27:36 -0800578 var libFlagsList []string
579
Colin Cross16b23492016-01-06 14:41:07 -0800580 if len(flags.libFlags) > 0 {
581 libFlagsList = append(libFlagsList, flags.libFlags)
582 }
583
Colin Cross3f40fa42015-01-30 17:27:36 -0800584 if len(wholeStaticLibs) > 0 {
Dan Willemsen490fd492015-11-24 17:53:15 -0800585 if ctx.Host() && ctx.Darwin() {
Colin Cross635c3b02016-05-18 15:37:25 -0700586 libFlagsList = append(libFlagsList, android.JoinWithPrefix(wholeStaticLibs.Strings(), "-force_load "))
Colin Cross0af4b842015-04-30 16:36:18 -0700587 } else {
588 libFlagsList = append(libFlagsList, "-Wl,--whole-archive ")
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700589 libFlagsList = append(libFlagsList, wholeStaticLibs.Strings()...)
Colin Cross0af4b842015-04-30 16:36:18 -0700590 libFlagsList = append(libFlagsList, "-Wl,--no-whole-archive ")
591 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800592 }
593
Colin Cross7a7cf972016-12-05 18:47:39 -0800594 if flags.groupStaticLibs && !ctx.Darwin() && len(staticLibs) > 0 {
Colin Cross18c0c5a2016-12-01 14:45:23 -0800595 libFlagsList = append(libFlagsList, "-Wl,--start-group")
596 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700597 libFlagsList = append(libFlagsList, staticLibs.Strings()...)
Colin Cross7a7cf972016-12-05 18:47:39 -0800598 if flags.groupStaticLibs && !ctx.Darwin() && len(staticLibs) > 0 {
Colin Cross18c0c5a2016-12-01 14:45:23 -0800599 libFlagsList = append(libFlagsList, "-Wl,--end-group")
600 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800601
Stephen Hines10347862016-07-18 15:54:54 -0700602 if groupLate && !ctx.Darwin() && len(lateStaticLibs) > 0 {
Dan Willemsenedc385f2015-07-08 13:02:23 -0700603 libFlagsList = append(libFlagsList, "-Wl,--start-group")
604 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700605 libFlagsList = append(libFlagsList, lateStaticLibs.Strings()...)
Stephen Hines10347862016-07-18 15:54:54 -0700606 if groupLate && !ctx.Darwin() && len(lateStaticLibs) > 0 {
Dan Willemsenedc385f2015-07-08 13:02:23 -0700607 libFlagsList = append(libFlagsList, "-Wl,--end-group")
608 }
609
Colin Cross3f40fa42015-01-30 17:27:36 -0800610 for _, lib := range sharedLibs {
Dan Albert9840e1b2016-07-21 08:47:33 -0700611 libFlagsList = append(libFlagsList, lib.String())
Colin Cross3f40fa42015-01-30 17:27:36 -0800612 }
613
Colin Cross3f40fa42015-01-30 17:27:36 -0800614 deps = append(deps, staticLibs...)
Colin Cross3075ad02015-03-17 10:47:08 -0700615 deps = append(deps, lateStaticLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800616 deps = append(deps, wholeStaticLibs...)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700617 if crtBegin.Valid() {
618 deps = append(deps, crtBegin.Path(), crtEnd.Path())
Colin Cross3f40fa42015-01-30 17:27:36 -0800619 }
620
Colin Cross635c3b02016-05-18 15:37:25 -0700621 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700622 Rule: ld,
623 Description: "link " + outputFile.Base(),
624 Output: outputFile,
625 Inputs: objFiles,
626 Implicits: deps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800627 Args: map[string]string{
Dan Albertce2b8392016-07-21 13:16:49 -0700628 "ldCmd": ldCmd,
629 "crtBegin": crtBegin.String(),
630 "libFlags": strings.Join(libFlagsList, " "),
631 "ldFlags": flags.ldFlags,
632 "crtEnd": crtEnd.String(),
Colin Cross3f40fa42015-01-30 17:27:36 -0800633 },
634 })
635}
636
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800637// Generate a rule to combine .dump sAbi dump files from multiple source files
638// into a single .ldump sAbi dump file
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700639func TransformDumpToLinkedDump(ctx android.ModuleContext, sAbiDumps android.Paths, soFile android.Path,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800640 symbolFile android.OptionalPath, apiLevel, baseName, exportedHeaderFlags string) android.OptionalPath {
641 outputFile := android.PathForModuleOut(ctx, baseName+".lsdump")
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700642 var symbolFilterStr string
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800643 var linkedDumpDep android.Path
644 if symbolFile.Valid() {
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700645 symbolFilterStr = "-v " + symbolFile.Path().String()
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800646 linkedDumpDep = symbolFile.Path()
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700647 } else {
648 linkedDumpDep = soFile
649 symbolFilterStr = "-so " + soFile.String()
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800650 }
651 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700652 Rule: sAbiLink,
653 Description: "header-abi-linker " + outputFile.Base(),
654 Output: outputFile,
655 Inputs: sAbiDumps,
656 Implicit: linkedDumpDep,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800657 Args: map[string]string{
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700658 "symbolFilter": symbolFilterStr,
659 "arch": ctx.Arch().ArchType.Name,
660 "api": apiLevel,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800661 "exportedHeaderFlags": exportedHeaderFlags,
662 },
663 })
664 return android.OptionalPathForPath(outputFile)
665}
666
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700667func UnzipRefDump(ctx android.ModuleContext, zippedRefDump android.Path, baseName string) android.Path {
668 outputFile := android.PathForModuleOut(ctx, baseName+"_ref.lsdump")
669 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
670 Rule: unzipRefSAbiDump,
671 Description: "gunzip" + outputFile.Base(),
672 Output: outputFile,
673 Input: zippedRefDump,
674 })
675 return outputFile
676}
677
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800678func SourceAbiDiff(ctx android.ModuleContext, inputDump android.Path, referenceDump android.Path,
679 baseName string) android.OptionalPath {
680 outputFile := android.PathForModuleOut(ctx, baseName+".abidiff")
681 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700682 Rule: sAbiDiff,
683 Description: "header-abi-diff " + outputFile.Base(),
684 Output: outputFile,
685 Input: inputDump,
686 Implicit: referenceDump,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800687 Args: map[string]string{
688 "referenceDump": referenceDump.String(),
Jayant Chowdharyf54e0a72017-05-22 10:53:24 -0700689 "libName": baseName,
690 "arch": ctx.Arch().ArchType.Name,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800691 },
692 })
693 return android.OptionalPathForPath(outputFile)
694}
695
Colin Cross26c34ed2016-09-30 17:10:16 -0700696// Generate a rule for extract a table of contents from a shared library (.so)
697func TransformSharedObjectToToc(ctx android.ModuleContext, inputFile android.WritablePath,
698 outputFile android.WritablePath, flags builderFlags) {
699
700 crossCompile := gccCmd(flags.toolchain, "")
701
702 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700703 Rule: toc,
704 Description: "generate toc " + inputFile.Base(),
705 Output: outputFile,
706 Input: inputFile,
Colin Cross26c34ed2016-09-30 17:10:16 -0700707 Args: map[string]string{
708 "crossCompile": crossCompile,
709 },
710 })
711}
712
Colin Cross3f40fa42015-01-30 17:27:36 -0800713// Generate a rule for compiling multiple .o files to a .o using ld partial linking
Colin Cross635c3b02016-05-18 15:37:25 -0700714func TransformObjsToObj(ctx android.ModuleContext, objFiles android.Paths,
715 flags builderFlags, outputFile android.WritablePath) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800716
Colin Cross41280a42015-11-23 14:01:42 -0800717 var ldCmd string
718 if flags.clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700719 ldCmd = "${config.ClangBin}/clang++"
Colin Cross41280a42015-11-23 14:01:42 -0800720 } else {
721 ldCmd = gccCmd(flags.toolchain, "g++")
722 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800723
Colin Cross635c3b02016-05-18 15:37:25 -0700724 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700725 Rule: partialLd,
726 Description: "link " + outputFile.Base(),
727 Output: outputFile,
728 Inputs: objFiles,
Colin Cross3f40fa42015-01-30 17:27:36 -0800729 Args: map[string]string{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700730 "ldCmd": ldCmd,
Colin Cross41280a42015-11-23 14:01:42 -0800731 "ldFlags": flags.ldFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800732 },
733 })
734}
735
Colin Crossbfae8852015-03-26 14:44:11 -0700736// Generate a rule for runing objcopy --prefix-symbols on a binary
Colin Cross635c3b02016-05-18 15:37:25 -0700737func TransformBinaryPrefixSymbols(ctx android.ModuleContext, prefix string, inputFile android.Path,
738 flags builderFlags, outputFile android.WritablePath) {
Colin Crossbfae8852015-03-26 14:44:11 -0700739
740 objcopyCmd := gccCmd(flags.toolchain, "objcopy")
741
Colin Cross635c3b02016-05-18 15:37:25 -0700742 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700743 Rule: prefixSymbols,
744 Description: "prefix symbols " + outputFile.Base(),
745 Output: outputFile,
746 Input: inputFile,
Colin Crossbfae8852015-03-26 14:44:11 -0700747 Args: map[string]string{
748 "objcopyCmd": objcopyCmd,
749 "prefix": prefix,
750 },
751 })
752}
753
Colin Cross635c3b02016-05-18 15:37:25 -0700754func TransformStrip(ctx android.ModuleContext, inputFile android.Path,
755 outputFile android.WritablePath, flags builderFlags) {
Colin Cross665dce92016-04-28 14:50:03 -0700756
757 crossCompile := gccCmd(flags.toolchain, "")
758 args := ""
759 if flags.stripAddGnuDebuglink {
760 args += " --add-gnu-debuglink"
761 }
762 if flags.stripKeepMiniDebugInfo {
763 args += " --keep-mini-debug-info"
764 }
765 if flags.stripKeepSymbols {
766 args += " --keep-symbols"
767 }
768
Colin Cross635c3b02016-05-18 15:37:25 -0700769 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700770 Rule: strip,
771 Description: "strip " + outputFile.Base(),
772 Output: outputFile,
773 Input: inputFile,
Colin Cross665dce92016-04-28 14:50:03 -0700774 Args: map[string]string{
775 "crossCompile": crossCompile,
776 "args": args,
777 },
778 })
779}
780
Colin Cross635c3b02016-05-18 15:37:25 -0700781func TransformDarwinStrip(ctx android.ModuleContext, inputFile android.Path,
782 outputFile android.WritablePath) {
Colin Crossb8ecdfe2016-05-03 15:10:29 -0700783
Colin Cross635c3b02016-05-18 15:37:25 -0700784 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700785 Rule: darwinStrip,
786 Description: "strip " + outputFile.Base(),
787 Output: outputFile,
788 Input: inputFile,
Colin Crossb8ecdfe2016-05-03 15:10:29 -0700789 })
790}
791
Dan Willemsen581341d2017-02-09 16:16:31 -0800792func TransformCoverageFilesToLib(ctx android.ModuleContext,
793 inputs Objects, flags builderFlags, baseName string) android.OptionalPath {
794
795 if len(inputs.coverageFiles) > 0 {
796 outputFile := android.PathForModuleOut(ctx, baseName+".gcnodir")
797
798 TransformObjToStaticLib(ctx, inputs.coverageFiles, flags, outputFile, nil)
799
800 return android.OptionalPathForPath(outputFile)
801 }
802
803 return android.OptionalPath{}
804}
805
Colin Cross635c3b02016-05-18 15:37:25 -0700806func CopyGccLib(ctx android.ModuleContext, libName string,
807 flags builderFlags, outputFile android.WritablePath) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800808
Colin Cross635c3b02016-05-18 15:37:25 -0700809 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700810 Rule: copyGccLib,
811 Description: "copy gcc library " + libName,
812 Output: outputFile,
Colin Cross3f40fa42015-01-30 17:27:36 -0800813 Args: map[string]string{
814 "ccCmd": gccCmd(flags.toolchain, "gcc"),
815 "cFlags": flags.globalFlags,
816 "libName": libName,
817 },
818 })
819}
820
Colin Crossb98c8b02016-07-29 13:44:28 -0700821func gccCmd(toolchain config.Toolchain, cmd string) string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800822 return filepath.Join(toolchain.GccRoot(), "bin", toolchain.GccTriple()+"-"+cmd)
823}
Colin Cross0af4b842015-04-30 16:36:18 -0700824
Colin Cross5b529592017-05-09 13:34:34 -0700825func splitListForSize(list android.Paths, limit int) (lists []android.Paths, err error) {
Colin Cross0af4b842015-04-30 16:36:18 -0700826 var i int
827
828 start := 0
829 bytes := 0
830 for i = range list {
Colin Cross5b529592017-05-09 13:34:34 -0700831 l := len(list[i].String())
Colin Cross0af4b842015-04-30 16:36:18 -0700832 if l > limit {
833 return nil, fmt.Errorf("list element greater than size limit (%d)", limit)
834 }
835 if bytes+l > limit {
836 lists = append(lists, list[start:i])
837 start = i
838 bytes = 0
839 }
840 bytes += l + 1 // count a space between each list element
841 }
842
843 lists = append(lists, list[start:])
844
845 totalLen := 0
846 for _, l := range lists {
847 totalLen += len(l)
848 }
849 if totalLen != len(list) {
850 panic(fmt.Errorf("Failed breaking up list, %d != %d", len(list), totalLen))
851 }
852 return lists, nil
853}