blob: 4efcc2779ccb8a856f385313f34c1bfef0459936 [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 Cross635c3b02016-05-18 15:37:25 -070022 "android/soong/android"
Colin Cross0af4b842015-04-30 16:36:18 -070023 "fmt"
24 "runtime"
25 "strconv"
Colin Cross3f40fa42015-01-30 17:27:36 -080026
Colin Cross3f40fa42015-01-30 17:27:36 -080027 "path/filepath"
28 "strings"
Colin Crossed4cf0b2015-03-26 14:43:45 -070029
30 "github.com/google/blueprint"
Colin Cross3f40fa42015-01-30 17:27:36 -080031)
32
33const (
Dan Albertc3144b12015-04-28 18:17:56 -070034 objectExtension = ".o"
Colin Cross3f40fa42015-01-30 17:27:36 -080035 staticLibraryExtension = ".a"
36)
37
38var (
Colin Cross635c3b02016-05-18 15:37:25 -070039 pctx = android.NewPackageContext("android/soong/cc")
Colin Cross3f40fa42015-01-30 17:27:36 -080040
41 cc = pctx.StaticRule("cc",
42 blueprint.RuleParams{
43 Depfile: "${out}.d",
44 Deps: blueprint.DepsGCC,
Dan Willemsene6540452015-10-20 15:21:33 -070045 Command: "$relPwd $ccCmd -c $cFlags -MD -MF ${out}.d -o $out $in",
Dan Willemsenc94a7682015-11-17 15:27:28 -080046 CommandDeps: []string{"$ccCmd"},
Colin Cross3f40fa42015-01-30 17:27:36 -080047 Description: "cc $out",
48 },
Dan Willemsen322a0a62015-11-17 15:19:46 -080049 "ccCmd", "cFlags")
Colin Cross3f40fa42015-01-30 17:27:36 -080050
51 ld = pctx.StaticRule("ld",
52 blueprint.RuleParams{
Colin Cross7d21c442015-03-30 17:47:53 -070053 Command: "$ldCmd ${ldDirFlags} ${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 Description: "ld $out",
57 Rspfile: "${out}.rsp",
58 RspfileContent: "${in}",
Colin Cross3f40fa42015-01-30 17:27:36 -080059 },
Colin Cross28344522015-04-22 13:07:53 -070060 "ldCmd", "ldDirFlags", "crtBegin", "libFlags", "crtEnd", "ldFlags")
Colin Cross3f40fa42015-01-30 17:27:36 -080061
62 partialLd = pctx.StaticRule("partialLd",
63 blueprint.RuleParams{
Colin Cross41280a42015-11-23 14:01:42 -080064 Command: "$ldCmd -nostdlib -Wl,-r ${in} -o ${out} ${ldFlags}",
Dan Willemsenc94a7682015-11-17 15:27:28 -080065 CommandDeps: []string{"$ldCmd"},
Colin Cross3f40fa42015-01-30 17:27:36 -080066 Description: "partialLd $out",
67 },
Colin Cross41280a42015-11-23 14:01:42 -080068 "ldCmd", "ldFlags")
Colin Cross3f40fa42015-01-30 17:27:36 -080069
70 ar = pctx.StaticRule("ar",
71 blueprint.RuleParams{
Colin Cross7d21c442015-03-30 17:47:53 -070072 Command: "rm -f ${out} && $arCmd $arFlags $out @${out}.rsp",
Dan Willemsenc94a7682015-11-17 15:27:28 -080073 CommandDeps: []string{"$arCmd"},
Colin Cross7d21c442015-03-30 17:47:53 -070074 Description: "ar $out",
75 Rspfile: "${out}.rsp",
76 RspfileContent: "${in}",
Colin Cross3f40fa42015-01-30 17:27:36 -080077 },
78 "arCmd", "arFlags")
79
Colin Cross0af4b842015-04-30 16:36:18 -070080 darwinAr = pctx.StaticRule("darwinAr",
81 blueprint.RuleParams{
Colin Crossb8ecdfe2016-05-03 15:10:29 -070082 Command: "rm -f ${out} && ${macArPath} $arFlags $out $in",
83 CommandDeps: []string{"${macArPath}"},
Colin Cross0af4b842015-04-30 16:36:18 -070084 Description: "ar $out",
85 },
Colin Crossb8ecdfe2016-05-03 15:10:29 -070086 "arFlags")
Colin Cross0af4b842015-04-30 16:36:18 -070087
88 darwinAppendAr = pctx.StaticRule("darwinAppendAr",
89 blueprint.RuleParams{
Colin Crossb8ecdfe2016-05-03 15:10:29 -070090 Command: "cp -f ${inAr} ${out}.tmp && ${macArPath} $arFlags ${out}.tmp $in && mv ${out}.tmp ${out}",
Dan Willemsen9f0b5502016-05-13 14:05:09 -070091 CommandDeps: []string{"${macArPath}", "${inAr}"},
Colin Cross0af4b842015-04-30 16:36:18 -070092 Description: "ar $out",
93 },
Colin Crossb8ecdfe2016-05-03 15:10:29 -070094 "arFlags", "inAr")
95
96 darwinStrip = pctx.StaticRule("darwinStrip",
97 blueprint.RuleParams{
98 Command: "${macStripPath} -u -r -o $out $in",
99 CommandDeps: []string{"${macStripPath}"},
100 Description: "strip $out",
101 })
Colin Cross0af4b842015-04-30 16:36:18 -0700102
Colin Crossbfae8852015-03-26 14:44:11 -0700103 prefixSymbols = pctx.StaticRule("prefixSymbols",
104 blueprint.RuleParams{
105 Command: "$objcopyCmd --prefix-symbols=${prefix} ${in} ${out}",
Dan Willemsenc94a7682015-11-17 15:27:28 -0800106 CommandDeps: []string{"$objcopyCmd"},
Colin Crossbfae8852015-03-26 14:44:11 -0700107 Description: "prefixSymbols $out",
108 },
109 "objcopyCmd", "prefix")
110
Colin Cross665dce92016-04-28 14:50:03 -0700111 stripPath = pctx.SourcePathVariable("stripPath", "build/soong/scripts/strip.sh")
112
113 strip = pctx.StaticRule("strip",
114 blueprint.RuleParams{
115 Depfile: "${out}.d",
116 Deps: blueprint.DepsGCC,
117 Command: "CROSS_COMPILE=$crossCompile $stripPath ${args} -i ${in} -o ${out} -d ${out}.d",
118 CommandDeps: []string{"$stripPath"},
119 Description: "strip $out",
120 },
121 "args", "crossCompile")
122
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700123 emptyFile = pctx.StaticRule("emptyFile",
124 blueprint.RuleParams{
125 Command: "rm -f $out && touch $out",
126 Description: "empty file $out",
127 })
128
Colin Cross14747412016-04-27 16:10:38 -0700129 copyGccLibPath = pctx.SourcePathVariable("copyGccLibPath", "build/soong/scripts/copygcclib.sh")
Colin Cross3f40fa42015-01-30 17:27:36 -0800130
131 copyGccLib = pctx.StaticRule("copyGccLib",
132 blueprint.RuleParams{
133 Depfile: "${out}.d",
134 Deps: blueprint.DepsGCC,
135 Command: "$copyGccLibPath $out $ccCmd $cFlags -print-file-name=${libName}",
Dan Willemsenc94a7682015-11-17 15:27:28 -0800136 CommandDeps: []string{"$copyGccLibPath", "$ccCmd"},
Colin Cross3f40fa42015-01-30 17:27:36 -0800137 Description: "copy gcc $out",
138 },
139 "ccCmd", "cFlags", "libName")
140)
141
Dan Willemsen322a0a62015-11-17 15:19:46 -0800142func init() {
143 // We run gcc/clang with PWD=/proc/self/cwd to remove $TOP from the
144 // debug output. That way two builds in two different directories will
145 // create the same output.
146 if runtime.GOOS != "darwin" {
147 pctx.StaticVariable("relPwd", "PWD=/proc/self/cwd")
148 } else {
149 // Darwin doesn't have /proc
150 pctx.StaticVariable("relPwd", "")
151 }
152}
153
Colin Cross3f40fa42015-01-30 17:27:36 -0800154type builderFlags struct {
155 globalFlags string
156 asFlags string
157 cFlags string
158 conlyFlags string
159 cppFlags string
160 ldFlags string
Colin Cross16b23492016-01-06 14:41:07 -0800161 libFlags string
Colin Cross581c1892015-04-07 16:50:10 -0700162 yaccFlags string
Colin Cross3f40fa42015-01-30 17:27:36 -0800163 nocrt bool
Colin Cross97ba0732015-03-23 17:50:24 -0700164 toolchain Toolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800165 clang bool
Colin Cross665dce92016-04-28 14:50:03 -0700166
167 stripKeepSymbols bool
168 stripKeepMiniDebugInfo bool
169 stripAddGnuDebuglink bool
Colin Cross3f40fa42015-01-30 17:27:36 -0800170}
171
172// Generate rules for compiling multiple .c, .cpp, or .S files to individual .o files
Colin Cross635c3b02016-05-18 15:37:25 -0700173func TransformSourceToObj(ctx android.ModuleContext, subdir string, srcFiles android.Paths,
174 flags builderFlags, deps android.Paths) (objFiles android.Paths) {
Colin Cross581c1892015-04-07 16:50:10 -0700175
Colin Cross635c3b02016-05-18 15:37:25 -0700176 objFiles = make(android.Paths, len(srcFiles))
Colin Cross3f40fa42015-01-30 17:27:36 -0800177
178 cflags := flags.globalFlags + " " + flags.cFlags + " " + flags.conlyFlags
179 cppflags := flags.globalFlags + " " + flags.cFlags + " " + flags.cppFlags
180 asflags := flags.globalFlags + " " + flags.asFlags
181
Dan Willemsenbe03f342016-03-03 17:21:04 -0800182 if flags.clang {
183 cflags += " ${noOverrideClangGlobalCflags}"
184 cppflags += " ${noOverrideClangGlobalCflags}"
185 } else {
186 cflags += " ${noOverrideGlobalCflags}"
187 cppflags += " ${noOverrideGlobalCflags}"
188 }
189
Colin Cross3f40fa42015-01-30 17:27:36 -0800190 for i, srcFile := range srcFiles {
Colin Cross635c3b02016-05-18 15:37:25 -0700191 objFile := android.ObjPathWithExt(ctx, srcFile, subdir, "o")
Colin Cross3f40fa42015-01-30 17:27:36 -0800192
193 objFiles[i] = objFile
194
195 var moduleCflags string
196 var ccCmd string
197
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700198 switch srcFile.Ext() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800199 case ".S", ".s":
200 ccCmd = "gcc"
201 moduleCflags = asflags
202 case ".c":
203 ccCmd = "gcc"
204 moduleCflags = cflags
205 case ".cpp", ".cc":
206 ccCmd = "g++"
207 moduleCflags = cppflags
208 default:
209 ctx.ModuleErrorf("File %s has unknown extension", srcFile)
210 continue
211 }
212
213 if flags.clang {
214 switch ccCmd {
215 case "gcc":
216 ccCmd = "clang"
217 case "g++":
218 ccCmd = "clang++"
219 default:
220 panic("unrecoginzied ccCmd")
221 }
222
Colin Cross16b23492016-01-06 14:41:07 -0800223 ccCmd = "${clangBin}/" + ccCmd
Colin Cross3f40fa42015-01-30 17:27:36 -0800224 } else {
225 ccCmd = gccCmd(flags.toolchain, ccCmd)
226 }
227
Colin Cross635c3b02016-05-18 15:37:25 -0700228 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross3f40fa42015-01-30 17:27:36 -0800229 Rule: cc,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700230 Output: objFile,
231 Input: srcFile,
Dan Willemsenb40aab62016-04-20 14:21:14 -0700232 OrderOnly: deps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800233 Args: map[string]string{
Colin Cross28344522015-04-22 13:07:53 -0700234 "cFlags": moduleCflags,
235 "ccCmd": ccCmd,
Colin Cross3f40fa42015-01-30 17:27:36 -0800236 },
237 })
238 }
239
240 return objFiles
241}
242
243// Generate a rule for compiling multiple .o files to a static library (.a)
Colin Cross635c3b02016-05-18 15:37:25 -0700244func TransformObjToStaticLib(ctx android.ModuleContext, objFiles android.Paths,
245 flags builderFlags, outputFile android.ModuleOutPath) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800246
247 arCmd := gccCmd(flags.toolchain, "ar")
248 arFlags := "crsPD"
249
Colin Cross635c3b02016-05-18 15:37:25 -0700250 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700251 Rule: ar,
252 Output: outputFile,
253 Inputs: objFiles,
Colin Cross3f40fa42015-01-30 17:27:36 -0800254 Args: map[string]string{
255 "arFlags": arFlags,
256 "arCmd": arCmd,
257 },
258 })
259}
260
Colin Cross0af4b842015-04-30 16:36:18 -0700261// Generate a rule for compiling multiple .o files to a static library (.a) on
262// darwin. The darwin ar tool doesn't support @file for list files, and has a
263// very small command line length limit, so we have to split the ar into multiple
264// steps, each appending to the previous one.
Colin Cross635c3b02016-05-18 15:37:25 -0700265func TransformDarwinObjToStaticLib(ctx android.ModuleContext, objFiles android.Paths,
266 flags builderFlags, outputPath android.ModuleOutPath) {
Colin Cross0af4b842015-04-30 16:36:18 -0700267
Colin Cross0af4b842015-04-30 16:36:18 -0700268 arFlags := "cqs"
269
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700270 if len(objFiles) == 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700271 dummy := android.PathForModuleOut(ctx, "dummy"+objectExtension)
272 dummyAr := android.PathForModuleOut(ctx, "dummy"+staticLibraryExtension)
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700273
Colin Cross635c3b02016-05-18 15:37:25 -0700274 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700275 Rule: emptyFile,
276 Output: dummy,
277 })
278
Colin Cross635c3b02016-05-18 15:37:25 -0700279 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700280 Rule: darwinAr,
281 Output: dummyAr,
282 Input: dummy,
283 Args: map[string]string{
284 "arFlags": arFlags,
285 },
286 })
287
Colin Cross635c3b02016-05-18 15:37:25 -0700288 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700289 Rule: darwinAppendAr,
290 Output: outputPath,
291 Input: dummy,
292 Args: map[string]string{
293 "arFlags": "d",
294 "inAr": dummyAr.String(),
295 },
296 })
297
298 return
299 }
300
Colin Cross0af4b842015-04-30 16:36:18 -0700301 // ARG_MAX on darwin is 262144, use half that to be safe
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700302 objFilesLists, err := splitListForSize(objFiles.Strings(), 131072)
Colin Cross0af4b842015-04-30 16:36:18 -0700303 if err != nil {
304 ctx.ModuleErrorf("%s", err.Error())
305 }
306
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700307 outputFile := outputPath.String()
308
Colin Cross0af4b842015-04-30 16:36:18 -0700309 var in, out string
310 for i, l := range objFilesLists {
311 in = out
312 out = outputFile
313 if i != len(objFilesLists)-1 {
314 out += "." + strconv.Itoa(i)
315 }
316
317 if in == "" {
318 ctx.Build(pctx, blueprint.BuildParams{
319 Rule: darwinAr,
320 Outputs: []string{out},
321 Inputs: l,
322 Args: map[string]string{
323 "arFlags": arFlags,
Colin Cross0af4b842015-04-30 16:36:18 -0700324 },
325 })
326 } else {
327 ctx.Build(pctx, blueprint.BuildParams{
Colin Cross635c3b02016-05-18 15:37:25 -0700328 Rule: darwinAppendAr,
329 Outputs: []string{out},
330 Inputs: l,
Colin Cross0af4b842015-04-30 16:36:18 -0700331 Args: map[string]string{
332 "arFlags": arFlags,
Colin Cross0af4b842015-04-30 16:36:18 -0700333 "inAr": in,
334 },
335 })
336 }
337 }
338}
339
Colin Cross3f40fa42015-01-30 17:27:36 -0800340// Generate a rule for compiling multiple .o files, plus static libraries, whole static libraries,
341// and shared libraires, to a shared library (.so) or dynamic executable
Colin Cross635c3b02016-05-18 15:37:25 -0700342func TransformObjToDynamicBinary(ctx android.ModuleContext,
343 objFiles, sharedLibs, staticLibs, lateStaticLibs, wholeStaticLibs, deps android.Paths,
344 crtBegin, crtEnd android.OptionalPath, groupLate bool, flags builderFlags, outputFile android.WritablePath) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800345
346 var ldCmd string
347 if flags.clang {
Colin Cross16b23492016-01-06 14:41:07 -0800348 ldCmd = "${clangBin}/clang++"
Colin Cross3f40fa42015-01-30 17:27:36 -0800349 } else {
350 ldCmd = gccCmd(flags.toolchain, "g++")
351 }
352
353 var ldDirs []string
354 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
Dan Willemsenedc385f2015-07-08 13:02:23 -0700372 if groupLate && len(lateStaticLibs) > 0 {
373 libFlagsList = append(libFlagsList, "-Wl,--start-group")
374 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700375 libFlagsList = append(libFlagsList, lateStaticLibs.Strings()...)
Dan Willemsenedc385f2015-07-08 13:02:23 -0700376 if groupLate && len(lateStaticLibs) > 0 {
377 libFlagsList = append(libFlagsList, "-Wl,--end-group")
378 }
379
Colin Cross3f40fa42015-01-30 17:27:36 -0800380 for _, lib := range sharedLibs {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700381 dir, file := filepath.Split(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 }
388 libFlagsList = append(libFlagsList,
Dan Willemsen490fd492015-11-24 17:53:15 -0800389 "-l"+strings.TrimSuffix(strings.TrimPrefix(file, "lib"), flags.toolchain.ShlibSuffix()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800390 ldDirs = append(ldDirs, dir)
391 }
392
Colin Cross3f40fa42015-01-30 17:27:36 -0800393 deps = append(deps, sharedLibs...)
394 deps = append(deps, staticLibs...)
Colin Cross3075ad02015-03-17 10:47:08 -0700395 deps = append(deps, lateStaticLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800396 deps = append(deps, wholeStaticLibs...)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700397 if crtBegin.Valid() {
398 deps = append(deps, crtBegin.Path(), crtEnd.Path())
Colin Cross3f40fa42015-01-30 17:27:36 -0800399 }
400
Colin Cross635c3b02016-05-18 15:37:25 -0700401 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross3f40fa42015-01-30 17:27:36 -0800402 Rule: ld,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700403 Output: outputFile,
Colin Cross3f40fa42015-01-30 17:27:36 -0800404 Inputs: objFiles,
405 Implicits: deps,
406 Args: map[string]string{
407 "ldCmd": ldCmd,
408 "ldDirFlags": ldDirsToFlags(ldDirs),
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700409 "crtBegin": crtBegin.String(),
Colin Cross3f40fa42015-01-30 17:27:36 -0800410 "libFlags": strings.Join(libFlagsList, " "),
411 "ldFlags": flags.ldFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700412 "crtEnd": crtEnd.String(),
Colin Cross3f40fa42015-01-30 17:27:36 -0800413 },
414 })
415}
416
417// Generate a rule for compiling multiple .o files to a .o using ld partial linking
Colin Cross635c3b02016-05-18 15:37:25 -0700418func TransformObjsToObj(ctx android.ModuleContext, objFiles android.Paths,
419 flags builderFlags, outputFile android.WritablePath) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800420
Colin Cross41280a42015-11-23 14:01:42 -0800421 var ldCmd string
422 if flags.clang {
Colin Cross16b23492016-01-06 14:41:07 -0800423 ldCmd = "${clangBin}clang++"
Colin Cross41280a42015-11-23 14:01:42 -0800424 } else {
425 ldCmd = gccCmd(flags.toolchain, "g++")
426 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800427
Colin Cross635c3b02016-05-18 15:37:25 -0700428 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700429 Rule: partialLd,
430 Output: outputFile,
431 Inputs: objFiles,
Colin Cross3f40fa42015-01-30 17:27:36 -0800432 Args: map[string]string{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700433 "ldCmd": ldCmd,
Colin Cross41280a42015-11-23 14:01:42 -0800434 "ldFlags": flags.ldFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800435 },
436 })
437}
438
Colin Crossbfae8852015-03-26 14:44:11 -0700439// Generate a rule for runing objcopy --prefix-symbols on a binary
Colin Cross635c3b02016-05-18 15:37:25 -0700440func TransformBinaryPrefixSymbols(ctx android.ModuleContext, prefix string, inputFile android.Path,
441 flags builderFlags, outputFile android.WritablePath) {
Colin Crossbfae8852015-03-26 14:44:11 -0700442
443 objcopyCmd := gccCmd(flags.toolchain, "objcopy")
444
Colin Cross635c3b02016-05-18 15:37:25 -0700445 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700446 Rule: prefixSymbols,
447 Output: outputFile,
448 Input: inputFile,
Colin Crossbfae8852015-03-26 14:44:11 -0700449 Args: map[string]string{
450 "objcopyCmd": objcopyCmd,
451 "prefix": prefix,
452 },
453 })
454}
455
Colin Cross635c3b02016-05-18 15:37:25 -0700456func TransformStrip(ctx android.ModuleContext, inputFile android.Path,
457 outputFile android.WritablePath, flags builderFlags) {
Colin Cross665dce92016-04-28 14:50:03 -0700458
459 crossCompile := gccCmd(flags.toolchain, "")
460 args := ""
461 if flags.stripAddGnuDebuglink {
462 args += " --add-gnu-debuglink"
463 }
464 if flags.stripKeepMiniDebugInfo {
465 args += " --keep-mini-debug-info"
466 }
467 if flags.stripKeepSymbols {
468 args += " --keep-symbols"
469 }
470
Colin Cross635c3b02016-05-18 15:37:25 -0700471 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross665dce92016-04-28 14:50:03 -0700472 Rule: strip,
473 Output: outputFile,
474 Input: inputFile,
475 Args: map[string]string{
476 "crossCompile": crossCompile,
477 "args": args,
478 },
479 })
480}
481
Colin Cross635c3b02016-05-18 15:37:25 -0700482func TransformDarwinStrip(ctx android.ModuleContext, inputFile android.Path,
483 outputFile android.WritablePath) {
Colin Crossb8ecdfe2016-05-03 15:10:29 -0700484
Colin Cross635c3b02016-05-18 15:37:25 -0700485 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Crossb8ecdfe2016-05-03 15:10:29 -0700486 Rule: darwinStrip,
487 Output: outputFile,
488 Input: inputFile,
489 })
490}
491
Colin Cross635c3b02016-05-18 15:37:25 -0700492func CopyGccLib(ctx android.ModuleContext, libName string,
493 flags builderFlags, outputFile android.WritablePath) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800494
Colin Cross635c3b02016-05-18 15:37:25 -0700495 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700496 Rule: copyGccLib,
497 Output: outputFile,
Colin Cross3f40fa42015-01-30 17:27:36 -0800498 Args: map[string]string{
499 "ccCmd": gccCmd(flags.toolchain, "gcc"),
500 "cFlags": flags.globalFlags,
501 "libName": libName,
502 },
503 })
504}
505
Colin Cross97ba0732015-03-23 17:50:24 -0700506func gccCmd(toolchain Toolchain, cmd string) string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800507 return filepath.Join(toolchain.GccRoot(), "bin", toolchain.GccTriple()+"-"+cmd)
508}
Colin Cross0af4b842015-04-30 16:36:18 -0700509
510func splitListForSize(list []string, limit int) (lists [][]string, err error) {
511 var i int
512
513 start := 0
514 bytes := 0
515 for i = range list {
516 l := len(list[i])
517 if l > limit {
518 return nil, fmt.Errorf("list element greater than size limit (%d)", limit)
519 }
520 if bytes+l > limit {
521 lists = append(lists, list[start:i])
522 start = i
523 bytes = 0
524 }
525 bytes += l + 1 // count a space between each list element
526 }
527
528 lists = append(lists, list[start:])
529
530 totalLen := 0
531 for _, l := range lists {
532 totalLen += len(l)
533 }
534 if totalLen != len(list) {
535 panic(fmt.Errorf("Failed breaking up list, %d != %d", len(list), totalLen))
536 }
537 return lists, nil
538}