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 ( |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 22 | "fmt" |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 23 | "path/filepath" |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 24 | "runtime" |
| 25 | "strconv" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 26 | "strings" |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 27 | |
| 28 | "github.com/google/blueprint" |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 29 | |
| 30 | "android/soong/android" |
| 31 | "android/soong/cc/config" |
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 ( |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 40 | pctx = android.NewPackageContext("android/soong/cc") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 41 | |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 42 | cc = pctx.AndroidGomaStaticRule("cc", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 43 | blueprint.RuleParams{ |
| 44 | Depfile: "${out}.d", |
| 45 | Deps: blueprint.DepsGCC, |
Alistair Strachan | 777475c | 2016-08-26 12:55:49 -0700 | [diff] [blame] | 46 | Command: "$relPwd ${config.CcWrapper}$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 | |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 52 | ld = pctx.AndroidStaticRule("ld", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 53 | blueprint.RuleParams{ |
Dan Albert | ce2b839 | 2016-07-21 13:16:49 -0700 | [diff] [blame] | 54 | Command: "$ldCmd ${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 | }, |
Dan Albert | ce2b839 | 2016-07-21 13:16:49 -0700 | [diff] [blame] | 61 | "ldCmd", "crtBegin", "libFlags", "crtEnd", "ldFlags") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 62 | |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 63 | partialLd = pctx.AndroidStaticRule("partialLd", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 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 | |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 71 | ar = pctx.AndroidStaticRule("ar", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 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 | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 81 | darwinAr = pctx.AndroidStaticRule("darwinAr", |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 82 | blueprint.RuleParams{ |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 83 | Command: "rm -f ${out} && ${config.MacArPath} $arFlags $out $in", |
| 84 | CommandDeps: []string{"${config.MacArPath}"}, |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 85 | Description: "ar $out", |
| 86 | }, |
Colin Cross | b8ecdfe | 2016-05-03 15:10:29 -0700 | [diff] [blame] | 87 | "arFlags") |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 88 | |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 89 | darwinAppendAr = pctx.AndroidStaticRule("darwinAppendAr", |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 90 | blueprint.RuleParams{ |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 91 | Command: "cp -f ${inAr} ${out}.tmp && ${config.MacArPath} $arFlags ${out}.tmp $in && mv ${out}.tmp ${out}", |
| 92 | CommandDeps: []string{"${config.MacArPath}", "${inAr}"}, |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 93 | Description: "ar $out", |
| 94 | }, |
Colin Cross | b8ecdfe | 2016-05-03 15:10:29 -0700 | [diff] [blame] | 95 | "arFlags", "inAr") |
| 96 | |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 97 | darwinStrip = pctx.AndroidStaticRule("darwinStrip", |
Colin Cross | b8ecdfe | 2016-05-03 15:10:29 -0700 | [diff] [blame] | 98 | blueprint.RuleParams{ |
Colin Cross | a24166b | 2016-08-01 15:42:38 -0700 | [diff] [blame] | 99 | Command: "${config.MacStripPath} -u -r -o $out $in", |
| 100 | CommandDeps: []string{"${config.MacStripPath}"}, |
Colin Cross | b8ecdfe | 2016-05-03 15:10:29 -0700 | [diff] [blame] | 101 | Description: "strip $out", |
| 102 | }) |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 103 | |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 104 | prefixSymbols = pctx.AndroidStaticRule("prefixSymbols", |
Colin Cross | bfae885 | 2015-03-26 14:44:11 -0700 | [diff] [blame] | 105 | blueprint.RuleParams{ |
| 106 | Command: "$objcopyCmd --prefix-symbols=${prefix} ${in} ${out}", |
Dan Willemsen | c94a768 | 2015-11-17 15:27:28 -0800 | [diff] [blame] | 107 | CommandDeps: []string{"$objcopyCmd"}, |
Colin Cross | bfae885 | 2015-03-26 14:44:11 -0700 | [diff] [blame] | 108 | Description: "prefixSymbols $out", |
| 109 | }, |
| 110 | "objcopyCmd", "prefix") |
| 111 | |
Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 112 | stripPath = pctx.SourcePathVariable("stripPath", "build/soong/scripts/strip.sh") |
| 113 | |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 114 | strip = pctx.AndroidStaticRule("strip", |
Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 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 | |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 124 | emptyFile = pctx.AndroidStaticRule("emptyFile", |
Dan Willemsen | 9f0b550 | 2016-05-13 14:05:09 -0700 | [diff] [blame] | 125 | blueprint.RuleParams{ |
| 126 | Command: "rm -f $out && touch $out", |
| 127 | Description: "empty file $out", |
| 128 | }) |
| 129 | |
Colin Cross | 1474741 | 2016-04-27 16:10:38 -0700 | [diff] [blame] | 130 | copyGccLibPath = pctx.SourcePathVariable("copyGccLibPath", "build/soong/scripts/copygcclib.sh") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 131 | |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 132 | copyGccLib = pctx.AndroidStaticRule("copyGccLib", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 133 | blueprint.RuleParams{ |
| 134 | Depfile: "${out}.d", |
| 135 | Deps: blueprint.DepsGCC, |
| 136 | Command: "$copyGccLibPath $out $ccCmd $cFlags -print-file-name=${libName}", |
Dan Willemsen | c94a768 | 2015-11-17 15:27:28 -0800 | [diff] [blame] | 137 | CommandDeps: []string{"$copyGccLibPath", "$ccCmd"}, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 138 | Description: "copy gcc $out", |
| 139 | }, |
| 140 | "ccCmd", "cFlags", "libName") |
| 141 | ) |
| 142 | |
Dan Willemsen | 322a0a6 | 2015-11-17 15:19:46 -0800 | [diff] [blame] | 143 | func 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 Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 155 | type builderFlags struct { |
| 156 | globalFlags string |
| 157 | asFlags string |
| 158 | cFlags string |
| 159 | conlyFlags string |
| 160 | cppFlags string |
| 161 | ldFlags string |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 162 | libFlags string |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 163 | yaccFlags string |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 164 | toolchain config.Toolchain |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 165 | clang bool |
Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 166 | |
| 167 | stripKeepSymbols bool |
| 168 | stripKeepMiniDebugInfo bool |
| 169 | stripAddGnuDebuglink bool |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | // Generate rules for compiling multiple .c, .cpp, or .S files to individual .o files |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 173 | func TransformSourceToObj(ctx android.ModuleContext, subdir string, srcFiles android.Paths, |
| 174 | flags builderFlags, deps android.Paths) (objFiles android.Paths) { |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 175 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 176 | objFiles = make(android.Paths, len(srcFiles)) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 177 | |
| 178 | cflags := flags.globalFlags + " " + flags.cFlags + " " + flags.conlyFlags |
| 179 | cppflags := flags.globalFlags + " " + flags.cFlags + " " + flags.cppFlags |
| 180 | asflags := flags.globalFlags + " " + flags.asFlags |
| 181 | |
Dan Willemsen | be03f34 | 2016-03-03 17:21:04 -0800 | [diff] [blame] | 182 | if flags.clang { |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 183 | cflags += " ${config.NoOverrideClangGlobalCflags}" |
| 184 | cppflags += " ${config.NoOverrideClangGlobalCflags}" |
Dan Willemsen | be03f34 | 2016-03-03 17:21:04 -0800 | [diff] [blame] | 185 | } else { |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 186 | cflags += " ${config.NoOverrideGlobalCflags}" |
| 187 | cppflags += " ${config.NoOverrideGlobalCflags}" |
Dan Willemsen | be03f34 | 2016-03-03 17:21:04 -0800 | [diff] [blame] | 188 | } |
| 189 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 190 | for i, srcFile := range srcFiles { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 191 | objFile := android.ObjPathWithExt(ctx, srcFile, subdir, "o") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 192 | |
| 193 | objFiles[i] = objFile |
| 194 | |
| 195 | var moduleCflags string |
| 196 | var ccCmd string |
| 197 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 198 | switch srcFile.Ext() { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 199 | 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 Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 223 | ccCmd = "${config.ClangBin}/" + ccCmd |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 224 | } else { |
| 225 | ccCmd = gccCmd(flags.toolchain, ccCmd) |
| 226 | } |
| 227 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 228 | ctx.ModuleBuild(pctx, android.ModuleBuildParams{ |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 229 | Rule: cc, |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 230 | Output: objFile, |
| 231 | Input: srcFile, |
Dan Willemsen | b40aab6 | 2016-04-20 14:21:14 -0700 | [diff] [blame] | 232 | OrderOnly: deps, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 233 | Args: map[string]string{ |
Colin Cross | 2834452 | 2015-04-22 13:07:53 -0700 | [diff] [blame] | 234 | "cFlags": moduleCflags, |
| 235 | "ccCmd": ccCmd, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 236 | }, |
| 237 | }) |
| 238 | } |
| 239 | |
| 240 | return objFiles |
| 241 | } |
| 242 | |
| 243 | // Generate a rule for compiling multiple .o files to a static library (.a) |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 244 | func TransformObjToStaticLib(ctx android.ModuleContext, objFiles android.Paths, |
| 245 | flags builderFlags, outputFile android.ModuleOutPath) { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 246 | |
| 247 | arCmd := gccCmd(flags.toolchain, "ar") |
| 248 | arFlags := "crsPD" |
| 249 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 250 | ctx.ModuleBuild(pctx, android.ModuleBuildParams{ |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 251 | Rule: ar, |
| 252 | Output: outputFile, |
| 253 | Inputs: objFiles, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 254 | Args: map[string]string{ |
| 255 | "arFlags": arFlags, |
| 256 | "arCmd": arCmd, |
| 257 | }, |
| 258 | }) |
| 259 | } |
| 260 | |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 261 | // 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 Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 265 | func TransformDarwinObjToStaticLib(ctx android.ModuleContext, objFiles android.Paths, |
| 266 | flags builderFlags, outputPath android.ModuleOutPath) { |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 267 | |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 268 | arFlags := "cqs" |
| 269 | |
Dan Willemsen | 9f0b550 | 2016-05-13 14:05:09 -0700 | [diff] [blame] | 270 | if len(objFiles) == 0 { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 271 | dummy := android.PathForModuleOut(ctx, "dummy"+objectExtension) |
| 272 | dummyAr := android.PathForModuleOut(ctx, "dummy"+staticLibraryExtension) |
Dan Willemsen | 9f0b550 | 2016-05-13 14:05:09 -0700 | [diff] [blame] | 273 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 274 | ctx.ModuleBuild(pctx, android.ModuleBuildParams{ |
Dan Willemsen | 9f0b550 | 2016-05-13 14:05:09 -0700 | [diff] [blame] | 275 | Rule: emptyFile, |
| 276 | Output: dummy, |
| 277 | }) |
| 278 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 279 | ctx.ModuleBuild(pctx, android.ModuleBuildParams{ |
Dan Willemsen | 9f0b550 | 2016-05-13 14:05:09 -0700 | [diff] [blame] | 280 | Rule: darwinAr, |
| 281 | Output: dummyAr, |
| 282 | Input: dummy, |
| 283 | Args: map[string]string{ |
| 284 | "arFlags": arFlags, |
| 285 | }, |
| 286 | }) |
| 287 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 288 | ctx.ModuleBuild(pctx, android.ModuleBuildParams{ |
Dan Willemsen | 9f0b550 | 2016-05-13 14:05:09 -0700 | [diff] [blame] | 289 | 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 Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 301 | // ARG_MAX on darwin is 262144, use half that to be safe |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 302 | objFilesLists, err := splitListForSize(objFiles.Strings(), 131072) |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 303 | if err != nil { |
| 304 | ctx.ModuleErrorf("%s", err.Error()) |
| 305 | } |
| 306 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 307 | outputFile := outputPath.String() |
| 308 | |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 309 | 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 Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 324 | }, |
| 325 | }) |
| 326 | } else { |
| 327 | ctx.Build(pctx, blueprint.BuildParams{ |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 328 | Rule: darwinAppendAr, |
| 329 | Outputs: []string{out}, |
| 330 | Inputs: l, |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 331 | Args: map[string]string{ |
| 332 | "arFlags": arFlags, |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 333 | "inAr": in, |
| 334 | }, |
| 335 | }) |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 340 | // 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 Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 342 | func 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 Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 345 | |
| 346 | var ldCmd string |
| 347 | if flags.clang { |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 348 | ldCmd = "${config.ClangBin}/clang++" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 349 | } else { |
| 350 | ldCmd = gccCmd(flags.toolchain, "g++") |
| 351 | } |
| 352 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 353 | var libFlagsList []string |
| 354 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 355 | if len(flags.libFlags) > 0 { |
| 356 | libFlagsList = append(libFlagsList, flags.libFlags) |
| 357 | } |
| 358 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 359 | if len(wholeStaticLibs) > 0 { |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 360 | if ctx.Host() && ctx.Darwin() { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 361 | libFlagsList = append(libFlagsList, android.JoinWithPrefix(wholeStaticLibs.Strings(), "-force_load ")) |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 362 | } else { |
| 363 | libFlagsList = append(libFlagsList, "-Wl,--whole-archive ") |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 364 | libFlagsList = append(libFlagsList, wholeStaticLibs.Strings()...) |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 365 | libFlagsList = append(libFlagsList, "-Wl,--no-whole-archive ") |
| 366 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 367 | } |
| 368 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 369 | libFlagsList = append(libFlagsList, staticLibs.Strings()...) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 370 | |
Stephen Hines | 1034786 | 2016-07-18 15:54:54 -0700 | [diff] [blame] | 371 | if groupLate && !ctx.Darwin() && len(lateStaticLibs) > 0 { |
Dan Willemsen | edc385f | 2015-07-08 13:02:23 -0700 | [diff] [blame] | 372 | libFlagsList = append(libFlagsList, "-Wl,--start-group") |
| 373 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 374 | libFlagsList = append(libFlagsList, lateStaticLibs.Strings()...) |
Stephen Hines | 1034786 | 2016-07-18 15:54:54 -0700 | [diff] [blame] | 375 | if groupLate && !ctx.Darwin() && len(lateStaticLibs) > 0 { |
Dan Willemsen | edc385f | 2015-07-08 13:02:23 -0700 | [diff] [blame] | 376 | libFlagsList = append(libFlagsList, "-Wl,--end-group") |
| 377 | } |
| 378 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 379 | for _, lib := range sharedLibs { |
Dan Albert | 9840e1b | 2016-07-21 08:47:33 -0700 | [diff] [blame] | 380 | libFlagsList = append(libFlagsList, lib.String()) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 381 | } |
| 382 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 383 | deps = append(deps, sharedLibs...) |
| 384 | deps = append(deps, staticLibs...) |
Colin Cross | 3075ad0 | 2015-03-17 10:47:08 -0700 | [diff] [blame] | 385 | deps = append(deps, lateStaticLibs...) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 386 | deps = append(deps, wholeStaticLibs...) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 387 | if crtBegin.Valid() { |
| 388 | deps = append(deps, crtBegin.Path(), crtEnd.Path()) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 389 | } |
| 390 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 391 | ctx.ModuleBuild(pctx, android.ModuleBuildParams{ |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 392 | Rule: ld, |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 393 | Output: outputFile, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 394 | Inputs: objFiles, |
| 395 | Implicits: deps, |
| 396 | Args: map[string]string{ |
Dan Albert | ce2b839 | 2016-07-21 13:16:49 -0700 | [diff] [blame] | 397 | "ldCmd": ldCmd, |
| 398 | "crtBegin": crtBegin.String(), |
| 399 | "libFlags": strings.Join(libFlagsList, " "), |
| 400 | "ldFlags": flags.ldFlags, |
| 401 | "crtEnd": crtEnd.String(), |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 402 | }, |
| 403 | }) |
| 404 | } |
| 405 | |
| 406 | // Generate a rule for compiling multiple .o files to a .o using ld partial linking |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 407 | func TransformObjsToObj(ctx android.ModuleContext, objFiles android.Paths, |
| 408 | flags builderFlags, outputFile android.WritablePath) { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 409 | |
Colin Cross | 41280a4 | 2015-11-23 14:01:42 -0800 | [diff] [blame] | 410 | var ldCmd string |
| 411 | if flags.clang { |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 412 | ldCmd = "${config.ClangBin}/clang++" |
Colin Cross | 41280a4 | 2015-11-23 14:01:42 -0800 | [diff] [blame] | 413 | } else { |
| 414 | ldCmd = gccCmd(flags.toolchain, "g++") |
| 415 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 416 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 417 | ctx.ModuleBuild(pctx, android.ModuleBuildParams{ |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 418 | Rule: partialLd, |
| 419 | Output: outputFile, |
| 420 | Inputs: objFiles, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 421 | Args: map[string]string{ |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 422 | "ldCmd": ldCmd, |
Colin Cross | 41280a4 | 2015-11-23 14:01:42 -0800 | [diff] [blame] | 423 | "ldFlags": flags.ldFlags, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 424 | }, |
| 425 | }) |
| 426 | } |
| 427 | |
Colin Cross | bfae885 | 2015-03-26 14:44:11 -0700 | [diff] [blame] | 428 | // Generate a rule for runing objcopy --prefix-symbols on a binary |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 429 | func TransformBinaryPrefixSymbols(ctx android.ModuleContext, prefix string, inputFile android.Path, |
| 430 | flags builderFlags, outputFile android.WritablePath) { |
Colin Cross | bfae885 | 2015-03-26 14:44:11 -0700 | [diff] [blame] | 431 | |
| 432 | objcopyCmd := gccCmd(flags.toolchain, "objcopy") |
| 433 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 434 | ctx.ModuleBuild(pctx, android.ModuleBuildParams{ |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 435 | Rule: prefixSymbols, |
| 436 | Output: outputFile, |
| 437 | Input: inputFile, |
Colin Cross | bfae885 | 2015-03-26 14:44:11 -0700 | [diff] [blame] | 438 | Args: map[string]string{ |
| 439 | "objcopyCmd": objcopyCmd, |
| 440 | "prefix": prefix, |
| 441 | }, |
| 442 | }) |
| 443 | } |
| 444 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 445 | func TransformStrip(ctx android.ModuleContext, inputFile android.Path, |
| 446 | outputFile android.WritablePath, flags builderFlags) { |
Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 447 | |
| 448 | crossCompile := gccCmd(flags.toolchain, "") |
| 449 | args := "" |
| 450 | if flags.stripAddGnuDebuglink { |
| 451 | args += " --add-gnu-debuglink" |
| 452 | } |
| 453 | if flags.stripKeepMiniDebugInfo { |
| 454 | args += " --keep-mini-debug-info" |
| 455 | } |
| 456 | if flags.stripKeepSymbols { |
| 457 | args += " --keep-symbols" |
| 458 | } |
| 459 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 460 | ctx.ModuleBuild(pctx, android.ModuleBuildParams{ |
Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 461 | Rule: strip, |
| 462 | Output: outputFile, |
| 463 | Input: inputFile, |
| 464 | Args: map[string]string{ |
| 465 | "crossCompile": crossCompile, |
| 466 | "args": args, |
| 467 | }, |
| 468 | }) |
| 469 | } |
| 470 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 471 | func TransformDarwinStrip(ctx android.ModuleContext, inputFile android.Path, |
| 472 | outputFile android.WritablePath) { |
Colin Cross | b8ecdfe | 2016-05-03 15:10:29 -0700 | [diff] [blame] | 473 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 474 | ctx.ModuleBuild(pctx, android.ModuleBuildParams{ |
Colin Cross | b8ecdfe | 2016-05-03 15:10:29 -0700 | [diff] [blame] | 475 | Rule: darwinStrip, |
| 476 | Output: outputFile, |
| 477 | Input: inputFile, |
| 478 | }) |
| 479 | } |
| 480 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 481 | func CopyGccLib(ctx android.ModuleContext, libName string, |
| 482 | flags builderFlags, outputFile android.WritablePath) { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 483 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 484 | ctx.ModuleBuild(pctx, android.ModuleBuildParams{ |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 485 | Rule: copyGccLib, |
| 486 | Output: outputFile, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 487 | Args: map[string]string{ |
| 488 | "ccCmd": gccCmd(flags.toolchain, "gcc"), |
| 489 | "cFlags": flags.globalFlags, |
| 490 | "libName": libName, |
| 491 | }, |
| 492 | }) |
| 493 | } |
| 494 | |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 495 | func gccCmd(toolchain config.Toolchain, cmd string) string { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 496 | return filepath.Join(toolchain.GccRoot(), "bin", toolchain.GccTriple()+"-"+cmd) |
| 497 | } |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 498 | |
| 499 | func splitListForSize(list []string, limit int) (lists [][]string, err error) { |
| 500 | var i int |
| 501 | |
| 502 | start := 0 |
| 503 | bytes := 0 |
| 504 | for i = range list { |
| 505 | l := len(list[i]) |
| 506 | if l > limit { |
| 507 | return nil, fmt.Errorf("list element greater than size limit (%d)", limit) |
| 508 | } |
| 509 | if bytes+l > limit { |
| 510 | lists = append(lists, list[start:i]) |
| 511 | start = i |
| 512 | bytes = 0 |
| 513 | } |
| 514 | bytes += l + 1 // count a space between each list element |
| 515 | } |
| 516 | |
| 517 | lists = append(lists, list[start:]) |
| 518 | |
| 519 | totalLen := 0 |
| 520 | for _, l := range lists { |
| 521 | totalLen += len(l) |
| 522 | } |
| 523 | if totalLen != len(list) { |
| 524 | panic(fmt.Errorf("Failed breaking up list, %d != %d", len(list), totalLen)) |
| 525 | } |
| 526 | return lists, nil |
| 527 | } |