blob: 273cdd3f40a6bb7d7748d6a0702eb7f00c9b3c0a [file] [log] [blame]
Colin Cross3f40fa42015-01-30 17:27:36 -08001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package cc
16
17// This file generates the final rules for compiling all C/C++. All properties related to
18// compiling should have been translated into builderFlags or another argument to the Transform*
19// functions.
20
21import (
Colin Crossb98c8b02016-07-29 13:44:28 -070022 "path/filepath"
Colin Cross0af4b842015-04-30 16:36:18 -070023 "runtime"
Colin Cross3f40fa42015-01-30 17:27:36 -080024 "strings"
Colin Crossed4cf0b2015-03-26 14:43:45 -070025
26 "github.com/google/blueprint"
Josh Gao75a50a22019-06-07 17:58:59 -070027 "github.com/google/blueprint/pathtools"
Colin Crossb98c8b02016-07-29 13:44:28 -070028
29 "android/soong/android"
30 "android/soong/cc/config"
Ramy Medhat9a90fe52020-04-13 13:21:23 -040031 "android/soong/remoteexec"
Colin Cross3f40fa42015-01-30 17:27:36 -080032)
33
34const (
Dan Albertc3144b12015-04-28 18:17:56 -070035 objectExtension = ".o"
Colin Cross3f40fa42015-01-30 17:27:36 -080036 staticLibraryExtension = ".a"
37)
38
39var (
Colin Cross635c3b02016-05-18 15:37:25 -070040 pctx = android.NewPackageContext("android/soong/cc")
Colin Cross3f40fa42015-01-30 17:27:36 -080041
Chris Parsonsbf4f55f2020-11-23 17:02:44 -050042 // Rule to invoke gcc with given command, flags, and dependencies. Outputs a .d depfile.
Ramy Medhat8ea054a2020-01-27 14:19:44 -050043 cc = pctx.AndroidRemoteStaticRule("cc", android.RemoteRuleSupports{Goma: true, RBE: true},
Colin Cross3f40fa42015-01-30 17:27:36 -080044 blueprint.RuleParams{
45 Depfile: "${out}.d",
46 Deps: blueprint.DepsGCC,
Kousik Kumard207cbe2020-10-20 05:52:49 +000047 Command: "$relPwd ${config.CcWrapper}$ccCmd -c $cFlags -MD -MF ${out}.d -o $out $in",
Dan Willemsenc94a7682015-11-17 15:27:28 -080048 CommandDeps: []string{"$ccCmd"},
Colin Cross3f40fa42015-01-30 17:27:36 -080049 },
Dan Willemsen322a0a62015-11-17 15:19:46 -080050 "ccCmd", "cFlags")
Colin Cross3f40fa42015-01-30 17:27:36 -080051
Chris Parsonsbf4f55f2020-11-23 17:02:44 -050052 // Rule to invoke gcc with given command and flags, but no dependencies.
Kousik Kumar2976bfd2020-02-17 00:26:55 -080053 ccNoDeps = pctx.AndroidStaticRule("ccNoDeps",
Dan Willemsenfcabb1c2019-01-03 23:25:11 -080054 blueprint.RuleParams{
Kousik Kumard207cbe2020-10-20 05:52:49 +000055 Command: "$relPwd $ccCmd -c $cFlags -o $out $in",
Dan Willemsenfcabb1c2019-01-03 23:25:11 -080056 CommandDeps: []string{"$ccCmd"},
57 },
58 "ccCmd", "cFlags")
59
Chris Parsonsbf4f55f2020-11-23 17:02:44 -050060 // Rules to invoke ld to link binaries. Uses a .rsp file to list dependencies, as there may
61 // be many.
Colin Cross77cdcfd2021-03-12 11:28:25 -080062 ld, ldRE = pctx.RemoteStaticRules("ld",
Colin Cross3f40fa42015-01-30 17:27:36 -080063 blueprint.RuleParams{
Ramy Medhat31ec9422020-04-17 15:03:58 -040064 Command: "$reTemplate$ldCmd ${crtBegin} @${out}.rsp " +
Pete Bentley99f2fc22019-08-02 14:02:20 +010065 "${libFlags} ${crtEnd} -o ${out} ${ldFlags} ${extraLibFlags}",
Dan Willemsenc94a7682015-11-17 15:27:28 -080066 CommandDeps: []string{"$ldCmd"},
Colin Cross7d21c442015-03-30 17:47:53 -070067 Rspfile: "${out}.rsp",
68 RspfileContent: "${in}",
Colin Cross36ae1352019-03-29 15:55:30 -070069 // clang -Wl,--out-implib doesn't update its output file if it hasn't changed.
70 Restat: true,
Colin Cross3f40fa42015-01-30 17:27:36 -080071 },
Ramy Medhat31ec9422020-04-17 15:03:58 -040072 &remoteexec.REParams{
73 Labels: map[string]string{"type": "link", "tool": "clang"},
Ramy Medhat9a90fe52020-04-13 13:21:23 -040074 ExecStrategy: "${config.RECXXLinksExecStrategy}",
Ramy Medhat6797edc2020-08-28 14:21:55 -040075 Inputs: []string{"${out}.rsp", "$implicitInputs"},
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.
Colin Cross77cdcfd2021-03-12 11:28:25 -080083 partialLd, partialLdRE = pctx.RemoteStaticRules("partialLd",
Colin Cross3f40fa42015-01-30 17:27:36 -080084 blueprint.RuleParams{
Chih-Hung Hsieh3ede2942018-01-10 14:30:44 -080085 // Without -no-pie, clang 7.0 adds -pie to link Android files,
86 // but -r and -pie cannot be used together.
Ramy Medhat31ec9422020-04-17 15:03:58 -040087 Command: "$reTemplate$ldCmd -fuse-ld=lld -nostdlib -no-pie -Wl,-r ${in} -o ${out} ${ldFlags}",
Dan Willemsenc94a7682015-11-17 15:27:28 -080088 CommandDeps: []string{"$ldCmd"},
Ramy Medhat9a90fe52020-04-13 13:21:23 -040089 }, &remoteexec.REParams{
Ramy Medhat6797edc2020-08-28 14:21:55 -040090 Labels: map[string]string{"type": "link", "tool": "clang"},
91 ExecStrategy: "${config.RECXXLinksExecStrategy}",
92 Inputs: []string{"$inCommaList", "$implicitInputs"},
Kousik Kumar3fb61262020-04-22 13:31:09 -070093 OutputFiles: []string{"${out}", "$implicitOutputs"},
Ramy Medhat9a90fe52020-04-13 13:21:23 -040094 ToolchainInputs: []string{"$ldCmd"},
95 Platform: map[string]string{remoteexec.PoolKey: "${config.RECXXLinksPool}"},
Ramy Medhat6797edc2020-08-28 14:21:55 -040096 }, []string{"ldCmd", "ldFlags"}, []string{"implicitInputs", "inCommaList", "implicitOutputs"})
Colin Cross3f40fa42015-01-30 17:27:36 -080097
Chris Parsonsbf4f55f2020-11-23 17:02:44 -050098 // Rule to invoke `ar` with given cmd and flags, but no static library depenencies.
Colin Cross9d45bb72016-08-29 16:14:13 -070099 ar = pctx.AndroidStaticRule("ar",
Colin Cross3f40fa42015-01-30 17:27:36 -0800100 blueprint.RuleParams{
Colin Cross7d21c442015-03-30 17:47:53 -0700101 Command: "rm -f ${out} && $arCmd $arFlags $out @${out}.rsp",
Dan Willemsenc94a7682015-11-17 15:27:28 -0800102 CommandDeps: []string{"$arCmd"},
Colin Cross7d21c442015-03-30 17:47:53 -0700103 Rspfile: "${out}.rsp",
104 RspfileContent: "${in}",
Colin Cross3f40fa42015-01-30 17:27:36 -0800105 },
106 "arCmd", "arFlags")
107
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500108 // Rule to invoke `ar` with given cmd, flags, and library dependencies. Generates a .a
109 // (archive) file from .o files.
Martin Stjernholm391d94c2020-04-17 17:34:31 +0100110 arWithLibs = pctx.AndroidStaticRule("arWithLibs",
111 blueprint.RuleParams{
112 Command: "rm -f ${out} && $arCmd $arObjFlags $out @${out}.rsp && $arCmd $arLibFlags $out $arLibs",
113 CommandDeps: []string{"$arCmd"},
114 Rspfile: "${out}.rsp",
115 RspfileContent: "${arObjs}",
116 },
117 "arCmd", "arObjFlags", "arObjs", "arLibFlags", "arLibs")
118
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500119 // Rule to run objcopy --prefix-symbols (to prefix all symbols in a file with a given string).
Colin Cross9d45bb72016-08-29 16:14:13 -0700120 prefixSymbols = pctx.AndroidStaticRule("prefixSymbols",
Colin Crossbfae8852015-03-26 14:44:11 -0700121 blueprint.RuleParams{
122 Command: "$objcopyCmd --prefix-symbols=${prefix} ${in} ${out}",
Dan Willemsenc94a7682015-11-17 15:27:28 -0800123 CommandDeps: []string{"$objcopyCmd"},
Colin Crossbfae8852015-03-26 14:44:11 -0700124 },
125 "objcopyCmd", "prefix")
126
Nan Zhang43a485c2017-03-27 14:27:58 -0700127 _ = pctx.SourcePathVariable("stripPath", "build/soong/scripts/strip.sh")
Dan Willemsen8fec83a2018-03-09 10:47:52 -0800128 _ = pctx.SourcePathVariable("xzCmd", "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/xz")
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).
Colin Cross77cdcfd2021-03-12 11:28:25 -0800192 clangTidy, clangTidyRE = pctx.RemoteStaticRules("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.
Colin Cross77cdcfd2021-03-12 11:28:25 -0800231 sAbiDump, sAbiDumpRE = pctx.RemoteStaticRules("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.
Colin Cross77cdcfd2021-03-12 11:28:25 -0800249 sAbiLink, sAbiLinkRE = pctx.RemoteStaticRules("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")
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
Liz Kammere2861132020-12-03 22:12:41 +0000571 case ".h", ".hpp":
572 ctx.PropertyErrorf("srcs", "Header file %s is not supported, instead use export_include_dirs or local_include_dirs.", srcFile)
573 continue
Colin Cross3f40fa42015-01-30 17:27:36 -0800574 default:
Liz Kammere2861132020-12-03 22:12:41 +0000575 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 -0800576 continue
577 }
578
Colin Cross67a5c132017-05-09 13:45:28 -0700579 ccDesc := ccCmd
580
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700581 ccCmd = "${config.ClangBin}/" + ccCmd
Colin Cross3f40fa42015-01-30 17:27:36 -0800582
Dan Willemsen581341d2017-02-09 16:16:31 -0800583 var implicitOutputs android.WritablePaths
584 if coverage {
585 gcnoFile := android.ObjPathWithExt(ctx, subdir, srcFile, "gcno")
586 implicitOutputs = append(implicitOutputs, gcnoFile)
587 coverageFiles = append(coverageFiles, gcnoFile)
588 }
589
Colin Crossae887032017-10-23 17:16:14 -0700590 ctx.Build(pctx, android.BuildParams{
Dan Willemsenfcabb1c2019-01-03 23:25:11 -0800591 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -0700592 Description: ccDesc + " " + srcFile.Rel(),
Dan Willemsen581341d2017-02-09 16:16:31 -0800593 Output: objFile,
594 ImplicitOutputs: implicitOutputs,
595 Input: srcFile,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800596 Implicits: cFlagsDeps,
597 OrderOnly: pathDeps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800598 Args: map[string]string{
Colin Cross6d88dba2019-11-06 07:06:58 -0800599 "cFlags": moduleFlags,
Colin Cross28344522015-04-22 13:07:53 -0700600 "ccCmd": ccCmd,
Colin Cross3f40fa42015-01-30 17:27:36 -0800601 },
602 })
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700603
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500604 // Register post-process build statements (such as for tidy or kythe).
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800605 if emitXref {
606 kytheFile := android.ObjPathWithExt(ctx, subdir, srcFile, "kzip")
607 ctx.Build(pctx, android.BuildParams{
608 Rule: kytheExtract,
609 Description: "Xref C++ extractor " + srcFile.Rel(),
610 Output: kytheFile,
611 Input: srcFile,
612 Implicits: cFlagsDeps,
613 OrderOnly: pathDeps,
614 Args: map[string]string{
Colin Cross6d88dba2019-11-06 07:06:58 -0800615 "cFlags": moduleFlags,
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800616 },
617 })
618 kytheFiles = append(kytheFiles, kytheFile)
619 }
620
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700621 if tidy {
Dan Willemsen21ec4902016-11-02 20:43:13 -0700622 tidyFile := android.ObjPathWithExt(ctx, subdir, srcFile, "tidy")
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700623 tidyFiles = append(tidyFiles, tidyFile)
624
Kousik Kumar4e30bba2020-06-18 09:17:51 -0700625 rule := clangTidy
Ramy Medhat16f23a42020-09-03 01:29:49 -0400626 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_CLANG_TIDY") {
Kousik Kumar4e30bba2020-06-18 09:17:51 -0700627 rule = clangTidyRE
628 }
629
Colin Crossae887032017-10-23 17:16:14 -0700630 ctx.Build(pctx, android.BuildParams{
Kousik Kumar4e30bba2020-06-18 09:17:51 -0700631 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -0700632 Description: "clang-tidy " + srcFile.Rel(),
633 Output: tidyFile,
634 Input: srcFile,
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700635 // We must depend on objFile, since clang-tidy doesn't
636 // support exporting dependencies.
Dan Willemsen6b4419c2019-08-09 12:45:53 -0700637 Implicit: objFile,
638 Implicits: cFlagsDeps,
639 OrderOnly: pathDeps,
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700640 Args: map[string]string{
Colin Cross6d88dba2019-11-06 07:06:58 -0800641 "cFlags": moduleToolingFlags,
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700642 "tidyFlags": flags.tidyFlags,
643 },
644 })
645 }
646
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800647 if dump {
648 sAbiDumpFile := android.ObjPathWithExt(ctx, subdir, srcFile, "sdump")
649 sAbiDumpFiles = append(sAbiDumpFiles, sAbiDumpFile)
650
Ramy Medhat31ec9422020-04-17 15:03:58 -0400651 dumpRule := sAbiDump
Ramy Medhat16f23a42020-09-03 01:29:49 -0400652 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_ABI_DUMPER") {
Ramy Medhat31ec9422020-04-17 15:03:58 -0400653 dumpRule = sAbiDumpRE
654 }
Colin Crossae887032017-10-23 17:16:14 -0700655 ctx.Build(pctx, android.BuildParams{
Ramy Medhat31ec9422020-04-17 15:03:58 -0400656 Rule: dumpRule,
Colin Cross67a5c132017-05-09 13:45:28 -0700657 Description: "header-abi-dumper " + srcFile.Rel(),
658 Output: sAbiDumpFile,
659 Input: srcFile,
660 Implicit: objFile,
Dan Willemsen6b4419c2019-08-09 12:45:53 -0700661 Implicits: cFlagsDeps,
662 OrderOnly: pathDeps,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800663 Args: map[string]string{
Colin Cross6d88dba2019-11-06 07:06:58 -0800664 "cFlags": moduleToolingFlags,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800665 "exportDirs": flags.sAbiFlags,
666 },
667 })
668 }
669
Colin Cross3f40fa42015-01-30 17:27:36 -0800670 }
671
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700672 return Objects{
Dan Willemsen581341d2017-02-09 16:16:31 -0800673 objFiles: objFiles,
674 tidyFiles: tidyFiles,
675 coverageFiles: coverageFiles,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800676 sAbiDumpFiles: sAbiDumpFiles,
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800677 kytheFiles: kytheFiles,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700678 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800679}
680
681// Generate a rule for compiling multiple .o files to a static library (.a)
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500682func transformObjToStaticLib(ctx android.ModuleContext,
Martin Stjernholm391d94c2020-04-17 17:34:31 +0100683 objFiles android.Paths, wholeStaticLibs android.Paths,
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700684 flags builderFlags, outputFile android.ModuleOutPath, deps android.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800685
Stephen Hinesf1addeb2018-01-09 23:29:04 -0800686 arCmd := "${config.ClangBin}/llvm-ar"
Martin Stjernholm391d94c2020-04-17 17:34:31 +0100687 arFlags := ""
Stephen Hinesf1addeb2018-01-09 23:29:04 -0800688 if !ctx.Darwin() {
689 arFlags += " -format=gnu"
690 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800691
Martin Stjernholm391d94c2020-04-17 17:34:31 +0100692 if len(wholeStaticLibs) == 0 {
693 ctx.Build(pctx, android.BuildParams{
694 Rule: ar,
695 Description: "static link " + outputFile.Base(),
696 Output: outputFile,
697 Inputs: objFiles,
698 Implicits: deps,
699 Args: map[string]string{
700 "arFlags": "crsPD" + arFlags,
701 "arCmd": arCmd,
702 },
703 })
704
705 } else {
706 ctx.Build(pctx, android.BuildParams{
707 Rule: arWithLibs,
708 Description: "static link " + outputFile.Base(),
709 Output: outputFile,
710 Inputs: append(objFiles, wholeStaticLibs...),
711 Implicits: deps,
712 Args: map[string]string{
713 "arCmd": arCmd,
714 "arObjFlags": "crsPD" + arFlags,
715 "arObjs": strings.Join(objFiles.Strings(), " "),
716 "arLibFlags": "cqsL" + arFlags,
717 "arLibs": strings.Join(wholeStaticLibs.Strings(), " "),
718 },
719 })
720 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800721}
722
723// Generate a rule for compiling multiple .o files, plus static libraries, whole static libraries,
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700724// and shared libraries, to a shared library (.so) or dynamic executable
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500725func transformObjToDynamicBinary(ctx android.ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -0700726 objFiles, sharedLibs, staticLibs, lateStaticLibs, wholeStaticLibs, deps android.Paths,
Josh Gao75a50a22019-06-07 17:58:59 -0700727 crtBegin, crtEnd android.OptionalPath, groupLate bool, flags builderFlags, outputFile android.WritablePath, implicitOutputs android.WritablePaths) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800728
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700729 ldCmd := "${config.ClangBin}/clang++"
Colin Cross3f40fa42015-01-30 17:27:36 -0800730
Colin Cross3f40fa42015-01-30 17:27:36 -0800731 var libFlagsList []string
732
Colin Cross16b23492016-01-06 14:41:07 -0800733 if len(flags.libFlags) > 0 {
734 libFlagsList = append(libFlagsList, flags.libFlags)
735 }
736
Colin Cross3f40fa42015-01-30 17:27:36 -0800737 if len(wholeStaticLibs) > 0 {
Dan Willemsen490fd492015-11-24 17:53:15 -0800738 if ctx.Host() && ctx.Darwin() {
Colin Cross635c3b02016-05-18 15:37:25 -0700739 libFlagsList = append(libFlagsList, android.JoinWithPrefix(wholeStaticLibs.Strings(), "-force_load "))
Colin Cross0af4b842015-04-30 16:36:18 -0700740 } else {
741 libFlagsList = append(libFlagsList, "-Wl,--whole-archive ")
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700742 libFlagsList = append(libFlagsList, wholeStaticLibs.Strings()...)
Colin Cross0af4b842015-04-30 16:36:18 -0700743 libFlagsList = append(libFlagsList, "-Wl,--no-whole-archive ")
744 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800745 }
746
Colin Cross7a7cf972016-12-05 18:47:39 -0800747 if flags.groupStaticLibs && !ctx.Darwin() && len(staticLibs) > 0 {
Colin Cross18c0c5a2016-12-01 14:45:23 -0800748 libFlagsList = append(libFlagsList, "-Wl,--start-group")
749 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700750 libFlagsList = append(libFlagsList, staticLibs.Strings()...)
Colin Cross7a7cf972016-12-05 18:47:39 -0800751 if flags.groupStaticLibs && !ctx.Darwin() && len(staticLibs) > 0 {
Colin Cross18c0c5a2016-12-01 14:45:23 -0800752 libFlagsList = append(libFlagsList, "-Wl,--end-group")
753 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800754
Stephen Hines10347862016-07-18 15:54:54 -0700755 if groupLate && !ctx.Darwin() && len(lateStaticLibs) > 0 {
Dan Willemsenedc385f2015-07-08 13:02:23 -0700756 libFlagsList = append(libFlagsList, "-Wl,--start-group")
757 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700758 libFlagsList = append(libFlagsList, lateStaticLibs.Strings()...)
Stephen Hines10347862016-07-18 15:54:54 -0700759 if groupLate && !ctx.Darwin() && len(lateStaticLibs) > 0 {
Dan Willemsenedc385f2015-07-08 13:02:23 -0700760 libFlagsList = append(libFlagsList, "-Wl,--end-group")
761 }
762
Colin Cross3f40fa42015-01-30 17:27:36 -0800763 for _, lib := range sharedLibs {
Josh Gao75a50a22019-06-07 17:58:59 -0700764 libFile := lib.String()
765 if ctx.Windows() {
766 libFile = pathtools.ReplaceExtension(libFile, "lib")
767 }
768 libFlagsList = append(libFlagsList, libFile)
Colin Cross3f40fa42015-01-30 17:27:36 -0800769 }
770
Colin Cross3f40fa42015-01-30 17:27:36 -0800771 deps = append(deps, staticLibs...)
Colin Cross3075ad02015-03-17 10:47:08 -0700772 deps = append(deps, lateStaticLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800773 deps = append(deps, wholeStaticLibs...)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700774 if crtBegin.Valid() {
775 deps = append(deps, crtBegin.Path(), crtEnd.Path())
Colin Cross3f40fa42015-01-30 17:27:36 -0800776 }
777
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400778 rule := ld
Kousik Kumar3fb61262020-04-22 13:31:09 -0700779 args := map[string]string{
780 "ldCmd": ldCmd,
781 "crtBegin": crtBegin.String(),
782 "libFlags": strings.Join(libFlagsList, " "),
783 "extraLibFlags": flags.extraLibFlags,
784 "ldFlags": flags.globalLdFlags + " " + flags.localLdFlags,
785 "crtEnd": crtEnd.String(),
786 }
Ramy Medhat16f23a42020-09-03 01:29:49 -0400787 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_CXX_LINKS") {
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400788 rule = ldRE
Kousik Kumar3fb61262020-04-22 13:31:09 -0700789 args["implicitOutputs"] = strings.Join(implicitOutputs.Strings(), ",")
Ramy Medhat6797edc2020-08-28 14:21:55 -0400790 args["implicitInputs"] = strings.Join(deps.Strings(), ",")
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400791 }
792
Colin Crossae887032017-10-23 17:16:14 -0700793 ctx.Build(pctx, android.BuildParams{
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400794 Rule: rule,
Josh Gao75a50a22019-06-07 17:58:59 -0700795 Description: "link " + outputFile.Base(),
796 Output: outputFile,
797 ImplicitOutputs: implicitOutputs,
798 Inputs: objFiles,
799 Implicits: deps,
Kousik Kumar3fb61262020-04-22 13:31:09 -0700800 Args: args,
Colin Cross3f40fa42015-01-30 17:27:36 -0800801 })
802}
803
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800804// Generate a rule to combine .dump sAbi dump files from multiple source files
805// into a single .ldump sAbi dump file
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500806func transformDumpToLinkedDump(ctx android.ModuleContext, sAbiDumps android.Paths, soFile android.Path,
Logan Chiene3d7a0d2019-01-17 00:18:02 +0800807 baseName, exportedHeaderFlags string, symbolFile android.OptionalPath,
808 excludedSymbolVersions, excludedSymbolTags []string) android.OptionalPath {
809
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800810 outputFile := android.PathForModuleOut(ctx, baseName+".lsdump")
Logan Chiene3d7a0d2019-01-17 00:18:02 +0800811
812 implicits := android.Paths{soFile}
Jayant Chowdharydf344d52018-01-17 11:11:42 -0800813 symbolFilterStr := "-so " + soFile.String()
Logan Chiene3d7a0d2019-01-17 00:18:02 +0800814
815 if symbolFile.Valid() {
816 implicits = append(implicits, symbolFile.Path())
817 symbolFilterStr += " -v " + symbolFile.String()
818 }
819 for _, ver := range excludedSymbolVersions {
820 symbolFilterStr += " --exclude-symbol-version " + ver
821 }
822 for _, tag := range excludedSymbolTags {
823 symbolFilterStr += " --exclude-symbol-tag " + tag
824 }
Ramy Medhat808594c2020-05-07 06:56:47 -0400825 rule := sAbiLink
826 args := map[string]string{
827 "symbolFilter": symbolFilterStr,
828 "arch": ctx.Arch().ArchType.Name,
829 "exportedHeaderFlags": exportedHeaderFlags,
830 }
Ramy Medhat16f23a42020-09-03 01:29:49 -0400831 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_ABI_LINKER") {
Ramy Medhat808594c2020-05-07 06:56:47 -0400832 rule = sAbiLinkRE
833 rbeImplicits := implicits.Strings()
834 for _, p := range strings.Split(exportedHeaderFlags, " ") {
835 if len(p) > 2 {
836 // Exclude the -I prefix.
837 rbeImplicits = append(rbeImplicits, p[2:])
838 }
839 }
Ramy Medhat6797edc2020-08-28 14:21:55 -0400840 args["implicitInputs"] = strings.Join(rbeImplicits, ",")
Ramy Medhat808594c2020-05-07 06:56:47 -0400841 }
Colin Crossae887032017-10-23 17:16:14 -0700842 ctx.Build(pctx, android.BuildParams{
Ramy Medhat808594c2020-05-07 06:56:47 -0400843 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -0700844 Description: "header-abi-linker " + outputFile.Base(),
845 Output: outputFile,
846 Inputs: sAbiDumps,
Logan Chiene3d7a0d2019-01-17 00:18:02 +0800847 Implicits: implicits,
Ramy Medhat808594c2020-05-07 06:56:47 -0400848 Args: args,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800849 })
850 return android.OptionalPathForPath(outputFile)
851}
852
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500853// unzipRefDump registers a build statement to unzip a reference abi dump.
854func unzipRefDump(ctx android.ModuleContext, zippedRefDump android.Path, baseName string) android.Path {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700855 outputFile := android.PathForModuleOut(ctx, baseName+"_ref.lsdump")
Colin Crossae887032017-10-23 17:16:14 -0700856 ctx.Build(pctx, android.BuildParams{
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700857 Rule: unzipRefSAbiDump,
858 Description: "gunzip" + outputFile.Base(),
859 Output: outputFile,
860 Input: zippedRefDump,
861 })
862 return outputFile
863}
864
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500865// sourceAbiDiff registers a build statement to compare linked sAbi dump files (.ldump).
866func sourceAbiDiff(ctx android.ModuleContext, inputDump android.Path, referenceDump android.Path,
Logan Chien2a65dda2019-10-01 15:58:07 -0700867 baseName, exportedHeaderFlags string, checkAllApis, isLlndk, isNdk, isVndkExt bool) android.OptionalPath {
Logan Chienf3511742017-10-31 18:04:35 +0800868
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800869 outputFile := android.PathForModuleOut(ctx, baseName+".abidiff")
Jayant Chowdharyc7434e22018-05-31 15:42:26 -0700870 libName := strings.TrimSuffix(baseName, filepath.Ext(baseName))
Logan Chien6227fed2019-02-18 13:12:21 +0800871 createReferenceDumpFlags := ""
872
Logan Chien2a65dda2019-10-01 15:58:07 -0700873 var extraFlags []string
874 if checkAllApis {
875 extraFlags = append(extraFlags, "-check-all-apis")
876 } else {
877 extraFlags = append(extraFlags,
878 "-allow-unreferenced-changes",
879 "-allow-unreferenced-elf-symbol-changes")
Jayant Chowdharye4499502018-01-17 13:13:33 -0800880 }
Logan Chien2a65dda2019-10-01 15:58:07 -0700881
882 if exportedHeaderFlags == "" {
883 extraFlags = append(extraFlags, "-advice-only")
884 }
885
Logan Chien62f1f942019-02-18 15:40:42 +0800886 if isLlndk || isNdk {
Logan Chien6227fed2019-02-18 13:12:21 +0800887 createReferenceDumpFlags = "--llndk"
Logan Chien62f1f942019-02-18 15:40:42 +0800888 if isLlndk {
889 // TODO(b/130324828): "-consider-opaque-types-different" should apply to
890 // both LLNDK and NDK shared libs. However, a known issue in header-abi-diff
891 // breaks libaaudio. Remove the if-guard after the issue is fixed.
Logan Chien2a65dda2019-10-01 15:58:07 -0700892 extraFlags = append(extraFlags, "-consider-opaque-types-different")
Logan Chien62f1f942019-02-18 15:40:42 +0800893 }
Jayant Chowdharyc7434e22018-05-31 15:42:26 -0700894 }
Logan Chienf3511742017-10-31 18:04:35 +0800895 if isVndkExt {
Logan Chien2a65dda2019-10-01 15:58:07 -0700896 extraFlags = append(extraFlags, "-allow-extensions")
Logan Chienf3511742017-10-31 18:04:35 +0800897 }
898
Colin Crossae887032017-10-23 17:16:14 -0700899 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700900 Rule: sAbiDiff,
901 Description: "header-abi-diff " + outputFile.Base(),
902 Output: outputFile,
903 Input: inputDump,
904 Implicit: referenceDump,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800905 Args: map[string]string{
Logan Chien6227fed2019-02-18 13:12:21 +0800906 "referenceDump": referenceDump.String(),
907 "libName": libName,
908 "arch": ctx.Arch().ArchType.Name,
Logan Chien2a65dda2019-10-01 15:58:07 -0700909 "extraFlags": strings.Join(extraFlags, " "),
Logan Chien6227fed2019-02-18 13:12:21 +0800910 "createReferenceDumpFlags": createReferenceDumpFlags,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800911 },
912 })
913 return android.OptionalPathForPath(outputFile)
914}
915
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700916// Generate a rule for extracting a table of contents from a shared library (.so)
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500917func transformSharedObjectToToc(ctx android.ModuleContext, inputFile android.Path,
Colin Cross26c34ed2016-09-30 17:10:16 -0700918 outputFile android.WritablePath, flags builderFlags) {
919
Colin Crossb496cfd2018-09-10 16:50:05 -0700920 var format string
921 var crossCompile string
922 if ctx.Darwin() {
923 format = "--macho"
924 crossCompile = "${config.MacToolPath}"
925 } else if ctx.Windows() {
926 format = "--pe"
927 crossCompile = gccCmd(flags.toolchain, "")
928 } else {
929 format = "--elf"
930 crossCompile = gccCmd(flags.toolchain, "")
931 }
Colin Cross26c34ed2016-09-30 17:10:16 -0700932
Colin Crossae887032017-10-23 17:16:14 -0700933 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700934 Rule: toc,
935 Description: "generate toc " + inputFile.Base(),
936 Output: outputFile,
937 Input: inputFile,
Colin Cross26c34ed2016-09-30 17:10:16 -0700938 Args: map[string]string{
939 "crossCompile": crossCompile,
Colin Crossb496cfd2018-09-10 16:50:05 -0700940 "format": format,
Colin Cross26c34ed2016-09-30 17:10:16 -0700941 },
942 })
943}
944
Colin Cross3f40fa42015-01-30 17:27:36 -0800945// Generate a rule for compiling multiple .o files to a .o using ld partial linking
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500946func transformObjsToObj(ctx android.ModuleContext, objFiles android.Paths,
Dan Willemsen724ab5d2019-09-19 10:50:18 -0700947 flags builderFlags, outputFile android.WritablePath, deps android.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800948
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700949 ldCmd := "${config.ClangBin}/clang++"
Colin Cross3f40fa42015-01-30 17:27:36 -0800950
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400951 rule := partialLd
952 args := map[string]string{
953 "ldCmd": ldCmd,
954 "ldFlags": flags.globalLdFlags + " " + flags.localLdFlags,
955 }
Ramy Medhat16f23a42020-09-03 01:29:49 -0400956 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_CXX_LINKS") {
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400957 rule = partialLdRE
958 args["inCommaList"] = strings.Join(objFiles.Strings(), ",")
Ramy Medhat6797edc2020-08-28 14:21:55 -0400959 args["implicitInputs"] = strings.Join(deps.Strings(), ",")
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400960 }
Colin Crossae887032017-10-23 17:16:14 -0700961 ctx.Build(pctx, android.BuildParams{
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400962 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -0700963 Description: "link " + outputFile.Base(),
964 Output: outputFile,
965 Inputs: objFiles,
Dan Willemsen724ab5d2019-09-19 10:50:18 -0700966 Implicits: deps,
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400967 Args: args,
Colin Cross3f40fa42015-01-30 17:27:36 -0800968 })
969}
970
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500971// Generate a rule for running objcopy --prefix-symbols on a binary
972func transformBinaryPrefixSymbols(ctx android.ModuleContext, prefix string, inputFile android.Path,
Colin Cross635c3b02016-05-18 15:37:25 -0700973 flags builderFlags, outputFile android.WritablePath) {
Colin Crossbfae8852015-03-26 14:44:11 -0700974
975 objcopyCmd := gccCmd(flags.toolchain, "objcopy")
976
Colin Crossae887032017-10-23 17:16:14 -0700977 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700978 Rule: prefixSymbols,
979 Description: "prefix symbols " + outputFile.Base(),
980 Output: outputFile,
981 Input: inputFile,
Colin Crossbfae8852015-03-26 14:44:11 -0700982 Args: map[string]string{
983 "objcopyCmd": objcopyCmd,
984 "prefix": prefix,
985 },
986 })
987}
988
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500989// Registers a build statement to invoke `strip` (to discard symbols and data from object files).
990func transformStrip(ctx android.ModuleContext, inputFile android.Path,
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200991 outputFile android.WritablePath, flags StripFlags) {
Colin Cross665dce92016-04-28 14:50:03 -0700992
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200993 crossCompile := gccCmd(flags.Toolchain, "")
Colin Cross665dce92016-04-28 14:50:03 -0700994 args := ""
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200995 if flags.StripAddGnuDebuglink {
Colin Cross665dce92016-04-28 14:50:03 -0700996 args += " --add-gnu-debuglink"
997 }
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200998 if flags.StripKeepMiniDebugInfo {
Colin Cross665dce92016-04-28 14:50:03 -0700999 args += " --keep-mini-debug-info"
1000 }
Thiébaud Weksteend4587452020-08-19 14:53:01 +02001001 if flags.StripKeepSymbols {
Colin Cross665dce92016-04-28 14:50:03 -07001002 args += " --keep-symbols"
1003 }
Thiébaud Weksteend4587452020-08-19 14:53:01 +02001004 if flags.StripKeepSymbolsList != "" {
1005 args += " -k" + flags.StripKeepSymbolsList
Yi Kongacee27c2019-03-29 20:05:14 -07001006 }
Thiébaud Weksteend4587452020-08-19 14:53:01 +02001007 if flags.StripKeepSymbolsAndDebugFrame {
Christopher Ferrisb43fe7a2019-05-17 16:39:54 -07001008 args += " --keep-symbols-and-debug-frame"
1009 }
Thiébaud Weksteend4587452020-08-19 14:53:01 +02001010 if flags.StripUseGnuStrip {
Yi Kongb5c34d72018-11-07 16:28:49 -08001011 args += " --use-gnu-strip"
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -07001012 }
Colin Cross665dce92016-04-28 14:50:03 -07001013
Colin Crossae887032017-10-23 17:16:14 -07001014 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -07001015 Rule: strip,
1016 Description: "strip " + outputFile.Base(),
1017 Output: outputFile,
1018 Input: inputFile,
Colin Cross665dce92016-04-28 14:50:03 -07001019 Args: map[string]string{
1020 "crossCompile": crossCompile,
1021 "args": args,
1022 },
1023 })
1024}
1025
Chris Parsonsbf4f55f2020-11-23 17:02:44 -05001026// Registers build statement to invoke `strip` on darwin architecture.
1027func transformDarwinStrip(ctx android.ModuleContext, inputFile android.Path,
Colin Cross635c3b02016-05-18 15:37:25 -07001028 outputFile android.WritablePath) {
Colin Crossb8ecdfe2016-05-03 15:10:29 -07001029
Colin Crossae887032017-10-23 17:16:14 -07001030 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -07001031 Rule: darwinStrip,
1032 Description: "strip " + outputFile.Base(),
1033 Output: outputFile,
1034 Input: inputFile,
Colin Crossb8ecdfe2016-05-03 15:10:29 -07001035 })
1036}
1037
Chris Parsonsbf4f55f2020-11-23 17:02:44 -05001038// Registers build statement to zip one or more coverage files.
1039func transformCoverageFilesToZip(ctx android.ModuleContext,
Oliver Nguyenc7434142019-04-24 14:22:25 -07001040 inputs Objects, baseName string) android.OptionalPath {
Dan Willemsen581341d2017-02-09 16:16:31 -08001041
1042 if len(inputs.coverageFiles) > 0 {
Oliver Nguyenc7434142019-04-24 14:22:25 -07001043 outputFile := android.PathForModuleOut(ctx, baseName+".zip")
Dan Willemsen581341d2017-02-09 16:16:31 -08001044
Oliver Nguyenc7434142019-04-24 14:22:25 -07001045 ctx.Build(pctx, android.BuildParams{
1046 Rule: zip,
1047 Description: "zip " + outputFile.Base(),
1048 Inputs: inputs.coverageFiles,
1049 Output: outputFile,
1050 })
Dan Willemsen581341d2017-02-09 16:16:31 -08001051
1052 return android.OptionalPathForPath(outputFile)
1053 }
1054
1055 return android.OptionalPath{}
1056}
1057
Chris Parsonsbf4f55f2020-11-23 17:02:44 -05001058// Rule to repack an archive (.a) file with a subset of object files.
1059func transformArchiveRepack(ctx android.ModuleContext, inputFile android.Path,
Yi Kongc49c3932019-10-15 02:01:19 -07001060 outputFile android.WritablePath, objects []string) {
1061
1062 ctx.Build(pctx, android.BuildParams{
1063 Rule: archiveRepack,
1064 Description: "Repack archive " + outputFile.Base(),
1065 Output: outputFile,
1066 Input: inputFile,
1067 Args: map[string]string{
1068 "objects": strings.Join(objects, " "),
1069 },
1070 })
1071}
1072
Colin Crossb98c8b02016-07-29 13:44:28 -07001073func gccCmd(toolchain config.Toolchain, cmd string) string {
Colin Cross3f40fa42015-01-30 17:27:36 -08001074 return filepath.Join(toolchain.GccRoot(), "bin", toolchain.GccTriple()+"-"+cmd)
1075}