blob: 439e3721bc65ab2d3e62516dfd0d014e6d07dc26 [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.
Ramy Medhat9a90fe52020-04-13 13:21:23 -040062 ld, ldRE = remoteexec.StaticRules(pctx, "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"},
Ramy Medhat9a90fe52020-04-13 13:21:23 -040076 RSPFile: "${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.
Ramy Medhat9a90fe52020-04-13 13:21:23 -040083 partialLd, partialLdRE = remoteexec.StaticRules(pctx, "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")
Colin Cross665dce92016-04-28 14:50:03 -0700129
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500130 // Rule to invoke `strip` (to discard symbols and data from object files).
131 strip = pctx.AndroidStaticRule("strip",
132 blueprint.RuleParams{
133 Depfile: "${out}.d",
134 Deps: blueprint.DepsGCC,
135 Command: "CROSS_COMPILE=$crossCompile XZ=$xzCmd CLANG_BIN=${config.ClangBin} $stripPath ${args} -i ${in} -o ${out} -d ${out}.d",
136 CommandDeps: []string{"$stripPath", "$xzCmd"},
137 Pool: darwinStripPool,
138 },
139 "args", "crossCompile")
140
141 // Rule to invoke `strip` (to discard symbols and data from object files) on darwin architecture.
142 darwinStrip = pctx.AndroidStaticRule("darwinStrip",
143 blueprint.RuleParams{
144 Command: "${config.MacStripPath} -u -r -o $out $in",
145 CommandDeps: []string{"${config.MacStripPath}"},
146 })
147
Colin Crossee3ea312019-05-17 15:36:46 -0700148 // 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 -0700149 // file descriptors on darwin. Limit concurrent calls to 5 on darwin.
Colin Crossee3ea312019-05-17 15:36:46 -0700150 darwinStripPool = func() blueprint.Pool {
151 if runtime.GOOS == "darwin" {
152 return pctx.StaticPool("darwinStripPool", blueprint.PoolParams{
Colin Crossbadf8d62019-05-22 13:25:50 -0700153 Depth: 5,
Colin Crossee3ea312019-05-17 15:36:46 -0700154 })
155 } else {
156 return nil
157 }
158 }()
159
Yi Kongc49c3932019-10-15 02:01:19 -0700160 _ = pctx.SourcePathVariable("archiveRepackPath", "build/soong/scripts/archive_repack.sh")
161
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500162 // Rule to repack an archive (.a) file with a subset of object files.
Yi Kongc49c3932019-10-15 02:01:19 -0700163 archiveRepack = pctx.AndroidStaticRule("archiveRepack",
164 blueprint.RuleParams{
165 Depfile: "${out}.d",
166 Deps: blueprint.DepsGCC,
167 Command: "CLANG_BIN=${config.ClangBin} $archiveRepackPath -i ${in} -o ${out} -d ${out}.d ${objects}",
168 CommandDeps: []string{"$archiveRepackPath"},
169 },
170 "objects")
171
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500172 // Rule to create an empty file at a given path.
Colin Cross9d45bb72016-08-29 16:14:13 -0700173 emptyFile = pctx.AndroidStaticRule("emptyFile",
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700174 blueprint.RuleParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700175 Command: "rm -f $out && touch $out",
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700176 })
177
Nan Zhang43a485c2017-03-27 14:27:58 -0700178 _ = pctx.SourcePathVariable("tocPath", "build/soong/scripts/toc.sh")
Colin Cross26c34ed2016-09-30 17:10:16 -0700179
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500180 // A rule for extracting a table of contents from a shared library (.so).
Colin Cross26c34ed2016-09-30 17:10:16 -0700181 toc = pctx.AndroidStaticRule("toc",
182 blueprint.RuleParams{
183 Depfile: "${out}.d",
184 Deps: blueprint.DepsGCC,
Colin Crossb496cfd2018-09-10 16:50:05 -0700185 Command: "CROSS_COMPILE=$crossCompile $tocPath $format -i ${in} -o ${out} -d ${out}.d",
Colin Cross26c34ed2016-09-30 17:10:16 -0700186 CommandDeps: []string{"$tocPath"},
187 Restat: true,
188 },
Colin Crossb496cfd2018-09-10 16:50:05 -0700189 "crossCompile", "format")
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700190
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500191 // Rule for invoking clang-tidy (a clang-based linter).
Kousik Kumar4e30bba2020-06-18 09:17:51 -0700192 clangTidy, clangTidyRE = remoteexec.StaticRules(pctx, "clangTidy",
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700193 blueprint.RuleParams{
Kousik Kumar4e30bba2020-06-18 09:17:51 -0700194 Command: "rm -f $out && $reTemplate${config.ClangBin}/clang-tidy $tidyFlags $in -- $cFlags && touch $out",
George Burgess IVc4624c02019-04-04 16:22:37 -0700195 CommandDeps: []string{"${config.ClangBin}/clang-tidy"},
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700196 },
Kousik Kumar4e30bba2020-06-18 09:17:51 -0700197 &remoteexec.REParams{
198 Labels: map[string]string{"type": "lint", "tool": "clang-tidy", "lang": "cpp"},
199 ExecStrategy: "${config.REClangTidyExecStrategy}",
200 Inputs: []string{"$in"},
201 // OutputFile here is $in for remote-execution since its possible that
202 // clang-tidy modifies the given input file itself and $out refers to the
203 // ".tidy" file generated for ninja-dependency reasons.
Colin Cross053fca12020-08-19 13:51:47 -0700204 OutputFiles: []string{"$in"},
205 Platform: map[string]string{remoteexec.PoolKey: "${config.REClangTidyPool}"},
Kousik Kumar4e30bba2020-06-18 09:17:51 -0700206 }, []string{"cFlags", "tidyFlags"}, []string{})
Colin Cross91e90042016-12-02 17:13:24 -0800207
Nan Zhang43a485c2017-03-27 14:27:58 -0700208 _ = pctx.SourcePathVariable("yasmCmd", "prebuilts/misc/${config.HostPrebuiltTag}/yasm/yasm")
Colin Cross91e90042016-12-02 17:13:24 -0800209
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500210 // Rule for invoking yasm to compile .asm assembly files.
Colin Cross91e90042016-12-02 17:13:24 -0800211 yasm = pctx.AndroidStaticRule("yasm",
212 blueprint.RuleParams{
Dan Willemsen1d3e5452017-08-22 20:53:45 -0700213 Command: "$yasmCmd $asFlags -o $out $in && $yasmCmd $asFlags -M $in >$out.d",
Colin Cross91e90042016-12-02 17:13:24 -0800214 CommandDeps: []string{"$yasmCmd"},
Dan Willemsen1d3e5452017-08-22 20:53:45 -0700215 Depfile: "$out.d",
216 Deps: blueprint.DepsGCC,
Colin Cross91e90042016-12-02 17:13:24 -0800217 },
218 "asFlags")
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800219
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500220 // Rule to invoke windres, for interaction with Windows resources.
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700221 windres = pctx.AndroidStaticRule("windres",
222 blueprint.RuleParams{
Nick Desaulniers18eeffa2020-01-29 16:20:11 -0800223 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 -0700224 CommandDeps: []string{"$windresCmd"},
225 },
226 "windresCmd", "flags")
227
Jayant Chowdharya4c6df52018-02-20 12:36:51 -0800228 _ = pctx.SourcePathVariable("sAbiDumper", "prebuilts/clang-tools/${config.HostPrebuiltTag}/bin/header-abi-dumper")
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800229
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700230 // -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 -0400231 sAbiDump, sAbiDumpRE = remoteexec.StaticRules(pctx, "sAbiDump",
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800232 blueprint.RuleParams{
Ramy Medhat31ec9422020-04-17 15:03:58 -0400233 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 -0800234 CommandDeps: []string{"$sAbiDumper"},
Ramy Medhat31ec9422020-04-17 15:03:58 -0400235 }, &remoteexec.REParams{
236 Labels: map[string]string{"type": "abi-dump", "tool": "header-abi-dumper"},
237 ExecStrategy: "${config.REAbiDumperExecStrategy}",
238 Platform: map[string]string{
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500239 remoteexec.PoolKey: "${config.RECXXPool}",
Ramy Medhat31ec9422020-04-17 15:03:58 -0400240 },
241 }, []string{"cFlags", "exportDirs"}, nil)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800242
Jayant Chowdharya4c6df52018-02-20 12:36:51 -0800243 _ = pctx.SourcePathVariable("sAbiLinker", "prebuilts/clang-tools/${config.HostPrebuiltTag}/bin/header-abi-linker")
Ramy Medhat808594c2020-05-07 06:56:47 -0400244 _ = pctx.SourcePathVariable("sAbiLinkerLibs", "prebuilts/clang-tools/${config.HostPrebuiltTag}/lib64")
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800245
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500246 // Rule to combine .dump sAbi dump files from multiple source files into a single .ldump
247 // sAbi dump file.
Ramy Medhat808594c2020-05-07 06:56:47 -0400248 sAbiLink, sAbiLinkRE = remoteexec.StaticRules(pctx, "sAbiLink",
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800249 blueprint.RuleParams{
Ramy Medhat808594c2020-05-07 06:56:47 -0400250 Command: "$reTemplate$sAbiLinker -o ${out} $symbolFilter -arch $arch $exportedHeaderFlags @${out}.rsp ",
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800251 CommandDeps: []string{"$sAbiLinker"},
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800252 Rspfile: "${out}.rsp",
253 RspfileContent: "${in}",
Ramy Medhat808594c2020-05-07 06:56:47 -0400254 }, &remoteexec.REParams{
255 Labels: map[string]string{"type": "tool", "name": "abi-linker"},
256 ExecStrategy: "${config.REAbiLinkerExecStrategy}",
Ramy Medhat6797edc2020-08-28 14:21:55 -0400257 Inputs: []string{"$sAbiLinkerLibs", "${out}.rsp", "$implicitInputs"},
Ramy Medhat808594c2020-05-07 06:56:47 -0400258 RSPFile: "${out}.rsp",
259 OutputFiles: []string{"$out"},
260 ToolchainInputs: []string{"$sAbiLinker"},
261 Platform: map[string]string{remoteexec.PoolKey: "${config.RECXXPool}"},
Ramy Medhat6797edc2020-08-28 14:21:55 -0400262 }, []string{"symbolFilter", "arch", "exportedHeaderFlags"}, []string{"implicitInputs"})
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800263
Jayant Chowdharya4c6df52018-02-20 12:36:51 -0800264 _ = pctx.SourcePathVariable("sAbiDiffer", "prebuilts/clang-tools/${config.HostPrebuiltTag}/bin/header-abi-diff")
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700265
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500266 // Rule to compare linked sAbi dump files (.ldump).
Colin Cross2e2dbc22019-09-25 13:31:46 -0700267 sAbiDiff = pctx.RuleFunc("sAbiDiff",
Dan Willemsen54daaf02018-03-12 13:24:09 -0700268 func(ctx android.PackageRuleContext) blueprint.RuleParams {
Logan Chien2a65dda2019-10-01 15:58:07 -0700269 commandStr := "($sAbiDiffer ${extraFlags} -lib ${libName} -arch ${arch} -o ${out} -new ${in} -old ${referenceDump})"
Logan Chien6227fed2019-02-18 13:12:21 +0800270 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 +0800271 commandStr += " && (mkdir -p $$DIST_DIR/abidiffs && cp ${out} $$DIST_DIR/abidiffs/)"
Jayant Chowdharyd8b70a32018-02-01 17:23:09 -0800272 commandStr += " && exit 1)"
Jayant Chowdhary219139d2017-11-27 14:52:21 -0800273 return blueprint.RuleParams{
274 Command: commandStr,
275 CommandDeps: []string{"$sAbiDiffer"},
Dan Willemsen54daaf02018-03-12 13:24:09 -0700276 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800277 },
Logan Chien2a65dda2019-10-01 15:58:07 -0700278 "extraFlags", "referenceDump", "libName", "arch", "createReferenceDumpFlags")
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700279
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500280 // Rule to unzip a reference abi dump.
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700281 unzipRefSAbiDump = pctx.AndroidStaticRule("unzipRefSAbiDump",
282 blueprint.RuleParams{
283 Command: "gunzip -c $in > $out",
284 })
Oliver Nguyenc7434142019-04-24 14:22:25 -0700285
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500286 // Rule to zip files.
Oliver Nguyenc7434142019-04-24 14:22:25 -0700287 zip = pctx.AndroidStaticRule("zip",
288 blueprint.RuleParams{
Colin Cross053fca12020-08-19 13:51:47 -0700289 Command: "${SoongZipCmd} -o ${out} -C $$OUT_DIR -r ${out}.rsp",
Oliver Nguyenc7434142019-04-24 14:22:25 -0700290 CommandDeps: []string{"${SoongZipCmd}"},
Colin Cross053fca12020-08-19 13:51:47 -0700291 Rspfile: "${out}.rsp",
Oliver Nguyenc7434142019-04-24 14:22:25 -0700292 RspfileContent: "$in",
293 })
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800294
295 _ = pctx.SourcePathVariable("cxxExtractor",
296 "prebuilts/clang-tools/${config.HostPrebuiltTag}/bin/cxx_extractor")
Sasha Smundak65143642019-09-26 20:14:28 -0700297 _ = pctx.SourcePathVariable("kytheVnames", "build/soong/vnames.json")
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800298 _ = pctx.VariableFunc("kytheCorpus",
299 func(ctx android.PackageVarContext) string { return ctx.Config().XrefCorpusName() })
Sasha Smundak6c2d4f92020-01-09 17:34:23 -0800300 _ = pctx.VariableFunc("kytheCuEncoding",
301 func(ctx android.PackageVarContext) string { return ctx.Config().XrefCuEncoding() })
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500302
303 // Rule to use kythe extractors to generate .kzip files, used to build code cross references.
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800304 kytheExtract = pctx.StaticRule("kythe",
305 blueprint.RuleParams{
Sasha Smundak6c2d4f92020-01-09 17:34:23 -0800306 Command: `rm -f $out && ` +
Sasha Smundaka4ef83b2020-04-21 17:08:35 -0700307 `KYTHE_CORPUS=${kytheCorpus} ` +
308 `KYTHE_OUTPUT_FILE=$out ` +
309 `KYTHE_VNAMES=$kytheVnames ` +
310 `KYTHE_KZIP_ENCODING=${kytheCuEncoding} ` +
311 `KYTHE_CANONICALIZE_VNAME_PATHS=prefer-relative ` +
Sasha Smundak6c2d4f92020-01-09 17:34:23 -0800312 `$cxxExtractor $cFlags $in `,
Sasha Smundak65143642019-09-26 20:14:28 -0700313 CommandDeps: []string{"$cxxExtractor", "$kytheVnames"},
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800314 },
315 "cFlags")
Colin Cross3f40fa42015-01-30 17:27:36 -0800316)
317
Ivan Lozanof3717ee2020-05-20 09:03:20 -0400318func PwdPrefix() string {
319 // Darwin doesn't have /proc
320 if runtime.GOOS != "darwin" {
321 return "PWD=/proc/self/cwd"
322 }
323 return ""
324}
325
Dan Willemsen322a0a62015-11-17 15:19:46 -0800326func init() {
327 // We run gcc/clang with PWD=/proc/self/cwd to remove $TOP from the
328 // debug output. That way two builds in two different directories will
329 // create the same output.
Ivan Lozanof3717ee2020-05-20 09:03:20 -0400330 pctx.StaticVariable("relPwd", PwdPrefix())
Oliver Nguyenc7434142019-04-24 14:22:25 -0700331
332 pctx.HostBinToolVariable("SoongZipCmd", "soong_zip")
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400333 pctx.Import("android/soong/remoteexec")
Dan Willemsen322a0a62015-11-17 15:19:46 -0800334}
335
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500336// builderFlags contains various types of command line flags (and settings) for use in building
337// build statements related to C++.
Colin Cross3f40fa42015-01-30 17:27:36 -0800338type builderFlags struct {
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500339 // Global flags (which build system or toolchain is responsible for). These are separate from
340 // local flags because they should appear first (so that they may be overridden by local flags).
Colin Cross6d88dba2019-11-06 07:06:58 -0800341 globalCommonFlags string
342 globalAsFlags string
343 globalYasmFlags string
344 globalCFlags string
345 globalToolingCFlags string // A separate set of cFlags for clang LibTooling tools
346 globalToolingCppFlags string // A separate set of cppFlags for clang LibTooling tools
347 globalConlyFlags string
348 globalCppFlags string
349 globalLdFlags string
350
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500351 // Local flags (which individual modules are responsible for). These may override global flags.
Colin Cross6d88dba2019-11-06 07:06:58 -0800352 localCommonFlags string
353 localAsFlags string
354 localYasmFlags string
355 localCFlags string
356 localToolingCFlags string // A separate set of cFlags for clang LibTooling tools
357 localToolingCppFlags string // A separate set of cppFlags for clang LibTooling tools
358 localConlyFlags string
359 localCppFlags string
360 localLdFlags string
361
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500362 libFlags string // Flags to add to the linker directly after specifying libraries to link.
363 extraLibFlags string // Flags to add to the linker last.
364 tidyFlags string // Flags that apply to clang-tidy
365 sAbiFlags string // Flags that apply to header-abi-dumps
366 aidlFlags string // Flags that apply to aidl source files
367 rsFlags string // Flags that apply to renderscript source files
Colin Cross6d88dba2019-11-06 07:06:58 -0800368 toolchain config.Toolchain
Colin Cross665dce92016-04-28 14:50:03 -0700369
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500370 // True if these extra features are enabled.
371 tidy bool
372 gcovCoverage bool
373 sAbiDump bool
374 emitXrefs bool
375
376 assemblerWithCpp bool // True if .s files should be processed with the c preprocessor.
Dan Willemsen98ab3112019-08-27 21:20:40 -0700377
Colin Crossc3199482017-03-30 15:03:04 -0700378 systemIncludeFlags string
379
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500380 // True if static libraries should be grouped (using `-Wl,--start-group` and `-Wl,--end-group`).
Colin Cross18c0c5a2016-12-01 14:45:23 -0800381 groupStaticLibs bool
382
Colin Cross19878da2019-03-28 14:45:07 -0700383 proto android.ProtoFlags
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500384 protoC bool // If true, compile protos as `.c` files. Otherwise, output as `.cc`.
385 protoOptionsFile bool // If true, output a proto options file.
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700386
387 yacc *YaccProperties
Matthias Maennich22fd4d12020-07-15 10:58:56 +0200388 lex *LexProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800389}
390
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500391// StripFlags represents flags related to stripping. This is separate from builderFlags, as these
392// flags are useful outside of this package (such as for Rust).
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200393type StripFlags struct {
394 Toolchain config.Toolchain
395 StripKeepSymbols bool
396 StripKeepSymbolsList string
397 StripKeepSymbolsAndDebugFrame bool
398 StripKeepMiniDebugInfo bool
399 StripAddGnuDebuglink bool
400 StripUseGnuStrip bool
401}
402
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500403// Objects is a collection of file paths corresponding to outputs for C++ related build statements.
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700404type Objects struct {
Dan Willemsen581341d2017-02-09 16:16:31 -0800405 objFiles android.Paths
406 tidyFiles android.Paths
407 coverageFiles android.Paths
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800408 sAbiDumpFiles android.Paths
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800409 kytheFiles android.Paths
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700410}
411
412func (a Objects) Copy() Objects {
413 return Objects{
Dan Willemsen581341d2017-02-09 16:16:31 -0800414 objFiles: append(android.Paths{}, a.objFiles...),
415 tidyFiles: append(android.Paths{}, a.tidyFiles...),
416 coverageFiles: append(android.Paths{}, a.coverageFiles...),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800417 sAbiDumpFiles: append(android.Paths{}, a.sAbiDumpFiles...),
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800418 kytheFiles: append(android.Paths{}, a.kytheFiles...),
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700419 }
420}
421
422func (a Objects) Append(b Objects) Objects {
423 return Objects{
Dan Willemsen581341d2017-02-09 16:16:31 -0800424 objFiles: append(a.objFiles, b.objFiles...),
425 tidyFiles: append(a.tidyFiles, b.tidyFiles...),
426 coverageFiles: append(a.coverageFiles, b.coverageFiles...),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800427 sAbiDumpFiles: append(a.sAbiDumpFiles, b.sAbiDumpFiles...),
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800428 kytheFiles: append(a.kytheFiles, b.kytheFiles...),
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700429 }
430}
431
Colin Cross3f40fa42015-01-30 17:27:36 -0800432// Generate rules for compiling multiple .c, .cpp, or .S files to individual .o files
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500433func transformSourceToObj(ctx android.ModuleContext, subdir string, srcFiles android.Paths,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800434 flags builderFlags, pathDeps android.Paths, cFlagsDeps android.Paths) Objects {
Colin Cross581c1892015-04-07 16:50:10 -0700435
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500436 // Source files are one-to-one with tidy, coverage, or kythe files, if enabled.
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700437 objFiles := make(android.Paths, len(srcFiles))
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700438 var tidyFiles android.Paths
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700439 if flags.tidy {
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700440 tidyFiles = make(android.Paths, 0, len(srcFiles))
441 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800442 var coverageFiles android.Paths
Oliver Nguyen04526782020-04-21 12:40:27 -0700443 if flags.gcovCoverage {
Dan Willemsen581341d2017-02-09 16:16:31 -0800444 coverageFiles = make(android.Paths, 0, len(srcFiles))
445 }
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800446 var kytheFiles android.Paths
447 if flags.emitXrefs {
448 kytheFiles = make(android.Paths, 0, len(srcFiles))
449 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800450
Colin Cross6d88dba2019-11-06 07:06:58 -0800451 // Produce fully expanded flags for use by C tools, C compiles, C++ tools, C++ compiles, and asm compiles
452 // respectively.
453 toolingCflags := flags.globalCommonFlags + " " +
454 flags.globalToolingCFlags + " " +
455 flags.globalConlyFlags + " " +
456 flags.localCommonFlags + " " +
457 flags.localToolingCFlags + " " +
458 flags.localConlyFlags + " " +
459 flags.systemIncludeFlags
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700460
Colin Cross6d88dba2019-11-06 07:06:58 -0800461 cflags := flags.globalCommonFlags + " " +
462 flags.globalCFlags + " " +
463 flags.globalConlyFlags + " " +
464 flags.localCommonFlags + " " +
465 flags.localCFlags + " " +
466 flags.localConlyFlags + " " +
467 flags.systemIncludeFlags
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700468
Colin Cross6d88dba2019-11-06 07:06:58 -0800469 toolingCppflags := flags.globalCommonFlags + " " +
470 flags.globalToolingCFlags + " " +
471 flags.globalToolingCppFlags + " " +
472 flags.localCommonFlags + " " +
473 flags.localToolingCFlags + " " +
474 flags.localToolingCppFlags + " " +
475 flags.systemIncludeFlags
Colin Crossc3199482017-03-30 15:03:04 -0700476
Colin Cross6d88dba2019-11-06 07:06:58 -0800477 cppflags := flags.globalCommonFlags + " " +
478 flags.globalCFlags + " " +
479 flags.globalCppFlags + " " +
480 flags.localCommonFlags + " " +
481 flags.localCFlags + " " +
482 flags.localCppFlags + " " +
483 flags.systemIncludeFlags
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700484
Colin Cross6d88dba2019-11-06 07:06:58 -0800485 asflags := flags.globalCommonFlags + " " +
486 flags.globalAsFlags + " " +
487 flags.localCommonFlags + " " +
488 flags.localAsFlags + " " +
489 flags.systemIncludeFlags
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700490
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800491 var sAbiDumpFiles android.Paths
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700492 if flags.sAbiDump {
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800493 sAbiDumpFiles = make(android.Paths, 0, len(srcFiles))
494 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800495
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700496 cflags += " ${config.NoOverrideClangGlobalCflags}"
497 toolingCflags += " ${config.NoOverrideClangGlobalCflags}"
498 cppflags += " ${config.NoOverrideClangGlobalCflags}"
499 toolingCppflags += " ${config.NoOverrideClangGlobalCflags}"
Dan Willemsenbe03f342016-03-03 17:21:04 -0800500
Colin Cross3f40fa42015-01-30 17:27:36 -0800501 for i, srcFile := range srcFiles {
Dan Willemsen21ec4902016-11-02 20:43:13 -0700502 objFile := android.ObjPathWithExt(ctx, subdir, srcFile, "o")
Colin Cross3f40fa42015-01-30 17:27:36 -0800503
504 objFiles[i] = objFile
505
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500506 // Register compilation build statements. The actual rule used depends on the source file type.
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700507 switch srcFile.Ext() {
508 case ".asm":
Colin Crossae887032017-10-23 17:16:14 -0700509 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700510 Rule: yasm,
511 Description: "yasm " + srcFile.Rel(),
512 Output: objFile,
513 Input: srcFile,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800514 Implicits: cFlagsDeps,
515 OrderOnly: pathDeps,
Colin Cross91e90042016-12-02 17:13:24 -0800516 Args: map[string]string{
Colin Cross6d88dba2019-11-06 07:06:58 -0800517 "asFlags": flags.globalYasmFlags + " " + flags.localYasmFlags,
Colin Cross91e90042016-12-02 17:13:24 -0800518 },
519 })
520 continue
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700521 case ".rc":
Colin Crossae887032017-10-23 17:16:14 -0700522 ctx.Build(pctx, android.BuildParams{
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700523 Rule: windres,
524 Description: "windres " + srcFile.Rel(),
525 Output: objFile,
526 Input: srcFile,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800527 Implicits: cFlagsDeps,
528 OrderOnly: pathDeps,
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700529 Args: map[string]string{
530 "windresCmd": gccCmd(flags.toolchain, "windres"),
531 "flags": flags.toolchain.WindresFlags(),
532 },
533 })
534 continue
Pete Bentleyfcf55bf2019-08-16 20:14:32 +0100535 case ".o":
536 objFiles[i] = srcFile
537 continue
Colin Cross91e90042016-12-02 17:13:24 -0800538 }
539
Colin Cross6d88dba2019-11-06 07:06:58 -0800540 var moduleFlags string
541 var moduleToolingFlags string
542
Colin Cross3f40fa42015-01-30 17:27:36 -0800543 var ccCmd string
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700544 tidy := flags.tidy
Oliver Nguyen04526782020-04-21 12:40:27 -0700545 coverage := flags.gcovCoverage
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700546 dump := flags.sAbiDump
Dan Willemsenfcabb1c2019-01-03 23:25:11 -0800547 rule := cc
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800548 emitXref := flags.emitXrefs
Colin Cross3f40fa42015-01-30 17:27:36 -0800549
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700550 switch srcFile.Ext() {
Dan Willemsenfcabb1c2019-01-03 23:25:11 -0800551 case ".s":
Dan Willemsen98ab3112019-08-27 21:20:40 -0700552 if !flags.assemblerWithCpp {
553 rule = ccNoDeps
554 }
Dan Willemsenfcabb1c2019-01-03 23:25:11 -0800555 fallthrough
556 case ".S":
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700557 ccCmd = "clang"
Colin Cross6d88dba2019-11-06 07:06:58 -0800558 moduleFlags = asflags
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700559 tidy = false
Dan Willemsen581341d2017-02-09 16:16:31 -0800560 coverage = false
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800561 dump = false
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800562 emitXref = false
Colin Cross3f40fa42015-01-30 17:27:36 -0800563 case ".c":
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700564 ccCmd = "clang"
Colin Cross6d88dba2019-11-06 07:06:58 -0800565 moduleFlags = cflags
566 moduleToolingFlags = toolingCflags
Colin Crossd34ab7c2019-06-27 14:46:10 -0700567 case ".cpp", ".cc", ".cxx", ".mm":
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700568 ccCmd = "clang++"
Colin Cross6d88dba2019-11-06 07:06:58 -0800569 moduleFlags = cppflags
570 moduleToolingFlags = toolingCppflags
Colin Cross3f40fa42015-01-30 17:27:36 -0800571 default:
572 ctx.ModuleErrorf("File %s has unknown extension", srcFile)
573 continue
574 }
575
Colin Cross67a5c132017-05-09 13:45:28 -0700576 ccDesc := ccCmd
577
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700578 ccCmd = "${config.ClangBin}/" + ccCmd
Colin Cross3f40fa42015-01-30 17:27:36 -0800579
Dan Willemsen581341d2017-02-09 16:16:31 -0800580 var implicitOutputs android.WritablePaths
581 if coverage {
582 gcnoFile := android.ObjPathWithExt(ctx, subdir, srcFile, "gcno")
583 implicitOutputs = append(implicitOutputs, gcnoFile)
584 coverageFiles = append(coverageFiles, gcnoFile)
585 }
586
Colin Crossae887032017-10-23 17:16:14 -0700587 ctx.Build(pctx, android.BuildParams{
Dan Willemsenfcabb1c2019-01-03 23:25:11 -0800588 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -0700589 Description: ccDesc + " " + srcFile.Rel(),
Dan Willemsen581341d2017-02-09 16:16:31 -0800590 Output: objFile,
591 ImplicitOutputs: implicitOutputs,
592 Input: srcFile,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800593 Implicits: cFlagsDeps,
594 OrderOnly: pathDeps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800595 Args: map[string]string{
Colin Cross6d88dba2019-11-06 07:06:58 -0800596 "cFlags": moduleFlags,
Colin Cross28344522015-04-22 13:07:53 -0700597 "ccCmd": ccCmd,
Colin Cross3f40fa42015-01-30 17:27:36 -0800598 },
599 })
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700600
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500601 // Register post-process build statements (such as for tidy or kythe).
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800602 if emitXref {
603 kytheFile := android.ObjPathWithExt(ctx, subdir, srcFile, "kzip")
604 ctx.Build(pctx, android.BuildParams{
605 Rule: kytheExtract,
606 Description: "Xref C++ extractor " + srcFile.Rel(),
607 Output: kytheFile,
608 Input: srcFile,
609 Implicits: cFlagsDeps,
610 OrderOnly: pathDeps,
611 Args: map[string]string{
Colin Cross6d88dba2019-11-06 07:06:58 -0800612 "cFlags": moduleFlags,
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800613 },
614 })
615 kytheFiles = append(kytheFiles, kytheFile)
616 }
617
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700618 if tidy {
Dan Willemsen21ec4902016-11-02 20:43:13 -0700619 tidyFile := android.ObjPathWithExt(ctx, subdir, srcFile, "tidy")
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700620 tidyFiles = append(tidyFiles, tidyFile)
621
Kousik Kumar4e30bba2020-06-18 09:17:51 -0700622 rule := clangTidy
Ramy Medhat16f23a42020-09-03 01:29:49 -0400623 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_CLANG_TIDY") {
Kousik Kumar4e30bba2020-06-18 09:17:51 -0700624 rule = clangTidyRE
625 }
626
Colin Crossae887032017-10-23 17:16:14 -0700627 ctx.Build(pctx, android.BuildParams{
Kousik Kumar4e30bba2020-06-18 09:17:51 -0700628 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -0700629 Description: "clang-tidy " + srcFile.Rel(),
630 Output: tidyFile,
631 Input: srcFile,
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700632 // We must depend on objFile, since clang-tidy doesn't
633 // support exporting dependencies.
Dan Willemsen6b4419c2019-08-09 12:45:53 -0700634 Implicit: objFile,
635 Implicits: cFlagsDeps,
636 OrderOnly: pathDeps,
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700637 Args: map[string]string{
Colin Cross6d88dba2019-11-06 07:06:58 -0800638 "cFlags": moduleToolingFlags,
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700639 "tidyFlags": flags.tidyFlags,
640 },
641 })
642 }
643
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800644 if dump {
645 sAbiDumpFile := android.ObjPathWithExt(ctx, subdir, srcFile, "sdump")
646 sAbiDumpFiles = append(sAbiDumpFiles, sAbiDumpFile)
647
Ramy Medhat31ec9422020-04-17 15:03:58 -0400648 dumpRule := sAbiDump
Ramy Medhat16f23a42020-09-03 01:29:49 -0400649 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_ABI_DUMPER") {
Ramy Medhat31ec9422020-04-17 15:03:58 -0400650 dumpRule = sAbiDumpRE
651 }
Colin Crossae887032017-10-23 17:16:14 -0700652 ctx.Build(pctx, android.BuildParams{
Ramy Medhat31ec9422020-04-17 15:03:58 -0400653 Rule: dumpRule,
Colin Cross67a5c132017-05-09 13:45:28 -0700654 Description: "header-abi-dumper " + srcFile.Rel(),
655 Output: sAbiDumpFile,
656 Input: srcFile,
657 Implicit: objFile,
Dan Willemsen6b4419c2019-08-09 12:45:53 -0700658 Implicits: cFlagsDeps,
659 OrderOnly: pathDeps,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800660 Args: map[string]string{
Colin Cross6d88dba2019-11-06 07:06:58 -0800661 "cFlags": moduleToolingFlags,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800662 "exportDirs": flags.sAbiFlags,
663 },
664 })
665 }
666
Colin Cross3f40fa42015-01-30 17:27:36 -0800667 }
668
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700669 return Objects{
Dan Willemsen581341d2017-02-09 16:16:31 -0800670 objFiles: objFiles,
671 tidyFiles: tidyFiles,
672 coverageFiles: coverageFiles,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800673 sAbiDumpFiles: sAbiDumpFiles,
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800674 kytheFiles: kytheFiles,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700675 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800676}
677
678// Generate a rule for compiling multiple .o files to a static library (.a)
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500679func transformObjToStaticLib(ctx android.ModuleContext,
Martin Stjernholm391d94c2020-04-17 17:34:31 +0100680 objFiles android.Paths, wholeStaticLibs android.Paths,
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700681 flags builderFlags, outputFile android.ModuleOutPath, deps android.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800682
Stephen Hinesf1addeb2018-01-09 23:29:04 -0800683 arCmd := "${config.ClangBin}/llvm-ar"
Martin Stjernholm391d94c2020-04-17 17:34:31 +0100684 arFlags := ""
Stephen Hinesf1addeb2018-01-09 23:29:04 -0800685 if !ctx.Darwin() {
686 arFlags += " -format=gnu"
687 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800688
Martin Stjernholm391d94c2020-04-17 17:34:31 +0100689 if len(wholeStaticLibs) == 0 {
690 ctx.Build(pctx, android.BuildParams{
691 Rule: ar,
692 Description: "static link " + outputFile.Base(),
693 Output: outputFile,
694 Inputs: objFiles,
695 Implicits: deps,
696 Args: map[string]string{
697 "arFlags": "crsPD" + arFlags,
698 "arCmd": arCmd,
699 },
700 })
701
702 } else {
703 ctx.Build(pctx, android.BuildParams{
704 Rule: arWithLibs,
705 Description: "static link " + outputFile.Base(),
706 Output: outputFile,
707 Inputs: append(objFiles, wholeStaticLibs...),
708 Implicits: deps,
709 Args: map[string]string{
710 "arCmd": arCmd,
711 "arObjFlags": "crsPD" + arFlags,
712 "arObjs": strings.Join(objFiles.Strings(), " "),
713 "arLibFlags": "cqsL" + arFlags,
714 "arLibs": strings.Join(wholeStaticLibs.Strings(), " "),
715 },
716 })
717 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800718}
719
720// Generate a rule for compiling multiple .o files, plus static libraries, whole static libraries,
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700721// and shared libraries, to a shared library (.so) or dynamic executable
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500722func transformObjToDynamicBinary(ctx android.ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -0700723 objFiles, sharedLibs, staticLibs, lateStaticLibs, wholeStaticLibs, deps android.Paths,
Josh Gao75a50a22019-06-07 17:58:59 -0700724 crtBegin, crtEnd android.OptionalPath, groupLate bool, flags builderFlags, outputFile android.WritablePath, implicitOutputs android.WritablePaths) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800725
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700726 ldCmd := "${config.ClangBin}/clang++"
Colin Cross3f40fa42015-01-30 17:27:36 -0800727
Colin Cross3f40fa42015-01-30 17:27:36 -0800728 var libFlagsList []string
729
Colin Cross16b23492016-01-06 14:41:07 -0800730 if len(flags.libFlags) > 0 {
731 libFlagsList = append(libFlagsList, flags.libFlags)
732 }
733
Colin Cross3f40fa42015-01-30 17:27:36 -0800734 if len(wholeStaticLibs) > 0 {
Dan Willemsen490fd492015-11-24 17:53:15 -0800735 if ctx.Host() && ctx.Darwin() {
Colin Cross635c3b02016-05-18 15:37:25 -0700736 libFlagsList = append(libFlagsList, android.JoinWithPrefix(wholeStaticLibs.Strings(), "-force_load "))
Colin Cross0af4b842015-04-30 16:36:18 -0700737 } else {
738 libFlagsList = append(libFlagsList, "-Wl,--whole-archive ")
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700739 libFlagsList = append(libFlagsList, wholeStaticLibs.Strings()...)
Colin Cross0af4b842015-04-30 16:36:18 -0700740 libFlagsList = append(libFlagsList, "-Wl,--no-whole-archive ")
741 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800742 }
743
Colin Cross7a7cf972016-12-05 18:47:39 -0800744 if flags.groupStaticLibs && !ctx.Darwin() && len(staticLibs) > 0 {
Colin Cross18c0c5a2016-12-01 14:45:23 -0800745 libFlagsList = append(libFlagsList, "-Wl,--start-group")
746 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700747 libFlagsList = append(libFlagsList, staticLibs.Strings()...)
Colin Cross7a7cf972016-12-05 18:47:39 -0800748 if flags.groupStaticLibs && !ctx.Darwin() && len(staticLibs) > 0 {
Colin Cross18c0c5a2016-12-01 14:45:23 -0800749 libFlagsList = append(libFlagsList, "-Wl,--end-group")
750 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800751
Stephen Hines10347862016-07-18 15:54:54 -0700752 if groupLate && !ctx.Darwin() && len(lateStaticLibs) > 0 {
Dan Willemsenedc385f2015-07-08 13:02:23 -0700753 libFlagsList = append(libFlagsList, "-Wl,--start-group")
754 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700755 libFlagsList = append(libFlagsList, lateStaticLibs.Strings()...)
Stephen Hines10347862016-07-18 15:54:54 -0700756 if groupLate && !ctx.Darwin() && len(lateStaticLibs) > 0 {
Dan Willemsenedc385f2015-07-08 13:02:23 -0700757 libFlagsList = append(libFlagsList, "-Wl,--end-group")
758 }
759
Colin Cross3f40fa42015-01-30 17:27:36 -0800760 for _, lib := range sharedLibs {
Josh Gao75a50a22019-06-07 17:58:59 -0700761 libFile := lib.String()
762 if ctx.Windows() {
763 libFile = pathtools.ReplaceExtension(libFile, "lib")
764 }
765 libFlagsList = append(libFlagsList, libFile)
Colin Cross3f40fa42015-01-30 17:27:36 -0800766 }
767
Colin Cross3f40fa42015-01-30 17:27:36 -0800768 deps = append(deps, staticLibs...)
Colin Cross3075ad02015-03-17 10:47:08 -0700769 deps = append(deps, lateStaticLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800770 deps = append(deps, wholeStaticLibs...)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700771 if crtBegin.Valid() {
772 deps = append(deps, crtBegin.Path(), crtEnd.Path())
Colin Cross3f40fa42015-01-30 17:27:36 -0800773 }
774
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400775 rule := ld
Kousik Kumar3fb61262020-04-22 13:31:09 -0700776 args := map[string]string{
777 "ldCmd": ldCmd,
778 "crtBegin": crtBegin.String(),
779 "libFlags": strings.Join(libFlagsList, " "),
780 "extraLibFlags": flags.extraLibFlags,
781 "ldFlags": flags.globalLdFlags + " " + flags.localLdFlags,
782 "crtEnd": crtEnd.String(),
783 }
Ramy Medhat16f23a42020-09-03 01:29:49 -0400784 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_CXX_LINKS") {
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400785 rule = ldRE
Kousik Kumar3fb61262020-04-22 13:31:09 -0700786 args["implicitOutputs"] = strings.Join(implicitOutputs.Strings(), ",")
Ramy Medhat6797edc2020-08-28 14:21:55 -0400787 args["implicitInputs"] = strings.Join(deps.Strings(), ",")
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400788 }
789
Colin Crossae887032017-10-23 17:16:14 -0700790 ctx.Build(pctx, android.BuildParams{
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400791 Rule: rule,
Josh Gao75a50a22019-06-07 17:58:59 -0700792 Description: "link " + outputFile.Base(),
793 Output: outputFile,
794 ImplicitOutputs: implicitOutputs,
795 Inputs: objFiles,
796 Implicits: deps,
Kousik Kumar3fb61262020-04-22 13:31:09 -0700797 Args: args,
Colin Cross3f40fa42015-01-30 17:27:36 -0800798 })
799}
800
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800801// Generate a rule to combine .dump sAbi dump files from multiple source files
802// into a single .ldump sAbi dump file
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500803func transformDumpToLinkedDump(ctx android.ModuleContext, sAbiDumps android.Paths, soFile android.Path,
Logan Chiene3d7a0d2019-01-17 00:18:02 +0800804 baseName, exportedHeaderFlags string, symbolFile android.OptionalPath,
805 excludedSymbolVersions, excludedSymbolTags []string) android.OptionalPath {
806
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800807 outputFile := android.PathForModuleOut(ctx, baseName+".lsdump")
Logan Chiene3d7a0d2019-01-17 00:18:02 +0800808
809 implicits := android.Paths{soFile}
Jayant Chowdharydf344d52018-01-17 11:11:42 -0800810 symbolFilterStr := "-so " + soFile.String()
Logan Chiene3d7a0d2019-01-17 00:18:02 +0800811
812 if symbolFile.Valid() {
813 implicits = append(implicits, symbolFile.Path())
814 symbolFilterStr += " -v " + symbolFile.String()
815 }
816 for _, ver := range excludedSymbolVersions {
817 symbolFilterStr += " --exclude-symbol-version " + ver
818 }
819 for _, tag := range excludedSymbolTags {
820 symbolFilterStr += " --exclude-symbol-tag " + tag
821 }
Ramy Medhat808594c2020-05-07 06:56:47 -0400822 rule := sAbiLink
823 args := map[string]string{
824 "symbolFilter": symbolFilterStr,
825 "arch": ctx.Arch().ArchType.Name,
826 "exportedHeaderFlags": exportedHeaderFlags,
827 }
Ramy Medhat16f23a42020-09-03 01:29:49 -0400828 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_ABI_LINKER") {
Ramy Medhat808594c2020-05-07 06:56:47 -0400829 rule = sAbiLinkRE
830 rbeImplicits := implicits.Strings()
831 for _, p := range strings.Split(exportedHeaderFlags, " ") {
832 if len(p) > 2 {
833 // Exclude the -I prefix.
834 rbeImplicits = append(rbeImplicits, p[2:])
835 }
836 }
Ramy Medhat6797edc2020-08-28 14:21:55 -0400837 args["implicitInputs"] = strings.Join(rbeImplicits, ",")
Ramy Medhat808594c2020-05-07 06:56:47 -0400838 }
Colin Crossae887032017-10-23 17:16:14 -0700839 ctx.Build(pctx, android.BuildParams{
Ramy Medhat808594c2020-05-07 06:56:47 -0400840 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -0700841 Description: "header-abi-linker " + outputFile.Base(),
842 Output: outputFile,
843 Inputs: sAbiDumps,
Logan Chiene3d7a0d2019-01-17 00:18:02 +0800844 Implicits: implicits,
Ramy Medhat808594c2020-05-07 06:56:47 -0400845 Args: args,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800846 })
847 return android.OptionalPathForPath(outputFile)
848}
849
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500850// unzipRefDump registers a build statement to unzip a reference abi dump.
851func unzipRefDump(ctx android.ModuleContext, zippedRefDump android.Path, baseName string) android.Path {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700852 outputFile := android.PathForModuleOut(ctx, baseName+"_ref.lsdump")
Colin Crossae887032017-10-23 17:16:14 -0700853 ctx.Build(pctx, android.BuildParams{
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700854 Rule: unzipRefSAbiDump,
855 Description: "gunzip" + outputFile.Base(),
856 Output: outputFile,
857 Input: zippedRefDump,
858 })
859 return outputFile
860}
861
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500862// sourceAbiDiff registers a build statement to compare linked sAbi dump files (.ldump).
863func sourceAbiDiff(ctx android.ModuleContext, inputDump android.Path, referenceDump android.Path,
Logan Chien2a65dda2019-10-01 15:58:07 -0700864 baseName, exportedHeaderFlags string, checkAllApis, isLlndk, isNdk, isVndkExt bool) android.OptionalPath {
Logan Chienf3511742017-10-31 18:04:35 +0800865
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800866 outputFile := android.PathForModuleOut(ctx, baseName+".abidiff")
Jayant Chowdharyc7434e22018-05-31 15:42:26 -0700867 libName := strings.TrimSuffix(baseName, filepath.Ext(baseName))
Logan Chien6227fed2019-02-18 13:12:21 +0800868 createReferenceDumpFlags := ""
869
Logan Chien2a65dda2019-10-01 15:58:07 -0700870 var extraFlags []string
871 if checkAllApis {
872 extraFlags = append(extraFlags, "-check-all-apis")
873 } else {
874 extraFlags = append(extraFlags,
875 "-allow-unreferenced-changes",
876 "-allow-unreferenced-elf-symbol-changes")
Jayant Chowdharye4499502018-01-17 13:13:33 -0800877 }
Logan Chien2a65dda2019-10-01 15:58:07 -0700878
879 if exportedHeaderFlags == "" {
880 extraFlags = append(extraFlags, "-advice-only")
881 }
882
Logan Chien62f1f942019-02-18 15:40:42 +0800883 if isLlndk || isNdk {
Logan Chien6227fed2019-02-18 13:12:21 +0800884 createReferenceDumpFlags = "--llndk"
Logan Chien62f1f942019-02-18 15:40:42 +0800885 if isLlndk {
886 // TODO(b/130324828): "-consider-opaque-types-different" should apply to
887 // both LLNDK and NDK shared libs. However, a known issue in header-abi-diff
888 // breaks libaaudio. Remove the if-guard after the issue is fixed.
Logan Chien2a65dda2019-10-01 15:58:07 -0700889 extraFlags = append(extraFlags, "-consider-opaque-types-different")
Logan Chien62f1f942019-02-18 15:40:42 +0800890 }
Jayant Chowdharyc7434e22018-05-31 15:42:26 -0700891 }
Logan Chienf3511742017-10-31 18:04:35 +0800892 if isVndkExt {
Logan Chien2a65dda2019-10-01 15:58:07 -0700893 extraFlags = append(extraFlags, "-allow-extensions")
Logan Chienf3511742017-10-31 18:04:35 +0800894 }
895
Colin Crossae887032017-10-23 17:16:14 -0700896 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700897 Rule: sAbiDiff,
898 Description: "header-abi-diff " + outputFile.Base(),
899 Output: outputFile,
900 Input: inputDump,
901 Implicit: referenceDump,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800902 Args: map[string]string{
Logan Chien6227fed2019-02-18 13:12:21 +0800903 "referenceDump": referenceDump.String(),
904 "libName": libName,
905 "arch": ctx.Arch().ArchType.Name,
Logan Chien2a65dda2019-10-01 15:58:07 -0700906 "extraFlags": strings.Join(extraFlags, " "),
Logan Chien6227fed2019-02-18 13:12:21 +0800907 "createReferenceDumpFlags": createReferenceDumpFlags,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800908 },
909 })
910 return android.OptionalPathForPath(outputFile)
911}
912
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700913// Generate a rule for extracting a table of contents from a shared library (.so)
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500914func transformSharedObjectToToc(ctx android.ModuleContext, inputFile android.Path,
Colin Cross26c34ed2016-09-30 17:10:16 -0700915 outputFile android.WritablePath, flags builderFlags) {
916
Colin Crossb496cfd2018-09-10 16:50:05 -0700917 var format string
918 var crossCompile string
919 if ctx.Darwin() {
920 format = "--macho"
921 crossCompile = "${config.MacToolPath}"
922 } else if ctx.Windows() {
923 format = "--pe"
924 crossCompile = gccCmd(flags.toolchain, "")
925 } else {
926 format = "--elf"
927 crossCompile = gccCmd(flags.toolchain, "")
928 }
Colin Cross26c34ed2016-09-30 17:10:16 -0700929
Colin Crossae887032017-10-23 17:16:14 -0700930 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700931 Rule: toc,
932 Description: "generate toc " + inputFile.Base(),
933 Output: outputFile,
934 Input: inputFile,
Colin Cross26c34ed2016-09-30 17:10:16 -0700935 Args: map[string]string{
936 "crossCompile": crossCompile,
Colin Crossb496cfd2018-09-10 16:50:05 -0700937 "format": format,
Colin Cross26c34ed2016-09-30 17:10:16 -0700938 },
939 })
940}
941
Colin Cross3f40fa42015-01-30 17:27:36 -0800942// Generate a rule for compiling multiple .o files to a .o using ld partial linking
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500943func transformObjsToObj(ctx android.ModuleContext, objFiles android.Paths,
Dan Willemsen724ab5d2019-09-19 10:50:18 -0700944 flags builderFlags, outputFile android.WritablePath, deps android.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800945
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700946 ldCmd := "${config.ClangBin}/clang++"
Colin Cross3f40fa42015-01-30 17:27:36 -0800947
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400948 rule := partialLd
949 args := map[string]string{
950 "ldCmd": ldCmd,
951 "ldFlags": flags.globalLdFlags + " " + flags.localLdFlags,
952 }
Ramy Medhat16f23a42020-09-03 01:29:49 -0400953 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_CXX_LINKS") {
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400954 rule = partialLdRE
955 args["inCommaList"] = strings.Join(objFiles.Strings(), ",")
Ramy Medhat6797edc2020-08-28 14:21:55 -0400956 args["implicitInputs"] = strings.Join(deps.Strings(), ",")
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400957 }
Colin Crossae887032017-10-23 17:16:14 -0700958 ctx.Build(pctx, android.BuildParams{
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400959 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -0700960 Description: "link " + outputFile.Base(),
961 Output: outputFile,
962 Inputs: objFiles,
Dan Willemsen724ab5d2019-09-19 10:50:18 -0700963 Implicits: deps,
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400964 Args: args,
Colin Cross3f40fa42015-01-30 17:27:36 -0800965 })
966}
967
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500968// Generate a rule for running objcopy --prefix-symbols on a binary
969func transformBinaryPrefixSymbols(ctx android.ModuleContext, prefix string, inputFile android.Path,
Colin Cross635c3b02016-05-18 15:37:25 -0700970 flags builderFlags, outputFile android.WritablePath) {
Colin Crossbfae8852015-03-26 14:44:11 -0700971
972 objcopyCmd := gccCmd(flags.toolchain, "objcopy")
973
Colin Crossae887032017-10-23 17:16:14 -0700974 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700975 Rule: prefixSymbols,
976 Description: "prefix symbols " + outputFile.Base(),
977 Output: outputFile,
978 Input: inputFile,
Colin Crossbfae8852015-03-26 14:44:11 -0700979 Args: map[string]string{
980 "objcopyCmd": objcopyCmd,
981 "prefix": prefix,
982 },
983 })
984}
985
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500986// Registers a build statement to invoke `strip` (to discard symbols and data from object files).
987func transformStrip(ctx android.ModuleContext, inputFile android.Path,
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200988 outputFile android.WritablePath, flags StripFlags) {
Colin Cross665dce92016-04-28 14:50:03 -0700989
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200990 crossCompile := gccCmd(flags.Toolchain, "")
Colin Cross665dce92016-04-28 14:50:03 -0700991 args := ""
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200992 if flags.StripAddGnuDebuglink {
Colin Cross665dce92016-04-28 14:50:03 -0700993 args += " --add-gnu-debuglink"
994 }
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200995 if flags.StripKeepMiniDebugInfo {
Colin Cross665dce92016-04-28 14:50:03 -0700996 args += " --keep-mini-debug-info"
997 }
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200998 if flags.StripKeepSymbols {
Colin Cross665dce92016-04-28 14:50:03 -0700999 args += " --keep-symbols"
1000 }
Thiébaud Weksteend4587452020-08-19 14:53:01 +02001001 if flags.StripKeepSymbolsList != "" {
1002 args += " -k" + flags.StripKeepSymbolsList
Yi Kongacee27c2019-03-29 20:05:14 -07001003 }
Thiébaud Weksteend4587452020-08-19 14:53:01 +02001004 if flags.StripKeepSymbolsAndDebugFrame {
Christopher Ferrisb43fe7a2019-05-17 16:39:54 -07001005 args += " --keep-symbols-and-debug-frame"
1006 }
Thiébaud Weksteend4587452020-08-19 14:53:01 +02001007 if flags.StripUseGnuStrip {
Yi Kongb5c34d72018-11-07 16:28:49 -08001008 args += " --use-gnu-strip"
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -07001009 }
Colin Cross665dce92016-04-28 14:50:03 -07001010
Colin Crossae887032017-10-23 17:16:14 -07001011 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -07001012 Rule: strip,
1013 Description: "strip " + outputFile.Base(),
1014 Output: outputFile,
1015 Input: inputFile,
Colin Cross665dce92016-04-28 14:50:03 -07001016 Args: map[string]string{
1017 "crossCompile": crossCompile,
1018 "args": args,
1019 },
1020 })
1021}
1022
Chris Parsonsbf4f55f2020-11-23 17:02:44 -05001023// Registers build statement to invoke `strip` on darwin architecture.
1024func transformDarwinStrip(ctx android.ModuleContext, inputFile android.Path,
Colin Cross635c3b02016-05-18 15:37:25 -07001025 outputFile android.WritablePath) {
Colin Crossb8ecdfe2016-05-03 15:10:29 -07001026
Colin Crossae887032017-10-23 17:16:14 -07001027 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -07001028 Rule: darwinStrip,
1029 Description: "strip " + outputFile.Base(),
1030 Output: outputFile,
1031 Input: inputFile,
Colin Crossb8ecdfe2016-05-03 15:10:29 -07001032 })
1033}
1034
Chris Parsonsbf4f55f2020-11-23 17:02:44 -05001035// Registers build statement to zip one or more coverage files.
1036func transformCoverageFilesToZip(ctx android.ModuleContext,
Oliver Nguyenc7434142019-04-24 14:22:25 -07001037 inputs Objects, baseName string) android.OptionalPath {
Dan Willemsen581341d2017-02-09 16:16:31 -08001038
1039 if len(inputs.coverageFiles) > 0 {
Oliver Nguyenc7434142019-04-24 14:22:25 -07001040 outputFile := android.PathForModuleOut(ctx, baseName+".zip")
Dan Willemsen581341d2017-02-09 16:16:31 -08001041
Oliver Nguyenc7434142019-04-24 14:22:25 -07001042 ctx.Build(pctx, android.BuildParams{
1043 Rule: zip,
1044 Description: "zip " + outputFile.Base(),
1045 Inputs: inputs.coverageFiles,
1046 Output: outputFile,
1047 })
Dan Willemsen581341d2017-02-09 16:16:31 -08001048
1049 return android.OptionalPathForPath(outputFile)
1050 }
1051
1052 return android.OptionalPath{}
1053}
1054
Chris Parsonsbf4f55f2020-11-23 17:02:44 -05001055// Rule to repack an archive (.a) file with a subset of object files.
1056func transformArchiveRepack(ctx android.ModuleContext, inputFile android.Path,
Yi Kongc49c3932019-10-15 02:01:19 -07001057 outputFile android.WritablePath, objects []string) {
1058
1059 ctx.Build(pctx, android.BuildParams{
1060 Rule: archiveRepack,
1061 Description: "Repack archive " + outputFile.Base(),
1062 Output: outputFile,
1063 Input: inputFile,
1064 Args: map[string]string{
1065 "objects": strings.Join(objects, " "),
1066 },
1067 })
1068}
1069
Colin Crossb98c8b02016-07-29 13:44:28 -07001070func gccCmd(toolchain config.Toolchain, cmd string) string {
Colin Cross3f40fa42015-01-30 17:27:36 -08001071 return filepath.Join(toolchain.GccRoot(), "bin", toolchain.GccTriple()+"-"+cmd)
1072}