blob: bde8c96472bcf93e3b08a4e229852fe866852f79 [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 Crossb98c8b02016-07-29 13:44:28 -070022 "path/filepath"
Colin Cross0af4b842015-04-30 16:36:18 -070023 "runtime"
Colin Cross3f40fa42015-01-30 17:27:36 -080024 "strings"
Colin Crossed4cf0b2015-03-26 14:43:45 -070025
26 "github.com/google/blueprint"
Josh Gao75a50a22019-06-07 17:58:59 -070027 "github.com/google/blueprint/pathtools"
Colin Crossb98c8b02016-07-29 13:44:28 -070028
29 "android/soong/android"
30 "android/soong/cc/config"
Ramy Medhat9a90fe52020-04-13 13:21:23 -040031 "android/soong/remoteexec"
Colin Cross3f40fa42015-01-30 17:27:36 -080032)
33
34const (
Dan Albertc3144b12015-04-28 18:17:56 -070035 objectExtension = ".o"
Colin Cross3f40fa42015-01-30 17:27:36 -080036 staticLibraryExtension = ".a"
37)
38
39var (
Colin Cross635c3b02016-05-18 15:37:25 -070040 pctx = android.NewPackageContext("android/soong/cc")
Colin Cross3f40fa42015-01-30 17:27:36 -080041
Chris Parsonsbf4f55f2020-11-23 17:02:44 -050042 // Rule to invoke gcc with given command, flags, and dependencies. Outputs a .d depfile.
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,
Kousik Kumard207cbe2020-10-20 05:52:49 +000047 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
Chris Parsonsbf4f55f2020-11-23 17:02:44 -050052 // Rule to invoke gcc with given command and flags, but no dependencies.
Kousik Kumar2976bfd2020-02-17 00:26:55 -080053 ccNoDeps = pctx.AndroidStaticRule("ccNoDeps",
Dan Willemsenfcabb1c2019-01-03 23:25:11 -080054 blueprint.RuleParams{
Kousik Kumard207cbe2020-10-20 05:52:49 +000055 Command: "$relPwd $ccCmd -c $cFlags -o $out $in",
Dan Willemsenfcabb1c2019-01-03 23:25:11 -080056 CommandDeps: []string{"$ccCmd"},
57 },
58 "ccCmd", "cFlags")
59
Chris Parsonsbf4f55f2020-11-23 17:02:44 -050060 // Rules to invoke ld to link binaries. Uses a .rsp file to list dependencies, as there may
61 // be many.
Colin Cross77cdcfd2021-03-12 11:28:25 -080062 ld, ldRE = pctx.RemoteStaticRules("ld",
Colin Cross3f40fa42015-01-30 17:27:36 -080063 blueprint.RuleParams{
Ramy Medhat31ec9422020-04-17 15:03:58 -040064 Command: "$reTemplate$ldCmd ${crtBegin} @${out}.rsp " +
Pete Bentley99f2fc22019-08-02 14:02:20 +010065 "${libFlags} ${crtEnd} -o ${out} ${ldFlags} ${extraLibFlags}",
Dan Willemsenc94a7682015-11-17 15:27:28 -080066 CommandDeps: []string{"$ldCmd"},
Colin Cross7d21c442015-03-30 17:47:53 -070067 Rspfile: "${out}.rsp",
68 RspfileContent: "${in}",
Colin Cross36ae1352019-03-29 15:55:30 -070069 // clang -Wl,--out-implib doesn't update its output file if it hasn't changed.
70 Restat: true,
Colin Cross3f40fa42015-01-30 17:27:36 -080071 },
Ramy Medhat31ec9422020-04-17 15:03:58 -040072 &remoteexec.REParams{
73 Labels: map[string]string{"type": "link", "tool": "clang"},
Ramy Medhat9a90fe52020-04-13 13:21:23 -040074 ExecStrategy: "${config.RECXXLinksExecStrategy}",
Ramy Medhat6797edc2020-08-28 14:21:55 -040075 Inputs: []string{"${out}.rsp", "$implicitInputs"},
Colin Crossa4eafdd2021-03-24 14:09:28 -070076 RSPFiles: []string{"${out}.rsp"},
Kousik Kumar3fb61262020-04-22 13:31:09 -070077 OutputFiles: []string{"${out}", "$implicitOutputs"},
Ramy Medhat9a90fe52020-04-13 13:21:23 -040078 ToolchainInputs: []string{"$ldCmd"},
79 Platform: map[string]string{remoteexec.PoolKey: "${config.RECXXLinksPool}"},
Ramy Medhat6797edc2020-08-28 14:21:55 -040080 }, []string{"ldCmd", "crtBegin", "libFlags", "crtEnd", "ldFlags", "extraLibFlags"}, []string{"implicitInputs", "implicitOutputs"})
Colin Cross3f40fa42015-01-30 17:27:36 -080081
Chris Parsonsbf4f55f2020-11-23 17:02:44 -050082 // Rules for .o files to combine to other .o files, using ld partial linking.
Colin Cross77cdcfd2021-03-12 11:28:25 -080083 partialLd, partialLdRE = pctx.RemoteStaticRules("partialLd",
Colin Cross3f40fa42015-01-30 17:27:36 -080084 blueprint.RuleParams{
Chih-Hung Hsieh3ede2942018-01-10 14:30:44 -080085 // Without -no-pie, clang 7.0 adds -pie to link Android files,
86 // but -r and -pie cannot be used together.
Ramy Medhat31ec9422020-04-17 15:03:58 -040087 Command: "$reTemplate$ldCmd -fuse-ld=lld -nostdlib -no-pie -Wl,-r ${in} -o ${out} ${ldFlags}",
Dan Willemsenc94a7682015-11-17 15:27:28 -080088 CommandDeps: []string{"$ldCmd"},
Ramy Medhat9a90fe52020-04-13 13:21:23 -040089 }, &remoteexec.REParams{
Ramy Medhat6797edc2020-08-28 14:21:55 -040090 Labels: map[string]string{"type": "link", "tool": "clang"},
91 ExecStrategy: "${config.RECXXLinksExecStrategy}",
92 Inputs: []string{"$inCommaList", "$implicitInputs"},
Kousik Kumar3fb61262020-04-22 13:31:09 -070093 OutputFiles: []string{"${out}", "$implicitOutputs"},
Ramy Medhat9a90fe52020-04-13 13:21:23 -040094 ToolchainInputs: []string{"$ldCmd"},
95 Platform: map[string]string{remoteexec.PoolKey: "${config.RECXXLinksPool}"},
Ramy Medhat6797edc2020-08-28 14:21:55 -040096 }, []string{"ldCmd", "ldFlags"}, []string{"implicitInputs", "inCommaList", "implicitOutputs"})
Colin Cross3f40fa42015-01-30 17:27:36 -080097
Chris Parsonsbf4f55f2020-11-23 17:02:44 -050098 // Rule to invoke `ar` with given cmd and flags, but no static library depenencies.
Colin Cross9d45bb72016-08-29 16:14:13 -070099 ar = pctx.AndroidStaticRule("ar",
Colin Cross3f40fa42015-01-30 17:27:36 -0800100 blueprint.RuleParams{
Colin Cross7d21c442015-03-30 17:47:53 -0700101 Command: "rm -f ${out} && $arCmd $arFlags $out @${out}.rsp",
Dan Willemsenc94a7682015-11-17 15:27:28 -0800102 CommandDeps: []string{"$arCmd"},
Colin Cross7d21c442015-03-30 17:47:53 -0700103 Rspfile: "${out}.rsp",
104 RspfileContent: "${in}",
Colin Cross3f40fa42015-01-30 17:27:36 -0800105 },
106 "arCmd", "arFlags")
107
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500108 // Rule to invoke `ar` with given cmd, flags, and library dependencies. Generates a .a
109 // (archive) file from .o files.
Martin Stjernholm391d94c2020-04-17 17:34:31 +0100110 arWithLibs = pctx.AndroidStaticRule("arWithLibs",
111 blueprint.RuleParams{
112 Command: "rm -f ${out} && $arCmd $arObjFlags $out @${out}.rsp && $arCmd $arLibFlags $out $arLibs",
113 CommandDeps: []string{"$arCmd"},
114 Rspfile: "${out}.rsp",
115 RspfileContent: "${arObjs}",
116 },
117 "arCmd", "arObjFlags", "arObjs", "arLibFlags", "arLibs")
118
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500119 // Rule to run objcopy --prefix-symbols (to prefix all symbols in a file with a given string).
Colin Cross9d45bb72016-08-29 16:14:13 -0700120 prefixSymbols = pctx.AndroidStaticRule("prefixSymbols",
Colin Crossbfae8852015-03-26 14:44:11 -0700121 blueprint.RuleParams{
122 Command: "$objcopyCmd --prefix-symbols=${prefix} ${in} ${out}",
Dan Willemsenc94a7682015-11-17 15:27:28 -0800123 CommandDeps: []string{"$objcopyCmd"},
Colin Crossbfae8852015-03-26 14:44:11 -0700124 },
125 "objcopyCmd", "prefix")
126
Nan Zhang43a485c2017-03-27 14:27:58 -0700127 _ = pctx.SourcePathVariable("stripPath", "build/soong/scripts/strip.sh")
Dan Willemsen8fec83a2018-03-09 10:47:52 -0800128 _ = pctx.SourcePathVariable("xzCmd", "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/xz")
David Srbecky69315e42021-04-29 21:06:47 +0100129 _ = pctx.SourcePathVariable("createMiniDebugInfo", "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/create_minidebuginfo")
Colin Cross665dce92016-04-28 14:50:03 -0700130
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500131 // Rule to invoke `strip` (to discard symbols and data from object files).
132 strip = pctx.AndroidStaticRule("strip",
133 blueprint.RuleParams{
David Srbecky69315e42021-04-29 21:06:47 +0100134 Depfile: "${out}.d",
135 Deps: blueprint.DepsGCC,
136 Command: "XZ=$xzCmd CREATE_MINIDEBUGINFO=$createMiniDebugInfo CLANG_BIN=${config.ClangBin} $stripPath ${args} -i ${in} -o ${out} -d ${out}.d",
137 CommandDeps: func() []string {
138 if runtime.GOOS != "darwin" {
139 return []string{"$stripPath", "$xzCmd", "$createMiniDebugInfo"}
140 } else {
141 return []string{"$stripPath", "$xzCmd"}
142 }
143 }(),
144 Pool: darwinStripPool,
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500145 },
Elliott Hughesafcd3392021-05-05 13:54:11 -0700146 "args")
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500147
148 // Rule to invoke `strip` (to discard symbols and data from object files) on darwin architecture.
149 darwinStrip = pctx.AndroidStaticRule("darwinStrip",
150 blueprint.RuleParams{
151 Command: "${config.MacStripPath} -u -r -o $out $in",
152 CommandDeps: []string{"${config.MacStripPath}"},
153 })
154
Colin Crossee3ea312019-05-17 15:36:46 -0700155 // 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 -0700156 // file descriptors on darwin. Limit concurrent calls to 5 on darwin.
Colin Crossee3ea312019-05-17 15:36:46 -0700157 darwinStripPool = func() blueprint.Pool {
158 if runtime.GOOS == "darwin" {
159 return pctx.StaticPool("darwinStripPool", blueprint.PoolParams{
Colin Crossbadf8d62019-05-22 13:25:50 -0700160 Depth: 5,
Colin Crossee3ea312019-05-17 15:36:46 -0700161 })
162 } else {
163 return nil
164 }
165 }()
166
Yi Kongc49c3932019-10-15 02:01:19 -0700167 _ = pctx.SourcePathVariable("archiveRepackPath", "build/soong/scripts/archive_repack.sh")
168
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500169 // Rule to repack an archive (.a) file with a subset of object files.
Yi Kongc49c3932019-10-15 02:01:19 -0700170 archiveRepack = pctx.AndroidStaticRule("archiveRepack",
171 blueprint.RuleParams{
172 Depfile: "${out}.d",
173 Deps: blueprint.DepsGCC,
174 Command: "CLANG_BIN=${config.ClangBin} $archiveRepackPath -i ${in} -o ${out} -d ${out}.d ${objects}",
175 CommandDeps: []string{"$archiveRepackPath"},
176 },
177 "objects")
178
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500179 // Rule to create an empty file at a given path.
Colin Cross9d45bb72016-08-29 16:14:13 -0700180 emptyFile = pctx.AndroidStaticRule("emptyFile",
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700181 blueprint.RuleParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700182 Command: "rm -f $out && touch $out",
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700183 })
184
Nan Zhang43a485c2017-03-27 14:27:58 -0700185 _ = pctx.SourcePathVariable("tocPath", "build/soong/scripts/toc.sh")
Colin Cross26c34ed2016-09-30 17:10:16 -0700186
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500187 // A rule for extracting a table of contents from a shared library (.so).
Colin Cross26c34ed2016-09-30 17:10:16 -0700188 toc = pctx.AndroidStaticRule("toc",
189 blueprint.RuleParams{
190 Depfile: "${out}.d",
191 Deps: blueprint.DepsGCC,
Yi Kong4ad44e72021-04-01 17:45:42 +0800192 Command: "CLANG_BIN=$clangBin $tocPath $format -i ${in} -o ${out} -d ${out}.d",
Colin Cross26c34ed2016-09-30 17:10:16 -0700193 CommandDeps: []string{"$tocPath"},
194 Restat: true,
195 },
Yi Kong4ad44e72021-04-01 17:45:42 +0800196 "clangBin", "format")
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700197
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500198 // Rule for invoking clang-tidy (a clang-based linter).
Colin Cross77cdcfd2021-03-12 11:28:25 -0800199 clangTidy, clangTidyRE = pctx.RemoteStaticRules("clangTidy",
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700200 blueprint.RuleParams{
Kousik Kumar4e30bba2020-06-18 09:17:51 -0700201 Command: "rm -f $out && $reTemplate${config.ClangBin}/clang-tidy $tidyFlags $in -- $cFlags && touch $out",
George Burgess IVc4624c02019-04-04 16:22:37 -0700202 CommandDeps: []string{"${config.ClangBin}/clang-tidy"},
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700203 },
Kousik Kumar4e30bba2020-06-18 09:17:51 -0700204 &remoteexec.REParams{
205 Labels: map[string]string{"type": "lint", "tool": "clang-tidy", "lang": "cpp"},
206 ExecStrategy: "${config.REClangTidyExecStrategy}",
207 Inputs: []string{"$in"},
208 // OutputFile here is $in for remote-execution since its possible that
209 // clang-tidy modifies the given input file itself and $out refers to the
210 // ".tidy" file generated for ninja-dependency reasons.
Colin Cross053fca12020-08-19 13:51:47 -0700211 OutputFiles: []string{"$in"},
212 Platform: map[string]string{remoteexec.PoolKey: "${config.REClangTidyPool}"},
Kousik Kumar4e30bba2020-06-18 09:17:51 -0700213 }, []string{"cFlags", "tidyFlags"}, []string{})
Colin Cross91e90042016-12-02 17:13:24 -0800214
Nan Zhang43a485c2017-03-27 14:27:58 -0700215 _ = pctx.SourcePathVariable("yasmCmd", "prebuilts/misc/${config.HostPrebuiltTag}/yasm/yasm")
Colin Cross91e90042016-12-02 17:13:24 -0800216
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500217 // Rule for invoking yasm to compile .asm assembly files.
Colin Cross91e90042016-12-02 17:13:24 -0800218 yasm = pctx.AndroidStaticRule("yasm",
219 blueprint.RuleParams{
Dan Willemsen1d3e5452017-08-22 20:53:45 -0700220 Command: "$yasmCmd $asFlags -o $out $in && $yasmCmd $asFlags -M $in >$out.d",
Colin Cross91e90042016-12-02 17:13:24 -0800221 CommandDeps: []string{"$yasmCmd"},
Dan Willemsen1d3e5452017-08-22 20:53:45 -0700222 Depfile: "$out.d",
223 Deps: blueprint.DepsGCC,
Colin Cross91e90042016-12-02 17:13:24 -0800224 },
225 "asFlags")
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800226
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500227 // Rule to invoke windres, for interaction with Windows resources.
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700228 windres = pctx.AndroidStaticRule("windres",
229 blueprint.RuleParams{
Nick Desaulniers18eeffa2020-01-29 16:20:11 -0800230 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 -0700231 CommandDeps: []string{"$windresCmd"},
232 },
233 "windresCmd", "flags")
234
Jayant Chowdharya4c6df52018-02-20 12:36:51 -0800235 _ = pctx.SourcePathVariable("sAbiDumper", "prebuilts/clang-tools/${config.HostPrebuiltTag}/bin/header-abi-dumper")
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800236
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700237 // -w has been added since header-abi-dumper does not need to produce any sort of diagnostic information.
Colin Cross77cdcfd2021-03-12 11:28:25 -0800238 sAbiDump, sAbiDumpRE = pctx.RemoteStaticRules("sAbiDump",
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800239 blueprint.RuleParams{
Ramy Medhat31ec9422020-04-17 15:03:58 -0400240 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 -0800241 CommandDeps: []string{"$sAbiDumper"},
Ramy Medhat31ec9422020-04-17 15:03:58 -0400242 }, &remoteexec.REParams{
243 Labels: map[string]string{"type": "abi-dump", "tool": "header-abi-dumper"},
244 ExecStrategy: "${config.REAbiDumperExecStrategy}",
Ulf Adams18931762020-11-25 22:54:35 +0100245 Inputs: []string{"$sAbiLinkerLibs"},
Ramy Medhat31ec9422020-04-17 15:03:58 -0400246 Platform: map[string]string{
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500247 remoteexec.PoolKey: "${config.RECXXPool}",
Ramy Medhat31ec9422020-04-17 15:03:58 -0400248 },
249 }, []string{"cFlags", "exportDirs"}, nil)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800250
Jayant Chowdharya4c6df52018-02-20 12:36:51 -0800251 _ = pctx.SourcePathVariable("sAbiLinker", "prebuilts/clang-tools/${config.HostPrebuiltTag}/bin/header-abi-linker")
Ramy Medhat808594c2020-05-07 06:56:47 -0400252 _ = pctx.SourcePathVariable("sAbiLinkerLibs", "prebuilts/clang-tools/${config.HostPrebuiltTag}/lib64")
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800253
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500254 // Rule to combine .dump sAbi dump files from multiple source files into a single .ldump
255 // sAbi dump file.
Colin Cross77cdcfd2021-03-12 11:28:25 -0800256 sAbiLink, sAbiLinkRE = pctx.RemoteStaticRules("sAbiLink",
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800257 blueprint.RuleParams{
Ramy Medhat808594c2020-05-07 06:56:47 -0400258 Command: "$reTemplate$sAbiLinker -o ${out} $symbolFilter -arch $arch $exportedHeaderFlags @${out}.rsp ",
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800259 CommandDeps: []string{"$sAbiLinker"},
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800260 Rspfile: "${out}.rsp",
261 RspfileContent: "${in}",
Ramy Medhat808594c2020-05-07 06:56:47 -0400262 }, &remoteexec.REParams{
263 Labels: map[string]string{"type": "tool", "name": "abi-linker"},
264 ExecStrategy: "${config.REAbiLinkerExecStrategy}",
Ramy Medhat6797edc2020-08-28 14:21:55 -0400265 Inputs: []string{"$sAbiLinkerLibs", "${out}.rsp", "$implicitInputs"},
Colin Crossa4eafdd2021-03-24 14:09:28 -0700266 RSPFiles: []string{"${out}.rsp"},
Ramy Medhat808594c2020-05-07 06:56:47 -0400267 OutputFiles: []string{"$out"},
268 ToolchainInputs: []string{"$sAbiLinker"},
269 Platform: map[string]string{remoteexec.PoolKey: "${config.RECXXPool}"},
Ramy Medhat6797edc2020-08-28 14:21:55 -0400270 }, []string{"symbolFilter", "arch", "exportedHeaderFlags"}, []string{"implicitInputs"})
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800271
Jayant Chowdharya4c6df52018-02-20 12:36:51 -0800272 _ = pctx.SourcePathVariable("sAbiDiffer", "prebuilts/clang-tools/${config.HostPrebuiltTag}/bin/header-abi-diff")
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700273
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500274 // Rule to compare linked sAbi dump files (.ldump).
Colin Cross2e2dbc22019-09-25 13:31:46 -0700275 sAbiDiff = pctx.RuleFunc("sAbiDiff",
Dan Willemsen54daaf02018-03-12 13:24:09 -0700276 func(ctx android.PackageRuleContext) blueprint.RuleParams {
Logan Chien2a65dda2019-10-01 15:58:07 -0700277 commandStr := "($sAbiDiffer ${extraFlags} -lib ${libName} -arch ${arch} -o ${out} -new ${in} -old ${referenceDump})"
Logan Chien6227fed2019-02-18 13:12:21 +0800278 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 +0800279 commandStr += " && (mkdir -p $$DIST_DIR/abidiffs && cp ${out} $$DIST_DIR/abidiffs/)"
Jayant Chowdharyd8b70a32018-02-01 17:23:09 -0800280 commandStr += " && exit 1)"
Jayant Chowdhary219139d2017-11-27 14:52:21 -0800281 return blueprint.RuleParams{
282 Command: commandStr,
283 CommandDeps: []string{"$sAbiDiffer"},
Dan Willemsen54daaf02018-03-12 13:24:09 -0700284 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800285 },
Logan Chien2a65dda2019-10-01 15:58:07 -0700286 "extraFlags", "referenceDump", "libName", "arch", "createReferenceDumpFlags")
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700287
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500288 // Rule to unzip a reference abi dump.
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700289 unzipRefSAbiDump = pctx.AndroidStaticRule("unzipRefSAbiDump",
290 blueprint.RuleParams{
291 Command: "gunzip -c $in > $out",
292 })
Oliver Nguyenc7434142019-04-24 14:22:25 -0700293
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500294 // Rule to zip files.
Oliver Nguyenc7434142019-04-24 14:22:25 -0700295 zip = pctx.AndroidStaticRule("zip",
296 blueprint.RuleParams{
Colin Cross053fca12020-08-19 13:51:47 -0700297 Command: "${SoongZipCmd} -o ${out} -C $$OUT_DIR -r ${out}.rsp",
Oliver Nguyenc7434142019-04-24 14:22:25 -0700298 CommandDeps: []string{"${SoongZipCmd}"},
Colin Cross053fca12020-08-19 13:51:47 -0700299 Rspfile: "${out}.rsp",
Oliver Nguyenc7434142019-04-24 14:22:25 -0700300 RspfileContent: "$in",
301 })
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800302
303 _ = pctx.SourcePathVariable("cxxExtractor",
304 "prebuilts/clang-tools/${config.HostPrebuiltTag}/bin/cxx_extractor")
Sasha Smundak65143642019-09-26 20:14:28 -0700305 _ = pctx.SourcePathVariable("kytheVnames", "build/soong/vnames.json")
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800306 _ = pctx.VariableFunc("kytheCorpus",
307 func(ctx android.PackageVarContext) string { return ctx.Config().XrefCorpusName() })
Sasha Smundak6c2d4f92020-01-09 17:34:23 -0800308 _ = pctx.VariableFunc("kytheCuEncoding",
309 func(ctx android.PackageVarContext) string { return ctx.Config().XrefCuEncoding() })
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500310
311 // Rule to use kythe extractors to generate .kzip files, used to build code cross references.
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800312 kytheExtract = pctx.StaticRule("kythe",
313 blueprint.RuleParams{
Sasha Smundak6c2d4f92020-01-09 17:34:23 -0800314 Command: `rm -f $out && ` +
Sasha Smundaka4ef83b2020-04-21 17:08:35 -0700315 `KYTHE_CORPUS=${kytheCorpus} ` +
316 `KYTHE_OUTPUT_FILE=$out ` +
317 `KYTHE_VNAMES=$kytheVnames ` +
318 `KYTHE_KZIP_ENCODING=${kytheCuEncoding} ` +
319 `KYTHE_CANONICALIZE_VNAME_PATHS=prefer-relative ` +
Sasha Smundak6c2d4f92020-01-09 17:34:23 -0800320 `$cxxExtractor $cFlags $in `,
Sasha Smundak65143642019-09-26 20:14:28 -0700321 CommandDeps: []string{"$cxxExtractor", "$kytheVnames"},
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800322 },
323 "cFlags")
Colin Cross3f40fa42015-01-30 17:27:36 -0800324)
325
Ivan Lozanof3717ee2020-05-20 09:03:20 -0400326func PwdPrefix() string {
327 // Darwin doesn't have /proc
328 if runtime.GOOS != "darwin" {
329 return "PWD=/proc/self/cwd"
330 }
331 return ""
332}
333
Dan Willemsen322a0a62015-11-17 15:19:46 -0800334func init() {
335 // We run gcc/clang with PWD=/proc/self/cwd to remove $TOP from the
336 // debug output. That way two builds in two different directories will
337 // create the same output.
Ivan Lozanof3717ee2020-05-20 09:03:20 -0400338 pctx.StaticVariable("relPwd", PwdPrefix())
Oliver Nguyenc7434142019-04-24 14:22:25 -0700339
340 pctx.HostBinToolVariable("SoongZipCmd", "soong_zip")
Dan Willemsen322a0a62015-11-17 15:19:46 -0800341}
342
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500343// builderFlags contains various types of command line flags (and settings) for use in building
344// build statements related to C++.
Colin Cross3f40fa42015-01-30 17:27:36 -0800345type builderFlags struct {
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500346 // Global flags (which build system or toolchain is responsible for). These are separate from
347 // local flags because they should appear first (so that they may be overridden by local flags).
Colin Cross6d88dba2019-11-06 07:06:58 -0800348 globalCommonFlags string
349 globalAsFlags string
350 globalYasmFlags string
351 globalCFlags string
352 globalToolingCFlags string // A separate set of cFlags for clang LibTooling tools
353 globalToolingCppFlags string // A separate set of cppFlags for clang LibTooling tools
354 globalConlyFlags string
355 globalCppFlags string
356 globalLdFlags string
357
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500358 // Local flags (which individual modules are responsible for). These may override global flags.
Colin Cross6d88dba2019-11-06 07:06:58 -0800359 localCommonFlags string
360 localAsFlags string
361 localYasmFlags string
362 localCFlags string
363 localToolingCFlags string // A separate set of cFlags for clang LibTooling tools
364 localToolingCppFlags string // A separate set of cppFlags for clang LibTooling tools
365 localConlyFlags string
366 localCppFlags string
367 localLdFlags string
368
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500369 libFlags string // Flags to add to the linker directly after specifying libraries to link.
370 extraLibFlags string // Flags to add to the linker last.
371 tidyFlags string // Flags that apply to clang-tidy
372 sAbiFlags string // Flags that apply to header-abi-dumps
373 aidlFlags string // Flags that apply to aidl source files
374 rsFlags string // Flags that apply to renderscript source files
Colin Cross6d88dba2019-11-06 07:06:58 -0800375 toolchain config.Toolchain
Colin Cross665dce92016-04-28 14:50:03 -0700376
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500377 // True if these extra features are enabled.
378 tidy bool
379 gcovCoverage bool
380 sAbiDump bool
381 emitXrefs bool
382
383 assemblerWithCpp bool // True if .s files should be processed with the c preprocessor.
Dan Willemsen98ab3112019-08-27 21:20:40 -0700384
Colin Crossc3199482017-03-30 15:03:04 -0700385 systemIncludeFlags string
386
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500387 // True if static libraries should be grouped (using `-Wl,--start-group` and `-Wl,--end-group`).
Colin Cross18c0c5a2016-12-01 14:45:23 -0800388 groupStaticLibs bool
389
Colin Cross19878da2019-03-28 14:45:07 -0700390 proto android.ProtoFlags
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500391 protoC bool // If true, compile protos as `.c` files. Otherwise, output as `.cc`.
392 protoOptionsFile bool // If true, output a proto options file.
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700393
394 yacc *YaccProperties
Matthias Maennich22fd4d12020-07-15 10:58:56 +0200395 lex *LexProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800396}
397
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500398// StripFlags represents flags related to stripping. This is separate from builderFlags, as these
399// flags are useful outside of this package (such as for Rust).
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200400type StripFlags struct {
401 Toolchain config.Toolchain
402 StripKeepSymbols bool
403 StripKeepSymbolsList string
404 StripKeepSymbolsAndDebugFrame bool
405 StripKeepMiniDebugInfo bool
406 StripAddGnuDebuglink bool
407 StripUseGnuStrip bool
408}
409
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500410// Objects is a collection of file paths corresponding to outputs for C++ related build statements.
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700411type Objects struct {
Dan Willemsen581341d2017-02-09 16:16:31 -0800412 objFiles android.Paths
413 tidyFiles android.Paths
414 coverageFiles android.Paths
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800415 sAbiDumpFiles android.Paths
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800416 kytheFiles android.Paths
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700417}
418
419func (a Objects) Copy() Objects {
420 return Objects{
Dan Willemsen581341d2017-02-09 16:16:31 -0800421 objFiles: append(android.Paths{}, a.objFiles...),
422 tidyFiles: append(android.Paths{}, a.tidyFiles...),
423 coverageFiles: append(android.Paths{}, a.coverageFiles...),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800424 sAbiDumpFiles: append(android.Paths{}, a.sAbiDumpFiles...),
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800425 kytheFiles: append(android.Paths{}, a.kytheFiles...),
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700426 }
427}
428
429func (a Objects) Append(b Objects) Objects {
430 return Objects{
Dan Willemsen581341d2017-02-09 16:16:31 -0800431 objFiles: append(a.objFiles, b.objFiles...),
432 tidyFiles: append(a.tidyFiles, b.tidyFiles...),
433 coverageFiles: append(a.coverageFiles, b.coverageFiles...),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800434 sAbiDumpFiles: append(a.sAbiDumpFiles, b.sAbiDumpFiles...),
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800435 kytheFiles: append(a.kytheFiles, b.kytheFiles...),
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700436 }
437}
438
Colin Cross3f40fa42015-01-30 17:27:36 -0800439// Generate rules for compiling multiple .c, .cpp, or .S files to individual .o files
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500440func transformSourceToObj(ctx android.ModuleContext, subdir string, srcFiles android.Paths,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800441 flags builderFlags, pathDeps android.Paths, cFlagsDeps android.Paths) Objects {
Colin Cross581c1892015-04-07 16:50:10 -0700442
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500443 // Source files are one-to-one with tidy, coverage, or kythe files, if enabled.
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700444 objFiles := make(android.Paths, len(srcFiles))
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700445 var tidyFiles android.Paths
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700446 if flags.tidy {
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700447 tidyFiles = make(android.Paths, 0, len(srcFiles))
448 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800449 var coverageFiles android.Paths
Oliver Nguyen04526782020-04-21 12:40:27 -0700450 if flags.gcovCoverage {
Dan Willemsen581341d2017-02-09 16:16:31 -0800451 coverageFiles = make(android.Paths, 0, len(srcFiles))
452 }
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800453 var kytheFiles android.Paths
454 if flags.emitXrefs {
455 kytheFiles = make(android.Paths, 0, len(srcFiles))
456 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800457
Colin Cross6d88dba2019-11-06 07:06:58 -0800458 // Produce fully expanded flags for use by C tools, C compiles, C++ tools, C++ compiles, and asm compiles
459 // respectively.
460 toolingCflags := flags.globalCommonFlags + " " +
461 flags.globalToolingCFlags + " " +
462 flags.globalConlyFlags + " " +
463 flags.localCommonFlags + " " +
464 flags.localToolingCFlags + " " +
465 flags.localConlyFlags + " " +
466 flags.systemIncludeFlags
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700467
Colin Cross6d88dba2019-11-06 07:06:58 -0800468 cflags := flags.globalCommonFlags + " " +
469 flags.globalCFlags + " " +
470 flags.globalConlyFlags + " " +
471 flags.localCommonFlags + " " +
472 flags.localCFlags + " " +
473 flags.localConlyFlags + " " +
474 flags.systemIncludeFlags
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700475
Colin Cross6d88dba2019-11-06 07:06:58 -0800476 toolingCppflags := flags.globalCommonFlags + " " +
477 flags.globalToolingCFlags + " " +
478 flags.globalToolingCppFlags + " " +
479 flags.localCommonFlags + " " +
480 flags.localToolingCFlags + " " +
481 flags.localToolingCppFlags + " " +
482 flags.systemIncludeFlags
Colin Crossc3199482017-03-30 15:03:04 -0700483
Colin Cross6d88dba2019-11-06 07:06:58 -0800484 cppflags := flags.globalCommonFlags + " " +
485 flags.globalCFlags + " " +
486 flags.globalCppFlags + " " +
487 flags.localCommonFlags + " " +
488 flags.localCFlags + " " +
489 flags.localCppFlags + " " +
490 flags.systemIncludeFlags
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700491
Colin Cross6d88dba2019-11-06 07:06:58 -0800492 asflags := flags.globalCommonFlags + " " +
493 flags.globalAsFlags + " " +
494 flags.localCommonFlags + " " +
495 flags.localAsFlags + " " +
496 flags.systemIncludeFlags
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700497
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800498 var sAbiDumpFiles android.Paths
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700499 if flags.sAbiDump {
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800500 sAbiDumpFiles = make(android.Paths, 0, len(srcFiles))
501 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800502
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700503 cflags += " ${config.NoOverrideClangGlobalCflags}"
504 toolingCflags += " ${config.NoOverrideClangGlobalCflags}"
505 cppflags += " ${config.NoOverrideClangGlobalCflags}"
506 toolingCppflags += " ${config.NoOverrideClangGlobalCflags}"
Dan Willemsenbe03f342016-03-03 17:21:04 -0800507
Colin Cross3f40fa42015-01-30 17:27:36 -0800508 for i, srcFile := range srcFiles {
Dan Willemsen21ec4902016-11-02 20:43:13 -0700509 objFile := android.ObjPathWithExt(ctx, subdir, srcFile, "o")
Colin Cross3f40fa42015-01-30 17:27:36 -0800510
511 objFiles[i] = objFile
512
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500513 // Register compilation build statements. The actual rule used depends on the source file type.
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700514 switch srcFile.Ext() {
515 case ".asm":
Colin Crossae887032017-10-23 17:16:14 -0700516 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700517 Rule: yasm,
518 Description: "yasm " + srcFile.Rel(),
519 Output: objFile,
520 Input: srcFile,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800521 Implicits: cFlagsDeps,
522 OrderOnly: pathDeps,
Colin Cross91e90042016-12-02 17:13:24 -0800523 Args: map[string]string{
Colin Cross6d88dba2019-11-06 07:06:58 -0800524 "asFlags": flags.globalYasmFlags + " " + flags.localYasmFlags,
Colin Cross91e90042016-12-02 17:13:24 -0800525 },
526 })
527 continue
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700528 case ".rc":
Colin Crossae887032017-10-23 17:16:14 -0700529 ctx.Build(pctx, android.BuildParams{
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700530 Rule: windres,
531 Description: "windres " + srcFile.Rel(),
532 Output: objFile,
533 Input: srcFile,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800534 Implicits: cFlagsDeps,
535 OrderOnly: pathDeps,
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700536 Args: map[string]string{
Elliott Hughes5f314b12021-05-18 11:56:02 -0700537 "windresCmd": mingwCmd(flags.toolchain, "windres"),
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700538 "flags": flags.toolchain.WindresFlags(),
539 },
540 })
541 continue
Pete Bentleyfcf55bf2019-08-16 20:14:32 +0100542 case ".o":
543 objFiles[i] = srcFile
544 continue
Colin Cross91e90042016-12-02 17:13:24 -0800545 }
546
Colin Cross6d88dba2019-11-06 07:06:58 -0800547 var moduleFlags string
548 var moduleToolingFlags string
549
Colin Cross3f40fa42015-01-30 17:27:36 -0800550 var ccCmd string
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700551 tidy := flags.tidy
Oliver Nguyen04526782020-04-21 12:40:27 -0700552 coverage := flags.gcovCoverage
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700553 dump := flags.sAbiDump
Dan Willemsenfcabb1c2019-01-03 23:25:11 -0800554 rule := cc
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800555 emitXref := flags.emitXrefs
Colin Cross3f40fa42015-01-30 17:27:36 -0800556
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700557 switch srcFile.Ext() {
Dan Willemsenfcabb1c2019-01-03 23:25:11 -0800558 case ".s":
Dan Willemsen98ab3112019-08-27 21:20:40 -0700559 if !flags.assemblerWithCpp {
560 rule = ccNoDeps
561 }
Dan Willemsenfcabb1c2019-01-03 23:25:11 -0800562 fallthrough
563 case ".S":
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700564 ccCmd = "clang"
Colin Cross6d88dba2019-11-06 07:06:58 -0800565 moduleFlags = asflags
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700566 tidy = false
Dan Willemsen581341d2017-02-09 16:16:31 -0800567 coverage = false
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800568 dump = false
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800569 emitXref = false
Colin Cross3f40fa42015-01-30 17:27:36 -0800570 case ".c":
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700571 ccCmd = "clang"
Colin Cross6d88dba2019-11-06 07:06:58 -0800572 moduleFlags = cflags
573 moduleToolingFlags = toolingCflags
Colin Crossd34ab7c2019-06-27 14:46:10 -0700574 case ".cpp", ".cc", ".cxx", ".mm":
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700575 ccCmd = "clang++"
Colin Cross6d88dba2019-11-06 07:06:58 -0800576 moduleFlags = cppflags
577 moduleToolingFlags = toolingCppflags
Liz Kammere2861132020-12-03 22:12:41 +0000578 case ".h", ".hpp":
579 ctx.PropertyErrorf("srcs", "Header file %s is not supported, instead use export_include_dirs or local_include_dirs.", srcFile)
580 continue
Colin Cross3f40fa42015-01-30 17:27:36 -0800581 default:
Liz Kammere2861132020-12-03 22:12:41 +0000582 ctx.PropertyErrorf("srcs", "File %s has unknown extension. Supported extensions: .s, .S, .c, .cpp, .cc, .cxx, .mm", srcFile)
Colin Cross3f40fa42015-01-30 17:27:36 -0800583 continue
584 }
585
Colin Cross67a5c132017-05-09 13:45:28 -0700586 ccDesc := ccCmd
587
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700588 ccCmd = "${config.ClangBin}/" + ccCmd
Colin Cross3f40fa42015-01-30 17:27:36 -0800589
Dan Willemsen581341d2017-02-09 16:16:31 -0800590 var implicitOutputs android.WritablePaths
591 if coverage {
592 gcnoFile := android.ObjPathWithExt(ctx, subdir, srcFile, "gcno")
593 implicitOutputs = append(implicitOutputs, gcnoFile)
594 coverageFiles = append(coverageFiles, gcnoFile)
595 }
596
Colin Crossae887032017-10-23 17:16:14 -0700597 ctx.Build(pctx, android.BuildParams{
Dan Willemsenfcabb1c2019-01-03 23:25:11 -0800598 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -0700599 Description: ccDesc + " " + srcFile.Rel(),
Dan Willemsen581341d2017-02-09 16:16:31 -0800600 Output: objFile,
601 ImplicitOutputs: implicitOutputs,
602 Input: srcFile,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800603 Implicits: cFlagsDeps,
604 OrderOnly: pathDeps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800605 Args: map[string]string{
Colin Cross6d88dba2019-11-06 07:06:58 -0800606 "cFlags": moduleFlags,
Colin Cross28344522015-04-22 13:07:53 -0700607 "ccCmd": ccCmd,
Colin Cross3f40fa42015-01-30 17:27:36 -0800608 },
609 })
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700610
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500611 // Register post-process build statements (such as for tidy or kythe).
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800612 if emitXref {
613 kytheFile := android.ObjPathWithExt(ctx, subdir, srcFile, "kzip")
614 ctx.Build(pctx, android.BuildParams{
615 Rule: kytheExtract,
616 Description: "Xref C++ extractor " + srcFile.Rel(),
617 Output: kytheFile,
618 Input: srcFile,
619 Implicits: cFlagsDeps,
620 OrderOnly: pathDeps,
621 Args: map[string]string{
Colin Cross6d88dba2019-11-06 07:06:58 -0800622 "cFlags": moduleFlags,
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800623 },
624 })
625 kytheFiles = append(kytheFiles, kytheFile)
626 }
627
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700628 if tidy {
Dan Willemsen21ec4902016-11-02 20:43:13 -0700629 tidyFile := android.ObjPathWithExt(ctx, subdir, srcFile, "tidy")
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700630 tidyFiles = append(tidyFiles, tidyFile)
631
Kousik Kumar4e30bba2020-06-18 09:17:51 -0700632 rule := clangTidy
Ramy Medhat16f23a42020-09-03 01:29:49 -0400633 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_CLANG_TIDY") {
Kousik Kumar4e30bba2020-06-18 09:17:51 -0700634 rule = clangTidyRE
635 }
636
Colin Crossae887032017-10-23 17:16:14 -0700637 ctx.Build(pctx, android.BuildParams{
Kousik Kumar4e30bba2020-06-18 09:17:51 -0700638 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -0700639 Description: "clang-tidy " + srcFile.Rel(),
640 Output: tidyFile,
641 Input: srcFile,
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700642 // We must depend on objFile, since clang-tidy doesn't
643 // support exporting dependencies.
Dan Willemsen6b4419c2019-08-09 12:45:53 -0700644 Implicit: objFile,
645 Implicits: cFlagsDeps,
646 OrderOnly: pathDeps,
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700647 Args: map[string]string{
Colin Cross6d88dba2019-11-06 07:06:58 -0800648 "cFlags": moduleToolingFlags,
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700649 "tidyFlags": flags.tidyFlags,
650 },
651 })
652 }
653
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800654 if dump {
655 sAbiDumpFile := android.ObjPathWithExt(ctx, subdir, srcFile, "sdump")
656 sAbiDumpFiles = append(sAbiDumpFiles, sAbiDumpFile)
657
Ramy Medhat31ec9422020-04-17 15:03:58 -0400658 dumpRule := sAbiDump
Ramy Medhat16f23a42020-09-03 01:29:49 -0400659 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_ABI_DUMPER") {
Ramy Medhat31ec9422020-04-17 15:03:58 -0400660 dumpRule = sAbiDumpRE
661 }
Colin Crossae887032017-10-23 17:16:14 -0700662 ctx.Build(pctx, android.BuildParams{
Ramy Medhat31ec9422020-04-17 15:03:58 -0400663 Rule: dumpRule,
Colin Cross67a5c132017-05-09 13:45:28 -0700664 Description: "header-abi-dumper " + srcFile.Rel(),
665 Output: sAbiDumpFile,
666 Input: srcFile,
667 Implicit: objFile,
Dan Willemsen6b4419c2019-08-09 12:45:53 -0700668 Implicits: cFlagsDeps,
669 OrderOnly: pathDeps,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800670 Args: map[string]string{
Colin Cross6d88dba2019-11-06 07:06:58 -0800671 "cFlags": moduleToolingFlags,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800672 "exportDirs": flags.sAbiFlags,
673 },
674 })
675 }
676
Colin Cross3f40fa42015-01-30 17:27:36 -0800677 }
678
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700679 return Objects{
Dan Willemsen581341d2017-02-09 16:16:31 -0800680 objFiles: objFiles,
681 tidyFiles: tidyFiles,
682 coverageFiles: coverageFiles,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800683 sAbiDumpFiles: sAbiDumpFiles,
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800684 kytheFiles: kytheFiles,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700685 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800686}
687
688// Generate a rule for compiling multiple .o files to a static library (.a)
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500689func transformObjToStaticLib(ctx android.ModuleContext,
Martin Stjernholm391d94c2020-04-17 17:34:31 +0100690 objFiles android.Paths, wholeStaticLibs android.Paths,
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700691 flags builderFlags, outputFile android.ModuleOutPath, deps android.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800692
Stephen Hinesf1addeb2018-01-09 23:29:04 -0800693 arCmd := "${config.ClangBin}/llvm-ar"
Martin Stjernholm391d94c2020-04-17 17:34:31 +0100694 arFlags := ""
Stephen Hinesf1addeb2018-01-09 23:29:04 -0800695 if !ctx.Darwin() {
696 arFlags += " -format=gnu"
697 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800698
Martin Stjernholm391d94c2020-04-17 17:34:31 +0100699 if len(wholeStaticLibs) == 0 {
700 ctx.Build(pctx, android.BuildParams{
701 Rule: ar,
702 Description: "static link " + outputFile.Base(),
703 Output: outputFile,
704 Inputs: objFiles,
705 Implicits: deps,
706 Args: map[string]string{
707 "arFlags": "crsPD" + arFlags,
708 "arCmd": arCmd,
709 },
710 })
711
712 } else {
713 ctx.Build(pctx, android.BuildParams{
714 Rule: arWithLibs,
715 Description: "static link " + outputFile.Base(),
716 Output: outputFile,
717 Inputs: append(objFiles, wholeStaticLibs...),
718 Implicits: deps,
719 Args: map[string]string{
720 "arCmd": arCmd,
721 "arObjFlags": "crsPD" + arFlags,
722 "arObjs": strings.Join(objFiles.Strings(), " "),
723 "arLibFlags": "cqsL" + arFlags,
724 "arLibs": strings.Join(wholeStaticLibs.Strings(), " "),
725 },
726 })
727 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800728}
729
730// Generate a rule for compiling multiple .o files, plus static libraries, whole static libraries,
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700731// and shared libraries, to a shared library (.so) or dynamic executable
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500732func transformObjToDynamicBinary(ctx android.ModuleContext,
Colin Crossc465efd2021-06-11 18:00:04 -0700733 objFiles, sharedLibs, staticLibs, lateStaticLibs, wholeStaticLibs, deps, crtBegin, crtEnd android.Paths,
734 groupLate bool, flags builderFlags, outputFile android.WritablePath,
735 implicitOutputs android.WritablePaths, validations android.WritablePaths) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800736
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700737 ldCmd := "${config.ClangBin}/clang++"
Colin Cross3f40fa42015-01-30 17:27:36 -0800738
Colin Cross3f40fa42015-01-30 17:27:36 -0800739 var libFlagsList []string
740
Colin Cross16b23492016-01-06 14:41:07 -0800741 if len(flags.libFlags) > 0 {
742 libFlagsList = append(libFlagsList, flags.libFlags)
743 }
744
Colin Cross3f40fa42015-01-30 17:27:36 -0800745 if len(wholeStaticLibs) > 0 {
Dan Willemsen490fd492015-11-24 17:53:15 -0800746 if ctx.Host() && ctx.Darwin() {
Colin Cross635c3b02016-05-18 15:37:25 -0700747 libFlagsList = append(libFlagsList, android.JoinWithPrefix(wholeStaticLibs.Strings(), "-force_load "))
Colin Cross0af4b842015-04-30 16:36:18 -0700748 } else {
749 libFlagsList = append(libFlagsList, "-Wl,--whole-archive ")
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700750 libFlagsList = append(libFlagsList, wholeStaticLibs.Strings()...)
Colin Cross0af4b842015-04-30 16:36:18 -0700751 libFlagsList = append(libFlagsList, "-Wl,--no-whole-archive ")
752 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800753 }
754
Colin Cross7a7cf972016-12-05 18:47:39 -0800755 if flags.groupStaticLibs && !ctx.Darwin() && len(staticLibs) > 0 {
Colin Cross18c0c5a2016-12-01 14:45:23 -0800756 libFlagsList = append(libFlagsList, "-Wl,--start-group")
757 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700758 libFlagsList = append(libFlagsList, staticLibs.Strings()...)
Colin Cross7a7cf972016-12-05 18:47:39 -0800759 if flags.groupStaticLibs && !ctx.Darwin() && len(staticLibs) > 0 {
Colin Cross18c0c5a2016-12-01 14:45:23 -0800760 libFlagsList = append(libFlagsList, "-Wl,--end-group")
761 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800762
Stephen Hines10347862016-07-18 15:54:54 -0700763 if groupLate && !ctx.Darwin() && len(lateStaticLibs) > 0 {
Dan Willemsenedc385f2015-07-08 13:02:23 -0700764 libFlagsList = append(libFlagsList, "-Wl,--start-group")
765 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700766 libFlagsList = append(libFlagsList, lateStaticLibs.Strings()...)
Stephen Hines10347862016-07-18 15:54:54 -0700767 if groupLate && !ctx.Darwin() && len(lateStaticLibs) > 0 {
Dan Willemsenedc385f2015-07-08 13:02:23 -0700768 libFlagsList = append(libFlagsList, "-Wl,--end-group")
769 }
770
Colin Cross3f40fa42015-01-30 17:27:36 -0800771 for _, lib := range sharedLibs {
Josh Gao75a50a22019-06-07 17:58:59 -0700772 libFile := lib.String()
773 if ctx.Windows() {
774 libFile = pathtools.ReplaceExtension(libFile, "lib")
775 }
776 libFlagsList = append(libFlagsList, libFile)
Colin Cross3f40fa42015-01-30 17:27:36 -0800777 }
778
Colin Cross3f40fa42015-01-30 17:27:36 -0800779 deps = append(deps, staticLibs...)
Colin Cross3075ad02015-03-17 10:47:08 -0700780 deps = append(deps, lateStaticLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800781 deps = append(deps, wholeStaticLibs...)
Colin Crossc465efd2021-06-11 18:00:04 -0700782 deps = append(deps, crtBegin...)
783 deps = append(deps, crtEnd...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800784
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400785 rule := ld
Kousik Kumar3fb61262020-04-22 13:31:09 -0700786 args := map[string]string{
787 "ldCmd": ldCmd,
Colin Crossc465efd2021-06-11 18:00:04 -0700788 "crtBegin": strings.Join(crtBegin.Strings(), " "),
Kousik Kumar3fb61262020-04-22 13:31:09 -0700789 "libFlags": strings.Join(libFlagsList, " "),
790 "extraLibFlags": flags.extraLibFlags,
791 "ldFlags": flags.globalLdFlags + " " + flags.localLdFlags,
Colin Crossc465efd2021-06-11 18:00:04 -0700792 "crtEnd": strings.Join(crtEnd.Strings(), " "),
Kousik Kumar3fb61262020-04-22 13:31:09 -0700793 }
Ramy Medhat16f23a42020-09-03 01:29:49 -0400794 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_CXX_LINKS") {
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400795 rule = ldRE
Kousik Kumar3fb61262020-04-22 13:31:09 -0700796 args["implicitOutputs"] = strings.Join(implicitOutputs.Strings(), ",")
Ramy Medhat6797edc2020-08-28 14:21:55 -0400797 args["implicitInputs"] = strings.Join(deps.Strings(), ",")
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400798 }
799
Colin Crossae887032017-10-23 17:16:14 -0700800 ctx.Build(pctx, android.BuildParams{
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400801 Rule: rule,
Josh Gao75a50a22019-06-07 17:58:59 -0700802 Description: "link " + outputFile.Base(),
803 Output: outputFile,
804 ImplicitOutputs: implicitOutputs,
805 Inputs: objFiles,
806 Implicits: deps,
Colin Cross58dd6a92021-05-13 17:45:36 -0700807 OrderOnly: sharedLibs,
Colin Crossf04eb992021-06-11 15:22:41 -0700808 Validations: validations.Paths(),
Kousik Kumar3fb61262020-04-22 13:31:09 -0700809 Args: args,
Colin Cross3f40fa42015-01-30 17:27:36 -0800810 })
811}
812
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800813// Generate a rule to combine .dump sAbi dump files from multiple source files
814// into a single .ldump sAbi dump file
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500815func transformDumpToLinkedDump(ctx android.ModuleContext, sAbiDumps android.Paths, soFile android.Path,
Logan Chiene3d7a0d2019-01-17 00:18:02 +0800816 baseName, exportedHeaderFlags string, symbolFile android.OptionalPath,
817 excludedSymbolVersions, excludedSymbolTags []string) android.OptionalPath {
818
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800819 outputFile := android.PathForModuleOut(ctx, baseName+".lsdump")
Logan Chiene3d7a0d2019-01-17 00:18:02 +0800820
821 implicits := android.Paths{soFile}
Jayant Chowdharydf344d52018-01-17 11:11:42 -0800822 symbolFilterStr := "-so " + soFile.String()
Logan Chiene3d7a0d2019-01-17 00:18:02 +0800823
824 if symbolFile.Valid() {
825 implicits = append(implicits, symbolFile.Path())
826 symbolFilterStr += " -v " + symbolFile.String()
827 }
828 for _, ver := range excludedSymbolVersions {
829 symbolFilterStr += " --exclude-symbol-version " + ver
830 }
831 for _, tag := range excludedSymbolTags {
832 symbolFilterStr += " --exclude-symbol-tag " + tag
833 }
Ramy Medhat808594c2020-05-07 06:56:47 -0400834 rule := sAbiLink
835 args := map[string]string{
836 "symbolFilter": symbolFilterStr,
837 "arch": ctx.Arch().ArchType.Name,
838 "exportedHeaderFlags": exportedHeaderFlags,
839 }
Ramy Medhat16f23a42020-09-03 01:29:49 -0400840 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_ABI_LINKER") {
Ramy Medhat808594c2020-05-07 06:56:47 -0400841 rule = sAbiLinkRE
842 rbeImplicits := implicits.Strings()
843 for _, p := range strings.Split(exportedHeaderFlags, " ") {
844 if len(p) > 2 {
845 // Exclude the -I prefix.
846 rbeImplicits = append(rbeImplicits, p[2:])
847 }
848 }
Ramy Medhat6797edc2020-08-28 14:21:55 -0400849 args["implicitInputs"] = strings.Join(rbeImplicits, ",")
Ramy Medhat808594c2020-05-07 06:56:47 -0400850 }
Colin Crossae887032017-10-23 17:16:14 -0700851 ctx.Build(pctx, android.BuildParams{
Ramy Medhat808594c2020-05-07 06:56:47 -0400852 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -0700853 Description: "header-abi-linker " + outputFile.Base(),
854 Output: outputFile,
855 Inputs: sAbiDumps,
Logan Chiene3d7a0d2019-01-17 00:18:02 +0800856 Implicits: implicits,
Ramy Medhat808594c2020-05-07 06:56:47 -0400857 Args: args,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800858 })
859 return android.OptionalPathForPath(outputFile)
860}
861
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500862// unzipRefDump registers a build statement to unzip a reference abi dump.
863func unzipRefDump(ctx android.ModuleContext, zippedRefDump android.Path, baseName string) android.Path {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700864 outputFile := android.PathForModuleOut(ctx, baseName+"_ref.lsdump")
Colin Crossae887032017-10-23 17:16:14 -0700865 ctx.Build(pctx, android.BuildParams{
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700866 Rule: unzipRefSAbiDump,
867 Description: "gunzip" + outputFile.Base(),
868 Output: outputFile,
869 Input: zippedRefDump,
870 })
871 return outputFile
872}
873
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500874// sourceAbiDiff registers a build statement to compare linked sAbi dump files (.ldump).
875func sourceAbiDiff(ctx android.ModuleContext, inputDump android.Path, referenceDump android.Path,
Logan Chien2a65dda2019-10-01 15:58:07 -0700876 baseName, exportedHeaderFlags string, checkAllApis, isLlndk, isNdk, isVndkExt bool) android.OptionalPath {
Logan Chienf3511742017-10-31 18:04:35 +0800877
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800878 outputFile := android.PathForModuleOut(ctx, baseName+".abidiff")
Jayant Chowdharyc7434e22018-05-31 15:42:26 -0700879 libName := strings.TrimSuffix(baseName, filepath.Ext(baseName))
Logan Chien6227fed2019-02-18 13:12:21 +0800880 createReferenceDumpFlags := ""
881
Logan Chien2a65dda2019-10-01 15:58:07 -0700882 var extraFlags []string
883 if checkAllApis {
884 extraFlags = append(extraFlags, "-check-all-apis")
885 } else {
886 extraFlags = append(extraFlags,
887 "-allow-unreferenced-changes",
888 "-allow-unreferenced-elf-symbol-changes")
Jayant Chowdharye4499502018-01-17 13:13:33 -0800889 }
Logan Chien2a65dda2019-10-01 15:58:07 -0700890
891 if exportedHeaderFlags == "" {
892 extraFlags = append(extraFlags, "-advice-only")
893 }
894
Logan Chien62f1f942019-02-18 15:40:42 +0800895 if isLlndk || isNdk {
Logan Chien6227fed2019-02-18 13:12:21 +0800896 createReferenceDumpFlags = "--llndk"
Logan Chien62f1f942019-02-18 15:40:42 +0800897 if isLlndk {
898 // TODO(b/130324828): "-consider-opaque-types-different" should apply to
899 // both LLNDK and NDK shared libs. However, a known issue in header-abi-diff
900 // breaks libaaudio. Remove the if-guard after the issue is fixed.
Logan Chien2a65dda2019-10-01 15:58:07 -0700901 extraFlags = append(extraFlags, "-consider-opaque-types-different")
Logan Chien62f1f942019-02-18 15:40:42 +0800902 }
Jayant Chowdharyc7434e22018-05-31 15:42:26 -0700903 }
Logan Chienf3511742017-10-31 18:04:35 +0800904 if isVndkExt {
Logan Chien2a65dda2019-10-01 15:58:07 -0700905 extraFlags = append(extraFlags, "-allow-extensions")
Logan Chienf3511742017-10-31 18:04:35 +0800906 }
907
Colin Crossae887032017-10-23 17:16:14 -0700908 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700909 Rule: sAbiDiff,
910 Description: "header-abi-diff " + outputFile.Base(),
911 Output: outputFile,
912 Input: inputDump,
913 Implicit: referenceDump,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800914 Args: map[string]string{
Logan Chien6227fed2019-02-18 13:12:21 +0800915 "referenceDump": referenceDump.String(),
916 "libName": libName,
917 "arch": ctx.Arch().ArchType.Name,
Logan Chien2a65dda2019-10-01 15:58:07 -0700918 "extraFlags": strings.Join(extraFlags, " "),
Logan Chien6227fed2019-02-18 13:12:21 +0800919 "createReferenceDumpFlags": createReferenceDumpFlags,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800920 },
921 })
922 return android.OptionalPathForPath(outputFile)
923}
924
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700925// Generate a rule for extracting a table of contents from a shared library (.so)
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500926func transformSharedObjectToToc(ctx android.ModuleContext, inputFile android.Path,
Colin Cross26c34ed2016-09-30 17:10:16 -0700927 outputFile android.WritablePath, flags builderFlags) {
928
Colin Crossb496cfd2018-09-10 16:50:05 -0700929 var format string
Colin Crossb496cfd2018-09-10 16:50:05 -0700930 if ctx.Darwin() {
931 format = "--macho"
Colin Crossb496cfd2018-09-10 16:50:05 -0700932 } else if ctx.Windows() {
933 format = "--pe"
Colin Crossb496cfd2018-09-10 16:50:05 -0700934 } else {
935 format = "--elf"
Colin Crossb496cfd2018-09-10 16:50:05 -0700936 }
Colin Cross26c34ed2016-09-30 17:10:16 -0700937
Colin Crossae887032017-10-23 17:16:14 -0700938 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700939 Rule: toc,
940 Description: "generate toc " + inputFile.Base(),
941 Output: outputFile,
942 Input: inputFile,
Colin Cross26c34ed2016-09-30 17:10:16 -0700943 Args: map[string]string{
Yi Kong4ad44e72021-04-01 17:45:42 +0800944 "clangBin": "${config.ClangBin}",
945 "format": format,
Colin Cross26c34ed2016-09-30 17:10:16 -0700946 },
947 })
948}
949
Colin Cross3f40fa42015-01-30 17:27:36 -0800950// Generate a rule for compiling multiple .o files to a .o using ld partial linking
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500951func transformObjsToObj(ctx android.ModuleContext, objFiles android.Paths,
Dan Willemsen724ab5d2019-09-19 10:50:18 -0700952 flags builderFlags, outputFile android.WritablePath, deps android.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800953
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700954 ldCmd := "${config.ClangBin}/clang++"
Colin Cross3f40fa42015-01-30 17:27:36 -0800955
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400956 rule := partialLd
957 args := map[string]string{
958 "ldCmd": ldCmd,
959 "ldFlags": flags.globalLdFlags + " " + flags.localLdFlags,
960 }
Ramy Medhat16f23a42020-09-03 01:29:49 -0400961 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_CXX_LINKS") {
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400962 rule = partialLdRE
963 args["inCommaList"] = strings.Join(objFiles.Strings(), ",")
Ramy Medhat6797edc2020-08-28 14:21:55 -0400964 args["implicitInputs"] = strings.Join(deps.Strings(), ",")
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400965 }
Colin Crossae887032017-10-23 17:16:14 -0700966 ctx.Build(pctx, android.BuildParams{
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400967 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -0700968 Description: "link " + outputFile.Base(),
969 Output: outputFile,
970 Inputs: objFiles,
Dan Willemsen724ab5d2019-09-19 10:50:18 -0700971 Implicits: deps,
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400972 Args: args,
Colin Cross3f40fa42015-01-30 17:27:36 -0800973 })
974}
975
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500976// Generate a rule for running objcopy --prefix-symbols on a binary
977func transformBinaryPrefixSymbols(ctx android.ModuleContext, prefix string, inputFile android.Path,
Colin Cross635c3b02016-05-18 15:37:25 -0700978 flags builderFlags, outputFile android.WritablePath) {
Colin Crossbfae8852015-03-26 14:44:11 -0700979
Elliott Hughes2e70f792021-04-19 12:58:43 -0700980 objcopyCmd := "${config.ClangBin}/llvm-objcopy"
Colin Crossbfae8852015-03-26 14:44:11 -0700981
Colin Crossae887032017-10-23 17:16:14 -0700982 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700983 Rule: prefixSymbols,
984 Description: "prefix symbols " + outputFile.Base(),
985 Output: outputFile,
986 Input: inputFile,
Colin Crossbfae8852015-03-26 14:44:11 -0700987 Args: map[string]string{
988 "objcopyCmd": objcopyCmd,
989 "prefix": prefix,
990 },
991 })
992}
993
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500994// Registers a build statement to invoke `strip` (to discard symbols and data from object files).
995func transformStrip(ctx android.ModuleContext, inputFile android.Path,
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200996 outputFile android.WritablePath, flags StripFlags) {
Colin Cross665dce92016-04-28 14:50:03 -0700997
Colin Cross665dce92016-04-28 14:50:03 -0700998 args := ""
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200999 if flags.StripAddGnuDebuglink {
Colin Cross665dce92016-04-28 14:50:03 -07001000 args += " --add-gnu-debuglink"
1001 }
Thiébaud Weksteend4587452020-08-19 14:53:01 +02001002 if flags.StripKeepMiniDebugInfo {
Colin Cross665dce92016-04-28 14:50:03 -07001003 args += " --keep-mini-debug-info"
1004 }
Thiébaud Weksteend4587452020-08-19 14:53:01 +02001005 if flags.StripKeepSymbols {
Colin Cross665dce92016-04-28 14:50:03 -07001006 args += " --keep-symbols"
1007 }
Thiébaud Weksteend4587452020-08-19 14:53:01 +02001008 if flags.StripKeepSymbolsList != "" {
1009 args += " -k" + flags.StripKeepSymbolsList
Yi Kongacee27c2019-03-29 20:05:14 -07001010 }
Thiébaud Weksteend4587452020-08-19 14:53:01 +02001011 if flags.StripKeepSymbolsAndDebugFrame {
Christopher Ferrisb43fe7a2019-05-17 16:39:54 -07001012 args += " --keep-symbols-and-debug-frame"
1013 }
Colin Cross665dce92016-04-28 14:50:03 -07001014
Colin Crossae887032017-10-23 17:16:14 -07001015 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -07001016 Rule: strip,
1017 Description: "strip " + outputFile.Base(),
1018 Output: outputFile,
1019 Input: inputFile,
Colin Cross665dce92016-04-28 14:50:03 -07001020 Args: map[string]string{
Elliott Hughesafcd3392021-05-05 13:54:11 -07001021 "args": args,
Colin Cross665dce92016-04-28 14:50:03 -07001022 },
1023 })
1024}
1025
Chris Parsonsbf4f55f2020-11-23 17:02:44 -05001026// Registers build statement to invoke `strip` on darwin architecture.
1027func transformDarwinStrip(ctx android.ModuleContext, inputFile android.Path,
Colin Cross635c3b02016-05-18 15:37:25 -07001028 outputFile android.WritablePath) {
Colin Crossb8ecdfe2016-05-03 15:10:29 -07001029
Colin Crossae887032017-10-23 17:16:14 -07001030 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -07001031 Rule: darwinStrip,
1032 Description: "strip " + outputFile.Base(),
1033 Output: outputFile,
1034 Input: inputFile,
Colin Crossb8ecdfe2016-05-03 15:10:29 -07001035 })
1036}
1037
Chris Parsonsbf4f55f2020-11-23 17:02:44 -05001038// Registers build statement to zip one or more coverage files.
1039func transformCoverageFilesToZip(ctx android.ModuleContext,
Oliver Nguyenc7434142019-04-24 14:22:25 -07001040 inputs Objects, baseName string) android.OptionalPath {
Dan Willemsen581341d2017-02-09 16:16:31 -08001041
1042 if len(inputs.coverageFiles) > 0 {
Oliver Nguyenc7434142019-04-24 14:22:25 -07001043 outputFile := android.PathForModuleOut(ctx, baseName+".zip")
Dan Willemsen581341d2017-02-09 16:16:31 -08001044
Oliver Nguyenc7434142019-04-24 14:22:25 -07001045 ctx.Build(pctx, android.BuildParams{
1046 Rule: zip,
1047 Description: "zip " + outputFile.Base(),
1048 Inputs: inputs.coverageFiles,
1049 Output: outputFile,
1050 })
Dan Willemsen581341d2017-02-09 16:16:31 -08001051
1052 return android.OptionalPathForPath(outputFile)
1053 }
1054
1055 return android.OptionalPath{}
1056}
1057
Chris Parsonsbf4f55f2020-11-23 17:02:44 -05001058// Rule to repack an archive (.a) file with a subset of object files.
1059func transformArchiveRepack(ctx android.ModuleContext, inputFile android.Path,
Yi Kongc49c3932019-10-15 02:01:19 -07001060 outputFile android.WritablePath, objects []string) {
1061
1062 ctx.Build(pctx, android.BuildParams{
1063 Rule: archiveRepack,
1064 Description: "Repack archive " + outputFile.Base(),
1065 Output: outputFile,
1066 Input: inputFile,
1067 Args: map[string]string{
1068 "objects": strings.Join(objects, " "),
1069 },
1070 })
1071}
1072
Elliott Hughes5f314b12021-05-18 11:56:02 -07001073func mingwCmd(toolchain config.Toolchain, cmd string) string {
Colin Cross3f40fa42015-01-30 17:27:36 -08001074 return filepath.Join(toolchain.GccRoot(), "bin", toolchain.GccTriple()+"-"+cmd)
1075}