blob: 72c2fa555bf3c62de0759986652f6dda06af8695 [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
Yi Kongc49c3932019-10-15 02:01:19 -0700168 _ = pctx.SourcePathVariable("archiveRepackPath", "build/soong/scripts/archive_repack.sh")
169
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500170 // Rule to repack an archive (.a) file with a subset of object files.
Yi Kongc49c3932019-10-15 02:01:19 -0700171 archiveRepack = pctx.AndroidStaticRule("archiveRepack",
172 blueprint.RuleParams{
173 Depfile: "${out}.d",
174 Deps: blueprint.DepsGCC,
175 Command: "CLANG_BIN=${config.ClangBin} $archiveRepackPath -i ${in} -o ${out} -d ${out}.d ${objects}",
176 CommandDeps: []string{"$archiveRepackPath"},
177 },
178 "objects")
179
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500180 // Rule to create an empty file at a given path.
Colin Cross9d45bb72016-08-29 16:14:13 -0700181 emptyFile = pctx.AndroidStaticRule("emptyFile",
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700182 blueprint.RuleParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700183 Command: "rm -f $out && touch $out",
Dan Willemsen9f0b5502016-05-13 14:05:09 -0700184 })
185
Nan Zhang43a485c2017-03-27 14:27:58 -0700186 _ = pctx.SourcePathVariable("tocPath", "build/soong/scripts/toc.sh")
Colin Cross26c34ed2016-09-30 17:10:16 -0700187
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500188 // A rule for extracting a table of contents from a shared library (.so).
Colin Cross26c34ed2016-09-30 17:10:16 -0700189 toc = pctx.AndroidStaticRule("toc",
190 blueprint.RuleParams{
191 Depfile: "${out}.d",
192 Deps: blueprint.DepsGCC,
Yi Kong4ad44e72021-04-01 17:45:42 +0800193 Command: "CLANG_BIN=$clangBin $tocPath $format -i ${in} -o ${out} -d ${out}.d",
Colin Cross26c34ed2016-09-30 17:10:16 -0700194 CommandDeps: []string{"$tocPath"},
195 Restat: true,
196 },
Yi Kong4ad44e72021-04-01 17:45:42 +0800197 "clangBin", "format")
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700198
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500199 // Rule for invoking clang-tidy (a clang-based linter).
Colin Cross77cdcfd2021-03-12 11:28:25 -0800200 clangTidy, clangTidyRE = pctx.RemoteStaticRules("clangTidy",
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700201 blueprint.RuleParams{
Chih-Hung Hsieh8e09f262021-09-10 17:25:28 -0700202 Depfile: "${out}.d",
203 Deps: blueprint.DepsGCC,
204 // Pick bash because some machines with old /bin/sh cannot handle arrays.
205 // All $cFlags and $tidyFlags should have single quotes escaped.
206 // Assume no single quotes in other parameters like $in, $out, $ccCmd.
207 Command: "/bin/bash -c 'SRCF=$in; TIDYF=$out; CLANGFLAGS=($cFlags); " +
208 "rm -f $$TIDYF $${TIDYF}.d && " +
209 "${config.CcWrapper}$ccCmd \"$${CLANGFLAGS[@]}\" -E -o /dev/null $$SRCF " +
210 "-MQ $$TIDYF -MD -MF $${TIDYF}.d && " +
211 "$tidyVars $reTemplate${config.ClangBin}/clang-tidy $tidyFlags $$SRCF " +
212 "-- \"$${CLANGFLAGS[@]}\" && touch $$TIDYF'",
213 CommandDeps: []string{"${config.ClangBin}/clang-tidy", "$ccCmd"},
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700214 },
Kousik Kumar4e30bba2020-06-18 09:17:51 -0700215 &remoteexec.REParams{
Chih-Hung Hsieh95dfbce2021-09-09 19:12:05 -0700216 Labels: map[string]string{"type": "lint", "tool": "clang-tidy", "lang": "cpp"},
217 ExecStrategy: "${config.REClangTidyExecStrategy}",
218 Inputs: []string{"$in"},
219 EnvironmentVariables: []string{"TIDY_TIMEOUT"},
Chih-Hung Hsiehfde8e6d2021-08-30 12:49:39 -0700220 // Although clang-tidy has an option to "fix" source files, that feature is hardly useable
221 // under parallel compilation and RBE. So we assume no OutputFiles here.
222 // The clang-tidy fix option is best run locally in single thread.
223 // Copying source file back to local caused two problems:
224 // (1) New timestamps trigger clang and clang-tidy compilations again.
225 // (2) Changing source files caused concurrent clang or clang-tidy jobs to crash.
226 Platform: map[string]string{remoteexec.PoolKey: "${config.REClangTidyPool}"},
Chih-Hung Hsieh8e09f262021-09-10 17:25:28 -0700227 }, []string{"ccCmd", "cFlags", "tidyFlags", "tidyVars"}, []string{})
Colin Cross91e90042016-12-02 17:13:24 -0800228
Nan Zhang43a485c2017-03-27 14:27:58 -0700229 _ = pctx.SourcePathVariable("yasmCmd", "prebuilts/misc/${config.HostPrebuiltTag}/yasm/yasm")
Colin Cross91e90042016-12-02 17:13:24 -0800230
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500231 // Rule for invoking yasm to compile .asm assembly files.
Colin Cross91e90042016-12-02 17:13:24 -0800232 yasm = pctx.AndroidStaticRule("yasm",
233 blueprint.RuleParams{
Dan Willemsen1d3e5452017-08-22 20:53:45 -0700234 Command: "$yasmCmd $asFlags -o $out $in && $yasmCmd $asFlags -M $in >$out.d",
Colin Cross91e90042016-12-02 17:13:24 -0800235 CommandDeps: []string{"$yasmCmd"},
Dan Willemsen1d3e5452017-08-22 20:53:45 -0700236 Depfile: "$out.d",
237 Deps: blueprint.DepsGCC,
Colin Cross91e90042016-12-02 17:13:24 -0800238 },
239 "asFlags")
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800240
Jayant Chowdharya4c6df52018-02-20 12:36:51 -0800241 _ = pctx.SourcePathVariable("sAbiDumper", "prebuilts/clang-tools/${config.HostPrebuiltTag}/bin/header-abi-dumper")
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800242
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700243 // -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 -0800244 sAbiDump, sAbiDumpRE = pctx.RemoteStaticRules("sAbiDump",
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800245 blueprint.RuleParams{
Hsin-Yi Chen79781a92021-07-09 15:59:03 +0800246 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 -0800247 CommandDeps: []string{"$sAbiDumper"},
Ramy Medhat31ec9422020-04-17 15:03:58 -0400248 }, &remoteexec.REParams{
249 Labels: map[string]string{"type": "abi-dump", "tool": "header-abi-dumper"},
250 ExecStrategy: "${config.REAbiDumperExecStrategy}",
Ulf Adams18931762020-11-25 22:54:35 +0100251 Inputs: []string{"$sAbiLinkerLibs"},
Ramy Medhat31ec9422020-04-17 15:03:58 -0400252 Platform: map[string]string{
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500253 remoteexec.PoolKey: "${config.RECXXPool}",
Ramy Medhat31ec9422020-04-17 15:03:58 -0400254 },
255 }, []string{"cFlags", "exportDirs"}, nil)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800256
Jayant Chowdharya4c6df52018-02-20 12:36:51 -0800257 _ = pctx.SourcePathVariable("sAbiLinker", "prebuilts/clang-tools/${config.HostPrebuiltTag}/bin/header-abi-linker")
Ramy Medhat808594c2020-05-07 06:56:47 -0400258 _ = pctx.SourcePathVariable("sAbiLinkerLibs", "prebuilts/clang-tools/${config.HostPrebuiltTag}/lib64")
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800259
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500260 // Rule to combine .dump sAbi dump files from multiple source files into a single .ldump
261 // sAbi dump file.
Colin Cross77cdcfd2021-03-12 11:28:25 -0800262 sAbiLink, sAbiLinkRE = pctx.RemoteStaticRules("sAbiLink",
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800263 blueprint.RuleParams{
Hsin-Yi Chen79781a92021-07-09 15:59:03 +0800264 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 -0800265 CommandDeps: []string{"$sAbiLinker"},
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800266 Rspfile: "${out}.rsp",
267 RspfileContent: "${in}",
Ramy Medhat808594c2020-05-07 06:56:47 -0400268 }, &remoteexec.REParams{
269 Labels: map[string]string{"type": "tool", "name": "abi-linker"},
270 ExecStrategy: "${config.REAbiLinkerExecStrategy}",
Ramy Medhat6797edc2020-08-28 14:21:55 -0400271 Inputs: []string{"$sAbiLinkerLibs", "${out}.rsp", "$implicitInputs"},
Colin Crossa4eafdd2021-03-24 14:09:28 -0700272 RSPFiles: []string{"${out}.rsp"},
Ramy Medhat808594c2020-05-07 06:56:47 -0400273 OutputFiles: []string{"$out"},
274 ToolchainInputs: []string{"$sAbiLinker"},
275 Platform: map[string]string{remoteexec.PoolKey: "${config.RECXXPool}"},
Ramy Medhat6797edc2020-08-28 14:21:55 -0400276 }, []string{"symbolFilter", "arch", "exportedHeaderFlags"}, []string{"implicitInputs"})
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800277
Jayant Chowdharya4c6df52018-02-20 12:36:51 -0800278 _ = pctx.SourcePathVariable("sAbiDiffer", "prebuilts/clang-tools/${config.HostPrebuiltTag}/bin/header-abi-diff")
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700279
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500280 // Rule to compare linked sAbi dump files (.ldump).
Colin Cross2e2dbc22019-09-25 13:31:46 -0700281 sAbiDiff = pctx.RuleFunc("sAbiDiff",
Dan Willemsen54daaf02018-03-12 13:24:09 -0700282 func(ctx android.PackageRuleContext) blueprint.RuleParams {
Logan Chien2a65dda2019-10-01 15:58:07 -0700283 commandStr := "($sAbiDiffer ${extraFlags} -lib ${libName} -arch ${arch} -o ${out} -new ${in} -old ${referenceDump})"
Logan Chien6227fed2019-02-18 13:12:21 +0800284 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 +0800285 commandStr += " && (mkdir -p $$DIST_DIR/abidiffs && cp ${out} $$DIST_DIR/abidiffs/)"
Jayant Chowdharyd8b70a32018-02-01 17:23:09 -0800286 commandStr += " && exit 1)"
Jayant Chowdhary219139d2017-11-27 14:52:21 -0800287 return blueprint.RuleParams{
288 Command: commandStr,
289 CommandDeps: []string{"$sAbiDiffer"},
Dan Willemsen54daaf02018-03-12 13:24:09 -0700290 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800291 },
Logan Chien2a65dda2019-10-01 15:58:07 -0700292 "extraFlags", "referenceDump", "libName", "arch", "createReferenceDumpFlags")
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700293
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500294 // Rule to unzip a reference abi dump.
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700295 unzipRefSAbiDump = pctx.AndroidStaticRule("unzipRefSAbiDump",
296 blueprint.RuleParams{
297 Command: "gunzip -c $in > $out",
298 })
Oliver Nguyenc7434142019-04-24 14:22:25 -0700299
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500300 // Rule to zip files.
Oliver Nguyenc7434142019-04-24 14:22:25 -0700301 zip = pctx.AndroidStaticRule("zip",
302 blueprint.RuleParams{
Colin Cross053fca12020-08-19 13:51:47 -0700303 Command: "${SoongZipCmd} -o ${out} -C $$OUT_DIR -r ${out}.rsp",
Oliver Nguyenc7434142019-04-24 14:22:25 -0700304 CommandDeps: []string{"${SoongZipCmd}"},
Colin Cross053fca12020-08-19 13:51:47 -0700305 Rspfile: "${out}.rsp",
Oliver Nguyenc7434142019-04-24 14:22:25 -0700306 RspfileContent: "$in",
307 })
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800308
309 _ = pctx.SourcePathVariable("cxxExtractor",
310 "prebuilts/clang-tools/${config.HostPrebuiltTag}/bin/cxx_extractor")
Sasha Smundak65143642019-09-26 20:14:28 -0700311 _ = pctx.SourcePathVariable("kytheVnames", "build/soong/vnames.json")
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800312 _ = pctx.VariableFunc("kytheCorpus",
313 func(ctx android.PackageVarContext) string { return ctx.Config().XrefCorpusName() })
Sasha Smundak6c2d4f92020-01-09 17:34:23 -0800314 _ = pctx.VariableFunc("kytheCuEncoding",
315 func(ctx android.PackageVarContext) string { return ctx.Config().XrefCuEncoding() })
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500316
317 // Rule to use kythe extractors to generate .kzip files, used to build code cross references.
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800318 kytheExtract = pctx.StaticRule("kythe",
319 blueprint.RuleParams{
Sasha Smundak6c2d4f92020-01-09 17:34:23 -0800320 Command: `rm -f $out && ` +
Sasha Smundaka4ef83b2020-04-21 17:08:35 -0700321 `KYTHE_CORPUS=${kytheCorpus} ` +
322 `KYTHE_OUTPUT_FILE=$out ` +
323 `KYTHE_VNAMES=$kytheVnames ` +
324 `KYTHE_KZIP_ENCODING=${kytheCuEncoding} ` +
325 `KYTHE_CANONICALIZE_VNAME_PATHS=prefer-relative ` +
Sasha Smundak6c2d4f92020-01-09 17:34:23 -0800326 `$cxxExtractor $cFlags $in `,
Sasha Smundak65143642019-09-26 20:14:28 -0700327 CommandDeps: []string{"$cxxExtractor", "$kytheVnames"},
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800328 },
329 "cFlags")
Colin Cross3f40fa42015-01-30 17:27:36 -0800330)
331
Ivan Lozanof3717ee2020-05-20 09:03:20 -0400332func PwdPrefix() string {
333 // Darwin doesn't have /proc
334 if runtime.GOOS != "darwin" {
335 return "PWD=/proc/self/cwd"
336 }
337 return ""
338}
339
Dan Willemsen322a0a62015-11-17 15:19:46 -0800340func init() {
341 // We run gcc/clang with PWD=/proc/self/cwd to remove $TOP from the
342 // debug output. That way two builds in two different directories will
343 // create the same output.
Ivan Lozanof3717ee2020-05-20 09:03:20 -0400344 pctx.StaticVariable("relPwd", PwdPrefix())
Oliver Nguyenc7434142019-04-24 14:22:25 -0700345
346 pctx.HostBinToolVariable("SoongZipCmd", "soong_zip")
Dan Willemsen322a0a62015-11-17 15:19:46 -0800347}
348
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500349// builderFlags contains various types of command line flags (and settings) for use in building
350// build statements related to C++.
Colin Cross3f40fa42015-01-30 17:27:36 -0800351type builderFlags struct {
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500352 // Global flags (which build system or toolchain is responsible for). These are separate from
353 // local flags because they should appear first (so that they may be overridden by local flags).
Colin Cross6d88dba2019-11-06 07:06:58 -0800354 globalCommonFlags string
355 globalAsFlags string
356 globalYasmFlags string
357 globalCFlags string
358 globalToolingCFlags string // A separate set of cFlags for clang LibTooling tools
359 globalToolingCppFlags string // A separate set of cppFlags for clang LibTooling tools
360 globalConlyFlags string
361 globalCppFlags string
362 globalLdFlags string
363
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500364 // Local flags (which individual modules are responsible for). These may override global flags.
Colin Cross6d88dba2019-11-06 07:06:58 -0800365 localCommonFlags string
366 localAsFlags string
367 localYasmFlags string
368 localCFlags string
369 localToolingCFlags string // A separate set of cFlags for clang LibTooling tools
370 localToolingCppFlags string // A separate set of cppFlags for clang LibTooling tools
371 localConlyFlags string
372 localCppFlags string
373 localLdFlags string
374
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500375 libFlags string // Flags to add to the linker directly after specifying libraries to link.
376 extraLibFlags string // Flags to add to the linker last.
377 tidyFlags string // Flags that apply to clang-tidy
378 sAbiFlags string // Flags that apply to header-abi-dumps
379 aidlFlags string // Flags that apply to aidl source files
380 rsFlags string // Flags that apply to renderscript source files
Colin Cross6d88dba2019-11-06 07:06:58 -0800381 toolchain config.Toolchain
Colin Cross665dce92016-04-28 14:50:03 -0700382
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500383 // True if these extra features are enabled.
384 tidy bool
385 gcovCoverage bool
386 sAbiDump bool
387 emitXrefs bool
388
389 assemblerWithCpp bool // True if .s files should be processed with the c preprocessor.
Dan Willemsen98ab3112019-08-27 21:20:40 -0700390
Colin Crossc3199482017-03-30 15:03:04 -0700391 systemIncludeFlags string
392
Colin Cross19878da2019-03-28 14:45:07 -0700393 proto android.ProtoFlags
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500394 protoC bool // If true, compile protos as `.c` files. Otherwise, output as `.cc`.
395 protoOptionsFile bool // If true, output a proto options file.
Dan Willemsen4e0aa232019-04-10 22:59:54 -0700396
397 yacc *YaccProperties
Matthias Maennich22fd4d12020-07-15 10:58:56 +0200398 lex *LexProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800399}
400
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500401// StripFlags represents flags related to stripping. This is separate from builderFlags, as these
402// flags are useful outside of this package (such as for Rust).
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200403type StripFlags struct {
404 Toolchain config.Toolchain
405 StripKeepSymbols bool
406 StripKeepSymbolsList string
407 StripKeepSymbolsAndDebugFrame bool
408 StripKeepMiniDebugInfo bool
409 StripAddGnuDebuglink bool
410 StripUseGnuStrip bool
411}
412
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500413// Objects is a collection of file paths corresponding to outputs for C++ related build statements.
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700414type Objects struct {
Dan Willemsen581341d2017-02-09 16:16:31 -0800415 objFiles android.Paths
Chih-Hung Hsieh3bb934f2021-10-25 11:09:19 -0700416 tidyFiles android.WritablePaths
Dan Willemsen581341d2017-02-09 16:16:31 -0800417 coverageFiles android.Paths
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800418 sAbiDumpFiles android.Paths
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800419 kytheFiles android.Paths
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700420}
421
422func (a Objects) Copy() Objects {
423 return Objects{
Dan Willemsen581341d2017-02-09 16:16:31 -0800424 objFiles: append(android.Paths{}, a.objFiles...),
Chih-Hung Hsieh3bb934f2021-10-25 11:09:19 -0700425 tidyFiles: append(android.WritablePaths{}, a.tidyFiles...),
Dan Willemsen581341d2017-02-09 16:16:31 -0800426 coverageFiles: append(android.Paths{}, a.coverageFiles...),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800427 sAbiDumpFiles: append(android.Paths{}, a.sAbiDumpFiles...),
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800428 kytheFiles: append(android.Paths{}, a.kytheFiles...),
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700429 }
430}
431
432func (a Objects) Append(b Objects) Objects {
433 return Objects{
Dan Willemsen581341d2017-02-09 16:16:31 -0800434 objFiles: append(a.objFiles, b.objFiles...),
435 tidyFiles: append(a.tidyFiles, b.tidyFiles...),
436 coverageFiles: append(a.coverageFiles, b.coverageFiles...),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800437 sAbiDumpFiles: append(a.sAbiDumpFiles, b.sAbiDumpFiles...),
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800438 kytheFiles: append(a.kytheFiles, b.kytheFiles...),
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700439 }
440}
441
Chih-Hung Hsieh8e09f262021-09-10 17:25:28 -0700442func escapeSingleQuotes(s string) string {
443 // Replace single quotes to work when embedded in a single quoted string for bash.
444 // Relying on string concatenation of bash to get A'B from quoted 'A'\''B'.
445 return strings.Replace(s, `'`, `'\''`, -1)
446}
447
Colin Cross3f40fa42015-01-30 17:27:36 -0800448// Generate rules for compiling multiple .c, .cpp, or .S files to individual .o files
Chih-Hung Hsieh769a51c2021-09-17 17:18:39 -0700449func transformSourceToObj(ctx android.ModuleContext, subdir string, srcFiles, noTidySrcs android.Paths,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800450 flags builderFlags, pathDeps android.Paths, cFlagsDeps android.Paths) Objects {
Colin Cross581c1892015-04-07 16:50:10 -0700451
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500452 // Source files are one-to-one with tidy, coverage, or kythe files, if enabled.
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700453 objFiles := make(android.Paths, len(srcFiles))
Chih-Hung Hsieh3bb934f2021-10-25 11:09:19 -0700454 var tidyFiles android.WritablePaths
Chih-Hung Hsieh769a51c2021-09-17 17:18:39 -0700455 noTidySrcsMap := make(map[android.Path]bool)
Chih-Hung Hsieh95dfbce2021-09-09 19:12:05 -0700456 var tidyVars string
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700457 if flags.tidy {
Chih-Hung Hsieh3bb934f2021-10-25 11:09:19 -0700458 tidyFiles = make(android.WritablePaths, 0, len(srcFiles))
Chih-Hung Hsieh769a51c2021-09-17 17:18:39 -0700459 for _, path := range noTidySrcs {
460 noTidySrcsMap[path] = true
461 }
Chih-Hung Hsieh95dfbce2021-09-09 19:12:05 -0700462 tidyTimeout := ctx.Config().Getenv("TIDY_TIMEOUT")
463 if len(tidyTimeout) > 0 {
464 tidyVars += "TIDY_TIMEOUT=" + tidyTimeout
465 }
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700466 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800467 var coverageFiles android.Paths
Oliver Nguyen04526782020-04-21 12:40:27 -0700468 if flags.gcovCoverage {
Dan Willemsen581341d2017-02-09 16:16:31 -0800469 coverageFiles = make(android.Paths, 0, len(srcFiles))
470 }
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800471 var kytheFiles android.Paths
472 if flags.emitXrefs {
473 kytheFiles = make(android.Paths, 0, len(srcFiles))
474 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800475
Colin Cross6d88dba2019-11-06 07:06:58 -0800476 // Produce fully expanded flags for use by C tools, C compiles, C++ tools, C++ compiles, and asm compiles
477 // respectively.
478 toolingCflags := flags.globalCommonFlags + " " +
479 flags.globalToolingCFlags + " " +
480 flags.globalConlyFlags + " " +
481 flags.localCommonFlags + " " +
482 flags.localToolingCFlags + " " +
483 flags.localConlyFlags + " " +
484 flags.systemIncludeFlags
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700485
Colin Cross6d88dba2019-11-06 07:06:58 -0800486 cflags := flags.globalCommonFlags + " " +
487 flags.globalCFlags + " " +
488 flags.globalConlyFlags + " " +
489 flags.localCommonFlags + " " +
490 flags.localCFlags + " " +
491 flags.localConlyFlags + " " +
492 flags.systemIncludeFlags
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700493
Colin Cross6d88dba2019-11-06 07:06:58 -0800494 toolingCppflags := flags.globalCommonFlags + " " +
495 flags.globalToolingCFlags + " " +
496 flags.globalToolingCppFlags + " " +
497 flags.localCommonFlags + " " +
498 flags.localToolingCFlags + " " +
499 flags.localToolingCppFlags + " " +
500 flags.systemIncludeFlags
Colin Crossc3199482017-03-30 15:03:04 -0700501
Colin Cross6d88dba2019-11-06 07:06:58 -0800502 cppflags := flags.globalCommonFlags + " " +
503 flags.globalCFlags + " " +
504 flags.globalCppFlags + " " +
505 flags.localCommonFlags + " " +
506 flags.localCFlags + " " +
507 flags.localCppFlags + " " +
508 flags.systemIncludeFlags
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700509
Colin Cross6d88dba2019-11-06 07:06:58 -0800510 asflags := flags.globalCommonFlags + " " +
511 flags.globalAsFlags + " " +
512 flags.localCommonFlags + " " +
513 flags.localAsFlags + " " +
514 flags.systemIncludeFlags
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700515
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800516 var sAbiDumpFiles android.Paths
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700517 if flags.sAbiDump {
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800518 sAbiDumpFiles = make(android.Paths, 0, len(srcFiles))
519 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800520
Colin Cross0523ba22021-07-14 18:45:05 -0700521 cflags += " ${config.NoOverrideGlobalCflags}"
522 toolingCflags += " ${config.NoOverrideGlobalCflags}"
523 cppflags += " ${config.NoOverrideGlobalCflags}"
524 toolingCppflags += " ${config.NoOverrideGlobalCflags}"
Dan Willemsenbe03f342016-03-03 17:21:04 -0800525
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -0700526 // Multiple source files have build rules usually share the same cFlags or tidyFlags.
527 // Define only one version in this module and share it in multiple build rules.
528 // To simplify the code, the shared variables are all named as $flags<nnn>.
529 numSharedFlags := 0
530 flagsMap := make(map[string]string)
531
532 // Share flags only when there are multiple files or tidy rules.
533 var hasMultipleRules = len(srcFiles) > 1 || flags.tidy
534
535 var shareFlags = func(kind string, flags string) string {
536 if !hasMultipleRules || len(flags) < 60 {
537 // Modules have long names and so do the module variables.
538 // It does not save space by replacing a short name with a long one.
539 return flags
540 }
541 mapKey := kind + flags
542 n, ok := flagsMap[mapKey]
543 if !ok {
544 numSharedFlags += 1
545 n = strconv.Itoa(numSharedFlags)
546 flagsMap[mapKey] = n
547 ctx.Variable(pctx, kind+n, flags)
548 }
549 return "$" + kind + n
550 }
551
Colin Cross3f40fa42015-01-30 17:27:36 -0800552 for i, srcFile := range srcFiles {
Dan Willemsen21ec4902016-11-02 20:43:13 -0700553 objFile := android.ObjPathWithExt(ctx, subdir, srcFile, "o")
Colin Cross3f40fa42015-01-30 17:27:36 -0800554
555 objFiles[i] = objFile
556
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500557 // Register compilation build statements. The actual rule used depends on the source file type.
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700558 switch srcFile.Ext() {
559 case ".asm":
Colin Crossae887032017-10-23 17:16:14 -0700560 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700561 Rule: yasm,
562 Description: "yasm " + srcFile.Rel(),
563 Output: objFile,
564 Input: srcFile,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800565 Implicits: cFlagsDeps,
566 OrderOnly: pathDeps,
Colin Cross91e90042016-12-02 17:13:24 -0800567 Args: map[string]string{
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -0700568 "asFlags": shareFlags("asFlags", flags.globalYasmFlags+" "+flags.localYasmFlags),
Colin Cross91e90042016-12-02 17:13:24 -0800569 },
570 })
571 continue
Pete Bentleyfcf55bf2019-08-16 20:14:32 +0100572 case ".o":
573 objFiles[i] = srcFile
574 continue
Colin Cross91e90042016-12-02 17:13:24 -0800575 }
576
Colin Cross6d88dba2019-11-06 07:06:58 -0800577 var moduleFlags string
578 var moduleToolingFlags string
579
Colin Cross3f40fa42015-01-30 17:27:36 -0800580 var ccCmd string
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700581 tidy := flags.tidy
Oliver Nguyen04526782020-04-21 12:40:27 -0700582 coverage := flags.gcovCoverage
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700583 dump := flags.sAbiDump
Dan Willemsenfcabb1c2019-01-03 23:25:11 -0800584 rule := cc
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800585 emitXref := flags.emitXrefs
Colin Cross3f40fa42015-01-30 17:27:36 -0800586
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700587 switch srcFile.Ext() {
Dan Willemsenfcabb1c2019-01-03 23:25:11 -0800588 case ".s":
Dan Willemsen98ab3112019-08-27 21:20:40 -0700589 if !flags.assemblerWithCpp {
590 rule = ccNoDeps
591 }
Dan Willemsenfcabb1c2019-01-03 23:25:11 -0800592 fallthrough
593 case ".S":
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700594 ccCmd = "clang"
Colin Cross6d88dba2019-11-06 07:06:58 -0800595 moduleFlags = asflags
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700596 tidy = false
Dan Willemsen581341d2017-02-09 16:16:31 -0800597 coverage = false
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800598 dump = false
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800599 emitXref = false
Colin Cross3f40fa42015-01-30 17:27:36 -0800600 case ".c":
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700601 ccCmd = "clang"
Colin Cross6d88dba2019-11-06 07:06:58 -0800602 moduleFlags = cflags
603 moduleToolingFlags = toolingCflags
Colin Crossd34ab7c2019-06-27 14:46:10 -0700604 case ".cpp", ".cc", ".cxx", ".mm":
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700605 ccCmd = "clang++"
Colin Cross6d88dba2019-11-06 07:06:58 -0800606 moduleFlags = cppflags
607 moduleToolingFlags = toolingCppflags
Liz Kammere2861132020-12-03 22:12:41 +0000608 case ".h", ".hpp":
609 ctx.PropertyErrorf("srcs", "Header file %s is not supported, instead use export_include_dirs or local_include_dirs.", srcFile)
610 continue
Colin Cross3f40fa42015-01-30 17:27:36 -0800611 default:
Liz Kammere2861132020-12-03 22:12:41 +0000612 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 -0800613 continue
614 }
615
Colin Cross67a5c132017-05-09 13:45:28 -0700616 ccDesc := ccCmd
617
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700618 ccCmd = "${config.ClangBin}/" + ccCmd
Colin Cross3f40fa42015-01-30 17:27:36 -0800619
Dan Willemsen581341d2017-02-09 16:16:31 -0800620 var implicitOutputs android.WritablePaths
621 if coverage {
622 gcnoFile := android.ObjPathWithExt(ctx, subdir, srcFile, "gcno")
623 implicitOutputs = append(implicitOutputs, gcnoFile)
624 coverageFiles = append(coverageFiles, gcnoFile)
625 }
626
Colin Crossae887032017-10-23 17:16:14 -0700627 ctx.Build(pctx, android.BuildParams{
Dan Willemsenfcabb1c2019-01-03 23:25:11 -0800628 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -0700629 Description: ccDesc + " " + srcFile.Rel(),
Dan Willemsen581341d2017-02-09 16:16:31 -0800630 Output: objFile,
631 ImplicitOutputs: implicitOutputs,
632 Input: srcFile,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800633 Implicits: cFlagsDeps,
634 OrderOnly: pathDeps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800635 Args: map[string]string{
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -0700636 "cFlags": shareFlags("cFlags", moduleFlags),
637 "ccCmd": ccCmd, // short and not shared
Colin Cross3f40fa42015-01-30 17:27:36 -0800638 },
639 })
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700640
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500641 // Register post-process build statements (such as for tidy or kythe).
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800642 if emitXref {
643 kytheFile := android.ObjPathWithExt(ctx, subdir, srcFile, "kzip")
644 ctx.Build(pctx, android.BuildParams{
645 Rule: kytheExtract,
646 Description: "Xref C++ extractor " + srcFile.Rel(),
647 Output: kytheFile,
648 Input: srcFile,
649 Implicits: cFlagsDeps,
650 OrderOnly: pathDeps,
651 Args: map[string]string{
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -0700652 "cFlags": shareFlags("cFlags", moduleFlags),
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800653 },
654 })
655 kytheFiles = append(kytheFiles, kytheFile)
656 }
657
Chih-Hung Hsieh769a51c2021-09-17 17:18:39 -0700658 // Even with tidy, some src file could be skipped by noTidySrcsMap.
659 if tidy && !noTidySrcsMap[srcFile] {
Dan Willemsen21ec4902016-11-02 20:43:13 -0700660 tidyFile := android.ObjPathWithExt(ctx, subdir, srcFile, "tidy")
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700661 tidyFiles = append(tidyFiles, tidyFile)
662
Kousik Kumar4e30bba2020-06-18 09:17:51 -0700663 rule := clangTidy
Ramy Medhat16f23a42020-09-03 01:29:49 -0400664 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_CLANG_TIDY") {
Kousik Kumar4e30bba2020-06-18 09:17:51 -0700665 rule = clangTidyRE
666 }
667
Chih-Hung Hsiehd0f82fe2021-09-05 20:15:38 -0700668 ctx.TidyFile(tidyFile)
Colin Crossae887032017-10-23 17:16:14 -0700669 ctx.Build(pctx, android.BuildParams{
Kousik Kumar4e30bba2020-06-18 09:17:51 -0700670 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -0700671 Description: "clang-tidy " + srcFile.Rel(),
672 Output: tidyFile,
673 Input: srcFile,
Chih-Hung Hsieh8e09f262021-09-10 17:25:28 -0700674 Implicits: cFlagsDeps,
675 OrderOnly: pathDeps,
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700676 Args: map[string]string{
Chih-Hung Hsieh8e09f262021-09-10 17:25:28 -0700677 "ccCmd": ccCmd,
678 "cFlags": shareFlags("cFlags", escapeSingleQuotes(moduleToolingFlags)),
679 "tidyFlags": shareFlags("tidyFlags", escapeSingleQuotes(config.TidyFlagsForSrcFile(srcFile, flags.tidyFlags))),
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -0700680 "tidyVars": tidyVars, // short and not shared
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700681 },
682 })
683 }
684
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800685 if dump {
686 sAbiDumpFile := android.ObjPathWithExt(ctx, subdir, srcFile, "sdump")
687 sAbiDumpFiles = append(sAbiDumpFiles, sAbiDumpFile)
688
Ramy Medhat31ec9422020-04-17 15:03:58 -0400689 dumpRule := sAbiDump
Ramy Medhat16f23a42020-09-03 01:29:49 -0400690 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_ABI_DUMPER") {
Ramy Medhat31ec9422020-04-17 15:03:58 -0400691 dumpRule = sAbiDumpRE
692 }
Colin Crossae887032017-10-23 17:16:14 -0700693 ctx.Build(pctx, android.BuildParams{
Ramy Medhat31ec9422020-04-17 15:03:58 -0400694 Rule: dumpRule,
Colin Cross67a5c132017-05-09 13:45:28 -0700695 Description: "header-abi-dumper " + srcFile.Rel(),
696 Output: sAbiDumpFile,
697 Input: srcFile,
698 Implicit: objFile,
Dan Willemsen6b4419c2019-08-09 12:45:53 -0700699 Implicits: cFlagsDeps,
700 OrderOnly: pathDeps,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800701 Args: map[string]string{
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -0700702 "cFlags": shareFlags("cFlags", moduleToolingFlags),
703 "exportDirs": shareFlags("exportDirs", flags.sAbiFlags),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800704 },
705 })
706 }
707
Colin Cross3f40fa42015-01-30 17:27:36 -0800708 }
709
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700710 return Objects{
Dan Willemsen581341d2017-02-09 16:16:31 -0800711 objFiles: objFiles,
712 tidyFiles: tidyFiles,
713 coverageFiles: coverageFiles,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800714 sAbiDumpFiles: sAbiDumpFiles,
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800715 kytheFiles: kytheFiles,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700716 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800717}
718
719// Generate a rule for compiling multiple .o files to a static library (.a)
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500720func transformObjToStaticLib(ctx android.ModuleContext,
Martin Stjernholm391d94c2020-04-17 17:34:31 +0100721 objFiles android.Paths, wholeStaticLibs android.Paths,
Chih-Hung Hsieh3bb934f2021-10-25 11:09:19 -0700722 flags builderFlags, outputFile android.ModuleOutPath, deps android.Paths, validations android.WritablePaths) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800723
Stephen Hinesf1addeb2018-01-09 23:29:04 -0800724 arCmd := "${config.ClangBin}/llvm-ar"
Martin Stjernholm391d94c2020-04-17 17:34:31 +0100725 arFlags := ""
Stephen Hinesf1addeb2018-01-09 23:29:04 -0800726 if !ctx.Darwin() {
727 arFlags += " -format=gnu"
728 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800729
Martin Stjernholm391d94c2020-04-17 17:34:31 +0100730 if len(wholeStaticLibs) == 0 {
731 ctx.Build(pctx, android.BuildParams{
732 Rule: ar,
733 Description: "static link " + outputFile.Base(),
734 Output: outputFile,
735 Inputs: objFiles,
736 Implicits: deps,
Chih-Hung Hsieh3bb934f2021-10-25 11:09:19 -0700737 Validations: validations.Paths(),
Martin Stjernholm391d94c2020-04-17 17:34:31 +0100738 Args: map[string]string{
739 "arFlags": "crsPD" + arFlags,
740 "arCmd": arCmd,
741 },
742 })
743
744 } else {
745 ctx.Build(pctx, android.BuildParams{
746 Rule: arWithLibs,
747 Description: "static link " + outputFile.Base(),
748 Output: outputFile,
749 Inputs: append(objFiles, wholeStaticLibs...),
750 Implicits: deps,
751 Args: map[string]string{
752 "arCmd": arCmd,
753 "arObjFlags": "crsPD" + arFlags,
754 "arObjs": strings.Join(objFiles.Strings(), " "),
755 "arLibFlags": "cqsL" + arFlags,
756 "arLibs": strings.Join(wholeStaticLibs.Strings(), " "),
757 },
758 })
759 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800760}
761
762// Generate a rule for compiling multiple .o files, plus static libraries, whole static libraries,
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700763// and shared libraries, to a shared library (.so) or dynamic executable
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500764func transformObjToDynamicBinary(ctx android.ModuleContext,
Colin Crossc465efd2021-06-11 18:00:04 -0700765 objFiles, sharedLibs, staticLibs, lateStaticLibs, wholeStaticLibs, deps, crtBegin, crtEnd android.Paths,
766 groupLate bool, flags builderFlags, outputFile android.WritablePath,
767 implicitOutputs android.WritablePaths, validations android.WritablePaths) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800768
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700769 ldCmd := "${config.ClangBin}/clang++"
Colin Cross3f40fa42015-01-30 17:27:36 -0800770
Colin Cross3f40fa42015-01-30 17:27:36 -0800771 var libFlagsList []string
772
Colin Cross16b23492016-01-06 14:41:07 -0800773 if len(flags.libFlags) > 0 {
774 libFlagsList = append(libFlagsList, flags.libFlags)
775 }
776
Colin Cross3f40fa42015-01-30 17:27:36 -0800777 if len(wholeStaticLibs) > 0 {
Dan Willemsen490fd492015-11-24 17:53:15 -0800778 if ctx.Host() && ctx.Darwin() {
Colin Cross635c3b02016-05-18 15:37:25 -0700779 libFlagsList = append(libFlagsList, android.JoinWithPrefix(wholeStaticLibs.Strings(), "-force_load "))
Colin Cross0af4b842015-04-30 16:36:18 -0700780 } else {
781 libFlagsList = append(libFlagsList, "-Wl,--whole-archive ")
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700782 libFlagsList = append(libFlagsList, wholeStaticLibs.Strings()...)
Colin Cross0af4b842015-04-30 16:36:18 -0700783 libFlagsList = append(libFlagsList, "-Wl,--no-whole-archive ")
784 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800785 }
786
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700787 libFlagsList = append(libFlagsList, staticLibs.Strings()...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800788
Stephen Hines10347862016-07-18 15:54:54 -0700789 if groupLate && !ctx.Darwin() && len(lateStaticLibs) > 0 {
Dan Willemsenedc385f2015-07-08 13:02:23 -0700790 libFlagsList = append(libFlagsList, "-Wl,--start-group")
791 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700792 libFlagsList = append(libFlagsList, lateStaticLibs.Strings()...)
Stephen Hines10347862016-07-18 15:54:54 -0700793 if groupLate && !ctx.Darwin() && len(lateStaticLibs) > 0 {
Dan Willemsenedc385f2015-07-08 13:02:23 -0700794 libFlagsList = append(libFlagsList, "-Wl,--end-group")
795 }
796
Colin Cross3f40fa42015-01-30 17:27:36 -0800797 for _, lib := range sharedLibs {
Josh Gao75a50a22019-06-07 17:58:59 -0700798 libFile := lib.String()
799 if ctx.Windows() {
800 libFile = pathtools.ReplaceExtension(libFile, "lib")
801 }
802 libFlagsList = append(libFlagsList, libFile)
Colin Cross3f40fa42015-01-30 17:27:36 -0800803 }
804
Colin Cross3f40fa42015-01-30 17:27:36 -0800805 deps = append(deps, staticLibs...)
Colin Cross3075ad02015-03-17 10:47:08 -0700806 deps = append(deps, lateStaticLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800807 deps = append(deps, wholeStaticLibs...)
Colin Crossc465efd2021-06-11 18:00:04 -0700808 deps = append(deps, crtBegin...)
809 deps = append(deps, crtEnd...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800810
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400811 rule := ld
Kousik Kumar3fb61262020-04-22 13:31:09 -0700812 args := map[string]string{
813 "ldCmd": ldCmd,
Colin Crossc465efd2021-06-11 18:00:04 -0700814 "crtBegin": strings.Join(crtBegin.Strings(), " "),
Kousik Kumar3fb61262020-04-22 13:31:09 -0700815 "libFlags": strings.Join(libFlagsList, " "),
816 "extraLibFlags": flags.extraLibFlags,
817 "ldFlags": flags.globalLdFlags + " " + flags.localLdFlags,
Colin Crossc465efd2021-06-11 18:00:04 -0700818 "crtEnd": strings.Join(crtEnd.Strings(), " "),
Kousik Kumar3fb61262020-04-22 13:31:09 -0700819 }
Ramy Medhat16f23a42020-09-03 01:29:49 -0400820 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_CXX_LINKS") {
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400821 rule = ldRE
Kousik Kumar3fb61262020-04-22 13:31:09 -0700822 args["implicitOutputs"] = strings.Join(implicitOutputs.Strings(), ",")
Ramy Medhat6797edc2020-08-28 14:21:55 -0400823 args["implicitInputs"] = strings.Join(deps.Strings(), ",")
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400824 }
825
Colin Crossae887032017-10-23 17:16:14 -0700826 ctx.Build(pctx, android.BuildParams{
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400827 Rule: rule,
Josh Gao75a50a22019-06-07 17:58:59 -0700828 Description: "link " + outputFile.Base(),
829 Output: outputFile,
830 ImplicitOutputs: implicitOutputs,
831 Inputs: objFiles,
832 Implicits: deps,
Colin Cross58dd6a92021-05-13 17:45:36 -0700833 OrderOnly: sharedLibs,
Colin Crossf04eb992021-06-11 15:22:41 -0700834 Validations: validations.Paths(),
Kousik Kumar3fb61262020-04-22 13:31:09 -0700835 Args: args,
Colin Cross3f40fa42015-01-30 17:27:36 -0800836 })
837}
838
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800839// Generate a rule to combine .dump sAbi dump files from multiple source files
840// into a single .ldump sAbi dump file
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500841func transformDumpToLinkedDump(ctx android.ModuleContext, sAbiDumps android.Paths, soFile android.Path,
Logan Chiene3d7a0d2019-01-17 00:18:02 +0800842 baseName, exportedHeaderFlags string, symbolFile android.OptionalPath,
843 excludedSymbolVersions, excludedSymbolTags []string) android.OptionalPath {
844
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800845 outputFile := android.PathForModuleOut(ctx, baseName+".lsdump")
Logan Chiene3d7a0d2019-01-17 00:18:02 +0800846
847 implicits := android.Paths{soFile}
Jayant Chowdharydf344d52018-01-17 11:11:42 -0800848 symbolFilterStr := "-so " + soFile.String()
Logan Chiene3d7a0d2019-01-17 00:18:02 +0800849
850 if symbolFile.Valid() {
851 implicits = append(implicits, symbolFile.Path())
852 symbolFilterStr += " -v " + symbolFile.String()
853 }
854 for _, ver := range excludedSymbolVersions {
855 symbolFilterStr += " --exclude-symbol-version " + ver
856 }
857 for _, tag := range excludedSymbolTags {
858 symbolFilterStr += " --exclude-symbol-tag " + tag
859 }
Ramy Medhat808594c2020-05-07 06:56:47 -0400860 rule := sAbiLink
861 args := map[string]string{
862 "symbolFilter": symbolFilterStr,
863 "arch": ctx.Arch().ArchType.Name,
864 "exportedHeaderFlags": exportedHeaderFlags,
865 }
Ramy Medhat16f23a42020-09-03 01:29:49 -0400866 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_ABI_LINKER") {
Ramy Medhat808594c2020-05-07 06:56:47 -0400867 rule = sAbiLinkRE
868 rbeImplicits := implicits.Strings()
869 for _, p := range strings.Split(exportedHeaderFlags, " ") {
870 if len(p) > 2 {
871 // Exclude the -I prefix.
872 rbeImplicits = append(rbeImplicits, p[2:])
873 }
874 }
Ramy Medhat6797edc2020-08-28 14:21:55 -0400875 args["implicitInputs"] = strings.Join(rbeImplicits, ",")
Ramy Medhat808594c2020-05-07 06:56:47 -0400876 }
Colin Crossae887032017-10-23 17:16:14 -0700877 ctx.Build(pctx, android.BuildParams{
Ramy Medhat808594c2020-05-07 06:56:47 -0400878 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -0700879 Description: "header-abi-linker " + outputFile.Base(),
880 Output: outputFile,
881 Inputs: sAbiDumps,
Logan Chiene3d7a0d2019-01-17 00:18:02 +0800882 Implicits: implicits,
Ramy Medhat808594c2020-05-07 06:56:47 -0400883 Args: args,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800884 })
885 return android.OptionalPathForPath(outputFile)
886}
887
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500888// unzipRefDump registers a build statement to unzip a reference abi dump.
889func unzipRefDump(ctx android.ModuleContext, zippedRefDump android.Path, baseName string) android.Path {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700890 outputFile := android.PathForModuleOut(ctx, baseName+"_ref.lsdump")
Colin Crossae887032017-10-23 17:16:14 -0700891 ctx.Build(pctx, android.BuildParams{
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700892 Rule: unzipRefSAbiDump,
893 Description: "gunzip" + outputFile.Base(),
894 Output: outputFile,
895 Input: zippedRefDump,
896 })
897 return outputFile
898}
899
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500900// sourceAbiDiff registers a build statement to compare linked sAbi dump files (.ldump).
901func sourceAbiDiff(ctx android.ModuleContext, inputDump android.Path, referenceDump android.Path,
Logan Chien2a65dda2019-10-01 15:58:07 -0700902 baseName, exportedHeaderFlags string, checkAllApis, isLlndk, isNdk, isVndkExt bool) android.OptionalPath {
Logan Chienf3511742017-10-31 18:04:35 +0800903
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800904 outputFile := android.PathForModuleOut(ctx, baseName+".abidiff")
Jayant Chowdharyc7434e22018-05-31 15:42:26 -0700905 libName := strings.TrimSuffix(baseName, filepath.Ext(baseName))
Logan Chien6227fed2019-02-18 13:12:21 +0800906 createReferenceDumpFlags := ""
907
Logan Chien2a65dda2019-10-01 15:58:07 -0700908 var extraFlags []string
909 if checkAllApis {
910 extraFlags = append(extraFlags, "-check-all-apis")
911 } else {
912 extraFlags = append(extraFlags,
913 "-allow-unreferenced-changes",
914 "-allow-unreferenced-elf-symbol-changes")
Jayant Chowdharye4499502018-01-17 13:13:33 -0800915 }
Logan Chien2a65dda2019-10-01 15:58:07 -0700916
917 if exportedHeaderFlags == "" {
918 extraFlags = append(extraFlags, "-advice-only")
919 }
920
Logan Chien62f1f942019-02-18 15:40:42 +0800921 if isLlndk || isNdk {
Logan Chien6227fed2019-02-18 13:12:21 +0800922 createReferenceDumpFlags = "--llndk"
Logan Chien62f1f942019-02-18 15:40:42 +0800923 if isLlndk {
924 // TODO(b/130324828): "-consider-opaque-types-different" should apply to
925 // both LLNDK and NDK shared libs. However, a known issue in header-abi-diff
926 // breaks libaaudio. Remove the if-guard after the issue is fixed.
Logan Chien2a65dda2019-10-01 15:58:07 -0700927 extraFlags = append(extraFlags, "-consider-opaque-types-different")
Logan Chien62f1f942019-02-18 15:40:42 +0800928 }
Jayant Chowdharyc7434e22018-05-31 15:42:26 -0700929 }
Logan Chienf3511742017-10-31 18:04:35 +0800930 if isVndkExt {
Logan Chien2a65dda2019-10-01 15:58:07 -0700931 extraFlags = append(extraFlags, "-allow-extensions")
Logan Chienf3511742017-10-31 18:04:35 +0800932 }
933
Colin Crossae887032017-10-23 17:16:14 -0700934 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700935 Rule: sAbiDiff,
936 Description: "header-abi-diff " + outputFile.Base(),
937 Output: outputFile,
938 Input: inputDump,
939 Implicit: referenceDump,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800940 Args: map[string]string{
Logan Chien6227fed2019-02-18 13:12:21 +0800941 "referenceDump": referenceDump.String(),
942 "libName": libName,
943 "arch": ctx.Arch().ArchType.Name,
Logan Chien2a65dda2019-10-01 15:58:07 -0700944 "extraFlags": strings.Join(extraFlags, " "),
Logan Chien6227fed2019-02-18 13:12:21 +0800945 "createReferenceDumpFlags": createReferenceDumpFlags,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800946 },
947 })
948 return android.OptionalPathForPath(outputFile)
949}
950
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700951// Generate a rule for extracting a table of contents from a shared library (.so)
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400952func TransformSharedObjectToToc(ctx android.ModuleContext, inputFile android.Path, outputFile android.WritablePath) {
Colin Cross26c34ed2016-09-30 17:10:16 -0700953
Colin Crossb496cfd2018-09-10 16:50:05 -0700954 var format string
Colin Crossb496cfd2018-09-10 16:50:05 -0700955 if ctx.Darwin() {
956 format = "--macho"
Colin Crossb496cfd2018-09-10 16:50:05 -0700957 } else if ctx.Windows() {
958 format = "--pe"
Colin Crossb496cfd2018-09-10 16:50:05 -0700959 } else {
960 format = "--elf"
Colin Crossb496cfd2018-09-10 16:50:05 -0700961 }
Colin Cross26c34ed2016-09-30 17:10:16 -0700962
Colin Crossae887032017-10-23 17:16:14 -0700963 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700964 Rule: toc,
965 Description: "generate toc " + inputFile.Base(),
966 Output: outputFile,
967 Input: inputFile,
Colin Cross26c34ed2016-09-30 17:10:16 -0700968 Args: map[string]string{
Yi Kong4ad44e72021-04-01 17:45:42 +0800969 "clangBin": "${config.ClangBin}",
970 "format": format,
Colin Cross26c34ed2016-09-30 17:10:16 -0700971 },
972 })
973}
974
Colin Cross3f40fa42015-01-30 17:27:36 -0800975// Generate a rule for compiling multiple .o files to a .o using ld partial linking
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500976func transformObjsToObj(ctx android.ModuleContext, objFiles android.Paths,
Dan Willemsen724ab5d2019-09-19 10:50:18 -0700977 flags builderFlags, outputFile android.WritablePath, deps android.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800978
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700979 ldCmd := "${config.ClangBin}/clang++"
Colin Cross3f40fa42015-01-30 17:27:36 -0800980
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400981 rule := partialLd
982 args := map[string]string{
983 "ldCmd": ldCmd,
984 "ldFlags": flags.globalLdFlags + " " + flags.localLdFlags,
985 }
Ramy Medhat16f23a42020-09-03 01:29:49 -0400986 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_CXX_LINKS") {
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400987 rule = partialLdRE
988 args["inCommaList"] = strings.Join(objFiles.Strings(), ",")
Ramy Medhat6797edc2020-08-28 14:21:55 -0400989 args["implicitInputs"] = strings.Join(deps.Strings(), ",")
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400990 }
Colin Crossae887032017-10-23 17:16:14 -0700991 ctx.Build(pctx, android.BuildParams{
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400992 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -0700993 Description: "link " + outputFile.Base(),
994 Output: outputFile,
995 Inputs: objFiles,
Dan Willemsen724ab5d2019-09-19 10:50:18 -0700996 Implicits: deps,
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400997 Args: args,
Colin Cross3f40fa42015-01-30 17:27:36 -0800998 })
999}
1000
Chris Parsonsbf4f55f2020-11-23 17:02:44 -05001001// Generate a rule for running objcopy --prefix-symbols on a binary
1002func transformBinaryPrefixSymbols(ctx android.ModuleContext, prefix string, inputFile android.Path,
Colin Cross635c3b02016-05-18 15:37:25 -07001003 flags builderFlags, outputFile android.WritablePath) {
Colin Crossbfae8852015-03-26 14:44:11 -07001004
Elliott Hughes2e70f792021-04-19 12:58:43 -07001005 objcopyCmd := "${config.ClangBin}/llvm-objcopy"
Colin Crossbfae8852015-03-26 14:44:11 -07001006
Colin Crossae887032017-10-23 17:16:14 -07001007 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -07001008 Rule: prefixSymbols,
1009 Description: "prefix symbols " + outputFile.Base(),
1010 Output: outputFile,
1011 Input: inputFile,
Colin Crossbfae8852015-03-26 14:44:11 -07001012 Args: map[string]string{
1013 "objcopyCmd": objcopyCmd,
1014 "prefix": prefix,
1015 },
1016 })
1017}
1018
Chris Parsonsbf4f55f2020-11-23 17:02:44 -05001019// Registers a build statement to invoke `strip` (to discard symbols and data from object files).
1020func transformStrip(ctx android.ModuleContext, inputFile android.Path,
Thiébaud Weksteend4587452020-08-19 14:53:01 +02001021 outputFile android.WritablePath, flags StripFlags) {
Colin Cross665dce92016-04-28 14:50:03 -07001022
Colin Cross665dce92016-04-28 14:50:03 -07001023 args := ""
Thiébaud Weksteend4587452020-08-19 14:53:01 +02001024 if flags.StripAddGnuDebuglink {
Colin Cross665dce92016-04-28 14:50:03 -07001025 args += " --add-gnu-debuglink"
1026 }
Thiébaud Weksteend4587452020-08-19 14:53:01 +02001027 if flags.StripKeepMiniDebugInfo {
Colin Cross665dce92016-04-28 14:50:03 -07001028 args += " --keep-mini-debug-info"
1029 }
Thiébaud Weksteend4587452020-08-19 14:53:01 +02001030 if flags.StripKeepSymbols {
Colin Cross665dce92016-04-28 14:50:03 -07001031 args += " --keep-symbols"
1032 }
Thiébaud Weksteend4587452020-08-19 14:53:01 +02001033 if flags.StripKeepSymbolsList != "" {
1034 args += " -k" + flags.StripKeepSymbolsList
Yi Kongacee27c2019-03-29 20:05:14 -07001035 }
Thiébaud Weksteend4587452020-08-19 14:53:01 +02001036 if flags.StripKeepSymbolsAndDebugFrame {
Christopher Ferrisb43fe7a2019-05-17 16:39:54 -07001037 args += " --keep-symbols-and-debug-frame"
1038 }
Colin Cross665dce92016-04-28 14:50:03 -07001039
Colin Crossae887032017-10-23 17:16:14 -07001040 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -07001041 Rule: strip,
1042 Description: "strip " + outputFile.Base(),
1043 Output: outputFile,
1044 Input: inputFile,
Colin Cross665dce92016-04-28 14:50:03 -07001045 Args: map[string]string{
Elliott Hughesafcd3392021-05-05 13:54:11 -07001046 "args": args,
Colin Cross665dce92016-04-28 14:50:03 -07001047 },
1048 })
1049}
1050
Chris Parsonsbf4f55f2020-11-23 17:02:44 -05001051// Registers build statement to invoke `strip` on darwin architecture.
1052func transformDarwinStrip(ctx android.ModuleContext, inputFile android.Path,
Colin Cross635c3b02016-05-18 15:37:25 -07001053 outputFile android.WritablePath) {
Colin Crossb8ecdfe2016-05-03 15:10:29 -07001054
Colin Crossae887032017-10-23 17:16:14 -07001055 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -07001056 Rule: darwinStrip,
1057 Description: "strip " + outputFile.Base(),
1058 Output: outputFile,
1059 Input: inputFile,
Colin Crossb8ecdfe2016-05-03 15:10:29 -07001060 })
1061}
1062
Chris Parsonsbf4f55f2020-11-23 17:02:44 -05001063// Registers build statement to zip one or more coverage files.
1064func transformCoverageFilesToZip(ctx android.ModuleContext,
Oliver Nguyenc7434142019-04-24 14:22:25 -07001065 inputs Objects, baseName string) android.OptionalPath {
Dan Willemsen581341d2017-02-09 16:16:31 -08001066
1067 if len(inputs.coverageFiles) > 0 {
Oliver Nguyenc7434142019-04-24 14:22:25 -07001068 outputFile := android.PathForModuleOut(ctx, baseName+".zip")
Dan Willemsen581341d2017-02-09 16:16:31 -08001069
Oliver Nguyenc7434142019-04-24 14:22:25 -07001070 ctx.Build(pctx, android.BuildParams{
1071 Rule: zip,
1072 Description: "zip " + outputFile.Base(),
1073 Inputs: inputs.coverageFiles,
1074 Output: outputFile,
1075 })
Dan Willemsen581341d2017-02-09 16:16:31 -08001076
1077 return android.OptionalPathForPath(outputFile)
1078 }
1079
1080 return android.OptionalPath{}
1081}
1082
Chris Parsonsbf4f55f2020-11-23 17:02:44 -05001083// Rule to repack an archive (.a) file with a subset of object files.
1084func transformArchiveRepack(ctx android.ModuleContext, inputFile android.Path,
Yi Kongc49c3932019-10-15 02:01:19 -07001085 outputFile android.WritablePath, objects []string) {
1086
1087 ctx.Build(pctx, android.BuildParams{
1088 Rule: archiveRepack,
1089 Description: "Repack archive " + outputFile.Base(),
1090 Output: outputFile,
1091 Input: inputFile,
1092 Args: map[string]string{
1093 "objects": strings.Join(objects, " "),
1094 },
1095 })
1096}
1097
Elliott Hughes5f314b12021-05-18 11:56:02 -07001098func mingwCmd(toolchain config.Toolchain, cmd string) string {
Colin Cross3f40fa42015-01-30 17:27:36 -08001099 return filepath.Join(toolchain.GccRoot(), "bin", toolchain.GccTriple()+"-"+cmd)
1100}