blob: 9248f9799697bb49fa903b1717ac5d6d69ea3aa2 [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"
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -070024 "strconv"
Colin Cross3f40fa42015-01-30 17:27:36 -080025 "strings"
Colin Crossed4cf0b2015-03-26 14:43:45 -070026
27 "github.com/google/blueprint"
Josh Gao75a50a22019-06-07 17:58:59 -070028 "github.com/google/blueprint/pathtools"
Colin Crossb98c8b02016-07-29 13:44:28 -070029
30 "android/soong/android"
31 "android/soong/cc/config"
Ramy Medhat9a90fe52020-04-13 13:21:23 -040032 "android/soong/remoteexec"
Colin Cross3f40fa42015-01-30 17:27:36 -080033)
34
35const (
Dan Albertc3144b12015-04-28 18:17:56 -070036 objectExtension = ".o"
Colin Cross3f40fa42015-01-30 17:27:36 -080037 staticLibraryExtension = ".a"
38)
39
40var (
Colin Cross635c3b02016-05-18 15:37:25 -070041 pctx = android.NewPackageContext("android/soong/cc")
Colin Cross3f40fa42015-01-30 17:27:36 -080042
Chris Parsonsbf4f55f2020-11-23 17:02:44 -050043 // Rule to invoke gcc with given command, flags, and dependencies. Outputs a .d depfile.
Ramy Medhat8ea054a2020-01-27 14:19:44 -050044 cc = pctx.AndroidRemoteStaticRule("cc", android.RemoteRuleSupports{Goma: true, RBE: true},
Colin Cross3f40fa42015-01-30 17:27:36 -080045 blueprint.RuleParams{
46 Depfile: "${out}.d",
47 Deps: blueprint.DepsGCC,
Kousik Kumard207cbe2020-10-20 05:52:49 +000048 Command: "$relPwd ${config.CcWrapper}$ccCmd -c $cFlags -MD -MF ${out}.d -o $out $in",
Dan Willemsenc94a7682015-11-17 15:27:28 -080049 CommandDeps: []string{"$ccCmd"},
Colin Cross3f40fa42015-01-30 17:27:36 -080050 },
Dan Willemsen322a0a62015-11-17 15:19:46 -080051 "ccCmd", "cFlags")
Colin Cross3f40fa42015-01-30 17:27:36 -080052
Chris Parsonsbf4f55f2020-11-23 17:02:44 -050053 // Rule to invoke gcc with given command and flags, but no dependencies.
Kousik Kumar2976bfd2020-02-17 00:26:55 -080054 ccNoDeps = pctx.AndroidStaticRule("ccNoDeps",
Dan Willemsenfcabb1c2019-01-03 23:25:11 -080055 blueprint.RuleParams{
Kousik Kumard207cbe2020-10-20 05:52:49 +000056 Command: "$relPwd $ccCmd -c $cFlags -o $out $in",
Dan Willemsenfcabb1c2019-01-03 23:25:11 -080057 CommandDeps: []string{"$ccCmd"},
58 },
59 "ccCmd", "cFlags")
60
Chris Parsonsbf4f55f2020-11-23 17:02:44 -050061 // Rules to invoke ld to link binaries. Uses a .rsp file to list dependencies, as there may
62 // be many.
Colin Cross77cdcfd2021-03-12 11:28:25 -080063 ld, ldRE = pctx.RemoteStaticRules("ld",
Colin Cross3f40fa42015-01-30 17:27:36 -080064 blueprint.RuleParams{
Ramy Medhat31ec9422020-04-17 15:03:58 -040065 Command: "$reTemplate$ldCmd ${crtBegin} @${out}.rsp " +
Pete Bentley99f2fc22019-08-02 14:02:20 +010066 "${libFlags} ${crtEnd} -o ${out} ${ldFlags} ${extraLibFlags}",
Dan Willemsenc94a7682015-11-17 15:27:28 -080067 CommandDeps: []string{"$ldCmd"},
Colin Cross7d21c442015-03-30 17:47:53 -070068 Rspfile: "${out}.rsp",
69 RspfileContent: "${in}",
Colin Cross36ae1352019-03-29 15:55:30 -070070 // clang -Wl,--out-implib doesn't update its output file if it hasn't changed.
71 Restat: true,
Colin Cross3f40fa42015-01-30 17:27:36 -080072 },
Ramy Medhat31ec9422020-04-17 15:03:58 -040073 &remoteexec.REParams{
74 Labels: map[string]string{"type": "link", "tool": "clang"},
Ramy Medhat9a90fe52020-04-13 13:21:23 -040075 ExecStrategy: "${config.RECXXLinksExecStrategy}",
Ramy Medhat6797edc2020-08-28 14:21:55 -040076 Inputs: []string{"${out}.rsp", "$implicitInputs"},
Colin Crossa4eafdd2021-03-24 14:09:28 -070077 RSPFiles: []string{"${out}.rsp"},
Kousik Kumar3fb61262020-04-22 13:31:09 -070078 OutputFiles: []string{"${out}", "$implicitOutputs"},
Ramy Medhat9a90fe52020-04-13 13:21:23 -040079 ToolchainInputs: []string{"$ldCmd"},
80 Platform: map[string]string{remoteexec.PoolKey: "${config.RECXXLinksPool}"},
Ramy Medhat6797edc2020-08-28 14:21:55 -040081 }, []string{"ldCmd", "crtBegin", "libFlags", "crtEnd", "ldFlags", "extraLibFlags"}, []string{"implicitInputs", "implicitOutputs"})
Colin Cross3f40fa42015-01-30 17:27:36 -080082
Chris Parsonsbf4f55f2020-11-23 17:02:44 -050083 // Rules for .o files to combine to other .o files, using ld partial linking.
Colin Cross77cdcfd2021-03-12 11:28:25 -080084 partialLd, partialLdRE = pctx.RemoteStaticRules("partialLd",
Colin Cross3f40fa42015-01-30 17:27:36 -080085 blueprint.RuleParams{
Chih-Hung Hsieh3ede2942018-01-10 14:30:44 -080086 // Without -no-pie, clang 7.0 adds -pie to link Android files,
87 // but -r and -pie cannot be used together.
Ramy Medhat31ec9422020-04-17 15:03:58 -040088 Command: "$reTemplate$ldCmd -fuse-ld=lld -nostdlib -no-pie -Wl,-r ${in} -o ${out} ${ldFlags}",
Dan Willemsenc94a7682015-11-17 15:27:28 -080089 CommandDeps: []string{"$ldCmd"},
Ramy Medhat9a90fe52020-04-13 13:21:23 -040090 }, &remoteexec.REParams{
Ramy Medhat6797edc2020-08-28 14:21:55 -040091 Labels: map[string]string{"type": "link", "tool": "clang"},
92 ExecStrategy: "${config.RECXXLinksExecStrategy}",
93 Inputs: []string{"$inCommaList", "$implicitInputs"},
Kousik Kumar3fb61262020-04-22 13:31:09 -070094 OutputFiles: []string{"${out}", "$implicitOutputs"},
Ramy Medhat9a90fe52020-04-13 13:21:23 -040095 ToolchainInputs: []string{"$ldCmd"},
96 Platform: map[string]string{remoteexec.PoolKey: "${config.RECXXLinksPool}"},
Ramy Medhat6797edc2020-08-28 14:21:55 -040097 }, []string{"ldCmd", "ldFlags"}, []string{"implicitInputs", "inCommaList", "implicitOutputs"})
Colin Cross3f40fa42015-01-30 17:27:36 -080098
Chris Parsonsbf4f55f2020-11-23 17:02:44 -050099 // Rule to invoke `ar` with given cmd and flags, but no static library depenencies.
Colin Cross9d45bb72016-08-29 16:14:13 -0700100 ar = pctx.AndroidStaticRule("ar",
Colin Cross3f40fa42015-01-30 17:27:36 -0800101 blueprint.RuleParams{
Colin Cross7d21c442015-03-30 17:47:53 -0700102 Command: "rm -f ${out} && $arCmd $arFlags $out @${out}.rsp",
Dan Willemsenc94a7682015-11-17 15:27:28 -0800103 CommandDeps: []string{"$arCmd"},
Colin Cross7d21c442015-03-30 17:47:53 -0700104 Rspfile: "${out}.rsp",
105 RspfileContent: "${in}",
Colin Cross3f40fa42015-01-30 17:27:36 -0800106 },
107 "arCmd", "arFlags")
108
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500109 // Rule to invoke `ar` with given cmd, flags, and library dependencies. Generates a .a
110 // (archive) file from .o files.
Martin Stjernholm391d94c2020-04-17 17:34:31 +0100111 arWithLibs = pctx.AndroidStaticRule("arWithLibs",
112 blueprint.RuleParams{
113 Command: "rm -f ${out} && $arCmd $arObjFlags $out @${out}.rsp && $arCmd $arLibFlags $out $arLibs",
114 CommandDeps: []string{"$arCmd"},
115 Rspfile: "${out}.rsp",
116 RspfileContent: "${arObjs}",
117 },
118 "arCmd", "arObjFlags", "arObjs", "arLibFlags", "arLibs")
119
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500120 // Rule to run objcopy --prefix-symbols (to prefix all symbols in a file with a given string).
Colin Cross9d45bb72016-08-29 16:14:13 -0700121 prefixSymbols = pctx.AndroidStaticRule("prefixSymbols",
Colin Crossbfae8852015-03-26 14:44:11 -0700122 blueprint.RuleParams{
123 Command: "$objcopyCmd --prefix-symbols=${prefix} ${in} ${out}",
Dan Willemsenc94a7682015-11-17 15:27:28 -0800124 CommandDeps: []string{"$objcopyCmd"},
Colin Crossbfae8852015-03-26 14:44:11 -0700125 },
126 "objcopyCmd", "prefix")
127
Nan Zhang43a485c2017-03-27 14:27:58 -0700128 _ = pctx.SourcePathVariable("stripPath", "build/soong/scripts/strip.sh")
Dan Willemsen8fec83a2018-03-09 10:47:52 -0800129 _ = pctx.SourcePathVariable("xzCmd", "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/xz")
David Srbecky69315e42021-04-29 21:06:47 +0100130 _ = pctx.SourcePathVariable("createMiniDebugInfo", "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/create_minidebuginfo")
Colin Cross665dce92016-04-28 14:50:03 -0700131
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500132 // Rule to invoke `strip` (to discard symbols and data from object files).
133 strip = pctx.AndroidStaticRule("strip",
134 blueprint.RuleParams{
David Srbecky69315e42021-04-29 21:06:47 +0100135 Depfile: "${out}.d",
136 Deps: blueprint.DepsGCC,
137 Command: "XZ=$xzCmd CREATE_MINIDEBUGINFO=$createMiniDebugInfo CLANG_BIN=${config.ClangBin} $stripPath ${args} -i ${in} -o ${out} -d ${out}.d",
138 CommandDeps: func() []string {
139 if runtime.GOOS != "darwin" {
140 return []string{"$stripPath", "$xzCmd", "$createMiniDebugInfo"}
141 } else {
142 return []string{"$stripPath", "$xzCmd"}
143 }
144 }(),
145 Pool: darwinStripPool,
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500146 },
Elliott Hughesafcd3392021-05-05 13:54:11 -0700147 "args")
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500148
149 // Rule to invoke `strip` (to discard symbols and data from object files) on darwin architecture.
150 darwinStrip = pctx.AndroidStaticRule("darwinStrip",
151 blueprint.RuleParams{
152 Command: "${config.MacStripPath} -u -r -o $out $in",
153 CommandDeps: []string{"${config.MacStripPath}"},
154 })
155
Colin Crossee3ea312019-05-17 15:36:46 -0700156 // 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 -0700157 // file descriptors on darwin. Limit concurrent calls to 5 on darwin.
Colin Crossee3ea312019-05-17 15:36:46 -0700158 darwinStripPool = func() blueprint.Pool {
159 if runtime.GOOS == "darwin" {
160 return pctx.StaticPool("darwinStripPool", blueprint.PoolParams{
Colin Crossbadf8d62019-05-22 13:25:50 -0700161 Depth: 5,
Colin Crossee3ea312019-05-17 15:36:46 -0700162 })
163 } else {
164 return nil
165 }
166 }()
167
Dan Willemsen47450072021-10-19 20:24:49 -0700168 darwinLipo = pctx.AndroidStaticRule("darwinLipo",
169 blueprint.RuleParams{
170 Command: "${config.MacLipoPath} -create -output $out $in",
171 CommandDeps: []string{"${config.MacLipoPath}"},
172 })
173
Yi Kongc49c3932019-10-15 02:01:19 -0700174 _ = pctx.SourcePathVariable("archiveRepackPath", "build/soong/scripts/archive_repack.sh")
175
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500176 // Rule to repack an archive (.a) file with a subset of object files.
Yi Kongc49c3932019-10-15 02:01:19 -0700177 archiveRepack = pctx.AndroidStaticRule("archiveRepack",
178 blueprint.RuleParams{
179 Depfile: "${out}.d",
180 Deps: blueprint.DepsGCC,
181 Command: "CLANG_BIN=${config.ClangBin} $archiveRepackPath -i ${in} -o ${out} -d ${out}.d ${objects}",
182 CommandDeps: []string{"$archiveRepackPath"},
183 },
184 "objects")
185
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500186 // Rule to create an empty file at a given path.
Colin Cross9d45bb72016-08-29 16:14:13 -0700187 emptyFile = pctx.AndroidStaticRule("emptyFile",
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700188 blueprint.RuleParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700189 Command: "rm -f $out && touch $out",
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700190 })
191
Nan Zhang43a485c2017-03-27 14:27:58 -0700192 _ = pctx.SourcePathVariable("tocPath", "build/soong/scripts/toc.sh")
Colin Cross26c34ed2016-09-30 17:10:16 -0700193
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500194 // A rule for extracting a table of contents from a shared library (.so).
Colin Cross26c34ed2016-09-30 17:10:16 -0700195 toc = pctx.AndroidStaticRule("toc",
196 blueprint.RuleParams{
197 Depfile: "${out}.d",
198 Deps: blueprint.DepsGCC,
Yi Kong4ad44e72021-04-01 17:45:42 +0800199 Command: "CLANG_BIN=$clangBin $tocPath $format -i ${in} -o ${out} -d ${out}.d",
Colin Cross26c34ed2016-09-30 17:10:16 -0700200 CommandDeps: []string{"$tocPath"},
201 Restat: true,
202 },
Yi Kong4ad44e72021-04-01 17:45:42 +0800203 "clangBin", "format")
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700204
Chih-Hung Hsieh30ab2f02022-05-28 18:52:12 -0700205 // Rules for invoking clang-tidy (a clang-based linter).
Colin Cross77cdcfd2021-03-12 11:28:25 -0800206 clangTidy, clangTidyRE = pctx.RemoteStaticRules("clangTidy",
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700207 blueprint.RuleParams{
Chih-Hung Hsieh8e09f262021-09-10 17:25:28 -0700208 Depfile: "${out}.d",
209 Deps: blueprint.DepsGCC,
Chih-Hung Hsieh30ab2f02022-05-28 18:52:12 -0700210 Command: "CLANG_CMD=$clangCmd TIDY_FILE=$out " +
211 "$tidyVars$reTemplate${config.ClangBin}/clang-tidy.sh $in $tidyFlags -- $cFlags",
212 CommandDeps: []string{"${config.ClangBin}/clang-tidy.sh", "$ccCmd", "$tidyCmd"},
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700213 },
Kousik Kumar4e30bba2020-06-18 09:17:51 -0700214 &remoteexec.REParams{
Chih-Hung Hsieh95dfbce2021-09-09 19:12:05 -0700215 Labels: map[string]string{"type": "lint", "tool": "clang-tidy", "lang": "cpp"},
216 ExecStrategy: "${config.REClangTidyExecStrategy}",
Chih-Hung Hsieh30ab2f02022-05-28 18:52:12 -0700217 Inputs: []string{"$in"},
218 OutputFiles: []string{"${out}", "${out}.d"},
219 ToolchainInputs: []string{"$ccCmd", "$tidyCmd"},
220 EnvironmentVariables: []string{"CLANG_CMD", "TIDY_FILE", "TIDY_TIMEOUT"},
Chih-Hung Hsiehfde8e6d2021-08-30 12:49:39 -0700221 // Although clang-tidy has an option to "fix" source files, that feature is hardly useable
222 // under parallel compilation and RBE. So we assume no OutputFiles here.
223 // The clang-tidy fix option is best run locally in single thread.
224 // Copying source file back to local caused two problems:
225 // (1) New timestamps trigger clang and clang-tidy compilations again.
226 // (2) Changing source files caused concurrent clang or clang-tidy jobs to crash.
227 Platform: map[string]string{remoteexec.PoolKey: "${config.REClangTidyPool}"},
Chih-Hung Hsieh30ab2f02022-05-28 18:52:12 -0700228 }, []string{"cFlags", "ccCmd", "clangCmd", "tidyCmd", "tidyFlags", "tidyVars"}, []string{})
Colin Cross91e90042016-12-02 17:13:24 -0800229
Nan Zhang43a485c2017-03-27 14:27:58 -0700230 _ = pctx.SourcePathVariable("yasmCmd", "prebuilts/misc/${config.HostPrebuiltTag}/yasm/yasm")
Colin Cross91e90042016-12-02 17:13:24 -0800231
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500232 // Rule for invoking yasm to compile .asm assembly files.
Colin Cross91e90042016-12-02 17:13:24 -0800233 yasm = pctx.AndroidStaticRule("yasm",
234 blueprint.RuleParams{
Dan Willemsen1d3e5452017-08-22 20:53:45 -0700235 Command: "$yasmCmd $asFlags -o $out $in && $yasmCmd $asFlags -M $in >$out.d",
Colin Cross91e90042016-12-02 17:13:24 -0800236 CommandDeps: []string{"$yasmCmd"},
Dan Willemsen1d3e5452017-08-22 20:53:45 -0700237 Depfile: "$out.d",
238 Deps: blueprint.DepsGCC,
Colin Cross91e90042016-12-02 17:13:24 -0800239 },
240 "asFlags")
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800241
Jayant Chowdharya4c6df52018-02-20 12:36:51 -0800242 _ = pctx.SourcePathVariable("sAbiDumper", "prebuilts/clang-tools/${config.HostPrebuiltTag}/bin/header-abi-dumper")
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800243
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700244 // -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 -0800245 sAbiDump, sAbiDumpRE = pctx.RemoteStaticRules("sAbiDump",
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800246 blueprint.RuleParams{
Hsin-Yi Chen79781a92021-07-09 15:59:03 +0800247 Command: "rm -f $out && $reTemplate$sAbiDumper --root-dir . --root-dir $$OUT_DIR:out -o ${out} $in $exportDirs -- $cFlags -w -isystem prebuilts/clang-tools/${config.HostPrebuiltTag}/clang-headers",
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800248 CommandDeps: []string{"$sAbiDumper"},
Ramy Medhat31ec9422020-04-17 15:03:58 -0400249 }, &remoteexec.REParams{
250 Labels: map[string]string{"type": "abi-dump", "tool": "header-abi-dumper"},
251 ExecStrategy: "${config.REAbiDumperExecStrategy}",
Ulf Adams18931762020-11-25 22:54:35 +0100252 Inputs: []string{"$sAbiLinkerLibs"},
Ramy Medhat31ec9422020-04-17 15:03:58 -0400253 Platform: map[string]string{
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500254 remoteexec.PoolKey: "${config.RECXXPool}",
Ramy Medhat31ec9422020-04-17 15:03:58 -0400255 },
256 }, []string{"cFlags", "exportDirs"}, nil)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800257
Jayant Chowdharya4c6df52018-02-20 12:36:51 -0800258 _ = pctx.SourcePathVariable("sAbiLinker", "prebuilts/clang-tools/${config.HostPrebuiltTag}/bin/header-abi-linker")
Ramy Medhat808594c2020-05-07 06:56:47 -0400259 _ = pctx.SourcePathVariable("sAbiLinkerLibs", "prebuilts/clang-tools/${config.HostPrebuiltTag}/lib64")
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800260
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500261 // Rule to combine .dump sAbi dump files from multiple source files into a single .ldump
262 // sAbi dump file.
Colin Cross77cdcfd2021-03-12 11:28:25 -0800263 sAbiLink, sAbiLinkRE = pctx.RemoteStaticRules("sAbiLink",
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800264 blueprint.RuleParams{
Hsin-Yi Chen79781a92021-07-09 15:59:03 +0800265 Command: "$reTemplate$sAbiLinker --root-dir . --root-dir $$OUT_DIR:out -o ${out} $symbolFilter -arch $arch $exportedHeaderFlags @${out}.rsp",
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800266 CommandDeps: []string{"$sAbiLinker"},
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800267 Rspfile: "${out}.rsp",
268 RspfileContent: "${in}",
Ramy Medhat808594c2020-05-07 06:56:47 -0400269 }, &remoteexec.REParams{
270 Labels: map[string]string{"type": "tool", "name": "abi-linker"},
271 ExecStrategy: "${config.REAbiLinkerExecStrategy}",
Ramy Medhat6797edc2020-08-28 14:21:55 -0400272 Inputs: []string{"$sAbiLinkerLibs", "${out}.rsp", "$implicitInputs"},
Colin Crossa4eafdd2021-03-24 14:09:28 -0700273 RSPFiles: []string{"${out}.rsp"},
Ramy Medhat808594c2020-05-07 06:56:47 -0400274 OutputFiles: []string{"$out"},
275 ToolchainInputs: []string{"$sAbiLinker"},
276 Platform: map[string]string{remoteexec.PoolKey: "${config.RECXXPool}"},
Ramy Medhat6797edc2020-08-28 14:21:55 -0400277 }, []string{"symbolFilter", "arch", "exportedHeaderFlags"}, []string{"implicitInputs"})
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800278
Jayant Chowdharya4c6df52018-02-20 12:36:51 -0800279 _ = pctx.SourcePathVariable("sAbiDiffer", "prebuilts/clang-tools/${config.HostPrebuiltTag}/bin/header-abi-diff")
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700280
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500281 // Rule to compare linked sAbi dump files (.ldump).
Colin Cross2e2dbc22019-09-25 13:31:46 -0700282 sAbiDiff = pctx.RuleFunc("sAbiDiff",
Dan Willemsen54daaf02018-03-12 13:24:09 -0700283 func(ctx android.PackageRuleContext) blueprint.RuleParams {
Logan Chien2a65dda2019-10-01 15:58:07 -0700284 commandStr := "($sAbiDiffer ${extraFlags} -lib ${libName} -arch ${arch} -o ${out} -new ${in} -old ${referenceDump})"
Logan Chien6227fed2019-02-18 13:12:21 +0800285 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 +0800286 commandStr += " && (mkdir -p $$DIST_DIR/abidiffs && cp ${out} $$DIST_DIR/abidiffs/)"
Jayant Chowdharyd8b70a32018-02-01 17:23:09 -0800287 commandStr += " && exit 1)"
Jayant Chowdhary219139d2017-11-27 14:52:21 -0800288 return blueprint.RuleParams{
289 Command: commandStr,
290 CommandDeps: []string{"$sAbiDiffer"},
Dan Willemsen54daaf02018-03-12 13:24:09 -0700291 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800292 },
Logan Chien2a65dda2019-10-01 15:58:07 -0700293 "extraFlags", "referenceDump", "libName", "arch", "createReferenceDumpFlags")
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700294
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500295 // Rule to unzip a reference abi dump.
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700296 unzipRefSAbiDump = pctx.AndroidStaticRule("unzipRefSAbiDump",
297 blueprint.RuleParams{
298 Command: "gunzip -c $in > $out",
299 })
Oliver Nguyenc7434142019-04-24 14:22:25 -0700300
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500301 // Rule to zip files.
Oliver Nguyenc7434142019-04-24 14:22:25 -0700302 zip = pctx.AndroidStaticRule("zip",
303 blueprint.RuleParams{
Colin Cross053fca12020-08-19 13:51:47 -0700304 Command: "${SoongZipCmd} -o ${out} -C $$OUT_DIR -r ${out}.rsp",
Oliver Nguyenc7434142019-04-24 14:22:25 -0700305 CommandDeps: []string{"${SoongZipCmd}"},
Colin Cross053fca12020-08-19 13:51:47 -0700306 Rspfile: "${out}.rsp",
Oliver Nguyenc7434142019-04-24 14:22:25 -0700307 RspfileContent: "$in",
308 })
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800309
310 _ = pctx.SourcePathVariable("cxxExtractor",
311 "prebuilts/clang-tools/${config.HostPrebuiltTag}/bin/cxx_extractor")
Sasha Smundak65143642019-09-26 20:14:28 -0700312 _ = pctx.SourcePathVariable("kytheVnames", "build/soong/vnames.json")
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800313 _ = pctx.VariableFunc("kytheCorpus",
314 func(ctx android.PackageVarContext) string { return ctx.Config().XrefCorpusName() })
Sasha Smundak6c2d4f92020-01-09 17:34:23 -0800315 _ = pctx.VariableFunc("kytheCuEncoding",
316 func(ctx android.PackageVarContext) string { return ctx.Config().XrefCuEncoding() })
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500317
318 // Rule to use kythe extractors to generate .kzip files, used to build code cross references.
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800319 kytheExtract = pctx.StaticRule("kythe",
320 blueprint.RuleParams{
Sasha Smundak6c2d4f92020-01-09 17:34:23 -0800321 Command: `rm -f $out && ` +
Sasha Smundaka4ef83b2020-04-21 17:08:35 -0700322 `KYTHE_CORPUS=${kytheCorpus} ` +
323 `KYTHE_OUTPUT_FILE=$out ` +
324 `KYTHE_VNAMES=$kytheVnames ` +
325 `KYTHE_KZIP_ENCODING=${kytheCuEncoding} ` +
326 `KYTHE_CANONICALIZE_VNAME_PATHS=prefer-relative ` +
Sasha Smundak6c2d4f92020-01-09 17:34:23 -0800327 `$cxxExtractor $cFlags $in `,
Sasha Smundak65143642019-09-26 20:14:28 -0700328 CommandDeps: []string{"$cxxExtractor", "$kytheVnames"},
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800329 },
330 "cFlags")
Colin Cross3f40fa42015-01-30 17:27:36 -0800331)
332
Ivan Lozanof3717ee2020-05-20 09:03:20 -0400333func PwdPrefix() string {
334 // Darwin doesn't have /proc
335 if runtime.GOOS != "darwin" {
336 return "PWD=/proc/self/cwd"
337 }
338 return ""
339}
340
Dan Willemsen322a0a62015-11-17 15:19:46 -0800341func init() {
342 // We run gcc/clang with PWD=/proc/self/cwd to remove $TOP from the
343 // debug output. That way two builds in two different directories will
344 // create the same output.
Ivan Lozanof3717ee2020-05-20 09:03:20 -0400345 pctx.StaticVariable("relPwd", PwdPrefix())
Oliver Nguyenc7434142019-04-24 14:22:25 -0700346
347 pctx.HostBinToolVariable("SoongZipCmd", "soong_zip")
Dan Willemsen322a0a62015-11-17 15:19:46 -0800348}
349
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500350// builderFlags contains various types of command line flags (and settings) for use in building
351// build statements related to C++.
Colin Cross3f40fa42015-01-30 17:27:36 -0800352type builderFlags struct {
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500353 // Global flags (which build system or toolchain is responsible for). These are separate from
354 // local flags because they should appear first (so that they may be overridden by local flags).
Colin Cross6d88dba2019-11-06 07:06:58 -0800355 globalCommonFlags string
356 globalAsFlags string
357 globalYasmFlags string
358 globalCFlags string
359 globalToolingCFlags string // A separate set of cFlags for clang LibTooling tools
360 globalToolingCppFlags string // A separate set of cppFlags for clang LibTooling tools
361 globalConlyFlags string
362 globalCppFlags string
363 globalLdFlags string
364
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500365 // Local flags (which individual modules are responsible for). These may override global flags.
Colin Cross6d88dba2019-11-06 07:06:58 -0800366 localCommonFlags string
367 localAsFlags string
368 localYasmFlags string
369 localCFlags string
370 localToolingCFlags string // A separate set of cFlags for clang LibTooling tools
371 localToolingCppFlags string // A separate set of cppFlags for clang LibTooling tools
372 localConlyFlags string
373 localCppFlags string
374 localLdFlags string
375
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500376 libFlags string // Flags to add to the linker directly after specifying libraries to link.
377 extraLibFlags string // Flags to add to the linker last.
378 tidyFlags string // Flags that apply to clang-tidy
379 sAbiFlags string // Flags that apply to header-abi-dumps
380 aidlFlags string // Flags that apply to aidl source files
381 rsFlags string // Flags that apply to renderscript source files
Colin Cross6d88dba2019-11-06 07:06:58 -0800382 toolchain config.Toolchain
Colin Cross665dce92016-04-28 14:50:03 -0700383
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500384 // True if these extra features are enabled.
Chih-Hung Hsieh7540a782022-01-08 19:56:09 -0800385 tidy bool
386 needTidyFiles bool
387 gcovCoverage bool
388 sAbiDump bool
389 emitXrefs bool
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500390
391 assemblerWithCpp bool // True if .s files should be processed with the c preprocessor.
Dan Willemsen98ab3112019-08-27 21:20:40 -0700392
Colin Crossc3199482017-03-30 15:03:04 -0700393 systemIncludeFlags string
394
Colin Cross19878da2019-03-28 14:45:07 -0700395 proto android.ProtoFlags
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500396 protoC bool // If true, compile protos as `.c` files. Otherwise, output as `.cc`.
397 protoOptionsFile bool // If true, output a proto options file.
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700398
399 yacc *YaccProperties
Matthias Maennich22fd4d12020-07-15 10:58:56 +0200400 lex *LexProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800401}
402
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500403// StripFlags represents flags related to stripping. This is separate from builderFlags, as these
404// flags are useful outside of this package (such as for Rust).
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200405type StripFlags struct {
406 Toolchain config.Toolchain
407 StripKeepSymbols bool
408 StripKeepSymbolsList string
409 StripKeepSymbolsAndDebugFrame bool
410 StripKeepMiniDebugInfo bool
411 StripAddGnuDebuglink bool
412 StripUseGnuStrip bool
413}
414
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500415// Objects is a collection of file paths corresponding to outputs for C++ related build statements.
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700416type Objects struct {
Dan Willemsen581341d2017-02-09 16:16:31 -0800417 objFiles android.Paths
Chih-Hung Hsieh80783772021-10-11 16:46:56 -0700418 tidyFiles android.Paths
Chih-Hung Hsieh7540a782022-01-08 19:56:09 -0800419 tidyDepFiles android.Paths // link dependent .tidy files
Dan Willemsen581341d2017-02-09 16:16:31 -0800420 coverageFiles android.Paths
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800421 sAbiDumpFiles android.Paths
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800422 kytheFiles android.Paths
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700423}
424
425func (a Objects) Copy() Objects {
426 return Objects{
Dan Willemsen581341d2017-02-09 16:16:31 -0800427 objFiles: append(android.Paths{}, a.objFiles...),
Chih-Hung Hsieh80783772021-10-11 16:46:56 -0700428 tidyFiles: append(android.Paths{}, a.tidyFiles...),
Chih-Hung Hsieh7540a782022-01-08 19:56:09 -0800429 tidyDepFiles: append(android.Paths{}, a.tidyDepFiles...),
Dan Willemsen581341d2017-02-09 16:16:31 -0800430 coverageFiles: append(android.Paths{}, a.coverageFiles...),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800431 sAbiDumpFiles: append(android.Paths{}, a.sAbiDumpFiles...),
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800432 kytheFiles: append(android.Paths{}, a.kytheFiles...),
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700433 }
434}
435
436func (a Objects) Append(b Objects) Objects {
437 return Objects{
Dan Willemsen581341d2017-02-09 16:16:31 -0800438 objFiles: append(a.objFiles, b.objFiles...),
439 tidyFiles: append(a.tidyFiles, b.tidyFiles...),
Chih-Hung Hsieh7540a782022-01-08 19:56:09 -0800440 tidyDepFiles: append(a.tidyDepFiles, b.tidyDepFiles...),
Dan Willemsen581341d2017-02-09 16:16:31 -0800441 coverageFiles: append(a.coverageFiles, b.coverageFiles...),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800442 sAbiDumpFiles: append(a.sAbiDumpFiles, b.sAbiDumpFiles...),
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800443 kytheFiles: append(a.kytheFiles, b.kytheFiles...),
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700444 }
445}
446
Colin Cross3f40fa42015-01-30 17:27:36 -0800447// Generate rules for compiling multiple .c, .cpp, or .S files to individual .o files
Chih-Hung Hsieh9db8a0c2022-02-17 12:54:45 -0800448func transformSourceToObj(ctx ModuleContext, subdir string, srcFiles, noTidySrcs, timeoutTidySrcs android.Paths,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800449 flags builderFlags, pathDeps android.Paths, cFlagsDeps android.Paths) Objects {
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500450 // Source files are one-to-one with tidy, coverage, or kythe files, if enabled.
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700451 objFiles := make(android.Paths, len(srcFiles))
Chih-Hung Hsieh80783772021-10-11 16:46:56 -0700452 var tidyFiles android.Paths
Chih-Hung Hsieh8439a142022-02-15 15:32:57 -0800453 noTidySrcsMap := make(map[string]bool)
Chih-Hung Hsieh95dfbce2021-09-09 19:12:05 -0700454 var tidyVars string
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700455 if flags.tidy {
Chih-Hung Hsieh80783772021-10-11 16:46:56 -0700456 tidyFiles = make(android.Paths, 0, len(srcFiles))
Chih-Hung Hsieh769a51c2021-09-17 17:18:39 -0700457 for _, path := range noTidySrcs {
Chih-Hung Hsieh8439a142022-02-15 15:32:57 -0800458 noTidySrcsMap[path.String()] = true
Chih-Hung Hsieh769a51c2021-09-17 17:18:39 -0700459 }
Chih-Hung Hsieh95dfbce2021-09-09 19:12:05 -0700460 tidyTimeout := ctx.Config().Getenv("TIDY_TIMEOUT")
461 if len(tidyTimeout) > 0 {
Chih-Hung Hsiehc74f63e2022-02-10 17:41:13 -0800462 tidyVars += "TIDY_TIMEOUT=" + tidyTimeout + " "
Chih-Hung Hsieh9db8a0c2022-02-17 12:54:45 -0800463 // add timeoutTidySrcs into noTidySrcsMap if TIDY_TIMEOUT is set
464 for _, path := range timeoutTidySrcs {
465 noTidySrcsMap[path.String()] = true
466 }
Chih-Hung Hsieh95dfbce2021-09-09 19:12:05 -0700467 }
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700468 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800469 var coverageFiles android.Paths
Oliver Nguyen04526782020-04-21 12:40:27 -0700470 if flags.gcovCoverage {
Dan Willemsen581341d2017-02-09 16:16:31 -0800471 coverageFiles = make(android.Paths, 0, len(srcFiles))
472 }
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800473 var kytheFiles android.Paths
474 if flags.emitXrefs {
475 kytheFiles = make(android.Paths, 0, len(srcFiles))
476 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800477
Colin Cross6d88dba2019-11-06 07:06:58 -0800478 // Produce fully expanded flags for use by C tools, C compiles, C++ tools, C++ compiles, and asm compiles
479 // respectively.
480 toolingCflags := flags.globalCommonFlags + " " +
481 flags.globalToolingCFlags + " " +
482 flags.globalConlyFlags + " " +
483 flags.localCommonFlags + " " +
484 flags.localToolingCFlags + " " +
485 flags.localConlyFlags + " " +
486 flags.systemIncludeFlags
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700487
Colin Cross6d88dba2019-11-06 07:06:58 -0800488 cflags := flags.globalCommonFlags + " " +
489 flags.globalCFlags + " " +
490 flags.globalConlyFlags + " " +
491 flags.localCommonFlags + " " +
492 flags.localCFlags + " " +
493 flags.localConlyFlags + " " +
494 flags.systemIncludeFlags
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700495
Colin Cross6d88dba2019-11-06 07:06:58 -0800496 toolingCppflags := flags.globalCommonFlags + " " +
497 flags.globalToolingCFlags + " " +
498 flags.globalToolingCppFlags + " " +
499 flags.localCommonFlags + " " +
500 flags.localToolingCFlags + " " +
501 flags.localToolingCppFlags + " " +
502 flags.systemIncludeFlags
Colin Crossc3199482017-03-30 15:03:04 -0700503
Colin Cross6d88dba2019-11-06 07:06:58 -0800504 cppflags := flags.globalCommonFlags + " " +
505 flags.globalCFlags + " " +
506 flags.globalCppFlags + " " +
507 flags.localCommonFlags + " " +
508 flags.localCFlags + " " +
509 flags.localCppFlags + " " +
510 flags.systemIncludeFlags
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700511
Colin Cross6d88dba2019-11-06 07:06:58 -0800512 asflags := flags.globalCommonFlags + " " +
513 flags.globalAsFlags + " " +
514 flags.localCommonFlags + " " +
515 flags.localAsFlags + " " +
516 flags.systemIncludeFlags
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700517
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800518 var sAbiDumpFiles android.Paths
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700519 if flags.sAbiDump {
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800520 sAbiDumpFiles = make(android.Paths, 0, len(srcFiles))
521 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800522
Colin Cross0523ba22021-07-14 18:45:05 -0700523 cflags += " ${config.NoOverrideGlobalCflags}"
524 toolingCflags += " ${config.NoOverrideGlobalCflags}"
525 cppflags += " ${config.NoOverrideGlobalCflags}"
526 toolingCppflags += " ${config.NoOverrideGlobalCflags}"
Dan Willemsenbe03f342016-03-03 17:21:04 -0800527
Stephen Hinese24303f2021-12-14 15:07:08 -0800528 modulePath := android.PathForModuleSrc(ctx).String()
529 if android.IsThirdPartyPath(modulePath) {
530 cflags += " ${config.NoOverrideExternalGlobalCflags}"
531 toolingCflags += " ${config.NoOverrideExternalGlobalCflags}"
532 cppflags += " ${config.NoOverrideExternalGlobalCflags}"
533 toolingCppflags += " ${config.NoOverrideExternalGlobalCflags}"
534 }
535
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -0700536 // Multiple source files have build rules usually share the same cFlags or tidyFlags.
537 // Define only one version in this module and share it in multiple build rules.
538 // To simplify the code, the shared variables are all named as $flags<nnn>.
Chih-Hung Hsieh7540a782022-01-08 19:56:09 -0800539 shared := ctx.getSharedFlags()
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -0700540
541 // Share flags only when there are multiple files or tidy rules.
542 var hasMultipleRules = len(srcFiles) > 1 || flags.tidy
543
544 var shareFlags = func(kind string, flags string) string {
545 if !hasMultipleRules || len(flags) < 60 {
546 // Modules have long names and so do the module variables.
547 // It does not save space by replacing a short name with a long one.
548 return flags
549 }
550 mapKey := kind + flags
Chih-Hung Hsieh7540a782022-01-08 19:56:09 -0800551 n, ok := shared.flagsMap[mapKey]
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -0700552 if !ok {
Chih-Hung Hsieh7540a782022-01-08 19:56:09 -0800553 shared.numSharedFlags += 1
554 n = strconv.Itoa(shared.numSharedFlags)
555 shared.flagsMap[mapKey] = n
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -0700556 ctx.Variable(pctx, kind+n, flags)
557 }
558 return "$" + kind + n
559 }
560
Colin Cross3f40fa42015-01-30 17:27:36 -0800561 for i, srcFile := range srcFiles {
Dan Willemsen21ec4902016-11-02 20:43:13 -0700562 objFile := android.ObjPathWithExt(ctx, subdir, srcFile, "o")
Colin Cross3f40fa42015-01-30 17:27:36 -0800563
564 objFiles[i] = objFile
565
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500566 // Register compilation build statements. The actual rule used depends on the source file type.
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700567 switch srcFile.Ext() {
568 case ".asm":
Colin Crossae887032017-10-23 17:16:14 -0700569 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700570 Rule: yasm,
571 Description: "yasm " + srcFile.Rel(),
572 Output: objFile,
573 Input: srcFile,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800574 Implicits: cFlagsDeps,
575 OrderOnly: pathDeps,
Colin Cross91e90042016-12-02 17:13:24 -0800576 Args: map[string]string{
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -0700577 "asFlags": shareFlags("asFlags", flags.globalYasmFlags+" "+flags.localYasmFlags),
Colin Cross91e90042016-12-02 17:13:24 -0800578 },
579 })
580 continue
Pete Bentleyfcf55bf2019-08-16 20:14:32 +0100581 case ".o":
582 objFiles[i] = srcFile
583 continue
Colin Cross91e90042016-12-02 17:13:24 -0800584 }
585
Colin Cross6d88dba2019-11-06 07:06:58 -0800586 var moduleFlags string
587 var moduleToolingFlags string
588
Colin Cross3f40fa42015-01-30 17:27:36 -0800589 var ccCmd string
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700590 tidy := flags.tidy
Oliver Nguyen04526782020-04-21 12:40:27 -0700591 coverage := flags.gcovCoverage
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700592 dump := flags.sAbiDump
Dan Willemsenfcabb1c2019-01-03 23:25:11 -0800593 rule := cc
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800594 emitXref := flags.emitXrefs
Colin Cross3f40fa42015-01-30 17:27:36 -0800595
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700596 switch srcFile.Ext() {
Dan Willemsenfcabb1c2019-01-03 23:25:11 -0800597 case ".s":
Dan Willemsen98ab3112019-08-27 21:20:40 -0700598 if !flags.assemblerWithCpp {
599 rule = ccNoDeps
600 }
Dan Willemsenfcabb1c2019-01-03 23:25:11 -0800601 fallthrough
602 case ".S":
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700603 ccCmd = "clang"
Colin Cross6d88dba2019-11-06 07:06:58 -0800604 moduleFlags = asflags
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700605 tidy = false
Dan Willemsen581341d2017-02-09 16:16:31 -0800606 coverage = false
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800607 dump = false
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800608 emitXref = false
Colin Cross3f40fa42015-01-30 17:27:36 -0800609 case ".c":
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700610 ccCmd = "clang"
Colin Cross6d88dba2019-11-06 07:06:58 -0800611 moduleFlags = cflags
612 moduleToolingFlags = toolingCflags
Colin Crossd34ab7c2019-06-27 14:46:10 -0700613 case ".cpp", ".cc", ".cxx", ".mm":
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700614 ccCmd = "clang++"
Colin Cross6d88dba2019-11-06 07:06:58 -0800615 moduleFlags = cppflags
616 moduleToolingFlags = toolingCppflags
Liz Kammere2861132020-12-03 22:12:41 +0000617 case ".h", ".hpp":
618 ctx.PropertyErrorf("srcs", "Header file %s is not supported, instead use export_include_dirs or local_include_dirs.", srcFile)
619 continue
Colin Cross3f40fa42015-01-30 17:27:36 -0800620 default:
Liz Kammere2861132020-12-03 22:12:41 +0000621 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 -0800622 continue
623 }
624
Chih-Hung Hsieh30ab2f02022-05-28 18:52:12 -0700625 // ccCmd is "clang" or "clang++"
Colin Cross67a5c132017-05-09 13:45:28 -0700626 ccDesc := ccCmd
627
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700628 ccCmd = "${config.ClangBin}/" + ccCmd
Colin Cross3f40fa42015-01-30 17:27:36 -0800629
Dan Willemsen581341d2017-02-09 16:16:31 -0800630 var implicitOutputs android.WritablePaths
631 if coverage {
632 gcnoFile := android.ObjPathWithExt(ctx, subdir, srcFile, "gcno")
633 implicitOutputs = append(implicitOutputs, gcnoFile)
634 coverageFiles = append(coverageFiles, gcnoFile)
635 }
636
Colin Crossae887032017-10-23 17:16:14 -0700637 ctx.Build(pctx, android.BuildParams{
Dan Willemsenfcabb1c2019-01-03 23:25:11 -0800638 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -0700639 Description: ccDesc + " " + srcFile.Rel(),
Dan Willemsen581341d2017-02-09 16:16:31 -0800640 Output: objFile,
641 ImplicitOutputs: implicitOutputs,
642 Input: srcFile,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800643 Implicits: cFlagsDeps,
644 OrderOnly: pathDeps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800645 Args: map[string]string{
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -0700646 "cFlags": shareFlags("cFlags", moduleFlags),
647 "ccCmd": ccCmd, // short and not shared
Colin Cross3f40fa42015-01-30 17:27:36 -0800648 },
649 })
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700650
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500651 // Register post-process build statements (such as for tidy or kythe).
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800652 if emitXref {
653 kytheFile := android.ObjPathWithExt(ctx, subdir, srcFile, "kzip")
654 ctx.Build(pctx, android.BuildParams{
655 Rule: kytheExtract,
656 Description: "Xref C++ extractor " + srcFile.Rel(),
657 Output: kytheFile,
658 Input: srcFile,
659 Implicits: cFlagsDeps,
660 OrderOnly: pathDeps,
661 Args: map[string]string{
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -0700662 "cFlags": shareFlags("cFlags", moduleFlags),
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800663 },
664 })
665 kytheFiles = append(kytheFiles, kytheFile)
666 }
667
Chih-Hung Hsieh769a51c2021-09-17 17:18:39 -0700668 // Even with tidy, some src file could be skipped by noTidySrcsMap.
Chih-Hung Hsieh8439a142022-02-15 15:32:57 -0800669 if tidy && !noTidySrcsMap[srcFile.String()] {
Dan Willemsen21ec4902016-11-02 20:43:13 -0700670 tidyFile := android.ObjPathWithExt(ctx, subdir, srcFile, "tidy")
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700671 tidyFiles = append(tidyFiles, tidyFile)
Chih-Hung Hsieh30ab2f02022-05-28 18:52:12 -0700672 tidyCmd := "${config.ClangBin}/clang-tidy"
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700673
Kousik Kumar4e30bba2020-06-18 09:17:51 -0700674 rule := clangTidy
Ramy Medhat16f23a42020-09-03 01:29:49 -0400675 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_CLANG_TIDY") {
Kousik Kumar4e30bba2020-06-18 09:17:51 -0700676 rule = clangTidyRE
677 }
678
Chih-Hung Hsiehc74f63e2022-02-10 17:41:13 -0800679 sharedCFlags := shareFlags("cFlags", moduleFlags)
680 srcRelPath := srcFile.Rel()
681
Chih-Hung Hsieh30ab2f02022-05-28 18:52:12 -0700682 // Add the .tidy rule
Chih-Hung Hsiehc74f63e2022-02-10 17:41:13 -0800683 ctx.Build(pctx, android.BuildParams{
684 Rule: rule,
685 Description: "clang-tidy " + srcRelPath,
686 Output: tidyFile,
687 Input: srcFile,
688 Implicits: cFlagsDeps,
Chih-Hung Hsieh30ab2f02022-05-28 18:52:12 -0700689 OrderOnly: pathDeps,
Chih-Hung Hsiehc74f63e2022-02-10 17:41:13 -0800690 Args: map[string]string{
691 "cFlags": sharedCFlags,
Chih-Hung Hsieh30ab2f02022-05-28 18:52:12 -0700692 "ccCmd": ccCmd,
693 "clangCmd": ccDesc,
694 "tidyCmd": tidyCmd,
Chih-Hung Hsiehc74f63e2022-02-10 17:41:13 -0800695 "tidyFlags": shareFlags("tidyFlags", config.TidyFlagsForSrcFile(srcFile, flags.tidyFlags)),
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -0700696 "tidyVars": tidyVars, // short and not shared
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700697 },
698 })
699 }
700
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800701 if dump {
702 sAbiDumpFile := android.ObjPathWithExt(ctx, subdir, srcFile, "sdump")
703 sAbiDumpFiles = append(sAbiDumpFiles, sAbiDumpFile)
704
Ramy Medhat31ec9422020-04-17 15:03:58 -0400705 dumpRule := sAbiDump
Ramy Medhat16f23a42020-09-03 01:29:49 -0400706 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_ABI_DUMPER") {
Ramy Medhat31ec9422020-04-17 15:03:58 -0400707 dumpRule = sAbiDumpRE
708 }
Colin Crossae887032017-10-23 17:16:14 -0700709 ctx.Build(pctx, android.BuildParams{
Ramy Medhat31ec9422020-04-17 15:03:58 -0400710 Rule: dumpRule,
Colin Cross67a5c132017-05-09 13:45:28 -0700711 Description: "header-abi-dumper " + srcFile.Rel(),
712 Output: sAbiDumpFile,
713 Input: srcFile,
714 Implicit: objFile,
Dan Willemsen6b4419c2019-08-09 12:45:53 -0700715 Implicits: cFlagsDeps,
716 OrderOnly: pathDeps,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800717 Args: map[string]string{
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -0700718 "cFlags": shareFlags("cFlags", moduleToolingFlags),
719 "exportDirs": shareFlags("exportDirs", flags.sAbiFlags),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800720 },
721 })
722 }
723
Colin Cross3f40fa42015-01-30 17:27:36 -0800724 }
725
Chih-Hung Hsieh7540a782022-01-08 19:56:09 -0800726 var tidyDepFiles android.Paths
727 if flags.needTidyFiles {
728 tidyDepFiles = tidyFiles
729 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700730 return Objects{
Dan Willemsen581341d2017-02-09 16:16:31 -0800731 objFiles: objFiles,
732 tidyFiles: tidyFiles,
Chih-Hung Hsieh7540a782022-01-08 19:56:09 -0800733 tidyDepFiles: tidyDepFiles,
Dan Willemsen581341d2017-02-09 16:16:31 -0800734 coverageFiles: coverageFiles,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800735 sAbiDumpFiles: sAbiDumpFiles,
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800736 kytheFiles: kytheFiles,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700737 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800738}
739
740// Generate a rule for compiling multiple .o files to a static library (.a)
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500741func transformObjToStaticLib(ctx android.ModuleContext,
Martin Stjernholm391d94c2020-04-17 17:34:31 +0100742 objFiles android.Paths, wholeStaticLibs android.Paths,
Chih-Hung Hsieh80783772021-10-11 16:46:56 -0700743 flags builderFlags, outputFile android.ModuleOutPath, deps android.Paths, validations android.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800744
Stephen Hinesf1addeb2018-01-09 23:29:04 -0800745 arCmd := "${config.ClangBin}/llvm-ar"
Martin Stjernholm391d94c2020-04-17 17:34:31 +0100746 arFlags := ""
Stephen Hinesf1addeb2018-01-09 23:29:04 -0800747 if !ctx.Darwin() {
Mitch Phillipsed0dd762022-01-25 10:35:50 -0800748 arFlags += " --format=gnu"
Stephen Hinesf1addeb2018-01-09 23:29:04 -0800749 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800750
Martin Stjernholm391d94c2020-04-17 17:34:31 +0100751 if len(wholeStaticLibs) == 0 {
752 ctx.Build(pctx, android.BuildParams{
753 Rule: ar,
754 Description: "static link " + outputFile.Base(),
755 Output: outputFile,
756 Inputs: objFiles,
757 Implicits: deps,
Chih-Hung Hsieh80783772021-10-11 16:46:56 -0700758 Validations: validations,
Martin Stjernholm391d94c2020-04-17 17:34:31 +0100759 Args: map[string]string{
760 "arFlags": "crsPD" + arFlags,
761 "arCmd": arCmd,
762 },
763 })
764
765 } else {
766 ctx.Build(pctx, android.BuildParams{
767 Rule: arWithLibs,
768 Description: "static link " + outputFile.Base(),
769 Output: outputFile,
770 Inputs: append(objFiles, wholeStaticLibs...),
771 Implicits: deps,
772 Args: map[string]string{
773 "arCmd": arCmd,
774 "arObjFlags": "crsPD" + arFlags,
775 "arObjs": strings.Join(objFiles.Strings(), " "),
776 "arLibFlags": "cqsL" + arFlags,
777 "arLibs": strings.Join(wholeStaticLibs.Strings(), " "),
778 },
779 })
780 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800781}
782
783// Generate a rule for compiling multiple .o files, plus static libraries, whole static libraries,
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700784// and shared libraries, to a shared library (.so) or dynamic executable
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500785func transformObjToDynamicBinary(ctx android.ModuleContext,
Colin Crossc465efd2021-06-11 18:00:04 -0700786 objFiles, sharedLibs, staticLibs, lateStaticLibs, wholeStaticLibs, deps, crtBegin, crtEnd android.Paths,
787 groupLate bool, flags builderFlags, outputFile android.WritablePath,
Chih-Hung Hsieh80783772021-10-11 16:46:56 -0700788 implicitOutputs android.WritablePaths, validations android.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800789
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700790 ldCmd := "${config.ClangBin}/clang++"
Colin Cross3f40fa42015-01-30 17:27:36 -0800791
Colin Cross3f40fa42015-01-30 17:27:36 -0800792 var libFlagsList []string
793
Colin Cross16b23492016-01-06 14:41:07 -0800794 if len(flags.libFlags) > 0 {
795 libFlagsList = append(libFlagsList, flags.libFlags)
796 }
797
Colin Cross3f40fa42015-01-30 17:27:36 -0800798 if len(wholeStaticLibs) > 0 {
Dan Willemsen490fd492015-11-24 17:53:15 -0800799 if ctx.Host() && ctx.Darwin() {
Colin Cross635c3b02016-05-18 15:37:25 -0700800 libFlagsList = append(libFlagsList, android.JoinWithPrefix(wholeStaticLibs.Strings(), "-force_load "))
Colin Cross0af4b842015-04-30 16:36:18 -0700801 } else {
802 libFlagsList = append(libFlagsList, "-Wl,--whole-archive ")
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700803 libFlagsList = append(libFlagsList, wholeStaticLibs.Strings()...)
Colin Cross0af4b842015-04-30 16:36:18 -0700804 libFlagsList = append(libFlagsList, "-Wl,--no-whole-archive ")
805 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800806 }
807
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700808 libFlagsList = append(libFlagsList, staticLibs.Strings()...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800809
Stephen Hines10347862016-07-18 15:54:54 -0700810 if groupLate && !ctx.Darwin() && len(lateStaticLibs) > 0 {
Dan Willemsenedc385f2015-07-08 13:02:23 -0700811 libFlagsList = append(libFlagsList, "-Wl,--start-group")
812 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700813 libFlagsList = append(libFlagsList, lateStaticLibs.Strings()...)
Stephen Hines10347862016-07-18 15:54:54 -0700814 if groupLate && !ctx.Darwin() && len(lateStaticLibs) > 0 {
Dan Willemsenedc385f2015-07-08 13:02:23 -0700815 libFlagsList = append(libFlagsList, "-Wl,--end-group")
816 }
817
Colin Cross3f40fa42015-01-30 17:27:36 -0800818 for _, lib := range sharedLibs {
Josh Gao75a50a22019-06-07 17:58:59 -0700819 libFile := lib.String()
820 if ctx.Windows() {
821 libFile = pathtools.ReplaceExtension(libFile, "lib")
822 }
823 libFlagsList = append(libFlagsList, libFile)
Colin Cross3f40fa42015-01-30 17:27:36 -0800824 }
825
Colin Cross3f40fa42015-01-30 17:27:36 -0800826 deps = append(deps, staticLibs...)
Colin Cross3075ad02015-03-17 10:47:08 -0700827 deps = append(deps, lateStaticLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800828 deps = append(deps, wholeStaticLibs...)
Colin Crossc465efd2021-06-11 18:00:04 -0700829 deps = append(deps, crtBegin...)
830 deps = append(deps, crtEnd...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800831
Colin Cross97d87762022-02-07 15:44:54 -0800832 var depFile android.WritablePath
833 var depFileLdFlags string
834 depsType := blueprint.DepsNone
835 if !ctx.Windows() && !ctx.Darwin() {
836 // lld only supports --dependency-file for elf files
837 depFile = outputFile.ReplaceExtension(ctx, "d")
838 depFileLdFlags = " -Wl,--dependency-file=" + depFile.String()
839 depsType = blueprint.DepsGCC
840 implicitOutputs = append(implicitOutputs, depFile)
841 }
842
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400843 rule := ld
Kousik Kumar3fb61262020-04-22 13:31:09 -0700844 args := map[string]string{
845 "ldCmd": ldCmd,
Colin Crossc465efd2021-06-11 18:00:04 -0700846 "crtBegin": strings.Join(crtBegin.Strings(), " "),
Kousik Kumar3fb61262020-04-22 13:31:09 -0700847 "libFlags": strings.Join(libFlagsList, " "),
848 "extraLibFlags": flags.extraLibFlags,
Colin Cross97d87762022-02-07 15:44:54 -0800849 "ldFlags": flags.globalLdFlags + " " + flags.localLdFlags + depFileLdFlags,
Colin Crossc465efd2021-06-11 18:00:04 -0700850 "crtEnd": strings.Join(crtEnd.Strings(), " "),
Kousik Kumar3fb61262020-04-22 13:31:09 -0700851 }
Ramy Medhat16f23a42020-09-03 01:29:49 -0400852 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_CXX_LINKS") {
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400853 rule = ldRE
Kousik Kumar3fb61262020-04-22 13:31:09 -0700854 args["implicitOutputs"] = strings.Join(implicitOutputs.Strings(), ",")
Ramy Medhat6797edc2020-08-28 14:21:55 -0400855 args["implicitInputs"] = strings.Join(deps.Strings(), ",")
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400856 }
857
Colin Crossae887032017-10-23 17:16:14 -0700858 ctx.Build(pctx, android.BuildParams{
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400859 Rule: rule,
Colin Cross97d87762022-02-07 15:44:54 -0800860 Deps: depsType,
861 Depfile: depFile,
Josh Gao75a50a22019-06-07 17:58:59 -0700862 Description: "link " + outputFile.Base(),
863 Output: outputFile,
864 ImplicitOutputs: implicitOutputs,
865 Inputs: objFiles,
866 Implicits: deps,
Colin Cross58dd6a92021-05-13 17:45:36 -0700867 OrderOnly: sharedLibs,
Chih-Hung Hsieh80783772021-10-11 16:46:56 -0700868 Validations: validations,
Kousik Kumar3fb61262020-04-22 13:31:09 -0700869 Args: args,
Colin Cross3f40fa42015-01-30 17:27:36 -0800870 })
871}
872
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800873// Generate a rule to combine .dump sAbi dump files from multiple source files
874// into a single .ldump sAbi dump file
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500875func transformDumpToLinkedDump(ctx android.ModuleContext, sAbiDumps android.Paths, soFile android.Path,
Logan Chiene3d7a0d2019-01-17 00:18:02 +0800876 baseName, exportedHeaderFlags string, symbolFile android.OptionalPath,
877 excludedSymbolVersions, excludedSymbolTags []string) android.OptionalPath {
878
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800879 outputFile := android.PathForModuleOut(ctx, baseName+".lsdump")
Logan Chiene3d7a0d2019-01-17 00:18:02 +0800880
881 implicits := android.Paths{soFile}
Jayant Chowdharydf344d52018-01-17 11:11:42 -0800882 symbolFilterStr := "-so " + soFile.String()
Logan Chiene3d7a0d2019-01-17 00:18:02 +0800883
884 if symbolFile.Valid() {
885 implicits = append(implicits, symbolFile.Path())
886 symbolFilterStr += " -v " + symbolFile.String()
887 }
888 for _, ver := range excludedSymbolVersions {
889 symbolFilterStr += " --exclude-symbol-version " + ver
890 }
891 for _, tag := range excludedSymbolTags {
892 symbolFilterStr += " --exclude-symbol-tag " + tag
893 }
Ramy Medhat808594c2020-05-07 06:56:47 -0400894 rule := sAbiLink
895 args := map[string]string{
896 "symbolFilter": symbolFilterStr,
897 "arch": ctx.Arch().ArchType.Name,
898 "exportedHeaderFlags": exportedHeaderFlags,
899 }
Ramy Medhat16f23a42020-09-03 01:29:49 -0400900 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_ABI_LINKER") {
Ramy Medhat808594c2020-05-07 06:56:47 -0400901 rule = sAbiLinkRE
902 rbeImplicits := implicits.Strings()
903 for _, p := range strings.Split(exportedHeaderFlags, " ") {
904 if len(p) > 2 {
905 // Exclude the -I prefix.
906 rbeImplicits = append(rbeImplicits, p[2:])
907 }
908 }
Ramy Medhat6797edc2020-08-28 14:21:55 -0400909 args["implicitInputs"] = strings.Join(rbeImplicits, ",")
Ramy Medhat808594c2020-05-07 06:56:47 -0400910 }
Colin Crossae887032017-10-23 17:16:14 -0700911 ctx.Build(pctx, android.BuildParams{
Ramy Medhat808594c2020-05-07 06:56:47 -0400912 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -0700913 Description: "header-abi-linker " + outputFile.Base(),
914 Output: outputFile,
915 Inputs: sAbiDumps,
Logan Chiene3d7a0d2019-01-17 00:18:02 +0800916 Implicits: implicits,
Ramy Medhat808594c2020-05-07 06:56:47 -0400917 Args: args,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800918 })
919 return android.OptionalPathForPath(outputFile)
920}
921
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500922// unzipRefDump registers a build statement to unzip a reference abi dump.
923func unzipRefDump(ctx android.ModuleContext, zippedRefDump android.Path, baseName string) android.Path {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700924 outputFile := android.PathForModuleOut(ctx, baseName+"_ref.lsdump")
Colin Crossae887032017-10-23 17:16:14 -0700925 ctx.Build(pctx, android.BuildParams{
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700926 Rule: unzipRefSAbiDump,
927 Description: "gunzip" + outputFile.Base(),
928 Output: outputFile,
929 Input: zippedRefDump,
930 })
931 return outputFile
932}
933
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500934// sourceAbiDiff registers a build statement to compare linked sAbi dump files (.ldump).
935func sourceAbiDiff(ctx android.ModuleContext, inputDump android.Path, referenceDump android.Path,
Logan Chien2a65dda2019-10-01 15:58:07 -0700936 baseName, exportedHeaderFlags string, checkAllApis, isLlndk, isNdk, isVndkExt bool) android.OptionalPath {
Logan Chienf3511742017-10-31 18:04:35 +0800937
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800938 outputFile := android.PathForModuleOut(ctx, baseName+".abidiff")
Jayant Chowdharyc7434e22018-05-31 15:42:26 -0700939 libName := strings.TrimSuffix(baseName, filepath.Ext(baseName))
Logan Chien6227fed2019-02-18 13:12:21 +0800940 createReferenceDumpFlags := ""
941
Logan Chien2a65dda2019-10-01 15:58:07 -0700942 var extraFlags []string
943 if checkAllApis {
944 extraFlags = append(extraFlags, "-check-all-apis")
945 } else {
946 extraFlags = append(extraFlags,
947 "-allow-unreferenced-changes",
948 "-allow-unreferenced-elf-symbol-changes")
Jayant Chowdharye4499502018-01-17 13:13:33 -0800949 }
Logan Chien2a65dda2019-10-01 15:58:07 -0700950
951 if exportedHeaderFlags == "" {
952 extraFlags = append(extraFlags, "-advice-only")
953 }
954
Logan Chien62f1f942019-02-18 15:40:42 +0800955 if isLlndk || isNdk {
Logan Chien6227fed2019-02-18 13:12:21 +0800956 createReferenceDumpFlags = "--llndk"
Logan Chien62f1f942019-02-18 15:40:42 +0800957 if isLlndk {
958 // TODO(b/130324828): "-consider-opaque-types-different" should apply to
959 // both LLNDK and NDK shared libs. However, a known issue in header-abi-diff
960 // breaks libaaudio. Remove the if-guard after the issue is fixed.
Logan Chien2a65dda2019-10-01 15:58:07 -0700961 extraFlags = append(extraFlags, "-consider-opaque-types-different")
Logan Chien62f1f942019-02-18 15:40:42 +0800962 }
Jayant Chowdharyc7434e22018-05-31 15:42:26 -0700963 }
Logan Chienf3511742017-10-31 18:04:35 +0800964 if isVndkExt {
Logan Chien2a65dda2019-10-01 15:58:07 -0700965 extraFlags = append(extraFlags, "-allow-extensions")
Logan Chienf3511742017-10-31 18:04:35 +0800966 }
967
Colin Crossae887032017-10-23 17:16:14 -0700968 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700969 Rule: sAbiDiff,
970 Description: "header-abi-diff " + outputFile.Base(),
971 Output: outputFile,
972 Input: inputDump,
973 Implicit: referenceDump,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800974 Args: map[string]string{
Logan Chien6227fed2019-02-18 13:12:21 +0800975 "referenceDump": referenceDump.String(),
976 "libName": libName,
977 "arch": ctx.Arch().ArchType.Name,
Logan Chien2a65dda2019-10-01 15:58:07 -0700978 "extraFlags": strings.Join(extraFlags, " "),
Logan Chien6227fed2019-02-18 13:12:21 +0800979 "createReferenceDumpFlags": createReferenceDumpFlags,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800980 },
981 })
982 return android.OptionalPathForPath(outputFile)
983}
984
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700985// Generate a rule for extracting a table of contents from a shared library (.so)
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400986func TransformSharedObjectToToc(ctx android.ModuleContext, inputFile android.Path, outputFile android.WritablePath) {
Colin Cross26c34ed2016-09-30 17:10:16 -0700987
Colin Crossb496cfd2018-09-10 16:50:05 -0700988 var format string
Colin Crossb496cfd2018-09-10 16:50:05 -0700989 if ctx.Darwin() {
990 format = "--macho"
Colin Crossb496cfd2018-09-10 16:50:05 -0700991 } else if ctx.Windows() {
992 format = "--pe"
Colin Crossb496cfd2018-09-10 16:50:05 -0700993 } else {
994 format = "--elf"
Colin Crossb496cfd2018-09-10 16:50:05 -0700995 }
Colin Cross26c34ed2016-09-30 17:10:16 -0700996
Colin Crossae887032017-10-23 17:16:14 -0700997 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700998 Rule: toc,
999 Description: "generate toc " + inputFile.Base(),
1000 Output: outputFile,
1001 Input: inputFile,
Colin Cross26c34ed2016-09-30 17:10:16 -07001002 Args: map[string]string{
Yi Kong4ad44e72021-04-01 17:45:42 +08001003 "clangBin": "${config.ClangBin}",
1004 "format": format,
Colin Cross26c34ed2016-09-30 17:10:16 -07001005 },
1006 })
1007}
1008
Colin Cross3f40fa42015-01-30 17:27:36 -08001009// Generate a rule for compiling multiple .o files to a .o using ld partial linking
Chris Parsonsbf4f55f2020-11-23 17:02:44 -05001010func transformObjsToObj(ctx android.ModuleContext, objFiles android.Paths,
Dan Willemsen724ab5d2019-09-19 10:50:18 -07001011 flags builderFlags, outputFile android.WritablePath, deps android.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001012
Dan Willemsen8536d6b2018-10-07 20:54:34 -07001013 ldCmd := "${config.ClangBin}/clang++"
Colin Cross3f40fa42015-01-30 17:27:36 -08001014
Colin Cross97d87762022-02-07 15:44:54 -08001015 var implicitOutputs android.WritablePaths
1016 var depFile android.WritablePath
1017 var depFileLdFlags string
1018 depsType := blueprint.DepsNone
1019 if !ctx.Windows() && !ctx.Darwin() {
1020 // lld only supports --dependency-file for elf files
1021 depFile = outputFile.ReplaceExtension(ctx, "d")
1022 depFileLdFlags = " -Wl,--dependency-file=" + depFile.String()
1023 depsType = blueprint.DepsGCC
1024 implicitOutputs = append(implicitOutputs, depFile)
1025 }
1026
Ramy Medhat9a90fe52020-04-13 13:21:23 -04001027 rule := partialLd
1028 args := map[string]string{
1029 "ldCmd": ldCmd,
Colin Cross97d87762022-02-07 15:44:54 -08001030 "ldFlags": flags.globalLdFlags + " " + flags.localLdFlags + depFileLdFlags,
Ramy Medhat9a90fe52020-04-13 13:21:23 -04001031 }
Ramy Medhat16f23a42020-09-03 01:29:49 -04001032 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_CXX_LINKS") {
Ramy Medhat9a90fe52020-04-13 13:21:23 -04001033 rule = partialLdRE
1034 args["inCommaList"] = strings.Join(objFiles.Strings(), ",")
Ramy Medhat6797edc2020-08-28 14:21:55 -04001035 args["implicitInputs"] = strings.Join(deps.Strings(), ",")
Colin Cross97d87762022-02-07 15:44:54 -08001036 args["implicitOutputs"] = strings.Join(implicitOutputs.Strings(), ",")
Ramy Medhat9a90fe52020-04-13 13:21:23 -04001037 }
Colin Crossae887032017-10-23 17:16:14 -07001038 ctx.Build(pctx, android.BuildParams{
Ramy Medhat9a90fe52020-04-13 13:21:23 -04001039 Rule: rule,
Colin Cross97d87762022-02-07 15:44:54 -08001040 Deps: depsType,
1041 Depfile: depFile,
Colin Cross67a5c132017-05-09 13:45:28 -07001042 Description: "link " + outputFile.Base(),
1043 Output: outputFile,
1044 Inputs: objFiles,
Dan Willemsen724ab5d2019-09-19 10:50:18 -07001045 Implicits: deps,
Ramy Medhat9a90fe52020-04-13 13:21:23 -04001046 Args: args,
Colin Cross3f40fa42015-01-30 17:27:36 -08001047 })
1048}
1049
Chris Parsonsbf4f55f2020-11-23 17:02:44 -05001050// Generate a rule for running objcopy --prefix-symbols on a binary
1051func transformBinaryPrefixSymbols(ctx android.ModuleContext, prefix string, inputFile android.Path,
Colin Cross635c3b02016-05-18 15:37:25 -07001052 flags builderFlags, outputFile android.WritablePath) {
Colin Crossbfae8852015-03-26 14:44:11 -07001053
Elliott Hughes2e70f792021-04-19 12:58:43 -07001054 objcopyCmd := "${config.ClangBin}/llvm-objcopy"
Colin Crossbfae8852015-03-26 14:44:11 -07001055
Colin Crossae887032017-10-23 17:16:14 -07001056 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -07001057 Rule: prefixSymbols,
1058 Description: "prefix symbols " + outputFile.Base(),
1059 Output: outputFile,
1060 Input: inputFile,
Colin Crossbfae8852015-03-26 14:44:11 -07001061 Args: map[string]string{
1062 "objcopyCmd": objcopyCmd,
1063 "prefix": prefix,
1064 },
1065 })
1066}
1067
Chris Parsonsbf4f55f2020-11-23 17:02:44 -05001068// Registers a build statement to invoke `strip` (to discard symbols and data from object files).
1069func transformStrip(ctx android.ModuleContext, inputFile android.Path,
Thiébaud Weksteend4587452020-08-19 14:53:01 +02001070 outputFile android.WritablePath, flags StripFlags) {
Colin Cross665dce92016-04-28 14:50:03 -07001071
Colin Cross665dce92016-04-28 14:50:03 -07001072 args := ""
Thiébaud Weksteend4587452020-08-19 14:53:01 +02001073 if flags.StripAddGnuDebuglink {
Colin Cross665dce92016-04-28 14:50:03 -07001074 args += " --add-gnu-debuglink"
1075 }
Thiébaud Weksteend4587452020-08-19 14:53:01 +02001076 if flags.StripKeepMiniDebugInfo {
Colin Cross665dce92016-04-28 14:50:03 -07001077 args += " --keep-mini-debug-info"
1078 }
Thiébaud Weksteend4587452020-08-19 14:53:01 +02001079 if flags.StripKeepSymbols {
Colin Cross665dce92016-04-28 14:50:03 -07001080 args += " --keep-symbols"
1081 }
Thiébaud Weksteend4587452020-08-19 14:53:01 +02001082 if flags.StripKeepSymbolsList != "" {
1083 args += " -k" + flags.StripKeepSymbolsList
Yi Kongacee27c2019-03-29 20:05:14 -07001084 }
Thiébaud Weksteend4587452020-08-19 14:53:01 +02001085 if flags.StripKeepSymbolsAndDebugFrame {
Christopher Ferrisb43fe7a2019-05-17 16:39:54 -07001086 args += " --keep-symbols-and-debug-frame"
1087 }
Colin Cross665dce92016-04-28 14:50:03 -07001088
Colin Crossae887032017-10-23 17:16:14 -07001089 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -07001090 Rule: strip,
1091 Description: "strip " + outputFile.Base(),
1092 Output: outputFile,
1093 Input: inputFile,
Colin Cross665dce92016-04-28 14:50:03 -07001094 Args: map[string]string{
Elliott Hughesafcd3392021-05-05 13:54:11 -07001095 "args": args,
Colin Cross665dce92016-04-28 14:50:03 -07001096 },
1097 })
1098}
1099
Chris Parsonsbf4f55f2020-11-23 17:02:44 -05001100// Registers build statement to invoke `strip` on darwin architecture.
1101func transformDarwinStrip(ctx android.ModuleContext, inputFile android.Path,
Colin Cross635c3b02016-05-18 15:37:25 -07001102 outputFile android.WritablePath) {
Colin Crossb8ecdfe2016-05-03 15:10:29 -07001103
Colin Crossae887032017-10-23 17:16:14 -07001104 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -07001105 Rule: darwinStrip,
1106 Description: "strip " + outputFile.Base(),
1107 Output: outputFile,
1108 Input: inputFile,
Colin Crossb8ecdfe2016-05-03 15:10:29 -07001109 })
1110}
1111
Dan Willemsen47450072021-10-19 20:24:49 -07001112func transformDarwinUniversalBinary(ctx android.ModuleContext, outputFile android.WritablePath, inputFiles ...android.Path) {
1113 ctx.Build(pctx, android.BuildParams{
1114 Rule: darwinLipo,
1115 Description: "lipo " + outputFile.Base(),
1116 Output: outputFile,
1117 Inputs: inputFiles,
1118 })
1119}
1120
Chris Parsonsbf4f55f2020-11-23 17:02:44 -05001121// Registers build statement to zip one or more coverage files.
1122func transformCoverageFilesToZip(ctx android.ModuleContext,
Oliver Nguyenc7434142019-04-24 14:22:25 -07001123 inputs Objects, baseName string) android.OptionalPath {
Dan Willemsen581341d2017-02-09 16:16:31 -08001124
1125 if len(inputs.coverageFiles) > 0 {
Oliver Nguyenc7434142019-04-24 14:22:25 -07001126 outputFile := android.PathForModuleOut(ctx, baseName+".zip")
Dan Willemsen581341d2017-02-09 16:16:31 -08001127
Oliver Nguyenc7434142019-04-24 14:22:25 -07001128 ctx.Build(pctx, android.BuildParams{
1129 Rule: zip,
1130 Description: "zip " + outputFile.Base(),
1131 Inputs: inputs.coverageFiles,
1132 Output: outputFile,
1133 })
Dan Willemsen581341d2017-02-09 16:16:31 -08001134
1135 return android.OptionalPathForPath(outputFile)
1136 }
1137
1138 return android.OptionalPath{}
1139}
1140
Chris Parsonsbf4f55f2020-11-23 17:02:44 -05001141// Rule to repack an archive (.a) file with a subset of object files.
1142func transformArchiveRepack(ctx android.ModuleContext, inputFile android.Path,
Yi Kongc49c3932019-10-15 02:01:19 -07001143 outputFile android.WritablePath, objects []string) {
1144
1145 ctx.Build(pctx, android.BuildParams{
1146 Rule: archiveRepack,
1147 Description: "Repack archive " + outputFile.Base(),
1148 Output: outputFile,
1149 Input: inputFile,
1150 Args: map[string]string{
1151 "objects": strings.Join(objects, " "),
1152 },
1153 })
1154}
1155
Elliott Hughes5f314b12021-05-18 11:56:02 -07001156func mingwCmd(toolchain config.Toolchain, cmd string) string {
Colin Cross3f40fa42015-01-30 17:27:36 -08001157 return filepath.Join(toolchain.GccRoot(), "bin", toolchain.GccTriple()+"-"+cmd)
1158}