blob: c31bc88d1399c3c2ed0ac835c8abda7f7b15f560 [file] [log] [blame]
Ivan Lozanoffee3342019-08-27 12:03:00 -07001// Copyright 2019 The Android Open Source Project
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 rust
16
17import (
Ivan Lozano1776a2a2020-11-11 10:59:52 -050018 "path/filepath"
Ivan Lozanoffee3342019-08-27 12:03:00 -070019 "strings"
20
21 "github.com/google/blueprint"
22
23 "android/soong/android"
Thiébaud Weksteen71512f32020-11-03 15:17:51 +010024 "android/soong/rust/config"
Ivan Lozanoffee3342019-08-27 12:03:00 -070025)
26
27var (
28 _ = pctx.SourcePathVariable("rustcCmd", "${config.RustBin}/rustc")
Peter Collingbournee7c71c32023-03-31 20:21:19 -070029 _ = pctx.SourcePathVariable("mkcraterspCmd", "build/soong/scripts/mkcratersp.py")
Ivan Lozanoffee3342019-08-27 12:03:00 -070030 rustc = pctx.AndroidStaticRule("rustc",
31 blueprint.RuleParams{
Ivan Lozano43845682020-07-09 21:03:28 -040032 Command: "$envVars $rustcCmd " +
Peter Collingbournee7c71c32023-03-31 20:21:19 -070033 "-C linker=$mkcraterspCmd " +
Chih-Hung Hsieh29aa9fd2020-08-13 15:46:21 -070034 "--emit link -o $out --emit dep-info=$out.d.raw $in ${libFlags} $rustcFlags" +
35 " && grep \"^$out:\" $out.d.raw > $out.d",
Peter Collingbournee7c71c32023-03-31 20:21:19 -070036 CommandDeps: []string{"$rustcCmd", "$mkcraterspCmd"},
Ivan Lozanob2df9f82019-11-05 12:16:46 -080037 // Rustc deps-info writes out make compatible dep files: https://github.com/rust-lang/rust/issues/7633
Chih-Hung Hsieh29aa9fd2020-08-13 15:46:21 -070038 // Rustc emits unneeded dependency lines for the .d and input .rs files.
39 // Those extra lines cause ninja warning:
40 // "warning: depfile has multiple output paths"
41 // For ninja, we keep/grep only the dependency rule for the rust $out file.
Ivan Lozanob2df9f82019-11-05 12:16:46 -080042 Deps: blueprint.DepsGCC,
43 Depfile: "$out.d",
Ivan Lozanoffee3342019-08-27 12:03:00 -070044 },
Peter Collingbournee7c71c32023-03-31 20:21:19 -070045 "rustcFlags", "libFlags", "envVars")
46 rustLink = pctx.AndroidStaticRule("rustLink",
47 blueprint.RuleParams{
48 Command: "${config.RustLinker} -o $out ${crtBegin} ${config.RustLinkerArgs} @$in ${linkFlags} ${crtEnd}",
49 },
50 "linkFlags", "crtBegin", "crtEnd")
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040051
Dan Albert06feee92021-03-19 15:06:02 -070052 _ = pctx.SourcePathVariable("rustdocCmd", "${config.RustBin}/rustdoc")
53 rustdoc = pctx.AndroidStaticRule("rustdoc",
54 blueprint.RuleParams{
Dan Albertb433bf72021-04-27 17:12:02 -070055 Command: "$envVars $rustdocCmd $rustdocFlags $in -o $outDir && " +
Dan Albert06feee92021-03-19 15:06:02 -070056 "touch $out",
57 CommandDeps: []string{"$rustdocCmd"},
58 },
59 "rustdocFlags", "outDir", "envVars")
60
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020061 _ = pctx.SourcePathVariable("clippyCmd", "${config.RustBin}/clippy-driver")
62 clippyDriver = pctx.AndroidStaticRule("clippy",
63 blueprint.RuleParams{
Ivan Lozanobae62be2020-07-21 13:28:27 -040064 Command: "$envVars $clippyCmd " +
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020065 // Because clippy-driver uses rustc as backend, we need to have some output even during the linting.
66 // Use the metadata output as it has the smallest footprint.
Thiébaud Weksteen94c83252021-04-07 16:00:19 +020067 "--emit metadata -o $out --emit dep-info=$out.d.raw $in ${libFlags} " +
68 "$rustcFlags $clippyFlags" +
69 " && grep \"^$out:\" $out.d.raw > $out.d",
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020070 CommandDeps: []string{"$clippyCmd"},
Thiébaud Weksteen94c83252021-04-07 16:00:19 +020071 Deps: blueprint.DepsGCC,
72 Depfile: "$out.d",
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020073 },
Ivan Lozanobae62be2020-07-21 13:28:27 -040074 "rustcFlags", "libFlags", "clippyFlags", "envVars")
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020075
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040076 zip = pctx.AndroidStaticRule("zip",
77 blueprint.RuleParams{
78 Command: "cat $out.rsp | tr ' ' '\\n' | tr -d \\' | sort -u > ${out}.tmp && ${SoongZipCmd} -o ${out} -C $$OUT_DIR -l ${out}.tmp",
79 CommandDeps: []string{"${SoongZipCmd}"},
80 Rspfile: "$out.rsp",
81 RspfileContent: "$in",
82 })
Ivan Lozano43845682020-07-09 21:03:28 -040083
84 cp = pctx.AndroidStaticRule("cp",
85 blueprint.RuleParams{
86 Command: "cp `cat $outDir.rsp` $outDir",
87 Rspfile: "${outDir}.rsp",
88 RspfileContent: "$in",
89 },
90 "outDir")
Sasha Smundaka76acba2022-04-18 20:12:56 -070091
92 // Cross-referencing:
93 _ = pctx.SourcePathVariable("rustExtractor",
94 "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/rust_extractor")
95 _ = pctx.VariableFunc("kytheCorpus",
96 func(ctx android.PackageVarContext) string { return ctx.Config().XrefCorpusName() })
97 _ = pctx.VariableFunc("kytheCuEncoding",
98 func(ctx android.PackageVarContext) string { return ctx.Config().XrefCuEncoding() })
99 _ = pctx.SourcePathVariable("kytheVnames", "build/soong/vnames.json")
100 kytheExtract = pctx.AndroidStaticRule("kythe",
101 blueprint.RuleParams{
102 Command: `KYTHE_CORPUS=${kytheCorpus} ` +
103 `KYTHE_OUTPUT_FILE=$out ` +
104 `KYTHE_VNAMES=$kytheVnames ` +
105 `KYTHE_KZIP_ENCODING=${kytheCuEncoding} ` +
106 `KYTHE_CANONICALIZE_VNAME_PATHS=prefer-relative ` +
107 `$rustExtractor $envVars ` +
108 `$rustcCmd ` +
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700109 `-C linker=true ` +
Sasha Smundaka76acba2022-04-18 20:12:56 -0700110 `$in ${libFlags} $rustcFlags`,
111 CommandDeps: []string{"$rustExtractor", "$kytheVnames"},
112 Rspfile: "${out}.rsp",
113 RspfileContent: "$in",
114 },
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700115 "rustcFlags", "libFlags", "envVars")
Ivan Lozanoffee3342019-08-27 12:03:00 -0700116)
117
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400118type buildOutput struct {
Joel Galensonfa049382021-01-14 16:03:18 -0800119 outputFile android.Path
Sasha Smundaka76acba2022-04-18 20:12:56 -0700120 kytheFile android.Path
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400121}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700122
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400123func init() {
124 pctx.HostBinToolVariable("SoongZipCmd", "soong_zip")
Ivan Lozanoffee3342019-08-27 12:03:00 -0700125}
126
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200127func TransformSrcToBinary(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Dan Albert06feee92021-03-19 15:06:02 -0700128 outputFile android.WritablePath) buildOutput {
Pirama Arumuga Nainarf77913f2021-10-25 15:37:43 -0700129 flags.GlobalRustFlags = append(flags.GlobalRustFlags, "-C lto=thin")
Ivan Lozano31b095d2019-11-20 10:14:33 -0800130
Dan Albert06feee92021-03-19 15:06:02 -0700131 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "bin")
Ivan Lozanoffee3342019-08-27 12:03:00 -0700132}
133
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200134func TransformSrctoRlib(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Dan Albert06feee92021-03-19 15:06:02 -0700135 outputFile android.WritablePath) buildOutput {
136 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "rlib")
Ivan Lozanoffee3342019-08-27 12:03:00 -0700137}
138
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200139func TransformSrctoDylib(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Dan Albert06feee92021-03-19 15:06:02 -0700140 outputFile android.WritablePath) buildOutput {
Chris Wailes5f788402023-03-02 16:06:01 -0800141 flags.GlobalRustFlags = append(flags.GlobalRustFlags, "-C lto=thin")
142
Dan Albert06feee92021-03-19 15:06:02 -0700143 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "dylib")
Ivan Lozanoffee3342019-08-27 12:03:00 -0700144}
145
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200146func TransformSrctoStatic(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Dan Albert06feee92021-03-19 15:06:02 -0700147 outputFile android.WritablePath) buildOutput {
Pirama Arumuga Nainarf77913f2021-10-25 15:37:43 -0700148 flags.GlobalRustFlags = append(flags.GlobalRustFlags, "-C lto=thin")
Dan Albert06feee92021-03-19 15:06:02 -0700149 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "staticlib")
Ivan Lozano52767be2019-10-18 14:49:46 -0700150}
151
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200152func TransformSrctoShared(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Dan Albert06feee92021-03-19 15:06:02 -0700153 outputFile android.WritablePath) buildOutput {
Pirama Arumuga Nainarf77913f2021-10-25 15:37:43 -0700154 flags.GlobalRustFlags = append(flags.GlobalRustFlags, "-C lto=thin")
Dan Albert06feee92021-03-19 15:06:02 -0700155 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "cdylib")
Ivan Lozano52767be2019-10-18 14:49:46 -0700156}
157
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200158func TransformSrctoProcMacro(ctx ModuleContext, mainSrc android.Path, deps PathDeps,
Dan Albert06feee92021-03-19 15:06:02 -0700159 flags Flags, outputFile android.WritablePath) buildOutput {
160 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "proc-macro")
Ivan Lozanoffee3342019-08-27 12:03:00 -0700161}
162
163func rustLibsToPaths(libs RustLibraries) android.Paths {
164 var paths android.Paths
165 for _, lib := range libs {
166 paths = append(paths, lib.Path)
167 }
168 return paths
169}
170
Dan Albert06feee92021-03-19 15:06:02 -0700171func makeLibFlags(deps PathDeps) []string {
172 var libFlags []string
173
174 // Collect library/crate flags
175 for _, lib := range deps.RLibs {
176 libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String())
177 }
178 for _, lib := range deps.DyLibs {
179 libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String())
180 }
181 for _, proc_macro := range deps.ProcMacros {
182 libFlags = append(libFlags, "--extern "+proc_macro.CrateName+"="+proc_macro.Path.String())
183 }
184
185 for _, path := range deps.linkDirs {
186 libFlags = append(libFlags, "-L "+path)
187 }
188
189 return libFlags
190}
191
192func rustEnvVars(ctx ModuleContext, deps PathDeps) []string {
193 var envVars []string
194
195 // libstd requires a specific environment variable to be set. This is
196 // not officially documented and may be removed in the future. See
197 // https://github.com/rust-lang/rust/blob/master/library/std/src/env.rs#L866.
198 if ctx.RustModule().CrateName() == "std" {
199 envVars = append(envVars, "STD_ENV_ARCH="+config.StdEnvArch[ctx.RustModule().Arch().ArchType])
200 }
201
202 if len(deps.SrcDeps) > 0 {
203 moduleGenDir := ctx.RustModule().compiler.CargoOutDir()
204 // We must calculate an absolute path for OUT_DIR since Rust's include! macro (which normally consumes this)
205 // assumes that paths are relative to the source file.
206 var outDirPrefix string
207 if !filepath.IsAbs(moduleGenDir.String()) {
208 // If OUT_DIR is not absolute, we use $$PWD to generate an absolute path (os.Getwd() returns '/')
209 outDirPrefix = "$$PWD/"
210 } else {
211 // If OUT_DIR is absolute, then moduleGenDir will be an absolute path, so we don't need to set this to anything.
212 outDirPrefix = ""
213 }
214 envVars = append(envVars, "OUT_DIR="+filepath.Join(outDirPrefix, moduleGenDir.String()))
Peter Collingbourne0dcd62e2023-03-31 23:05:16 -0700215 } else {
216 // TODO(pcc): Change this to "OUT_DIR=" after fixing crates to not rely on this value.
217 envVars = append(envVars, "OUT_DIR=out")
Dan Albert06feee92021-03-19 15:06:02 -0700218 }
219
Matthew Maurer34609fa2023-06-26 21:10:13 +0000220 envVars = append(envVars, "ANDROID_RUST_VERSION="+config.GetRustVersion(ctx))
221
222 if ctx.RustModule().compiler.CargoEnvCompat() {
223 if bin, ok := ctx.RustModule().compiler.(*binaryDecorator); ok {
224 envVars = append(envVars, "CARGO_BIN_NAME="+bin.getStem(ctx))
225 }
226 envVars = append(envVars, "CARGO_CRATE_NAME="+ctx.RustModule().CrateName())
227 envVars = append(envVars, "CARGO_PKG_NAME="+ctx.RustModule().CrateName())
228 pkgVersion := ctx.RustModule().compiler.CargoPkgVersion()
229 if pkgVersion != "" {
230 envVars = append(envVars, "CARGO_PKG_VERSION="+pkgVersion)
231 }
232 }
233
234 envVars = append(envVars, "AR=${cc_config.ClangBin}/llvm-ar")
235
Dan Albert06feee92021-03-19 15:06:02 -0700236 return envVars
237}
238
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200239func transformSrctoCrate(ctx ModuleContext, main android.Path, deps PathDeps, flags Flags,
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400240 outputFile android.WritablePath, crateType string) buildOutput {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700241
242 var inputs android.Paths
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700243 var implicits, linkImplicits, linkOrderOnly android.Paths
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400244 var output buildOutput
Dan Albert06feee92021-03-19 15:06:02 -0700245 var rustcFlags, linkFlags []string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400246
247 output.outputFile = outputFile
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800248 crateName := ctx.RustModule().CrateName()
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200249 targetTriple := ctx.toolchain().RustTriple()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700250
Dan Albert06feee92021-03-19 15:06:02 -0700251 envVars := rustEnvVars(ctx, deps)
Thiébaud Weksteen71512f32020-11-03 15:17:51 +0100252
Ivan Lozanoffee3342019-08-27 12:03:00 -0700253 inputs = append(inputs, main)
254
255 // Collect rustc flags
Ivan Lozanof1c84332019-09-20 11:00:37 -0700256 rustcFlags = append(rustcFlags, flags.GlobalRustFlags...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700257 rustcFlags = append(rustcFlags, flags.RustFlags...)
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400258 rustcFlags = append(rustcFlags, "--crate-type="+crateType)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800259 if crateName != "" {
260 rustcFlags = append(rustcFlags, "--crate-name="+crateName)
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700261 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700262 if targetTriple != "" {
263 rustcFlags = append(rustcFlags, "--target="+targetTriple)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700264 linkFlags = append(linkFlags, "-target "+targetTriple)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700265 }
Matthew Maurerbb3add12020-06-25 09:34:12 -0700266
267 // Suppress an implicit sysroot
268 rustcFlags = append(rustcFlags, "--sysroot=/dev/null")
269
Chris Wailesd9781fd2021-12-03 17:17:28 -0800270 // Enable incremental compilation if requested by user
271 if ctx.Config().IsEnvTrue("SOONG_RUSTC_INCREMENTAL") {
272 incrementalPath := android.PathForOutput(ctx, "rustc").String()
273
Chris Wailes6d12db42023-02-24 16:58:18 -0800274 rustcFlags = append(rustcFlags, "-Cincremental="+incrementalPath)
275 }
276
277 // Disallow experimental features
278 modulePath := android.PathForModuleSrc(ctx).String()
279 if !(android.IsThirdPartyPath(modulePath) || strings.HasPrefix(modulePath, "prebuilts")) {
Chris Wailes547bfdd2023-05-31 11:53:44 -0700280 rustcFlags = append(rustcFlags, "-Zallow-features=\"\"")
Chris Wailesd9781fd2021-12-03 17:17:28 -0800281 }
282
Ivan Lozanof1c84332019-09-20 11:00:37 -0700283 // Collect linker flags
284 linkFlags = append(linkFlags, flags.GlobalLinkFlags...)
285 linkFlags = append(linkFlags, flags.LinkFlags...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700286
Ivan Lozanoa2268632021-07-22 10:52:06 -0400287 // Check if this module needs to use the bootstrap linker
288 if ctx.RustModule().Bootstrap() && !ctx.RustModule().InRecovery() && !ctx.RustModule().InRamdisk() && !ctx.RustModule().InVendorRamdisk() {
289 dynamicLinker := "-Wl,-dynamic-linker,/system/bin/bootstrap/linker"
290 if ctx.toolchain().Is64Bit() {
291 dynamicLinker += "64"
292 }
293 linkFlags = append(linkFlags, dynamicLinker)
294 }
295
Dan Albert06feee92021-03-19 15:06:02 -0700296 libFlags := makeLibFlags(deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700297
298 // Collect dependencies
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800299 implicits = append(implicits, rustLibsToPaths(deps.RLibs)...)
300 implicits = append(implicits, rustLibsToPaths(deps.DyLibs)...)
301 implicits = append(implicits, rustLibsToPaths(deps.ProcMacros)...)
Yi Kong46c6e592022-01-20 22:55:00 +0800302 implicits = append(implicits, deps.AfdoProfiles...)
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700303 implicits = append(implicits, deps.srcProviderFiles...)
304 implicits = append(implicits, deps.WholeStaticLibs...)
Ivan Lozano43845682020-07-09 21:03:28 -0400305
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700306 linkImplicits = append(linkImplicits, deps.LibDeps...)
307 linkImplicits = append(linkImplicits, deps.CrtBegin...)
308 linkImplicits = append(linkImplicits, deps.CrtEnd...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700309
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700310 linkOrderOnly = append(linkOrderOnly, deps.linkObjects...)
Sam Delmerico51d6d1c2023-03-28 16:54:00 -0400311
Ivan Lozano43845682020-07-09 21:03:28 -0400312 if len(deps.SrcDeps) > 0 {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100313 moduleGenDir := ctx.RustModule().compiler.CargoOutDir()
Ivan Lozano43845682020-07-09 21:03:28 -0400314 var outputs android.WritablePaths
315
316 for _, genSrc := range deps.SrcDeps {
Ivan Lozano10735d92020-07-22 09:14:47 -0400317 if android.SuffixInList(outputs.Strings(), genSubDir+genSrc.Base()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400318 ctx.PropertyErrorf("srcs",
319 "multiple source providers generate the same filename output: "+genSrc.Base())
320 }
Ivan Lozano10735d92020-07-22 09:14:47 -0400321 outputs = append(outputs, android.PathForModuleOut(ctx, genSubDir+genSrc.Base()))
Ivan Lozano43845682020-07-09 21:03:28 -0400322 }
323
324 ctx.Build(pctx, android.BuildParams{
325 Rule: cp,
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100326 Description: "cp " + moduleGenDir.Path().Rel(),
Ivan Lozano43845682020-07-09 21:03:28 -0400327 Outputs: outputs,
328 Inputs: deps.SrcDeps,
329 Args: map[string]string{
330 "outDir": moduleGenDir.String(),
331 },
332 })
333 implicits = append(implicits, outputs.Paths()...)
Ivan Lozano43845682020-07-09 21:03:28 -0400334 }
335
Ivan Lozanobae62be2020-07-21 13:28:27 -0400336 if flags.Clippy {
337 clippyFile := android.PathForModuleOut(ctx, outputFile.Base()+".clippy")
338 ctx.Build(pctx, android.BuildParams{
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700339 Rule: clippyDriver,
340 Description: "clippy " + main.Rel(),
341 Output: clippyFile,
342 Inputs: inputs,
343 Implicits: implicits,
Ivan Lozanobae62be2020-07-21 13:28:27 -0400344 Args: map[string]string{
345 "rustcFlags": strings.Join(rustcFlags, " "),
346 "libFlags": strings.Join(libFlags, " "),
347 "clippyFlags": strings.Join(flags.ClippyFlags, " "),
348 "envVars": strings.Join(envVars, " "),
349 },
350 })
351 // Declare the clippy build as an implicit dependency of the original crate.
352 implicits = append(implicits, clippyFile)
353 }
354
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700355 rustcOutputFile := outputFile
356 usesLinker := crateType == "bin" || crateType == "dylib" || crateType == "cdylib" || crateType == "proc-macro"
357 if usesLinker {
358 rustcOutputFile = android.PathForModuleOut(ctx, outputFile.Base()+".rsp")
359 }
360
Ivan Lozanoffee3342019-08-27 12:03:00 -0700361 ctx.Build(pctx, android.BuildParams{
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700362 Rule: rustc,
363 Description: "rustc " + main.Rel(),
364 Output: rustcOutputFile,
365 Inputs: inputs,
366 Implicits: implicits,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700367 Args: map[string]string{
368 "rustcFlags": strings.Join(rustcFlags, " "),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700369 "libFlags": strings.Join(libFlags, " "),
Ivan Lozano43845682020-07-09 21:03:28 -0400370 "envVars": strings.Join(envVars, " "),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700371 },
372 })
373
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700374 if usesLinker {
375 ctx.Build(pctx, android.BuildParams{
376 Rule: rustLink,
377 Description: "rustLink " + main.Rel(),
378 Output: outputFile,
379 Inputs: android.Paths{rustcOutputFile},
380 Implicits: linkImplicits,
381 OrderOnly: linkOrderOnly,
382 Args: map[string]string{
383 "linkFlags": strings.Join(linkFlags, " "),
384 "crtBegin": strings.Join(deps.CrtBegin.Strings(), " "),
385 "crtEnd": strings.Join(deps.CrtEnd.Strings(), " "),
386 },
387 })
388 }
389
Sasha Smundaka76acba2022-04-18 20:12:56 -0700390 if flags.EmitXrefs {
391 kytheFile := android.PathForModuleOut(ctx, outputFile.Base()+".kzip")
392 ctx.Build(pctx, android.BuildParams{
393 Rule: kytheExtract,
394 Description: "Xref Rust extractor " + main.Rel(),
395 Output: kytheFile,
396 Inputs: inputs,
397 Implicits: implicits,
398 Args: map[string]string{
399 "rustcFlags": strings.Join(rustcFlags, " "),
Sasha Smundaka76acba2022-04-18 20:12:56 -0700400 "libFlags": strings.Join(libFlags, " "),
Sasha Smundaka76acba2022-04-18 20:12:56 -0700401 "envVars": strings.Join(envVars, " "),
402 },
403 })
404 output.kytheFile = kytheFile
405 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400406 return output
407}
Dan Albert06feee92021-03-19 15:06:02 -0700408
409func Rustdoc(ctx ModuleContext, main android.Path, deps PathDeps,
410 flags Flags) android.ModuleOutPath {
411
412 rustdocFlags := append([]string{}, flags.RustdocFlags...)
413 rustdocFlags = append(rustdocFlags, "--sysroot=/dev/null")
414
Dan Albertb433bf72021-04-27 17:12:02 -0700415 // Build an index for all our crates. -Z unstable options is required to use
416 // this flag.
417 rustdocFlags = append(rustdocFlags, "-Z", "unstable-options", "--enable-index-page")
418
Dan Albert06feee92021-03-19 15:06:02 -0700419 targetTriple := ctx.toolchain().RustTriple()
420
421 // Collect rustc flags
422 if targetTriple != "" {
423 rustdocFlags = append(rustdocFlags, "--target="+targetTriple)
424 }
425
426 crateName := ctx.RustModule().CrateName()
Dan Albertb433bf72021-04-27 17:12:02 -0700427 rustdocFlags = append(rustdocFlags, "--crate-name "+crateName)
Dan Albert06feee92021-03-19 15:06:02 -0700428
429 rustdocFlags = append(rustdocFlags, makeLibFlags(deps)...)
430 docTimestampFile := android.PathForModuleOut(ctx, "rustdoc.timestamp")
Dan Albertb433bf72021-04-27 17:12:02 -0700431
Chris Wailesb2703ad2021-07-30 13:25:42 -0700432 // Silence warnings about renamed lints for third-party crates
433 modulePath := android.PathForModuleSrc(ctx).String()
434 if android.IsThirdPartyPath(modulePath) {
Chris Wailes7b3eb242023-02-14 16:09:49 -0800435 rustdocFlags = append(rustdocFlags, " -A warnings")
Chris Wailesb2703ad2021-07-30 13:25:42 -0700436 }
Chris Wailes9953a192021-07-28 12:07:16 -0700437
Dan Albertb433bf72021-04-27 17:12:02 -0700438 // Yes, the same out directory is used simultaneously by all rustdoc builds.
439 // This is what cargo does. The docs for individual crates get generated to
440 // a subdirectory named for the crate, and rustdoc synchronizes writes to
441 // shared pieces like the index and search data itself.
442 // https://github.com/rust-lang/rust/blob/master/src/librustdoc/html/render/write_shared.rs#L144-L146
443 docDir := android.PathForOutput(ctx, "rustdoc")
Dan Albert06feee92021-03-19 15:06:02 -0700444
445 ctx.Build(pctx, android.BuildParams{
446 Rule: rustdoc,
447 Description: "rustdoc " + main.Rel(),
448 Output: docTimestampFile,
449 Input: main,
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400450 Implicit: ctx.RustModule().UnstrippedOutputFile(),
Dan Albert06feee92021-03-19 15:06:02 -0700451 Args: map[string]string{
452 "rustdocFlags": strings.Join(rustdocFlags, " "),
453 "outDir": docDir.String(),
454 "envVars": strings.Join(rustEnvVars(ctx, deps), " "),
455 },
456 })
457
458 return docTimestampFile
459}