blob: 51c4ce9f70e8fc3423f2c0e84794576289a8b3dc [file] [log] [blame]
Colin Cross3f40fa42015-01-30 17:27:36 -08001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package cc
16
17// This file generates the final rules for compiling all C/C++. All properties related to
18// compiling should have been translated into builderFlags or another argument to the Transform*
19// functions.
20
21import (
Colin Cross0af4b842015-04-30 16:36:18 -070022 "fmt"
Colin Crossb98c8b02016-07-29 13:44:28 -070023 "path/filepath"
Colin Cross0af4b842015-04-30 16:36:18 -070024 "runtime"
25 "strconv"
Colin Cross3f40fa42015-01-30 17:27:36 -080026 "strings"
Colin Crossed4cf0b2015-03-26 14:43:45 -070027
28 "github.com/google/blueprint"
Colin Crossb98c8b02016-07-29 13:44:28 -070029
30 "android/soong/android"
31 "android/soong/cc/config"
Colin Cross3f40fa42015-01-30 17:27:36 -080032)
33
34const (
Dan Albertc3144b12015-04-28 18:17:56 -070035 objectExtension = ".o"
Colin Cross3f40fa42015-01-30 17:27:36 -080036 staticLibraryExtension = ".a"
37)
38
39var (
Colin Cross635c3b02016-05-18 15:37:25 -070040 pctx = android.NewPackageContext("android/soong/cc")
Colin Cross3f40fa42015-01-30 17:27:36 -080041
Colin Cross9d45bb72016-08-29 16:14:13 -070042 cc = pctx.AndroidGomaStaticRule("cc",
Colin Cross3f40fa42015-01-30 17:27:36 -080043 blueprint.RuleParams{
44 Depfile: "${out}.d",
45 Deps: blueprint.DepsGCC,
Alistair Strachan777475c2016-08-26 12:55:49 -070046 Command: "$relPwd ${config.CcWrapper}$ccCmd -c $cFlags -MD -MF ${out}.d -o $out $in",
Dan Willemsenc94a7682015-11-17 15:27:28 -080047 CommandDeps: []string{"$ccCmd"},
Colin Cross3f40fa42015-01-30 17:27:36 -080048 },
Dan Willemsen322a0a62015-11-17 15:19:46 -080049 "ccCmd", "cFlags")
Colin Cross3f40fa42015-01-30 17:27:36 -080050
Colin Cross9d45bb72016-08-29 16:14:13 -070051 ld = pctx.AndroidStaticRule("ld",
Colin Cross3f40fa42015-01-30 17:27:36 -080052 blueprint.RuleParams{
Dan Albertce2b8392016-07-21 13:16:49 -070053 Command: "$ldCmd ${crtBegin} @${out}.rsp " +
Colin Cross28344522015-04-22 13:07:53 -070054 "${libFlags} ${crtEnd} -o ${out} ${ldFlags}",
Dan Willemsenc94a7682015-11-17 15:27:28 -080055 CommandDeps: []string{"$ldCmd"},
Colin Cross7d21c442015-03-30 17:47:53 -070056 Rspfile: "${out}.rsp",
57 RspfileContent: "${in}",
Colin Cross3f40fa42015-01-30 17:27:36 -080058 },
Dan Albertce2b8392016-07-21 13:16:49 -070059 "ldCmd", "crtBegin", "libFlags", "crtEnd", "ldFlags")
Colin Cross3f40fa42015-01-30 17:27:36 -080060
Colin Cross9d45bb72016-08-29 16:14:13 -070061 partialLd = pctx.AndroidStaticRule("partialLd",
Colin Cross3f40fa42015-01-30 17:27:36 -080062 blueprint.RuleParams{
Colin Cross41280a42015-11-23 14:01:42 -080063 Command: "$ldCmd -nostdlib -Wl,-r ${in} -o ${out} ${ldFlags}",
Dan Willemsenc94a7682015-11-17 15:27:28 -080064 CommandDeps: []string{"$ldCmd"},
Colin Cross3f40fa42015-01-30 17:27:36 -080065 },
Colin Cross41280a42015-11-23 14:01:42 -080066 "ldCmd", "ldFlags")
Colin Cross3f40fa42015-01-30 17:27:36 -080067
Colin Cross9d45bb72016-08-29 16:14:13 -070068 ar = pctx.AndroidStaticRule("ar",
Colin Cross3f40fa42015-01-30 17:27:36 -080069 blueprint.RuleParams{
Colin Cross7d21c442015-03-30 17:47:53 -070070 Command: "rm -f ${out} && $arCmd $arFlags $out @${out}.rsp",
Dan Willemsenc94a7682015-11-17 15:27:28 -080071 CommandDeps: []string{"$arCmd"},
Colin Cross7d21c442015-03-30 17:47:53 -070072 Rspfile: "${out}.rsp",
73 RspfileContent: "${in}",
Colin Cross3f40fa42015-01-30 17:27:36 -080074 },
75 "arCmd", "arFlags")
76
Colin Cross9d45bb72016-08-29 16:14:13 -070077 darwinAr = pctx.AndroidStaticRule("darwinAr",
Colin Cross0af4b842015-04-30 16:36:18 -070078 blueprint.RuleParams{
Colin Crossb98c8b02016-07-29 13:44:28 -070079 Command: "rm -f ${out} && ${config.MacArPath} $arFlags $out $in",
80 CommandDeps: []string{"${config.MacArPath}"},
Colin Cross0af4b842015-04-30 16:36:18 -070081 },
Colin Crossb8ecdfe2016-05-03 15:10:29 -070082 "arFlags")
Colin Cross0af4b842015-04-30 16:36:18 -070083
Colin Cross9d45bb72016-08-29 16:14:13 -070084 darwinAppendAr = pctx.AndroidStaticRule("darwinAppendAr",
Colin Cross0af4b842015-04-30 16:36:18 -070085 blueprint.RuleParams{
Colin Crossb98c8b02016-07-29 13:44:28 -070086 Command: "cp -f ${inAr} ${out}.tmp && ${config.MacArPath} $arFlags ${out}.tmp $in && mv ${out}.tmp ${out}",
87 CommandDeps: []string{"${config.MacArPath}", "${inAr}"},
Colin Cross0af4b842015-04-30 16:36:18 -070088 },
Colin Crossb8ecdfe2016-05-03 15:10:29 -070089 "arFlags", "inAr")
90
Colin Cross9d45bb72016-08-29 16:14:13 -070091 darwinStrip = pctx.AndroidStaticRule("darwinStrip",
Colin Crossb8ecdfe2016-05-03 15:10:29 -070092 blueprint.RuleParams{
Colin Crossa24166b2016-08-01 15:42:38 -070093 Command: "${config.MacStripPath} -u -r -o $out $in",
94 CommandDeps: []string{"${config.MacStripPath}"},
Colin Crossb8ecdfe2016-05-03 15:10:29 -070095 })
Colin Cross0af4b842015-04-30 16:36:18 -070096
Colin Cross9d45bb72016-08-29 16:14:13 -070097 prefixSymbols = pctx.AndroidStaticRule("prefixSymbols",
Colin Crossbfae8852015-03-26 14:44:11 -070098 blueprint.RuleParams{
99 Command: "$objcopyCmd --prefix-symbols=${prefix} ${in} ${out}",
Dan Willemsenc94a7682015-11-17 15:27:28 -0800100 CommandDeps: []string{"$objcopyCmd"},
Colin Crossbfae8852015-03-26 14:44:11 -0700101 },
102 "objcopyCmd", "prefix")
103
Nan Zhang43a485c2017-03-27 14:27:58 -0700104 _ = pctx.SourcePathVariable("stripPath", "build/soong/scripts/strip.sh")
Colin Cross665dce92016-04-28 14:50:03 -0700105
Colin Cross9d45bb72016-08-29 16:14:13 -0700106 strip = pctx.AndroidStaticRule("strip",
Colin Cross665dce92016-04-28 14:50:03 -0700107 blueprint.RuleParams{
108 Depfile: "${out}.d",
109 Deps: blueprint.DepsGCC,
110 Command: "CROSS_COMPILE=$crossCompile $stripPath ${args} -i ${in} -o ${out} -d ${out}.d",
111 CommandDeps: []string{"$stripPath"},
Colin Cross665dce92016-04-28 14:50:03 -0700112 },
113 "args", "crossCompile")
114
Colin Cross9d45bb72016-08-29 16:14:13 -0700115 emptyFile = pctx.AndroidStaticRule("emptyFile",
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700116 blueprint.RuleParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700117 Command: "rm -f $out && touch $out",
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700118 })
119
Nan Zhang43a485c2017-03-27 14:27:58 -0700120 _ = pctx.SourcePathVariable("copyGccLibPath", "build/soong/scripts/copygcclib.sh")
Colin Cross3f40fa42015-01-30 17:27:36 -0800121
Colin Cross9d45bb72016-08-29 16:14:13 -0700122 copyGccLib = pctx.AndroidStaticRule("copyGccLib",
Colin Cross3f40fa42015-01-30 17:27:36 -0800123 blueprint.RuleParams{
124 Depfile: "${out}.d",
125 Deps: blueprint.DepsGCC,
126 Command: "$copyGccLibPath $out $ccCmd $cFlags -print-file-name=${libName}",
Dan Willemsenc94a7682015-11-17 15:27:28 -0800127 CommandDeps: []string{"$copyGccLibPath", "$ccCmd"},
Colin Cross3f40fa42015-01-30 17:27:36 -0800128 },
129 "ccCmd", "cFlags", "libName")
Colin Cross26c34ed2016-09-30 17:10:16 -0700130
Nan Zhang43a485c2017-03-27 14:27:58 -0700131 _ = pctx.SourcePathVariable("tocPath", "build/soong/scripts/toc.sh")
Colin Cross26c34ed2016-09-30 17:10:16 -0700132
133 toc = pctx.AndroidStaticRule("toc",
134 blueprint.RuleParams{
135 Depfile: "${out}.d",
136 Deps: blueprint.DepsGCC,
137 Command: "CROSS_COMPILE=$crossCompile $tocPath -i ${in} -o ${out} -d ${out}.d",
138 CommandDeps: []string{"$tocPath"},
139 Restat: true,
140 },
141 "crossCompile")
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700142
143 clangTidy = pctx.AndroidStaticRule("clangTidy",
144 blueprint.RuleParams{
145 Command: "rm -f $out && ${config.ClangBin}/clang-tidy $tidyFlags $in -- $cFlags && touch $out",
146 CommandDeps: []string{"${config.ClangBin}/clang-tidy"},
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700147 },
148 "cFlags", "tidyFlags")
Colin Cross91e90042016-12-02 17:13:24 -0800149
Nan Zhang43a485c2017-03-27 14:27:58 -0700150 _ = pctx.SourcePathVariable("yasmCmd", "prebuilts/misc/${config.HostPrebuiltTag}/yasm/yasm")
Colin Cross91e90042016-12-02 17:13:24 -0800151
152 yasm = pctx.AndroidStaticRule("yasm",
153 blueprint.RuleParams{
154 Command: "$yasmCmd $asFlags -o $out $in",
155 CommandDeps: []string{"$yasmCmd"},
Colin Cross91e90042016-12-02 17:13:24 -0800156 },
157 "asFlags")
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800158
159 _ = pctx.SourcePathVariable("sAbiDumper", "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/header-abi-dumper")
160
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700161 // -w has been added since header-abi-dumper does not need to produce any sort of diagnostic information.
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800162 sAbiDump = pctx.AndroidStaticRule("sAbiDump",
163 blueprint.RuleParams{
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700164 Command: "rm -f $out && $sAbiDumper -o ${out} $in $exportDirs -- $cFlags -w -isystem ${config.RSIncludePath}",
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800165 CommandDeps: []string{"$sAbiDumper"},
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800166 },
167 "cFlags", "exportDirs")
168
169 _ = pctx.SourcePathVariable("sAbiLinker", "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/header-abi-linker")
170
171 sAbiLink = pctx.AndroidStaticRule("sAbiLink",
172 blueprint.RuleParams{
173 Command: "$sAbiLinker -o ${out} $symbolFile -arch $arch -api $api $exportedHeaderFlags @${out}.rsp ",
174 CommandDeps: []string{"$sAbiLinker"},
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800175 Rspfile: "${out}.rsp",
176 RspfileContent: "${in}",
177 },
178 "symbolFile", "arch", "api", "exportedHeaderFlags")
179
180 _ = pctx.SourcePathVariable("sAbiDiffer", "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/header-abi-diff")
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700181
Jayant Chowdhary918b1d92017-04-18 10:44:00 -0700182 // Abidiff check turned on in advice-only mode. Builds will not fail on abi incompatibilties / extensions.
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800183 sAbiDiff = pctx.AndroidStaticRule("sAbiDiff",
184 blueprint.RuleParams{
Jayant Chowdharyf54e0a72017-05-22 10:53:24 -0700185 Command: "$sAbiDiffer -lib $libName -arch $arch -advice-only -o ${out} -new $in -old $referenceDump",
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800186 CommandDeps: []string{"$sAbiDiffer"},
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800187 },
Jayant Chowdharyf54e0a72017-05-22 10:53:24 -0700188 "referenceDump", "libName", "arch")
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700189
190 unzipRefSAbiDump = pctx.AndroidStaticRule("unzipRefSAbiDump",
191 blueprint.RuleParams{
192 Command: "gunzip -c $in > $out",
193 })
Colin Cross3f40fa42015-01-30 17:27:36 -0800194)
195
Dan Willemsen322a0a62015-11-17 15:19:46 -0800196func init() {
197 // We run gcc/clang with PWD=/proc/self/cwd to remove $TOP from the
198 // debug output. That way two builds in two different directories will
199 // create the same output.
200 if runtime.GOOS != "darwin" {
201 pctx.StaticVariable("relPwd", "PWD=/proc/self/cwd")
202 } else {
203 // Darwin doesn't have /proc
204 pctx.StaticVariable("relPwd", "")
205 }
206}
207
Colin Cross3f40fa42015-01-30 17:27:36 -0800208type builderFlags struct {
209 globalFlags string
Vishwath Mohan83d9f712017-03-16 11:01:23 -0700210 arFlags string
Colin Cross3f40fa42015-01-30 17:27:36 -0800211 asFlags string
212 cFlags string
213 conlyFlags string
214 cppFlags string
215 ldFlags string
Colin Cross16b23492016-01-06 14:41:07 -0800216 libFlags string
Colin Cross581c1892015-04-07 16:50:10 -0700217 yaccFlags string
Colin Cross0c461f12016-10-20 16:11:43 -0700218 protoFlags string
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700219 tidyFlags string
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800220 sAbiFlags string
Colin Cross91e90042016-12-02 17:13:24 -0800221 yasmFlags string
Dan Willemsene1240db2016-11-03 14:28:51 -0700222 aidlFlags string
Colin Cross2a252be2017-05-01 17:37:24 -0700223 rsFlags string
Colin Crossb98c8b02016-07-29 13:44:28 -0700224 toolchain config.Toolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800225 clang bool
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700226 tidy bool
Dan Willemsen581341d2017-02-09 16:16:31 -0800227 coverage bool
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800228 sAbiDump bool
Colin Cross665dce92016-04-28 14:50:03 -0700229
Colin Crossc3199482017-03-30 15:03:04 -0700230 systemIncludeFlags string
231
Colin Cross18c0c5a2016-12-01 14:45:23 -0800232 groupStaticLibs bool
233
Colin Cross665dce92016-04-28 14:50:03 -0700234 stripKeepSymbols bool
235 stripKeepMiniDebugInfo bool
236 stripAddGnuDebuglink bool
Colin Cross3f40fa42015-01-30 17:27:36 -0800237}
238
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700239type Objects struct {
Dan Willemsen581341d2017-02-09 16:16:31 -0800240 objFiles android.Paths
241 tidyFiles android.Paths
242 coverageFiles android.Paths
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800243 sAbiDumpFiles android.Paths
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700244}
245
246func (a Objects) Copy() Objects {
247 return Objects{
Dan Willemsen581341d2017-02-09 16:16:31 -0800248 objFiles: append(android.Paths{}, a.objFiles...),
249 tidyFiles: append(android.Paths{}, a.tidyFiles...),
250 coverageFiles: append(android.Paths{}, a.coverageFiles...),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800251 sAbiDumpFiles: append(android.Paths{}, a.sAbiDumpFiles...),
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700252 }
253}
254
255func (a Objects) Append(b Objects) Objects {
256 return Objects{
Dan Willemsen581341d2017-02-09 16:16:31 -0800257 objFiles: append(a.objFiles, b.objFiles...),
258 tidyFiles: append(a.tidyFiles, b.tidyFiles...),
259 coverageFiles: append(a.coverageFiles, b.coverageFiles...),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800260 sAbiDumpFiles: append(a.sAbiDumpFiles, b.sAbiDumpFiles...),
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700261 }
262}
263
Colin Cross3f40fa42015-01-30 17:27:36 -0800264// Generate rules for compiling multiple .c, .cpp, or .S files to individual .o files
Colin Cross635c3b02016-05-18 15:37:25 -0700265func TransformSourceToObj(ctx android.ModuleContext, subdir string, srcFiles android.Paths,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700266 flags builderFlags, deps android.Paths) Objects {
Colin Cross581c1892015-04-07 16:50:10 -0700267
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700268 objFiles := make(android.Paths, len(srcFiles))
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700269 var tidyFiles android.Paths
270 if flags.tidy && flags.clang {
271 tidyFiles = make(android.Paths, 0, len(srcFiles))
272 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800273 var coverageFiles android.Paths
274 if flags.coverage {
275 coverageFiles = make(android.Paths, 0, len(srcFiles))
276 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800277
Colin Crossc3199482017-03-30 15:03:04 -0700278 cflags := strings.Join([]string{
279 flags.globalFlags,
280 flags.systemIncludeFlags,
281 flags.cFlags,
282 flags.conlyFlags,
283 }, " ")
284
285 cppflags := strings.Join([]string{
286 flags.globalFlags,
287 flags.systemIncludeFlags,
288 flags.cFlags,
289 flags.cppFlags,
290 }, " ")
291
292 asflags := strings.Join([]string{
293 flags.globalFlags,
294 flags.systemIncludeFlags,
295 flags.asFlags,
296 }, " ")
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800297 var sAbiDumpFiles android.Paths
298 if flags.sAbiDump && flags.clang {
299 sAbiDumpFiles = make(android.Paths, 0, len(srcFiles))
300 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800301
Dan Willemsenbe03f342016-03-03 17:21:04 -0800302 if flags.clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700303 cflags += " ${config.NoOverrideClangGlobalCflags}"
304 cppflags += " ${config.NoOverrideClangGlobalCflags}"
Dan Willemsenbe03f342016-03-03 17:21:04 -0800305 } else {
Colin Crossb98c8b02016-07-29 13:44:28 -0700306 cflags += " ${config.NoOverrideGlobalCflags}"
307 cppflags += " ${config.NoOverrideGlobalCflags}"
Dan Willemsenbe03f342016-03-03 17:21:04 -0800308 }
309
Colin Cross3f40fa42015-01-30 17:27:36 -0800310 for i, srcFile := range srcFiles {
Dan Willemsen21ec4902016-11-02 20:43:13 -0700311 objFile := android.ObjPathWithExt(ctx, subdir, srcFile, "o")
Colin Cross3f40fa42015-01-30 17:27:36 -0800312
313 objFiles[i] = objFile
314
Colin Cross91e90042016-12-02 17:13:24 -0800315 if srcFile.Ext() == ".asm" {
316 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700317 Rule: yasm,
318 Description: "yasm " + srcFile.Rel(),
319 Output: objFile,
320 Input: srcFile,
321 OrderOnly: deps,
Colin Cross91e90042016-12-02 17:13:24 -0800322 Args: map[string]string{
323 "asFlags": flags.yasmFlags,
324 },
325 })
326 continue
327 }
328
Colin Cross3f40fa42015-01-30 17:27:36 -0800329 var moduleCflags string
330 var ccCmd string
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700331 tidy := flags.tidy && flags.clang
Dan Willemsen581341d2017-02-09 16:16:31 -0800332 coverage := flags.coverage
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800333 dump := flags.sAbiDump && flags.clang
Colin Cross3f40fa42015-01-30 17:27:36 -0800334
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700335 switch srcFile.Ext() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800336 case ".S", ".s":
337 ccCmd = "gcc"
338 moduleCflags = asflags
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700339 tidy = false
Dan Willemsen581341d2017-02-09 16:16:31 -0800340 coverage = false
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800341 dump = false
Colin Cross3f40fa42015-01-30 17:27:36 -0800342 case ".c":
343 ccCmd = "gcc"
344 moduleCflags = cflags
Colin Cross9978ffe2016-12-01 15:31:22 -0800345 case ".cpp", ".cc", ".mm":
Colin Cross3f40fa42015-01-30 17:27:36 -0800346 ccCmd = "g++"
347 moduleCflags = cppflags
348 default:
349 ctx.ModuleErrorf("File %s has unknown extension", srcFile)
350 continue
351 }
352
353 if flags.clang {
354 switch ccCmd {
355 case "gcc":
356 ccCmd = "clang"
357 case "g++":
358 ccCmd = "clang++"
359 default:
360 panic("unrecoginzied ccCmd")
361 }
Colin Cross67a5c132017-05-09 13:45:28 -0700362 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800363
Colin Cross67a5c132017-05-09 13:45:28 -0700364 ccDesc := ccCmd
365
366 if flags.clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700367 ccCmd = "${config.ClangBin}/" + ccCmd
Colin Cross3f40fa42015-01-30 17:27:36 -0800368 } else {
369 ccCmd = gccCmd(flags.toolchain, ccCmd)
370 }
371
Dan Willemsen581341d2017-02-09 16:16:31 -0800372 var implicitOutputs android.WritablePaths
373 if coverage {
374 gcnoFile := android.ObjPathWithExt(ctx, subdir, srcFile, "gcno")
375 implicitOutputs = append(implicitOutputs, gcnoFile)
376 coverageFiles = append(coverageFiles, gcnoFile)
377 }
378
Colin Cross635c3b02016-05-18 15:37:25 -0700379 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Dan Willemsen581341d2017-02-09 16:16:31 -0800380 Rule: cc,
Colin Cross67a5c132017-05-09 13:45:28 -0700381 Description: ccDesc + " " + srcFile.Rel(),
Dan Willemsen581341d2017-02-09 16:16:31 -0800382 Output: objFile,
383 ImplicitOutputs: implicitOutputs,
384 Input: srcFile,
385 OrderOnly: deps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800386 Args: map[string]string{
Colin Cross28344522015-04-22 13:07:53 -0700387 "cFlags": moduleCflags,
388 "ccCmd": ccCmd,
Colin Cross3f40fa42015-01-30 17:27:36 -0800389 },
390 })
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700391
392 if tidy {
Dan Willemsen21ec4902016-11-02 20:43:13 -0700393 tidyFile := android.ObjPathWithExt(ctx, subdir, srcFile, "tidy")
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700394 tidyFiles = append(tidyFiles, tidyFile)
395
396 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700397 Rule: clangTidy,
398 Description: "clang-tidy " + srcFile.Rel(),
399 Output: tidyFile,
400 Input: srcFile,
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700401 // We must depend on objFile, since clang-tidy doesn't
402 // support exporting dependencies.
403 Implicit: objFile,
404 Args: map[string]string{
405 "cFlags": moduleCflags,
406 "tidyFlags": flags.tidyFlags,
407 },
408 })
409 }
410
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800411 if dump {
412 sAbiDumpFile := android.ObjPathWithExt(ctx, subdir, srcFile, "sdump")
413 sAbiDumpFiles = append(sAbiDumpFiles, sAbiDumpFile)
414
415 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700416 Rule: sAbiDump,
417 Description: "header-abi-dumper " + srcFile.Rel(),
418 Output: sAbiDumpFile,
419 Input: srcFile,
420 Implicit: objFile,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800421 Args: map[string]string{
422 "cFlags": moduleCflags,
423 "exportDirs": flags.sAbiFlags,
424 },
425 })
426 }
427
Colin Cross3f40fa42015-01-30 17:27:36 -0800428 }
429
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700430 return Objects{
Dan Willemsen581341d2017-02-09 16:16:31 -0800431 objFiles: objFiles,
432 tidyFiles: tidyFiles,
433 coverageFiles: coverageFiles,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800434 sAbiDumpFiles: sAbiDumpFiles,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700435 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800436}
437
438// Generate a rule for compiling multiple .o files to a static library (.a)
Colin Cross635c3b02016-05-18 15:37:25 -0700439func TransformObjToStaticLib(ctx android.ModuleContext, objFiles android.Paths,
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700440 flags builderFlags, outputFile android.ModuleOutPath, deps android.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800441
Dan Willemsen581341d2017-02-09 16:16:31 -0800442 if ctx.Darwin() {
443 transformDarwinObjToStaticLib(ctx, objFiles, flags, outputFile, deps)
444 return
445 }
446
Colin Cross3f40fa42015-01-30 17:27:36 -0800447 arCmd := gccCmd(flags.toolchain, "ar")
448 arFlags := "crsPD"
Vishwath Mohan83d9f712017-03-16 11:01:23 -0700449 if flags.arFlags != "" {
450 arFlags += " " + flags.arFlags
451 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800452
Colin Cross635c3b02016-05-18 15:37:25 -0700453 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700454 Rule: ar,
455 Description: "static link " + outputFile.Base(),
456 Output: outputFile,
457 Inputs: objFiles,
458 Implicits: deps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800459 Args: map[string]string{
460 "arFlags": arFlags,
461 "arCmd": arCmd,
462 },
463 })
464}
465
Colin Cross0af4b842015-04-30 16:36:18 -0700466// Generate a rule for compiling multiple .o files to a static library (.a) on
467// darwin. The darwin ar tool doesn't support @file for list files, and has a
468// very small command line length limit, so we have to split the ar into multiple
469// steps, each appending to the previous one.
Dan Willemsen581341d2017-02-09 16:16:31 -0800470func transformDarwinObjToStaticLib(ctx android.ModuleContext, objFiles android.Paths,
Colin Cross5b529592017-05-09 13:34:34 -0700471 flags builderFlags, outputFile android.ModuleOutPath, deps android.Paths) {
Colin Cross0af4b842015-04-30 16:36:18 -0700472
Colin Cross0af4b842015-04-30 16:36:18 -0700473 arFlags := "cqs"
474
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700475 if len(objFiles) == 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700476 dummy := android.PathForModuleOut(ctx, "dummy"+objectExtension)
477 dummyAr := android.PathForModuleOut(ctx, "dummy"+staticLibraryExtension)
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700478
Colin Cross635c3b02016-05-18 15:37:25 -0700479 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700480 Rule: emptyFile,
481 Description: "empty object file",
482 Output: dummy,
483 Implicits: deps,
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700484 })
485
Colin Cross635c3b02016-05-18 15:37:25 -0700486 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700487 Rule: darwinAr,
488 Description: "empty static archive",
489 Output: dummyAr,
490 Input: dummy,
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700491 Args: map[string]string{
492 "arFlags": arFlags,
493 },
494 })
495
Colin Cross635c3b02016-05-18 15:37:25 -0700496 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700497 Rule: darwinAppendAr,
498 Description: "static link " + outputFile.Base(),
499 Output: outputFile,
500 Input: dummy,
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700501 Args: map[string]string{
502 "arFlags": "d",
503 "inAr": dummyAr.String(),
504 },
505 })
506
507 return
508 }
509
Colin Cross0af4b842015-04-30 16:36:18 -0700510 // ARG_MAX on darwin is 262144, use half that to be safe
Colin Cross5b529592017-05-09 13:34:34 -0700511 objFilesLists, err := splitListForSize(objFiles, 131072)
Colin Cross0af4b842015-04-30 16:36:18 -0700512 if err != nil {
513 ctx.ModuleErrorf("%s", err.Error())
514 }
515
Colin Cross5b529592017-05-09 13:34:34 -0700516 var in, out android.WritablePath
Colin Cross0af4b842015-04-30 16:36:18 -0700517 for i, l := range objFilesLists {
518 in = out
519 out = outputFile
520 if i != len(objFilesLists)-1 {
Colin Cross5b529592017-05-09 13:34:34 -0700521 out = android.PathForModuleOut(ctx, outputFile.Base()+strconv.Itoa(i))
Colin Cross0af4b842015-04-30 16:36:18 -0700522 }
523
Colin Cross5b529592017-05-09 13:34:34 -0700524 build := android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700525 Rule: darwinAr,
526 Description: "static link " + out.Base(),
527 Output: out,
528 Inputs: l,
529 Implicits: deps,
Colin Cross5b529592017-05-09 13:34:34 -0700530 Args: map[string]string{
531 "arFlags": arFlags,
532 },
Colin Cross0af4b842015-04-30 16:36:18 -0700533 }
Colin Cross5b529592017-05-09 13:34:34 -0700534 if i != 0 {
535 build.Rule = darwinAppendAr
536 build.Args["inAr"] = in.String()
537 }
538 ctx.ModuleBuild(pctx, build)
Colin Cross0af4b842015-04-30 16:36:18 -0700539 }
540}
541
Colin Cross3f40fa42015-01-30 17:27:36 -0800542// Generate a rule for compiling multiple .o files, plus static libraries, whole static libraries,
543// and shared libraires, to a shared library (.so) or dynamic executable
Colin Cross635c3b02016-05-18 15:37:25 -0700544func TransformObjToDynamicBinary(ctx android.ModuleContext,
545 objFiles, sharedLibs, staticLibs, lateStaticLibs, wholeStaticLibs, deps android.Paths,
546 crtBegin, crtEnd android.OptionalPath, groupLate bool, flags builderFlags, outputFile android.WritablePath) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800547
548 var ldCmd string
549 if flags.clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700550 ldCmd = "${config.ClangBin}/clang++"
Colin Cross3f40fa42015-01-30 17:27:36 -0800551 } else {
552 ldCmd = gccCmd(flags.toolchain, "g++")
553 }
554
Colin Cross3f40fa42015-01-30 17:27:36 -0800555 var libFlagsList []string
556
Colin Cross16b23492016-01-06 14:41:07 -0800557 if len(flags.libFlags) > 0 {
558 libFlagsList = append(libFlagsList, flags.libFlags)
559 }
560
Colin Cross3f40fa42015-01-30 17:27:36 -0800561 if len(wholeStaticLibs) > 0 {
Dan Willemsen490fd492015-11-24 17:53:15 -0800562 if ctx.Host() && ctx.Darwin() {
Colin Cross635c3b02016-05-18 15:37:25 -0700563 libFlagsList = append(libFlagsList, android.JoinWithPrefix(wholeStaticLibs.Strings(), "-force_load "))
Colin Cross0af4b842015-04-30 16:36:18 -0700564 } else {
565 libFlagsList = append(libFlagsList, "-Wl,--whole-archive ")
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700566 libFlagsList = append(libFlagsList, wholeStaticLibs.Strings()...)
Colin Cross0af4b842015-04-30 16:36:18 -0700567 libFlagsList = append(libFlagsList, "-Wl,--no-whole-archive ")
568 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800569 }
570
Colin Cross7a7cf972016-12-05 18:47:39 -0800571 if flags.groupStaticLibs && !ctx.Darwin() && len(staticLibs) > 0 {
Colin Cross18c0c5a2016-12-01 14:45:23 -0800572 libFlagsList = append(libFlagsList, "-Wl,--start-group")
573 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700574 libFlagsList = append(libFlagsList, staticLibs.Strings()...)
Colin Cross7a7cf972016-12-05 18:47:39 -0800575 if flags.groupStaticLibs && !ctx.Darwin() && len(staticLibs) > 0 {
Colin Cross18c0c5a2016-12-01 14:45:23 -0800576 libFlagsList = append(libFlagsList, "-Wl,--end-group")
577 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800578
Stephen Hines10347862016-07-18 15:54:54 -0700579 if groupLate && !ctx.Darwin() && len(lateStaticLibs) > 0 {
Dan Willemsenedc385f2015-07-08 13:02:23 -0700580 libFlagsList = append(libFlagsList, "-Wl,--start-group")
581 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700582 libFlagsList = append(libFlagsList, lateStaticLibs.Strings()...)
Stephen Hines10347862016-07-18 15:54:54 -0700583 if groupLate && !ctx.Darwin() && len(lateStaticLibs) > 0 {
Dan Willemsenedc385f2015-07-08 13:02:23 -0700584 libFlagsList = append(libFlagsList, "-Wl,--end-group")
585 }
586
Colin Cross3f40fa42015-01-30 17:27:36 -0800587 for _, lib := range sharedLibs {
Dan Albert9840e1b2016-07-21 08:47:33 -0700588 libFlagsList = append(libFlagsList, lib.String())
Colin Cross3f40fa42015-01-30 17:27:36 -0800589 }
590
Colin Cross3f40fa42015-01-30 17:27:36 -0800591 deps = append(deps, staticLibs...)
Colin Cross3075ad02015-03-17 10:47:08 -0700592 deps = append(deps, lateStaticLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800593 deps = append(deps, wholeStaticLibs...)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700594 if crtBegin.Valid() {
595 deps = append(deps, crtBegin.Path(), crtEnd.Path())
Colin Cross3f40fa42015-01-30 17:27:36 -0800596 }
597
Colin Cross635c3b02016-05-18 15:37:25 -0700598 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700599 Rule: ld,
600 Description: "link " + outputFile.Base(),
601 Output: outputFile,
602 Inputs: objFiles,
603 Implicits: deps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800604 Args: map[string]string{
Dan Albertce2b8392016-07-21 13:16:49 -0700605 "ldCmd": ldCmd,
606 "crtBegin": crtBegin.String(),
607 "libFlags": strings.Join(libFlagsList, " "),
608 "ldFlags": flags.ldFlags,
609 "crtEnd": crtEnd.String(),
Colin Cross3f40fa42015-01-30 17:27:36 -0800610 },
611 })
612}
613
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800614// Generate a rule to combine .dump sAbi dump files from multiple source files
615// into a single .ldump sAbi dump file
616func TransformDumpToLinkedDump(ctx android.ModuleContext, sAbiDumps android.Paths,
617 symbolFile android.OptionalPath, apiLevel, baseName, exportedHeaderFlags string) android.OptionalPath {
618 outputFile := android.PathForModuleOut(ctx, baseName+".lsdump")
619 var symbolFileStr string
620 var linkedDumpDep android.Path
621 if symbolFile.Valid() {
622 symbolFileStr = "-v " + symbolFile.Path().String()
623 linkedDumpDep = symbolFile.Path()
624 }
625 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700626 Rule: sAbiLink,
627 Description: "header-abi-linker " + outputFile.Base(),
628 Output: outputFile,
629 Inputs: sAbiDumps,
630 Implicit: linkedDumpDep,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800631 Args: map[string]string{
632 "symbolFile": symbolFileStr,
633 "arch": ctx.Arch().ArchType.Name,
634 "api": apiLevel,
635 "exportedHeaderFlags": exportedHeaderFlags,
636 },
637 })
638 return android.OptionalPathForPath(outputFile)
639}
640
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700641func UnzipRefDump(ctx android.ModuleContext, zippedRefDump android.Path, baseName string) android.Path {
642 outputFile := android.PathForModuleOut(ctx, baseName+"_ref.lsdump")
643 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
644 Rule: unzipRefSAbiDump,
645 Description: "gunzip" + outputFile.Base(),
646 Output: outputFile,
647 Input: zippedRefDump,
648 })
649 return outputFile
650}
651
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800652func SourceAbiDiff(ctx android.ModuleContext, inputDump android.Path, referenceDump android.Path,
653 baseName string) android.OptionalPath {
654 outputFile := android.PathForModuleOut(ctx, baseName+".abidiff")
655 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700656 Rule: sAbiDiff,
657 Description: "header-abi-diff " + outputFile.Base(),
658 Output: outputFile,
659 Input: inputDump,
660 Implicit: referenceDump,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800661 Args: map[string]string{
662 "referenceDump": referenceDump.String(),
Jayant Chowdharyf54e0a72017-05-22 10:53:24 -0700663 "libName": baseName,
664 "arch": ctx.Arch().ArchType.Name,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800665 },
666 })
667 return android.OptionalPathForPath(outputFile)
668}
669
Colin Cross26c34ed2016-09-30 17:10:16 -0700670// Generate a rule for extract a table of contents from a shared library (.so)
671func TransformSharedObjectToToc(ctx android.ModuleContext, inputFile android.WritablePath,
672 outputFile android.WritablePath, flags builderFlags) {
673
674 crossCompile := gccCmd(flags.toolchain, "")
675
676 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700677 Rule: toc,
678 Description: "generate toc " + inputFile.Base(),
679 Output: outputFile,
680 Input: inputFile,
Colin Cross26c34ed2016-09-30 17:10:16 -0700681 Args: map[string]string{
682 "crossCompile": crossCompile,
683 },
684 })
685}
686
Colin Cross3f40fa42015-01-30 17:27:36 -0800687// Generate a rule for compiling multiple .o files to a .o using ld partial linking
Colin Cross635c3b02016-05-18 15:37:25 -0700688func TransformObjsToObj(ctx android.ModuleContext, objFiles android.Paths,
689 flags builderFlags, outputFile android.WritablePath) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800690
Colin Cross41280a42015-11-23 14:01:42 -0800691 var ldCmd string
692 if flags.clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700693 ldCmd = "${config.ClangBin}/clang++"
Colin Cross41280a42015-11-23 14:01:42 -0800694 } else {
695 ldCmd = gccCmd(flags.toolchain, "g++")
696 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800697
Colin Cross635c3b02016-05-18 15:37:25 -0700698 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700699 Rule: partialLd,
700 Description: "link " + outputFile.Base(),
701 Output: outputFile,
702 Inputs: objFiles,
Colin Cross3f40fa42015-01-30 17:27:36 -0800703 Args: map[string]string{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700704 "ldCmd": ldCmd,
Colin Cross41280a42015-11-23 14:01:42 -0800705 "ldFlags": flags.ldFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800706 },
707 })
708}
709
Colin Crossbfae8852015-03-26 14:44:11 -0700710// Generate a rule for runing objcopy --prefix-symbols on a binary
Colin Cross635c3b02016-05-18 15:37:25 -0700711func TransformBinaryPrefixSymbols(ctx android.ModuleContext, prefix string, inputFile android.Path,
712 flags builderFlags, outputFile android.WritablePath) {
Colin Crossbfae8852015-03-26 14:44:11 -0700713
714 objcopyCmd := gccCmd(flags.toolchain, "objcopy")
715
Colin Cross635c3b02016-05-18 15:37:25 -0700716 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700717 Rule: prefixSymbols,
718 Description: "prefix symbols " + outputFile.Base(),
719 Output: outputFile,
720 Input: inputFile,
Colin Crossbfae8852015-03-26 14:44:11 -0700721 Args: map[string]string{
722 "objcopyCmd": objcopyCmd,
723 "prefix": prefix,
724 },
725 })
726}
727
Colin Cross635c3b02016-05-18 15:37:25 -0700728func TransformStrip(ctx android.ModuleContext, inputFile android.Path,
729 outputFile android.WritablePath, flags builderFlags) {
Colin Cross665dce92016-04-28 14:50:03 -0700730
731 crossCompile := gccCmd(flags.toolchain, "")
732 args := ""
733 if flags.stripAddGnuDebuglink {
734 args += " --add-gnu-debuglink"
735 }
736 if flags.stripKeepMiniDebugInfo {
737 args += " --keep-mini-debug-info"
738 }
739 if flags.stripKeepSymbols {
740 args += " --keep-symbols"
741 }
742
Colin Cross635c3b02016-05-18 15:37:25 -0700743 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700744 Rule: strip,
745 Description: "strip " + outputFile.Base(),
746 Output: outputFile,
747 Input: inputFile,
Colin Cross665dce92016-04-28 14:50:03 -0700748 Args: map[string]string{
749 "crossCompile": crossCompile,
750 "args": args,
751 },
752 })
753}
754
Colin Cross635c3b02016-05-18 15:37:25 -0700755func TransformDarwinStrip(ctx android.ModuleContext, inputFile android.Path,
756 outputFile android.WritablePath) {
Colin Crossb8ecdfe2016-05-03 15:10:29 -0700757
Colin Cross635c3b02016-05-18 15:37:25 -0700758 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700759 Rule: darwinStrip,
760 Description: "strip " + outputFile.Base(),
761 Output: outputFile,
762 Input: inputFile,
Colin Crossb8ecdfe2016-05-03 15:10:29 -0700763 })
764}
765
Dan Willemsen581341d2017-02-09 16:16:31 -0800766func TransformCoverageFilesToLib(ctx android.ModuleContext,
767 inputs Objects, flags builderFlags, baseName string) android.OptionalPath {
768
769 if len(inputs.coverageFiles) > 0 {
770 outputFile := android.PathForModuleOut(ctx, baseName+".gcnodir")
771
772 TransformObjToStaticLib(ctx, inputs.coverageFiles, flags, outputFile, nil)
773
774 return android.OptionalPathForPath(outputFile)
775 }
776
777 return android.OptionalPath{}
778}
779
Colin Cross635c3b02016-05-18 15:37:25 -0700780func CopyGccLib(ctx android.ModuleContext, libName string,
781 flags builderFlags, outputFile android.WritablePath) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800782
Colin Cross635c3b02016-05-18 15:37:25 -0700783 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700784 Rule: copyGccLib,
785 Description: "copy gcc library " + libName,
786 Output: outputFile,
Colin Cross3f40fa42015-01-30 17:27:36 -0800787 Args: map[string]string{
788 "ccCmd": gccCmd(flags.toolchain, "gcc"),
789 "cFlags": flags.globalFlags,
790 "libName": libName,
791 },
792 })
793}
794
Colin Crossb98c8b02016-07-29 13:44:28 -0700795func gccCmd(toolchain config.Toolchain, cmd string) string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800796 return filepath.Join(toolchain.GccRoot(), "bin", toolchain.GccTriple()+"-"+cmd)
797}
Colin Cross0af4b842015-04-30 16:36:18 -0700798
Colin Cross5b529592017-05-09 13:34:34 -0700799func splitListForSize(list android.Paths, limit int) (lists []android.Paths, err error) {
Colin Cross0af4b842015-04-30 16:36:18 -0700800 var i int
801
802 start := 0
803 bytes := 0
804 for i = range list {
Colin Cross5b529592017-05-09 13:34:34 -0700805 l := len(list[i].String())
Colin Cross0af4b842015-04-30 16:36:18 -0700806 if l > limit {
807 return nil, fmt.Errorf("list element greater than size limit (%d)", limit)
808 }
809 if bytes+l > limit {
810 lists = append(lists, list[start:i])
811 start = i
812 bytes = 0
813 }
814 bytes += l + 1 // count a space between each list element
815 }
816
817 lists = append(lists, list[start:])
818
819 totalLen := 0
820 for _, l := range lists {
821 totalLen += len(l)
822 }
823 if totalLen != len(list) {
824 panic(fmt.Errorf("Failed breaking up list, %d != %d", len(list), totalLen))
825 }
826 return lists, nil
827}