blob: 41cc0c78eb42046f4819d82dac46b17fb8efa3cb [file] [log] [blame]
Colin Cross3f40fa42015-01-30 17:27:36 -08001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package cc
16
17// This file generates the final rules for compiling all C/C++. All properties related to
18// compiling should have been translated into builderFlags or another argument to the Transform*
19// functions.
20
21import (
Colin Cross0af4b842015-04-30 16:36:18 -070022 "fmt"
Colin Crossb98c8b02016-07-29 13:44:28 -070023 "path/filepath"
Colin Cross0af4b842015-04-30 16:36:18 -070024 "runtime"
Colin Cross3f40fa42015-01-30 17:27:36 -080025 "strings"
Colin Crossed4cf0b2015-03-26 14:43:45 -070026
27 "github.com/google/blueprint"
Josh Gao75a50a22019-06-07 17:58:59 -070028 "github.com/google/blueprint/pathtools"
Colin Crossb98c8b02016-07-29 13:44:28 -070029
30 "android/soong/android"
31 "android/soong/cc/config"
Ramy Medhat9a90fe52020-04-13 13:21:23 -040032 "android/soong/remoteexec"
Colin Cross3f40fa42015-01-30 17:27:36 -080033)
34
35const (
Dan Albertc3144b12015-04-28 18:17:56 -070036 objectExtension = ".o"
Colin Cross3f40fa42015-01-30 17:27:36 -080037 staticLibraryExtension = ".a"
38)
39
40var (
Colin Cross635c3b02016-05-18 15:37:25 -070041 pctx = android.NewPackageContext("android/soong/cc")
Colin Cross3f40fa42015-01-30 17:27:36 -080042
Ramy Medhat8ea054a2020-01-27 14:19:44 -050043 cc = pctx.AndroidRemoteStaticRule("cc", android.RemoteRuleSupports{Goma: true, RBE: true},
Colin Cross3f40fa42015-01-30 17:27:36 -080044 blueprint.RuleParams{
45 Depfile: "${out}.d",
46 Deps: blueprint.DepsGCC,
Alistair Strachan777475c2016-08-26 12:55:49 -070047 Command: "$relPwd ${config.CcWrapper}$ccCmd -c $cFlags -MD -MF ${out}.d -o $out $in",
Dan Willemsenc94a7682015-11-17 15:27:28 -080048 CommandDeps: []string{"$ccCmd"},
Colin Cross3f40fa42015-01-30 17:27:36 -080049 },
Dan Willemsen322a0a62015-11-17 15:19:46 -080050 "ccCmd", "cFlags")
Colin Cross3f40fa42015-01-30 17:27:36 -080051
Kousik Kumar2976bfd2020-02-17 00:26:55 -080052 ccNoDeps = pctx.AndroidStaticRule("ccNoDeps",
Dan Willemsenfcabb1c2019-01-03 23:25:11 -080053 blueprint.RuleParams{
Kousik Kumar2976bfd2020-02-17 00:26:55 -080054 Command: "$relPwd $ccCmd -c $cFlags -o $out $in",
Dan Willemsenfcabb1c2019-01-03 23:25:11 -080055 CommandDeps: []string{"$ccCmd"},
56 },
57 "ccCmd", "cFlags")
58
Ramy Medhat9a90fe52020-04-13 13:21:23 -040059 ld, ldRE = remoteexec.StaticRules(pctx, "ld",
Colin Cross3f40fa42015-01-30 17:27:36 -080060 blueprint.RuleParams{
Ramy Medhat31ec9422020-04-17 15:03:58 -040061 Command: "$reTemplate$ldCmd ${crtBegin} @${out}.rsp " +
Pete Bentley99f2fc22019-08-02 14:02:20 +010062 "${libFlags} ${crtEnd} -o ${out} ${ldFlags} ${extraLibFlags}",
Dan Willemsenc94a7682015-11-17 15:27:28 -080063 CommandDeps: []string{"$ldCmd"},
Colin Cross7d21c442015-03-30 17:47:53 -070064 Rspfile: "${out}.rsp",
65 RspfileContent: "${in}",
Colin Cross36ae1352019-03-29 15:55:30 -070066 // clang -Wl,--out-implib doesn't update its output file if it hasn't changed.
67 Restat: true,
Colin Cross3f40fa42015-01-30 17:27:36 -080068 },
Ramy Medhat31ec9422020-04-17 15:03:58 -040069 &remoteexec.REParams{
70 Labels: map[string]string{"type": "link", "tool": "clang"},
Ramy Medhat9a90fe52020-04-13 13:21:23 -040071 ExecStrategy: "${config.RECXXLinksExecStrategy}",
72 Inputs: []string{"${out}.rsp"},
73 RSPFile: "${out}.rsp",
Kousik Kumar3fb61262020-04-22 13:31:09 -070074 OutputFiles: []string{"${out}", "$implicitOutputs"},
Ramy Medhat9a90fe52020-04-13 13:21:23 -040075 ToolchainInputs: []string{"$ldCmd"},
76 Platform: map[string]string{remoteexec.PoolKey: "${config.RECXXLinksPool}"},
Kousik Kumar3fb61262020-04-22 13:31:09 -070077 }, []string{"ldCmd", "crtBegin", "libFlags", "crtEnd", "ldFlags", "extraLibFlags"}, []string{"implicitOutputs"})
Colin Cross3f40fa42015-01-30 17:27:36 -080078
Ramy Medhat9a90fe52020-04-13 13:21:23 -040079 partialLd, partialLdRE = remoteexec.StaticRules(pctx, "partialLd",
Colin Cross3f40fa42015-01-30 17:27:36 -080080 blueprint.RuleParams{
Chih-Hung Hsieh3ede2942018-01-10 14:30:44 -080081 // Without -no-pie, clang 7.0 adds -pie to link Android files,
82 // but -r and -pie cannot be used together.
Ramy Medhat31ec9422020-04-17 15:03:58 -040083 Command: "$reTemplate$ldCmd -fuse-ld=lld -nostdlib -no-pie -Wl,-r ${in} -o ${out} ${ldFlags}",
Dan Willemsenc94a7682015-11-17 15:27:28 -080084 CommandDeps: []string{"$ldCmd"},
Ramy Medhat9a90fe52020-04-13 13:21:23 -040085 }, &remoteexec.REParams{
Kousik Kumar3fb61262020-04-22 13:31:09 -070086 Labels: map[string]string{"type": "link", "tool": "clang"},
87 ExecStrategy: "${config.RECXXLinksExecStrategy}", Inputs: []string{"$inCommaList"},
88 OutputFiles: []string{"${out}", "$implicitOutputs"},
Ramy Medhat9a90fe52020-04-13 13:21:23 -040089 ToolchainInputs: []string{"$ldCmd"},
90 Platform: map[string]string{remoteexec.PoolKey: "${config.RECXXLinksPool}"},
Kousik Kumar3fb61262020-04-22 13:31:09 -070091 }, []string{"ldCmd", "ldFlags"}, []string{"inCommaList", "implicitOutputs"})
Colin Cross3f40fa42015-01-30 17:27:36 -080092
Colin Cross9d45bb72016-08-29 16:14:13 -070093 ar = pctx.AndroidStaticRule("ar",
Colin Cross3f40fa42015-01-30 17:27:36 -080094 blueprint.RuleParams{
Colin Cross7d21c442015-03-30 17:47:53 -070095 Command: "rm -f ${out} && $arCmd $arFlags $out @${out}.rsp",
Dan Willemsenc94a7682015-11-17 15:27:28 -080096 CommandDeps: []string{"$arCmd"},
Colin Cross7d21c442015-03-30 17:47:53 -070097 Rspfile: "${out}.rsp",
98 RspfileContent: "${in}",
Colin Cross3f40fa42015-01-30 17:27:36 -080099 },
100 "arCmd", "arFlags")
101
Colin Cross9d45bb72016-08-29 16:14:13 -0700102 darwinStrip = pctx.AndroidStaticRule("darwinStrip",
Colin Crossb8ecdfe2016-05-03 15:10:29 -0700103 blueprint.RuleParams{
Colin Crossa24166b2016-08-01 15:42:38 -0700104 Command: "${config.MacStripPath} -u -r -o $out $in",
105 CommandDeps: []string{"${config.MacStripPath}"},
Colin Crossb8ecdfe2016-05-03 15:10:29 -0700106 })
Colin Cross0af4b842015-04-30 16:36:18 -0700107
Colin Cross9d45bb72016-08-29 16:14:13 -0700108 prefixSymbols = pctx.AndroidStaticRule("prefixSymbols",
Colin Crossbfae8852015-03-26 14:44:11 -0700109 blueprint.RuleParams{
110 Command: "$objcopyCmd --prefix-symbols=${prefix} ${in} ${out}",
Dan Willemsenc94a7682015-11-17 15:27:28 -0800111 CommandDeps: []string{"$objcopyCmd"},
Colin Crossbfae8852015-03-26 14:44:11 -0700112 },
113 "objcopyCmd", "prefix")
114
Nan Zhang43a485c2017-03-27 14:27:58 -0700115 _ = pctx.SourcePathVariable("stripPath", "build/soong/scripts/strip.sh")
Dan Willemsen8fec83a2018-03-09 10:47:52 -0800116 _ = pctx.SourcePathVariable("xzCmd", "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/xz")
Colin Cross665dce92016-04-28 14:50:03 -0700117
Colin Crossee3ea312019-05-17 15:36:46 -0700118 // b/132822437: objcopy uses a file descriptor per .o file when called on .a files, which runs the system out of
Colin Crossbadf8d62019-05-22 13:25:50 -0700119 // file descriptors on darwin. Limit concurrent calls to 5 on darwin.
Colin Crossee3ea312019-05-17 15:36:46 -0700120 darwinStripPool = func() blueprint.Pool {
121 if runtime.GOOS == "darwin" {
122 return pctx.StaticPool("darwinStripPool", blueprint.PoolParams{
Colin Crossbadf8d62019-05-22 13:25:50 -0700123 Depth: 5,
Colin Crossee3ea312019-05-17 15:36:46 -0700124 })
125 } else {
126 return nil
127 }
128 }()
129
Colin Cross9d45bb72016-08-29 16:14:13 -0700130 strip = pctx.AndroidStaticRule("strip",
Colin Cross665dce92016-04-28 14:50:03 -0700131 blueprint.RuleParams{
132 Depfile: "${out}.d",
133 Deps: blueprint.DepsGCC,
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -0700134 Command: "CROSS_COMPILE=$crossCompile XZ=$xzCmd CLANG_BIN=${config.ClangBin} $stripPath ${args} -i ${in} -o ${out} -d ${out}.d",
Dan Willemsen8fec83a2018-03-09 10:47:52 -0800135 CommandDeps: []string{"$stripPath", "$xzCmd"},
Colin Crossee3ea312019-05-17 15:36:46 -0700136 Pool: darwinStripPool,
Colin Cross665dce92016-04-28 14:50:03 -0700137 },
138 "args", "crossCompile")
139
Yi Kongc49c3932019-10-15 02:01:19 -0700140 _ = pctx.SourcePathVariable("archiveRepackPath", "build/soong/scripts/archive_repack.sh")
141
142 archiveRepack = pctx.AndroidStaticRule("archiveRepack",
143 blueprint.RuleParams{
144 Depfile: "${out}.d",
145 Deps: blueprint.DepsGCC,
146 Command: "CLANG_BIN=${config.ClangBin} $archiveRepackPath -i ${in} -o ${out} -d ${out}.d ${objects}",
147 CommandDeps: []string{"$archiveRepackPath"},
148 },
149 "objects")
150
Colin Cross9d45bb72016-08-29 16:14:13 -0700151 emptyFile = pctx.AndroidStaticRule("emptyFile",
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700152 blueprint.RuleParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700153 Command: "rm -f $out && touch $out",
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700154 })
155
Nan Zhang43a485c2017-03-27 14:27:58 -0700156 _ = pctx.SourcePathVariable("tocPath", "build/soong/scripts/toc.sh")
Colin Cross26c34ed2016-09-30 17:10:16 -0700157
158 toc = pctx.AndroidStaticRule("toc",
159 blueprint.RuleParams{
160 Depfile: "${out}.d",
161 Deps: blueprint.DepsGCC,
Colin Crossb496cfd2018-09-10 16:50:05 -0700162 Command: "CROSS_COMPILE=$crossCompile $tocPath $format -i ${in} -o ${out} -d ${out}.d",
Colin Cross26c34ed2016-09-30 17:10:16 -0700163 CommandDeps: []string{"$tocPath"},
164 Restat: true,
165 },
Colin Crossb496cfd2018-09-10 16:50:05 -0700166 "crossCompile", "format")
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700167
168 clangTidy = pctx.AndroidStaticRule("clangTidy",
169 blueprint.RuleParams{
George Burgess IVc4624c02019-04-04 16:22:37 -0700170 Command: "rm -f $out && ${config.ClangBin}/clang-tidy $tidyFlags $in -- $cFlags && touch $out",
171 CommandDeps: []string{"${config.ClangBin}/clang-tidy"},
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700172 },
173 "cFlags", "tidyFlags")
Colin Cross91e90042016-12-02 17:13:24 -0800174
Nan Zhang43a485c2017-03-27 14:27:58 -0700175 _ = pctx.SourcePathVariable("yasmCmd", "prebuilts/misc/${config.HostPrebuiltTag}/yasm/yasm")
Colin Cross91e90042016-12-02 17:13:24 -0800176
177 yasm = pctx.AndroidStaticRule("yasm",
178 blueprint.RuleParams{
Dan Willemsen1d3e5452017-08-22 20:53:45 -0700179 Command: "$yasmCmd $asFlags -o $out $in && $yasmCmd $asFlags -M $in >$out.d",
Colin Cross91e90042016-12-02 17:13:24 -0800180 CommandDeps: []string{"$yasmCmd"},
Dan Willemsen1d3e5452017-08-22 20:53:45 -0700181 Depfile: "$out.d",
182 Deps: blueprint.DepsGCC,
Colin Cross91e90042016-12-02 17:13:24 -0800183 },
184 "asFlags")
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800185
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700186 windres = pctx.AndroidStaticRule("windres",
187 blueprint.RuleParams{
Nick Desaulniers18eeffa2020-01-29 16:20:11 -0800188 Command: "$windresCmd $flags -I$$(dirname $in) -i $in -o $out --preprocessor \"${config.ClangBin}/clang -E -xc-header -DRC_INVOKED\"",
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700189 CommandDeps: []string{"$windresCmd"},
190 },
191 "windresCmd", "flags")
192
Jayant Chowdharya4c6df52018-02-20 12:36:51 -0800193 _ = pctx.SourcePathVariable("sAbiDumper", "prebuilts/clang-tools/${config.HostPrebuiltTag}/bin/header-abi-dumper")
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800194
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700195 // -w has been added since header-abi-dumper does not need to produce any sort of diagnostic information.
Ramy Medhat31ec9422020-04-17 15:03:58 -0400196 sAbiDump, sAbiDumpRE = remoteexec.StaticRules(pctx, "sAbiDump",
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800197 blueprint.RuleParams{
Ramy Medhat31ec9422020-04-17 15:03:58 -0400198 Command: "rm -f $out && $reTemplate$sAbiDumper -o ${out} $in $exportDirs -- $cFlags -w -isystem prebuilts/clang-tools/${config.HostPrebuiltTag}/clang-headers",
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800199 CommandDeps: []string{"$sAbiDumper"},
Ramy Medhat31ec9422020-04-17 15:03:58 -0400200 }, &remoteexec.REParams{
201 Labels: map[string]string{"type": "abi-dump", "tool": "header-abi-dumper"},
202 ExecStrategy: "${config.REAbiDumperExecStrategy}",
203 Platform: map[string]string{
204 remoteexec.PoolKey: "${config.RECXXPool}",
205 "InputRootAbsolutePath": android.AbsSrcDirForExistingUseCases(),
206 },
207 }, []string{"cFlags", "exportDirs"}, nil)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800208
Jayant Chowdharya4c6df52018-02-20 12:36:51 -0800209 _ = pctx.SourcePathVariable("sAbiLinker", "prebuilts/clang-tools/${config.HostPrebuiltTag}/bin/header-abi-linker")
Ramy Medhat808594c2020-05-07 06:56:47 -0400210 _ = pctx.SourcePathVariable("sAbiLinkerLibs", "prebuilts/clang-tools/${config.HostPrebuiltTag}/lib64")
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800211
Ramy Medhat808594c2020-05-07 06:56:47 -0400212 sAbiLink, sAbiLinkRE = remoteexec.StaticRules(pctx, "sAbiLink",
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800213 blueprint.RuleParams{
Ramy Medhat808594c2020-05-07 06:56:47 -0400214 Command: "$reTemplate$sAbiLinker -o ${out} $symbolFilter -arch $arch $exportedHeaderFlags @${out}.rsp ",
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800215 CommandDeps: []string{"$sAbiLinker"},
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800216 Rspfile: "${out}.rsp",
217 RspfileContent: "${in}",
Ramy Medhat808594c2020-05-07 06:56:47 -0400218 }, &remoteexec.REParams{
219 Labels: map[string]string{"type": "tool", "name": "abi-linker"},
220 ExecStrategy: "${config.REAbiLinkerExecStrategy}",
221 Inputs: []string{"$sAbiLinkerLibs", "${out}.rsp", "$implicits"},
222 RSPFile: "${out}.rsp",
223 OutputFiles: []string{"$out"},
224 ToolchainInputs: []string{"$sAbiLinker"},
225 Platform: map[string]string{remoteexec.PoolKey: "${config.RECXXPool}"},
226 }, []string{"symbolFilter", "arch", "exportedHeaderFlags"}, []string{"implicits"})
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800227
Jayant Chowdharya4c6df52018-02-20 12:36:51 -0800228 _ = pctx.SourcePathVariable("sAbiDiffer", "prebuilts/clang-tools/${config.HostPrebuiltTag}/bin/header-abi-diff")
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700229
Colin Cross2e2dbc22019-09-25 13:31:46 -0700230 sAbiDiff = pctx.RuleFunc("sAbiDiff",
Dan Willemsen54daaf02018-03-12 13:24:09 -0700231 func(ctx android.PackageRuleContext) blueprint.RuleParams {
Logan Chien2a65dda2019-10-01 15:58:07 -0700232 commandStr := "($sAbiDiffer ${extraFlags} -lib ${libName} -arch ${arch} -o ${out} -new ${in} -old ${referenceDump})"
Logan Chien6227fed2019-02-18 13:12:21 +0800233 commandStr += "|| (echo 'error: Please update ABI references with: $$ANDROID_BUILD_TOP/development/vndk/tools/header-checker/utils/create_reference_dumps.py ${createReferenceDumpFlags} -l ${libName}'"
Logan Chien8f74fe62019-01-28 12:14:54 +0800234 commandStr += " && (mkdir -p $$DIST_DIR/abidiffs && cp ${out} $$DIST_DIR/abidiffs/)"
Jayant Chowdharyd8b70a32018-02-01 17:23:09 -0800235 commandStr += " && exit 1)"
Jayant Chowdhary219139d2017-11-27 14:52:21 -0800236 return blueprint.RuleParams{
237 Command: commandStr,
238 CommandDeps: []string{"$sAbiDiffer"},
Dan Willemsen54daaf02018-03-12 13:24:09 -0700239 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800240 },
Logan Chien2a65dda2019-10-01 15:58:07 -0700241 "extraFlags", "referenceDump", "libName", "arch", "createReferenceDumpFlags")
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700242
243 unzipRefSAbiDump = pctx.AndroidStaticRule("unzipRefSAbiDump",
244 blueprint.RuleParams{
245 Command: "gunzip -c $in > $out",
246 })
Oliver Nguyenc7434142019-04-24 14:22:25 -0700247
248 zip = pctx.AndroidStaticRule("zip",
249 blueprint.RuleParams{
250 Command: "cat $out.rsp | tr ' ' '\\n' | tr -d \\' | sort -u > ${out}.tmp && ${SoongZipCmd} -o ${out} -C $$OUT_DIR -l ${out}.tmp",
251 CommandDeps: []string{"${SoongZipCmd}"},
252 Rspfile: "$out.rsp",
253 RspfileContent: "$in",
254 })
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800255
256 _ = pctx.SourcePathVariable("cxxExtractor",
257 "prebuilts/clang-tools/${config.HostPrebuiltTag}/bin/cxx_extractor")
Sasha Smundak65143642019-09-26 20:14:28 -0700258 _ = pctx.SourcePathVariable("kytheVnames", "build/soong/vnames.json")
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800259 _ = pctx.VariableFunc("kytheCorpus",
260 func(ctx android.PackageVarContext) string { return ctx.Config().XrefCorpusName() })
Sasha Smundak6c2d4f92020-01-09 17:34:23 -0800261 _ = pctx.VariableFunc("kytheCuEncoding",
262 func(ctx android.PackageVarContext) string { return ctx.Config().XrefCuEncoding() })
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800263 kytheExtract = pctx.StaticRule("kythe",
264 blueprint.RuleParams{
Sasha Smundak6c2d4f92020-01-09 17:34:23 -0800265 Command: `rm -f $out && ` +
Sasha Smundaka4ef83b2020-04-21 17:08:35 -0700266 `KYTHE_CORPUS=${kytheCorpus} ` +
267 `KYTHE_OUTPUT_FILE=$out ` +
268 `KYTHE_VNAMES=$kytheVnames ` +
269 `KYTHE_KZIP_ENCODING=${kytheCuEncoding} ` +
270 `KYTHE_CANONICALIZE_VNAME_PATHS=prefer-relative ` +
Sasha Smundak6c2d4f92020-01-09 17:34:23 -0800271 `$cxxExtractor $cFlags $in `,
Sasha Smundak65143642019-09-26 20:14:28 -0700272 CommandDeps: []string{"$cxxExtractor", "$kytheVnames"},
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800273 },
274 "cFlags")
Colin Cross3f40fa42015-01-30 17:27:36 -0800275)
276
Dan Willemsen322a0a62015-11-17 15:19:46 -0800277func init() {
278 // We run gcc/clang with PWD=/proc/self/cwd to remove $TOP from the
279 // debug output. That way two builds in two different directories will
280 // create the same output.
281 if runtime.GOOS != "darwin" {
282 pctx.StaticVariable("relPwd", "PWD=/proc/self/cwd")
283 } else {
284 // Darwin doesn't have /proc
285 pctx.StaticVariable("relPwd", "")
286 }
Oliver Nguyenc7434142019-04-24 14:22:25 -0700287
288 pctx.HostBinToolVariable("SoongZipCmd", "soong_zip")
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400289 pctx.Import("android/soong/remoteexec")
Dan Willemsen322a0a62015-11-17 15:19:46 -0800290}
291
Colin Cross3f40fa42015-01-30 17:27:36 -0800292type builderFlags struct {
Colin Cross6d88dba2019-11-06 07:06:58 -0800293 globalCommonFlags string
294 globalAsFlags string
295 globalYasmFlags string
296 globalCFlags string
297 globalToolingCFlags string // A separate set of cFlags for clang LibTooling tools
298 globalToolingCppFlags string // A separate set of cppFlags for clang LibTooling tools
299 globalConlyFlags string
300 globalCppFlags string
301 globalLdFlags string
302
303 localCommonFlags string
304 localAsFlags string
305 localYasmFlags string
306 localCFlags string
307 localToolingCFlags string // A separate set of cFlags for clang LibTooling tools
308 localToolingCppFlags string // A separate set of cppFlags for clang LibTooling tools
309 localConlyFlags string
310 localCppFlags string
311 localLdFlags string
312
313 libFlags string
314 extraLibFlags string
315 tidyFlags string
316 sAbiFlags string
317 aidlFlags string
318 rsFlags string
319 toolchain config.Toolchain
320 tidy bool
Oliver Nguyen04526782020-04-21 12:40:27 -0700321 gcovCoverage bool
Colin Cross6d88dba2019-11-06 07:06:58 -0800322 sAbiDump bool
323 emitXrefs bool
Colin Cross665dce92016-04-28 14:50:03 -0700324
Dan Willemsen98ab3112019-08-27 21:20:40 -0700325 assemblerWithCpp bool
326
Colin Crossc3199482017-03-30 15:03:04 -0700327 systemIncludeFlags string
328
Colin Cross18c0c5a2016-12-01 14:45:23 -0800329 groupStaticLibs bool
330
Christopher Ferrisb43fe7a2019-05-17 16:39:54 -0700331 stripKeepSymbols bool
332 stripKeepSymbolsList string
333 stripKeepSymbolsAndDebugFrame bool
334 stripKeepMiniDebugInfo bool
335 stripAddGnuDebuglink bool
336 stripUseGnuStrip bool
Dan Willemsen60e62f02018-11-16 21:05:32 -0800337
Colin Cross19878da2019-03-28 14:45:07 -0700338 proto android.ProtoFlags
Dan Willemsen60e62f02018-11-16 21:05:32 -0800339 protoC bool
340 protoOptionsFile bool
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700341
342 yacc *YaccProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800343}
344
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700345type Objects struct {
Dan Willemsen581341d2017-02-09 16:16:31 -0800346 objFiles android.Paths
347 tidyFiles android.Paths
348 coverageFiles android.Paths
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800349 sAbiDumpFiles android.Paths
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800350 kytheFiles android.Paths
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700351}
352
353func (a Objects) Copy() Objects {
354 return Objects{
Dan Willemsen581341d2017-02-09 16:16:31 -0800355 objFiles: append(android.Paths{}, a.objFiles...),
356 tidyFiles: append(android.Paths{}, a.tidyFiles...),
357 coverageFiles: append(android.Paths{}, a.coverageFiles...),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800358 sAbiDumpFiles: append(android.Paths{}, a.sAbiDumpFiles...),
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800359 kytheFiles: append(android.Paths{}, a.kytheFiles...),
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700360 }
361}
362
363func (a Objects) Append(b Objects) Objects {
364 return Objects{
Dan Willemsen581341d2017-02-09 16:16:31 -0800365 objFiles: append(a.objFiles, b.objFiles...),
366 tidyFiles: append(a.tidyFiles, b.tidyFiles...),
367 coverageFiles: append(a.coverageFiles, b.coverageFiles...),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800368 sAbiDumpFiles: append(a.sAbiDumpFiles, b.sAbiDumpFiles...),
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800369 kytheFiles: append(a.kytheFiles, b.kytheFiles...),
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700370 }
371}
372
Colin Cross3f40fa42015-01-30 17:27:36 -0800373// Generate rules for compiling multiple .c, .cpp, or .S files to individual .o files
Colin Cross635c3b02016-05-18 15:37:25 -0700374func TransformSourceToObj(ctx android.ModuleContext, subdir string, srcFiles android.Paths,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800375 flags builderFlags, pathDeps android.Paths, cFlagsDeps android.Paths) Objects {
Colin Cross581c1892015-04-07 16:50:10 -0700376
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700377 objFiles := make(android.Paths, len(srcFiles))
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700378 var tidyFiles android.Paths
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700379 if flags.tidy {
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700380 tidyFiles = make(android.Paths, 0, len(srcFiles))
381 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800382 var coverageFiles android.Paths
Oliver Nguyen04526782020-04-21 12:40:27 -0700383 if flags.gcovCoverage {
Dan Willemsen581341d2017-02-09 16:16:31 -0800384 coverageFiles = make(android.Paths, 0, len(srcFiles))
385 }
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800386 var kytheFiles android.Paths
387 if flags.emitXrefs {
388 kytheFiles = make(android.Paths, 0, len(srcFiles))
389 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800390
Colin Cross6d88dba2019-11-06 07:06:58 -0800391 // Produce fully expanded flags for use by C tools, C compiles, C++ tools, C++ compiles, and asm compiles
392 // respectively.
393 toolingCflags := flags.globalCommonFlags + " " +
394 flags.globalToolingCFlags + " " +
395 flags.globalConlyFlags + " " +
396 flags.localCommonFlags + " " +
397 flags.localToolingCFlags + " " +
398 flags.localConlyFlags + " " +
399 flags.systemIncludeFlags
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700400
Colin Cross6d88dba2019-11-06 07:06:58 -0800401 cflags := flags.globalCommonFlags + " " +
402 flags.globalCFlags + " " +
403 flags.globalConlyFlags + " " +
404 flags.localCommonFlags + " " +
405 flags.localCFlags + " " +
406 flags.localConlyFlags + " " +
407 flags.systemIncludeFlags
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700408
Colin Cross6d88dba2019-11-06 07:06:58 -0800409 toolingCppflags := flags.globalCommonFlags + " " +
410 flags.globalToolingCFlags + " " +
411 flags.globalToolingCppFlags + " " +
412 flags.localCommonFlags + " " +
413 flags.localToolingCFlags + " " +
414 flags.localToolingCppFlags + " " +
415 flags.systemIncludeFlags
Colin Crossc3199482017-03-30 15:03:04 -0700416
Colin Cross6d88dba2019-11-06 07:06:58 -0800417 cppflags := flags.globalCommonFlags + " " +
418 flags.globalCFlags + " " +
419 flags.globalCppFlags + " " +
420 flags.localCommonFlags + " " +
421 flags.localCFlags + " " +
422 flags.localCppFlags + " " +
423 flags.systemIncludeFlags
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700424
Colin Cross6d88dba2019-11-06 07:06:58 -0800425 asflags := flags.globalCommonFlags + " " +
426 flags.globalAsFlags + " " +
427 flags.localCommonFlags + " " +
428 flags.localAsFlags + " " +
429 flags.systemIncludeFlags
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700430
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800431 var sAbiDumpFiles android.Paths
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700432 if flags.sAbiDump {
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800433 sAbiDumpFiles = make(android.Paths, 0, len(srcFiles))
434 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800435
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700436 cflags += " ${config.NoOverrideClangGlobalCflags}"
437 toolingCflags += " ${config.NoOverrideClangGlobalCflags}"
438 cppflags += " ${config.NoOverrideClangGlobalCflags}"
439 toolingCppflags += " ${config.NoOverrideClangGlobalCflags}"
Dan Willemsenbe03f342016-03-03 17:21:04 -0800440
Colin Cross3f40fa42015-01-30 17:27:36 -0800441 for i, srcFile := range srcFiles {
Dan Willemsen21ec4902016-11-02 20:43:13 -0700442 objFile := android.ObjPathWithExt(ctx, subdir, srcFile, "o")
Colin Cross3f40fa42015-01-30 17:27:36 -0800443
444 objFiles[i] = objFile
445
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700446 switch srcFile.Ext() {
447 case ".asm":
Colin Crossae887032017-10-23 17:16:14 -0700448 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700449 Rule: yasm,
450 Description: "yasm " + srcFile.Rel(),
451 Output: objFile,
452 Input: srcFile,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800453 Implicits: cFlagsDeps,
454 OrderOnly: pathDeps,
Colin Cross91e90042016-12-02 17:13:24 -0800455 Args: map[string]string{
Colin Cross6d88dba2019-11-06 07:06:58 -0800456 "asFlags": flags.globalYasmFlags + " " + flags.localYasmFlags,
Colin Cross91e90042016-12-02 17:13:24 -0800457 },
458 })
459 continue
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700460 case ".rc":
Colin Crossae887032017-10-23 17:16:14 -0700461 ctx.Build(pctx, android.BuildParams{
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700462 Rule: windres,
463 Description: "windres " + srcFile.Rel(),
464 Output: objFile,
465 Input: srcFile,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800466 Implicits: cFlagsDeps,
467 OrderOnly: pathDeps,
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700468 Args: map[string]string{
469 "windresCmd": gccCmd(flags.toolchain, "windres"),
470 "flags": flags.toolchain.WindresFlags(),
471 },
472 })
473 continue
Pete Bentleyfcf55bf2019-08-16 20:14:32 +0100474 case ".o":
475 objFiles[i] = srcFile
476 continue
Colin Cross91e90042016-12-02 17:13:24 -0800477 }
478
Colin Cross6d88dba2019-11-06 07:06:58 -0800479 var moduleFlags string
480 var moduleToolingFlags string
481
Colin Cross3f40fa42015-01-30 17:27:36 -0800482 var ccCmd string
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700483 tidy := flags.tidy
Oliver Nguyen04526782020-04-21 12:40:27 -0700484 coverage := flags.gcovCoverage
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700485 dump := flags.sAbiDump
Dan Willemsenfcabb1c2019-01-03 23:25:11 -0800486 rule := cc
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800487 emitXref := flags.emitXrefs
Colin Cross3f40fa42015-01-30 17:27:36 -0800488
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700489 switch srcFile.Ext() {
Dan Willemsenfcabb1c2019-01-03 23:25:11 -0800490 case ".s":
Dan Willemsen98ab3112019-08-27 21:20:40 -0700491 if !flags.assemblerWithCpp {
492 rule = ccNoDeps
493 }
Dan Willemsenfcabb1c2019-01-03 23:25:11 -0800494 fallthrough
495 case ".S":
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700496 ccCmd = "clang"
Colin Cross6d88dba2019-11-06 07:06:58 -0800497 moduleFlags = asflags
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700498 tidy = false
Dan Willemsen581341d2017-02-09 16:16:31 -0800499 coverage = false
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800500 dump = false
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800501 emitXref = false
Colin Cross3f40fa42015-01-30 17:27:36 -0800502 case ".c":
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700503 ccCmd = "clang"
Colin Cross6d88dba2019-11-06 07:06:58 -0800504 moduleFlags = cflags
505 moduleToolingFlags = toolingCflags
Colin Crossd34ab7c2019-06-27 14:46:10 -0700506 case ".cpp", ".cc", ".cxx", ".mm":
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700507 ccCmd = "clang++"
Colin Cross6d88dba2019-11-06 07:06:58 -0800508 moduleFlags = cppflags
509 moduleToolingFlags = toolingCppflags
Colin Cross3f40fa42015-01-30 17:27:36 -0800510 default:
511 ctx.ModuleErrorf("File %s has unknown extension", srcFile)
512 continue
513 }
514
Colin Cross67a5c132017-05-09 13:45:28 -0700515 ccDesc := ccCmd
516
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700517 ccCmd = "${config.ClangBin}/" + ccCmd
Colin Cross3f40fa42015-01-30 17:27:36 -0800518
Dan Willemsen581341d2017-02-09 16:16:31 -0800519 var implicitOutputs android.WritablePaths
520 if coverage {
521 gcnoFile := android.ObjPathWithExt(ctx, subdir, srcFile, "gcno")
522 implicitOutputs = append(implicitOutputs, gcnoFile)
523 coverageFiles = append(coverageFiles, gcnoFile)
524 }
525
Colin Crossae887032017-10-23 17:16:14 -0700526 ctx.Build(pctx, android.BuildParams{
Dan Willemsenfcabb1c2019-01-03 23:25:11 -0800527 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -0700528 Description: ccDesc + " " + srcFile.Rel(),
Dan Willemsen581341d2017-02-09 16:16:31 -0800529 Output: objFile,
530 ImplicitOutputs: implicitOutputs,
531 Input: srcFile,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800532 Implicits: cFlagsDeps,
533 OrderOnly: pathDeps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800534 Args: map[string]string{
Colin Cross6d88dba2019-11-06 07:06:58 -0800535 "cFlags": moduleFlags,
Colin Cross28344522015-04-22 13:07:53 -0700536 "ccCmd": ccCmd,
Colin Cross3f40fa42015-01-30 17:27:36 -0800537 },
538 })
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700539
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800540 if emitXref {
541 kytheFile := android.ObjPathWithExt(ctx, subdir, srcFile, "kzip")
542 ctx.Build(pctx, android.BuildParams{
543 Rule: kytheExtract,
544 Description: "Xref C++ extractor " + srcFile.Rel(),
545 Output: kytheFile,
546 Input: srcFile,
547 Implicits: cFlagsDeps,
548 OrderOnly: pathDeps,
549 Args: map[string]string{
Colin Cross6d88dba2019-11-06 07:06:58 -0800550 "cFlags": moduleFlags,
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800551 },
552 })
553 kytheFiles = append(kytheFiles, kytheFile)
554 }
555
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700556 if tidy {
Dan Willemsen21ec4902016-11-02 20:43:13 -0700557 tidyFile := android.ObjPathWithExt(ctx, subdir, srcFile, "tidy")
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700558 tidyFiles = append(tidyFiles, tidyFile)
559
Colin Crossae887032017-10-23 17:16:14 -0700560 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700561 Rule: clangTidy,
562 Description: "clang-tidy " + srcFile.Rel(),
563 Output: tidyFile,
564 Input: srcFile,
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700565 // We must depend on objFile, since clang-tidy doesn't
566 // support exporting dependencies.
Dan Willemsen6b4419c2019-08-09 12:45:53 -0700567 Implicit: objFile,
568 Implicits: cFlagsDeps,
569 OrderOnly: pathDeps,
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700570 Args: map[string]string{
Colin Cross6d88dba2019-11-06 07:06:58 -0800571 "cFlags": moduleToolingFlags,
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700572 "tidyFlags": flags.tidyFlags,
573 },
574 })
575 }
576
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800577 if dump {
578 sAbiDumpFile := android.ObjPathWithExt(ctx, subdir, srcFile, "sdump")
579 sAbiDumpFiles = append(sAbiDumpFiles, sAbiDumpFile)
580
Ramy Medhat31ec9422020-04-17 15:03:58 -0400581 dumpRule := sAbiDump
582 if ctx.Config().IsEnvTrue("RBE_ABI_DUMPER") {
583 dumpRule = sAbiDumpRE
584 }
Colin Crossae887032017-10-23 17:16:14 -0700585 ctx.Build(pctx, android.BuildParams{
Ramy Medhat31ec9422020-04-17 15:03:58 -0400586 Rule: dumpRule,
Colin Cross67a5c132017-05-09 13:45:28 -0700587 Description: "header-abi-dumper " + srcFile.Rel(),
588 Output: sAbiDumpFile,
589 Input: srcFile,
590 Implicit: objFile,
Dan Willemsen6b4419c2019-08-09 12:45:53 -0700591 Implicits: cFlagsDeps,
592 OrderOnly: pathDeps,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800593 Args: map[string]string{
Colin Cross6d88dba2019-11-06 07:06:58 -0800594 "cFlags": moduleToolingFlags,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800595 "exportDirs": flags.sAbiFlags,
596 },
597 })
598 }
599
Colin Cross3f40fa42015-01-30 17:27:36 -0800600 }
601
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700602 return Objects{
Dan Willemsen581341d2017-02-09 16:16:31 -0800603 objFiles: objFiles,
604 tidyFiles: tidyFiles,
605 coverageFiles: coverageFiles,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800606 sAbiDumpFiles: sAbiDumpFiles,
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800607 kytheFiles: kytheFiles,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700608 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800609}
610
611// Generate a rule for compiling multiple .o files to a static library (.a)
Colin Cross635c3b02016-05-18 15:37:25 -0700612func TransformObjToStaticLib(ctx android.ModuleContext, objFiles android.Paths,
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700613 flags builderFlags, outputFile android.ModuleOutPath, deps android.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800614
Stephen Hinesf1addeb2018-01-09 23:29:04 -0800615 arCmd := "${config.ClangBin}/llvm-ar"
Yi Kongee96a792019-09-06 15:11:57 -0700616 arFlags := "crsPD"
Stephen Hinesf1addeb2018-01-09 23:29:04 -0800617 if !ctx.Darwin() {
618 arFlags += " -format=gnu"
619 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800620
Colin Crossae887032017-10-23 17:16:14 -0700621 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700622 Rule: ar,
623 Description: "static link " + outputFile.Base(),
624 Output: outputFile,
625 Inputs: objFiles,
626 Implicits: deps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800627 Args: map[string]string{
628 "arFlags": arFlags,
629 "arCmd": arCmd,
630 },
631 })
632}
633
634// Generate a rule for compiling multiple .o files, plus static libraries, whole static libraries,
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700635// and shared libraries, to a shared library (.so) or dynamic executable
Colin Cross635c3b02016-05-18 15:37:25 -0700636func TransformObjToDynamicBinary(ctx android.ModuleContext,
637 objFiles, sharedLibs, staticLibs, lateStaticLibs, wholeStaticLibs, deps android.Paths,
Josh Gao75a50a22019-06-07 17:58:59 -0700638 crtBegin, crtEnd android.OptionalPath, groupLate bool, flags builderFlags, outputFile android.WritablePath, implicitOutputs android.WritablePaths) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800639
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700640 ldCmd := "${config.ClangBin}/clang++"
Colin Cross3f40fa42015-01-30 17:27:36 -0800641
Colin Cross3f40fa42015-01-30 17:27:36 -0800642 var libFlagsList []string
643
Colin Cross16b23492016-01-06 14:41:07 -0800644 if len(flags.libFlags) > 0 {
645 libFlagsList = append(libFlagsList, flags.libFlags)
646 }
647
Colin Cross3f40fa42015-01-30 17:27:36 -0800648 if len(wholeStaticLibs) > 0 {
Dan Willemsen490fd492015-11-24 17:53:15 -0800649 if ctx.Host() && ctx.Darwin() {
Colin Cross635c3b02016-05-18 15:37:25 -0700650 libFlagsList = append(libFlagsList, android.JoinWithPrefix(wholeStaticLibs.Strings(), "-force_load "))
Colin Cross0af4b842015-04-30 16:36:18 -0700651 } else {
652 libFlagsList = append(libFlagsList, "-Wl,--whole-archive ")
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700653 libFlagsList = append(libFlagsList, wholeStaticLibs.Strings()...)
Colin Cross0af4b842015-04-30 16:36:18 -0700654 libFlagsList = append(libFlagsList, "-Wl,--no-whole-archive ")
655 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800656 }
657
Colin Cross7a7cf972016-12-05 18:47:39 -0800658 if flags.groupStaticLibs && !ctx.Darwin() && len(staticLibs) > 0 {
Colin Cross18c0c5a2016-12-01 14:45:23 -0800659 libFlagsList = append(libFlagsList, "-Wl,--start-group")
660 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700661 libFlagsList = append(libFlagsList, staticLibs.Strings()...)
Colin Cross7a7cf972016-12-05 18:47:39 -0800662 if flags.groupStaticLibs && !ctx.Darwin() && len(staticLibs) > 0 {
Colin Cross18c0c5a2016-12-01 14:45:23 -0800663 libFlagsList = append(libFlagsList, "-Wl,--end-group")
664 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800665
Stephen Hines10347862016-07-18 15:54:54 -0700666 if groupLate && !ctx.Darwin() && len(lateStaticLibs) > 0 {
Dan Willemsenedc385f2015-07-08 13:02:23 -0700667 libFlagsList = append(libFlagsList, "-Wl,--start-group")
668 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700669 libFlagsList = append(libFlagsList, lateStaticLibs.Strings()...)
Stephen Hines10347862016-07-18 15:54:54 -0700670 if groupLate && !ctx.Darwin() && len(lateStaticLibs) > 0 {
Dan Willemsenedc385f2015-07-08 13:02:23 -0700671 libFlagsList = append(libFlagsList, "-Wl,--end-group")
672 }
673
Colin Cross3f40fa42015-01-30 17:27:36 -0800674 for _, lib := range sharedLibs {
Josh Gao75a50a22019-06-07 17:58:59 -0700675 libFile := lib.String()
676 if ctx.Windows() {
677 libFile = pathtools.ReplaceExtension(libFile, "lib")
678 }
679 libFlagsList = append(libFlagsList, libFile)
Colin Cross3f40fa42015-01-30 17:27:36 -0800680 }
681
Colin Cross3f40fa42015-01-30 17:27:36 -0800682 deps = append(deps, staticLibs...)
Colin Cross3075ad02015-03-17 10:47:08 -0700683 deps = append(deps, lateStaticLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800684 deps = append(deps, wholeStaticLibs...)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700685 if crtBegin.Valid() {
686 deps = append(deps, crtBegin.Path(), crtEnd.Path())
Colin Cross3f40fa42015-01-30 17:27:36 -0800687 }
688
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400689 rule := ld
Kousik Kumar3fb61262020-04-22 13:31:09 -0700690 args := map[string]string{
691 "ldCmd": ldCmd,
692 "crtBegin": crtBegin.String(),
693 "libFlags": strings.Join(libFlagsList, " "),
694 "extraLibFlags": flags.extraLibFlags,
695 "ldFlags": flags.globalLdFlags + " " + flags.localLdFlags,
696 "crtEnd": crtEnd.String(),
697 }
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400698 if ctx.Config().IsEnvTrue("RBE_CXX_LINKS") {
699 rule = ldRE
Kousik Kumar3fb61262020-04-22 13:31:09 -0700700 args["implicitOutputs"] = strings.Join(implicitOutputs.Strings(), ",")
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400701 }
702
Colin Crossae887032017-10-23 17:16:14 -0700703 ctx.Build(pctx, android.BuildParams{
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400704 Rule: rule,
Josh Gao75a50a22019-06-07 17:58:59 -0700705 Description: "link " + outputFile.Base(),
706 Output: outputFile,
707 ImplicitOutputs: implicitOutputs,
708 Inputs: objFiles,
709 Implicits: deps,
Kousik Kumar3fb61262020-04-22 13:31:09 -0700710 Args: args,
Colin Cross3f40fa42015-01-30 17:27:36 -0800711 })
712}
713
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800714// Generate a rule to combine .dump sAbi dump files from multiple source files
715// into a single .ldump sAbi dump file
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700716func TransformDumpToLinkedDump(ctx android.ModuleContext, sAbiDumps android.Paths, soFile android.Path,
Logan Chiene3d7a0d2019-01-17 00:18:02 +0800717 baseName, exportedHeaderFlags string, symbolFile android.OptionalPath,
718 excludedSymbolVersions, excludedSymbolTags []string) android.OptionalPath {
719
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800720 outputFile := android.PathForModuleOut(ctx, baseName+".lsdump")
Logan Chiene3d7a0d2019-01-17 00:18:02 +0800721
722 implicits := android.Paths{soFile}
Jayant Chowdharydf344d52018-01-17 11:11:42 -0800723 symbolFilterStr := "-so " + soFile.String()
Logan Chiene3d7a0d2019-01-17 00:18:02 +0800724
725 if symbolFile.Valid() {
726 implicits = append(implicits, symbolFile.Path())
727 symbolFilterStr += " -v " + symbolFile.String()
728 }
729 for _, ver := range excludedSymbolVersions {
730 symbolFilterStr += " --exclude-symbol-version " + ver
731 }
732 for _, tag := range excludedSymbolTags {
733 symbolFilterStr += " --exclude-symbol-tag " + tag
734 }
Ramy Medhat808594c2020-05-07 06:56:47 -0400735 rule := sAbiLink
736 args := map[string]string{
737 "symbolFilter": symbolFilterStr,
738 "arch": ctx.Arch().ArchType.Name,
739 "exportedHeaderFlags": exportedHeaderFlags,
740 }
741 if ctx.Config().IsEnvTrue("RBE_ABI_LINKER") {
742 rule = sAbiLinkRE
743 rbeImplicits := implicits.Strings()
744 for _, p := range strings.Split(exportedHeaderFlags, " ") {
745 if len(p) > 2 {
746 // Exclude the -I prefix.
747 rbeImplicits = append(rbeImplicits, p[2:])
748 }
749 }
750 args["implicits"] = strings.Join(rbeImplicits, ",")
751 }
Colin Crossae887032017-10-23 17:16:14 -0700752 ctx.Build(pctx, android.BuildParams{
Ramy Medhat808594c2020-05-07 06:56:47 -0400753 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -0700754 Description: "header-abi-linker " + outputFile.Base(),
755 Output: outputFile,
756 Inputs: sAbiDumps,
Logan Chiene3d7a0d2019-01-17 00:18:02 +0800757 Implicits: implicits,
Ramy Medhat808594c2020-05-07 06:56:47 -0400758 Args: args,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800759 })
760 return android.OptionalPathForPath(outputFile)
761}
762
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700763func UnzipRefDump(ctx android.ModuleContext, zippedRefDump android.Path, baseName string) android.Path {
764 outputFile := android.PathForModuleOut(ctx, baseName+"_ref.lsdump")
Colin Crossae887032017-10-23 17:16:14 -0700765 ctx.Build(pctx, android.BuildParams{
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700766 Rule: unzipRefSAbiDump,
767 Description: "gunzip" + outputFile.Base(),
768 Output: outputFile,
769 Input: zippedRefDump,
770 })
771 return outputFile
772}
773
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800774func SourceAbiDiff(ctx android.ModuleContext, inputDump android.Path, referenceDump android.Path,
Logan Chien2a65dda2019-10-01 15:58:07 -0700775 baseName, exportedHeaderFlags string, checkAllApis, isLlndk, isNdk, isVndkExt bool) android.OptionalPath {
Logan Chienf3511742017-10-31 18:04:35 +0800776
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800777 outputFile := android.PathForModuleOut(ctx, baseName+".abidiff")
Jayant Chowdharyc7434e22018-05-31 15:42:26 -0700778 libName := strings.TrimSuffix(baseName, filepath.Ext(baseName))
Logan Chien6227fed2019-02-18 13:12:21 +0800779 createReferenceDumpFlags := ""
780
Logan Chien2a65dda2019-10-01 15:58:07 -0700781 var extraFlags []string
782 if checkAllApis {
783 extraFlags = append(extraFlags, "-check-all-apis")
784 } else {
785 extraFlags = append(extraFlags,
786 "-allow-unreferenced-changes",
787 "-allow-unreferenced-elf-symbol-changes")
Jayant Chowdharye4499502018-01-17 13:13:33 -0800788 }
Logan Chien2a65dda2019-10-01 15:58:07 -0700789
790 if exportedHeaderFlags == "" {
791 extraFlags = append(extraFlags, "-advice-only")
792 }
793
Logan Chien62f1f942019-02-18 15:40:42 +0800794 if isLlndk || isNdk {
Logan Chien6227fed2019-02-18 13:12:21 +0800795 createReferenceDumpFlags = "--llndk"
Logan Chien62f1f942019-02-18 15:40:42 +0800796 if isLlndk {
797 // TODO(b/130324828): "-consider-opaque-types-different" should apply to
798 // both LLNDK and NDK shared libs. However, a known issue in header-abi-diff
799 // breaks libaaudio. Remove the if-guard after the issue is fixed.
Logan Chien2a65dda2019-10-01 15:58:07 -0700800 extraFlags = append(extraFlags, "-consider-opaque-types-different")
Logan Chien62f1f942019-02-18 15:40:42 +0800801 }
Jayant Chowdharyc7434e22018-05-31 15:42:26 -0700802 }
Logan Chienf3511742017-10-31 18:04:35 +0800803 if isVndkExt {
Logan Chien2a65dda2019-10-01 15:58:07 -0700804 extraFlags = append(extraFlags, "-allow-extensions")
Logan Chienf3511742017-10-31 18:04:35 +0800805 }
806
Colin Crossae887032017-10-23 17:16:14 -0700807 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700808 Rule: sAbiDiff,
809 Description: "header-abi-diff " + outputFile.Base(),
810 Output: outputFile,
811 Input: inputDump,
812 Implicit: referenceDump,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800813 Args: map[string]string{
Logan Chien6227fed2019-02-18 13:12:21 +0800814 "referenceDump": referenceDump.String(),
815 "libName": libName,
816 "arch": ctx.Arch().ArchType.Name,
Logan Chien2a65dda2019-10-01 15:58:07 -0700817 "extraFlags": strings.Join(extraFlags, " "),
Logan Chien6227fed2019-02-18 13:12:21 +0800818 "createReferenceDumpFlags": createReferenceDumpFlags,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800819 },
820 })
821 return android.OptionalPathForPath(outputFile)
822}
823
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700824// Generate a rule for extracting a table of contents from a shared library (.so)
825func TransformSharedObjectToToc(ctx android.ModuleContext, inputFile android.Path,
Colin Cross26c34ed2016-09-30 17:10:16 -0700826 outputFile android.WritablePath, flags builderFlags) {
827
Colin Crossb496cfd2018-09-10 16:50:05 -0700828 var format string
829 var crossCompile string
830 if ctx.Darwin() {
831 format = "--macho"
832 crossCompile = "${config.MacToolPath}"
833 } else if ctx.Windows() {
834 format = "--pe"
835 crossCompile = gccCmd(flags.toolchain, "")
836 } else {
837 format = "--elf"
838 crossCompile = gccCmd(flags.toolchain, "")
839 }
Colin Cross26c34ed2016-09-30 17:10:16 -0700840
Colin Crossae887032017-10-23 17:16:14 -0700841 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700842 Rule: toc,
843 Description: "generate toc " + inputFile.Base(),
844 Output: outputFile,
845 Input: inputFile,
Colin Cross26c34ed2016-09-30 17:10:16 -0700846 Args: map[string]string{
847 "crossCompile": crossCompile,
Colin Crossb496cfd2018-09-10 16:50:05 -0700848 "format": format,
Colin Cross26c34ed2016-09-30 17:10:16 -0700849 },
850 })
851}
852
Colin Cross3f40fa42015-01-30 17:27:36 -0800853// Generate a rule for compiling multiple .o files to a .o using ld partial linking
Colin Cross635c3b02016-05-18 15:37:25 -0700854func TransformObjsToObj(ctx android.ModuleContext, objFiles android.Paths,
Dan Willemsen724ab5d2019-09-19 10:50:18 -0700855 flags builderFlags, outputFile android.WritablePath, deps android.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800856
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700857 ldCmd := "${config.ClangBin}/clang++"
Colin Cross3f40fa42015-01-30 17:27:36 -0800858
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400859 rule := partialLd
860 args := map[string]string{
861 "ldCmd": ldCmd,
862 "ldFlags": flags.globalLdFlags + " " + flags.localLdFlags,
863 }
864 if ctx.Config().IsEnvTrue("RBE_CXX_LINKS") {
865 rule = partialLdRE
866 args["inCommaList"] = strings.Join(objFiles.Strings(), ",")
867 }
Colin Crossae887032017-10-23 17:16:14 -0700868 ctx.Build(pctx, android.BuildParams{
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400869 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -0700870 Description: "link " + outputFile.Base(),
871 Output: outputFile,
872 Inputs: objFiles,
Dan Willemsen724ab5d2019-09-19 10:50:18 -0700873 Implicits: deps,
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400874 Args: args,
Colin Cross3f40fa42015-01-30 17:27:36 -0800875 })
876}
877
Colin Crossbfae8852015-03-26 14:44:11 -0700878// Generate a rule for runing objcopy --prefix-symbols on a binary
Colin Cross635c3b02016-05-18 15:37:25 -0700879func TransformBinaryPrefixSymbols(ctx android.ModuleContext, prefix string, inputFile android.Path,
880 flags builderFlags, outputFile android.WritablePath) {
Colin Crossbfae8852015-03-26 14:44:11 -0700881
882 objcopyCmd := gccCmd(flags.toolchain, "objcopy")
883
Colin Crossae887032017-10-23 17:16:14 -0700884 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700885 Rule: prefixSymbols,
886 Description: "prefix symbols " + outputFile.Base(),
887 Output: outputFile,
888 Input: inputFile,
Colin Crossbfae8852015-03-26 14:44:11 -0700889 Args: map[string]string{
890 "objcopyCmd": objcopyCmd,
891 "prefix": prefix,
892 },
893 })
894}
895
Colin Cross635c3b02016-05-18 15:37:25 -0700896func TransformStrip(ctx android.ModuleContext, inputFile android.Path,
897 outputFile android.WritablePath, flags builderFlags) {
Colin Cross665dce92016-04-28 14:50:03 -0700898
899 crossCompile := gccCmd(flags.toolchain, "")
900 args := ""
901 if flags.stripAddGnuDebuglink {
902 args += " --add-gnu-debuglink"
903 }
904 if flags.stripKeepMiniDebugInfo {
905 args += " --keep-mini-debug-info"
906 }
907 if flags.stripKeepSymbols {
908 args += " --keep-symbols"
909 }
Yi Kongacee27c2019-03-29 20:05:14 -0700910 if flags.stripKeepSymbolsList != "" {
911 args += " -k" + flags.stripKeepSymbolsList
912 }
Christopher Ferrisb43fe7a2019-05-17 16:39:54 -0700913 if flags.stripKeepSymbolsAndDebugFrame {
914 args += " --keep-symbols-and-debug-frame"
915 }
Yi Kongb5c34d72018-11-07 16:28:49 -0800916 if flags.stripUseGnuStrip {
917 args += " --use-gnu-strip"
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -0700918 }
Colin Cross665dce92016-04-28 14:50:03 -0700919
Colin Crossae887032017-10-23 17:16:14 -0700920 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700921 Rule: strip,
922 Description: "strip " + outputFile.Base(),
923 Output: outputFile,
924 Input: inputFile,
Colin Cross665dce92016-04-28 14:50:03 -0700925 Args: map[string]string{
926 "crossCompile": crossCompile,
927 "args": args,
928 },
929 })
930}
931
Colin Cross635c3b02016-05-18 15:37:25 -0700932func TransformDarwinStrip(ctx android.ModuleContext, inputFile android.Path,
933 outputFile android.WritablePath) {
Colin Crossb8ecdfe2016-05-03 15:10:29 -0700934
Colin Crossae887032017-10-23 17:16:14 -0700935 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700936 Rule: darwinStrip,
937 Description: "strip " + outputFile.Base(),
938 Output: outputFile,
939 Input: inputFile,
Colin Crossb8ecdfe2016-05-03 15:10:29 -0700940 })
941}
942
Oliver Nguyenc7434142019-04-24 14:22:25 -0700943func TransformCoverageFilesToZip(ctx android.ModuleContext,
944 inputs Objects, baseName string) android.OptionalPath {
Dan Willemsen581341d2017-02-09 16:16:31 -0800945
946 if len(inputs.coverageFiles) > 0 {
Oliver Nguyenc7434142019-04-24 14:22:25 -0700947 outputFile := android.PathForModuleOut(ctx, baseName+".zip")
Dan Willemsen581341d2017-02-09 16:16:31 -0800948
Oliver Nguyenc7434142019-04-24 14:22:25 -0700949 ctx.Build(pctx, android.BuildParams{
950 Rule: zip,
951 Description: "zip " + outputFile.Base(),
952 Inputs: inputs.coverageFiles,
953 Output: outputFile,
954 })
Dan Willemsen581341d2017-02-09 16:16:31 -0800955
956 return android.OptionalPathForPath(outputFile)
957 }
958
959 return android.OptionalPath{}
960}
961
Yi Kongc49c3932019-10-15 02:01:19 -0700962func TransformArchiveRepack(ctx android.ModuleContext, inputFile android.Path,
963 outputFile android.WritablePath, objects []string) {
964
965 ctx.Build(pctx, android.BuildParams{
966 Rule: archiveRepack,
967 Description: "Repack archive " + outputFile.Base(),
968 Output: outputFile,
969 Input: inputFile,
970 Args: map[string]string{
971 "objects": strings.Join(objects, " "),
972 },
973 })
974}
975
Colin Crossb98c8b02016-07-29 13:44:28 -0700976func gccCmd(toolchain config.Toolchain, cmd string) string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800977 return filepath.Join(toolchain.GccRoot(), "bin", toolchain.GccTriple()+"-"+cmd)
978}
Colin Cross0af4b842015-04-30 16:36:18 -0700979
Colin Cross5b529592017-05-09 13:34:34 -0700980func splitListForSize(list android.Paths, limit int) (lists []android.Paths, err error) {
Colin Cross0af4b842015-04-30 16:36:18 -0700981 var i int
982
983 start := 0
984 bytes := 0
985 for i = range list {
Colin Cross5b529592017-05-09 13:34:34 -0700986 l := len(list[i].String())
Colin Cross0af4b842015-04-30 16:36:18 -0700987 if l > limit {
988 return nil, fmt.Errorf("list element greater than size limit (%d)", limit)
989 }
990 if bytes+l > limit {
991 lists = append(lists, list[start:i])
992 start = i
993 bytes = 0
994 }
995 bytes += l + 1 // count a space between each list element
996 }
997
998 lists = append(lists, list[start:])
999
1000 totalLen := 0
1001 for _, l := range lists {
1002 totalLen += len(l)
1003 }
1004 if totalLen != len(list) {
1005 panic(fmt.Errorf("Failed breaking up list, %d != %d", len(list), totalLen))
1006 }
1007 return lists, nil
1008}