blob: 9cd78d59a26dfee4ad438eef2e98fb69112129c7 [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,
David Srbecky33dfba12021-03-20 14:46:15 +0000135 Command: "CROSS_COMPILE=$crossCompile XZ=$xzCmd CLANG_BIN=${config.ClangBin} $stripPath ${args} -i ${in} -o ${out} -d ${out}.d",
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500136 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}",
Ulf Adams18931762020-11-25 22:54:35 +0100238 Inputs: []string{"$sAbiLinkerLibs"},
Ramy Medhat31ec9422020-04-17 15:03:58 -0400239 Platform: map[string]string{
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500240 remoteexec.PoolKey: "${config.RECXXPool}",
Ramy Medhat31ec9422020-04-17 15:03:58 -0400241 },
242 }, []string{"cFlags", "exportDirs"}, nil)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800243
Jayant Chowdharya4c6df52018-02-20 12:36:51 -0800244 _ = pctx.SourcePathVariable("sAbiLinker", "prebuilts/clang-tools/${config.HostPrebuiltTag}/bin/header-abi-linker")
Ramy Medhat808594c2020-05-07 06:56:47 -0400245 _ = pctx.SourcePathVariable("sAbiLinkerLibs", "prebuilts/clang-tools/${config.HostPrebuiltTag}/lib64")
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800246
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500247 // Rule to combine .dump sAbi dump files from multiple source files into a single .ldump
248 // sAbi dump file.
Ramy Medhat808594c2020-05-07 06:56:47 -0400249 sAbiLink, sAbiLinkRE = remoteexec.StaticRules(pctx, "sAbiLink",
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800250 blueprint.RuleParams{
Ramy Medhat808594c2020-05-07 06:56:47 -0400251 Command: "$reTemplate$sAbiLinker -o ${out} $symbolFilter -arch $arch $exportedHeaderFlags @${out}.rsp ",
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800252 CommandDeps: []string{"$sAbiLinker"},
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800253 Rspfile: "${out}.rsp",
254 RspfileContent: "${in}",
Ramy Medhat808594c2020-05-07 06:56:47 -0400255 }, &remoteexec.REParams{
256 Labels: map[string]string{"type": "tool", "name": "abi-linker"},
257 ExecStrategy: "${config.REAbiLinkerExecStrategy}",
Ramy Medhat6797edc2020-08-28 14:21:55 -0400258 Inputs: []string{"$sAbiLinkerLibs", "${out}.rsp", "$implicitInputs"},
Ramy Medhat808594c2020-05-07 06:56:47 -0400259 RSPFile: "${out}.rsp",
260 OutputFiles: []string{"$out"},
261 ToolchainInputs: []string{"$sAbiLinker"},
262 Platform: map[string]string{remoteexec.PoolKey: "${config.RECXXPool}"},
Ramy Medhat6797edc2020-08-28 14:21:55 -0400263 }, []string{"symbolFilter", "arch", "exportedHeaderFlags"}, []string{"implicitInputs"})
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800264
Jayant Chowdharya4c6df52018-02-20 12:36:51 -0800265 _ = pctx.SourcePathVariable("sAbiDiffer", "prebuilts/clang-tools/${config.HostPrebuiltTag}/bin/header-abi-diff")
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700266
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500267 // Rule to compare linked sAbi dump files (.ldump).
Colin Cross2e2dbc22019-09-25 13:31:46 -0700268 sAbiDiff = pctx.RuleFunc("sAbiDiff",
Dan Willemsen54daaf02018-03-12 13:24:09 -0700269 func(ctx android.PackageRuleContext) blueprint.RuleParams {
Logan Chien2a65dda2019-10-01 15:58:07 -0700270 commandStr := "($sAbiDiffer ${extraFlags} -lib ${libName} -arch ${arch} -o ${out} -new ${in} -old ${referenceDump})"
Logan Chien6227fed2019-02-18 13:12:21 +0800271 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 +0800272 commandStr += " && (mkdir -p $$DIST_DIR/abidiffs && cp ${out} $$DIST_DIR/abidiffs/)"
Jayant Chowdharyd8b70a32018-02-01 17:23:09 -0800273 commandStr += " && exit 1)"
Jayant Chowdhary219139d2017-11-27 14:52:21 -0800274 return blueprint.RuleParams{
275 Command: commandStr,
276 CommandDeps: []string{"$sAbiDiffer"},
Dan Willemsen54daaf02018-03-12 13:24:09 -0700277 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800278 },
Logan Chien2a65dda2019-10-01 15:58:07 -0700279 "extraFlags", "referenceDump", "libName", "arch", "createReferenceDumpFlags")
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700280
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500281 // Rule to unzip a reference abi dump.
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700282 unzipRefSAbiDump = pctx.AndroidStaticRule("unzipRefSAbiDump",
283 blueprint.RuleParams{
284 Command: "gunzip -c $in > $out",
285 })
Oliver Nguyenc7434142019-04-24 14:22:25 -0700286
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500287 // Rule to zip files.
Oliver Nguyenc7434142019-04-24 14:22:25 -0700288 zip = pctx.AndroidStaticRule("zip",
289 blueprint.RuleParams{
Colin Cross053fca12020-08-19 13:51:47 -0700290 Command: "${SoongZipCmd} -o ${out} -C $$OUT_DIR -r ${out}.rsp",
Oliver Nguyenc7434142019-04-24 14:22:25 -0700291 CommandDeps: []string{"${SoongZipCmd}"},
Colin Cross053fca12020-08-19 13:51:47 -0700292 Rspfile: "${out}.rsp",
Oliver Nguyenc7434142019-04-24 14:22:25 -0700293 RspfileContent: "$in",
294 })
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800295
296 _ = pctx.SourcePathVariable("cxxExtractor",
297 "prebuilts/clang-tools/${config.HostPrebuiltTag}/bin/cxx_extractor")
Sasha Smundak65143642019-09-26 20:14:28 -0700298 _ = pctx.SourcePathVariable("kytheVnames", "build/soong/vnames.json")
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800299 _ = pctx.VariableFunc("kytheCorpus",
300 func(ctx android.PackageVarContext) string { return ctx.Config().XrefCorpusName() })
Sasha Smundak6c2d4f92020-01-09 17:34:23 -0800301 _ = pctx.VariableFunc("kytheCuEncoding",
302 func(ctx android.PackageVarContext) string { return ctx.Config().XrefCuEncoding() })
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500303
304 // Rule to use kythe extractors to generate .kzip files, used to build code cross references.
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800305 kytheExtract = pctx.StaticRule("kythe",
306 blueprint.RuleParams{
Sasha Smundak6c2d4f92020-01-09 17:34:23 -0800307 Command: `rm -f $out && ` +
Sasha Smundaka4ef83b2020-04-21 17:08:35 -0700308 `KYTHE_CORPUS=${kytheCorpus} ` +
309 `KYTHE_OUTPUT_FILE=$out ` +
310 `KYTHE_VNAMES=$kytheVnames ` +
311 `KYTHE_KZIP_ENCODING=${kytheCuEncoding} ` +
312 `KYTHE_CANONICALIZE_VNAME_PATHS=prefer-relative ` +
Sasha Smundak6c2d4f92020-01-09 17:34:23 -0800313 `$cxxExtractor $cFlags $in `,
Sasha Smundak65143642019-09-26 20:14:28 -0700314 CommandDeps: []string{"$cxxExtractor", "$kytheVnames"},
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800315 },
316 "cFlags")
Colin Cross3f40fa42015-01-30 17:27:36 -0800317)
318
Ivan Lozanof3717ee2020-05-20 09:03:20 -0400319func PwdPrefix() string {
320 // Darwin doesn't have /proc
321 if runtime.GOOS != "darwin" {
322 return "PWD=/proc/self/cwd"
323 }
324 return ""
325}
326
Dan Willemsen322a0a62015-11-17 15:19:46 -0800327func init() {
328 // We run gcc/clang with PWD=/proc/self/cwd to remove $TOP from the
329 // debug output. That way two builds in two different directories will
330 // create the same output.
Ivan Lozanof3717ee2020-05-20 09:03:20 -0400331 pctx.StaticVariable("relPwd", PwdPrefix())
Oliver Nguyenc7434142019-04-24 14:22:25 -0700332
333 pctx.HostBinToolVariable("SoongZipCmd", "soong_zip")
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400334 pctx.Import("android/soong/remoteexec")
Dan Willemsen322a0a62015-11-17 15:19:46 -0800335}
336
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500337// builderFlags contains various types of command line flags (and settings) for use in building
338// build statements related to C++.
Colin Cross3f40fa42015-01-30 17:27:36 -0800339type builderFlags struct {
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500340 // Global flags (which build system or toolchain is responsible for). These are separate from
341 // local flags because they should appear first (so that they may be overridden by local flags).
Colin Cross6d88dba2019-11-06 07:06:58 -0800342 globalCommonFlags string
343 globalAsFlags string
344 globalYasmFlags string
345 globalCFlags string
346 globalToolingCFlags string // A separate set of cFlags for clang LibTooling tools
347 globalToolingCppFlags string // A separate set of cppFlags for clang LibTooling tools
348 globalConlyFlags string
349 globalCppFlags string
350 globalLdFlags string
351
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500352 // Local flags (which individual modules are responsible for). These may override global flags.
Colin Cross6d88dba2019-11-06 07:06:58 -0800353 localCommonFlags string
354 localAsFlags string
355 localYasmFlags string
356 localCFlags string
357 localToolingCFlags string // A separate set of cFlags for clang LibTooling tools
358 localToolingCppFlags string // A separate set of cppFlags for clang LibTooling tools
359 localConlyFlags string
360 localCppFlags string
361 localLdFlags string
362
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500363 libFlags string // Flags to add to the linker directly after specifying libraries to link.
364 extraLibFlags string // Flags to add to the linker last.
365 tidyFlags string // Flags that apply to clang-tidy
366 sAbiFlags string // Flags that apply to header-abi-dumps
367 aidlFlags string // Flags that apply to aidl source files
368 rsFlags string // Flags that apply to renderscript source files
Colin Cross6d88dba2019-11-06 07:06:58 -0800369 toolchain config.Toolchain
Colin Cross665dce92016-04-28 14:50:03 -0700370
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500371 // True if these extra features are enabled.
372 tidy bool
373 gcovCoverage bool
374 sAbiDump bool
375 emitXrefs bool
376
377 assemblerWithCpp bool // True if .s files should be processed with the c preprocessor.
Dan Willemsen98ab3112019-08-27 21:20:40 -0700378
Colin Crossc3199482017-03-30 15:03:04 -0700379 systemIncludeFlags string
380
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500381 // True if static libraries should be grouped (using `-Wl,--start-group` and `-Wl,--end-group`).
Colin Cross18c0c5a2016-12-01 14:45:23 -0800382 groupStaticLibs bool
383
Colin Cross19878da2019-03-28 14:45:07 -0700384 proto android.ProtoFlags
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500385 protoC bool // If true, compile protos as `.c` files. Otherwise, output as `.cc`.
386 protoOptionsFile bool // If true, output a proto options file.
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700387
388 yacc *YaccProperties
Matthias Maennich22fd4d12020-07-15 10:58:56 +0200389 lex *LexProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800390}
391
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500392// StripFlags represents flags related to stripping. This is separate from builderFlags, as these
393// flags are useful outside of this package (such as for Rust).
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200394type StripFlags struct {
395 Toolchain config.Toolchain
396 StripKeepSymbols bool
397 StripKeepSymbolsList string
398 StripKeepSymbolsAndDebugFrame bool
399 StripKeepMiniDebugInfo bool
400 StripAddGnuDebuglink bool
401 StripUseGnuStrip bool
402}
403
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500404// Objects is a collection of file paths corresponding to outputs for C++ related build statements.
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700405type Objects struct {
Dan Willemsen581341d2017-02-09 16:16:31 -0800406 objFiles android.Paths
407 tidyFiles android.Paths
408 coverageFiles android.Paths
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800409 sAbiDumpFiles android.Paths
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800410 kytheFiles android.Paths
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700411}
412
413func (a Objects) Copy() Objects {
414 return Objects{
Dan Willemsen581341d2017-02-09 16:16:31 -0800415 objFiles: append(android.Paths{}, a.objFiles...),
416 tidyFiles: append(android.Paths{}, a.tidyFiles...),
417 coverageFiles: append(android.Paths{}, a.coverageFiles...),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800418 sAbiDumpFiles: append(android.Paths{}, a.sAbiDumpFiles...),
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800419 kytheFiles: append(android.Paths{}, a.kytheFiles...),
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700420 }
421}
422
423func (a Objects) Append(b Objects) Objects {
424 return Objects{
Dan Willemsen581341d2017-02-09 16:16:31 -0800425 objFiles: append(a.objFiles, b.objFiles...),
426 tidyFiles: append(a.tidyFiles, b.tidyFiles...),
427 coverageFiles: append(a.coverageFiles, b.coverageFiles...),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800428 sAbiDumpFiles: append(a.sAbiDumpFiles, b.sAbiDumpFiles...),
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800429 kytheFiles: append(a.kytheFiles, b.kytheFiles...),
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700430 }
431}
432
Colin Cross3f40fa42015-01-30 17:27:36 -0800433// Generate rules for compiling multiple .c, .cpp, or .S files to individual .o files
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500434func transformSourceToObj(ctx android.ModuleContext, subdir string, srcFiles android.Paths,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800435 flags builderFlags, pathDeps android.Paths, cFlagsDeps android.Paths) Objects {
Colin Cross581c1892015-04-07 16:50:10 -0700436
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500437 // Source files are one-to-one with tidy, coverage, or kythe files, if enabled.
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700438 objFiles := make(android.Paths, len(srcFiles))
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700439 var tidyFiles android.Paths
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700440 if flags.tidy {
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700441 tidyFiles = make(android.Paths, 0, len(srcFiles))
442 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800443 var coverageFiles android.Paths
Oliver Nguyen04526782020-04-21 12:40:27 -0700444 if flags.gcovCoverage {
Dan Willemsen581341d2017-02-09 16:16:31 -0800445 coverageFiles = make(android.Paths, 0, len(srcFiles))
446 }
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800447 var kytheFiles android.Paths
448 if flags.emitXrefs {
449 kytheFiles = make(android.Paths, 0, len(srcFiles))
450 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800451
Colin Cross6d88dba2019-11-06 07:06:58 -0800452 // Produce fully expanded flags for use by C tools, C compiles, C++ tools, C++ compiles, and asm compiles
453 // respectively.
454 toolingCflags := flags.globalCommonFlags + " " +
455 flags.globalToolingCFlags + " " +
456 flags.globalConlyFlags + " " +
457 flags.localCommonFlags + " " +
458 flags.localToolingCFlags + " " +
459 flags.localConlyFlags + " " +
460 flags.systemIncludeFlags
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700461
Colin Cross6d88dba2019-11-06 07:06:58 -0800462 cflags := flags.globalCommonFlags + " " +
463 flags.globalCFlags + " " +
464 flags.globalConlyFlags + " " +
465 flags.localCommonFlags + " " +
466 flags.localCFlags + " " +
467 flags.localConlyFlags + " " +
468 flags.systemIncludeFlags
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700469
Colin Cross6d88dba2019-11-06 07:06:58 -0800470 toolingCppflags := flags.globalCommonFlags + " " +
471 flags.globalToolingCFlags + " " +
472 flags.globalToolingCppFlags + " " +
473 flags.localCommonFlags + " " +
474 flags.localToolingCFlags + " " +
475 flags.localToolingCppFlags + " " +
476 flags.systemIncludeFlags
Colin Crossc3199482017-03-30 15:03:04 -0700477
Colin Cross6d88dba2019-11-06 07:06:58 -0800478 cppflags := flags.globalCommonFlags + " " +
479 flags.globalCFlags + " " +
480 flags.globalCppFlags + " " +
481 flags.localCommonFlags + " " +
482 flags.localCFlags + " " +
483 flags.localCppFlags + " " +
484 flags.systemIncludeFlags
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700485
Colin Cross6d88dba2019-11-06 07:06:58 -0800486 asflags := flags.globalCommonFlags + " " +
487 flags.globalAsFlags + " " +
488 flags.localCommonFlags + " " +
489 flags.localAsFlags + " " +
490 flags.systemIncludeFlags
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700491
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800492 var sAbiDumpFiles android.Paths
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700493 if flags.sAbiDump {
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800494 sAbiDumpFiles = make(android.Paths, 0, len(srcFiles))
495 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800496
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700497 cflags += " ${config.NoOverrideClangGlobalCflags}"
498 toolingCflags += " ${config.NoOverrideClangGlobalCflags}"
499 cppflags += " ${config.NoOverrideClangGlobalCflags}"
500 toolingCppflags += " ${config.NoOverrideClangGlobalCflags}"
Dan Willemsenbe03f342016-03-03 17:21:04 -0800501
Colin Cross3f40fa42015-01-30 17:27:36 -0800502 for i, srcFile := range srcFiles {
Dan Willemsen21ec4902016-11-02 20:43:13 -0700503 objFile := android.ObjPathWithExt(ctx, subdir, srcFile, "o")
Colin Cross3f40fa42015-01-30 17:27:36 -0800504
505 objFiles[i] = objFile
506
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500507 // Register compilation build statements. The actual rule used depends on the source file type.
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700508 switch srcFile.Ext() {
509 case ".asm":
Colin Crossae887032017-10-23 17:16:14 -0700510 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700511 Rule: yasm,
512 Description: "yasm " + srcFile.Rel(),
513 Output: objFile,
514 Input: srcFile,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800515 Implicits: cFlagsDeps,
516 OrderOnly: pathDeps,
Colin Cross91e90042016-12-02 17:13:24 -0800517 Args: map[string]string{
Colin Cross6d88dba2019-11-06 07:06:58 -0800518 "asFlags": flags.globalYasmFlags + " " + flags.localYasmFlags,
Colin Cross91e90042016-12-02 17:13:24 -0800519 },
520 })
521 continue
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700522 case ".rc":
Colin Crossae887032017-10-23 17:16:14 -0700523 ctx.Build(pctx, android.BuildParams{
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700524 Rule: windres,
525 Description: "windres " + srcFile.Rel(),
526 Output: objFile,
527 Input: srcFile,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800528 Implicits: cFlagsDeps,
529 OrderOnly: pathDeps,
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700530 Args: map[string]string{
531 "windresCmd": gccCmd(flags.toolchain, "windres"),
532 "flags": flags.toolchain.WindresFlags(),
533 },
534 })
535 continue
Pete Bentleyfcf55bf2019-08-16 20:14:32 +0100536 case ".o":
537 objFiles[i] = srcFile
538 continue
Colin Cross91e90042016-12-02 17:13:24 -0800539 }
540
Colin Cross6d88dba2019-11-06 07:06:58 -0800541 var moduleFlags string
542 var moduleToolingFlags string
543
Colin Cross3f40fa42015-01-30 17:27:36 -0800544 var ccCmd string
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700545 tidy := flags.tidy
Oliver Nguyen04526782020-04-21 12:40:27 -0700546 coverage := flags.gcovCoverage
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700547 dump := flags.sAbiDump
Dan Willemsenfcabb1c2019-01-03 23:25:11 -0800548 rule := cc
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800549 emitXref := flags.emitXrefs
Colin Cross3f40fa42015-01-30 17:27:36 -0800550
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700551 switch srcFile.Ext() {
Dan Willemsenfcabb1c2019-01-03 23:25:11 -0800552 case ".s":
Dan Willemsen98ab3112019-08-27 21:20:40 -0700553 if !flags.assemblerWithCpp {
554 rule = ccNoDeps
555 }
Dan Willemsenfcabb1c2019-01-03 23:25:11 -0800556 fallthrough
557 case ".S":
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700558 ccCmd = "clang"
Colin Cross6d88dba2019-11-06 07:06:58 -0800559 moduleFlags = asflags
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700560 tidy = false
Dan Willemsen581341d2017-02-09 16:16:31 -0800561 coverage = false
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800562 dump = false
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800563 emitXref = false
Colin Cross3f40fa42015-01-30 17:27:36 -0800564 case ".c":
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700565 ccCmd = "clang"
Colin Cross6d88dba2019-11-06 07:06:58 -0800566 moduleFlags = cflags
567 moduleToolingFlags = toolingCflags
Colin Crossd34ab7c2019-06-27 14:46:10 -0700568 case ".cpp", ".cc", ".cxx", ".mm":
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700569 ccCmd = "clang++"
Colin Cross6d88dba2019-11-06 07:06:58 -0800570 moduleFlags = cppflags
571 moduleToolingFlags = toolingCppflags
Liz Kammere2861132020-12-03 22:12:41 +0000572 case ".h", ".hpp":
573 ctx.PropertyErrorf("srcs", "Header file %s is not supported, instead use export_include_dirs or local_include_dirs.", srcFile)
574 continue
Colin Cross3f40fa42015-01-30 17:27:36 -0800575 default:
Liz Kammere2861132020-12-03 22:12:41 +0000576 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 -0800577 continue
578 }
579
Colin Cross67a5c132017-05-09 13:45:28 -0700580 ccDesc := ccCmd
581
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700582 ccCmd = "${config.ClangBin}/" + ccCmd
Colin Cross3f40fa42015-01-30 17:27:36 -0800583
Dan Willemsen581341d2017-02-09 16:16:31 -0800584 var implicitOutputs android.WritablePaths
585 if coverage {
586 gcnoFile := android.ObjPathWithExt(ctx, subdir, srcFile, "gcno")
587 implicitOutputs = append(implicitOutputs, gcnoFile)
588 coverageFiles = append(coverageFiles, gcnoFile)
589 }
590
Colin Crossae887032017-10-23 17:16:14 -0700591 ctx.Build(pctx, android.BuildParams{
Dan Willemsenfcabb1c2019-01-03 23:25:11 -0800592 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -0700593 Description: ccDesc + " " + srcFile.Rel(),
Dan Willemsen581341d2017-02-09 16:16:31 -0800594 Output: objFile,
595 ImplicitOutputs: implicitOutputs,
596 Input: srcFile,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800597 Implicits: cFlagsDeps,
598 OrderOnly: pathDeps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800599 Args: map[string]string{
Colin Cross6d88dba2019-11-06 07:06:58 -0800600 "cFlags": moduleFlags,
Colin Cross28344522015-04-22 13:07:53 -0700601 "ccCmd": ccCmd,
Colin Cross3f40fa42015-01-30 17:27:36 -0800602 },
603 })
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700604
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500605 // Register post-process build statements (such as for tidy or kythe).
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800606 if emitXref {
607 kytheFile := android.ObjPathWithExt(ctx, subdir, srcFile, "kzip")
608 ctx.Build(pctx, android.BuildParams{
609 Rule: kytheExtract,
610 Description: "Xref C++ extractor " + srcFile.Rel(),
611 Output: kytheFile,
612 Input: srcFile,
613 Implicits: cFlagsDeps,
614 OrderOnly: pathDeps,
615 Args: map[string]string{
Colin Cross6d88dba2019-11-06 07:06:58 -0800616 "cFlags": moduleFlags,
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800617 },
618 })
619 kytheFiles = append(kytheFiles, kytheFile)
620 }
621
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700622 if tidy {
Dan Willemsen21ec4902016-11-02 20:43:13 -0700623 tidyFile := android.ObjPathWithExt(ctx, subdir, srcFile, "tidy")
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700624 tidyFiles = append(tidyFiles, tidyFile)
625
Kousik Kumar4e30bba2020-06-18 09:17:51 -0700626 rule := clangTidy
Ramy Medhat16f23a42020-09-03 01:29:49 -0400627 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_CLANG_TIDY") {
Kousik Kumar4e30bba2020-06-18 09:17:51 -0700628 rule = clangTidyRE
629 }
630
Colin Crossae887032017-10-23 17:16:14 -0700631 ctx.Build(pctx, android.BuildParams{
Kousik Kumar4e30bba2020-06-18 09:17:51 -0700632 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -0700633 Description: "clang-tidy " + srcFile.Rel(),
634 Output: tidyFile,
635 Input: srcFile,
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700636 // We must depend on objFile, since clang-tidy doesn't
637 // support exporting dependencies.
Dan Willemsen6b4419c2019-08-09 12:45:53 -0700638 Implicit: objFile,
639 Implicits: cFlagsDeps,
640 OrderOnly: pathDeps,
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700641 Args: map[string]string{
Colin Cross6d88dba2019-11-06 07:06:58 -0800642 "cFlags": moduleToolingFlags,
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700643 "tidyFlags": flags.tidyFlags,
644 },
645 })
646 }
647
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800648 if dump {
649 sAbiDumpFile := android.ObjPathWithExt(ctx, subdir, srcFile, "sdump")
650 sAbiDumpFiles = append(sAbiDumpFiles, sAbiDumpFile)
651
Ramy Medhat31ec9422020-04-17 15:03:58 -0400652 dumpRule := sAbiDump
Ramy Medhat16f23a42020-09-03 01:29:49 -0400653 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_ABI_DUMPER") {
Ramy Medhat31ec9422020-04-17 15:03:58 -0400654 dumpRule = sAbiDumpRE
655 }
Colin Crossae887032017-10-23 17:16:14 -0700656 ctx.Build(pctx, android.BuildParams{
Ramy Medhat31ec9422020-04-17 15:03:58 -0400657 Rule: dumpRule,
Colin Cross67a5c132017-05-09 13:45:28 -0700658 Description: "header-abi-dumper " + srcFile.Rel(),
659 Output: sAbiDumpFile,
660 Input: srcFile,
661 Implicit: objFile,
Dan Willemsen6b4419c2019-08-09 12:45:53 -0700662 Implicits: cFlagsDeps,
663 OrderOnly: pathDeps,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800664 Args: map[string]string{
Colin Cross6d88dba2019-11-06 07:06:58 -0800665 "cFlags": moduleToolingFlags,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800666 "exportDirs": flags.sAbiFlags,
667 },
668 })
669 }
670
Colin Cross3f40fa42015-01-30 17:27:36 -0800671 }
672
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700673 return Objects{
Dan Willemsen581341d2017-02-09 16:16:31 -0800674 objFiles: objFiles,
675 tidyFiles: tidyFiles,
676 coverageFiles: coverageFiles,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800677 sAbiDumpFiles: sAbiDumpFiles,
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800678 kytheFiles: kytheFiles,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700679 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800680}
681
682// Generate a rule for compiling multiple .o files to a static library (.a)
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500683func transformObjToStaticLib(ctx android.ModuleContext,
Martin Stjernholm391d94c2020-04-17 17:34:31 +0100684 objFiles android.Paths, wholeStaticLibs android.Paths,
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700685 flags builderFlags, outputFile android.ModuleOutPath, deps android.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800686
Stephen Hinesf1addeb2018-01-09 23:29:04 -0800687 arCmd := "${config.ClangBin}/llvm-ar"
Martin Stjernholm391d94c2020-04-17 17:34:31 +0100688 arFlags := ""
Stephen Hinesf1addeb2018-01-09 23:29:04 -0800689 if !ctx.Darwin() {
690 arFlags += " -format=gnu"
691 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800692
Martin Stjernholm391d94c2020-04-17 17:34:31 +0100693 if len(wholeStaticLibs) == 0 {
694 ctx.Build(pctx, android.BuildParams{
695 Rule: ar,
696 Description: "static link " + outputFile.Base(),
697 Output: outputFile,
698 Inputs: objFiles,
699 Implicits: deps,
700 Args: map[string]string{
701 "arFlags": "crsPD" + arFlags,
702 "arCmd": arCmd,
703 },
704 })
705
706 } else {
707 ctx.Build(pctx, android.BuildParams{
708 Rule: arWithLibs,
709 Description: "static link " + outputFile.Base(),
710 Output: outputFile,
711 Inputs: append(objFiles, wholeStaticLibs...),
712 Implicits: deps,
713 Args: map[string]string{
714 "arCmd": arCmd,
715 "arObjFlags": "crsPD" + arFlags,
716 "arObjs": strings.Join(objFiles.Strings(), " "),
717 "arLibFlags": "cqsL" + arFlags,
718 "arLibs": strings.Join(wholeStaticLibs.Strings(), " "),
719 },
720 })
721 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800722}
723
724// Generate a rule for compiling multiple .o files, plus static libraries, whole static libraries,
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700725// and shared libraries, to a shared library (.so) or dynamic executable
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500726func transformObjToDynamicBinary(ctx android.ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -0700727 objFiles, sharedLibs, staticLibs, lateStaticLibs, wholeStaticLibs, deps android.Paths,
Josh Gao75a50a22019-06-07 17:58:59 -0700728 crtBegin, crtEnd android.OptionalPath, groupLate bool, flags builderFlags, outputFile android.WritablePath, implicitOutputs android.WritablePaths) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800729
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700730 ldCmd := "${config.ClangBin}/clang++"
Colin Cross3f40fa42015-01-30 17:27:36 -0800731
Colin Cross3f40fa42015-01-30 17:27:36 -0800732 var libFlagsList []string
733
Colin Cross16b23492016-01-06 14:41:07 -0800734 if len(flags.libFlags) > 0 {
735 libFlagsList = append(libFlagsList, flags.libFlags)
736 }
737
Colin Cross3f40fa42015-01-30 17:27:36 -0800738 if len(wholeStaticLibs) > 0 {
Dan Willemsen490fd492015-11-24 17:53:15 -0800739 if ctx.Host() && ctx.Darwin() {
Colin Cross635c3b02016-05-18 15:37:25 -0700740 libFlagsList = append(libFlagsList, android.JoinWithPrefix(wholeStaticLibs.Strings(), "-force_load "))
Colin Cross0af4b842015-04-30 16:36:18 -0700741 } else {
742 libFlagsList = append(libFlagsList, "-Wl,--whole-archive ")
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700743 libFlagsList = append(libFlagsList, wholeStaticLibs.Strings()...)
Colin Cross0af4b842015-04-30 16:36:18 -0700744 libFlagsList = append(libFlagsList, "-Wl,--no-whole-archive ")
745 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800746 }
747
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,--start-group")
750 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700751 libFlagsList = append(libFlagsList, staticLibs.Strings()...)
Colin Cross7a7cf972016-12-05 18:47:39 -0800752 if flags.groupStaticLibs && !ctx.Darwin() && len(staticLibs) > 0 {
Colin Cross18c0c5a2016-12-01 14:45:23 -0800753 libFlagsList = append(libFlagsList, "-Wl,--end-group")
754 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800755
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,--start-group")
758 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700759 libFlagsList = append(libFlagsList, lateStaticLibs.Strings()...)
Stephen Hines10347862016-07-18 15:54:54 -0700760 if groupLate && !ctx.Darwin() && len(lateStaticLibs) > 0 {
Dan Willemsenedc385f2015-07-08 13:02:23 -0700761 libFlagsList = append(libFlagsList, "-Wl,--end-group")
762 }
763
Colin Cross3f40fa42015-01-30 17:27:36 -0800764 for _, lib := range sharedLibs {
Josh Gao75a50a22019-06-07 17:58:59 -0700765 libFile := lib.String()
766 if ctx.Windows() {
767 libFile = pathtools.ReplaceExtension(libFile, "lib")
768 }
769 libFlagsList = append(libFlagsList, libFile)
Colin Cross3f40fa42015-01-30 17:27:36 -0800770 }
771
Colin Cross3f40fa42015-01-30 17:27:36 -0800772 deps = append(deps, staticLibs...)
Colin Cross3075ad02015-03-17 10:47:08 -0700773 deps = append(deps, lateStaticLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800774 deps = append(deps, wholeStaticLibs...)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700775 if crtBegin.Valid() {
776 deps = append(deps, crtBegin.Path(), crtEnd.Path())
Colin Cross3f40fa42015-01-30 17:27:36 -0800777 }
778
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400779 rule := ld
Kousik Kumar3fb61262020-04-22 13:31:09 -0700780 args := map[string]string{
781 "ldCmd": ldCmd,
782 "crtBegin": crtBegin.String(),
783 "libFlags": strings.Join(libFlagsList, " "),
784 "extraLibFlags": flags.extraLibFlags,
785 "ldFlags": flags.globalLdFlags + " " + flags.localLdFlags,
786 "crtEnd": crtEnd.String(),
787 }
Ramy Medhat16f23a42020-09-03 01:29:49 -0400788 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_CXX_LINKS") {
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400789 rule = ldRE
Kousik Kumar3fb61262020-04-22 13:31:09 -0700790 args["implicitOutputs"] = strings.Join(implicitOutputs.Strings(), ",")
Ramy Medhat6797edc2020-08-28 14:21:55 -0400791 args["implicitInputs"] = strings.Join(deps.Strings(), ",")
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400792 }
793
Colin Crossae887032017-10-23 17:16:14 -0700794 ctx.Build(pctx, android.BuildParams{
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400795 Rule: rule,
Josh Gao75a50a22019-06-07 17:58:59 -0700796 Description: "link " + outputFile.Base(),
797 Output: outputFile,
798 ImplicitOutputs: implicitOutputs,
799 Inputs: objFiles,
800 Implicits: deps,
Kousik Kumar3fb61262020-04-22 13:31:09 -0700801 Args: args,
Colin Cross3f40fa42015-01-30 17:27:36 -0800802 })
803}
804
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800805// Generate a rule to combine .dump sAbi dump files from multiple source files
806// into a single .ldump sAbi dump file
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500807func transformDumpToLinkedDump(ctx android.ModuleContext, sAbiDumps android.Paths, soFile android.Path,
Logan Chiene3d7a0d2019-01-17 00:18:02 +0800808 baseName, exportedHeaderFlags string, symbolFile android.OptionalPath,
809 excludedSymbolVersions, excludedSymbolTags []string) android.OptionalPath {
810
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800811 outputFile := android.PathForModuleOut(ctx, baseName+".lsdump")
Logan Chiene3d7a0d2019-01-17 00:18:02 +0800812
813 implicits := android.Paths{soFile}
Jayant Chowdharydf344d52018-01-17 11:11:42 -0800814 symbolFilterStr := "-so " + soFile.String()
Logan Chiene3d7a0d2019-01-17 00:18:02 +0800815
816 if symbolFile.Valid() {
817 implicits = append(implicits, symbolFile.Path())
818 symbolFilterStr += " -v " + symbolFile.String()
819 }
820 for _, ver := range excludedSymbolVersions {
821 symbolFilterStr += " --exclude-symbol-version " + ver
822 }
823 for _, tag := range excludedSymbolTags {
824 symbolFilterStr += " --exclude-symbol-tag " + tag
825 }
Ramy Medhat808594c2020-05-07 06:56:47 -0400826 rule := sAbiLink
827 args := map[string]string{
828 "symbolFilter": symbolFilterStr,
829 "arch": ctx.Arch().ArchType.Name,
830 "exportedHeaderFlags": exportedHeaderFlags,
831 }
Ramy Medhat16f23a42020-09-03 01:29:49 -0400832 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_ABI_LINKER") {
Ramy Medhat808594c2020-05-07 06:56:47 -0400833 rule = sAbiLinkRE
834 rbeImplicits := implicits.Strings()
835 for _, p := range strings.Split(exportedHeaderFlags, " ") {
836 if len(p) > 2 {
837 // Exclude the -I prefix.
838 rbeImplicits = append(rbeImplicits, p[2:])
839 }
840 }
Ramy Medhat6797edc2020-08-28 14:21:55 -0400841 args["implicitInputs"] = strings.Join(rbeImplicits, ",")
Ramy Medhat808594c2020-05-07 06:56:47 -0400842 }
Colin Crossae887032017-10-23 17:16:14 -0700843 ctx.Build(pctx, android.BuildParams{
Ramy Medhat808594c2020-05-07 06:56:47 -0400844 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -0700845 Description: "header-abi-linker " + outputFile.Base(),
846 Output: outputFile,
847 Inputs: sAbiDumps,
Logan Chiene3d7a0d2019-01-17 00:18:02 +0800848 Implicits: implicits,
Ramy Medhat808594c2020-05-07 06:56:47 -0400849 Args: args,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800850 })
851 return android.OptionalPathForPath(outputFile)
852}
853
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500854// unzipRefDump registers a build statement to unzip a reference abi dump.
855func unzipRefDump(ctx android.ModuleContext, zippedRefDump android.Path, baseName string) android.Path {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700856 outputFile := android.PathForModuleOut(ctx, baseName+"_ref.lsdump")
Colin Crossae887032017-10-23 17:16:14 -0700857 ctx.Build(pctx, android.BuildParams{
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700858 Rule: unzipRefSAbiDump,
859 Description: "gunzip" + outputFile.Base(),
860 Output: outputFile,
861 Input: zippedRefDump,
862 })
863 return outputFile
864}
865
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500866// sourceAbiDiff registers a build statement to compare linked sAbi dump files (.ldump).
867func sourceAbiDiff(ctx android.ModuleContext, inputDump android.Path, referenceDump android.Path,
Logan Chien2a65dda2019-10-01 15:58:07 -0700868 baseName, exportedHeaderFlags string, checkAllApis, isLlndk, isNdk, isVndkExt bool) android.OptionalPath {
Logan Chienf3511742017-10-31 18:04:35 +0800869
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800870 outputFile := android.PathForModuleOut(ctx, baseName+".abidiff")
Jayant Chowdharyc7434e22018-05-31 15:42:26 -0700871 libName := strings.TrimSuffix(baseName, filepath.Ext(baseName))
Logan Chien6227fed2019-02-18 13:12:21 +0800872 createReferenceDumpFlags := ""
873
Logan Chien2a65dda2019-10-01 15:58:07 -0700874 var extraFlags []string
875 if checkAllApis {
876 extraFlags = append(extraFlags, "-check-all-apis")
877 } else {
878 extraFlags = append(extraFlags,
879 "-allow-unreferenced-changes",
880 "-allow-unreferenced-elf-symbol-changes")
Jayant Chowdharye4499502018-01-17 13:13:33 -0800881 }
Logan Chien2a65dda2019-10-01 15:58:07 -0700882
883 if exportedHeaderFlags == "" {
884 extraFlags = append(extraFlags, "-advice-only")
885 }
886
Logan Chien62f1f942019-02-18 15:40:42 +0800887 if isLlndk || isNdk {
Logan Chien6227fed2019-02-18 13:12:21 +0800888 createReferenceDumpFlags = "--llndk"
Logan Chien62f1f942019-02-18 15:40:42 +0800889 if isLlndk {
890 // TODO(b/130324828): "-consider-opaque-types-different" should apply to
891 // both LLNDK and NDK shared libs. However, a known issue in header-abi-diff
892 // breaks libaaudio. Remove the if-guard after the issue is fixed.
Logan Chien2a65dda2019-10-01 15:58:07 -0700893 extraFlags = append(extraFlags, "-consider-opaque-types-different")
Logan Chien62f1f942019-02-18 15:40:42 +0800894 }
Jayant Chowdharyc7434e22018-05-31 15:42:26 -0700895 }
Logan Chienf3511742017-10-31 18:04:35 +0800896 if isVndkExt {
Logan Chien2a65dda2019-10-01 15:58:07 -0700897 extraFlags = append(extraFlags, "-allow-extensions")
Logan Chienf3511742017-10-31 18:04:35 +0800898 }
899
Colin Crossae887032017-10-23 17:16:14 -0700900 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700901 Rule: sAbiDiff,
902 Description: "header-abi-diff " + outputFile.Base(),
903 Output: outputFile,
904 Input: inputDump,
905 Implicit: referenceDump,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800906 Args: map[string]string{
Logan Chien6227fed2019-02-18 13:12:21 +0800907 "referenceDump": referenceDump.String(),
908 "libName": libName,
909 "arch": ctx.Arch().ArchType.Name,
Logan Chien2a65dda2019-10-01 15:58:07 -0700910 "extraFlags": strings.Join(extraFlags, " "),
Logan Chien6227fed2019-02-18 13:12:21 +0800911 "createReferenceDumpFlags": createReferenceDumpFlags,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800912 },
913 })
914 return android.OptionalPathForPath(outputFile)
915}
916
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700917// Generate a rule for extracting a table of contents from a shared library (.so)
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500918func transformSharedObjectToToc(ctx android.ModuleContext, inputFile android.Path,
Colin Cross26c34ed2016-09-30 17:10:16 -0700919 outputFile android.WritablePath, flags builderFlags) {
920
Colin Crossb496cfd2018-09-10 16:50:05 -0700921 var format string
922 var crossCompile string
923 if ctx.Darwin() {
924 format = "--macho"
925 crossCompile = "${config.MacToolPath}"
926 } else if ctx.Windows() {
927 format = "--pe"
928 crossCompile = gccCmd(flags.toolchain, "")
929 } else {
930 format = "--elf"
931 crossCompile = gccCmd(flags.toolchain, "")
932 }
Colin Cross26c34ed2016-09-30 17:10:16 -0700933
Colin Crossae887032017-10-23 17:16:14 -0700934 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700935 Rule: toc,
936 Description: "generate toc " + inputFile.Base(),
937 Output: outputFile,
938 Input: inputFile,
Colin Cross26c34ed2016-09-30 17:10:16 -0700939 Args: map[string]string{
940 "crossCompile": crossCompile,
Colin Crossb496cfd2018-09-10 16:50:05 -0700941 "format": format,
Colin Cross26c34ed2016-09-30 17:10:16 -0700942 },
943 })
944}
945
Colin Cross3f40fa42015-01-30 17:27:36 -0800946// Generate a rule for compiling multiple .o files to a .o using ld partial linking
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500947func transformObjsToObj(ctx android.ModuleContext, objFiles android.Paths,
Dan Willemsen724ab5d2019-09-19 10:50:18 -0700948 flags builderFlags, outputFile android.WritablePath, deps android.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800949
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700950 ldCmd := "${config.ClangBin}/clang++"
Colin Cross3f40fa42015-01-30 17:27:36 -0800951
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400952 rule := partialLd
953 args := map[string]string{
954 "ldCmd": ldCmd,
955 "ldFlags": flags.globalLdFlags + " " + flags.localLdFlags,
956 }
Ramy Medhat16f23a42020-09-03 01:29:49 -0400957 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_CXX_LINKS") {
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400958 rule = partialLdRE
959 args["inCommaList"] = strings.Join(objFiles.Strings(), ",")
Ramy Medhat6797edc2020-08-28 14:21:55 -0400960 args["implicitInputs"] = strings.Join(deps.Strings(), ",")
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400961 }
Colin Crossae887032017-10-23 17:16:14 -0700962 ctx.Build(pctx, android.BuildParams{
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400963 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -0700964 Description: "link " + outputFile.Base(),
965 Output: outputFile,
966 Inputs: objFiles,
Dan Willemsen724ab5d2019-09-19 10:50:18 -0700967 Implicits: deps,
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400968 Args: args,
Colin Cross3f40fa42015-01-30 17:27:36 -0800969 })
970}
971
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500972// Generate a rule for running objcopy --prefix-symbols on a binary
973func transformBinaryPrefixSymbols(ctx android.ModuleContext, prefix string, inputFile android.Path,
Colin Cross635c3b02016-05-18 15:37:25 -0700974 flags builderFlags, outputFile android.WritablePath) {
Colin Crossbfae8852015-03-26 14:44:11 -0700975
976 objcopyCmd := gccCmd(flags.toolchain, "objcopy")
977
Colin Crossae887032017-10-23 17:16:14 -0700978 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700979 Rule: prefixSymbols,
980 Description: "prefix symbols " + outputFile.Base(),
981 Output: outputFile,
982 Input: inputFile,
Colin Crossbfae8852015-03-26 14:44:11 -0700983 Args: map[string]string{
984 "objcopyCmd": objcopyCmd,
985 "prefix": prefix,
986 },
987 })
988}
989
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500990// Registers a build statement to invoke `strip` (to discard symbols and data from object files).
991func transformStrip(ctx android.ModuleContext, inputFile android.Path,
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200992 outputFile android.WritablePath, flags StripFlags) {
Colin Cross665dce92016-04-28 14:50:03 -0700993
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200994 crossCompile := gccCmd(flags.Toolchain, "")
Colin Cross665dce92016-04-28 14:50:03 -0700995 args := ""
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200996 if flags.StripAddGnuDebuglink {
Colin Cross665dce92016-04-28 14:50:03 -0700997 args += " --add-gnu-debuglink"
998 }
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200999 if flags.StripKeepMiniDebugInfo {
Colin Cross665dce92016-04-28 14:50:03 -07001000 args += " --keep-mini-debug-info"
1001 }
Thiébaud Weksteend4587452020-08-19 14:53:01 +02001002 if flags.StripKeepSymbols {
Colin Cross665dce92016-04-28 14:50:03 -07001003 args += " --keep-symbols"
1004 }
Thiébaud Weksteend4587452020-08-19 14:53:01 +02001005 if flags.StripKeepSymbolsList != "" {
1006 args += " -k" + flags.StripKeepSymbolsList
Yi Kongacee27c2019-03-29 20:05:14 -07001007 }
Thiébaud Weksteend4587452020-08-19 14:53:01 +02001008 if flags.StripKeepSymbolsAndDebugFrame {
Christopher Ferrisb43fe7a2019-05-17 16:39:54 -07001009 args += " --keep-symbols-and-debug-frame"
1010 }
Thiébaud Weksteend4587452020-08-19 14:53:01 +02001011 if flags.StripUseGnuStrip {
Yi Kongb5c34d72018-11-07 16:28:49 -08001012 args += " --use-gnu-strip"
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -07001013 }
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{
1021 "crossCompile": crossCompile,
1022 "args": args,
1023 },
1024 })
1025}
1026
Chris Parsonsbf4f55f2020-11-23 17:02:44 -05001027// Registers build statement to invoke `strip` on darwin architecture.
1028func transformDarwinStrip(ctx android.ModuleContext, inputFile android.Path,
Colin Cross635c3b02016-05-18 15:37:25 -07001029 outputFile android.WritablePath) {
Colin Crossb8ecdfe2016-05-03 15:10:29 -07001030
Colin Crossae887032017-10-23 17:16:14 -07001031 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -07001032 Rule: darwinStrip,
1033 Description: "strip " + outputFile.Base(),
1034 Output: outputFile,
1035 Input: inputFile,
Colin Crossb8ecdfe2016-05-03 15:10:29 -07001036 })
1037}
1038
Chris Parsonsbf4f55f2020-11-23 17:02:44 -05001039// Registers build statement to zip one or more coverage files.
1040func transformCoverageFilesToZip(ctx android.ModuleContext,
Oliver Nguyenc7434142019-04-24 14:22:25 -07001041 inputs Objects, baseName string) android.OptionalPath {
Dan Willemsen581341d2017-02-09 16:16:31 -08001042
1043 if len(inputs.coverageFiles) > 0 {
Oliver Nguyenc7434142019-04-24 14:22:25 -07001044 outputFile := android.PathForModuleOut(ctx, baseName+".zip")
Dan Willemsen581341d2017-02-09 16:16:31 -08001045
Oliver Nguyenc7434142019-04-24 14:22:25 -07001046 ctx.Build(pctx, android.BuildParams{
1047 Rule: zip,
1048 Description: "zip " + outputFile.Base(),
1049 Inputs: inputs.coverageFiles,
1050 Output: outputFile,
1051 })
Dan Willemsen581341d2017-02-09 16:16:31 -08001052
1053 return android.OptionalPathForPath(outputFile)
1054 }
1055
1056 return android.OptionalPath{}
1057}
1058
Chris Parsonsbf4f55f2020-11-23 17:02:44 -05001059// Rule to repack an archive (.a) file with a subset of object files.
1060func transformArchiveRepack(ctx android.ModuleContext, inputFile android.Path,
Yi Kongc49c3932019-10-15 02:01:19 -07001061 outputFile android.WritablePath, objects []string) {
1062
1063 ctx.Build(pctx, android.BuildParams{
1064 Rule: archiveRepack,
1065 Description: "Repack archive " + outputFile.Base(),
1066 Output: outputFile,
1067 Input: inputFile,
1068 Args: map[string]string{
1069 "objects": strings.Join(objects, " "),
1070 },
1071 })
1072}
1073
Colin Crossb98c8b02016-07-29 13:44:28 -07001074func gccCmd(toolchain config.Toolchain, cmd string) string {
Colin Cross3f40fa42015-01-30 17:27:36 -08001075 return filepath.Join(toolchain.GccRoot(), "bin", toolchain.GccTriple()+"-"+cmd)
1076}