blob: b5bdc3d3796f1dc4e657fa58f3e06a0dc0e80a35 [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
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700161 windres = pctx.AndroidStaticRule("windres",
162 blueprint.RuleParams{
163 Command: "$windresCmd $flags -I$$(dirname $in) -i $in -o $out",
164 CommandDeps: []string{"$windresCmd"},
165 },
166 "windresCmd", "flags")
167
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800168 _ = pctx.SourcePathVariable("sAbiDumper", "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/header-abi-dumper")
169
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700170 // -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 -0800171 sAbiDump = pctx.AndroidStaticRule("sAbiDump",
172 blueprint.RuleParams{
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700173 Command: "rm -f $out && $sAbiDumper -o ${out} $in $exportDirs -- $cFlags -w -isystem ${config.RSIncludePath}",
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800174 CommandDeps: []string{"$sAbiDumper"},
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800175 },
176 "cFlags", "exportDirs")
177
178 _ = pctx.SourcePathVariable("sAbiLinker", "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/header-abi-linker")
179
180 sAbiLink = pctx.AndroidStaticRule("sAbiLink",
181 blueprint.RuleParams{
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700182 Command: "$sAbiLinker -o ${out} $symbolFilter -arch $arch -api $api $exportedHeaderFlags @${out}.rsp ",
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800183 CommandDeps: []string{"$sAbiLinker"},
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800184 Rspfile: "${out}.rsp",
185 RspfileContent: "${in}",
186 },
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700187 "symbolFilter", "arch", "api", "exportedHeaderFlags")
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800188
189 _ = pctx.SourcePathVariable("sAbiDiffer", "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/header-abi-diff")
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700190
Jayant Chowdhary918b1d92017-04-18 10:44:00 -0700191 // Abidiff check turned on in advice-only mode. Builds will not fail on abi incompatibilties / extensions.
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800192 sAbiDiff = pctx.AndroidStaticRule("sAbiDiff",
193 blueprint.RuleParams{
Jayant Chowdharyaf6eb712017-08-23 16:08:29 -0700194 Command: "$sAbiDiffer -lib $libName -arch $arch -advice-only -check-all-apis -o ${out} -new $in -old $referenceDump",
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800195 CommandDeps: []string{"$sAbiDiffer"},
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800196 },
Jayant Chowdharyf54e0a72017-05-22 10:53:24 -0700197 "referenceDump", "libName", "arch")
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700198
199 unzipRefSAbiDump = pctx.AndroidStaticRule("unzipRefSAbiDump",
200 blueprint.RuleParams{
201 Command: "gunzip -c $in > $out",
202 })
Colin Cross3f40fa42015-01-30 17:27:36 -0800203)
204
Dan Willemsen322a0a62015-11-17 15:19:46 -0800205func init() {
206 // We run gcc/clang with PWD=/proc/self/cwd to remove $TOP from the
207 // debug output. That way two builds in two different directories will
208 // create the same output.
209 if runtime.GOOS != "darwin" {
210 pctx.StaticVariable("relPwd", "PWD=/proc/self/cwd")
211 } else {
212 // Darwin doesn't have /proc
213 pctx.StaticVariable("relPwd", "")
214 }
215}
216
Colin Cross3f40fa42015-01-30 17:27:36 -0800217type builderFlags struct {
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700218 globalFlags string
219 arFlags string
220 asFlags string
221 cFlags string
222 toolingCFlags string // Seperate set of Cflags for clang LibTooling tools
223 conlyFlags string
224 cppFlags string
225 ldFlags string
226 libFlags string
227 yaccFlags string
228 protoFlags string
229 tidyFlags string
230 sAbiFlags string
231 yasmFlags string
232 aidlFlags string
233 rsFlags string
234 toolchain config.Toolchain
235 clang bool
236 tidy bool
237 coverage bool
238 sAbiDump bool
Colin Cross665dce92016-04-28 14:50:03 -0700239
Colin Crossc3199482017-03-30 15:03:04 -0700240 systemIncludeFlags string
241
Colin Cross18c0c5a2016-12-01 14:45:23 -0800242 groupStaticLibs bool
243
Colin Cross665dce92016-04-28 14:50:03 -0700244 stripKeepSymbols bool
245 stripKeepMiniDebugInfo bool
246 stripAddGnuDebuglink bool
Colin Cross3f40fa42015-01-30 17:27:36 -0800247}
248
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700249type Objects struct {
Dan Willemsen581341d2017-02-09 16:16:31 -0800250 objFiles android.Paths
251 tidyFiles android.Paths
252 coverageFiles android.Paths
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800253 sAbiDumpFiles android.Paths
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700254}
255
256func (a Objects) Copy() Objects {
257 return Objects{
Dan Willemsen581341d2017-02-09 16:16:31 -0800258 objFiles: append(android.Paths{}, a.objFiles...),
259 tidyFiles: append(android.Paths{}, a.tidyFiles...),
260 coverageFiles: append(android.Paths{}, a.coverageFiles...),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800261 sAbiDumpFiles: append(android.Paths{}, a.sAbiDumpFiles...),
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700262 }
263}
264
265func (a Objects) Append(b Objects) Objects {
266 return Objects{
Dan Willemsen581341d2017-02-09 16:16:31 -0800267 objFiles: append(a.objFiles, b.objFiles...),
268 tidyFiles: append(a.tidyFiles, b.tidyFiles...),
269 coverageFiles: append(a.coverageFiles, b.coverageFiles...),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800270 sAbiDumpFiles: append(a.sAbiDumpFiles, b.sAbiDumpFiles...),
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700271 }
272}
273
Colin Cross3f40fa42015-01-30 17:27:36 -0800274// Generate rules for compiling multiple .c, .cpp, or .S files to individual .o files
Colin Cross635c3b02016-05-18 15:37:25 -0700275func TransformSourceToObj(ctx android.ModuleContext, subdir string, srcFiles android.Paths,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700276 flags builderFlags, deps android.Paths) Objects {
Colin Cross581c1892015-04-07 16:50:10 -0700277
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700278 objFiles := make(android.Paths, len(srcFiles))
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700279 var tidyFiles android.Paths
280 if flags.tidy && flags.clang {
281 tidyFiles = make(android.Paths, 0, len(srcFiles))
282 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800283 var coverageFiles android.Paths
284 if flags.coverage {
285 coverageFiles = make(android.Paths, 0, len(srcFiles))
286 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800287
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700288 commonFlags := strings.Join([]string{
Colin Crossc3199482017-03-30 15:03:04 -0700289 flags.globalFlags,
290 flags.systemIncludeFlags,
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700291 }, " ")
292
293 toolingCflags := strings.Join([]string{
294 commonFlags,
295 flags.toolingCFlags,
296 flags.conlyFlags,
297 }, " ")
298
299 cflags := strings.Join([]string{
300 commonFlags,
Colin Crossc3199482017-03-30 15:03:04 -0700301 flags.cFlags,
302 flags.conlyFlags,
303 }, " ")
304
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700305 toolingCppflags := strings.Join([]string{
306 commonFlags,
307 flags.toolingCFlags,
308 flags.cppFlags,
309 }, " ")
310
Colin Crossc3199482017-03-30 15:03:04 -0700311 cppflags := strings.Join([]string{
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700312 commonFlags,
Colin Crossc3199482017-03-30 15:03:04 -0700313 flags.cFlags,
314 flags.cppFlags,
315 }, " ")
316
317 asflags := strings.Join([]string{
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700318 commonFlags,
Colin Crossc3199482017-03-30 15:03:04 -0700319 flags.asFlags,
320 }, " ")
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700321
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800322 var sAbiDumpFiles android.Paths
323 if flags.sAbiDump && flags.clang {
324 sAbiDumpFiles = make(android.Paths, 0, len(srcFiles))
325 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800326
Dan Willemsenbe03f342016-03-03 17:21:04 -0800327 if flags.clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700328 cflags += " ${config.NoOverrideClangGlobalCflags}"
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700329 toolingCflags += " ${config.NoOverrideClangGlobalCflags}"
Colin Crossb98c8b02016-07-29 13:44:28 -0700330 cppflags += " ${config.NoOverrideClangGlobalCflags}"
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700331 toolingCppflags += " ${config.NoOverrideClangGlobalCflags}"
Dan Willemsenbe03f342016-03-03 17:21:04 -0800332 } else {
Colin Crossb98c8b02016-07-29 13:44:28 -0700333 cflags += " ${config.NoOverrideGlobalCflags}"
334 cppflags += " ${config.NoOverrideGlobalCflags}"
Dan Willemsenbe03f342016-03-03 17:21:04 -0800335 }
336
Colin Cross3f40fa42015-01-30 17:27:36 -0800337 for i, srcFile := range srcFiles {
Dan Willemsen21ec4902016-11-02 20:43:13 -0700338 objFile := android.ObjPathWithExt(ctx, subdir, srcFile, "o")
Colin Cross3f40fa42015-01-30 17:27:36 -0800339
340 objFiles[i] = objFile
341
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700342 switch srcFile.Ext() {
343 case ".asm":
Colin Cross91e90042016-12-02 17:13:24 -0800344 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700345 Rule: yasm,
346 Description: "yasm " + srcFile.Rel(),
347 Output: objFile,
348 Input: srcFile,
349 OrderOnly: deps,
Colin Cross91e90042016-12-02 17:13:24 -0800350 Args: map[string]string{
351 "asFlags": flags.yasmFlags,
352 },
353 })
354 continue
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700355 case ".rc":
356 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
357 Rule: windres,
358 Description: "windres " + srcFile.Rel(),
359 Output: objFile,
360 Input: srcFile,
361 OrderOnly: deps,
362 Args: map[string]string{
363 "windresCmd": gccCmd(flags.toolchain, "windres"),
364 "flags": flags.toolchain.WindresFlags(),
365 },
366 })
367 continue
Colin Cross91e90042016-12-02 17:13:24 -0800368 }
369
Colin Cross3f40fa42015-01-30 17:27:36 -0800370 var moduleCflags string
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700371 var moduleToolingCflags string
Colin Cross3f40fa42015-01-30 17:27:36 -0800372 var ccCmd string
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700373 tidy := flags.tidy && flags.clang
Dan Willemsen581341d2017-02-09 16:16:31 -0800374 coverage := flags.coverage
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800375 dump := flags.sAbiDump && flags.clang
Colin Cross3f40fa42015-01-30 17:27:36 -0800376
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700377 switch srcFile.Ext() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800378 case ".S", ".s":
379 ccCmd = "gcc"
380 moduleCflags = asflags
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700381 tidy = false
Dan Willemsen581341d2017-02-09 16:16:31 -0800382 coverage = false
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800383 dump = false
Colin Cross3f40fa42015-01-30 17:27:36 -0800384 case ".c":
385 ccCmd = "gcc"
386 moduleCflags = cflags
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700387 moduleToolingCflags = toolingCflags
Colin Cross9978ffe2016-12-01 15:31:22 -0800388 case ".cpp", ".cc", ".mm":
Colin Cross3f40fa42015-01-30 17:27:36 -0800389 ccCmd = "g++"
390 moduleCflags = cppflags
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700391 moduleToolingCflags = toolingCppflags
Colin Cross3f40fa42015-01-30 17:27:36 -0800392 default:
393 ctx.ModuleErrorf("File %s has unknown extension", srcFile)
394 continue
395 }
396
397 if flags.clang {
398 switch ccCmd {
399 case "gcc":
400 ccCmd = "clang"
401 case "g++":
402 ccCmd = "clang++"
403 default:
404 panic("unrecoginzied ccCmd")
405 }
Colin Cross67a5c132017-05-09 13:45:28 -0700406 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800407
Colin Cross67a5c132017-05-09 13:45:28 -0700408 ccDesc := ccCmd
409
410 if flags.clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700411 ccCmd = "${config.ClangBin}/" + ccCmd
Colin Cross3f40fa42015-01-30 17:27:36 -0800412 } else {
413 ccCmd = gccCmd(flags.toolchain, ccCmd)
414 }
415
Dan Willemsen581341d2017-02-09 16:16:31 -0800416 var implicitOutputs android.WritablePaths
417 if coverage {
418 gcnoFile := android.ObjPathWithExt(ctx, subdir, srcFile, "gcno")
419 implicitOutputs = append(implicitOutputs, gcnoFile)
420 coverageFiles = append(coverageFiles, gcnoFile)
421 }
422
Colin Cross635c3b02016-05-18 15:37:25 -0700423 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Dan Willemsen581341d2017-02-09 16:16:31 -0800424 Rule: cc,
Colin Cross67a5c132017-05-09 13:45:28 -0700425 Description: ccDesc + " " + srcFile.Rel(),
Dan Willemsen581341d2017-02-09 16:16:31 -0800426 Output: objFile,
427 ImplicitOutputs: implicitOutputs,
428 Input: srcFile,
429 OrderOnly: deps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800430 Args: map[string]string{
Colin Cross28344522015-04-22 13:07:53 -0700431 "cFlags": moduleCflags,
432 "ccCmd": ccCmd,
Colin Cross3f40fa42015-01-30 17:27:36 -0800433 },
434 })
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700435
436 if tidy {
Dan Willemsen21ec4902016-11-02 20:43:13 -0700437 tidyFile := android.ObjPathWithExt(ctx, subdir, srcFile, "tidy")
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700438 tidyFiles = append(tidyFiles, tidyFile)
439
440 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700441 Rule: clangTidy,
442 Description: "clang-tidy " + srcFile.Rel(),
443 Output: tidyFile,
444 Input: srcFile,
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700445 // We must depend on objFile, since clang-tidy doesn't
446 // support exporting dependencies.
447 Implicit: objFile,
448 Args: map[string]string{
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700449 "cFlags": moduleToolingCflags,
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700450 "tidyFlags": flags.tidyFlags,
451 },
452 })
453 }
454
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800455 if dump {
456 sAbiDumpFile := android.ObjPathWithExt(ctx, subdir, srcFile, "sdump")
457 sAbiDumpFiles = append(sAbiDumpFiles, sAbiDumpFile)
458
459 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700460 Rule: sAbiDump,
461 Description: "header-abi-dumper " + srcFile.Rel(),
462 Output: sAbiDumpFile,
463 Input: srcFile,
464 Implicit: objFile,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800465 Args: map[string]string{
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700466 "cFlags": moduleToolingCflags,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800467 "exportDirs": flags.sAbiFlags,
468 },
469 })
470 }
471
Colin Cross3f40fa42015-01-30 17:27:36 -0800472 }
473
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700474 return Objects{
Dan Willemsen581341d2017-02-09 16:16:31 -0800475 objFiles: objFiles,
476 tidyFiles: tidyFiles,
477 coverageFiles: coverageFiles,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800478 sAbiDumpFiles: sAbiDumpFiles,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700479 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800480}
481
482// Generate a rule for compiling multiple .o files to a static library (.a)
Colin Cross635c3b02016-05-18 15:37:25 -0700483func TransformObjToStaticLib(ctx android.ModuleContext, objFiles android.Paths,
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700484 flags builderFlags, outputFile android.ModuleOutPath, deps android.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800485
Dan Willemsen581341d2017-02-09 16:16:31 -0800486 if ctx.Darwin() {
487 transformDarwinObjToStaticLib(ctx, objFiles, flags, outputFile, deps)
488 return
489 }
490
Colin Cross3f40fa42015-01-30 17:27:36 -0800491 arCmd := gccCmd(flags.toolchain, "ar")
492 arFlags := "crsPD"
Vishwath Mohan83d9f712017-03-16 11:01:23 -0700493 if flags.arFlags != "" {
494 arFlags += " " + flags.arFlags
495 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800496
Colin Cross635c3b02016-05-18 15:37:25 -0700497 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700498 Rule: ar,
499 Description: "static link " + outputFile.Base(),
500 Output: outputFile,
501 Inputs: objFiles,
502 Implicits: deps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800503 Args: map[string]string{
504 "arFlags": arFlags,
505 "arCmd": arCmd,
506 },
507 })
508}
509
Colin Cross0af4b842015-04-30 16:36:18 -0700510// Generate a rule for compiling multiple .o files to a static library (.a) on
511// darwin. The darwin ar tool doesn't support @file for list files, and has a
512// very small command line length limit, so we have to split the ar into multiple
513// steps, each appending to the previous one.
Dan Willemsen581341d2017-02-09 16:16:31 -0800514func transformDarwinObjToStaticLib(ctx android.ModuleContext, objFiles android.Paths,
Colin Cross5b529592017-05-09 13:34:34 -0700515 flags builderFlags, outputFile android.ModuleOutPath, deps android.Paths) {
Colin Cross0af4b842015-04-30 16:36:18 -0700516
Colin Cross0af4b842015-04-30 16:36:18 -0700517 arFlags := "cqs"
518
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700519 if len(objFiles) == 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700520 dummy := android.PathForModuleOut(ctx, "dummy"+objectExtension)
521 dummyAr := android.PathForModuleOut(ctx, "dummy"+staticLibraryExtension)
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700522
Colin Cross635c3b02016-05-18 15:37:25 -0700523 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700524 Rule: emptyFile,
525 Description: "empty object file",
526 Output: dummy,
527 Implicits: deps,
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700528 })
529
Colin Cross635c3b02016-05-18 15:37:25 -0700530 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700531 Rule: darwinAr,
532 Description: "empty static archive",
533 Output: dummyAr,
534 Input: dummy,
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700535 Args: map[string]string{
536 "arFlags": arFlags,
537 },
538 })
539
Colin Cross635c3b02016-05-18 15:37:25 -0700540 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700541 Rule: darwinAppendAr,
542 Description: "static link " + outputFile.Base(),
543 Output: outputFile,
544 Input: dummy,
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700545 Args: map[string]string{
546 "arFlags": "d",
547 "inAr": dummyAr.String(),
548 },
549 })
550
551 return
552 }
553
Colin Cross0af4b842015-04-30 16:36:18 -0700554 // ARG_MAX on darwin is 262144, use half that to be safe
Colin Cross5b529592017-05-09 13:34:34 -0700555 objFilesLists, err := splitListForSize(objFiles, 131072)
Colin Cross0af4b842015-04-30 16:36:18 -0700556 if err != nil {
557 ctx.ModuleErrorf("%s", err.Error())
558 }
559
Colin Cross5b529592017-05-09 13:34:34 -0700560 var in, out android.WritablePath
Colin Cross0af4b842015-04-30 16:36:18 -0700561 for i, l := range objFilesLists {
562 in = out
563 out = outputFile
564 if i != len(objFilesLists)-1 {
Colin Cross5b529592017-05-09 13:34:34 -0700565 out = android.PathForModuleOut(ctx, outputFile.Base()+strconv.Itoa(i))
Colin Cross0af4b842015-04-30 16:36:18 -0700566 }
567
Colin Cross5b529592017-05-09 13:34:34 -0700568 build := android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700569 Rule: darwinAr,
570 Description: "static link " + out.Base(),
571 Output: out,
572 Inputs: l,
573 Implicits: deps,
Colin Cross5b529592017-05-09 13:34:34 -0700574 Args: map[string]string{
575 "arFlags": arFlags,
576 },
Colin Cross0af4b842015-04-30 16:36:18 -0700577 }
Colin Cross5b529592017-05-09 13:34:34 -0700578 if i != 0 {
579 build.Rule = darwinAppendAr
580 build.Args["inAr"] = in.String()
581 }
582 ctx.ModuleBuild(pctx, build)
Colin Cross0af4b842015-04-30 16:36:18 -0700583 }
584}
585
Colin Cross3f40fa42015-01-30 17:27:36 -0800586// Generate a rule for compiling multiple .o files, plus static libraries, whole static libraries,
587// and shared libraires, to a shared library (.so) or dynamic executable
Colin Cross635c3b02016-05-18 15:37:25 -0700588func TransformObjToDynamicBinary(ctx android.ModuleContext,
589 objFiles, sharedLibs, staticLibs, lateStaticLibs, wholeStaticLibs, deps android.Paths,
590 crtBegin, crtEnd android.OptionalPath, groupLate bool, flags builderFlags, outputFile android.WritablePath) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800591
592 var ldCmd string
593 if flags.clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700594 ldCmd = "${config.ClangBin}/clang++"
Colin Cross3f40fa42015-01-30 17:27:36 -0800595 } else {
596 ldCmd = gccCmd(flags.toolchain, "g++")
597 }
598
Colin Cross3f40fa42015-01-30 17:27:36 -0800599 var libFlagsList []string
600
Colin Cross16b23492016-01-06 14:41:07 -0800601 if len(flags.libFlags) > 0 {
602 libFlagsList = append(libFlagsList, flags.libFlags)
603 }
604
Colin Cross3f40fa42015-01-30 17:27:36 -0800605 if len(wholeStaticLibs) > 0 {
Dan Willemsen490fd492015-11-24 17:53:15 -0800606 if ctx.Host() && ctx.Darwin() {
Colin Cross635c3b02016-05-18 15:37:25 -0700607 libFlagsList = append(libFlagsList, android.JoinWithPrefix(wholeStaticLibs.Strings(), "-force_load "))
Colin Cross0af4b842015-04-30 16:36:18 -0700608 } else {
609 libFlagsList = append(libFlagsList, "-Wl,--whole-archive ")
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700610 libFlagsList = append(libFlagsList, wholeStaticLibs.Strings()...)
Colin Cross0af4b842015-04-30 16:36:18 -0700611 libFlagsList = append(libFlagsList, "-Wl,--no-whole-archive ")
612 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800613 }
614
Colin Cross7a7cf972016-12-05 18:47:39 -0800615 if flags.groupStaticLibs && !ctx.Darwin() && len(staticLibs) > 0 {
Colin Cross18c0c5a2016-12-01 14:45:23 -0800616 libFlagsList = append(libFlagsList, "-Wl,--start-group")
617 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700618 libFlagsList = append(libFlagsList, staticLibs.Strings()...)
Colin Cross7a7cf972016-12-05 18:47:39 -0800619 if flags.groupStaticLibs && !ctx.Darwin() && len(staticLibs) > 0 {
Colin Cross18c0c5a2016-12-01 14:45:23 -0800620 libFlagsList = append(libFlagsList, "-Wl,--end-group")
621 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800622
Stephen Hines10347862016-07-18 15:54:54 -0700623 if groupLate && !ctx.Darwin() && len(lateStaticLibs) > 0 {
Dan Willemsenedc385f2015-07-08 13:02:23 -0700624 libFlagsList = append(libFlagsList, "-Wl,--start-group")
625 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700626 libFlagsList = append(libFlagsList, lateStaticLibs.Strings()...)
Stephen Hines10347862016-07-18 15:54:54 -0700627 if groupLate && !ctx.Darwin() && len(lateStaticLibs) > 0 {
Dan Willemsenedc385f2015-07-08 13:02:23 -0700628 libFlagsList = append(libFlagsList, "-Wl,--end-group")
629 }
630
Colin Cross3f40fa42015-01-30 17:27:36 -0800631 for _, lib := range sharedLibs {
Dan Albert9840e1b2016-07-21 08:47:33 -0700632 libFlagsList = append(libFlagsList, lib.String())
Colin Cross3f40fa42015-01-30 17:27:36 -0800633 }
634
Colin Cross3f40fa42015-01-30 17:27:36 -0800635 deps = append(deps, staticLibs...)
Colin Cross3075ad02015-03-17 10:47:08 -0700636 deps = append(deps, lateStaticLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800637 deps = append(deps, wholeStaticLibs...)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700638 if crtBegin.Valid() {
639 deps = append(deps, crtBegin.Path(), crtEnd.Path())
Colin Cross3f40fa42015-01-30 17:27:36 -0800640 }
641
Colin Cross635c3b02016-05-18 15:37:25 -0700642 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700643 Rule: ld,
644 Description: "link " + outputFile.Base(),
645 Output: outputFile,
646 Inputs: objFiles,
647 Implicits: deps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800648 Args: map[string]string{
Dan Albertce2b8392016-07-21 13:16:49 -0700649 "ldCmd": ldCmd,
650 "crtBegin": crtBegin.String(),
651 "libFlags": strings.Join(libFlagsList, " "),
652 "ldFlags": flags.ldFlags,
653 "crtEnd": crtEnd.String(),
Colin Cross3f40fa42015-01-30 17:27:36 -0800654 },
655 })
656}
657
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800658// Generate a rule to combine .dump sAbi dump files from multiple source files
659// into a single .ldump sAbi dump file
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700660func TransformDumpToLinkedDump(ctx android.ModuleContext, sAbiDumps android.Paths, soFile android.Path,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800661 symbolFile android.OptionalPath, apiLevel, baseName, exportedHeaderFlags string) android.OptionalPath {
662 outputFile := android.PathForModuleOut(ctx, baseName+".lsdump")
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700663 var symbolFilterStr string
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800664 var linkedDumpDep android.Path
665 if symbolFile.Valid() {
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700666 symbolFilterStr = "-v " + symbolFile.Path().String()
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800667 linkedDumpDep = symbolFile.Path()
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700668 } else {
669 linkedDumpDep = soFile
670 symbolFilterStr = "-so " + soFile.String()
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800671 }
672 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700673 Rule: sAbiLink,
674 Description: "header-abi-linker " + outputFile.Base(),
675 Output: outputFile,
676 Inputs: sAbiDumps,
677 Implicit: linkedDumpDep,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800678 Args: map[string]string{
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700679 "symbolFilter": symbolFilterStr,
680 "arch": ctx.Arch().ArchType.Name,
681 "api": apiLevel,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800682 "exportedHeaderFlags": exportedHeaderFlags,
683 },
684 })
685 return android.OptionalPathForPath(outputFile)
686}
687
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700688func UnzipRefDump(ctx android.ModuleContext, zippedRefDump android.Path, baseName string) android.Path {
689 outputFile := android.PathForModuleOut(ctx, baseName+"_ref.lsdump")
690 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
691 Rule: unzipRefSAbiDump,
692 Description: "gunzip" + outputFile.Base(),
693 Output: outputFile,
694 Input: zippedRefDump,
695 })
696 return outputFile
697}
698
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800699func SourceAbiDiff(ctx android.ModuleContext, inputDump android.Path, referenceDump android.Path,
700 baseName string) android.OptionalPath {
701 outputFile := android.PathForModuleOut(ctx, baseName+".abidiff")
702 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700703 Rule: sAbiDiff,
704 Description: "header-abi-diff " + outputFile.Base(),
705 Output: outputFile,
706 Input: inputDump,
707 Implicit: referenceDump,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800708 Args: map[string]string{
709 "referenceDump": referenceDump.String(),
Jayant Chowdharyf54e0a72017-05-22 10:53:24 -0700710 "libName": baseName,
711 "arch": ctx.Arch().ArchType.Name,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800712 },
713 })
714 return android.OptionalPathForPath(outputFile)
715}
716
Colin Cross26c34ed2016-09-30 17:10:16 -0700717// Generate a rule for extract a table of contents from a shared library (.so)
718func TransformSharedObjectToToc(ctx android.ModuleContext, inputFile android.WritablePath,
719 outputFile android.WritablePath, flags builderFlags) {
720
721 crossCompile := gccCmd(flags.toolchain, "")
722
723 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700724 Rule: toc,
725 Description: "generate toc " + inputFile.Base(),
726 Output: outputFile,
727 Input: inputFile,
Colin Cross26c34ed2016-09-30 17:10:16 -0700728 Args: map[string]string{
729 "crossCompile": crossCompile,
730 },
731 })
732}
733
Colin Cross3f40fa42015-01-30 17:27:36 -0800734// Generate a rule for compiling multiple .o files to a .o using ld partial linking
Colin Cross635c3b02016-05-18 15:37:25 -0700735func TransformObjsToObj(ctx android.ModuleContext, objFiles android.Paths,
736 flags builderFlags, outputFile android.WritablePath) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800737
Colin Cross41280a42015-11-23 14:01:42 -0800738 var ldCmd string
739 if flags.clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700740 ldCmd = "${config.ClangBin}/clang++"
Colin Cross41280a42015-11-23 14:01:42 -0800741 } else {
742 ldCmd = gccCmd(flags.toolchain, "g++")
743 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800744
Colin Cross635c3b02016-05-18 15:37:25 -0700745 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700746 Rule: partialLd,
747 Description: "link " + outputFile.Base(),
748 Output: outputFile,
749 Inputs: objFiles,
Colin Cross3f40fa42015-01-30 17:27:36 -0800750 Args: map[string]string{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700751 "ldCmd": ldCmd,
Colin Cross41280a42015-11-23 14:01:42 -0800752 "ldFlags": flags.ldFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800753 },
754 })
755}
756
Colin Crossbfae8852015-03-26 14:44:11 -0700757// Generate a rule for runing objcopy --prefix-symbols on a binary
Colin Cross635c3b02016-05-18 15:37:25 -0700758func TransformBinaryPrefixSymbols(ctx android.ModuleContext, prefix string, inputFile android.Path,
759 flags builderFlags, outputFile android.WritablePath) {
Colin Crossbfae8852015-03-26 14:44:11 -0700760
761 objcopyCmd := gccCmd(flags.toolchain, "objcopy")
762
Colin Cross635c3b02016-05-18 15:37:25 -0700763 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700764 Rule: prefixSymbols,
765 Description: "prefix symbols " + outputFile.Base(),
766 Output: outputFile,
767 Input: inputFile,
Colin Crossbfae8852015-03-26 14:44:11 -0700768 Args: map[string]string{
769 "objcopyCmd": objcopyCmd,
770 "prefix": prefix,
771 },
772 })
773}
774
Colin Cross635c3b02016-05-18 15:37:25 -0700775func TransformStrip(ctx android.ModuleContext, inputFile android.Path,
776 outputFile android.WritablePath, flags builderFlags) {
Colin Cross665dce92016-04-28 14:50:03 -0700777
778 crossCompile := gccCmd(flags.toolchain, "")
779 args := ""
780 if flags.stripAddGnuDebuglink {
781 args += " --add-gnu-debuglink"
782 }
783 if flags.stripKeepMiniDebugInfo {
784 args += " --keep-mini-debug-info"
785 }
786 if flags.stripKeepSymbols {
787 args += " --keep-symbols"
788 }
789
Colin Cross635c3b02016-05-18 15:37:25 -0700790 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700791 Rule: strip,
792 Description: "strip " + outputFile.Base(),
793 Output: outputFile,
794 Input: inputFile,
Colin Cross665dce92016-04-28 14:50:03 -0700795 Args: map[string]string{
796 "crossCompile": crossCompile,
797 "args": args,
798 },
799 })
800}
801
Colin Cross635c3b02016-05-18 15:37:25 -0700802func TransformDarwinStrip(ctx android.ModuleContext, inputFile android.Path,
803 outputFile android.WritablePath) {
Colin Crossb8ecdfe2016-05-03 15:10:29 -0700804
Colin Cross635c3b02016-05-18 15:37:25 -0700805 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700806 Rule: darwinStrip,
807 Description: "strip " + outputFile.Base(),
808 Output: outputFile,
809 Input: inputFile,
Colin Crossb8ecdfe2016-05-03 15:10:29 -0700810 })
811}
812
Dan Willemsen581341d2017-02-09 16:16:31 -0800813func TransformCoverageFilesToLib(ctx android.ModuleContext,
814 inputs Objects, flags builderFlags, baseName string) android.OptionalPath {
815
816 if len(inputs.coverageFiles) > 0 {
817 outputFile := android.PathForModuleOut(ctx, baseName+".gcnodir")
818
819 TransformObjToStaticLib(ctx, inputs.coverageFiles, flags, outputFile, nil)
820
821 return android.OptionalPathForPath(outputFile)
822 }
823
824 return android.OptionalPath{}
825}
826
Colin Cross635c3b02016-05-18 15:37:25 -0700827func CopyGccLib(ctx android.ModuleContext, libName string,
828 flags builderFlags, outputFile android.WritablePath) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800829
Colin Cross635c3b02016-05-18 15:37:25 -0700830 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700831 Rule: copyGccLib,
832 Description: "copy gcc library " + libName,
833 Output: outputFile,
Colin Cross3f40fa42015-01-30 17:27:36 -0800834 Args: map[string]string{
835 "ccCmd": gccCmd(flags.toolchain, "gcc"),
836 "cFlags": flags.globalFlags,
837 "libName": libName,
838 },
839 })
840}
841
Colin Crossb98c8b02016-07-29 13:44:28 -0700842func gccCmd(toolchain config.Toolchain, cmd string) string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800843 return filepath.Join(toolchain.GccRoot(), "bin", toolchain.GccTriple()+"-"+cmd)
844}
Colin Cross0af4b842015-04-30 16:36:18 -0700845
Colin Cross5b529592017-05-09 13:34:34 -0700846func splitListForSize(list android.Paths, limit int) (lists []android.Paths, err error) {
Colin Cross0af4b842015-04-30 16:36:18 -0700847 var i int
848
849 start := 0
850 bytes := 0
851 for i = range list {
Colin Cross5b529592017-05-09 13:34:34 -0700852 l := len(list[i].String())
Colin Cross0af4b842015-04-30 16:36:18 -0700853 if l > limit {
854 return nil, fmt.Errorf("list element greater than size limit (%d)", limit)
855 }
856 if bytes+l > limit {
857 lists = append(lists, list[start:i])
858 start = i
859 bytes = 0
860 }
861 bytes += l + 1 // count a space between each list element
862 }
863
864 lists = append(lists, list[start:])
865
866 totalLen := 0
867 for _, l := range lists {
868 totalLen += len(l)
869 }
870 if totalLen != len(list) {
871 panic(fmt.Errorf("Failed breaking up list, %d != %d", len(list), totalLen))
872 }
873 return lists, nil
874}