blob: 0dfaef49f39c24294608728d4daed8c99ff660e0 [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
220 return envVars
221}
222
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200223func transformSrctoCrate(ctx ModuleContext, main android.Path, deps PathDeps, flags Flags,
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400224 outputFile android.WritablePath, crateType string) buildOutput {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700225
226 var inputs android.Paths
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700227 var implicits, linkImplicits, linkOrderOnly android.Paths
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400228 var output buildOutput
Dan Albert06feee92021-03-19 15:06:02 -0700229 var rustcFlags, linkFlags []string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400230
231 output.outputFile = outputFile
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800232 crateName := ctx.RustModule().CrateName()
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200233 targetTriple := ctx.toolchain().RustTriple()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700234
Dan Albert06feee92021-03-19 15:06:02 -0700235 envVars := rustEnvVars(ctx, deps)
Thiébaud Weksteen71512f32020-11-03 15:17:51 +0100236
Ivan Lozanoffee3342019-08-27 12:03:00 -0700237 inputs = append(inputs, main)
238
239 // Collect rustc flags
Ivan Lozanof1c84332019-09-20 11:00:37 -0700240 rustcFlags = append(rustcFlags, flags.GlobalRustFlags...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700241 rustcFlags = append(rustcFlags, flags.RustFlags...)
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400242 rustcFlags = append(rustcFlags, "--crate-type="+crateType)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800243 if crateName != "" {
244 rustcFlags = append(rustcFlags, "--crate-name="+crateName)
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700245 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700246 if targetTriple != "" {
247 rustcFlags = append(rustcFlags, "--target="+targetTriple)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700248 linkFlags = append(linkFlags, "-target "+targetTriple)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700249 }
Matthew Maurerbb3add12020-06-25 09:34:12 -0700250
251 // Suppress an implicit sysroot
252 rustcFlags = append(rustcFlags, "--sysroot=/dev/null")
253
Chris Wailesd9781fd2021-12-03 17:17:28 -0800254 // Enable incremental compilation if requested by user
255 if ctx.Config().IsEnvTrue("SOONG_RUSTC_INCREMENTAL") {
256 incrementalPath := android.PathForOutput(ctx, "rustc").String()
257
Chris Wailes6d12db42023-02-24 16:58:18 -0800258 rustcFlags = append(rustcFlags, "-Cincremental="+incrementalPath)
259 }
260
261 // Disallow experimental features
262 modulePath := android.PathForModuleSrc(ctx).String()
263 if !(android.IsThirdPartyPath(modulePath) || strings.HasPrefix(modulePath, "prebuilts")) {
Chris Wailes547bfdd2023-05-31 11:53:44 -0700264 rustcFlags = append(rustcFlags, "-Zallow-features=\"\"")
Chris Wailesd9781fd2021-12-03 17:17:28 -0800265 }
266
Ivan Lozanof1c84332019-09-20 11:00:37 -0700267 // Collect linker flags
268 linkFlags = append(linkFlags, flags.GlobalLinkFlags...)
269 linkFlags = append(linkFlags, flags.LinkFlags...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700270
Ivan Lozanoa2268632021-07-22 10:52:06 -0400271 // Check if this module needs to use the bootstrap linker
272 if ctx.RustModule().Bootstrap() && !ctx.RustModule().InRecovery() && !ctx.RustModule().InRamdisk() && !ctx.RustModule().InVendorRamdisk() {
273 dynamicLinker := "-Wl,-dynamic-linker,/system/bin/bootstrap/linker"
274 if ctx.toolchain().Is64Bit() {
275 dynamicLinker += "64"
276 }
277 linkFlags = append(linkFlags, dynamicLinker)
278 }
279
Dan Albert06feee92021-03-19 15:06:02 -0700280 libFlags := makeLibFlags(deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700281
282 // Collect dependencies
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800283 implicits = append(implicits, rustLibsToPaths(deps.RLibs)...)
284 implicits = append(implicits, rustLibsToPaths(deps.DyLibs)...)
285 implicits = append(implicits, rustLibsToPaths(deps.ProcMacros)...)
Yi Kong46c6e592022-01-20 22:55:00 +0800286 implicits = append(implicits, deps.AfdoProfiles...)
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700287 implicits = append(implicits, deps.srcProviderFiles...)
288 implicits = append(implicits, deps.WholeStaticLibs...)
Ivan Lozano43845682020-07-09 21:03:28 -0400289
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700290 linkImplicits = append(linkImplicits, deps.LibDeps...)
291 linkImplicits = append(linkImplicits, deps.CrtBegin...)
292 linkImplicits = append(linkImplicits, deps.CrtEnd...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700293
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700294 linkOrderOnly = append(linkOrderOnly, deps.linkObjects...)
Sam Delmerico51d6d1c2023-03-28 16:54:00 -0400295
Ivan Lozano43845682020-07-09 21:03:28 -0400296 if len(deps.SrcDeps) > 0 {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100297 moduleGenDir := ctx.RustModule().compiler.CargoOutDir()
Ivan Lozano43845682020-07-09 21:03:28 -0400298 var outputs android.WritablePaths
299
300 for _, genSrc := range deps.SrcDeps {
Ivan Lozano10735d92020-07-22 09:14:47 -0400301 if android.SuffixInList(outputs.Strings(), genSubDir+genSrc.Base()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400302 ctx.PropertyErrorf("srcs",
303 "multiple source providers generate the same filename output: "+genSrc.Base())
304 }
Ivan Lozano10735d92020-07-22 09:14:47 -0400305 outputs = append(outputs, android.PathForModuleOut(ctx, genSubDir+genSrc.Base()))
Ivan Lozano43845682020-07-09 21:03:28 -0400306 }
307
308 ctx.Build(pctx, android.BuildParams{
309 Rule: cp,
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100310 Description: "cp " + moduleGenDir.Path().Rel(),
Ivan Lozano43845682020-07-09 21:03:28 -0400311 Outputs: outputs,
312 Inputs: deps.SrcDeps,
313 Args: map[string]string{
314 "outDir": moduleGenDir.String(),
315 },
316 })
317 implicits = append(implicits, outputs.Paths()...)
Ivan Lozano43845682020-07-09 21:03:28 -0400318 }
319
Ivan Lozano6d14ed12022-04-12 10:29:43 -0400320 envVars = append(envVars, "ANDROID_RUST_VERSION="+config.GetRustVersion(ctx))
Thiébaud Weksteen9997ea72021-02-22 10:52:22 +0100321
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400322 if ctx.RustModule().compiler.CargoEnvCompat() {
323 if _, ok := ctx.RustModule().compiler.(*binaryDecorator); ok {
324 envVars = append(envVars, "CARGO_BIN_NAME="+strings.TrimSuffix(outputFile.Base(), outputFile.Ext()))
325 }
326 envVars = append(envVars, "CARGO_CRATE_NAME="+ctx.RustModule().CrateName())
327 pkgVersion := ctx.RustModule().compiler.CargoPkgVersion()
328 if pkgVersion != "" {
329 envVars = append(envVars, "CARGO_PKG_VERSION="+pkgVersion)
330 }
331 }
332
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700333 envVars = append(envVars, "AR=${cc_config.ClangBin}/llvm-ar")
334
Ivan Lozanobae62be2020-07-21 13:28:27 -0400335 if flags.Clippy {
336 clippyFile := android.PathForModuleOut(ctx, outputFile.Base()+".clippy")
337 ctx.Build(pctx, android.BuildParams{
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700338 Rule: clippyDriver,
339 Description: "clippy " + main.Rel(),
340 Output: clippyFile,
341 Inputs: inputs,
342 Implicits: implicits,
Ivan Lozanobae62be2020-07-21 13:28:27 -0400343 Args: map[string]string{
344 "rustcFlags": strings.Join(rustcFlags, " "),
345 "libFlags": strings.Join(libFlags, " "),
346 "clippyFlags": strings.Join(flags.ClippyFlags, " "),
347 "envVars": strings.Join(envVars, " "),
348 },
349 })
350 // Declare the clippy build as an implicit dependency of the original crate.
351 implicits = append(implicits, clippyFile)
352 }
353
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700354 rustcOutputFile := outputFile
355 usesLinker := crateType == "bin" || crateType == "dylib" || crateType == "cdylib" || crateType == "proc-macro"
356 if usesLinker {
357 rustcOutputFile = android.PathForModuleOut(ctx, outputFile.Base()+".rsp")
358 }
359
Ivan Lozanoffee3342019-08-27 12:03:00 -0700360 ctx.Build(pctx, android.BuildParams{
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700361 Rule: rustc,
362 Description: "rustc " + main.Rel(),
363 Output: rustcOutputFile,
364 Inputs: inputs,
365 Implicits: implicits,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700366 Args: map[string]string{
367 "rustcFlags": strings.Join(rustcFlags, " "),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700368 "libFlags": strings.Join(libFlags, " "),
Ivan Lozano43845682020-07-09 21:03:28 -0400369 "envVars": strings.Join(envVars, " "),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700370 },
371 })
372
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700373 if usesLinker {
374 ctx.Build(pctx, android.BuildParams{
375 Rule: rustLink,
376 Description: "rustLink " + main.Rel(),
377 Output: outputFile,
378 Inputs: android.Paths{rustcOutputFile},
379 Implicits: linkImplicits,
380 OrderOnly: linkOrderOnly,
381 Args: map[string]string{
382 "linkFlags": strings.Join(linkFlags, " "),
383 "crtBegin": strings.Join(deps.CrtBegin.Strings(), " "),
384 "crtEnd": strings.Join(deps.CrtEnd.Strings(), " "),
385 },
386 })
387 }
388
Sasha Smundaka76acba2022-04-18 20:12:56 -0700389 if flags.EmitXrefs {
390 kytheFile := android.PathForModuleOut(ctx, outputFile.Base()+".kzip")
391 ctx.Build(pctx, android.BuildParams{
392 Rule: kytheExtract,
393 Description: "Xref Rust extractor " + main.Rel(),
394 Output: kytheFile,
395 Inputs: inputs,
396 Implicits: implicits,
397 Args: map[string]string{
398 "rustcFlags": strings.Join(rustcFlags, " "),
Sasha Smundaka76acba2022-04-18 20:12:56 -0700399 "libFlags": strings.Join(libFlags, " "),
Sasha Smundaka76acba2022-04-18 20:12:56 -0700400 "envVars": strings.Join(envVars, " "),
401 },
402 })
403 output.kytheFile = kytheFile
404 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400405 return output
406}
Dan Albert06feee92021-03-19 15:06:02 -0700407
408func Rustdoc(ctx ModuleContext, main android.Path, deps PathDeps,
409 flags Flags) android.ModuleOutPath {
410
411 rustdocFlags := append([]string{}, flags.RustdocFlags...)
412 rustdocFlags = append(rustdocFlags, "--sysroot=/dev/null")
413
Dan Albertb433bf72021-04-27 17:12:02 -0700414 // Build an index for all our crates. -Z unstable options is required to use
415 // this flag.
416 rustdocFlags = append(rustdocFlags, "-Z", "unstable-options", "--enable-index-page")
417
Dan Albert06feee92021-03-19 15:06:02 -0700418 targetTriple := ctx.toolchain().RustTriple()
419
420 // Collect rustc flags
421 if targetTriple != "" {
422 rustdocFlags = append(rustdocFlags, "--target="+targetTriple)
423 }
424
425 crateName := ctx.RustModule().CrateName()
Dan Albertb433bf72021-04-27 17:12:02 -0700426 rustdocFlags = append(rustdocFlags, "--crate-name "+crateName)
Dan Albert06feee92021-03-19 15:06:02 -0700427
428 rustdocFlags = append(rustdocFlags, makeLibFlags(deps)...)
429 docTimestampFile := android.PathForModuleOut(ctx, "rustdoc.timestamp")
Dan Albertb433bf72021-04-27 17:12:02 -0700430
Chris Wailesb2703ad2021-07-30 13:25:42 -0700431 // Silence warnings about renamed lints for third-party crates
432 modulePath := android.PathForModuleSrc(ctx).String()
433 if android.IsThirdPartyPath(modulePath) {
Chris Wailes7b3eb242023-02-14 16:09:49 -0800434 rustdocFlags = append(rustdocFlags, " -A warnings")
Chris Wailesb2703ad2021-07-30 13:25:42 -0700435 }
Chris Wailes9953a192021-07-28 12:07:16 -0700436
Dan Albertb433bf72021-04-27 17:12:02 -0700437 // Yes, the same out directory is used simultaneously by all rustdoc builds.
438 // This is what cargo does. The docs for individual crates get generated to
439 // a subdirectory named for the crate, and rustdoc synchronizes writes to
440 // shared pieces like the index and search data itself.
441 // https://github.com/rust-lang/rust/blob/master/src/librustdoc/html/render/write_shared.rs#L144-L146
442 docDir := android.PathForOutput(ctx, "rustdoc")
Dan Albert06feee92021-03-19 15:06:02 -0700443
444 ctx.Build(pctx, android.BuildParams{
445 Rule: rustdoc,
446 Description: "rustdoc " + main.Rel(),
447 Output: docTimestampFile,
448 Input: main,
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400449 Implicit: ctx.RustModule().UnstrippedOutputFile(),
Dan Albert06feee92021-03-19 15:06:02 -0700450 Args: map[string]string{
451 "rustdocFlags": strings.Join(rustdocFlags, " "),
452 "outDir": docDir.String(),
453 "envVars": strings.Join(rustEnvVars(ctx, deps), " "),
454 },
455 })
456
457 return docTimestampFile
458}