blob: 02ea63d71b9a3905d3eeda206d9373f0c3dcea6f [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
42 cc = pctx.StaticRule("cc",
43 blueprint.RuleParams{
44 Depfile: "${out}.d",
45 Deps: blueprint.DepsGCC,
Dan Willemsene6540452015-10-20 15:21:33 -070046 Command: "$relPwd $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 Description: "cc $out",
49 },
Dan Willemsen322a0a62015-11-17 15:19:46 -080050 "ccCmd", "cFlags")
Colin Cross3f40fa42015-01-30 17:27:36 -080051
52 ld = pctx.StaticRule("ld",
53 blueprint.RuleParams{
Dan Albertce2b8392016-07-21 13:16:49 -070054 Command: "$ldCmd ${crtBegin} @${out}.rsp " +
Colin Cross28344522015-04-22 13:07:53 -070055 "${libFlags} ${crtEnd} -o ${out} ${ldFlags}",
Dan Willemsenc94a7682015-11-17 15:27:28 -080056 CommandDeps: []string{"$ldCmd"},
Colin Cross7d21c442015-03-30 17:47:53 -070057 Description: "ld $out",
58 Rspfile: "${out}.rsp",
59 RspfileContent: "${in}",
Colin Cross3f40fa42015-01-30 17:27:36 -080060 },
Dan Albertce2b8392016-07-21 13:16:49 -070061 "ldCmd", "crtBegin", "libFlags", "crtEnd", "ldFlags")
Colin Cross3f40fa42015-01-30 17:27:36 -080062
63 partialLd = pctx.StaticRule("partialLd",
64 blueprint.RuleParams{
Colin Cross41280a42015-11-23 14:01:42 -080065 Command: "$ldCmd -nostdlib -Wl,-r ${in} -o ${out} ${ldFlags}",
Dan Willemsenc94a7682015-11-17 15:27:28 -080066 CommandDeps: []string{"$ldCmd"},
Colin Cross3f40fa42015-01-30 17:27:36 -080067 Description: "partialLd $out",
68 },
Colin Cross41280a42015-11-23 14:01:42 -080069 "ldCmd", "ldFlags")
Colin Cross3f40fa42015-01-30 17:27:36 -080070
71 ar = pctx.StaticRule("ar",
72 blueprint.RuleParams{
Colin Cross7d21c442015-03-30 17:47:53 -070073 Command: "rm -f ${out} && $arCmd $arFlags $out @${out}.rsp",
Dan Willemsenc94a7682015-11-17 15:27:28 -080074 CommandDeps: []string{"$arCmd"},
Colin Cross7d21c442015-03-30 17:47:53 -070075 Description: "ar $out",
76 Rspfile: "${out}.rsp",
77 RspfileContent: "${in}",
Colin Cross3f40fa42015-01-30 17:27:36 -080078 },
79 "arCmd", "arFlags")
80
Colin Cross0af4b842015-04-30 16:36:18 -070081 darwinAr = pctx.StaticRule("darwinAr",
82 blueprint.RuleParams{
Colin Crossb98c8b02016-07-29 13:44:28 -070083 Command: "rm -f ${out} && ${config.MacArPath} $arFlags $out $in",
84 CommandDeps: []string{"${config.MacArPath}"},
Colin Cross0af4b842015-04-30 16:36:18 -070085 Description: "ar $out",
86 },
Colin Crossb8ecdfe2016-05-03 15:10:29 -070087 "arFlags")
Colin Cross0af4b842015-04-30 16:36:18 -070088
89 darwinAppendAr = pctx.StaticRule("darwinAppendAr",
90 blueprint.RuleParams{
Colin Crossb98c8b02016-07-29 13:44:28 -070091 Command: "cp -f ${inAr} ${out}.tmp && ${config.MacArPath} $arFlags ${out}.tmp $in && mv ${out}.tmp ${out}",
92 CommandDeps: []string{"${config.MacArPath}", "${inAr}"},
Colin Cross0af4b842015-04-30 16:36:18 -070093 Description: "ar $out",
94 },
Colin Crossb8ecdfe2016-05-03 15:10:29 -070095 "arFlags", "inAr")
96
97 darwinStrip = pctx.StaticRule("darwinStrip",
98 blueprint.RuleParams{
99 Command: "${macStripPath} -u -r -o $out $in",
100 CommandDeps: []string{"${macStripPath}"},
101 Description: "strip $out",
102 })
Colin Cross0af4b842015-04-30 16:36:18 -0700103
Colin Crossbfae8852015-03-26 14:44:11 -0700104 prefixSymbols = pctx.StaticRule("prefixSymbols",
105 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 Description: "prefixSymbols $out",
109 },
110 "objcopyCmd", "prefix")
111
Colin Cross665dce92016-04-28 14:50:03 -0700112 stripPath = pctx.SourcePathVariable("stripPath", "build/soong/scripts/strip.sh")
113
114 strip = pctx.StaticRule("strip",
115 blueprint.RuleParams{
116 Depfile: "${out}.d",
117 Deps: blueprint.DepsGCC,
118 Command: "CROSS_COMPILE=$crossCompile $stripPath ${args} -i ${in} -o ${out} -d ${out}.d",
119 CommandDeps: []string{"$stripPath"},
120 Description: "strip $out",
121 },
122 "args", "crossCompile")
123
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700124 emptyFile = pctx.StaticRule("emptyFile",
125 blueprint.RuleParams{
126 Command: "rm -f $out && touch $out",
127 Description: "empty file $out",
128 })
129
Colin Cross14747412016-04-27 16:10:38 -0700130 copyGccLibPath = pctx.SourcePathVariable("copyGccLibPath", "build/soong/scripts/copygcclib.sh")
Colin Cross3f40fa42015-01-30 17:27:36 -0800131
132 copyGccLib = pctx.StaticRule("copyGccLib",
133 blueprint.RuleParams{
134 Depfile: "${out}.d",
135 Deps: blueprint.DepsGCC,
136 Command: "$copyGccLibPath $out $ccCmd $cFlags -print-file-name=${libName}",
Dan Willemsenc94a7682015-11-17 15:27:28 -0800137 CommandDeps: []string{"$copyGccLibPath", "$ccCmd"},
Colin Cross3f40fa42015-01-30 17:27:36 -0800138 Description: "copy gcc $out",
139 },
140 "ccCmd", "cFlags", "libName")
141)
142
Dan Willemsen322a0a62015-11-17 15:19:46 -0800143func init() {
144 // We run gcc/clang with PWD=/proc/self/cwd to remove $TOP from the
145 // debug output. That way two builds in two different directories will
146 // create the same output.
147 if runtime.GOOS != "darwin" {
148 pctx.StaticVariable("relPwd", "PWD=/proc/self/cwd")
149 } else {
150 // Darwin doesn't have /proc
151 pctx.StaticVariable("relPwd", "")
152 }
153}
154
Colin Cross3f40fa42015-01-30 17:27:36 -0800155type builderFlags struct {
156 globalFlags string
157 asFlags string
158 cFlags string
159 conlyFlags string
160 cppFlags string
161 ldFlags string
Colin Cross16b23492016-01-06 14:41:07 -0800162 libFlags string
Colin Cross581c1892015-04-07 16:50:10 -0700163 yaccFlags string
Colin Cross3f40fa42015-01-30 17:27:36 -0800164 nocrt bool
Colin Crossb98c8b02016-07-29 13:44:28 -0700165 toolchain config.Toolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800166 clang bool
Colin Cross665dce92016-04-28 14:50:03 -0700167
168 stripKeepSymbols bool
169 stripKeepMiniDebugInfo bool
170 stripAddGnuDebuglink bool
Colin Cross3f40fa42015-01-30 17:27:36 -0800171}
172
173// Generate rules for compiling multiple .c, .cpp, or .S files to individual .o files
Colin Cross635c3b02016-05-18 15:37:25 -0700174func TransformSourceToObj(ctx android.ModuleContext, subdir string, srcFiles android.Paths,
175 flags builderFlags, deps android.Paths) (objFiles android.Paths) {
Colin Cross581c1892015-04-07 16:50:10 -0700176
Colin Cross635c3b02016-05-18 15:37:25 -0700177 objFiles = make(android.Paths, len(srcFiles))
Colin Cross3f40fa42015-01-30 17:27:36 -0800178
179 cflags := flags.globalFlags + " " + flags.cFlags + " " + flags.conlyFlags
180 cppflags := flags.globalFlags + " " + flags.cFlags + " " + flags.cppFlags
181 asflags := flags.globalFlags + " " + flags.asFlags
182
Dan Willemsenbe03f342016-03-03 17:21:04 -0800183 if flags.clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700184 cflags += " ${config.NoOverrideClangGlobalCflags}"
185 cppflags += " ${config.NoOverrideClangGlobalCflags}"
Dan Willemsenbe03f342016-03-03 17:21:04 -0800186 } else {
Colin Crossb98c8b02016-07-29 13:44:28 -0700187 cflags += " ${config.NoOverrideGlobalCflags}"
188 cppflags += " ${config.NoOverrideGlobalCflags}"
Dan Willemsenbe03f342016-03-03 17:21:04 -0800189 }
190
Colin Cross3f40fa42015-01-30 17:27:36 -0800191 for i, srcFile := range srcFiles {
Colin Cross635c3b02016-05-18 15:37:25 -0700192 objFile := android.ObjPathWithExt(ctx, srcFile, subdir, "o")
Colin Cross3f40fa42015-01-30 17:27:36 -0800193
194 objFiles[i] = objFile
195
196 var moduleCflags string
197 var ccCmd string
198
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700199 switch srcFile.Ext() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800200 case ".S", ".s":
201 ccCmd = "gcc"
202 moduleCflags = asflags
203 case ".c":
204 ccCmd = "gcc"
205 moduleCflags = cflags
206 case ".cpp", ".cc":
207 ccCmd = "g++"
208 moduleCflags = cppflags
209 default:
210 ctx.ModuleErrorf("File %s has unknown extension", srcFile)
211 continue
212 }
213
214 if flags.clang {
215 switch ccCmd {
216 case "gcc":
217 ccCmd = "clang"
218 case "g++":
219 ccCmd = "clang++"
220 default:
221 panic("unrecoginzied ccCmd")
222 }
223
Colin Crossb98c8b02016-07-29 13:44:28 -0700224 ccCmd = "${config.ClangBin}/" + ccCmd
Colin Cross3f40fa42015-01-30 17:27:36 -0800225 } else {
226 ccCmd = gccCmd(flags.toolchain, ccCmd)
227 }
228
Colin Cross635c3b02016-05-18 15:37:25 -0700229 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross3f40fa42015-01-30 17:27:36 -0800230 Rule: cc,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700231 Output: objFile,
232 Input: srcFile,
Dan Willemsenb40aab62016-04-20 14:21:14 -0700233 OrderOnly: deps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800234 Args: map[string]string{
Colin Cross28344522015-04-22 13:07:53 -0700235 "cFlags": moduleCflags,
236 "ccCmd": ccCmd,
Colin Cross3f40fa42015-01-30 17:27:36 -0800237 },
238 })
239 }
240
241 return objFiles
242}
243
244// Generate a rule for compiling multiple .o files to a static library (.a)
Colin Cross635c3b02016-05-18 15:37:25 -0700245func TransformObjToStaticLib(ctx android.ModuleContext, objFiles android.Paths,
246 flags builderFlags, outputFile android.ModuleOutPath) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800247
248 arCmd := gccCmd(flags.toolchain, "ar")
249 arFlags := "crsPD"
250
Colin Cross635c3b02016-05-18 15:37:25 -0700251 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700252 Rule: ar,
253 Output: outputFile,
254 Inputs: objFiles,
Colin Cross3f40fa42015-01-30 17:27:36 -0800255 Args: map[string]string{
256 "arFlags": arFlags,
257 "arCmd": arCmd,
258 },
259 })
260}
261
Colin Cross0af4b842015-04-30 16:36:18 -0700262// Generate a rule for compiling multiple .o files to a static library (.a) on
263// darwin. The darwin ar tool doesn't support @file for list files, and has a
264// very small command line length limit, so we have to split the ar into multiple
265// steps, each appending to the previous one.
Colin Cross635c3b02016-05-18 15:37:25 -0700266func TransformDarwinObjToStaticLib(ctx android.ModuleContext, objFiles android.Paths,
267 flags builderFlags, outputPath android.ModuleOutPath) {
Colin Cross0af4b842015-04-30 16:36:18 -0700268
Colin Cross0af4b842015-04-30 16:36:18 -0700269 arFlags := "cqs"
270
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700271 if len(objFiles) == 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700272 dummy := android.PathForModuleOut(ctx, "dummy"+objectExtension)
273 dummyAr := android.PathForModuleOut(ctx, "dummy"+staticLibraryExtension)
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700274
Colin Cross635c3b02016-05-18 15:37:25 -0700275 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700276 Rule: emptyFile,
277 Output: dummy,
278 })
279
Colin Cross635c3b02016-05-18 15:37:25 -0700280 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700281 Rule: darwinAr,
282 Output: dummyAr,
283 Input: dummy,
284 Args: map[string]string{
285 "arFlags": arFlags,
286 },
287 })
288
Colin Cross635c3b02016-05-18 15:37:25 -0700289 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700290 Rule: darwinAppendAr,
291 Output: outputPath,
292 Input: dummy,
293 Args: map[string]string{
294 "arFlags": "d",
295 "inAr": dummyAr.String(),
296 },
297 })
298
299 return
300 }
301
Colin Cross0af4b842015-04-30 16:36:18 -0700302 // ARG_MAX on darwin is 262144, use half that to be safe
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700303 objFilesLists, err := splitListForSize(objFiles.Strings(), 131072)
Colin Cross0af4b842015-04-30 16:36:18 -0700304 if err != nil {
305 ctx.ModuleErrorf("%s", err.Error())
306 }
307
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700308 outputFile := outputPath.String()
309
Colin Cross0af4b842015-04-30 16:36:18 -0700310 var in, out string
311 for i, l := range objFilesLists {
312 in = out
313 out = outputFile
314 if i != len(objFilesLists)-1 {
315 out += "." + strconv.Itoa(i)
316 }
317
318 if in == "" {
319 ctx.Build(pctx, blueprint.BuildParams{
320 Rule: darwinAr,
321 Outputs: []string{out},
322 Inputs: l,
323 Args: map[string]string{
324 "arFlags": arFlags,
Colin Cross0af4b842015-04-30 16:36:18 -0700325 },
326 })
327 } else {
328 ctx.Build(pctx, blueprint.BuildParams{
Colin Cross635c3b02016-05-18 15:37:25 -0700329 Rule: darwinAppendAr,
330 Outputs: []string{out},
331 Inputs: l,
Colin Cross0af4b842015-04-30 16:36:18 -0700332 Args: map[string]string{
333 "arFlags": arFlags,
Colin Cross0af4b842015-04-30 16:36:18 -0700334 "inAr": in,
335 },
336 })
337 }
338 }
339}
340
Colin Cross3f40fa42015-01-30 17:27:36 -0800341// Generate a rule for compiling multiple .o files, plus static libraries, whole static libraries,
342// and shared libraires, to a shared library (.so) or dynamic executable
Colin Cross635c3b02016-05-18 15:37:25 -0700343func TransformObjToDynamicBinary(ctx android.ModuleContext,
344 objFiles, sharedLibs, staticLibs, lateStaticLibs, wholeStaticLibs, deps android.Paths,
345 crtBegin, crtEnd android.OptionalPath, groupLate bool, flags builderFlags, outputFile android.WritablePath) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800346
347 var ldCmd string
348 if flags.clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700349 ldCmd = "${config.ClangBin}/clang++"
Colin Cross3f40fa42015-01-30 17:27:36 -0800350 } else {
351 ldCmd = gccCmd(flags.toolchain, "g++")
352 }
353
Colin Cross3f40fa42015-01-30 17:27:36 -0800354 var libFlagsList []string
355
Colin Cross16b23492016-01-06 14:41:07 -0800356 if len(flags.libFlags) > 0 {
357 libFlagsList = append(libFlagsList, flags.libFlags)
358 }
359
Colin Cross3f40fa42015-01-30 17:27:36 -0800360 if len(wholeStaticLibs) > 0 {
Dan Willemsen490fd492015-11-24 17:53:15 -0800361 if ctx.Host() && ctx.Darwin() {
Colin Cross635c3b02016-05-18 15:37:25 -0700362 libFlagsList = append(libFlagsList, android.JoinWithPrefix(wholeStaticLibs.Strings(), "-force_load "))
Colin Cross0af4b842015-04-30 16:36:18 -0700363 } else {
364 libFlagsList = append(libFlagsList, "-Wl,--whole-archive ")
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700365 libFlagsList = append(libFlagsList, wholeStaticLibs.Strings()...)
Colin Cross0af4b842015-04-30 16:36:18 -0700366 libFlagsList = append(libFlagsList, "-Wl,--no-whole-archive ")
367 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800368 }
369
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700370 libFlagsList = append(libFlagsList, staticLibs.Strings()...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800371
Stephen Hines10347862016-07-18 15:54:54 -0700372 if groupLate && !ctx.Darwin() && len(lateStaticLibs) > 0 {
Dan Willemsenedc385f2015-07-08 13:02:23 -0700373 libFlagsList = append(libFlagsList, "-Wl,--start-group")
374 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700375 libFlagsList = append(libFlagsList, lateStaticLibs.Strings()...)
Stephen Hines10347862016-07-18 15:54:54 -0700376 if groupLate && !ctx.Darwin() && len(lateStaticLibs) > 0 {
Dan Willemsenedc385f2015-07-08 13:02:23 -0700377 libFlagsList = append(libFlagsList, "-Wl,--end-group")
378 }
379
Colin Cross3f40fa42015-01-30 17:27:36 -0800380 for _, lib := range sharedLibs {
Dan Albertce2b8392016-07-21 13:16:49 -0700381 file := filepath.Base(lib.String())
Colin Cross3f40fa42015-01-30 17:27:36 -0800382 if !strings.HasPrefix(file, "lib") {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700383 panic("shared library " + lib.String() + " does not start with lib")
Colin Cross3f40fa42015-01-30 17:27:36 -0800384 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800385 if !strings.HasSuffix(file, flags.toolchain.ShlibSuffix()) {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700386 panic("shared library " + lib.String() + " does not end with " + flags.toolchain.ShlibSuffix())
Colin Cross3f40fa42015-01-30 17:27:36 -0800387 }
Dan Albert9840e1b2016-07-21 08:47:33 -0700388 libFlagsList = append(libFlagsList, lib.String())
Colin Cross3f40fa42015-01-30 17:27:36 -0800389 }
390
Colin Cross3f40fa42015-01-30 17:27:36 -0800391 deps = append(deps, sharedLibs...)
392 deps = append(deps, staticLibs...)
Colin Cross3075ad02015-03-17 10:47:08 -0700393 deps = append(deps, lateStaticLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800394 deps = append(deps, wholeStaticLibs...)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700395 if crtBegin.Valid() {
396 deps = append(deps, crtBegin.Path(), crtEnd.Path())
Colin Cross3f40fa42015-01-30 17:27:36 -0800397 }
398
Colin Cross635c3b02016-05-18 15:37:25 -0700399 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross3f40fa42015-01-30 17:27:36 -0800400 Rule: ld,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700401 Output: outputFile,
Colin Cross3f40fa42015-01-30 17:27:36 -0800402 Inputs: objFiles,
403 Implicits: deps,
404 Args: map[string]string{
Dan Albertce2b8392016-07-21 13:16:49 -0700405 "ldCmd": ldCmd,
406 "crtBegin": crtBegin.String(),
407 "libFlags": strings.Join(libFlagsList, " "),
408 "ldFlags": flags.ldFlags,
409 "crtEnd": crtEnd.String(),
Colin Cross3f40fa42015-01-30 17:27:36 -0800410 },
411 })
412}
413
414// Generate a rule for compiling multiple .o files to a .o using ld partial linking
Colin Cross635c3b02016-05-18 15:37:25 -0700415func TransformObjsToObj(ctx android.ModuleContext, objFiles android.Paths,
416 flags builderFlags, outputFile android.WritablePath) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800417
Colin Cross41280a42015-11-23 14:01:42 -0800418 var ldCmd string
419 if flags.clang {
Colin Crossb98c8b02016-07-29 13:44:28 -0700420 ldCmd = "${config.ClangBin}/clang++"
Colin Cross41280a42015-11-23 14:01:42 -0800421 } else {
422 ldCmd = gccCmd(flags.toolchain, "g++")
423 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800424
Colin Cross635c3b02016-05-18 15:37:25 -0700425 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700426 Rule: partialLd,
427 Output: outputFile,
428 Inputs: objFiles,
Colin Cross3f40fa42015-01-30 17:27:36 -0800429 Args: map[string]string{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700430 "ldCmd": ldCmd,
Colin Cross41280a42015-11-23 14:01:42 -0800431 "ldFlags": flags.ldFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800432 },
433 })
434}
435
Colin Crossbfae8852015-03-26 14:44:11 -0700436// Generate a rule for runing objcopy --prefix-symbols on a binary
Colin Cross635c3b02016-05-18 15:37:25 -0700437func TransformBinaryPrefixSymbols(ctx android.ModuleContext, prefix string, inputFile android.Path,
438 flags builderFlags, outputFile android.WritablePath) {
Colin Crossbfae8852015-03-26 14:44:11 -0700439
440 objcopyCmd := gccCmd(flags.toolchain, "objcopy")
441
Colin Cross635c3b02016-05-18 15:37:25 -0700442 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700443 Rule: prefixSymbols,
444 Output: outputFile,
445 Input: inputFile,
Colin Crossbfae8852015-03-26 14:44:11 -0700446 Args: map[string]string{
447 "objcopyCmd": objcopyCmd,
448 "prefix": prefix,
449 },
450 })
451}
452
Colin Cross635c3b02016-05-18 15:37:25 -0700453func TransformStrip(ctx android.ModuleContext, inputFile android.Path,
454 outputFile android.WritablePath, flags builderFlags) {
Colin Cross665dce92016-04-28 14:50:03 -0700455
456 crossCompile := gccCmd(flags.toolchain, "")
457 args := ""
458 if flags.stripAddGnuDebuglink {
459 args += " --add-gnu-debuglink"
460 }
461 if flags.stripKeepMiniDebugInfo {
462 args += " --keep-mini-debug-info"
463 }
464 if flags.stripKeepSymbols {
465 args += " --keep-symbols"
466 }
467
Colin Cross635c3b02016-05-18 15:37:25 -0700468 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross665dce92016-04-28 14:50:03 -0700469 Rule: strip,
470 Output: outputFile,
471 Input: inputFile,
472 Args: map[string]string{
473 "crossCompile": crossCompile,
474 "args": args,
475 },
476 })
477}
478
Colin Cross635c3b02016-05-18 15:37:25 -0700479func TransformDarwinStrip(ctx android.ModuleContext, inputFile android.Path,
480 outputFile android.WritablePath) {
Colin Crossb8ecdfe2016-05-03 15:10:29 -0700481
Colin Cross635c3b02016-05-18 15:37:25 -0700482 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Crossb8ecdfe2016-05-03 15:10:29 -0700483 Rule: darwinStrip,
484 Output: outputFile,
485 Input: inputFile,
486 })
487}
488
Colin Cross635c3b02016-05-18 15:37:25 -0700489func CopyGccLib(ctx android.ModuleContext, libName string,
490 flags builderFlags, outputFile android.WritablePath) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800491
Colin Cross635c3b02016-05-18 15:37:25 -0700492 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700493 Rule: copyGccLib,
494 Output: outputFile,
Colin Cross3f40fa42015-01-30 17:27:36 -0800495 Args: map[string]string{
496 "ccCmd": gccCmd(flags.toolchain, "gcc"),
497 "cFlags": flags.globalFlags,
498 "libName": libName,
499 },
500 })
501}
502
Colin Crossb98c8b02016-07-29 13:44:28 -0700503func gccCmd(toolchain config.Toolchain, cmd string) string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800504 return filepath.Join(toolchain.GccRoot(), "bin", toolchain.GccTriple()+"-"+cmd)
505}
Colin Cross0af4b842015-04-30 16:36:18 -0700506
507func splitListForSize(list []string, limit int) (lists [][]string, err error) {
508 var i int
509
510 start := 0
511 bytes := 0
512 for i = range list {
513 l := len(list[i])
514 if l > limit {
515 return nil, fmt.Errorf("list element greater than size limit (%d)", limit)
516 }
517 if bytes+l > limit {
518 lists = append(lists, list[start:i])
519 start = i
520 bytes = 0
521 }
522 bytes += l + 1 // count a space between each list element
523 }
524
525 lists = append(lists, list[start:])
526
527 totalLen := 0
528 for _, l := range lists {
529 totalLen += len(l)
530 }
531 if totalLen != len(list) {
532 panic(fmt.Errorf("Failed breaking up list, %d != %d", len(list), totalLen))
533 }
534 return lists, nil
535}