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