blob: a4fda5bc4ad9c56a52f22b91cb076c795ea5a014 [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
161 sAbiDump = pctx.AndroidStaticRule("sAbiDump",
162 blueprint.RuleParams{
Jayant Chowdhary918b1d92017-04-18 10:44:00 -0700163 Command: "rm -f $out && $sAbiDumper -o ${out} $in $exportDirs -- $cFlags -Wno-packed -Qunused-arguments -isystem ${config.RSIncludePath}",
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800164 CommandDeps: []string{"$sAbiDumper"},
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800165 },
166 "cFlags", "exportDirs")
167
168 _ = pctx.SourcePathVariable("sAbiLinker", "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/header-abi-linker")
169
170 sAbiLink = pctx.AndroidStaticRule("sAbiLink",
171 blueprint.RuleParams{
172 Command: "$sAbiLinker -o ${out} $symbolFile -arch $arch -api $api $exportedHeaderFlags @${out}.rsp ",
173 CommandDeps: []string{"$sAbiLinker"},
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800174 Rspfile: "${out}.rsp",
175 RspfileContent: "${in}",
176 },
177 "symbolFile", "arch", "api", "exportedHeaderFlags")
178
179 _ = pctx.SourcePathVariable("sAbiDiffer", "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/header-abi-diff")
Jayant Chowdhary918b1d92017-04-18 10:44:00 -0700180 // Abidiff check turned on in advice-only mode. Builds will not fail on abi incompatibilties / extensions.
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800181 sAbiDiff = pctx.AndroidStaticRule("sAbiDiff",
182 blueprint.RuleParams{
Jayant Chowdhary918b1d92017-04-18 10:44:00 -0700183 Command: "$sAbiDiffer -advice-only -o ${out} -new $in -old $referenceDump",
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800184 CommandDeps: []string{"$sAbiDiffer"},
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800185 },
186 "referenceDump")
Colin Cross3f40fa42015-01-30 17:27:36 -0800187)
188
Dan Willemsen322a0a62015-11-17 15:19:46 -0800189func init() {
190 // We run gcc/clang with PWD=/proc/self/cwd to remove $TOP from the
191 // debug output. That way two builds in two different directories will
192 // create the same output.
193 if runtime.GOOS != "darwin" {
194 pctx.StaticVariable("relPwd", "PWD=/proc/self/cwd")
195 } else {
196 // Darwin doesn't have /proc
197 pctx.StaticVariable("relPwd", "")
198 }
199}
200
Colin Cross3f40fa42015-01-30 17:27:36 -0800201type builderFlags struct {
202 globalFlags string
Vishwath Mohan83d9f712017-03-16 11:01:23 -0700203 arFlags string
Colin Cross3f40fa42015-01-30 17:27:36 -0800204 asFlags string
205 cFlags string
206 conlyFlags string
207 cppFlags string
208 ldFlags string
Colin Cross16b23492016-01-06 14:41:07 -0800209 libFlags string
Colin Cross581c1892015-04-07 16:50:10 -0700210 yaccFlags string
Colin Cross0c461f12016-10-20 16:11:43 -0700211 protoFlags string
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700212 tidyFlags string
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800213 sAbiFlags string
Colin Cross91e90042016-12-02 17:13:24 -0800214 yasmFlags string
Dan Willemsene1240db2016-11-03 14:28:51 -0700215 aidlFlags string
Colin Cross2a252be2017-05-01 17:37:24 -0700216 rsFlags string
Colin Crossb98c8b02016-07-29 13:44:28 -0700217 toolchain config.Toolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800218 clang bool
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700219 tidy bool
Dan Willemsen581341d2017-02-09 16:16:31 -0800220 coverage bool
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800221 sAbiDump bool
Colin Cross665dce92016-04-28 14:50:03 -0700222
Colin Crossc3199482017-03-30 15:03:04 -0700223 systemIncludeFlags string
224
Colin Cross18c0c5a2016-12-01 14:45:23 -0800225 groupStaticLibs bool
226
Colin Cross665dce92016-04-28 14:50:03 -0700227 stripKeepSymbols bool
228 stripKeepMiniDebugInfo bool
229 stripAddGnuDebuglink bool
Colin Cross3f40fa42015-01-30 17:27:36 -0800230}
231
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700232type Objects struct {
Dan Willemsen581341d2017-02-09 16:16:31 -0800233 objFiles android.Paths
234 tidyFiles android.Paths
235 coverageFiles android.Paths
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800236 sAbiDumpFiles android.Paths
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700237}
238
239func (a Objects) Copy() Objects {
240 return Objects{
Dan Willemsen581341d2017-02-09 16:16:31 -0800241 objFiles: append(android.Paths{}, a.objFiles...),
242 tidyFiles: append(android.Paths{}, a.tidyFiles...),
243 coverageFiles: append(android.Paths{}, a.coverageFiles...),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800244 sAbiDumpFiles: append(android.Paths{}, a.sAbiDumpFiles...),
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700245 }
246}
247
248func (a Objects) Append(b Objects) Objects {
249 return Objects{
Dan Willemsen581341d2017-02-09 16:16:31 -0800250 objFiles: append(a.objFiles, b.objFiles...),
251 tidyFiles: append(a.tidyFiles, b.tidyFiles...),
252 coverageFiles: append(a.coverageFiles, b.coverageFiles...),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800253 sAbiDumpFiles: append(a.sAbiDumpFiles, b.sAbiDumpFiles...),
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700254 }
255}
256
Colin Cross3f40fa42015-01-30 17:27:36 -0800257// Generate rules for compiling multiple .c, .cpp, or .S files to individual .o files
Colin Cross635c3b02016-05-18 15:37:25 -0700258func TransformSourceToObj(ctx android.ModuleContext, subdir string, srcFiles android.Paths,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700259 flags builderFlags, deps android.Paths) Objects {
Colin Cross581c1892015-04-07 16:50:10 -0700260
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700261 objFiles := make(android.Paths, len(srcFiles))
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700262 var tidyFiles android.Paths
263 if flags.tidy && flags.clang {
264 tidyFiles = make(android.Paths, 0, len(srcFiles))
265 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800266 var coverageFiles android.Paths
267 if flags.coverage {
268 coverageFiles = make(android.Paths, 0, len(srcFiles))
269 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800270
Colin Crossc3199482017-03-30 15:03:04 -0700271 cflags := strings.Join([]string{
272 flags.globalFlags,
273 flags.systemIncludeFlags,
274 flags.cFlags,
275 flags.conlyFlags,
276 }, " ")
277
278 cppflags := strings.Join([]string{
279 flags.globalFlags,
280 flags.systemIncludeFlags,
281 flags.cFlags,
282 flags.cppFlags,
283 }, " ")
284
285 asflags := strings.Join([]string{
286 flags.globalFlags,
287 flags.systemIncludeFlags,
288 flags.asFlags,
289 }, " ")
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800290 var sAbiDumpFiles android.Paths
291 if flags.sAbiDump && flags.clang {
292 sAbiDumpFiles = make(android.Paths, 0, len(srcFiles))
293 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800294
Dan Willemsenbe03f342016-03-03 17:21:04 -0800295 if flags.clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700296 cflags += " ${config.NoOverrideClangGlobalCflags}"
297 cppflags += " ${config.NoOverrideClangGlobalCflags}"
Dan Willemsenbe03f342016-03-03 17:21:04 -0800298 } else {
Colin Crossb98c8b02016-07-29 13:44:28 -0700299 cflags += " ${config.NoOverrideGlobalCflags}"
300 cppflags += " ${config.NoOverrideGlobalCflags}"
Dan Willemsenbe03f342016-03-03 17:21:04 -0800301 }
302
Colin Cross3f40fa42015-01-30 17:27:36 -0800303 for i, srcFile := range srcFiles {
Dan Willemsen21ec4902016-11-02 20:43:13 -0700304 objFile := android.ObjPathWithExt(ctx, subdir, srcFile, "o")
Colin Cross3f40fa42015-01-30 17:27:36 -0800305
306 objFiles[i] = objFile
307
Colin Cross91e90042016-12-02 17:13:24 -0800308 if srcFile.Ext() == ".asm" {
309 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700310 Rule: yasm,
311 Description: "yasm " + srcFile.Rel(),
312 Output: objFile,
313 Input: srcFile,
314 OrderOnly: deps,
Colin Cross91e90042016-12-02 17:13:24 -0800315 Args: map[string]string{
316 "asFlags": flags.yasmFlags,
317 },
318 })
319 continue
320 }
321
Colin Cross3f40fa42015-01-30 17:27:36 -0800322 var moduleCflags string
323 var ccCmd string
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700324 tidy := flags.tidy && flags.clang
Dan Willemsen581341d2017-02-09 16:16:31 -0800325 coverage := flags.coverage
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800326 dump := flags.sAbiDump && flags.clang
Colin Cross3f40fa42015-01-30 17:27:36 -0800327
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700328 switch srcFile.Ext() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800329 case ".S", ".s":
330 ccCmd = "gcc"
331 moduleCflags = asflags
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700332 tidy = false
Dan Willemsen581341d2017-02-09 16:16:31 -0800333 coverage = false
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800334 dump = false
Colin Cross3f40fa42015-01-30 17:27:36 -0800335 case ".c":
336 ccCmd = "gcc"
337 moduleCflags = cflags
Colin Cross9978ffe2016-12-01 15:31:22 -0800338 case ".cpp", ".cc", ".mm":
Colin Cross3f40fa42015-01-30 17:27:36 -0800339 ccCmd = "g++"
340 moduleCflags = cppflags
341 default:
342 ctx.ModuleErrorf("File %s has unknown extension", srcFile)
343 continue
344 }
345
346 if flags.clang {
347 switch ccCmd {
348 case "gcc":
349 ccCmd = "clang"
350 case "g++":
351 ccCmd = "clang++"
352 default:
353 panic("unrecoginzied ccCmd")
354 }
Colin Cross67a5c132017-05-09 13:45:28 -0700355 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800356
Colin Cross67a5c132017-05-09 13:45:28 -0700357 ccDesc := ccCmd
358
359 if flags.clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700360 ccCmd = "${config.ClangBin}/" + ccCmd
Colin Cross3f40fa42015-01-30 17:27:36 -0800361 } else {
362 ccCmd = gccCmd(flags.toolchain, ccCmd)
363 }
364
Dan Willemsen581341d2017-02-09 16:16:31 -0800365 var implicitOutputs android.WritablePaths
366 if coverage {
367 gcnoFile := android.ObjPathWithExt(ctx, subdir, srcFile, "gcno")
368 implicitOutputs = append(implicitOutputs, gcnoFile)
369 coverageFiles = append(coverageFiles, gcnoFile)
370 }
371
Colin Cross635c3b02016-05-18 15:37:25 -0700372 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Dan Willemsen581341d2017-02-09 16:16:31 -0800373 Rule: cc,
Colin Cross67a5c132017-05-09 13:45:28 -0700374 Description: ccDesc + " " + srcFile.Rel(),
Dan Willemsen581341d2017-02-09 16:16:31 -0800375 Output: objFile,
376 ImplicitOutputs: implicitOutputs,
377 Input: srcFile,
378 OrderOnly: deps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800379 Args: map[string]string{
Colin Cross28344522015-04-22 13:07:53 -0700380 "cFlags": moduleCflags,
381 "ccCmd": ccCmd,
Colin Cross3f40fa42015-01-30 17:27:36 -0800382 },
383 })
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700384
385 if tidy {
Dan Willemsen21ec4902016-11-02 20:43:13 -0700386 tidyFile := android.ObjPathWithExt(ctx, subdir, srcFile, "tidy")
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700387 tidyFiles = append(tidyFiles, tidyFile)
388
389 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700390 Rule: clangTidy,
391 Description: "clang-tidy " + srcFile.Rel(),
392 Output: tidyFile,
393 Input: srcFile,
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700394 // We must depend on objFile, since clang-tidy doesn't
395 // support exporting dependencies.
396 Implicit: objFile,
397 Args: map[string]string{
398 "cFlags": moduleCflags,
399 "tidyFlags": flags.tidyFlags,
400 },
401 })
402 }
403
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800404 if dump {
405 sAbiDumpFile := android.ObjPathWithExt(ctx, subdir, srcFile, "sdump")
406 sAbiDumpFiles = append(sAbiDumpFiles, sAbiDumpFile)
407
408 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700409 Rule: sAbiDump,
410 Description: "header-abi-dumper " + srcFile.Rel(),
411 Output: sAbiDumpFile,
412 Input: srcFile,
413 Implicit: objFile,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800414 Args: map[string]string{
415 "cFlags": moduleCflags,
416 "exportDirs": flags.sAbiFlags,
417 },
418 })
419 }
420
Colin Cross3f40fa42015-01-30 17:27:36 -0800421 }
422
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700423 return Objects{
Dan Willemsen581341d2017-02-09 16:16:31 -0800424 objFiles: objFiles,
425 tidyFiles: tidyFiles,
426 coverageFiles: coverageFiles,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800427 sAbiDumpFiles: sAbiDumpFiles,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700428 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800429}
430
431// Generate a rule for compiling multiple .o files to a static library (.a)
Colin Cross635c3b02016-05-18 15:37:25 -0700432func TransformObjToStaticLib(ctx android.ModuleContext, objFiles android.Paths,
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700433 flags builderFlags, outputFile android.ModuleOutPath, deps android.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800434
Dan Willemsen581341d2017-02-09 16:16:31 -0800435 if ctx.Darwin() {
436 transformDarwinObjToStaticLib(ctx, objFiles, flags, outputFile, deps)
437 return
438 }
439
Colin Cross3f40fa42015-01-30 17:27:36 -0800440 arCmd := gccCmd(flags.toolchain, "ar")
441 arFlags := "crsPD"
Vishwath Mohan83d9f712017-03-16 11:01:23 -0700442 if flags.arFlags != "" {
443 arFlags += " " + flags.arFlags
444 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800445
Colin Cross635c3b02016-05-18 15:37:25 -0700446 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700447 Rule: ar,
448 Description: "static link " + outputFile.Base(),
449 Output: outputFile,
450 Inputs: objFiles,
451 Implicits: deps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800452 Args: map[string]string{
453 "arFlags": arFlags,
454 "arCmd": arCmd,
455 },
456 })
457}
458
Colin Cross0af4b842015-04-30 16:36:18 -0700459// Generate a rule for compiling multiple .o files to a static library (.a) on
460// darwin. The darwin ar tool doesn't support @file for list files, and has a
461// very small command line length limit, so we have to split the ar into multiple
462// steps, each appending to the previous one.
Dan Willemsen581341d2017-02-09 16:16:31 -0800463func transformDarwinObjToStaticLib(ctx android.ModuleContext, objFiles android.Paths,
Colin Cross5b529592017-05-09 13:34:34 -0700464 flags builderFlags, outputFile android.ModuleOutPath, deps android.Paths) {
Colin Cross0af4b842015-04-30 16:36:18 -0700465
Colin Cross0af4b842015-04-30 16:36:18 -0700466 arFlags := "cqs"
467
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700468 if len(objFiles) == 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700469 dummy := android.PathForModuleOut(ctx, "dummy"+objectExtension)
470 dummyAr := android.PathForModuleOut(ctx, "dummy"+staticLibraryExtension)
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700471
Colin Cross635c3b02016-05-18 15:37:25 -0700472 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700473 Rule: emptyFile,
474 Description: "empty object file",
475 Output: dummy,
476 Implicits: deps,
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700477 })
478
Colin Cross635c3b02016-05-18 15:37:25 -0700479 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700480 Rule: darwinAr,
481 Description: "empty static archive",
482 Output: dummyAr,
483 Input: dummy,
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700484 Args: map[string]string{
485 "arFlags": arFlags,
486 },
487 })
488
Colin Cross635c3b02016-05-18 15:37:25 -0700489 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700490 Rule: darwinAppendAr,
491 Description: "static link " + outputFile.Base(),
492 Output: outputFile,
493 Input: dummy,
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700494 Args: map[string]string{
495 "arFlags": "d",
496 "inAr": dummyAr.String(),
497 },
498 })
499
500 return
501 }
502
Colin Cross0af4b842015-04-30 16:36:18 -0700503 // ARG_MAX on darwin is 262144, use half that to be safe
Colin Cross5b529592017-05-09 13:34:34 -0700504 objFilesLists, err := splitListForSize(objFiles, 131072)
Colin Cross0af4b842015-04-30 16:36:18 -0700505 if err != nil {
506 ctx.ModuleErrorf("%s", err.Error())
507 }
508
Colin Cross5b529592017-05-09 13:34:34 -0700509 var in, out android.WritablePath
Colin Cross0af4b842015-04-30 16:36:18 -0700510 for i, l := range objFilesLists {
511 in = out
512 out = outputFile
513 if i != len(objFilesLists)-1 {
Colin Cross5b529592017-05-09 13:34:34 -0700514 out = android.PathForModuleOut(ctx, outputFile.Base()+strconv.Itoa(i))
Colin Cross0af4b842015-04-30 16:36:18 -0700515 }
516
Colin Cross5b529592017-05-09 13:34:34 -0700517 build := android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700518 Rule: darwinAr,
519 Description: "static link " + out.Base(),
520 Output: out,
521 Inputs: l,
522 Implicits: deps,
Colin Cross5b529592017-05-09 13:34:34 -0700523 Args: map[string]string{
524 "arFlags": arFlags,
525 },
Colin Cross0af4b842015-04-30 16:36:18 -0700526 }
Colin Cross5b529592017-05-09 13:34:34 -0700527 if i != 0 {
528 build.Rule = darwinAppendAr
529 build.Args["inAr"] = in.String()
530 }
531 ctx.ModuleBuild(pctx, build)
Colin Cross0af4b842015-04-30 16:36:18 -0700532 }
533}
534
Colin Cross3f40fa42015-01-30 17:27:36 -0800535// Generate a rule for compiling multiple .o files, plus static libraries, whole static libraries,
536// and shared libraires, to a shared library (.so) or dynamic executable
Colin Cross635c3b02016-05-18 15:37:25 -0700537func TransformObjToDynamicBinary(ctx android.ModuleContext,
538 objFiles, sharedLibs, staticLibs, lateStaticLibs, wholeStaticLibs, deps android.Paths,
539 crtBegin, crtEnd android.OptionalPath, groupLate bool, flags builderFlags, outputFile android.WritablePath) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800540
541 var ldCmd string
542 if flags.clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700543 ldCmd = "${config.ClangBin}/clang++"
Colin Cross3f40fa42015-01-30 17:27:36 -0800544 } else {
545 ldCmd = gccCmd(flags.toolchain, "g++")
546 }
547
Colin Cross3f40fa42015-01-30 17:27:36 -0800548 var libFlagsList []string
549
Colin Cross16b23492016-01-06 14:41:07 -0800550 if len(flags.libFlags) > 0 {
551 libFlagsList = append(libFlagsList, flags.libFlags)
552 }
553
Colin Cross3f40fa42015-01-30 17:27:36 -0800554 if len(wholeStaticLibs) > 0 {
Dan Willemsen490fd492015-11-24 17:53:15 -0800555 if ctx.Host() && ctx.Darwin() {
Colin Cross635c3b02016-05-18 15:37:25 -0700556 libFlagsList = append(libFlagsList, android.JoinWithPrefix(wholeStaticLibs.Strings(), "-force_load "))
Colin Cross0af4b842015-04-30 16:36:18 -0700557 } else {
558 libFlagsList = append(libFlagsList, "-Wl,--whole-archive ")
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700559 libFlagsList = append(libFlagsList, wholeStaticLibs.Strings()...)
Colin Cross0af4b842015-04-30 16:36:18 -0700560 libFlagsList = append(libFlagsList, "-Wl,--no-whole-archive ")
561 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800562 }
563
Colin Cross7a7cf972016-12-05 18:47:39 -0800564 if flags.groupStaticLibs && !ctx.Darwin() && len(staticLibs) > 0 {
Colin Cross18c0c5a2016-12-01 14:45:23 -0800565 libFlagsList = append(libFlagsList, "-Wl,--start-group")
566 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700567 libFlagsList = append(libFlagsList, staticLibs.Strings()...)
Colin Cross7a7cf972016-12-05 18:47:39 -0800568 if flags.groupStaticLibs && !ctx.Darwin() && len(staticLibs) > 0 {
Colin Cross18c0c5a2016-12-01 14:45:23 -0800569 libFlagsList = append(libFlagsList, "-Wl,--end-group")
570 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800571
Stephen Hines10347862016-07-18 15:54:54 -0700572 if groupLate && !ctx.Darwin() && len(lateStaticLibs) > 0 {
Dan Willemsenedc385f2015-07-08 13:02:23 -0700573 libFlagsList = append(libFlagsList, "-Wl,--start-group")
574 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700575 libFlagsList = append(libFlagsList, lateStaticLibs.Strings()...)
Stephen Hines10347862016-07-18 15:54:54 -0700576 if groupLate && !ctx.Darwin() && len(lateStaticLibs) > 0 {
Dan Willemsenedc385f2015-07-08 13:02:23 -0700577 libFlagsList = append(libFlagsList, "-Wl,--end-group")
578 }
579
Colin Cross3f40fa42015-01-30 17:27:36 -0800580 for _, lib := range sharedLibs {
Dan Albert9840e1b2016-07-21 08:47:33 -0700581 libFlagsList = append(libFlagsList, lib.String())
Colin Cross3f40fa42015-01-30 17:27:36 -0800582 }
583
Colin Cross3f40fa42015-01-30 17:27:36 -0800584 deps = append(deps, staticLibs...)
Colin Cross3075ad02015-03-17 10:47:08 -0700585 deps = append(deps, lateStaticLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800586 deps = append(deps, wholeStaticLibs...)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700587 if crtBegin.Valid() {
588 deps = append(deps, crtBegin.Path(), crtEnd.Path())
Colin Cross3f40fa42015-01-30 17:27:36 -0800589 }
590
Colin Cross635c3b02016-05-18 15:37:25 -0700591 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700592 Rule: ld,
593 Description: "link " + outputFile.Base(),
594 Output: outputFile,
595 Inputs: objFiles,
596 Implicits: deps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800597 Args: map[string]string{
Dan Albertce2b8392016-07-21 13:16:49 -0700598 "ldCmd": ldCmd,
599 "crtBegin": crtBegin.String(),
600 "libFlags": strings.Join(libFlagsList, " "),
601 "ldFlags": flags.ldFlags,
602 "crtEnd": crtEnd.String(),
Colin Cross3f40fa42015-01-30 17:27:36 -0800603 },
604 })
605}
606
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800607// Generate a rule to combine .dump sAbi dump files from multiple source files
608// into a single .ldump sAbi dump file
609func TransformDumpToLinkedDump(ctx android.ModuleContext, sAbiDumps android.Paths,
610 symbolFile android.OptionalPath, apiLevel, baseName, exportedHeaderFlags string) android.OptionalPath {
611 outputFile := android.PathForModuleOut(ctx, baseName+".lsdump")
612 var symbolFileStr string
613 var linkedDumpDep android.Path
614 if symbolFile.Valid() {
615 symbolFileStr = "-v " + symbolFile.Path().String()
616 linkedDumpDep = symbolFile.Path()
617 }
618 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700619 Rule: sAbiLink,
620 Description: "header-abi-linker " + outputFile.Base(),
621 Output: outputFile,
622 Inputs: sAbiDumps,
623 Implicit: linkedDumpDep,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800624 Args: map[string]string{
625 "symbolFile": symbolFileStr,
626 "arch": ctx.Arch().ArchType.Name,
627 "api": apiLevel,
628 "exportedHeaderFlags": exportedHeaderFlags,
629 },
630 })
631 return android.OptionalPathForPath(outputFile)
632}
633
634func SourceAbiDiff(ctx android.ModuleContext, inputDump android.Path, referenceDump android.Path,
635 baseName string) android.OptionalPath {
636 outputFile := android.PathForModuleOut(ctx, baseName+".abidiff")
637 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700638 Rule: sAbiDiff,
639 Description: "header-abi-diff " + outputFile.Base(),
640 Output: outputFile,
641 Input: inputDump,
642 Implicit: referenceDump,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800643 Args: map[string]string{
644 "referenceDump": referenceDump.String(),
645 },
646 })
647 return android.OptionalPathForPath(outputFile)
648}
649
Colin Cross26c34ed2016-09-30 17:10:16 -0700650// Generate a rule for extract a table of contents from a shared library (.so)
651func TransformSharedObjectToToc(ctx android.ModuleContext, inputFile android.WritablePath,
652 outputFile android.WritablePath, flags builderFlags) {
653
654 crossCompile := gccCmd(flags.toolchain, "")
655
656 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700657 Rule: toc,
658 Description: "generate toc " + inputFile.Base(),
659 Output: outputFile,
660 Input: inputFile,
Colin Cross26c34ed2016-09-30 17:10:16 -0700661 Args: map[string]string{
662 "crossCompile": crossCompile,
663 },
664 })
665}
666
Colin Cross3f40fa42015-01-30 17:27:36 -0800667// Generate a rule for compiling multiple .o files to a .o using ld partial linking
Colin Cross635c3b02016-05-18 15:37:25 -0700668func TransformObjsToObj(ctx android.ModuleContext, objFiles android.Paths,
669 flags builderFlags, outputFile android.WritablePath) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800670
Colin Cross41280a42015-11-23 14:01:42 -0800671 var ldCmd string
672 if flags.clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700673 ldCmd = "${config.ClangBin}/clang++"
Colin Cross41280a42015-11-23 14:01:42 -0800674 } else {
675 ldCmd = gccCmd(flags.toolchain, "g++")
676 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800677
Colin Cross635c3b02016-05-18 15:37:25 -0700678 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700679 Rule: partialLd,
680 Description: "link " + outputFile.Base(),
681 Output: outputFile,
682 Inputs: objFiles,
Colin Cross3f40fa42015-01-30 17:27:36 -0800683 Args: map[string]string{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700684 "ldCmd": ldCmd,
Colin Cross41280a42015-11-23 14:01:42 -0800685 "ldFlags": flags.ldFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800686 },
687 })
688}
689
Colin Crossbfae8852015-03-26 14:44:11 -0700690// Generate a rule for runing objcopy --prefix-symbols on a binary
Colin Cross635c3b02016-05-18 15:37:25 -0700691func TransformBinaryPrefixSymbols(ctx android.ModuleContext, prefix string, inputFile android.Path,
692 flags builderFlags, outputFile android.WritablePath) {
Colin Crossbfae8852015-03-26 14:44:11 -0700693
694 objcopyCmd := gccCmd(flags.toolchain, "objcopy")
695
Colin Cross635c3b02016-05-18 15:37:25 -0700696 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700697 Rule: prefixSymbols,
698 Description: "prefix symbols " + outputFile.Base(),
699 Output: outputFile,
700 Input: inputFile,
Colin Crossbfae8852015-03-26 14:44:11 -0700701 Args: map[string]string{
702 "objcopyCmd": objcopyCmd,
703 "prefix": prefix,
704 },
705 })
706}
707
Colin Cross635c3b02016-05-18 15:37:25 -0700708func TransformStrip(ctx android.ModuleContext, inputFile android.Path,
709 outputFile android.WritablePath, flags builderFlags) {
Colin Cross665dce92016-04-28 14:50:03 -0700710
711 crossCompile := gccCmd(flags.toolchain, "")
712 args := ""
713 if flags.stripAddGnuDebuglink {
714 args += " --add-gnu-debuglink"
715 }
716 if flags.stripKeepMiniDebugInfo {
717 args += " --keep-mini-debug-info"
718 }
719 if flags.stripKeepSymbols {
720 args += " --keep-symbols"
721 }
722
Colin Cross635c3b02016-05-18 15:37:25 -0700723 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700724 Rule: strip,
725 Description: "strip " + outputFile.Base(),
726 Output: outputFile,
727 Input: inputFile,
Colin Cross665dce92016-04-28 14:50:03 -0700728 Args: map[string]string{
729 "crossCompile": crossCompile,
730 "args": args,
731 },
732 })
733}
734
Colin Cross635c3b02016-05-18 15:37:25 -0700735func TransformDarwinStrip(ctx android.ModuleContext, inputFile android.Path,
736 outputFile android.WritablePath) {
Colin Crossb8ecdfe2016-05-03 15:10:29 -0700737
Colin Cross635c3b02016-05-18 15:37:25 -0700738 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700739 Rule: darwinStrip,
740 Description: "strip " + outputFile.Base(),
741 Output: outputFile,
742 Input: inputFile,
Colin Crossb8ecdfe2016-05-03 15:10:29 -0700743 })
744}
745
Dan Willemsen581341d2017-02-09 16:16:31 -0800746func TransformCoverageFilesToLib(ctx android.ModuleContext,
747 inputs Objects, flags builderFlags, baseName string) android.OptionalPath {
748
749 if len(inputs.coverageFiles) > 0 {
750 outputFile := android.PathForModuleOut(ctx, baseName+".gcnodir")
751
752 TransformObjToStaticLib(ctx, inputs.coverageFiles, flags, outputFile, nil)
753
754 return android.OptionalPathForPath(outputFile)
755 }
756
757 return android.OptionalPath{}
758}
759
Colin Cross635c3b02016-05-18 15:37:25 -0700760func CopyGccLib(ctx android.ModuleContext, libName string,
761 flags builderFlags, outputFile android.WritablePath) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800762
Colin Cross635c3b02016-05-18 15:37:25 -0700763 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700764 Rule: copyGccLib,
765 Description: "copy gcc library " + libName,
766 Output: outputFile,
Colin Cross3f40fa42015-01-30 17:27:36 -0800767 Args: map[string]string{
768 "ccCmd": gccCmd(flags.toolchain, "gcc"),
769 "cFlags": flags.globalFlags,
770 "libName": libName,
771 },
772 })
773}
774
Colin Crossb98c8b02016-07-29 13:44:28 -0700775func gccCmd(toolchain config.Toolchain, cmd string) string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800776 return filepath.Join(toolchain.GccRoot(), "bin", toolchain.GccTriple()+"-"+cmd)
777}
Colin Cross0af4b842015-04-30 16:36:18 -0700778
Colin Cross5b529592017-05-09 13:34:34 -0700779func splitListForSize(list android.Paths, limit int) (lists []android.Paths, err error) {
Colin Cross0af4b842015-04-30 16:36:18 -0700780 var i int
781
782 start := 0
783 bytes := 0
784 for i = range list {
Colin Cross5b529592017-05-09 13:34:34 -0700785 l := len(list[i].String())
Colin Cross0af4b842015-04-30 16:36:18 -0700786 if l > limit {
787 return nil, fmt.Errorf("list element greater than size limit (%d)", limit)
788 }
789 if bytes+l > limit {
790 lists = append(lists, list[start:i])
791 start = i
792 bytes = 0
793 }
794 bytes += l + 1 // count a space between each list element
795 }
796
797 lists = append(lists, list[start:])
798
799 totalLen := 0
800 for _, l := range lists {
801 totalLen += len(l)
802 }
803 if totalLen != len(list) {
804 panic(fmt.Errorf("Failed breaking up list, %d != %d", len(list), totalLen))
805 }
806 return lists, nil
807}