Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1 | // 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 | |
| 15 | package 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 | |
| 21 | import ( |
| 22 | "android/soong/common" |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 23 | "fmt" |
| 24 | "runtime" |
| 25 | "strconv" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 26 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 27 | "path/filepath" |
| 28 | "strings" |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 29 | |
| 30 | "github.com/google/blueprint" |
| 31 | "github.com/google/blueprint/pathtools" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 32 | ) |
| 33 | |
| 34 | const ( |
Dan Albert | c3144b1 | 2015-04-28 18:17:56 -0700 | [diff] [blame] | 35 | objectExtension = ".o" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 36 | staticLibraryExtension = ".a" |
| 37 | ) |
| 38 | |
| 39 | var ( |
| 40 | pctx = blueprint.NewPackageContext("android/soong/cc") |
| 41 | |
| 42 | cc = pctx.StaticRule("cc", |
| 43 | blueprint.RuleParams{ |
| 44 | Depfile: "${out}.d", |
| 45 | Deps: blueprint.DepsGCC, |
Dan Willemsen | e654045 | 2015-10-20 15:21:33 -0700 | [diff] [blame] | 46 | Command: "$relPwd $ccCmd -c $cFlags -MD -MF ${out}.d -o $out $in", |
Dan Willemsen | c94a768 | 2015-11-17 15:27:28 -0800 | [diff] [blame] | 47 | CommandDeps: []string{"$ccCmd"}, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 48 | Description: "cc $out", |
| 49 | }, |
Dan Willemsen | 322a0a6 | 2015-11-17 15:19:46 -0800 | [diff] [blame] | 50 | "ccCmd", "cFlags") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 51 | |
| 52 | ld = pctx.StaticRule("ld", |
| 53 | blueprint.RuleParams{ |
Colin Cross | 7d21c44 | 2015-03-30 17:47:53 -0700 | [diff] [blame] | 54 | Command: "$ldCmd ${ldDirFlags} ${crtBegin} @${out}.rsp " + |
Colin Cross | 2834452 | 2015-04-22 13:07:53 -0700 | [diff] [blame] | 55 | "${libFlags} ${crtEnd} -o ${out} ${ldFlags}", |
Dan Willemsen | c94a768 | 2015-11-17 15:27:28 -0800 | [diff] [blame] | 56 | CommandDeps: []string{"$ldCmd"}, |
Colin Cross | 7d21c44 | 2015-03-30 17:47:53 -0700 | [diff] [blame] | 57 | Description: "ld $out", |
| 58 | Rspfile: "${out}.rsp", |
| 59 | RspfileContent: "${in}", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 60 | }, |
Colin Cross | 2834452 | 2015-04-22 13:07:53 -0700 | [diff] [blame] | 61 | "ldCmd", "ldDirFlags", "crtBegin", "libFlags", "crtEnd", "ldFlags") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 62 | |
| 63 | partialLd = pctx.StaticRule("partialLd", |
| 64 | blueprint.RuleParams{ |
Colin Cross | 41280a4 | 2015-11-23 14:01:42 -0800 | [diff] [blame] | 65 | Command: "$ldCmd -nostdlib -Wl,-r ${in} -o ${out} ${ldFlags}", |
Dan Willemsen | c94a768 | 2015-11-17 15:27:28 -0800 | [diff] [blame] | 66 | CommandDeps: []string{"$ldCmd"}, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 67 | Description: "partialLd $out", |
| 68 | }, |
Colin Cross | 41280a4 | 2015-11-23 14:01:42 -0800 | [diff] [blame] | 69 | "ldCmd", "ldFlags") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 70 | |
| 71 | ar = pctx.StaticRule("ar", |
| 72 | blueprint.RuleParams{ |
Colin Cross | 7d21c44 | 2015-03-30 17:47:53 -0700 | [diff] [blame] | 73 | Command: "rm -f ${out} && $arCmd $arFlags $out @${out}.rsp", |
Dan Willemsen | c94a768 | 2015-11-17 15:27:28 -0800 | [diff] [blame] | 74 | CommandDeps: []string{"$arCmd"}, |
Colin Cross | 7d21c44 | 2015-03-30 17:47:53 -0700 | [diff] [blame] | 75 | Description: "ar $out", |
| 76 | Rspfile: "${out}.rsp", |
| 77 | RspfileContent: "${in}", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 78 | }, |
| 79 | "arCmd", "arFlags") |
| 80 | |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 81 | darwinAr = pctx.StaticRule("darwinAr", |
| 82 | blueprint.RuleParams{ |
| 83 | Command: "rm -f ${out} && $arCmd $arFlags $out $in", |
Dan Willemsen | c94a768 | 2015-11-17 15:27:28 -0800 | [diff] [blame] | 84 | CommandDeps: []string{"$arCmd"}, |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 85 | Description: "ar $out", |
| 86 | }, |
| 87 | "arCmd", "arFlags") |
| 88 | |
| 89 | darwinAppendAr = pctx.StaticRule("darwinAppendAr", |
| 90 | blueprint.RuleParams{ |
| 91 | Command: "cp -f ${inAr} ${out}.tmp && $arCmd $arFlags ${out}.tmp $in && mv ${out}.tmp ${out}", |
Dan Willemsen | c94a768 | 2015-11-17 15:27:28 -0800 | [diff] [blame] | 92 | CommandDeps: []string{"$arCmd"}, |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 93 | Description: "ar $out", |
| 94 | }, |
| 95 | "arCmd", "arFlags", "inAr") |
| 96 | |
Colin Cross | bfae885 | 2015-03-26 14:44:11 -0700 | [diff] [blame] | 97 | prefixSymbols = pctx.StaticRule("prefixSymbols", |
| 98 | blueprint.RuleParams{ |
| 99 | Command: "$objcopyCmd --prefix-symbols=${prefix} ${in} ${out}", |
Dan Willemsen | c94a768 | 2015-11-17 15:27:28 -0800 | [diff] [blame] | 100 | CommandDeps: []string{"$objcopyCmd"}, |
Colin Cross | bfae885 | 2015-03-26 14:44:11 -0700 | [diff] [blame] | 101 | Description: "prefixSymbols $out", |
| 102 | }, |
| 103 | "objcopyCmd", "prefix") |
| 104 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 105 | copyGccLibPath = pctx.StaticVariable("copyGccLibPath", "${SrcDir}/build/soong/copygcclib.sh") |
| 106 | |
| 107 | copyGccLib = pctx.StaticRule("copyGccLib", |
| 108 | blueprint.RuleParams{ |
| 109 | Depfile: "${out}.d", |
| 110 | Deps: blueprint.DepsGCC, |
| 111 | Command: "$copyGccLibPath $out $ccCmd $cFlags -print-file-name=${libName}", |
Dan Willemsen | c94a768 | 2015-11-17 15:27:28 -0800 | [diff] [blame] | 112 | CommandDeps: []string{"$copyGccLibPath", "$ccCmd"}, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 113 | Description: "copy gcc $out", |
| 114 | }, |
| 115 | "ccCmd", "cFlags", "libName") |
| 116 | ) |
| 117 | |
Dan Willemsen | 322a0a6 | 2015-11-17 15:19:46 -0800 | [diff] [blame] | 118 | func init() { |
| 119 | // We run gcc/clang with PWD=/proc/self/cwd to remove $TOP from the |
| 120 | // debug output. That way two builds in two different directories will |
| 121 | // create the same output. |
| 122 | if runtime.GOOS != "darwin" { |
| 123 | pctx.StaticVariable("relPwd", "PWD=/proc/self/cwd") |
| 124 | } else { |
| 125 | // Darwin doesn't have /proc |
| 126 | pctx.StaticVariable("relPwd", "") |
| 127 | } |
| 128 | } |
| 129 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 130 | type builderFlags struct { |
| 131 | globalFlags string |
| 132 | asFlags string |
| 133 | cFlags string |
| 134 | conlyFlags string |
| 135 | cppFlags string |
| 136 | ldFlags string |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 137 | yaccFlags string |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 138 | nocrt bool |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 139 | toolchain Toolchain |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 140 | clang bool |
| 141 | } |
| 142 | |
| 143 | // Generate rules for compiling multiple .c, .cpp, or .S files to individual .o files |
| 144 | func TransformSourceToObj(ctx common.AndroidModuleContext, subdir string, srcFiles []string, |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 145 | flags builderFlags, deps []string) (objFiles []string) { |
| 146 | |
Colin Cross | 1332b00 | 2015-04-07 17:11:30 -0700 | [diff] [blame] | 147 | srcRoot := ctx.AConfig().SrcDir() |
| 148 | intermediatesRoot := ctx.AConfig().IntermediatesDir() |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 149 | |
| 150 | objFiles = make([]string, len(srcFiles)) |
| 151 | objDir := common.ModuleObjDir(ctx) |
| 152 | if subdir != "" { |
| 153 | objDir = filepath.Join(objDir, subdir) |
| 154 | } |
| 155 | |
| 156 | cflags := flags.globalFlags + " " + flags.cFlags + " " + flags.conlyFlags |
| 157 | cppflags := flags.globalFlags + " " + flags.cFlags + " " + flags.cppFlags |
| 158 | asflags := flags.globalFlags + " " + flags.asFlags |
| 159 | |
| 160 | for i, srcFile := range srcFiles { |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 161 | var objFile string |
Dan Willemsen | 87b17d1 | 2015-07-14 00:39:06 -0700 | [diff] [blame] | 162 | if strings.HasPrefix(srcFile, intermediatesRoot) { |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 163 | objFile = strings.TrimPrefix(srcFile, intermediatesRoot) |
| 164 | objFile = filepath.Join(objDir, "gen", objFile) |
Dan Willemsen | 87b17d1 | 2015-07-14 00:39:06 -0700 | [diff] [blame] | 165 | } else if strings.HasPrefix(srcFile, srcRoot) { |
| 166 | srcFile, _ = filepath.Rel(srcRoot, srcFile) |
| 167 | objFile = filepath.Join(objDir, srcFile) |
| 168 | } else if srcRoot == "." && srcFile[0] != '/' { |
| 169 | objFile = filepath.Join(objDir, srcFile) |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 170 | } else { |
| 171 | ctx.ModuleErrorf("source file %q is not in source directory %q", srcFile, srcRoot) |
| 172 | continue |
| 173 | } |
| 174 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 175 | objFile = pathtools.ReplaceExtension(objFile, "o") |
| 176 | |
| 177 | objFiles[i] = objFile |
| 178 | |
| 179 | var moduleCflags string |
| 180 | var ccCmd string |
| 181 | |
| 182 | switch filepath.Ext(srcFile) { |
| 183 | case ".S", ".s": |
| 184 | ccCmd = "gcc" |
| 185 | moduleCflags = asflags |
| 186 | case ".c": |
| 187 | ccCmd = "gcc" |
| 188 | moduleCflags = cflags |
| 189 | case ".cpp", ".cc": |
| 190 | ccCmd = "g++" |
| 191 | moduleCflags = cppflags |
| 192 | default: |
| 193 | ctx.ModuleErrorf("File %s has unknown extension", srcFile) |
| 194 | continue |
| 195 | } |
| 196 | |
| 197 | if flags.clang { |
| 198 | switch ccCmd { |
| 199 | case "gcc": |
| 200 | ccCmd = "clang" |
| 201 | case "g++": |
| 202 | ccCmd = "clang++" |
| 203 | default: |
| 204 | panic("unrecoginzied ccCmd") |
| 205 | } |
| 206 | |
| 207 | ccCmd = "${clangPath}" + ccCmd |
| 208 | } else { |
| 209 | ccCmd = gccCmd(flags.toolchain, ccCmd) |
| 210 | } |
| 211 | |
| 212 | ctx.Build(pctx, blueprint.BuildParams{ |
| 213 | Rule: cc, |
| 214 | Outputs: []string{objFile}, |
| 215 | Inputs: []string{srcFile}, |
Dan Willemsen | c94a768 | 2015-11-17 15:27:28 -0800 | [diff] [blame] | 216 | Implicits: deps, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 217 | Args: map[string]string{ |
Colin Cross | 2834452 | 2015-04-22 13:07:53 -0700 | [diff] [blame] | 218 | "cFlags": moduleCflags, |
| 219 | "ccCmd": ccCmd, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 220 | }, |
| 221 | }) |
| 222 | } |
| 223 | |
| 224 | return objFiles |
| 225 | } |
| 226 | |
| 227 | // Generate a rule for compiling multiple .o files to a static library (.a) |
| 228 | func TransformObjToStaticLib(ctx common.AndroidModuleContext, objFiles []string, |
| 229 | flags builderFlags, outputFile string) { |
| 230 | |
| 231 | arCmd := gccCmd(flags.toolchain, "ar") |
| 232 | arFlags := "crsPD" |
| 233 | |
| 234 | ctx.Build(pctx, blueprint.BuildParams{ |
Dan Willemsen | c94a768 | 2015-11-17 15:27:28 -0800 | [diff] [blame] | 235 | Rule: ar, |
| 236 | Outputs: []string{outputFile}, |
| 237 | Inputs: objFiles, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 238 | Args: map[string]string{ |
| 239 | "arFlags": arFlags, |
| 240 | "arCmd": arCmd, |
| 241 | }, |
| 242 | }) |
| 243 | } |
| 244 | |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 245 | // Generate a rule for compiling multiple .o files to a static library (.a) on |
| 246 | // darwin. The darwin ar tool doesn't support @file for list files, and has a |
| 247 | // very small command line length limit, so we have to split the ar into multiple |
| 248 | // steps, each appending to the previous one. |
| 249 | func TransformDarwinObjToStaticLib(ctx common.AndroidModuleContext, objFiles []string, |
| 250 | flags builderFlags, outputFile string) { |
| 251 | |
| 252 | arCmd := "ar" |
| 253 | arFlags := "cqs" |
| 254 | |
| 255 | // ARG_MAX on darwin is 262144, use half that to be safe |
| 256 | objFilesLists, err := splitListForSize(objFiles, 131072) |
| 257 | if err != nil { |
| 258 | ctx.ModuleErrorf("%s", err.Error()) |
| 259 | } |
| 260 | |
| 261 | var in, out string |
| 262 | for i, l := range objFilesLists { |
| 263 | in = out |
| 264 | out = outputFile |
| 265 | if i != len(objFilesLists)-1 { |
| 266 | out += "." + strconv.Itoa(i) |
| 267 | } |
| 268 | |
| 269 | if in == "" { |
| 270 | ctx.Build(pctx, blueprint.BuildParams{ |
| 271 | Rule: darwinAr, |
| 272 | Outputs: []string{out}, |
| 273 | Inputs: l, |
| 274 | Args: map[string]string{ |
| 275 | "arFlags": arFlags, |
| 276 | "arCmd": arCmd, |
| 277 | }, |
| 278 | }) |
| 279 | } else { |
| 280 | ctx.Build(pctx, blueprint.BuildParams{ |
| 281 | Rule: darwinAppendAr, |
| 282 | Outputs: []string{out}, |
| 283 | Inputs: l, |
| 284 | Implicits: []string{in}, |
| 285 | Args: map[string]string{ |
| 286 | "arFlags": arFlags, |
| 287 | "arCmd": arCmd, |
| 288 | "inAr": in, |
| 289 | }, |
| 290 | }) |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 295 | // Generate a rule for compiling multiple .o files, plus static libraries, whole static libraries, |
| 296 | // and shared libraires, to a shared library (.so) or dynamic executable |
| 297 | func TransformObjToDynamicBinary(ctx common.AndroidModuleContext, |
Colin Cross | aee540a | 2015-07-06 17:48:31 -0700 | [diff] [blame] | 298 | objFiles, sharedLibs, staticLibs, lateStaticLibs, wholeStaticLibs, deps []string, |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 299 | crtBegin, crtEnd string, groupLate bool, flags builderFlags, outputFile string) { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 300 | |
| 301 | var ldCmd string |
| 302 | if flags.clang { |
| 303 | ldCmd = "${clangPath}clang++" |
| 304 | } else { |
| 305 | ldCmd = gccCmd(flags.toolchain, "g++") |
| 306 | } |
| 307 | |
| 308 | var ldDirs []string |
| 309 | var libFlagsList []string |
| 310 | |
| 311 | if len(wholeStaticLibs) > 0 { |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 312 | if ctx.Host() && ctx.Darwin() { |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 313 | libFlagsList = append(libFlagsList, common.JoinWithPrefix(wholeStaticLibs, "-force_load ")) |
| 314 | } else { |
| 315 | libFlagsList = append(libFlagsList, "-Wl,--whole-archive ") |
| 316 | libFlagsList = append(libFlagsList, wholeStaticLibs...) |
| 317 | libFlagsList = append(libFlagsList, "-Wl,--no-whole-archive ") |
| 318 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | libFlagsList = append(libFlagsList, staticLibs...) |
| 322 | |
Dan Willemsen | edc385f | 2015-07-08 13:02:23 -0700 | [diff] [blame] | 323 | if groupLate && len(lateStaticLibs) > 0 { |
| 324 | libFlagsList = append(libFlagsList, "-Wl,--start-group") |
| 325 | } |
| 326 | libFlagsList = append(libFlagsList, lateStaticLibs...) |
| 327 | if groupLate && len(lateStaticLibs) > 0 { |
| 328 | libFlagsList = append(libFlagsList, "-Wl,--end-group") |
| 329 | } |
| 330 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 331 | for _, lib := range sharedLibs { |
| 332 | dir, file := filepath.Split(lib) |
| 333 | if !strings.HasPrefix(file, "lib") { |
| 334 | panic("shared library " + lib + " does not start with lib") |
| 335 | } |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 336 | if !strings.HasSuffix(file, flags.toolchain.ShlibSuffix()) { |
| 337 | panic("shared library " + lib + " does not end with " + flags.toolchain.ShlibSuffix()) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 338 | } |
| 339 | libFlagsList = append(libFlagsList, |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 340 | "-l"+strings.TrimSuffix(strings.TrimPrefix(file, "lib"), flags.toolchain.ShlibSuffix())) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 341 | ldDirs = append(ldDirs, dir) |
| 342 | } |
| 343 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 344 | deps = append(deps, sharedLibs...) |
| 345 | deps = append(deps, staticLibs...) |
Colin Cross | 3075ad0 | 2015-03-17 10:47:08 -0700 | [diff] [blame] | 346 | deps = append(deps, lateStaticLibs...) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 347 | deps = append(deps, wholeStaticLibs...) |
| 348 | if crtBegin != "" { |
| 349 | deps = append(deps, crtBegin, crtEnd) |
| 350 | } |
| 351 | |
| 352 | ctx.Build(pctx, blueprint.BuildParams{ |
| 353 | Rule: ld, |
| 354 | Outputs: []string{outputFile}, |
| 355 | Inputs: objFiles, |
| 356 | Implicits: deps, |
| 357 | Args: map[string]string{ |
| 358 | "ldCmd": ldCmd, |
| 359 | "ldDirFlags": ldDirsToFlags(ldDirs), |
| 360 | "crtBegin": crtBegin, |
| 361 | "libFlags": strings.Join(libFlagsList, " "), |
| 362 | "ldFlags": flags.ldFlags, |
| 363 | "crtEnd": crtEnd, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 364 | }, |
| 365 | }) |
| 366 | } |
| 367 | |
| 368 | // Generate a rule for compiling multiple .o files to a .o using ld partial linking |
| 369 | func TransformObjsToObj(ctx common.AndroidModuleContext, objFiles []string, |
| 370 | flags builderFlags, outputFile string) { |
| 371 | |
Colin Cross | 41280a4 | 2015-11-23 14:01:42 -0800 | [diff] [blame] | 372 | var ldCmd string |
| 373 | if flags.clang { |
| 374 | ldCmd = "${clangPath}clang++" |
| 375 | } else { |
| 376 | ldCmd = gccCmd(flags.toolchain, "g++") |
| 377 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 378 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 379 | ctx.Build(pctx, blueprint.BuildParams{ |
Dan Willemsen | c94a768 | 2015-11-17 15:27:28 -0800 | [diff] [blame] | 380 | Rule: partialLd, |
| 381 | Outputs: []string{outputFile}, |
| 382 | Inputs: objFiles, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 383 | Args: map[string]string{ |
| 384 | "ldCmd": ldCmd, |
Colin Cross | 41280a4 | 2015-11-23 14:01:42 -0800 | [diff] [blame] | 385 | "ldFlags": flags.ldFlags, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 386 | }, |
| 387 | }) |
| 388 | } |
| 389 | |
Colin Cross | bfae885 | 2015-03-26 14:44:11 -0700 | [diff] [blame] | 390 | // Generate a rule for runing objcopy --prefix-symbols on a binary |
| 391 | func TransformBinaryPrefixSymbols(ctx common.AndroidModuleContext, prefix string, inputFile string, |
| 392 | flags builderFlags, outputFile string) { |
| 393 | |
| 394 | objcopyCmd := gccCmd(flags.toolchain, "objcopy") |
| 395 | |
| 396 | ctx.Build(pctx, blueprint.BuildParams{ |
Dan Willemsen | c94a768 | 2015-11-17 15:27:28 -0800 | [diff] [blame] | 397 | Rule: prefixSymbols, |
| 398 | Outputs: []string{outputFile}, |
| 399 | Inputs: []string{inputFile}, |
Colin Cross | bfae885 | 2015-03-26 14:44:11 -0700 | [diff] [blame] | 400 | Args: map[string]string{ |
| 401 | "objcopyCmd": objcopyCmd, |
| 402 | "prefix": prefix, |
| 403 | }, |
| 404 | }) |
| 405 | } |
| 406 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 407 | func CopyGccLib(ctx common.AndroidModuleContext, libName string, |
| 408 | flags builderFlags, outputFile string) { |
| 409 | |
| 410 | ctx.Build(pctx, blueprint.BuildParams{ |
| 411 | Rule: copyGccLib, |
| 412 | Outputs: []string{outputFile}, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 413 | Args: map[string]string{ |
| 414 | "ccCmd": gccCmd(flags.toolchain, "gcc"), |
| 415 | "cFlags": flags.globalFlags, |
| 416 | "libName": libName, |
| 417 | }, |
| 418 | }) |
| 419 | } |
| 420 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 421 | func gccCmd(toolchain Toolchain, cmd string) string { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 422 | return filepath.Join(toolchain.GccRoot(), "bin", toolchain.GccTriple()+"-"+cmd) |
| 423 | } |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 424 | |
| 425 | func splitListForSize(list []string, limit int) (lists [][]string, err error) { |
| 426 | var i int |
| 427 | |
| 428 | start := 0 |
| 429 | bytes := 0 |
| 430 | for i = range list { |
| 431 | l := len(list[i]) |
| 432 | if l > limit { |
| 433 | return nil, fmt.Errorf("list element greater than size limit (%d)", limit) |
| 434 | } |
| 435 | if bytes+l > limit { |
| 436 | lists = append(lists, list[start:i]) |
| 437 | start = i |
| 438 | bytes = 0 |
| 439 | } |
| 440 | bytes += l + 1 // count a space between each list element |
| 441 | } |
| 442 | |
| 443 | lists = append(lists, list[start:]) |
| 444 | |
| 445 | totalLen := 0 |
| 446 | for _, l := range lists { |
| 447 | totalLen += len(l) |
| 448 | } |
| 449 | if totalLen != len(list) { |
| 450 | panic(fmt.Errorf("Failed breaking up list, %d != %d", len(list), totalLen)) |
| 451 | } |
| 452 | return lists, nil |
| 453 | } |