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 | }, |
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 | |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 51 | ld = pctx.AndroidStaticRule("ld", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 52 | blueprint.RuleParams{ |
Dan Albert | ce2b839 | 2016-07-21 13:16:49 -0700 | [diff] [blame] | 53 | Command: "$ldCmd ${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 | Rspfile: "${out}.rsp", |
| 57 | RspfileContent: "${in}", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 58 | }, |
Dan Albert | ce2b839 | 2016-07-21 13:16:49 -0700 | [diff] [blame] | 59 | "ldCmd", "crtBegin", "libFlags", "crtEnd", "ldFlags") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 60 | |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 61 | partialLd = pctx.AndroidStaticRule("partialLd", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 62 | blueprint.RuleParams{ |
Colin Cross | 41280a4 | 2015-11-23 14:01:42 -0800 | [diff] [blame] | 63 | Command: "$ldCmd -nostdlib -Wl,-r ${in} -o ${out} ${ldFlags}", |
Dan Willemsen | c94a768 | 2015-11-17 15:27:28 -0800 | [diff] [blame] | 64 | CommandDeps: []string{"$ldCmd"}, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 65 | }, |
Colin Cross | 41280a4 | 2015-11-23 14:01:42 -0800 | [diff] [blame] | 66 | "ldCmd", "ldFlags") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 67 | |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 68 | ar = pctx.AndroidStaticRule("ar", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 69 | blueprint.RuleParams{ |
Colin Cross | 7d21c44 | 2015-03-30 17:47:53 -0700 | [diff] [blame] | 70 | Command: "rm -f ${out} && $arCmd $arFlags $out @${out}.rsp", |
Dan Willemsen | c94a768 | 2015-11-17 15:27:28 -0800 | [diff] [blame] | 71 | CommandDeps: []string{"$arCmd"}, |
Colin Cross | 7d21c44 | 2015-03-30 17:47:53 -0700 | [diff] [blame] | 72 | Rspfile: "${out}.rsp", |
| 73 | RspfileContent: "${in}", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 74 | }, |
| 75 | "arCmd", "arFlags") |
| 76 | |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 77 | darwinAr = pctx.AndroidStaticRule("darwinAr", |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 78 | blueprint.RuleParams{ |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 79 | Command: "rm -f ${out} && ${config.MacArPath} $arFlags $out $in", |
| 80 | CommandDeps: []string{"${config.MacArPath}"}, |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 81 | }, |
Colin Cross | b8ecdfe | 2016-05-03 15:10:29 -0700 | [diff] [blame] | 82 | "arFlags") |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 83 | |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 84 | darwinAppendAr = pctx.AndroidStaticRule("darwinAppendAr", |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 85 | blueprint.RuleParams{ |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 86 | Command: "cp -f ${inAr} ${out}.tmp && ${config.MacArPath} $arFlags ${out}.tmp $in && mv ${out}.tmp ${out}", |
| 87 | CommandDeps: []string{"${config.MacArPath}", "${inAr}"}, |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 88 | }, |
Colin Cross | b8ecdfe | 2016-05-03 15:10:29 -0700 | [diff] [blame] | 89 | "arFlags", "inAr") |
| 90 | |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 91 | darwinStrip = pctx.AndroidStaticRule("darwinStrip", |
Colin Cross | b8ecdfe | 2016-05-03 15:10:29 -0700 | [diff] [blame] | 92 | blueprint.RuleParams{ |
Colin Cross | a24166b | 2016-08-01 15:42:38 -0700 | [diff] [blame] | 93 | Command: "${config.MacStripPath} -u -r -o $out $in", |
| 94 | CommandDeps: []string{"${config.MacStripPath}"}, |
Colin Cross | b8ecdfe | 2016-05-03 15:10:29 -0700 | [diff] [blame] | 95 | }) |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 96 | |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 97 | prefixSymbols = pctx.AndroidStaticRule("prefixSymbols", |
Colin Cross | bfae885 | 2015-03-26 14:44:11 -0700 | [diff] [blame] | 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 | }, |
| 102 | "objcopyCmd", "prefix") |
| 103 | |
Nan Zhang | 43a485c | 2017-03-27 14:27:58 -0700 | [diff] [blame] | 104 | _ = pctx.SourcePathVariable("stripPath", "build/soong/scripts/strip.sh") |
Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 105 | |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 106 | strip = pctx.AndroidStaticRule("strip", |
Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 107 | blueprint.RuleParams{ |
| 108 | Depfile: "${out}.d", |
| 109 | Deps: blueprint.DepsGCC, |
| 110 | Command: "CROSS_COMPILE=$crossCompile $stripPath ${args} -i ${in} -o ${out} -d ${out}.d", |
| 111 | CommandDeps: []string{"$stripPath"}, |
Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 112 | }, |
| 113 | "args", "crossCompile") |
| 114 | |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 115 | emptyFile = pctx.AndroidStaticRule("emptyFile", |
Dan Willemsen | 9f0b550 | 2016-05-13 14:05:09 -0700 | [diff] [blame] | 116 | blueprint.RuleParams{ |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame^] | 117 | Command: "rm -f $out && touch $out", |
Dan Willemsen | 9f0b550 | 2016-05-13 14:05:09 -0700 | [diff] [blame] | 118 | }) |
| 119 | |
Nan Zhang | 43a485c | 2017-03-27 14:27:58 -0700 | [diff] [blame] | 120 | _ = pctx.SourcePathVariable("copyGccLibPath", "build/soong/scripts/copygcclib.sh") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 121 | |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 122 | copyGccLib = pctx.AndroidStaticRule("copyGccLib", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 123 | blueprint.RuleParams{ |
| 124 | Depfile: "${out}.d", |
| 125 | Deps: blueprint.DepsGCC, |
| 126 | Command: "$copyGccLibPath $out $ccCmd $cFlags -print-file-name=${libName}", |
Dan Willemsen | c94a768 | 2015-11-17 15:27:28 -0800 | [diff] [blame] | 127 | CommandDeps: []string{"$copyGccLibPath", "$ccCmd"}, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 128 | }, |
| 129 | "ccCmd", "cFlags", "libName") |
Colin Cross | 26c34ed | 2016-09-30 17:10:16 -0700 | [diff] [blame] | 130 | |
Nan Zhang | 43a485c | 2017-03-27 14:27:58 -0700 | [diff] [blame] | 131 | _ = pctx.SourcePathVariable("tocPath", "build/soong/scripts/toc.sh") |
Colin Cross | 26c34ed | 2016-09-30 17:10:16 -0700 | [diff] [blame] | 132 | |
| 133 | toc = pctx.AndroidStaticRule("toc", |
| 134 | blueprint.RuleParams{ |
| 135 | Depfile: "${out}.d", |
| 136 | Deps: blueprint.DepsGCC, |
| 137 | Command: "CROSS_COMPILE=$crossCompile $tocPath -i ${in} -o ${out} -d ${out}.d", |
| 138 | CommandDeps: []string{"$tocPath"}, |
| 139 | Restat: true, |
| 140 | }, |
| 141 | "crossCompile") |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 142 | |
| 143 | clangTidy = pctx.AndroidStaticRule("clangTidy", |
| 144 | blueprint.RuleParams{ |
| 145 | Command: "rm -f $out && ${config.ClangBin}/clang-tidy $tidyFlags $in -- $cFlags && touch $out", |
| 146 | CommandDeps: []string{"${config.ClangBin}/clang-tidy"}, |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 147 | }, |
| 148 | "cFlags", "tidyFlags") |
Colin Cross | 91e9004 | 2016-12-02 17:13:24 -0800 | [diff] [blame] | 149 | |
Nan Zhang | 43a485c | 2017-03-27 14:27:58 -0700 | [diff] [blame] | 150 | _ = pctx.SourcePathVariable("yasmCmd", "prebuilts/misc/${config.HostPrebuiltTag}/yasm/yasm") |
Colin Cross | 91e9004 | 2016-12-02 17:13:24 -0800 | [diff] [blame] | 151 | |
| 152 | yasm = pctx.AndroidStaticRule("yasm", |
| 153 | blueprint.RuleParams{ |
| 154 | Command: "$yasmCmd $asFlags -o $out $in", |
| 155 | CommandDeps: []string{"$yasmCmd"}, |
Colin Cross | 91e9004 | 2016-12-02 17:13:24 -0800 | [diff] [blame] | 156 | }, |
| 157 | "asFlags") |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 158 | |
| 159 | _ = pctx.SourcePathVariable("sAbiDumper", "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/header-abi-dumper") |
| 160 | |
| 161 | sAbiDump = pctx.AndroidStaticRule("sAbiDump", |
| 162 | blueprint.RuleParams{ |
Jayant Chowdhary | 918b1d9 | 2017-04-18 10:44:00 -0700 | [diff] [blame] | 163 | Command: "rm -f $out && $sAbiDumper -o ${out} $in $exportDirs -- $cFlags -Wno-packed -Qunused-arguments -isystem ${config.RSIncludePath}", |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 164 | CommandDeps: []string{"$sAbiDumper"}, |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 165 | }, |
| 166 | "cFlags", "exportDirs") |
| 167 | |
| 168 | _ = pctx.SourcePathVariable("sAbiLinker", "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/header-abi-linker") |
| 169 | |
| 170 | sAbiLink = pctx.AndroidStaticRule("sAbiLink", |
| 171 | blueprint.RuleParams{ |
| 172 | Command: "$sAbiLinker -o ${out} $symbolFile -arch $arch -api $api $exportedHeaderFlags @${out}.rsp ", |
| 173 | CommandDeps: []string{"$sAbiLinker"}, |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 174 | Rspfile: "${out}.rsp", |
| 175 | RspfileContent: "${in}", |
| 176 | }, |
| 177 | "symbolFile", "arch", "api", "exportedHeaderFlags") |
| 178 | |
| 179 | _ = pctx.SourcePathVariable("sAbiDiffer", "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/header-abi-diff") |
Jayant Chowdhary | 918b1d9 | 2017-04-18 10:44:00 -0700 | [diff] [blame] | 180 | // Abidiff check turned on in advice-only mode. Builds will not fail on abi incompatibilties / extensions. |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 181 | sAbiDiff = pctx.AndroidStaticRule("sAbiDiff", |
| 182 | blueprint.RuleParams{ |
Jayant Chowdhary | 918b1d9 | 2017-04-18 10:44:00 -0700 | [diff] [blame] | 183 | Command: "$sAbiDiffer -advice-only -o ${out} -new $in -old $referenceDump", |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 184 | CommandDeps: []string{"$sAbiDiffer"}, |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 185 | }, |
| 186 | "referenceDump") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 187 | ) |
| 188 | |
Dan Willemsen | 322a0a6 | 2015-11-17 15:19:46 -0800 | [diff] [blame] | 189 | func init() { |
| 190 | // We run gcc/clang with PWD=/proc/self/cwd to remove $TOP from the |
| 191 | // debug output. That way two builds in two different directories will |
| 192 | // create the same output. |
| 193 | if runtime.GOOS != "darwin" { |
| 194 | pctx.StaticVariable("relPwd", "PWD=/proc/self/cwd") |
| 195 | } else { |
| 196 | // Darwin doesn't have /proc |
| 197 | pctx.StaticVariable("relPwd", "") |
| 198 | } |
| 199 | } |
| 200 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 201 | type builderFlags struct { |
| 202 | globalFlags string |
Vishwath Mohan | 83d9f71 | 2017-03-16 11:01:23 -0700 | [diff] [blame] | 203 | arFlags string |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 204 | asFlags string |
| 205 | cFlags string |
| 206 | conlyFlags string |
| 207 | cppFlags string |
| 208 | ldFlags string |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 209 | libFlags string |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 210 | yaccFlags string |
Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 211 | protoFlags string |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 212 | tidyFlags string |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 213 | sAbiFlags string |
Colin Cross | 91e9004 | 2016-12-02 17:13:24 -0800 | [diff] [blame] | 214 | yasmFlags string |
Dan Willemsen | e1240db | 2016-11-03 14:28:51 -0700 | [diff] [blame] | 215 | aidlFlags string |
Colin Cross | 2a252be | 2017-05-01 17:37:24 -0700 | [diff] [blame] | 216 | rsFlags string |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 217 | toolchain config.Toolchain |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 218 | clang bool |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 219 | tidy bool |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 220 | coverage bool |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 221 | sAbiDump bool |
Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 222 | |
Colin Cross | c319948 | 2017-03-30 15:03:04 -0700 | [diff] [blame] | 223 | systemIncludeFlags string |
| 224 | |
Colin Cross | 18c0c5a | 2016-12-01 14:45:23 -0800 | [diff] [blame] | 225 | groupStaticLibs bool |
| 226 | |
Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 227 | stripKeepSymbols bool |
| 228 | stripKeepMiniDebugInfo bool |
| 229 | stripAddGnuDebuglink bool |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 230 | } |
| 231 | |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 232 | type Objects struct { |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 233 | objFiles android.Paths |
| 234 | tidyFiles android.Paths |
| 235 | coverageFiles android.Paths |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 236 | sAbiDumpFiles android.Paths |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | func (a Objects) Copy() Objects { |
| 240 | return Objects{ |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 241 | objFiles: append(android.Paths{}, a.objFiles...), |
| 242 | tidyFiles: append(android.Paths{}, a.tidyFiles...), |
| 243 | coverageFiles: append(android.Paths{}, a.coverageFiles...), |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 244 | sAbiDumpFiles: append(android.Paths{}, a.sAbiDumpFiles...), |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
| 248 | func (a Objects) Append(b Objects) Objects { |
| 249 | return Objects{ |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 250 | objFiles: append(a.objFiles, b.objFiles...), |
| 251 | tidyFiles: append(a.tidyFiles, b.tidyFiles...), |
| 252 | coverageFiles: append(a.coverageFiles, b.coverageFiles...), |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 253 | sAbiDumpFiles: append(a.sAbiDumpFiles, b.sAbiDumpFiles...), |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 257 | // 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] | 258 | func TransformSourceToObj(ctx android.ModuleContext, subdir string, srcFiles android.Paths, |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 259 | flags builderFlags, deps android.Paths) Objects { |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 260 | |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 261 | objFiles := make(android.Paths, len(srcFiles)) |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 262 | var tidyFiles android.Paths |
| 263 | if flags.tidy && flags.clang { |
| 264 | tidyFiles = make(android.Paths, 0, len(srcFiles)) |
| 265 | } |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 266 | var coverageFiles android.Paths |
| 267 | if flags.coverage { |
| 268 | coverageFiles = make(android.Paths, 0, len(srcFiles)) |
| 269 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 270 | |
Colin Cross | c319948 | 2017-03-30 15:03:04 -0700 | [diff] [blame] | 271 | cflags := strings.Join([]string{ |
| 272 | flags.globalFlags, |
| 273 | flags.systemIncludeFlags, |
| 274 | flags.cFlags, |
| 275 | flags.conlyFlags, |
| 276 | }, " ") |
| 277 | |
| 278 | cppflags := strings.Join([]string{ |
| 279 | flags.globalFlags, |
| 280 | flags.systemIncludeFlags, |
| 281 | flags.cFlags, |
| 282 | flags.cppFlags, |
| 283 | }, " ") |
| 284 | |
| 285 | asflags := strings.Join([]string{ |
| 286 | flags.globalFlags, |
| 287 | flags.systemIncludeFlags, |
| 288 | flags.asFlags, |
| 289 | }, " ") |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 290 | var sAbiDumpFiles android.Paths |
| 291 | if flags.sAbiDump && flags.clang { |
| 292 | sAbiDumpFiles = make(android.Paths, 0, len(srcFiles)) |
| 293 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 294 | |
Dan Willemsen | be03f34 | 2016-03-03 17:21:04 -0800 | [diff] [blame] | 295 | if flags.clang { |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 296 | cflags += " ${config.NoOverrideClangGlobalCflags}" |
| 297 | cppflags += " ${config.NoOverrideClangGlobalCflags}" |
Dan Willemsen | be03f34 | 2016-03-03 17:21:04 -0800 | [diff] [blame] | 298 | } else { |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 299 | cflags += " ${config.NoOverrideGlobalCflags}" |
| 300 | cppflags += " ${config.NoOverrideGlobalCflags}" |
Dan Willemsen | be03f34 | 2016-03-03 17:21:04 -0800 | [diff] [blame] | 301 | } |
| 302 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 303 | for i, srcFile := range srcFiles { |
Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 304 | objFile := android.ObjPathWithExt(ctx, subdir, srcFile, "o") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 305 | |
| 306 | objFiles[i] = objFile |
| 307 | |
Colin Cross | 91e9004 | 2016-12-02 17:13:24 -0800 | [diff] [blame] | 308 | if srcFile.Ext() == ".asm" { |
| 309 | ctx.ModuleBuild(pctx, android.ModuleBuildParams{ |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame^] | 310 | Rule: yasm, |
| 311 | Description: "yasm " + srcFile.Rel(), |
| 312 | Output: objFile, |
| 313 | Input: srcFile, |
| 314 | OrderOnly: deps, |
Colin Cross | 91e9004 | 2016-12-02 17:13:24 -0800 | [diff] [blame] | 315 | Args: map[string]string{ |
| 316 | "asFlags": flags.yasmFlags, |
| 317 | }, |
| 318 | }) |
| 319 | continue |
| 320 | } |
| 321 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 322 | var moduleCflags string |
| 323 | var ccCmd string |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 324 | tidy := flags.tidy && flags.clang |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 325 | coverage := flags.coverage |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 326 | dump := flags.sAbiDump && flags.clang |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 327 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 328 | switch srcFile.Ext() { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 329 | case ".S", ".s": |
| 330 | ccCmd = "gcc" |
| 331 | moduleCflags = asflags |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 332 | tidy = false |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 333 | coverage = false |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 334 | dump = false |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 335 | case ".c": |
| 336 | ccCmd = "gcc" |
| 337 | moduleCflags = cflags |
Colin Cross | 9978ffe | 2016-12-01 15:31:22 -0800 | [diff] [blame] | 338 | case ".cpp", ".cc", ".mm": |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 339 | ccCmd = "g++" |
| 340 | moduleCflags = cppflags |
| 341 | default: |
| 342 | ctx.ModuleErrorf("File %s has unknown extension", srcFile) |
| 343 | continue |
| 344 | } |
| 345 | |
| 346 | if flags.clang { |
| 347 | switch ccCmd { |
| 348 | case "gcc": |
| 349 | ccCmd = "clang" |
| 350 | case "g++": |
| 351 | ccCmd = "clang++" |
| 352 | default: |
| 353 | panic("unrecoginzied ccCmd") |
| 354 | } |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame^] | 355 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 356 | |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame^] | 357 | ccDesc := ccCmd |
| 358 | |
| 359 | if flags.clang { |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 360 | ccCmd = "${config.ClangBin}/" + ccCmd |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 361 | } else { |
| 362 | ccCmd = gccCmd(flags.toolchain, ccCmd) |
| 363 | } |
| 364 | |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 365 | var implicitOutputs android.WritablePaths |
| 366 | if coverage { |
| 367 | gcnoFile := android.ObjPathWithExt(ctx, subdir, srcFile, "gcno") |
| 368 | implicitOutputs = append(implicitOutputs, gcnoFile) |
| 369 | coverageFiles = append(coverageFiles, gcnoFile) |
| 370 | } |
| 371 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 372 | ctx.ModuleBuild(pctx, android.ModuleBuildParams{ |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 373 | Rule: cc, |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame^] | 374 | Description: ccDesc + " " + srcFile.Rel(), |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 375 | Output: objFile, |
| 376 | ImplicitOutputs: implicitOutputs, |
| 377 | Input: srcFile, |
| 378 | OrderOnly: deps, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 379 | Args: map[string]string{ |
Colin Cross | 2834452 | 2015-04-22 13:07:53 -0700 | [diff] [blame] | 380 | "cFlags": moduleCflags, |
| 381 | "ccCmd": ccCmd, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 382 | }, |
| 383 | }) |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 384 | |
| 385 | if tidy { |
Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 386 | tidyFile := android.ObjPathWithExt(ctx, subdir, srcFile, "tidy") |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 387 | tidyFiles = append(tidyFiles, tidyFile) |
| 388 | |
| 389 | ctx.ModuleBuild(pctx, android.ModuleBuildParams{ |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame^] | 390 | Rule: clangTidy, |
| 391 | Description: "clang-tidy " + srcFile.Rel(), |
| 392 | Output: tidyFile, |
| 393 | Input: srcFile, |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 394 | // We must depend on objFile, since clang-tidy doesn't |
| 395 | // support exporting dependencies. |
| 396 | Implicit: objFile, |
| 397 | Args: map[string]string{ |
| 398 | "cFlags": moduleCflags, |
| 399 | "tidyFlags": flags.tidyFlags, |
| 400 | }, |
| 401 | }) |
| 402 | } |
| 403 | |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 404 | if dump { |
| 405 | sAbiDumpFile := android.ObjPathWithExt(ctx, subdir, srcFile, "sdump") |
| 406 | sAbiDumpFiles = append(sAbiDumpFiles, sAbiDumpFile) |
| 407 | |
| 408 | ctx.ModuleBuild(pctx, android.ModuleBuildParams{ |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame^] | 409 | Rule: sAbiDump, |
| 410 | Description: "header-abi-dumper " + srcFile.Rel(), |
| 411 | Output: sAbiDumpFile, |
| 412 | Input: srcFile, |
| 413 | Implicit: objFile, |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 414 | Args: map[string]string{ |
| 415 | "cFlags": moduleCflags, |
| 416 | "exportDirs": flags.sAbiFlags, |
| 417 | }, |
| 418 | }) |
| 419 | } |
| 420 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 421 | } |
| 422 | |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 423 | return Objects{ |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 424 | objFiles: objFiles, |
| 425 | tidyFiles: tidyFiles, |
| 426 | coverageFiles: coverageFiles, |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 427 | sAbiDumpFiles: sAbiDumpFiles, |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 428 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | // 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] | 432 | func TransformObjToStaticLib(ctx android.ModuleContext, objFiles android.Paths, |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 433 | flags builderFlags, outputFile android.ModuleOutPath, deps android.Paths) { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 434 | |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 435 | if ctx.Darwin() { |
| 436 | transformDarwinObjToStaticLib(ctx, objFiles, flags, outputFile, deps) |
| 437 | return |
| 438 | } |
| 439 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 440 | arCmd := gccCmd(flags.toolchain, "ar") |
| 441 | arFlags := "crsPD" |
Vishwath Mohan | 83d9f71 | 2017-03-16 11:01:23 -0700 | [diff] [blame] | 442 | if flags.arFlags != "" { |
| 443 | arFlags += " " + flags.arFlags |
| 444 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 445 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 446 | ctx.ModuleBuild(pctx, android.ModuleBuildParams{ |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame^] | 447 | Rule: ar, |
| 448 | Description: "static link " + outputFile.Base(), |
| 449 | Output: outputFile, |
| 450 | Inputs: objFiles, |
| 451 | Implicits: deps, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 452 | Args: map[string]string{ |
| 453 | "arFlags": arFlags, |
| 454 | "arCmd": arCmd, |
| 455 | }, |
| 456 | }) |
| 457 | } |
| 458 | |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 459 | // Generate a rule for compiling multiple .o files to a static library (.a) on |
| 460 | // darwin. The darwin ar tool doesn't support @file for list files, and has a |
| 461 | // very small command line length limit, so we have to split the ar into multiple |
| 462 | // steps, each appending to the previous one. |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 463 | func transformDarwinObjToStaticLib(ctx android.ModuleContext, objFiles android.Paths, |
Colin Cross | 5b52959 | 2017-05-09 13:34:34 -0700 | [diff] [blame] | 464 | flags builderFlags, outputFile android.ModuleOutPath, deps android.Paths) { |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 465 | |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 466 | arFlags := "cqs" |
| 467 | |
Dan Willemsen | 9f0b550 | 2016-05-13 14:05:09 -0700 | [diff] [blame] | 468 | if len(objFiles) == 0 { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 469 | dummy := android.PathForModuleOut(ctx, "dummy"+objectExtension) |
| 470 | dummyAr := android.PathForModuleOut(ctx, "dummy"+staticLibraryExtension) |
Dan Willemsen | 9f0b550 | 2016-05-13 14:05:09 -0700 | [diff] [blame] | 471 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 472 | ctx.ModuleBuild(pctx, android.ModuleBuildParams{ |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame^] | 473 | Rule: emptyFile, |
| 474 | Description: "empty object file", |
| 475 | Output: dummy, |
| 476 | Implicits: deps, |
Dan Willemsen | 9f0b550 | 2016-05-13 14:05:09 -0700 | [diff] [blame] | 477 | }) |
| 478 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 479 | ctx.ModuleBuild(pctx, android.ModuleBuildParams{ |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame^] | 480 | Rule: darwinAr, |
| 481 | Description: "empty static archive", |
| 482 | Output: dummyAr, |
| 483 | Input: dummy, |
Dan Willemsen | 9f0b550 | 2016-05-13 14:05:09 -0700 | [diff] [blame] | 484 | Args: map[string]string{ |
| 485 | "arFlags": arFlags, |
| 486 | }, |
| 487 | }) |
| 488 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 489 | ctx.ModuleBuild(pctx, android.ModuleBuildParams{ |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame^] | 490 | Rule: darwinAppendAr, |
| 491 | Description: "static link " + outputFile.Base(), |
| 492 | Output: outputFile, |
| 493 | Input: dummy, |
Dan Willemsen | 9f0b550 | 2016-05-13 14:05:09 -0700 | [diff] [blame] | 494 | Args: map[string]string{ |
| 495 | "arFlags": "d", |
| 496 | "inAr": dummyAr.String(), |
| 497 | }, |
| 498 | }) |
| 499 | |
| 500 | return |
| 501 | } |
| 502 | |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 503 | // ARG_MAX on darwin is 262144, use half that to be safe |
Colin Cross | 5b52959 | 2017-05-09 13:34:34 -0700 | [diff] [blame] | 504 | objFilesLists, err := splitListForSize(objFiles, 131072) |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 505 | if err != nil { |
| 506 | ctx.ModuleErrorf("%s", err.Error()) |
| 507 | } |
| 508 | |
Colin Cross | 5b52959 | 2017-05-09 13:34:34 -0700 | [diff] [blame] | 509 | var in, out android.WritablePath |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 510 | for i, l := range objFilesLists { |
| 511 | in = out |
| 512 | out = outputFile |
| 513 | if i != len(objFilesLists)-1 { |
Colin Cross | 5b52959 | 2017-05-09 13:34:34 -0700 | [diff] [blame] | 514 | out = android.PathForModuleOut(ctx, outputFile.Base()+strconv.Itoa(i)) |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 515 | } |
| 516 | |
Colin Cross | 5b52959 | 2017-05-09 13:34:34 -0700 | [diff] [blame] | 517 | build := android.ModuleBuildParams{ |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame^] | 518 | Rule: darwinAr, |
| 519 | Description: "static link " + out.Base(), |
| 520 | Output: out, |
| 521 | Inputs: l, |
| 522 | Implicits: deps, |
Colin Cross | 5b52959 | 2017-05-09 13:34:34 -0700 | [diff] [blame] | 523 | Args: map[string]string{ |
| 524 | "arFlags": arFlags, |
| 525 | }, |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 526 | } |
Colin Cross | 5b52959 | 2017-05-09 13:34:34 -0700 | [diff] [blame] | 527 | if i != 0 { |
| 528 | build.Rule = darwinAppendAr |
| 529 | build.Args["inAr"] = in.String() |
| 530 | } |
| 531 | ctx.ModuleBuild(pctx, build) |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 532 | } |
| 533 | } |
| 534 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 535 | // Generate a rule for compiling multiple .o files, plus static libraries, whole static libraries, |
| 536 | // and shared libraires, to a shared library (.so) or dynamic executable |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 537 | func TransformObjToDynamicBinary(ctx android.ModuleContext, |
| 538 | objFiles, sharedLibs, staticLibs, lateStaticLibs, wholeStaticLibs, deps android.Paths, |
| 539 | crtBegin, crtEnd android.OptionalPath, groupLate bool, flags builderFlags, outputFile android.WritablePath) { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 540 | |
| 541 | var ldCmd string |
| 542 | if flags.clang { |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 543 | ldCmd = "${config.ClangBin}/clang++" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 544 | } else { |
| 545 | ldCmd = gccCmd(flags.toolchain, "g++") |
| 546 | } |
| 547 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 548 | var libFlagsList []string |
| 549 | |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 550 | if len(flags.libFlags) > 0 { |
| 551 | libFlagsList = append(libFlagsList, flags.libFlags) |
| 552 | } |
| 553 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 554 | if len(wholeStaticLibs) > 0 { |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 555 | if ctx.Host() && ctx.Darwin() { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 556 | libFlagsList = append(libFlagsList, android.JoinWithPrefix(wholeStaticLibs.Strings(), "-force_load ")) |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 557 | } else { |
| 558 | libFlagsList = append(libFlagsList, "-Wl,--whole-archive ") |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 559 | libFlagsList = append(libFlagsList, wholeStaticLibs.Strings()...) |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 560 | libFlagsList = append(libFlagsList, "-Wl,--no-whole-archive ") |
| 561 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 562 | } |
| 563 | |
Colin Cross | 7a7cf97 | 2016-12-05 18:47:39 -0800 | [diff] [blame] | 564 | if flags.groupStaticLibs && !ctx.Darwin() && len(staticLibs) > 0 { |
Colin Cross | 18c0c5a | 2016-12-01 14:45:23 -0800 | [diff] [blame] | 565 | libFlagsList = append(libFlagsList, "-Wl,--start-group") |
| 566 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 567 | libFlagsList = append(libFlagsList, staticLibs.Strings()...) |
Colin Cross | 7a7cf97 | 2016-12-05 18:47:39 -0800 | [diff] [blame] | 568 | if flags.groupStaticLibs && !ctx.Darwin() && len(staticLibs) > 0 { |
Colin Cross | 18c0c5a | 2016-12-01 14:45:23 -0800 | [diff] [blame] | 569 | libFlagsList = append(libFlagsList, "-Wl,--end-group") |
| 570 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 571 | |
Stephen Hines | 1034786 | 2016-07-18 15:54:54 -0700 | [diff] [blame] | 572 | if groupLate && !ctx.Darwin() && len(lateStaticLibs) > 0 { |
Dan Willemsen | edc385f | 2015-07-08 13:02:23 -0700 | [diff] [blame] | 573 | libFlagsList = append(libFlagsList, "-Wl,--start-group") |
| 574 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 575 | libFlagsList = append(libFlagsList, lateStaticLibs.Strings()...) |
Stephen Hines | 1034786 | 2016-07-18 15:54:54 -0700 | [diff] [blame] | 576 | if groupLate && !ctx.Darwin() && len(lateStaticLibs) > 0 { |
Dan Willemsen | edc385f | 2015-07-08 13:02:23 -0700 | [diff] [blame] | 577 | libFlagsList = append(libFlagsList, "-Wl,--end-group") |
| 578 | } |
| 579 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 580 | for _, lib := range sharedLibs { |
Dan Albert | 9840e1b | 2016-07-21 08:47:33 -0700 | [diff] [blame] | 581 | libFlagsList = append(libFlagsList, lib.String()) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 582 | } |
| 583 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 584 | deps = append(deps, staticLibs...) |
Colin Cross | 3075ad0 | 2015-03-17 10:47:08 -0700 | [diff] [blame] | 585 | deps = append(deps, lateStaticLibs...) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 586 | deps = append(deps, wholeStaticLibs...) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 587 | if crtBegin.Valid() { |
| 588 | deps = append(deps, crtBegin.Path(), crtEnd.Path()) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 589 | } |
| 590 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 591 | ctx.ModuleBuild(pctx, android.ModuleBuildParams{ |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame^] | 592 | Rule: ld, |
| 593 | Description: "link " + outputFile.Base(), |
| 594 | Output: outputFile, |
| 595 | Inputs: objFiles, |
| 596 | Implicits: deps, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 597 | Args: map[string]string{ |
Dan Albert | ce2b839 | 2016-07-21 13:16:49 -0700 | [diff] [blame] | 598 | "ldCmd": ldCmd, |
| 599 | "crtBegin": crtBegin.String(), |
| 600 | "libFlags": strings.Join(libFlagsList, " "), |
| 601 | "ldFlags": flags.ldFlags, |
| 602 | "crtEnd": crtEnd.String(), |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 603 | }, |
| 604 | }) |
| 605 | } |
| 606 | |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 607 | // Generate a rule to combine .dump sAbi dump files from multiple source files |
| 608 | // into a single .ldump sAbi dump file |
| 609 | func TransformDumpToLinkedDump(ctx android.ModuleContext, sAbiDumps android.Paths, |
| 610 | symbolFile android.OptionalPath, apiLevel, baseName, exportedHeaderFlags string) android.OptionalPath { |
| 611 | outputFile := android.PathForModuleOut(ctx, baseName+".lsdump") |
| 612 | var symbolFileStr string |
| 613 | var linkedDumpDep android.Path |
| 614 | if symbolFile.Valid() { |
| 615 | symbolFileStr = "-v " + symbolFile.Path().String() |
| 616 | linkedDumpDep = symbolFile.Path() |
| 617 | } |
| 618 | ctx.ModuleBuild(pctx, android.ModuleBuildParams{ |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame^] | 619 | Rule: sAbiLink, |
| 620 | Description: "header-abi-linker " + outputFile.Base(), |
| 621 | Output: outputFile, |
| 622 | Inputs: sAbiDumps, |
| 623 | Implicit: linkedDumpDep, |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 624 | Args: map[string]string{ |
| 625 | "symbolFile": symbolFileStr, |
| 626 | "arch": ctx.Arch().ArchType.Name, |
| 627 | "api": apiLevel, |
| 628 | "exportedHeaderFlags": exportedHeaderFlags, |
| 629 | }, |
| 630 | }) |
| 631 | return android.OptionalPathForPath(outputFile) |
| 632 | } |
| 633 | |
| 634 | func SourceAbiDiff(ctx android.ModuleContext, inputDump android.Path, referenceDump android.Path, |
| 635 | baseName string) android.OptionalPath { |
| 636 | outputFile := android.PathForModuleOut(ctx, baseName+".abidiff") |
| 637 | ctx.ModuleBuild(pctx, android.ModuleBuildParams{ |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame^] | 638 | Rule: sAbiDiff, |
| 639 | Description: "header-abi-diff " + outputFile.Base(), |
| 640 | Output: outputFile, |
| 641 | Input: inputDump, |
| 642 | Implicit: referenceDump, |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 643 | Args: map[string]string{ |
| 644 | "referenceDump": referenceDump.String(), |
| 645 | }, |
| 646 | }) |
| 647 | return android.OptionalPathForPath(outputFile) |
| 648 | } |
| 649 | |
Colin Cross | 26c34ed | 2016-09-30 17:10:16 -0700 | [diff] [blame] | 650 | // Generate a rule for extract a table of contents from a shared library (.so) |
| 651 | func TransformSharedObjectToToc(ctx android.ModuleContext, inputFile android.WritablePath, |
| 652 | outputFile android.WritablePath, flags builderFlags) { |
| 653 | |
| 654 | crossCompile := gccCmd(flags.toolchain, "") |
| 655 | |
| 656 | ctx.ModuleBuild(pctx, android.ModuleBuildParams{ |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame^] | 657 | Rule: toc, |
| 658 | Description: "generate toc " + inputFile.Base(), |
| 659 | Output: outputFile, |
| 660 | Input: inputFile, |
Colin Cross | 26c34ed | 2016-09-30 17:10:16 -0700 | [diff] [blame] | 661 | Args: map[string]string{ |
| 662 | "crossCompile": crossCompile, |
| 663 | }, |
| 664 | }) |
| 665 | } |
| 666 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 667 | // 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] | 668 | func TransformObjsToObj(ctx android.ModuleContext, objFiles android.Paths, |
| 669 | flags builderFlags, outputFile android.WritablePath) { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 670 | |
Colin Cross | 41280a4 | 2015-11-23 14:01:42 -0800 | [diff] [blame] | 671 | var ldCmd string |
| 672 | if flags.clang { |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 673 | ldCmd = "${config.ClangBin}/clang++" |
Colin Cross | 41280a4 | 2015-11-23 14:01:42 -0800 | [diff] [blame] | 674 | } else { |
| 675 | ldCmd = gccCmd(flags.toolchain, "g++") |
| 676 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 677 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 678 | ctx.ModuleBuild(pctx, android.ModuleBuildParams{ |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame^] | 679 | Rule: partialLd, |
| 680 | Description: "link " + outputFile.Base(), |
| 681 | Output: outputFile, |
| 682 | Inputs: objFiles, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 683 | Args: map[string]string{ |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 684 | "ldCmd": ldCmd, |
Colin Cross | 41280a4 | 2015-11-23 14:01:42 -0800 | [diff] [blame] | 685 | "ldFlags": flags.ldFlags, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 686 | }, |
| 687 | }) |
| 688 | } |
| 689 | |
Colin Cross | bfae885 | 2015-03-26 14:44:11 -0700 | [diff] [blame] | 690 | // Generate a rule for runing objcopy --prefix-symbols on a binary |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 691 | func TransformBinaryPrefixSymbols(ctx android.ModuleContext, prefix string, inputFile android.Path, |
| 692 | flags builderFlags, outputFile android.WritablePath) { |
Colin Cross | bfae885 | 2015-03-26 14:44:11 -0700 | [diff] [blame] | 693 | |
| 694 | objcopyCmd := gccCmd(flags.toolchain, "objcopy") |
| 695 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 696 | ctx.ModuleBuild(pctx, android.ModuleBuildParams{ |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame^] | 697 | Rule: prefixSymbols, |
| 698 | Description: "prefix symbols " + outputFile.Base(), |
| 699 | Output: outputFile, |
| 700 | Input: inputFile, |
Colin Cross | bfae885 | 2015-03-26 14:44:11 -0700 | [diff] [blame] | 701 | Args: map[string]string{ |
| 702 | "objcopyCmd": objcopyCmd, |
| 703 | "prefix": prefix, |
| 704 | }, |
| 705 | }) |
| 706 | } |
| 707 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 708 | func TransformStrip(ctx android.ModuleContext, inputFile android.Path, |
| 709 | outputFile android.WritablePath, flags builderFlags) { |
Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 710 | |
| 711 | crossCompile := gccCmd(flags.toolchain, "") |
| 712 | args := "" |
| 713 | if flags.stripAddGnuDebuglink { |
| 714 | args += " --add-gnu-debuglink" |
| 715 | } |
| 716 | if flags.stripKeepMiniDebugInfo { |
| 717 | args += " --keep-mini-debug-info" |
| 718 | } |
| 719 | if flags.stripKeepSymbols { |
| 720 | args += " --keep-symbols" |
| 721 | } |
| 722 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 723 | ctx.ModuleBuild(pctx, android.ModuleBuildParams{ |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame^] | 724 | Rule: strip, |
| 725 | Description: "strip " + outputFile.Base(), |
| 726 | Output: outputFile, |
| 727 | Input: inputFile, |
Colin Cross | 665dce9 | 2016-04-28 14:50:03 -0700 | [diff] [blame] | 728 | Args: map[string]string{ |
| 729 | "crossCompile": crossCompile, |
| 730 | "args": args, |
| 731 | }, |
| 732 | }) |
| 733 | } |
| 734 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 735 | func TransformDarwinStrip(ctx android.ModuleContext, inputFile android.Path, |
| 736 | outputFile android.WritablePath) { |
Colin Cross | b8ecdfe | 2016-05-03 15:10:29 -0700 | [diff] [blame] | 737 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 738 | ctx.ModuleBuild(pctx, android.ModuleBuildParams{ |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame^] | 739 | Rule: darwinStrip, |
| 740 | Description: "strip " + outputFile.Base(), |
| 741 | Output: outputFile, |
| 742 | Input: inputFile, |
Colin Cross | b8ecdfe | 2016-05-03 15:10:29 -0700 | [diff] [blame] | 743 | }) |
| 744 | } |
| 745 | |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 746 | func TransformCoverageFilesToLib(ctx android.ModuleContext, |
| 747 | inputs Objects, flags builderFlags, baseName string) android.OptionalPath { |
| 748 | |
| 749 | if len(inputs.coverageFiles) > 0 { |
| 750 | outputFile := android.PathForModuleOut(ctx, baseName+".gcnodir") |
| 751 | |
| 752 | TransformObjToStaticLib(ctx, inputs.coverageFiles, flags, outputFile, nil) |
| 753 | |
| 754 | return android.OptionalPathForPath(outputFile) |
| 755 | } |
| 756 | |
| 757 | return android.OptionalPath{} |
| 758 | } |
| 759 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 760 | func CopyGccLib(ctx android.ModuleContext, libName string, |
| 761 | flags builderFlags, outputFile android.WritablePath) { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 762 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 763 | ctx.ModuleBuild(pctx, android.ModuleBuildParams{ |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame^] | 764 | Rule: copyGccLib, |
| 765 | Description: "copy gcc library " + libName, |
| 766 | Output: outputFile, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 767 | Args: map[string]string{ |
| 768 | "ccCmd": gccCmd(flags.toolchain, "gcc"), |
| 769 | "cFlags": flags.globalFlags, |
| 770 | "libName": libName, |
| 771 | }, |
| 772 | }) |
| 773 | } |
| 774 | |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 775 | func gccCmd(toolchain config.Toolchain, cmd string) string { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 776 | return filepath.Join(toolchain.GccRoot(), "bin", toolchain.GccTriple()+"-"+cmd) |
| 777 | } |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 778 | |
Colin Cross | 5b52959 | 2017-05-09 13:34:34 -0700 | [diff] [blame] | 779 | func splitListForSize(list android.Paths, limit int) (lists []android.Paths, err error) { |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 780 | var i int |
| 781 | |
| 782 | start := 0 |
| 783 | bytes := 0 |
| 784 | for i = range list { |
Colin Cross | 5b52959 | 2017-05-09 13:34:34 -0700 | [diff] [blame] | 785 | l := len(list[i].String()) |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 786 | if l > limit { |
| 787 | return nil, fmt.Errorf("list element greater than size limit (%d)", limit) |
| 788 | } |
| 789 | if bytes+l > limit { |
| 790 | lists = append(lists, list[start:i]) |
| 791 | start = i |
| 792 | bytes = 0 |
| 793 | } |
| 794 | bytes += l + 1 // count a space between each list element |
| 795 | } |
| 796 | |
| 797 | lists = append(lists, list[start:]) |
| 798 | |
| 799 | totalLen := 0 |
| 800 | for _, l := range lists { |
| 801 | totalLen += len(l) |
| 802 | } |
| 803 | if totalLen != len(list) { |
| 804 | panic(fmt.Errorf("Failed breaking up list, %d != %d", len(list), totalLen)) |
| 805 | } |
| 806 | return lists, nil |
| 807 | } |