blob: 372c5317f7b6132ed3992e357d6a0a4275f21ff9 [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")
Dan Willemsen8fec83a2018-03-09 10:47:52 -0800112 _ = pctx.SourcePathVariable("xzCmd", "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/xz")
Colin Cross665dce92016-04-28 14:50:03 -0700113
Colin Cross9d45bb72016-08-29 16:14:13 -0700114 strip = pctx.AndroidStaticRule("strip",
Colin Cross665dce92016-04-28 14:50:03 -0700115 blueprint.RuleParams{
116 Depfile: "${out}.d",
117 Deps: blueprint.DepsGCC,
Dan Willemsen8fec83a2018-03-09 10:47:52 -0800118 Command: "CROSS_COMPILE=$crossCompile XZ=$xzCmd $stripPath ${args} -i ${in} -o ${out} -d ${out}.d",
119 CommandDeps: []string{"$stripPath", "$xzCmd"},
Colin Cross665dce92016-04-28 14:50:03 -0700120 },
121 "args", "crossCompile")
122
Colin Cross9d45bb72016-08-29 16:14:13 -0700123 emptyFile = pctx.AndroidStaticRule("emptyFile",
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700124 blueprint.RuleParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700125 Command: "rm -f $out && touch $out",
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700126 })
127
Nan Zhang43a485c2017-03-27 14:27:58 -0700128 _ = pctx.SourcePathVariable("copyGccLibPath", "build/soong/scripts/copygcclib.sh")
Colin Cross3f40fa42015-01-30 17:27:36 -0800129
Colin Cross9d45bb72016-08-29 16:14:13 -0700130 copyGccLib = pctx.AndroidStaticRule("copyGccLib",
Colin Cross3f40fa42015-01-30 17:27:36 -0800131 blueprint.RuleParams{
132 Depfile: "${out}.d",
133 Deps: blueprint.DepsGCC,
134 Command: "$copyGccLibPath $out $ccCmd $cFlags -print-file-name=${libName}",
Dan Willemsenc94a7682015-11-17 15:27:28 -0800135 CommandDeps: []string{"$copyGccLibPath", "$ccCmd"},
Colin Cross3f40fa42015-01-30 17:27:36 -0800136 },
137 "ccCmd", "cFlags", "libName")
Colin Cross26c34ed2016-09-30 17:10:16 -0700138
Nan Zhang43a485c2017-03-27 14:27:58 -0700139 _ = pctx.SourcePathVariable("tocPath", "build/soong/scripts/toc.sh")
Colin Cross26c34ed2016-09-30 17:10:16 -0700140
141 toc = pctx.AndroidStaticRule("toc",
142 blueprint.RuleParams{
143 Depfile: "${out}.d",
144 Deps: blueprint.DepsGCC,
145 Command: "CROSS_COMPILE=$crossCompile $tocPath -i ${in} -o ${out} -d ${out}.d",
146 CommandDeps: []string{"$tocPath"},
147 Restat: true,
148 },
149 "crossCompile")
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700150
151 clangTidy = pctx.AndroidStaticRule("clangTidy",
152 blueprint.RuleParams{
153 Command: "rm -f $out && ${config.ClangBin}/clang-tidy $tidyFlags $in -- $cFlags && touch $out",
154 CommandDeps: []string{"${config.ClangBin}/clang-tidy"},
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700155 },
156 "cFlags", "tidyFlags")
Colin Cross91e90042016-12-02 17:13:24 -0800157
Nan Zhang43a485c2017-03-27 14:27:58 -0700158 _ = pctx.SourcePathVariable("yasmCmd", "prebuilts/misc/${config.HostPrebuiltTag}/yasm/yasm")
Colin Cross91e90042016-12-02 17:13:24 -0800159
160 yasm = pctx.AndroidStaticRule("yasm",
161 blueprint.RuleParams{
Dan Willemsen1d3e5452017-08-22 20:53:45 -0700162 Command: "$yasmCmd $asFlags -o $out $in && $yasmCmd $asFlags -M $in >$out.d",
Colin Cross91e90042016-12-02 17:13:24 -0800163 CommandDeps: []string{"$yasmCmd"},
Dan Willemsen1d3e5452017-08-22 20:53:45 -0700164 Depfile: "$out.d",
165 Deps: blueprint.DepsGCC,
Colin Cross91e90042016-12-02 17:13:24 -0800166 },
167 "asFlags")
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800168
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700169 windres = pctx.AndroidStaticRule("windres",
170 blueprint.RuleParams{
171 Command: "$windresCmd $flags -I$$(dirname $in) -i $in -o $out",
172 CommandDeps: []string{"$windresCmd"},
173 },
174 "windresCmd", "flags")
175
Jayant Chowdharya4c6df52018-02-20 12:36:51 -0800176 _ = pctx.SourcePathVariable("sAbiDumper", "prebuilts/clang-tools/${config.HostPrebuiltTag}/bin/header-abi-dumper")
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800177
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700178 // -w has been added since header-abi-dumper does not need to produce any sort of diagnostic information.
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800179 sAbiDump = pctx.AndroidStaticRule("sAbiDump",
180 blueprint.RuleParams{
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700181 Command: "rm -f $out && $sAbiDumper -o ${out} $in $exportDirs -- $cFlags -w -isystem ${config.RSIncludePath}",
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800182 CommandDeps: []string{"$sAbiDumper"},
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800183 },
184 "cFlags", "exportDirs")
185
Jayant Chowdharya4c6df52018-02-20 12:36:51 -0800186 _ = pctx.SourcePathVariable("sAbiLinker", "prebuilts/clang-tools/${config.HostPrebuiltTag}/bin/header-abi-linker")
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800187
188 sAbiLink = pctx.AndroidStaticRule("sAbiLink",
189 blueprint.RuleParams{
Jayant Chowdharydf344d52018-01-17 11:11:42 -0800190 Command: "$sAbiLinker -o ${out} $symbolFilter -arch $arch $exportedHeaderFlags @${out}.rsp ",
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800191 CommandDeps: []string{"$sAbiLinker"},
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800192 Rspfile: "${out}.rsp",
193 RspfileContent: "${in}",
194 },
Jayant Chowdharydf344d52018-01-17 11:11:42 -0800195 "symbolFilter", "arch", "exportedHeaderFlags")
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800196
Jayant Chowdharya4c6df52018-02-20 12:36:51 -0800197 _ = pctx.SourcePathVariable("sAbiDiffer", "prebuilts/clang-tools/${config.HostPrebuiltTag}/bin/header-abi-diff")
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700198
Jayant Chowdhary219139d2017-11-27 14:52:21 -0800199 sAbiDiff = pctx.AndroidRuleFunc("sAbiDiff",
200 func(config android.Config) (blueprint.RuleParams, error) {
201
202 commandStr := "($sAbiDiffer $allowFlags -lib $libName -arch $arch -check-all-apis -o ${out} -new $in -old $referenceDump)"
203 distDir := config.ProductVariables.DistDir
204 if distDir != nil && *distDir != "" {
205 distAbiDiffDir := *distDir + "/abidiffs/"
206 commandStr += " || (mkdir -p " + distAbiDiffDir + " && cp ${out} " + distAbiDiffDir + " && exit 1)"
207 }
208 return blueprint.RuleParams{
209 Command: commandStr,
210 CommandDeps: []string{"$sAbiDiffer"},
211 }, nil
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800212 },
Jayant Chowdharya3bb1b32017-11-01 11:12:15 -0700213 "allowFlags", "referenceDump", "libName", "arch")
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700214
215 unzipRefSAbiDump = pctx.AndroidStaticRule("unzipRefSAbiDump",
216 blueprint.RuleParams{
217 Command: "gunzip -c $in > $out",
218 })
Colin Cross3f40fa42015-01-30 17:27:36 -0800219)
220
Dan Willemsen322a0a62015-11-17 15:19:46 -0800221func init() {
222 // We run gcc/clang with PWD=/proc/self/cwd to remove $TOP from the
223 // debug output. That way two builds in two different directories will
224 // create the same output.
225 if runtime.GOOS != "darwin" {
226 pctx.StaticVariable("relPwd", "PWD=/proc/self/cwd")
227 } else {
228 // Darwin doesn't have /proc
229 pctx.StaticVariable("relPwd", "")
230 }
231}
232
Colin Cross3f40fa42015-01-30 17:27:36 -0800233type builderFlags struct {
Joe Onorato09e94ab2017-11-18 18:23:14 -0800234 globalFlags string
235 arFlags string
236 asFlags string
237 cFlags string
238 toolingCFlags string // A separate set of Cflags for clang LibTooling tools
239 conlyFlags string
240 cppFlags string
241 ldFlags string
242 libFlags string
243 yaccFlags string
244 protoFlags string
245 protoOutParams string
246 tidyFlags string
247 sAbiFlags string
248 yasmFlags string
249 aidlFlags string
250 rsFlags string
251 toolchain config.Toolchain
252 clang bool
253 tidy bool
254 coverage bool
255 sAbiDump bool
Dan Willemsenab9f4262018-02-14 13:58:34 -0800256 protoRoot bool
Colin Cross665dce92016-04-28 14:50:03 -0700257
Colin Crossc3199482017-03-30 15:03:04 -0700258 systemIncludeFlags string
259
Colin Cross18c0c5a2016-12-01 14:45:23 -0800260 groupStaticLibs bool
Zhizhou Yang51be6322018-02-08 18:32:11 -0800261 arGoldPlugin bool
Colin Cross18c0c5a2016-12-01 14:45:23 -0800262
Colin Cross665dce92016-04-28 14:50:03 -0700263 stripKeepSymbols bool
264 stripKeepMiniDebugInfo bool
265 stripAddGnuDebuglink bool
Colin Cross3f40fa42015-01-30 17:27:36 -0800266}
267
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700268type Objects struct {
Dan Willemsen581341d2017-02-09 16:16:31 -0800269 objFiles android.Paths
270 tidyFiles android.Paths
271 coverageFiles android.Paths
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800272 sAbiDumpFiles android.Paths
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700273}
274
275func (a Objects) Copy() Objects {
276 return Objects{
Dan Willemsen581341d2017-02-09 16:16:31 -0800277 objFiles: append(android.Paths{}, a.objFiles...),
278 tidyFiles: append(android.Paths{}, a.tidyFiles...),
279 coverageFiles: append(android.Paths{}, a.coverageFiles...),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800280 sAbiDumpFiles: append(android.Paths{}, a.sAbiDumpFiles...),
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700281 }
282}
283
284func (a Objects) Append(b Objects) Objects {
285 return Objects{
Dan Willemsen581341d2017-02-09 16:16:31 -0800286 objFiles: append(a.objFiles, b.objFiles...),
287 tidyFiles: append(a.tidyFiles, b.tidyFiles...),
288 coverageFiles: append(a.coverageFiles, b.coverageFiles...),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800289 sAbiDumpFiles: append(a.sAbiDumpFiles, b.sAbiDumpFiles...),
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700290 }
291}
292
Colin Cross3f40fa42015-01-30 17:27:36 -0800293// Generate rules for compiling multiple .c, .cpp, or .S files to individual .o files
Colin Cross635c3b02016-05-18 15:37:25 -0700294func TransformSourceToObj(ctx android.ModuleContext, subdir string, srcFiles android.Paths,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800295 flags builderFlags, pathDeps android.Paths, cFlagsDeps android.Paths) Objects {
Colin Cross581c1892015-04-07 16:50:10 -0700296
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700297 objFiles := make(android.Paths, len(srcFiles))
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700298 var tidyFiles android.Paths
299 if flags.tidy && flags.clang {
300 tidyFiles = make(android.Paths, 0, len(srcFiles))
301 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800302 var coverageFiles android.Paths
303 if flags.coverage {
304 coverageFiles = make(android.Paths, 0, len(srcFiles))
305 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800306
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700307 commonFlags := strings.Join([]string{
Colin Crossc3199482017-03-30 15:03:04 -0700308 flags.globalFlags,
309 flags.systemIncludeFlags,
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700310 }, " ")
311
312 toolingCflags := strings.Join([]string{
313 commonFlags,
314 flags.toolingCFlags,
315 flags.conlyFlags,
316 }, " ")
317
318 cflags := strings.Join([]string{
319 commonFlags,
Colin Crossc3199482017-03-30 15:03:04 -0700320 flags.cFlags,
321 flags.conlyFlags,
322 }, " ")
323
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700324 toolingCppflags := strings.Join([]string{
325 commonFlags,
326 flags.toolingCFlags,
327 flags.cppFlags,
328 }, " ")
329
Colin Crossc3199482017-03-30 15:03:04 -0700330 cppflags := strings.Join([]string{
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700331 commonFlags,
Colin Crossc3199482017-03-30 15:03:04 -0700332 flags.cFlags,
333 flags.cppFlags,
334 }, " ")
335
336 asflags := strings.Join([]string{
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700337 commonFlags,
Colin Crossc3199482017-03-30 15:03:04 -0700338 flags.asFlags,
339 }, " ")
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700340
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800341 var sAbiDumpFiles android.Paths
342 if flags.sAbiDump && flags.clang {
343 sAbiDumpFiles = make(android.Paths, 0, len(srcFiles))
344 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800345
Dan Willemsenbe03f342016-03-03 17:21:04 -0800346 if flags.clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700347 cflags += " ${config.NoOverrideClangGlobalCflags}"
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700348 toolingCflags += " ${config.NoOverrideClangGlobalCflags}"
Colin Crossb98c8b02016-07-29 13:44:28 -0700349 cppflags += " ${config.NoOverrideClangGlobalCflags}"
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700350 toolingCppflags += " ${config.NoOverrideClangGlobalCflags}"
Dan Willemsenbe03f342016-03-03 17:21:04 -0800351 } else {
Colin Crossb98c8b02016-07-29 13:44:28 -0700352 cflags += " ${config.NoOverrideGlobalCflags}"
353 cppflags += " ${config.NoOverrideGlobalCflags}"
Dan Willemsenbe03f342016-03-03 17:21:04 -0800354 }
355
Colin Cross3f40fa42015-01-30 17:27:36 -0800356 for i, srcFile := range srcFiles {
Dan Willemsen21ec4902016-11-02 20:43:13 -0700357 objFile := android.ObjPathWithExt(ctx, subdir, srcFile, "o")
Colin Cross3f40fa42015-01-30 17:27:36 -0800358
359 objFiles[i] = objFile
360
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700361 switch srcFile.Ext() {
362 case ".asm":
Colin Crossae887032017-10-23 17:16:14 -0700363 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700364 Rule: yasm,
365 Description: "yasm " + srcFile.Rel(),
366 Output: objFile,
367 Input: srcFile,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800368 Implicits: cFlagsDeps,
369 OrderOnly: pathDeps,
Colin Cross91e90042016-12-02 17:13:24 -0800370 Args: map[string]string{
371 "asFlags": flags.yasmFlags,
372 },
373 })
374 continue
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700375 case ".rc":
Colin Crossae887032017-10-23 17:16:14 -0700376 ctx.Build(pctx, android.BuildParams{
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700377 Rule: windres,
378 Description: "windres " + srcFile.Rel(),
379 Output: objFile,
380 Input: srcFile,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800381 Implicits: cFlagsDeps,
382 OrderOnly: pathDeps,
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700383 Args: map[string]string{
384 "windresCmd": gccCmd(flags.toolchain, "windres"),
385 "flags": flags.toolchain.WindresFlags(),
386 },
387 })
388 continue
Colin Cross91e90042016-12-02 17:13:24 -0800389 }
390
Colin Cross3f40fa42015-01-30 17:27:36 -0800391 var moduleCflags string
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700392 var moduleToolingCflags string
Colin Cross3f40fa42015-01-30 17:27:36 -0800393 var ccCmd string
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700394 tidy := flags.tidy && flags.clang
Dan Willemsen581341d2017-02-09 16:16:31 -0800395 coverage := flags.coverage
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800396 dump := flags.sAbiDump && flags.clang
Colin Cross3f40fa42015-01-30 17:27:36 -0800397
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700398 switch srcFile.Ext() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800399 case ".S", ".s":
400 ccCmd = "gcc"
401 moduleCflags = asflags
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700402 tidy = false
Dan Willemsen581341d2017-02-09 16:16:31 -0800403 coverage = false
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800404 dump = false
Colin Cross3f40fa42015-01-30 17:27:36 -0800405 case ".c":
406 ccCmd = "gcc"
407 moduleCflags = cflags
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700408 moduleToolingCflags = toolingCflags
Colin Cross9978ffe2016-12-01 15:31:22 -0800409 case ".cpp", ".cc", ".mm":
Colin Cross3f40fa42015-01-30 17:27:36 -0800410 ccCmd = "g++"
411 moduleCflags = cppflags
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700412 moduleToolingCflags = toolingCppflags
Colin Cross3f40fa42015-01-30 17:27:36 -0800413 default:
414 ctx.ModuleErrorf("File %s has unknown extension", srcFile)
415 continue
416 }
417
418 if flags.clang {
419 switch ccCmd {
420 case "gcc":
421 ccCmd = "clang"
422 case "g++":
423 ccCmd = "clang++"
424 default:
425 panic("unrecoginzied ccCmd")
426 }
Colin Cross67a5c132017-05-09 13:45:28 -0700427 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800428
Colin Cross67a5c132017-05-09 13:45:28 -0700429 ccDesc := ccCmd
430
431 if flags.clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700432 ccCmd = "${config.ClangBin}/" + ccCmd
Colin Cross3f40fa42015-01-30 17:27:36 -0800433 } else {
434 ccCmd = gccCmd(flags.toolchain, ccCmd)
435 }
436
Dan Willemsen581341d2017-02-09 16:16:31 -0800437 var implicitOutputs android.WritablePaths
438 if coverage {
439 gcnoFile := android.ObjPathWithExt(ctx, subdir, srcFile, "gcno")
440 implicitOutputs = append(implicitOutputs, gcnoFile)
441 coverageFiles = append(coverageFiles, gcnoFile)
442 }
443
Colin Crossae887032017-10-23 17:16:14 -0700444 ctx.Build(pctx, android.BuildParams{
Dan Willemsen581341d2017-02-09 16:16:31 -0800445 Rule: cc,
Colin Cross67a5c132017-05-09 13:45:28 -0700446 Description: ccDesc + " " + srcFile.Rel(),
Dan Willemsen581341d2017-02-09 16:16:31 -0800447 Output: objFile,
448 ImplicitOutputs: implicitOutputs,
449 Input: srcFile,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800450 Implicits: cFlagsDeps,
451 OrderOnly: pathDeps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800452 Args: map[string]string{
Colin Cross28344522015-04-22 13:07:53 -0700453 "cFlags": moduleCflags,
454 "ccCmd": ccCmd,
Colin Cross3f40fa42015-01-30 17:27:36 -0800455 },
456 })
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700457
458 if tidy {
Dan Willemsen21ec4902016-11-02 20:43:13 -0700459 tidyFile := android.ObjPathWithExt(ctx, subdir, srcFile, "tidy")
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700460 tidyFiles = append(tidyFiles, tidyFile)
461
Colin Crossae887032017-10-23 17:16:14 -0700462 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700463 Rule: clangTidy,
464 Description: "clang-tidy " + srcFile.Rel(),
465 Output: tidyFile,
466 Input: srcFile,
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700467 // We must depend on objFile, since clang-tidy doesn't
468 // support exporting dependencies.
469 Implicit: objFile,
470 Args: map[string]string{
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700471 "cFlags": moduleToolingCflags,
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700472 "tidyFlags": flags.tidyFlags,
473 },
474 })
475 }
476
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800477 if dump {
478 sAbiDumpFile := android.ObjPathWithExt(ctx, subdir, srcFile, "sdump")
479 sAbiDumpFiles = append(sAbiDumpFiles, sAbiDumpFile)
480
Colin Crossae887032017-10-23 17:16:14 -0700481 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700482 Rule: sAbiDump,
483 Description: "header-abi-dumper " + srcFile.Rel(),
484 Output: sAbiDumpFile,
485 Input: srcFile,
486 Implicit: objFile,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800487 Args: map[string]string{
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700488 "cFlags": moduleToolingCflags,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800489 "exportDirs": flags.sAbiFlags,
490 },
491 })
492 }
493
Colin Cross3f40fa42015-01-30 17:27:36 -0800494 }
495
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700496 return Objects{
Dan Willemsen581341d2017-02-09 16:16:31 -0800497 objFiles: objFiles,
498 tidyFiles: tidyFiles,
499 coverageFiles: coverageFiles,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800500 sAbiDumpFiles: sAbiDumpFiles,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700501 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800502}
503
504// Generate a rule for compiling multiple .o files to a static library (.a)
Colin Cross635c3b02016-05-18 15:37:25 -0700505func TransformObjToStaticLib(ctx android.ModuleContext, objFiles android.Paths,
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700506 flags builderFlags, outputFile android.ModuleOutPath, deps android.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800507
Dan Willemsen581341d2017-02-09 16:16:31 -0800508 if ctx.Darwin() {
509 transformDarwinObjToStaticLib(ctx, objFiles, flags, outputFile, deps)
510 return
511 }
512
Stephen Hinesf1addeb2018-01-09 23:29:04 -0800513 arCmd := "${config.ClangBin}/llvm-ar"
514 arFlags := "crsD"
515 if !ctx.Darwin() {
516 arFlags += " -format=gnu"
517 }
Zhizhou Yang51be6322018-02-08 18:32:11 -0800518 if flags.arGoldPlugin {
519 arFlags += " --plugin ${config.LLVMGoldPlugin}"
520 }
Vishwath Mohan83d9f712017-03-16 11:01:23 -0700521 if flags.arFlags != "" {
522 arFlags += " " + flags.arFlags
523 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800524
Colin Crossae887032017-10-23 17:16:14 -0700525 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700526 Rule: ar,
527 Description: "static link " + outputFile.Base(),
528 Output: outputFile,
529 Inputs: objFiles,
530 Implicits: deps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800531 Args: map[string]string{
532 "arFlags": arFlags,
533 "arCmd": arCmd,
534 },
535 })
536}
537
Colin Cross0af4b842015-04-30 16:36:18 -0700538// Generate a rule for compiling multiple .o files to a static library (.a) on
539// darwin. The darwin ar tool doesn't support @file for list files, and has a
540// very small command line length limit, so we have to split the ar into multiple
541// steps, each appending to the previous one.
Dan Willemsen581341d2017-02-09 16:16:31 -0800542func transformDarwinObjToStaticLib(ctx android.ModuleContext, objFiles android.Paths,
Colin Cross5b529592017-05-09 13:34:34 -0700543 flags builderFlags, outputFile android.ModuleOutPath, deps android.Paths) {
Colin Cross0af4b842015-04-30 16:36:18 -0700544
Colin Cross0af4b842015-04-30 16:36:18 -0700545 arFlags := "cqs"
546
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700547 if len(objFiles) == 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700548 dummy := android.PathForModuleOut(ctx, "dummy"+objectExtension)
549 dummyAr := android.PathForModuleOut(ctx, "dummy"+staticLibraryExtension)
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700550
Colin Crossae887032017-10-23 17:16:14 -0700551 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700552 Rule: emptyFile,
553 Description: "empty object file",
554 Output: dummy,
555 Implicits: deps,
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700556 })
557
Colin Crossae887032017-10-23 17:16:14 -0700558 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700559 Rule: darwinAr,
560 Description: "empty static archive",
561 Output: dummyAr,
562 Input: dummy,
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700563 Args: map[string]string{
564 "arFlags": arFlags,
565 },
566 })
567
Colin Crossae887032017-10-23 17:16:14 -0700568 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700569 Rule: darwinAppendAr,
570 Description: "static link " + outputFile.Base(),
571 Output: outputFile,
572 Input: dummy,
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700573 Args: map[string]string{
574 "arFlags": "d",
575 "inAr": dummyAr.String(),
576 },
577 })
578
579 return
580 }
581
Colin Cross0af4b842015-04-30 16:36:18 -0700582 // ARG_MAX on darwin is 262144, use half that to be safe
Colin Cross5b529592017-05-09 13:34:34 -0700583 objFilesLists, err := splitListForSize(objFiles, 131072)
Colin Cross0af4b842015-04-30 16:36:18 -0700584 if err != nil {
585 ctx.ModuleErrorf("%s", err.Error())
586 }
587
Colin Cross5b529592017-05-09 13:34:34 -0700588 var in, out android.WritablePath
Colin Cross0af4b842015-04-30 16:36:18 -0700589 for i, l := range objFilesLists {
590 in = out
591 out = outputFile
592 if i != len(objFilesLists)-1 {
Colin Cross5b529592017-05-09 13:34:34 -0700593 out = android.PathForModuleOut(ctx, outputFile.Base()+strconv.Itoa(i))
Colin Cross0af4b842015-04-30 16:36:18 -0700594 }
595
Colin Crossae887032017-10-23 17:16:14 -0700596 build := android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700597 Rule: darwinAr,
598 Description: "static link " + out.Base(),
599 Output: out,
600 Inputs: l,
601 Implicits: deps,
Colin Cross5b529592017-05-09 13:34:34 -0700602 Args: map[string]string{
603 "arFlags": arFlags,
604 },
Colin Cross0af4b842015-04-30 16:36:18 -0700605 }
Colin Cross5b529592017-05-09 13:34:34 -0700606 if i != 0 {
607 build.Rule = darwinAppendAr
608 build.Args["inAr"] = in.String()
609 }
Colin Crossae887032017-10-23 17:16:14 -0700610 ctx.Build(pctx, build)
Colin Cross0af4b842015-04-30 16:36:18 -0700611 }
612}
613
Colin Cross3f40fa42015-01-30 17:27:36 -0800614// Generate a rule for compiling multiple .o files, plus static libraries, whole static libraries,
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700615// and shared libraries, to a shared library (.so) or dynamic executable
Colin Cross635c3b02016-05-18 15:37:25 -0700616func TransformObjToDynamicBinary(ctx android.ModuleContext,
617 objFiles, sharedLibs, staticLibs, lateStaticLibs, wholeStaticLibs, deps android.Paths,
618 crtBegin, crtEnd android.OptionalPath, groupLate bool, flags builderFlags, outputFile android.WritablePath) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800619
620 var ldCmd string
621 if flags.clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700622 ldCmd = "${config.ClangBin}/clang++"
Colin Cross3f40fa42015-01-30 17:27:36 -0800623 } else {
624 ldCmd = gccCmd(flags.toolchain, "g++")
625 }
626
Colin Cross3f40fa42015-01-30 17:27:36 -0800627 var libFlagsList []string
628
Colin Cross16b23492016-01-06 14:41:07 -0800629 if len(flags.libFlags) > 0 {
630 libFlagsList = append(libFlagsList, flags.libFlags)
631 }
632
Colin Cross3f40fa42015-01-30 17:27:36 -0800633 if len(wholeStaticLibs) > 0 {
Dan Willemsen490fd492015-11-24 17:53:15 -0800634 if ctx.Host() && ctx.Darwin() {
Colin Cross635c3b02016-05-18 15:37:25 -0700635 libFlagsList = append(libFlagsList, android.JoinWithPrefix(wholeStaticLibs.Strings(), "-force_load "))
Colin Cross0af4b842015-04-30 16:36:18 -0700636 } else {
637 libFlagsList = append(libFlagsList, "-Wl,--whole-archive ")
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700638 libFlagsList = append(libFlagsList, wholeStaticLibs.Strings()...)
Colin Cross0af4b842015-04-30 16:36:18 -0700639 libFlagsList = append(libFlagsList, "-Wl,--no-whole-archive ")
640 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800641 }
642
Colin Cross7a7cf972016-12-05 18:47:39 -0800643 if flags.groupStaticLibs && !ctx.Darwin() && len(staticLibs) > 0 {
Colin Cross18c0c5a2016-12-01 14:45:23 -0800644 libFlagsList = append(libFlagsList, "-Wl,--start-group")
645 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700646 libFlagsList = append(libFlagsList, staticLibs.Strings()...)
Colin Cross7a7cf972016-12-05 18:47:39 -0800647 if flags.groupStaticLibs && !ctx.Darwin() && len(staticLibs) > 0 {
Colin Cross18c0c5a2016-12-01 14:45:23 -0800648 libFlagsList = append(libFlagsList, "-Wl,--end-group")
649 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800650
Stephen Hines10347862016-07-18 15:54:54 -0700651 if groupLate && !ctx.Darwin() && len(lateStaticLibs) > 0 {
Dan Willemsenedc385f2015-07-08 13:02:23 -0700652 libFlagsList = append(libFlagsList, "-Wl,--start-group")
653 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700654 libFlagsList = append(libFlagsList, lateStaticLibs.Strings()...)
Stephen Hines10347862016-07-18 15:54:54 -0700655 if groupLate && !ctx.Darwin() && len(lateStaticLibs) > 0 {
Dan Willemsenedc385f2015-07-08 13:02:23 -0700656 libFlagsList = append(libFlagsList, "-Wl,--end-group")
657 }
658
Colin Cross3f40fa42015-01-30 17:27:36 -0800659 for _, lib := range sharedLibs {
Dan Albert9840e1b2016-07-21 08:47:33 -0700660 libFlagsList = append(libFlagsList, lib.String())
Colin Cross3f40fa42015-01-30 17:27:36 -0800661 }
662
Colin Cross3f40fa42015-01-30 17:27:36 -0800663 deps = append(deps, staticLibs...)
Colin Cross3075ad02015-03-17 10:47:08 -0700664 deps = append(deps, lateStaticLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800665 deps = append(deps, wholeStaticLibs...)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700666 if crtBegin.Valid() {
667 deps = append(deps, crtBegin.Path(), crtEnd.Path())
Colin Cross3f40fa42015-01-30 17:27:36 -0800668 }
669
Colin Crossae887032017-10-23 17:16:14 -0700670 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700671 Rule: ld,
672 Description: "link " + outputFile.Base(),
673 Output: outputFile,
674 Inputs: objFiles,
675 Implicits: deps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800676 Args: map[string]string{
Dan Albertce2b8392016-07-21 13:16:49 -0700677 "ldCmd": ldCmd,
678 "crtBegin": crtBegin.String(),
679 "libFlags": strings.Join(libFlagsList, " "),
680 "ldFlags": flags.ldFlags,
681 "crtEnd": crtEnd.String(),
Colin Cross3f40fa42015-01-30 17:27:36 -0800682 },
683 })
684}
685
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800686// Generate a rule to combine .dump sAbi dump files from multiple source files
687// into a single .ldump sAbi dump file
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700688func TransformDumpToLinkedDump(ctx android.ModuleContext, sAbiDumps android.Paths, soFile android.Path,
Jayant Chowdharydf344d52018-01-17 11:11:42 -0800689 baseName, exportedHeaderFlags string) android.OptionalPath {
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800690 outputFile := android.PathForModuleOut(ctx, baseName+".lsdump")
Jayant Chowdharydcd33b62018-02-23 16:43:23 -0800691 sabiLock.Lock()
692 lsdumpPaths = append(lsdumpPaths, outputFile.String())
693 sabiLock.Unlock()
Jayant Chowdharydf344d52018-01-17 11:11:42 -0800694 symbolFilterStr := "-so " + soFile.String()
Colin Crossae887032017-10-23 17:16:14 -0700695 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700696 Rule: sAbiLink,
697 Description: "header-abi-linker " + outputFile.Base(),
698 Output: outputFile,
699 Inputs: sAbiDumps,
Jayant Chowdharydf344d52018-01-17 11:11:42 -0800700 Implicit: soFile,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800701 Args: map[string]string{
Jayant Chowdharydf344d52018-01-17 11:11:42 -0800702 "symbolFilter": symbolFilterStr,
703 "arch": ctx.Arch().ArchType.Name,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800704 "exportedHeaderFlags": exportedHeaderFlags,
705 },
706 })
707 return android.OptionalPathForPath(outputFile)
708}
709
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700710func UnzipRefDump(ctx android.ModuleContext, zippedRefDump android.Path, baseName string) android.Path {
711 outputFile := android.PathForModuleOut(ctx, baseName+"_ref.lsdump")
Colin Crossae887032017-10-23 17:16:14 -0700712 ctx.Build(pctx, android.BuildParams{
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700713 Rule: unzipRefSAbiDump,
714 Description: "gunzip" + outputFile.Base(),
715 Output: outputFile,
716 Input: zippedRefDump,
717 })
718 return outputFile
719}
720
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800721func SourceAbiDiff(ctx android.ModuleContext, inputDump android.Path, referenceDump android.Path,
Logan Chienf3511742017-10-31 18:04:35 +0800722 baseName, exportedHeaderFlags string, isVndkExt bool) android.OptionalPath {
723
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800724 outputFile := android.PathForModuleOut(ctx, baseName+".abidiff")
Logan Chienf3511742017-10-31 18:04:35 +0800725
Jayant Chowdharye4499502018-01-17 13:13:33 -0800726 localAbiCheckAllowFlags := append([]string(nil), abiCheckAllowFlags...)
727 if exportedHeaderFlags == "" {
728 localAbiCheckAllowFlags = append(localAbiCheckAllowFlags, "-advice-only")
729 }
Logan Chienf3511742017-10-31 18:04:35 +0800730 if isVndkExt {
731 localAbiCheckAllowFlags = append(localAbiCheckAllowFlags, "-allow-extensions")
732 }
733
Colin Crossae887032017-10-23 17:16:14 -0700734 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700735 Rule: sAbiDiff,
736 Description: "header-abi-diff " + outputFile.Base(),
737 Output: outputFile,
738 Input: inputDump,
739 Implicit: referenceDump,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800740 Args: map[string]string{
741 "referenceDump": referenceDump.String(),
Jayant Chowdharyf54e0a72017-05-22 10:53:24 -0700742 "libName": baseName,
743 "arch": ctx.Arch().ArchType.Name,
Jayant Chowdharye4499502018-01-17 13:13:33 -0800744 "allowFlags": strings.Join(localAbiCheckAllowFlags, " "),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800745 },
746 })
747 return android.OptionalPathForPath(outputFile)
748}
749
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700750// Generate a rule for extracting a table of contents from a shared library (.so)
751func TransformSharedObjectToToc(ctx android.ModuleContext, inputFile android.Path,
Colin Cross26c34ed2016-09-30 17:10:16 -0700752 outputFile android.WritablePath, flags builderFlags) {
753
754 crossCompile := gccCmd(flags.toolchain, "")
755
Colin Crossae887032017-10-23 17:16:14 -0700756 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700757 Rule: toc,
758 Description: "generate toc " + inputFile.Base(),
759 Output: outputFile,
760 Input: inputFile,
Colin Cross26c34ed2016-09-30 17:10:16 -0700761 Args: map[string]string{
762 "crossCompile": crossCompile,
763 },
764 })
765}
766
Colin Cross3f40fa42015-01-30 17:27:36 -0800767// Generate a rule for compiling multiple .o files to a .o using ld partial linking
Colin Cross635c3b02016-05-18 15:37:25 -0700768func TransformObjsToObj(ctx android.ModuleContext, objFiles android.Paths,
769 flags builderFlags, outputFile android.WritablePath) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800770
Colin Cross41280a42015-11-23 14:01:42 -0800771 var ldCmd string
772 if flags.clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700773 ldCmd = "${config.ClangBin}/clang++"
Colin Cross41280a42015-11-23 14:01:42 -0800774 } else {
775 ldCmd = gccCmd(flags.toolchain, "g++")
776 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800777
Colin Crossae887032017-10-23 17:16:14 -0700778 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700779 Rule: partialLd,
780 Description: "link " + outputFile.Base(),
781 Output: outputFile,
782 Inputs: objFiles,
Colin Cross3f40fa42015-01-30 17:27:36 -0800783 Args: map[string]string{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700784 "ldCmd": ldCmd,
Colin Cross41280a42015-11-23 14:01:42 -0800785 "ldFlags": flags.ldFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800786 },
787 })
788}
789
Colin Crossbfae8852015-03-26 14:44:11 -0700790// Generate a rule for runing objcopy --prefix-symbols on a binary
Colin Cross635c3b02016-05-18 15:37:25 -0700791func TransformBinaryPrefixSymbols(ctx android.ModuleContext, prefix string, inputFile android.Path,
792 flags builderFlags, outputFile android.WritablePath) {
Colin Crossbfae8852015-03-26 14:44:11 -0700793
794 objcopyCmd := gccCmd(flags.toolchain, "objcopy")
795
Colin Crossae887032017-10-23 17:16:14 -0700796 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700797 Rule: prefixSymbols,
798 Description: "prefix symbols " + outputFile.Base(),
799 Output: outputFile,
800 Input: inputFile,
Colin Crossbfae8852015-03-26 14:44:11 -0700801 Args: map[string]string{
802 "objcopyCmd": objcopyCmd,
803 "prefix": prefix,
804 },
805 })
806}
807
Colin Cross635c3b02016-05-18 15:37:25 -0700808func TransformStrip(ctx android.ModuleContext, inputFile android.Path,
809 outputFile android.WritablePath, flags builderFlags) {
Colin Cross665dce92016-04-28 14:50:03 -0700810
811 crossCompile := gccCmd(flags.toolchain, "")
812 args := ""
813 if flags.stripAddGnuDebuglink {
814 args += " --add-gnu-debuglink"
815 }
816 if flags.stripKeepMiniDebugInfo {
817 args += " --keep-mini-debug-info"
818 }
819 if flags.stripKeepSymbols {
820 args += " --keep-symbols"
821 }
822
Colin Crossae887032017-10-23 17:16:14 -0700823 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700824 Rule: strip,
825 Description: "strip " + outputFile.Base(),
826 Output: outputFile,
827 Input: inputFile,
Colin Cross665dce92016-04-28 14:50:03 -0700828 Args: map[string]string{
829 "crossCompile": crossCompile,
830 "args": args,
831 },
832 })
833}
834
Colin Cross635c3b02016-05-18 15:37:25 -0700835func TransformDarwinStrip(ctx android.ModuleContext, inputFile android.Path,
836 outputFile android.WritablePath) {
Colin Crossb8ecdfe2016-05-03 15:10:29 -0700837
Colin Crossae887032017-10-23 17:16:14 -0700838 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700839 Rule: darwinStrip,
840 Description: "strip " + outputFile.Base(),
841 Output: outputFile,
842 Input: inputFile,
Colin Crossb8ecdfe2016-05-03 15:10:29 -0700843 })
844}
845
Dan Willemsen581341d2017-02-09 16:16:31 -0800846func TransformCoverageFilesToLib(ctx android.ModuleContext,
847 inputs Objects, flags builderFlags, baseName string) android.OptionalPath {
848
849 if len(inputs.coverageFiles) > 0 {
850 outputFile := android.PathForModuleOut(ctx, baseName+".gcnodir")
851
852 TransformObjToStaticLib(ctx, inputs.coverageFiles, flags, outputFile, nil)
853
854 return android.OptionalPathForPath(outputFile)
855 }
856
857 return android.OptionalPath{}
858}
859
Colin Cross635c3b02016-05-18 15:37:25 -0700860func CopyGccLib(ctx android.ModuleContext, libName string,
861 flags builderFlags, outputFile android.WritablePath) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800862
Colin Crossae887032017-10-23 17:16:14 -0700863 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700864 Rule: copyGccLib,
865 Description: "copy gcc library " + libName,
866 Output: outputFile,
Colin Cross3f40fa42015-01-30 17:27:36 -0800867 Args: map[string]string{
868 "ccCmd": gccCmd(flags.toolchain, "gcc"),
869 "cFlags": flags.globalFlags,
870 "libName": libName,
871 },
872 })
873}
874
Colin Crossb98c8b02016-07-29 13:44:28 -0700875func gccCmd(toolchain config.Toolchain, cmd string) string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800876 return filepath.Join(toolchain.GccRoot(), "bin", toolchain.GccTriple()+"-"+cmd)
877}
Colin Cross0af4b842015-04-30 16:36:18 -0700878
Colin Cross5b529592017-05-09 13:34:34 -0700879func splitListForSize(list android.Paths, limit int) (lists []android.Paths, err error) {
Colin Cross0af4b842015-04-30 16:36:18 -0700880 var i int
881
882 start := 0
883 bytes := 0
884 for i = range list {
Colin Cross5b529592017-05-09 13:34:34 -0700885 l := len(list[i].String())
Colin Cross0af4b842015-04-30 16:36:18 -0700886 if l > limit {
887 return nil, fmt.Errorf("list element greater than size limit (%d)", limit)
888 }
889 if bytes+l > limit {
890 lists = append(lists, list[start:i])
891 start = i
892 bytes = 0
893 }
894 bytes += l + 1 // count a space between each list element
895 }
896
897 lists = append(lists, list[start:])
898
899 totalLen := 0
900 for _, l := range lists {
901 totalLen += len(l)
902 }
903 if totalLen != len(list) {
904 panic(fmt.Errorf("Failed breaking up list, %d != %d", len(list), totalLen))
905 }
906 return lists, nil
907}