blob: 4b20e2b9a9d461b7823bd8e9504062f0ce6d7cc3 [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")
29 rustc = pctx.AndroidStaticRule("rustc",
30 blueprint.RuleParams{
Ivan Lozano43845682020-07-09 21:03:28 -040031 Command: "$envVars $rustcCmd " +
Ivan Lozanoffee3342019-08-27 12:03:00 -070032 "-C linker=${config.RustLinker} " +
Ivan Lozanof1c84332019-09-20 11:00:37 -070033 "-C link-args=\"${crtBegin} ${config.RustLinkerArgs} ${linkFlags} ${crtEnd}\" " +
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",
Ivan Lozanoffee3342019-08-27 12:03:00 -070036 CommandDeps: []string{"$rustcCmd"},
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 },
Ivan Lozano43845682020-07-09 21:03:28 -040045 "rustcFlags", "linkFlags", "libFlags", "crtBegin", "crtEnd", "envVars")
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040046
Dan Albert06feee92021-03-19 15:06:02 -070047 _ = pctx.SourcePathVariable("rustdocCmd", "${config.RustBin}/rustdoc")
48 rustdoc = pctx.AndroidStaticRule("rustdoc",
49 blueprint.RuleParams{
Dan Albertb433bf72021-04-27 17:12:02 -070050 Command: "$envVars $rustdocCmd $rustdocFlags $in -o $outDir && " +
Dan Albert06feee92021-03-19 15:06:02 -070051 "touch $out",
52 CommandDeps: []string{"$rustdocCmd"},
53 },
54 "rustdocFlags", "outDir", "envVars")
55
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020056 _ = pctx.SourcePathVariable("clippyCmd", "${config.RustBin}/clippy-driver")
57 clippyDriver = pctx.AndroidStaticRule("clippy",
58 blueprint.RuleParams{
Ivan Lozanobae62be2020-07-21 13:28:27 -040059 Command: "$envVars $clippyCmd " +
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020060 // Because clippy-driver uses rustc as backend, we need to have some output even during the linting.
61 // Use the metadata output as it has the smallest footprint.
Thiébaud Weksteen94c83252021-04-07 16:00:19 +020062 "--emit metadata -o $out --emit dep-info=$out.d.raw $in ${libFlags} " +
63 "$rustcFlags $clippyFlags" +
64 " && grep \"^$out:\" $out.d.raw > $out.d",
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020065 CommandDeps: []string{"$clippyCmd"},
Thiébaud Weksteen94c83252021-04-07 16:00:19 +020066 Deps: blueprint.DepsGCC,
67 Depfile: "$out.d",
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020068 },
Ivan Lozanobae62be2020-07-21 13:28:27 -040069 "rustcFlags", "libFlags", "clippyFlags", "envVars")
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020070
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040071 zip = pctx.AndroidStaticRule("zip",
72 blueprint.RuleParams{
73 Command: "cat $out.rsp | tr ' ' '\\n' | tr -d \\' | sort -u > ${out}.tmp && ${SoongZipCmd} -o ${out} -C $$OUT_DIR -l ${out}.tmp",
74 CommandDeps: []string{"${SoongZipCmd}"},
75 Rspfile: "$out.rsp",
76 RspfileContent: "$in",
77 })
Ivan Lozano43845682020-07-09 21:03:28 -040078
79 cp = pctx.AndroidStaticRule("cp",
80 blueprint.RuleParams{
81 Command: "cp `cat $outDir.rsp` $outDir",
82 Rspfile: "${outDir}.rsp",
83 RspfileContent: "$in",
84 },
85 "outDir")
Sasha Smundaka76acba2022-04-18 20:12:56 -070086
87 // Cross-referencing:
88 _ = pctx.SourcePathVariable("rustExtractor",
89 "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/rust_extractor")
90 _ = pctx.VariableFunc("kytheCorpus",
91 func(ctx android.PackageVarContext) string { return ctx.Config().XrefCorpusName() })
92 _ = pctx.VariableFunc("kytheCuEncoding",
93 func(ctx android.PackageVarContext) string { return ctx.Config().XrefCuEncoding() })
94 _ = pctx.SourcePathVariable("kytheVnames", "build/soong/vnames.json")
95 kytheExtract = pctx.AndroidStaticRule("kythe",
96 blueprint.RuleParams{
97 Command: `KYTHE_CORPUS=${kytheCorpus} ` +
98 `KYTHE_OUTPUT_FILE=$out ` +
99 `KYTHE_VNAMES=$kytheVnames ` +
100 `KYTHE_KZIP_ENCODING=${kytheCuEncoding} ` +
101 `KYTHE_CANONICALIZE_VNAME_PATHS=prefer-relative ` +
102 `$rustExtractor $envVars ` +
103 `$rustcCmd ` +
104 `-C linker=${config.RustLinker} ` +
105 `-C link-args="${crtBegin} ${config.RustLinkerArgs} ${linkFlags} ${crtEnd}" ` +
106 `$in ${libFlags} $rustcFlags`,
107 CommandDeps: []string{"$rustExtractor", "$kytheVnames"},
108 Rspfile: "${out}.rsp",
109 RspfileContent: "$in",
110 },
111 "rustcFlags", "linkFlags", "libFlags", "crtBegin", "crtEnd", "envVars")
Ivan Lozanoffee3342019-08-27 12:03:00 -0700112)
113
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400114type buildOutput struct {
Joel Galensonfa049382021-01-14 16:03:18 -0800115 outputFile android.Path
Sasha Smundaka76acba2022-04-18 20:12:56 -0700116 kytheFile android.Path
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400117}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700118
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400119func init() {
120 pctx.HostBinToolVariable("SoongZipCmd", "soong_zip")
Ivan Lozanoffee3342019-08-27 12:03:00 -0700121}
122
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200123func TransformSrcToBinary(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Dan Albert06feee92021-03-19 15:06:02 -0700124 outputFile android.WritablePath) buildOutput {
Pirama Arumuga Nainarf77913f2021-10-25 15:37:43 -0700125 flags.GlobalRustFlags = append(flags.GlobalRustFlags, "-C lto=thin")
Ivan Lozano31b095d2019-11-20 10:14:33 -0800126
Dan Albert06feee92021-03-19 15:06:02 -0700127 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "bin")
Ivan Lozanoffee3342019-08-27 12:03:00 -0700128}
129
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200130func TransformSrctoRlib(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Dan Albert06feee92021-03-19 15:06:02 -0700131 outputFile android.WritablePath) buildOutput {
132 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "rlib")
Ivan Lozanoffee3342019-08-27 12:03:00 -0700133}
134
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200135func TransformSrctoDylib(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Dan Albert06feee92021-03-19 15:06:02 -0700136 outputFile android.WritablePath) buildOutput {
Chris Wailes5f788402023-03-02 16:06:01 -0800137 flags.GlobalRustFlags = append(flags.GlobalRustFlags, "-C lto=thin")
138
Dan Albert06feee92021-03-19 15:06:02 -0700139 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "dylib")
Ivan Lozanoffee3342019-08-27 12:03:00 -0700140}
141
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200142func TransformSrctoStatic(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Dan Albert06feee92021-03-19 15:06:02 -0700143 outputFile android.WritablePath) buildOutput {
Pirama Arumuga Nainarf77913f2021-10-25 15:37:43 -0700144 flags.GlobalRustFlags = append(flags.GlobalRustFlags, "-C lto=thin")
Dan Albert06feee92021-03-19 15:06:02 -0700145 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "staticlib")
Ivan Lozano52767be2019-10-18 14:49:46 -0700146}
147
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200148func TransformSrctoShared(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Dan Albert06feee92021-03-19 15:06:02 -0700149 outputFile android.WritablePath) buildOutput {
Pirama Arumuga Nainarf77913f2021-10-25 15:37:43 -0700150 flags.GlobalRustFlags = append(flags.GlobalRustFlags, "-C lto=thin")
Dan Albert06feee92021-03-19 15:06:02 -0700151 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "cdylib")
Ivan Lozano52767be2019-10-18 14:49:46 -0700152}
153
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200154func TransformSrctoProcMacro(ctx ModuleContext, mainSrc android.Path, deps PathDeps,
Dan Albert06feee92021-03-19 15:06:02 -0700155 flags Flags, outputFile android.WritablePath) buildOutput {
156 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "proc-macro")
Ivan Lozanoffee3342019-08-27 12:03:00 -0700157}
158
159func rustLibsToPaths(libs RustLibraries) android.Paths {
160 var paths android.Paths
161 for _, lib := range libs {
162 paths = append(paths, lib.Path)
163 }
164 return paths
165}
166
Dan Albert06feee92021-03-19 15:06:02 -0700167func makeLibFlags(deps PathDeps) []string {
168 var libFlags []string
169
170 // Collect library/crate flags
171 for _, lib := range deps.RLibs {
172 libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String())
173 }
174 for _, lib := range deps.DyLibs {
175 libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String())
176 }
177 for _, proc_macro := range deps.ProcMacros {
178 libFlags = append(libFlags, "--extern "+proc_macro.CrateName+"="+proc_macro.Path.String())
179 }
180
181 for _, path := range deps.linkDirs {
182 libFlags = append(libFlags, "-L "+path)
183 }
184
185 return libFlags
186}
187
188func rustEnvVars(ctx ModuleContext, deps PathDeps) []string {
189 var envVars []string
190
191 // libstd requires a specific environment variable to be set. This is
192 // not officially documented and may be removed in the future. See
193 // https://github.com/rust-lang/rust/blob/master/library/std/src/env.rs#L866.
194 if ctx.RustModule().CrateName() == "std" {
195 envVars = append(envVars, "STD_ENV_ARCH="+config.StdEnvArch[ctx.RustModule().Arch().ArchType])
196 }
197
198 if len(deps.SrcDeps) > 0 {
199 moduleGenDir := ctx.RustModule().compiler.CargoOutDir()
200 // We must calculate an absolute path for OUT_DIR since Rust's include! macro (which normally consumes this)
201 // assumes that paths are relative to the source file.
202 var outDirPrefix string
203 if !filepath.IsAbs(moduleGenDir.String()) {
204 // If OUT_DIR is not absolute, we use $$PWD to generate an absolute path (os.Getwd() returns '/')
205 outDirPrefix = "$$PWD/"
206 } else {
207 // If OUT_DIR is absolute, then moduleGenDir will be an absolute path, so we don't need to set this to anything.
208 outDirPrefix = ""
209 }
210 envVars = append(envVars, "OUT_DIR="+filepath.Join(outDirPrefix, moduleGenDir.String()))
211 }
212
213 return envVars
214}
215
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200216func transformSrctoCrate(ctx ModuleContext, main android.Path, deps PathDeps, flags Flags,
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400217 outputFile android.WritablePath, crateType string) buildOutput {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700218
219 var inputs android.Paths
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800220 var implicits android.Paths
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400221 var output buildOutput
Dan Albert06feee92021-03-19 15:06:02 -0700222 var rustcFlags, linkFlags []string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400223 var implicitOutputs android.WritablePaths
224
225 output.outputFile = outputFile
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800226 crateName := ctx.RustModule().CrateName()
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200227 targetTriple := ctx.toolchain().RustTriple()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700228
Dan Albert06feee92021-03-19 15:06:02 -0700229 envVars := rustEnvVars(ctx, deps)
Thiébaud Weksteen71512f32020-11-03 15:17:51 +0100230
Ivan Lozanoffee3342019-08-27 12:03:00 -0700231 inputs = append(inputs, main)
232
233 // Collect rustc flags
Ivan Lozanof1c84332019-09-20 11:00:37 -0700234 rustcFlags = append(rustcFlags, flags.GlobalRustFlags...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700235 rustcFlags = append(rustcFlags, flags.RustFlags...)
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400236 rustcFlags = append(rustcFlags, "--crate-type="+crateType)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800237 if crateName != "" {
238 rustcFlags = append(rustcFlags, "--crate-name="+crateName)
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700239 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700240 if targetTriple != "" {
241 rustcFlags = append(rustcFlags, "--target="+targetTriple)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700242 linkFlags = append(linkFlags, "-target "+targetTriple)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700243 }
Matthew Maurerbb3add12020-06-25 09:34:12 -0700244
245 // Suppress an implicit sysroot
246 rustcFlags = append(rustcFlags, "--sysroot=/dev/null")
247
Chris Wailesd9781fd2021-12-03 17:17:28 -0800248 // Enable incremental compilation if requested by user
249 if ctx.Config().IsEnvTrue("SOONG_RUSTC_INCREMENTAL") {
250 incrementalPath := android.PathForOutput(ctx, "rustc").String()
251
Chris Wailes6d12db42023-02-24 16:58:18 -0800252 rustcFlags = append(rustcFlags, "-Cincremental="+incrementalPath)
253 }
254
255 // Disallow experimental features
256 modulePath := android.PathForModuleSrc(ctx).String()
257 if !(android.IsThirdPartyPath(modulePath) || strings.HasPrefix(modulePath, "prebuilts")) {
Chris Wailes7d6e8432023-03-06 12:27:47 -0800258 rustcFlags = append(rustcFlags, "-Zallow-features=\"default_alloc_error_handler,custom_inner_attributes,mixed_integer_ops\"")
Chris Wailesd9781fd2021-12-03 17:17:28 -0800259 }
260
Ivan Lozanof1c84332019-09-20 11:00:37 -0700261 // Collect linker flags
262 linkFlags = append(linkFlags, flags.GlobalLinkFlags...)
263 linkFlags = append(linkFlags, flags.LinkFlags...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700264
Ivan Lozanoa2268632021-07-22 10:52:06 -0400265 // Check if this module needs to use the bootstrap linker
266 if ctx.RustModule().Bootstrap() && !ctx.RustModule().InRecovery() && !ctx.RustModule().InRamdisk() && !ctx.RustModule().InVendorRamdisk() {
267 dynamicLinker := "-Wl,-dynamic-linker,/system/bin/bootstrap/linker"
268 if ctx.toolchain().Is64Bit() {
269 dynamicLinker += "64"
270 }
271 linkFlags = append(linkFlags, dynamicLinker)
272 }
273
Dan Albert06feee92021-03-19 15:06:02 -0700274 libFlags := makeLibFlags(deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700275
276 // Collect dependencies
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800277 implicits = append(implicits, rustLibsToPaths(deps.RLibs)...)
278 implicits = append(implicits, rustLibsToPaths(deps.DyLibs)...)
279 implicits = append(implicits, rustLibsToPaths(deps.ProcMacros)...)
280 implicits = append(implicits, deps.StaticLibs...)
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500281 implicits = append(implicits, deps.SharedLibDeps...)
Ivan Lozano9d74a522020-12-01 09:25:22 -0500282 implicits = append(implicits, deps.srcProviderFiles...)
Yi Kong46c6e592022-01-20 22:55:00 +0800283 implicits = append(implicits, deps.AfdoProfiles...)
Ivan Lozano43845682020-07-09 21:03:28 -0400284
Colin Crossfe605e12022-01-23 20:46:16 -0800285 implicits = append(implicits, deps.CrtBegin...)
286 implicits = append(implicits, deps.CrtEnd...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700287
Ivan Lozano43845682020-07-09 21:03:28 -0400288 if len(deps.SrcDeps) > 0 {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100289 moduleGenDir := ctx.RustModule().compiler.CargoOutDir()
Ivan Lozano43845682020-07-09 21:03:28 -0400290 var outputs android.WritablePaths
291
292 for _, genSrc := range deps.SrcDeps {
Ivan Lozano10735d92020-07-22 09:14:47 -0400293 if android.SuffixInList(outputs.Strings(), genSubDir+genSrc.Base()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400294 ctx.PropertyErrorf("srcs",
295 "multiple source providers generate the same filename output: "+genSrc.Base())
296 }
Ivan Lozano10735d92020-07-22 09:14:47 -0400297 outputs = append(outputs, android.PathForModuleOut(ctx, genSubDir+genSrc.Base()))
Ivan Lozano43845682020-07-09 21:03:28 -0400298 }
299
300 ctx.Build(pctx, android.BuildParams{
301 Rule: cp,
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100302 Description: "cp " + moduleGenDir.Path().Rel(),
Ivan Lozano43845682020-07-09 21:03:28 -0400303 Outputs: outputs,
304 Inputs: deps.SrcDeps,
305 Args: map[string]string{
306 "outDir": moduleGenDir.String(),
307 },
308 })
309 implicits = append(implicits, outputs.Paths()...)
Ivan Lozano43845682020-07-09 21:03:28 -0400310 }
311
Ivan Lozano6d14ed12022-04-12 10:29:43 -0400312 envVars = append(envVars, "ANDROID_RUST_VERSION="+config.GetRustVersion(ctx))
Thiébaud Weksteen9997ea72021-02-22 10:52:22 +0100313
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400314 if ctx.RustModule().compiler.CargoEnvCompat() {
315 if _, ok := ctx.RustModule().compiler.(*binaryDecorator); ok {
316 envVars = append(envVars, "CARGO_BIN_NAME="+strings.TrimSuffix(outputFile.Base(), outputFile.Ext()))
317 }
318 envVars = append(envVars, "CARGO_CRATE_NAME="+ctx.RustModule().CrateName())
319 pkgVersion := ctx.RustModule().compiler.CargoPkgVersion()
320 if pkgVersion != "" {
321 envVars = append(envVars, "CARGO_PKG_VERSION="+pkgVersion)
322 }
323 }
324
Ivan Lozanobae62be2020-07-21 13:28:27 -0400325 if flags.Clippy {
326 clippyFile := android.PathForModuleOut(ctx, outputFile.Base()+".clippy")
327 ctx.Build(pctx, android.BuildParams{
328 Rule: clippyDriver,
329 Description: "clippy " + main.Rel(),
330 Output: clippyFile,
331 ImplicitOutputs: nil,
332 Inputs: inputs,
333 Implicits: implicits,
334 Args: map[string]string{
335 "rustcFlags": strings.Join(rustcFlags, " "),
336 "libFlags": strings.Join(libFlags, " "),
337 "clippyFlags": strings.Join(flags.ClippyFlags, " "),
338 "envVars": strings.Join(envVars, " "),
339 },
340 })
341 // Declare the clippy build as an implicit dependency of the original crate.
342 implicits = append(implicits, clippyFile)
343 }
344
Ivan Lozanoffee3342019-08-27 12:03:00 -0700345 ctx.Build(pctx, android.BuildParams{
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400346 Rule: rustc,
347 Description: "rustc " + main.Rel(),
348 Output: outputFile,
349 ImplicitOutputs: implicitOutputs,
350 Inputs: inputs,
351 Implicits: implicits,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700352 Args: map[string]string{
353 "rustcFlags": strings.Join(rustcFlags, " "),
Ivan Lozanof1c84332019-09-20 11:00:37 -0700354 "linkFlags": strings.Join(linkFlags, " "),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700355 "libFlags": strings.Join(libFlags, " "),
Colin Crossfe605e12022-01-23 20:46:16 -0800356 "crtBegin": strings.Join(deps.CrtBegin.Strings(), " "),
357 "crtEnd": strings.Join(deps.CrtEnd.Strings(), " "),
Ivan Lozano43845682020-07-09 21:03:28 -0400358 "envVars": strings.Join(envVars, " "),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700359 },
360 })
361
Sasha Smundaka76acba2022-04-18 20:12:56 -0700362 if flags.EmitXrefs {
363 kytheFile := android.PathForModuleOut(ctx, outputFile.Base()+".kzip")
364 ctx.Build(pctx, android.BuildParams{
365 Rule: kytheExtract,
366 Description: "Xref Rust extractor " + main.Rel(),
367 Output: kytheFile,
368 Inputs: inputs,
369 Implicits: implicits,
370 Args: map[string]string{
371 "rustcFlags": strings.Join(rustcFlags, " "),
372 "linkFlags": strings.Join(linkFlags, " "),
373 "libFlags": strings.Join(libFlags, " "),
374 "crtBegin": strings.Join(deps.CrtBegin.Strings(), " "),
375 "crtEnd": strings.Join(deps.CrtEnd.Strings(), " "),
376 "envVars": strings.Join(envVars, " "),
377 },
378 })
379 output.kytheFile = kytheFile
380 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400381 return output
382}
Dan Albert06feee92021-03-19 15:06:02 -0700383
384func Rustdoc(ctx ModuleContext, main android.Path, deps PathDeps,
385 flags Flags) android.ModuleOutPath {
386
387 rustdocFlags := append([]string{}, flags.RustdocFlags...)
388 rustdocFlags = append(rustdocFlags, "--sysroot=/dev/null")
389
Dan Albertb433bf72021-04-27 17:12:02 -0700390 // Build an index for all our crates. -Z unstable options is required to use
391 // this flag.
392 rustdocFlags = append(rustdocFlags, "-Z", "unstable-options", "--enable-index-page")
393
Dan Albert06feee92021-03-19 15:06:02 -0700394 targetTriple := ctx.toolchain().RustTriple()
395
396 // Collect rustc flags
397 if targetTriple != "" {
398 rustdocFlags = append(rustdocFlags, "--target="+targetTriple)
399 }
400
401 crateName := ctx.RustModule().CrateName()
Dan Albertb433bf72021-04-27 17:12:02 -0700402 rustdocFlags = append(rustdocFlags, "--crate-name "+crateName)
Dan Albert06feee92021-03-19 15:06:02 -0700403
404 rustdocFlags = append(rustdocFlags, makeLibFlags(deps)...)
405 docTimestampFile := android.PathForModuleOut(ctx, "rustdoc.timestamp")
Dan Albertb433bf72021-04-27 17:12:02 -0700406
Chris Wailesb2703ad2021-07-30 13:25:42 -0700407 // Silence warnings about renamed lints for third-party crates
408 modulePath := android.PathForModuleSrc(ctx).String()
409 if android.IsThirdPartyPath(modulePath) {
Chris Wailes7b3eb242023-02-14 16:09:49 -0800410 rustdocFlags = append(rustdocFlags, " -A warnings")
Chris Wailesb2703ad2021-07-30 13:25:42 -0700411 }
Chris Wailes9953a192021-07-28 12:07:16 -0700412
Dan Albertb433bf72021-04-27 17:12:02 -0700413 // Yes, the same out directory is used simultaneously by all rustdoc builds.
414 // This is what cargo does. The docs for individual crates get generated to
415 // a subdirectory named for the crate, and rustdoc synchronizes writes to
416 // shared pieces like the index and search data itself.
417 // https://github.com/rust-lang/rust/blob/master/src/librustdoc/html/render/write_shared.rs#L144-L146
418 docDir := android.PathForOutput(ctx, "rustdoc")
Dan Albert06feee92021-03-19 15:06:02 -0700419
420 ctx.Build(pctx, android.BuildParams{
421 Rule: rustdoc,
422 Description: "rustdoc " + main.Rel(),
423 Output: docTimestampFile,
424 Input: main,
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400425 Implicit: ctx.RustModule().UnstrippedOutputFile(),
Dan Albert06feee92021-03-19 15:06:02 -0700426 Args: map[string]string{
427 "rustdocFlags": strings.Join(rustdocFlags, " "),
428 "outDir": docDir.String(),
429 "envVars": strings.Join(rustEnvVars(ctx, deps), " "),
430 },
431 })
432
433 return docTimestampFile
434}