blob: 1d12b5f09077ea068facd32aec8fcc86a24927e6 [file] [log] [blame]
Colin Cross3f40fa42015-01-30 17:27:36 -08001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package cc
16
17// This file generates the final rules for compiling all C/C++. All properties related to
18// compiling should have been translated into builderFlags or another argument to the Transform*
19// functions.
20
21import (
Colin Cross0af4b842015-04-30 16:36:18 -070022 "fmt"
Colin Crossb98c8b02016-07-29 13:44:28 -070023 "path/filepath"
Colin Cross0af4b842015-04-30 16:36:18 -070024 "runtime"
25 "strconv"
Colin Cross3f40fa42015-01-30 17:27:36 -080026 "strings"
Colin Crossed4cf0b2015-03-26 14:43:45 -070027
28 "github.com/google/blueprint"
Colin Crossb98c8b02016-07-29 13:44:28 -070029
30 "android/soong/android"
31 "android/soong/cc/config"
Colin Cross3f40fa42015-01-30 17:27:36 -080032)
33
34const (
Dan Albertc3144b12015-04-28 18:17:56 -070035 objectExtension = ".o"
Colin Cross3f40fa42015-01-30 17:27:36 -080036 staticLibraryExtension = ".a"
37)
38
39var (
Jayant Chowdharya3bb1b32017-11-01 11:12:15 -070040 abiCheckAllowFlags = []string{
Jayant Chowdharya3bb1b32017-11-01 11:12:15 -070041 "-allow-unreferenced-changes",
42 "-allow-unreferenced-elf-symbol-changes",
43 }
44)
45
46var (
Colin Cross635c3b02016-05-18 15:37:25 -070047 pctx = android.NewPackageContext("android/soong/cc")
Colin Cross3f40fa42015-01-30 17:27:36 -080048
Colin Cross9d45bb72016-08-29 16:14:13 -070049 cc = pctx.AndroidGomaStaticRule("cc",
Colin Cross3f40fa42015-01-30 17:27:36 -080050 blueprint.RuleParams{
51 Depfile: "${out}.d",
52 Deps: blueprint.DepsGCC,
Alistair Strachan777475c2016-08-26 12:55:49 -070053 Command: "$relPwd ${config.CcWrapper}$ccCmd -c $cFlags -MD -MF ${out}.d -o $out $in",
Dan Willemsenc94a7682015-11-17 15:27:28 -080054 CommandDeps: []string{"$ccCmd"},
Colin Cross3f40fa42015-01-30 17:27:36 -080055 },
Dan Willemsen322a0a62015-11-17 15:19:46 -080056 "ccCmd", "cFlags")
Colin Cross3f40fa42015-01-30 17:27:36 -080057
Colin Cross9d45bb72016-08-29 16:14:13 -070058 ld = pctx.AndroidStaticRule("ld",
Colin Cross3f40fa42015-01-30 17:27:36 -080059 blueprint.RuleParams{
Dan Albertce2b8392016-07-21 13:16:49 -070060 Command: "$ldCmd ${crtBegin} @${out}.rsp " +
Colin Cross28344522015-04-22 13:07:53 -070061 "${libFlags} ${crtEnd} -o ${out} ${ldFlags}",
Dan Willemsenc94a7682015-11-17 15:27:28 -080062 CommandDeps: []string{"$ldCmd"},
Colin Cross7d21c442015-03-30 17:47:53 -070063 Rspfile: "${out}.rsp",
64 RspfileContent: "${in}",
Colin Cross3f40fa42015-01-30 17:27:36 -080065 },
Dan Albertce2b8392016-07-21 13:16:49 -070066 "ldCmd", "crtBegin", "libFlags", "crtEnd", "ldFlags")
Colin Cross3f40fa42015-01-30 17:27:36 -080067
Colin Cross9d45bb72016-08-29 16:14:13 -070068 partialLd = pctx.AndroidStaticRule("partialLd",
Colin Cross3f40fa42015-01-30 17:27:36 -080069 blueprint.RuleParams{
Colin Cross41280a42015-11-23 14:01:42 -080070 Command: "$ldCmd -nostdlib -Wl,-r ${in} -o ${out} ${ldFlags}",
Dan Willemsenc94a7682015-11-17 15:27:28 -080071 CommandDeps: []string{"$ldCmd"},
Colin Cross3f40fa42015-01-30 17:27:36 -080072 },
Colin Cross41280a42015-11-23 14:01:42 -080073 "ldCmd", "ldFlags")
Colin Cross3f40fa42015-01-30 17:27:36 -080074
Colin Cross9d45bb72016-08-29 16:14:13 -070075 ar = pctx.AndroidStaticRule("ar",
Colin Cross3f40fa42015-01-30 17:27:36 -080076 blueprint.RuleParams{
Colin Cross7d21c442015-03-30 17:47:53 -070077 Command: "rm -f ${out} && $arCmd $arFlags $out @${out}.rsp",
Dan Willemsenc94a7682015-11-17 15:27:28 -080078 CommandDeps: []string{"$arCmd"},
Colin Cross7d21c442015-03-30 17:47:53 -070079 Rspfile: "${out}.rsp",
80 RspfileContent: "${in}",
Colin Cross3f40fa42015-01-30 17:27:36 -080081 },
82 "arCmd", "arFlags")
83
Colin Cross9d45bb72016-08-29 16:14:13 -070084 darwinAr = pctx.AndroidStaticRule("darwinAr",
Colin Cross0af4b842015-04-30 16:36:18 -070085 blueprint.RuleParams{
Colin Crossb98c8b02016-07-29 13:44:28 -070086 Command: "rm -f ${out} && ${config.MacArPath} $arFlags $out $in",
87 CommandDeps: []string{"${config.MacArPath}"},
Colin Cross0af4b842015-04-30 16:36:18 -070088 },
Colin Crossb8ecdfe2016-05-03 15:10:29 -070089 "arFlags")
Colin Cross0af4b842015-04-30 16:36:18 -070090
Colin Cross9d45bb72016-08-29 16:14:13 -070091 darwinAppendAr = pctx.AndroidStaticRule("darwinAppendAr",
Colin Cross0af4b842015-04-30 16:36:18 -070092 blueprint.RuleParams{
Colin Crossb98c8b02016-07-29 13:44:28 -070093 Command: "cp -f ${inAr} ${out}.tmp && ${config.MacArPath} $arFlags ${out}.tmp $in && mv ${out}.tmp ${out}",
94 CommandDeps: []string{"${config.MacArPath}", "${inAr}"},
Colin Cross0af4b842015-04-30 16:36:18 -070095 },
Colin Crossb8ecdfe2016-05-03 15:10:29 -070096 "arFlags", "inAr")
97
Colin Cross9d45bb72016-08-29 16:14:13 -070098 darwinStrip = pctx.AndroidStaticRule("darwinStrip",
Colin Crossb8ecdfe2016-05-03 15:10:29 -070099 blueprint.RuleParams{
Colin Crossa24166b2016-08-01 15:42:38 -0700100 Command: "${config.MacStripPath} -u -r -o $out $in",
101 CommandDeps: []string{"${config.MacStripPath}"},
Colin Crossb8ecdfe2016-05-03 15:10:29 -0700102 })
Colin Cross0af4b842015-04-30 16:36:18 -0700103
Colin Cross9d45bb72016-08-29 16:14:13 -0700104 prefixSymbols = pctx.AndroidStaticRule("prefixSymbols",
Colin Crossbfae8852015-03-26 14:44:11 -0700105 blueprint.RuleParams{
106 Command: "$objcopyCmd --prefix-symbols=${prefix} ${in} ${out}",
Dan Willemsenc94a7682015-11-17 15:27:28 -0800107 CommandDeps: []string{"$objcopyCmd"},
Colin Crossbfae8852015-03-26 14:44:11 -0700108 },
109 "objcopyCmd", "prefix")
110
Nan Zhang43a485c2017-03-27 14:27:58 -0700111 _ = pctx.SourcePathVariable("stripPath", "build/soong/scripts/strip.sh")
Colin Cross665dce92016-04-28 14:50:03 -0700112
Colin Cross9d45bb72016-08-29 16:14:13 -0700113 strip = pctx.AndroidStaticRule("strip",
Colin Cross665dce92016-04-28 14:50:03 -0700114 blueprint.RuleParams{
115 Depfile: "${out}.d",
116 Deps: blueprint.DepsGCC,
117 Command: "CROSS_COMPILE=$crossCompile $stripPath ${args} -i ${in} -o ${out} -d ${out}.d",
118 CommandDeps: []string{"$stripPath"},
Colin Cross665dce92016-04-28 14:50:03 -0700119 },
120 "args", "crossCompile")
121
Colin Cross9d45bb72016-08-29 16:14:13 -0700122 emptyFile = pctx.AndroidStaticRule("emptyFile",
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700123 blueprint.RuleParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700124 Command: "rm -f $out && touch $out",
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700125 })
126
Nan Zhang43a485c2017-03-27 14:27:58 -0700127 _ = pctx.SourcePathVariable("copyGccLibPath", "build/soong/scripts/copygcclib.sh")
Colin Cross3f40fa42015-01-30 17:27:36 -0800128
Colin Cross9d45bb72016-08-29 16:14:13 -0700129 copyGccLib = pctx.AndroidStaticRule("copyGccLib",
Colin Cross3f40fa42015-01-30 17:27:36 -0800130 blueprint.RuleParams{
131 Depfile: "${out}.d",
132 Deps: blueprint.DepsGCC,
133 Command: "$copyGccLibPath $out $ccCmd $cFlags -print-file-name=${libName}",
Dan Willemsenc94a7682015-11-17 15:27:28 -0800134 CommandDeps: []string{"$copyGccLibPath", "$ccCmd"},
Colin Cross3f40fa42015-01-30 17:27:36 -0800135 },
136 "ccCmd", "cFlags", "libName")
Colin Cross26c34ed2016-09-30 17:10:16 -0700137
Nan Zhang43a485c2017-03-27 14:27:58 -0700138 _ = pctx.SourcePathVariable("tocPath", "build/soong/scripts/toc.sh")
Colin Cross26c34ed2016-09-30 17:10:16 -0700139
140 toc = pctx.AndroidStaticRule("toc",
141 blueprint.RuleParams{
142 Depfile: "${out}.d",
143 Deps: blueprint.DepsGCC,
144 Command: "CROSS_COMPILE=$crossCompile $tocPath -i ${in} -o ${out} -d ${out}.d",
145 CommandDeps: []string{"$tocPath"},
146 Restat: true,
147 },
148 "crossCompile")
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700149
150 clangTidy = pctx.AndroidStaticRule("clangTidy",
151 blueprint.RuleParams{
152 Command: "rm -f $out && ${config.ClangBin}/clang-tidy $tidyFlags $in -- $cFlags && touch $out",
153 CommandDeps: []string{"${config.ClangBin}/clang-tidy"},
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700154 },
155 "cFlags", "tidyFlags")
Colin Cross91e90042016-12-02 17:13:24 -0800156
Nan Zhang43a485c2017-03-27 14:27:58 -0700157 _ = pctx.SourcePathVariable("yasmCmd", "prebuilts/misc/${config.HostPrebuiltTag}/yasm/yasm")
Colin Cross91e90042016-12-02 17:13:24 -0800158
159 yasm = pctx.AndroidStaticRule("yasm",
160 blueprint.RuleParams{
Dan Willemsen1d3e5452017-08-22 20:53:45 -0700161 Command: "$yasmCmd $asFlags -o $out $in && $yasmCmd $asFlags -M $in >$out.d",
Colin Cross91e90042016-12-02 17:13:24 -0800162 CommandDeps: []string{"$yasmCmd"},
Dan Willemsen1d3e5452017-08-22 20:53:45 -0700163 Depfile: "$out.d",
164 Deps: blueprint.DepsGCC,
Colin Cross91e90042016-12-02 17:13:24 -0800165 },
166 "asFlags")
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800167
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700168 windres = pctx.AndroidStaticRule("windres",
169 blueprint.RuleParams{
170 Command: "$windresCmd $flags -I$$(dirname $in) -i $in -o $out",
171 CommandDeps: []string{"$windresCmd"},
172 },
173 "windresCmd", "flags")
174
Jayant Chowdharya4c6df52018-02-20 12:36:51 -0800175 _ = pctx.SourcePathVariable("sAbiDumper", "prebuilts/clang-tools/${config.HostPrebuiltTag}/bin/header-abi-dumper")
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800176
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700177 // -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 -0800178 sAbiDump = pctx.AndroidStaticRule("sAbiDump",
179 blueprint.RuleParams{
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700180 Command: "rm -f $out && $sAbiDumper -o ${out} $in $exportDirs -- $cFlags -w -isystem ${config.RSIncludePath}",
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800181 CommandDeps: []string{"$sAbiDumper"},
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800182 },
183 "cFlags", "exportDirs")
184
Jayant Chowdharya4c6df52018-02-20 12:36:51 -0800185 _ = pctx.SourcePathVariable("sAbiLinker", "prebuilts/clang-tools/${config.HostPrebuiltTag}/bin/header-abi-linker")
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800186
187 sAbiLink = pctx.AndroidStaticRule("sAbiLink",
188 blueprint.RuleParams{
Jayant Chowdharydf344d52018-01-17 11:11:42 -0800189 Command: "$sAbiLinker -o ${out} $symbolFilter -arch $arch $exportedHeaderFlags @${out}.rsp ",
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800190 CommandDeps: []string{"$sAbiLinker"},
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800191 Rspfile: "${out}.rsp",
192 RspfileContent: "${in}",
193 },
Jayant Chowdharydf344d52018-01-17 11:11:42 -0800194 "symbolFilter", "arch", "exportedHeaderFlags")
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800195
Jayant Chowdharya4c6df52018-02-20 12:36:51 -0800196 _ = pctx.SourcePathVariable("sAbiDiffer", "prebuilts/clang-tools/${config.HostPrebuiltTag}/bin/header-abi-diff")
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700197
Jayant Chowdhary219139d2017-11-27 14:52:21 -0800198 sAbiDiff = pctx.AndroidRuleFunc("sAbiDiff",
199 func(config android.Config) (blueprint.RuleParams, error) {
200
201 commandStr := "($sAbiDiffer $allowFlags -lib $libName -arch $arch -check-all-apis -o ${out} -new $in -old $referenceDump)"
202 distDir := config.ProductVariables.DistDir
203 if distDir != nil && *distDir != "" {
204 distAbiDiffDir := *distDir + "/abidiffs/"
205 commandStr += " || (mkdir -p " + distAbiDiffDir + " && cp ${out} " + distAbiDiffDir + " && exit 1)"
206 }
207 return blueprint.RuleParams{
208 Command: commandStr,
209 CommandDeps: []string{"$sAbiDiffer"},
210 }, nil
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800211 },
Jayant Chowdharya3bb1b32017-11-01 11:12:15 -0700212 "allowFlags", "referenceDump", "libName", "arch")
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700213
214 unzipRefSAbiDump = pctx.AndroidStaticRule("unzipRefSAbiDump",
215 blueprint.RuleParams{
216 Command: "gunzip -c $in > $out",
217 })
Colin Cross3f40fa42015-01-30 17:27:36 -0800218)
219
Dan Willemsen322a0a62015-11-17 15:19:46 -0800220func init() {
221 // We run gcc/clang with PWD=/proc/self/cwd to remove $TOP from the
222 // debug output. That way two builds in two different directories will
223 // create the same output.
224 if runtime.GOOS != "darwin" {
225 pctx.StaticVariable("relPwd", "PWD=/proc/self/cwd")
226 } else {
227 // Darwin doesn't have /proc
228 pctx.StaticVariable("relPwd", "")
229 }
230}
231
Colin Cross3f40fa42015-01-30 17:27:36 -0800232type builderFlags struct {
Joe Onorato09e94ab2017-11-18 18:23:14 -0800233 globalFlags string
234 arFlags string
235 asFlags string
236 cFlags string
237 toolingCFlags string // A separate set of Cflags for clang LibTooling tools
238 conlyFlags string
239 cppFlags string
240 ldFlags string
241 libFlags string
242 yaccFlags string
243 protoFlags string
244 protoOutParams string
245 tidyFlags string
246 sAbiFlags string
247 yasmFlags string
248 aidlFlags string
249 rsFlags string
250 toolchain config.Toolchain
251 clang bool
252 tidy bool
253 coverage bool
254 sAbiDump bool
Dan Willemsenab9f4262018-02-14 13:58:34 -0800255 protoRoot bool
Colin Cross665dce92016-04-28 14:50:03 -0700256
Colin Crossc3199482017-03-30 15:03:04 -0700257 systemIncludeFlags string
258
Colin Cross18c0c5a2016-12-01 14:45:23 -0800259 groupStaticLibs bool
Zhizhou Yang51be6322018-02-08 18:32:11 -0800260 arGoldPlugin bool
Colin Cross18c0c5a2016-12-01 14:45:23 -0800261
Colin Cross665dce92016-04-28 14:50:03 -0700262 stripKeepSymbols bool
263 stripKeepMiniDebugInfo bool
264 stripAddGnuDebuglink bool
Colin Cross3f40fa42015-01-30 17:27:36 -0800265}
266
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700267type Objects struct {
Dan Willemsen581341d2017-02-09 16:16:31 -0800268 objFiles android.Paths
269 tidyFiles android.Paths
270 coverageFiles android.Paths
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800271 sAbiDumpFiles android.Paths
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700272}
273
274func (a Objects) Copy() Objects {
275 return Objects{
Dan Willemsen581341d2017-02-09 16:16:31 -0800276 objFiles: append(android.Paths{}, a.objFiles...),
277 tidyFiles: append(android.Paths{}, a.tidyFiles...),
278 coverageFiles: append(android.Paths{}, a.coverageFiles...),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800279 sAbiDumpFiles: append(android.Paths{}, a.sAbiDumpFiles...),
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700280 }
281}
282
283func (a Objects) Append(b Objects) Objects {
284 return Objects{
Dan Willemsen581341d2017-02-09 16:16:31 -0800285 objFiles: append(a.objFiles, b.objFiles...),
286 tidyFiles: append(a.tidyFiles, b.tidyFiles...),
287 coverageFiles: append(a.coverageFiles, b.coverageFiles...),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800288 sAbiDumpFiles: append(a.sAbiDumpFiles, b.sAbiDumpFiles...),
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700289 }
290}
291
Colin Cross3f40fa42015-01-30 17:27:36 -0800292// Generate rules for compiling multiple .c, .cpp, or .S files to individual .o files
Colin Cross635c3b02016-05-18 15:37:25 -0700293func TransformSourceToObj(ctx android.ModuleContext, subdir string, srcFiles android.Paths,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800294 flags builderFlags, pathDeps android.Paths, cFlagsDeps android.Paths) Objects {
Colin Cross581c1892015-04-07 16:50:10 -0700295
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700296 objFiles := make(android.Paths, len(srcFiles))
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700297 var tidyFiles android.Paths
298 if flags.tidy && flags.clang {
299 tidyFiles = make(android.Paths, 0, len(srcFiles))
300 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800301 var coverageFiles android.Paths
302 if flags.coverage {
303 coverageFiles = make(android.Paths, 0, len(srcFiles))
304 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800305
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700306 commonFlags := strings.Join([]string{
Colin Crossc3199482017-03-30 15:03:04 -0700307 flags.globalFlags,
308 flags.systemIncludeFlags,
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700309 }, " ")
310
311 toolingCflags := strings.Join([]string{
312 commonFlags,
313 flags.toolingCFlags,
314 flags.conlyFlags,
315 }, " ")
316
317 cflags := strings.Join([]string{
318 commonFlags,
Colin Crossc3199482017-03-30 15:03:04 -0700319 flags.cFlags,
320 flags.conlyFlags,
321 }, " ")
322
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700323 toolingCppflags := strings.Join([]string{
324 commonFlags,
325 flags.toolingCFlags,
326 flags.cppFlags,
327 }, " ")
328
Colin Crossc3199482017-03-30 15:03:04 -0700329 cppflags := strings.Join([]string{
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700330 commonFlags,
Colin Crossc3199482017-03-30 15:03:04 -0700331 flags.cFlags,
332 flags.cppFlags,
333 }, " ")
334
335 asflags := strings.Join([]string{
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700336 commonFlags,
Colin Crossc3199482017-03-30 15:03:04 -0700337 flags.asFlags,
338 }, " ")
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700339
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800340 var sAbiDumpFiles android.Paths
341 if flags.sAbiDump && flags.clang {
342 sAbiDumpFiles = make(android.Paths, 0, len(srcFiles))
343 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800344
Dan Willemsenbe03f342016-03-03 17:21:04 -0800345 if flags.clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700346 cflags += " ${config.NoOverrideClangGlobalCflags}"
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700347 toolingCflags += " ${config.NoOverrideClangGlobalCflags}"
Colin Crossb98c8b02016-07-29 13:44:28 -0700348 cppflags += " ${config.NoOverrideClangGlobalCflags}"
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700349 toolingCppflags += " ${config.NoOverrideClangGlobalCflags}"
Dan Willemsenbe03f342016-03-03 17:21:04 -0800350 } else {
Colin Crossb98c8b02016-07-29 13:44:28 -0700351 cflags += " ${config.NoOverrideGlobalCflags}"
352 cppflags += " ${config.NoOverrideGlobalCflags}"
Dan Willemsenbe03f342016-03-03 17:21:04 -0800353 }
354
Colin Cross3f40fa42015-01-30 17:27:36 -0800355 for i, srcFile := range srcFiles {
Dan Willemsen21ec4902016-11-02 20:43:13 -0700356 objFile := android.ObjPathWithExt(ctx, subdir, srcFile, "o")
Colin Cross3f40fa42015-01-30 17:27:36 -0800357
358 objFiles[i] = objFile
359
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700360 switch srcFile.Ext() {
361 case ".asm":
Colin Crossae887032017-10-23 17:16:14 -0700362 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700363 Rule: yasm,
364 Description: "yasm " + srcFile.Rel(),
365 Output: objFile,
366 Input: srcFile,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800367 Implicits: cFlagsDeps,
368 OrderOnly: pathDeps,
Colin Cross91e90042016-12-02 17:13:24 -0800369 Args: map[string]string{
370 "asFlags": flags.yasmFlags,
371 },
372 })
373 continue
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700374 case ".rc":
Colin Crossae887032017-10-23 17:16:14 -0700375 ctx.Build(pctx, android.BuildParams{
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700376 Rule: windres,
377 Description: "windres " + srcFile.Rel(),
378 Output: objFile,
379 Input: srcFile,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800380 Implicits: cFlagsDeps,
381 OrderOnly: pathDeps,
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700382 Args: map[string]string{
383 "windresCmd": gccCmd(flags.toolchain, "windres"),
384 "flags": flags.toolchain.WindresFlags(),
385 },
386 })
387 continue
Colin Cross91e90042016-12-02 17:13:24 -0800388 }
389
Colin Cross3f40fa42015-01-30 17:27:36 -0800390 var moduleCflags string
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700391 var moduleToolingCflags string
Colin Cross3f40fa42015-01-30 17:27:36 -0800392 var ccCmd string
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700393 tidy := flags.tidy && flags.clang
Dan Willemsen581341d2017-02-09 16:16:31 -0800394 coverage := flags.coverage
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800395 dump := flags.sAbiDump && flags.clang
Colin Cross3f40fa42015-01-30 17:27:36 -0800396
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700397 switch srcFile.Ext() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800398 case ".S", ".s":
399 ccCmd = "gcc"
400 moduleCflags = asflags
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700401 tidy = false
Dan Willemsen581341d2017-02-09 16:16:31 -0800402 coverage = false
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800403 dump = false
Colin Cross3f40fa42015-01-30 17:27:36 -0800404 case ".c":
405 ccCmd = "gcc"
406 moduleCflags = cflags
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700407 moduleToolingCflags = toolingCflags
Colin Cross9978ffe2016-12-01 15:31:22 -0800408 case ".cpp", ".cc", ".mm":
Colin Cross3f40fa42015-01-30 17:27:36 -0800409 ccCmd = "g++"
410 moduleCflags = cppflags
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700411 moduleToolingCflags = toolingCppflags
Colin Cross3f40fa42015-01-30 17:27:36 -0800412 default:
413 ctx.ModuleErrorf("File %s has unknown extension", srcFile)
414 continue
415 }
416
417 if flags.clang {
418 switch ccCmd {
419 case "gcc":
420 ccCmd = "clang"
421 case "g++":
422 ccCmd = "clang++"
423 default:
424 panic("unrecoginzied ccCmd")
425 }
Colin Cross67a5c132017-05-09 13:45:28 -0700426 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800427
Colin Cross67a5c132017-05-09 13:45:28 -0700428 ccDesc := ccCmd
429
430 if flags.clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700431 ccCmd = "${config.ClangBin}/" + ccCmd
Colin Cross3f40fa42015-01-30 17:27:36 -0800432 } else {
433 ccCmd = gccCmd(flags.toolchain, ccCmd)
434 }
435
Dan Willemsen581341d2017-02-09 16:16:31 -0800436 var implicitOutputs android.WritablePaths
437 if coverage {
438 gcnoFile := android.ObjPathWithExt(ctx, subdir, srcFile, "gcno")
439 implicitOutputs = append(implicitOutputs, gcnoFile)
440 coverageFiles = append(coverageFiles, gcnoFile)
441 }
442
Colin Crossae887032017-10-23 17:16:14 -0700443 ctx.Build(pctx, android.BuildParams{
Dan Willemsen581341d2017-02-09 16:16:31 -0800444 Rule: cc,
Colin Cross67a5c132017-05-09 13:45:28 -0700445 Description: ccDesc + " " + srcFile.Rel(),
Dan Willemsen581341d2017-02-09 16:16:31 -0800446 Output: objFile,
447 ImplicitOutputs: implicitOutputs,
448 Input: srcFile,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800449 Implicits: cFlagsDeps,
450 OrderOnly: pathDeps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800451 Args: map[string]string{
Colin Cross28344522015-04-22 13:07:53 -0700452 "cFlags": moduleCflags,
453 "ccCmd": ccCmd,
Colin Cross3f40fa42015-01-30 17:27:36 -0800454 },
455 })
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700456
457 if tidy {
Dan Willemsen21ec4902016-11-02 20:43:13 -0700458 tidyFile := android.ObjPathWithExt(ctx, subdir, srcFile, "tidy")
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700459 tidyFiles = append(tidyFiles, tidyFile)
460
Colin Crossae887032017-10-23 17:16:14 -0700461 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700462 Rule: clangTidy,
463 Description: "clang-tidy " + srcFile.Rel(),
464 Output: tidyFile,
465 Input: srcFile,
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700466 // We must depend on objFile, since clang-tidy doesn't
467 // support exporting dependencies.
468 Implicit: objFile,
469 Args: map[string]string{
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700470 "cFlags": moduleToolingCflags,
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700471 "tidyFlags": flags.tidyFlags,
472 },
473 })
474 }
475
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800476 if dump {
477 sAbiDumpFile := android.ObjPathWithExt(ctx, subdir, srcFile, "sdump")
478 sAbiDumpFiles = append(sAbiDumpFiles, sAbiDumpFile)
479
Colin Crossae887032017-10-23 17:16:14 -0700480 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700481 Rule: sAbiDump,
482 Description: "header-abi-dumper " + srcFile.Rel(),
483 Output: sAbiDumpFile,
484 Input: srcFile,
485 Implicit: objFile,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800486 Args: map[string]string{
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700487 "cFlags": moduleToolingCflags,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800488 "exportDirs": flags.sAbiFlags,
489 },
490 })
491 }
492
Colin Cross3f40fa42015-01-30 17:27:36 -0800493 }
494
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700495 return Objects{
Dan Willemsen581341d2017-02-09 16:16:31 -0800496 objFiles: objFiles,
497 tidyFiles: tidyFiles,
498 coverageFiles: coverageFiles,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800499 sAbiDumpFiles: sAbiDumpFiles,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700500 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800501}
502
503// Generate a rule for compiling multiple .o files to a static library (.a)
Colin Cross635c3b02016-05-18 15:37:25 -0700504func TransformObjToStaticLib(ctx android.ModuleContext, objFiles android.Paths,
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700505 flags builderFlags, outputFile android.ModuleOutPath, deps android.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800506
Dan Willemsen581341d2017-02-09 16:16:31 -0800507 if ctx.Darwin() {
508 transformDarwinObjToStaticLib(ctx, objFiles, flags, outputFile, deps)
509 return
510 }
511
Stephen Hinesf1addeb2018-01-09 23:29:04 -0800512 arCmd := "${config.ClangBin}/llvm-ar"
513 arFlags := "crsD"
514 if !ctx.Darwin() {
515 arFlags += " -format=gnu"
516 }
Zhizhou Yang51be6322018-02-08 18:32:11 -0800517 if flags.arGoldPlugin {
518 arFlags += " --plugin ${config.LLVMGoldPlugin}"
519 }
Vishwath Mohan83d9f712017-03-16 11:01:23 -0700520 if flags.arFlags != "" {
521 arFlags += " " + flags.arFlags
522 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800523
Colin Crossae887032017-10-23 17:16:14 -0700524 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700525 Rule: ar,
526 Description: "static link " + outputFile.Base(),
527 Output: outputFile,
528 Inputs: objFiles,
529 Implicits: deps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800530 Args: map[string]string{
531 "arFlags": arFlags,
532 "arCmd": arCmd,
533 },
534 })
535}
536
Colin Cross0af4b842015-04-30 16:36:18 -0700537// Generate a rule for compiling multiple .o files to a static library (.a) on
538// darwin. The darwin ar tool doesn't support @file for list files, and has a
539// very small command line length limit, so we have to split the ar into multiple
540// steps, each appending to the previous one.
Dan Willemsen581341d2017-02-09 16:16:31 -0800541func transformDarwinObjToStaticLib(ctx android.ModuleContext, objFiles android.Paths,
Colin Cross5b529592017-05-09 13:34:34 -0700542 flags builderFlags, outputFile android.ModuleOutPath, deps android.Paths) {
Colin Cross0af4b842015-04-30 16:36:18 -0700543
Colin Cross0af4b842015-04-30 16:36:18 -0700544 arFlags := "cqs"
545
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700546 if len(objFiles) == 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700547 dummy := android.PathForModuleOut(ctx, "dummy"+objectExtension)
548 dummyAr := android.PathForModuleOut(ctx, "dummy"+staticLibraryExtension)
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700549
Colin Crossae887032017-10-23 17:16:14 -0700550 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700551 Rule: emptyFile,
552 Description: "empty object file",
553 Output: dummy,
554 Implicits: deps,
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700555 })
556
Colin Crossae887032017-10-23 17:16:14 -0700557 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700558 Rule: darwinAr,
559 Description: "empty static archive",
560 Output: dummyAr,
561 Input: dummy,
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700562 Args: map[string]string{
563 "arFlags": arFlags,
564 },
565 })
566
Colin Crossae887032017-10-23 17:16:14 -0700567 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700568 Rule: darwinAppendAr,
569 Description: "static link " + outputFile.Base(),
570 Output: outputFile,
571 Input: dummy,
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700572 Args: map[string]string{
573 "arFlags": "d",
574 "inAr": dummyAr.String(),
575 },
576 })
577
578 return
579 }
580
Colin Cross0af4b842015-04-30 16:36:18 -0700581 // ARG_MAX on darwin is 262144, use half that to be safe
Colin Cross5b529592017-05-09 13:34:34 -0700582 objFilesLists, err := splitListForSize(objFiles, 131072)
Colin Cross0af4b842015-04-30 16:36:18 -0700583 if err != nil {
584 ctx.ModuleErrorf("%s", err.Error())
585 }
586
Colin Cross5b529592017-05-09 13:34:34 -0700587 var in, out android.WritablePath
Colin Cross0af4b842015-04-30 16:36:18 -0700588 for i, l := range objFilesLists {
589 in = out
590 out = outputFile
591 if i != len(objFilesLists)-1 {
Colin Cross5b529592017-05-09 13:34:34 -0700592 out = android.PathForModuleOut(ctx, outputFile.Base()+strconv.Itoa(i))
Colin Cross0af4b842015-04-30 16:36:18 -0700593 }
594
Colin Crossae887032017-10-23 17:16:14 -0700595 build := android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700596 Rule: darwinAr,
597 Description: "static link " + out.Base(),
598 Output: out,
599 Inputs: l,
600 Implicits: deps,
Colin Cross5b529592017-05-09 13:34:34 -0700601 Args: map[string]string{
602 "arFlags": arFlags,
603 },
Colin Cross0af4b842015-04-30 16:36:18 -0700604 }
Colin Cross5b529592017-05-09 13:34:34 -0700605 if i != 0 {
606 build.Rule = darwinAppendAr
607 build.Args["inAr"] = in.String()
608 }
Colin Crossae887032017-10-23 17:16:14 -0700609 ctx.Build(pctx, build)
Colin Cross0af4b842015-04-30 16:36:18 -0700610 }
611}
612
Colin Cross3f40fa42015-01-30 17:27:36 -0800613// Generate a rule for compiling multiple .o files, plus static libraries, whole static libraries,
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700614// and shared libraries, to a shared library (.so) or dynamic executable
Colin Cross635c3b02016-05-18 15:37:25 -0700615func TransformObjToDynamicBinary(ctx android.ModuleContext,
616 objFiles, sharedLibs, staticLibs, lateStaticLibs, wholeStaticLibs, deps android.Paths,
617 crtBegin, crtEnd android.OptionalPath, groupLate bool, flags builderFlags, outputFile android.WritablePath) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800618
619 var ldCmd string
620 if flags.clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700621 ldCmd = "${config.ClangBin}/clang++"
Colin Cross3f40fa42015-01-30 17:27:36 -0800622 } else {
623 ldCmd = gccCmd(flags.toolchain, "g++")
624 }
625
Colin Cross3f40fa42015-01-30 17:27:36 -0800626 var libFlagsList []string
627
Colin Cross16b23492016-01-06 14:41:07 -0800628 if len(flags.libFlags) > 0 {
629 libFlagsList = append(libFlagsList, flags.libFlags)
630 }
631
Colin Cross3f40fa42015-01-30 17:27:36 -0800632 if len(wholeStaticLibs) > 0 {
Dan Willemsen490fd492015-11-24 17:53:15 -0800633 if ctx.Host() && ctx.Darwin() {
Colin Cross635c3b02016-05-18 15:37:25 -0700634 libFlagsList = append(libFlagsList, android.JoinWithPrefix(wholeStaticLibs.Strings(), "-force_load "))
Colin Cross0af4b842015-04-30 16:36:18 -0700635 } else {
636 libFlagsList = append(libFlagsList, "-Wl,--whole-archive ")
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700637 libFlagsList = append(libFlagsList, wholeStaticLibs.Strings()...)
Colin Cross0af4b842015-04-30 16:36:18 -0700638 libFlagsList = append(libFlagsList, "-Wl,--no-whole-archive ")
639 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800640 }
641
Colin Cross7a7cf972016-12-05 18:47:39 -0800642 if flags.groupStaticLibs && !ctx.Darwin() && len(staticLibs) > 0 {
Colin Cross18c0c5a2016-12-01 14:45:23 -0800643 libFlagsList = append(libFlagsList, "-Wl,--start-group")
644 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700645 libFlagsList = append(libFlagsList, staticLibs.Strings()...)
Colin Cross7a7cf972016-12-05 18:47:39 -0800646 if flags.groupStaticLibs && !ctx.Darwin() && len(staticLibs) > 0 {
Colin Cross18c0c5a2016-12-01 14:45:23 -0800647 libFlagsList = append(libFlagsList, "-Wl,--end-group")
648 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800649
Stephen Hines10347862016-07-18 15:54:54 -0700650 if groupLate && !ctx.Darwin() && len(lateStaticLibs) > 0 {
Dan Willemsenedc385f2015-07-08 13:02:23 -0700651 libFlagsList = append(libFlagsList, "-Wl,--start-group")
652 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700653 libFlagsList = append(libFlagsList, lateStaticLibs.Strings()...)
Stephen Hines10347862016-07-18 15:54:54 -0700654 if groupLate && !ctx.Darwin() && len(lateStaticLibs) > 0 {
Dan Willemsenedc385f2015-07-08 13:02:23 -0700655 libFlagsList = append(libFlagsList, "-Wl,--end-group")
656 }
657
Colin Cross3f40fa42015-01-30 17:27:36 -0800658 for _, lib := range sharedLibs {
Dan Albert9840e1b2016-07-21 08:47:33 -0700659 libFlagsList = append(libFlagsList, lib.String())
Colin Cross3f40fa42015-01-30 17:27:36 -0800660 }
661
Colin Cross3f40fa42015-01-30 17:27:36 -0800662 deps = append(deps, staticLibs...)
Colin Cross3075ad02015-03-17 10:47:08 -0700663 deps = append(deps, lateStaticLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800664 deps = append(deps, wholeStaticLibs...)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700665 if crtBegin.Valid() {
666 deps = append(deps, crtBegin.Path(), crtEnd.Path())
Colin Cross3f40fa42015-01-30 17:27:36 -0800667 }
668
Colin Crossae887032017-10-23 17:16:14 -0700669 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700670 Rule: ld,
671 Description: "link " + outputFile.Base(),
672 Output: outputFile,
673 Inputs: objFiles,
674 Implicits: deps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800675 Args: map[string]string{
Dan Albertce2b8392016-07-21 13:16:49 -0700676 "ldCmd": ldCmd,
677 "crtBegin": crtBegin.String(),
678 "libFlags": strings.Join(libFlagsList, " "),
679 "ldFlags": flags.ldFlags,
680 "crtEnd": crtEnd.String(),
Colin Cross3f40fa42015-01-30 17:27:36 -0800681 },
682 })
683}
684
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800685// Generate a rule to combine .dump sAbi dump files from multiple source files
686// into a single .ldump sAbi dump file
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700687func TransformDumpToLinkedDump(ctx android.ModuleContext, sAbiDumps android.Paths, soFile android.Path,
Jayant Chowdharydf344d52018-01-17 11:11:42 -0800688 baseName, exportedHeaderFlags string) android.OptionalPath {
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800689 outputFile := android.PathForModuleOut(ctx, baseName+".lsdump")
Jayant Chowdharydf344d52018-01-17 11:11:42 -0800690 symbolFilterStr := "-so " + soFile.String()
Colin Crossae887032017-10-23 17:16:14 -0700691 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700692 Rule: sAbiLink,
693 Description: "header-abi-linker " + outputFile.Base(),
694 Output: outputFile,
695 Inputs: sAbiDumps,
Jayant Chowdharydf344d52018-01-17 11:11:42 -0800696 Implicit: soFile,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800697 Args: map[string]string{
Jayant Chowdharydf344d52018-01-17 11:11:42 -0800698 "symbolFilter": symbolFilterStr,
699 "arch": ctx.Arch().ArchType.Name,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800700 "exportedHeaderFlags": exportedHeaderFlags,
701 },
702 })
703 return android.OptionalPathForPath(outputFile)
704}
705
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700706func UnzipRefDump(ctx android.ModuleContext, zippedRefDump android.Path, baseName string) android.Path {
707 outputFile := android.PathForModuleOut(ctx, baseName+"_ref.lsdump")
Colin Crossae887032017-10-23 17:16:14 -0700708 ctx.Build(pctx, android.BuildParams{
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700709 Rule: unzipRefSAbiDump,
710 Description: "gunzip" + outputFile.Base(),
711 Output: outputFile,
712 Input: zippedRefDump,
713 })
714 return outputFile
715}
716
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800717func SourceAbiDiff(ctx android.ModuleContext, inputDump android.Path, referenceDump android.Path,
Logan Chienf3511742017-10-31 18:04:35 +0800718 baseName, exportedHeaderFlags string, isVndkExt bool) android.OptionalPath {
719
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800720 outputFile := android.PathForModuleOut(ctx, baseName+".abidiff")
Logan Chienf3511742017-10-31 18:04:35 +0800721
Jayant Chowdharye4499502018-01-17 13:13:33 -0800722 localAbiCheckAllowFlags := append([]string(nil), abiCheckAllowFlags...)
723 if exportedHeaderFlags == "" {
724 localAbiCheckAllowFlags = append(localAbiCheckAllowFlags, "-advice-only")
725 }
Logan Chienf3511742017-10-31 18:04:35 +0800726 if isVndkExt {
727 localAbiCheckAllowFlags = append(localAbiCheckAllowFlags, "-allow-extensions")
728 }
729
Colin Crossae887032017-10-23 17:16:14 -0700730 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700731 Rule: sAbiDiff,
732 Description: "header-abi-diff " + outputFile.Base(),
733 Output: outputFile,
734 Input: inputDump,
735 Implicit: referenceDump,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800736 Args: map[string]string{
737 "referenceDump": referenceDump.String(),
Jayant Chowdharyf54e0a72017-05-22 10:53:24 -0700738 "libName": baseName,
739 "arch": ctx.Arch().ArchType.Name,
Jayant Chowdharye4499502018-01-17 13:13:33 -0800740 "allowFlags": strings.Join(localAbiCheckAllowFlags, " "),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800741 },
742 })
743 return android.OptionalPathForPath(outputFile)
744}
745
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700746// Generate a rule for extracting a table of contents from a shared library (.so)
747func TransformSharedObjectToToc(ctx android.ModuleContext, inputFile android.Path,
Colin Cross26c34ed2016-09-30 17:10:16 -0700748 outputFile android.WritablePath, flags builderFlags) {
749
750 crossCompile := gccCmd(flags.toolchain, "")
751
Colin Crossae887032017-10-23 17:16:14 -0700752 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700753 Rule: toc,
754 Description: "generate toc " + inputFile.Base(),
755 Output: outputFile,
756 Input: inputFile,
Colin Cross26c34ed2016-09-30 17:10:16 -0700757 Args: map[string]string{
758 "crossCompile": crossCompile,
759 },
760 })
761}
762
Colin Cross3f40fa42015-01-30 17:27:36 -0800763// Generate a rule for compiling multiple .o files to a .o using ld partial linking
Colin Cross635c3b02016-05-18 15:37:25 -0700764func TransformObjsToObj(ctx android.ModuleContext, objFiles android.Paths,
765 flags builderFlags, outputFile android.WritablePath) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800766
Colin Cross41280a42015-11-23 14:01:42 -0800767 var ldCmd string
768 if flags.clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700769 ldCmd = "${config.ClangBin}/clang++"
Colin Cross41280a42015-11-23 14:01:42 -0800770 } else {
771 ldCmd = gccCmd(flags.toolchain, "g++")
772 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800773
Colin Crossae887032017-10-23 17:16:14 -0700774 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700775 Rule: partialLd,
776 Description: "link " + outputFile.Base(),
777 Output: outputFile,
778 Inputs: objFiles,
Colin Cross3f40fa42015-01-30 17:27:36 -0800779 Args: map[string]string{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700780 "ldCmd": ldCmd,
Colin Cross41280a42015-11-23 14:01:42 -0800781 "ldFlags": flags.ldFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800782 },
783 })
784}
785
Colin Crossbfae8852015-03-26 14:44:11 -0700786// Generate a rule for runing objcopy --prefix-symbols on a binary
Colin Cross635c3b02016-05-18 15:37:25 -0700787func TransformBinaryPrefixSymbols(ctx android.ModuleContext, prefix string, inputFile android.Path,
788 flags builderFlags, outputFile android.WritablePath) {
Colin Crossbfae8852015-03-26 14:44:11 -0700789
790 objcopyCmd := gccCmd(flags.toolchain, "objcopy")
791
Colin Crossae887032017-10-23 17:16:14 -0700792 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700793 Rule: prefixSymbols,
794 Description: "prefix symbols " + outputFile.Base(),
795 Output: outputFile,
796 Input: inputFile,
Colin Crossbfae8852015-03-26 14:44:11 -0700797 Args: map[string]string{
798 "objcopyCmd": objcopyCmd,
799 "prefix": prefix,
800 },
801 })
802}
803
Colin Cross635c3b02016-05-18 15:37:25 -0700804func TransformStrip(ctx android.ModuleContext, inputFile android.Path,
805 outputFile android.WritablePath, flags builderFlags) {
Colin Cross665dce92016-04-28 14:50:03 -0700806
807 crossCompile := gccCmd(flags.toolchain, "")
808 args := ""
809 if flags.stripAddGnuDebuglink {
810 args += " --add-gnu-debuglink"
811 }
812 if flags.stripKeepMiniDebugInfo {
813 args += " --keep-mini-debug-info"
814 }
815 if flags.stripKeepSymbols {
816 args += " --keep-symbols"
817 }
818
Colin Crossae887032017-10-23 17:16:14 -0700819 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700820 Rule: strip,
821 Description: "strip " + outputFile.Base(),
822 Output: outputFile,
823 Input: inputFile,
Colin Cross665dce92016-04-28 14:50:03 -0700824 Args: map[string]string{
825 "crossCompile": crossCompile,
826 "args": args,
827 },
828 })
829}
830
Colin Cross635c3b02016-05-18 15:37:25 -0700831func TransformDarwinStrip(ctx android.ModuleContext, inputFile android.Path,
832 outputFile android.WritablePath) {
Colin Crossb8ecdfe2016-05-03 15:10:29 -0700833
Colin Crossae887032017-10-23 17:16:14 -0700834 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700835 Rule: darwinStrip,
836 Description: "strip " + outputFile.Base(),
837 Output: outputFile,
838 Input: inputFile,
Colin Crossb8ecdfe2016-05-03 15:10:29 -0700839 })
840}
841
Dan Willemsen581341d2017-02-09 16:16:31 -0800842func TransformCoverageFilesToLib(ctx android.ModuleContext,
843 inputs Objects, flags builderFlags, baseName string) android.OptionalPath {
844
845 if len(inputs.coverageFiles) > 0 {
846 outputFile := android.PathForModuleOut(ctx, baseName+".gcnodir")
847
848 TransformObjToStaticLib(ctx, inputs.coverageFiles, flags, outputFile, nil)
849
850 return android.OptionalPathForPath(outputFile)
851 }
852
853 return android.OptionalPath{}
854}
855
Colin Cross635c3b02016-05-18 15:37:25 -0700856func CopyGccLib(ctx android.ModuleContext, libName string,
857 flags builderFlags, outputFile android.WritablePath) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800858
Colin Crossae887032017-10-23 17:16:14 -0700859 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700860 Rule: copyGccLib,
861 Description: "copy gcc library " + libName,
862 Output: outputFile,
Colin Cross3f40fa42015-01-30 17:27:36 -0800863 Args: map[string]string{
864 "ccCmd": gccCmd(flags.toolchain, "gcc"),
865 "cFlags": flags.globalFlags,
866 "libName": libName,
867 },
868 })
869}
870
Colin Crossb98c8b02016-07-29 13:44:28 -0700871func gccCmd(toolchain config.Toolchain, cmd string) string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800872 return filepath.Join(toolchain.GccRoot(), "bin", toolchain.GccTriple()+"-"+cmd)
873}
Colin Cross0af4b842015-04-30 16:36:18 -0700874
Colin Cross5b529592017-05-09 13:34:34 -0700875func splitListForSize(list android.Paths, limit int) (lists []android.Paths, err error) {
Colin Cross0af4b842015-04-30 16:36:18 -0700876 var i int
877
878 start := 0
879 bytes := 0
880 for i = range list {
Colin Cross5b529592017-05-09 13:34:34 -0700881 l := len(list[i].String())
Colin Cross0af4b842015-04-30 16:36:18 -0700882 if l > limit {
883 return nil, fmt.Errorf("list element greater than size limit (%d)", limit)
884 }
885 if bytes+l > limit {
886 lists = append(lists, list[start:i])
887 start = i
888 bytes = 0
889 }
890 bytes += l + 1 // count a space between each list element
891 }
892
893 lists = append(lists, list[start:])
894
895 totalLen := 0
896 for _, l := range lists {
897 totalLen += len(l)
898 }
899 if totalLen != len(list) {
900 panic(fmt.Errorf("Failed breaking up list, %d != %d", len(list), totalLen))
901 }
902 return lists, nil
903}