blob: e3b4bee10c9dcb00a92d7ad8e619b8b65402ca0c [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 (
Wen-yi Chu41326c12023-09-22 03:58:59 +000028 _ = pctx.SourcePathVariable("rustcCmd", "${config.RustBin}/rustc")
Wen-yi Chu41326c12023-09-22 03:58:59 +000029 rustc = pctx.AndroidStaticRule("rustc",
30 blueprint.RuleParams{
31 Command: "$envVars $rustcCmd " +
Colin Cross004bd3f2023-10-02 11:39:17 -070032 "-C linker=${config.RustLinker} " +
33 "-C link-args=\"${crtBegin} ${earlyLinkFlags} ${linkFlags} ${crtEnd}\" " +
Wen-yi Chu41326c12023-09-22 03:58:59 +000034 "--emit link -o $out --emit dep-info=$out.d.raw $in ${libFlags} $rustcFlags" +
35 " && grep \"^$out:\" $out.d.raw > $out.d",
Colin Cross004bd3f2023-10-02 11:39:17 -070036 CommandDeps: []string{"$rustcCmd"},
Wen-yi Chu41326c12023-09-22 03:58:59 +000037 // Rustc deps-info writes out make compatible dep files: https://github.com/rust-lang/rust/issues/7633
38 // 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.
42 Deps: blueprint.DepsGCC,
43 Depfile: "$out.d",
44 },
Colin Cross004bd3f2023-10-02 11:39:17 -070045 "rustcFlags", "earlyLinkFlags", "linkFlags", "libFlags", "crtBegin", "crtEnd", "envVars")
Wen-yi Chu41326c12023-09-22 03:58:59 +000046
47 _ = pctx.SourcePathVariable("rustdocCmd", "${config.RustBin}/rustdoc")
48 rustdoc = pctx.AndroidStaticRule("rustdoc",
49 blueprint.RuleParams{
50 Command: "$envVars $rustdocCmd $rustdocFlags $in -o $outDir && " +
51 "touch $out",
52 CommandDeps: []string{"$rustdocCmd"},
53 },
54 "rustdocFlags", "outDir", "envVars")
55
56 _ = pctx.SourcePathVariable("clippyCmd", "${config.RustBin}/clippy-driver")
57 clippyDriver = pctx.AndroidStaticRule("clippy",
58 blueprint.RuleParams{
59 Command: "$envVars $clippyCmd " +
60 // 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.
62 "--emit metadata -o $out --emit dep-info=$out.d.raw $in ${libFlags} " +
63 "$rustcFlags $clippyFlags" +
64 " && grep \"^$out:\" $out.d.raw > $out.d",
65 CommandDeps: []string{"$clippyCmd"},
66 Deps: blueprint.DepsGCC,
67 Depfile: "$out.d",
68 },
69 "rustcFlags", "libFlags", "clippyFlags", "envVars")
70
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
Wen-yi Chu41326c12023-09-22 03:58:59 +000079 cp = pctx.AndroidStaticRule("cp",
Ivan Lozano43845682020-07-09 21:03:28 -040080 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() })
Wen-yi Chu41326c12023-09-22 03:58:59 +000094 _ = 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 ` +
Colin Cross004bd3f2023-10-02 11:39:17 -0700104 `-C linker=${config.RustLinker} ` +
105 `-C link-args="${crtBegin} ${linkFlags} ${crtEnd}" ` +
Wen-yi Chu41326c12023-09-22 03:58:59 +0000106 `$in ${libFlags} $rustcFlags`,
107 CommandDeps: []string{"$rustExtractor", "$kytheVnames"},
108 Rspfile: "${out}.rsp",
109 RspfileContent: "$in",
110 },
Colin Cross004bd3f2023-10-02 11:39:17 -0700111 "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
Wen-yi Chu41326c12023-09-22 03:58:59 +0000123func TransformSrcToBinary(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Dan Albert06feee92021-03-19 15:06:02 -0700124 outputFile android.WritablePath) buildOutput {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000125 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "bin")
Ivan Lozanoffee3342019-08-27 12:03:00 -0700126}
127
Wen-yi Chu41326c12023-09-22 03:58:59 +0000128func TransformSrctoRlib(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Dan Albert06feee92021-03-19 15:06:02 -0700129 outputFile android.WritablePath) buildOutput {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000130 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "rlib")
Ivan Lozanoffee3342019-08-27 12:03:00 -0700131}
132
Wen-yi Chu41326c12023-09-22 03:58:59 +0000133func TransformSrctoDylib(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Dan Albert06feee92021-03-19 15:06:02 -0700134 outputFile android.WritablePath) buildOutput {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000135 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "dylib")
Ivan Lozanoffee3342019-08-27 12:03:00 -0700136}
137
Wen-yi Chu41326c12023-09-22 03:58:59 +0000138func TransformSrctoStatic(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Dan Albert06feee92021-03-19 15:06:02 -0700139 outputFile android.WritablePath) buildOutput {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000140 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "staticlib")
Ivan Lozano52767be2019-10-18 14:49:46 -0700141}
142
Wen-yi Chu41326c12023-09-22 03:58:59 +0000143func TransformSrctoShared(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Dan Albert06feee92021-03-19 15:06:02 -0700144 outputFile android.WritablePath) buildOutput {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000145 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "cdylib")
Ivan Lozano52767be2019-10-18 14:49:46 -0700146}
147
Wen-yi Chu41326c12023-09-22 03:58:59 +0000148func TransformSrctoProcMacro(ctx ModuleContext, mainSrc android.Path, deps PathDeps,
Dan Albert06feee92021-03-19 15:06:02 -0700149 flags Flags, outputFile android.WritablePath) buildOutput {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000150 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "proc-macro")
Ivan Lozanoffee3342019-08-27 12:03:00 -0700151}
152
153func rustLibsToPaths(libs RustLibraries) android.Paths {
154 var paths android.Paths
155 for _, lib := range libs {
156 paths = append(paths, lib.Path)
157 }
158 return paths
159}
160
Wen-yi Chu41326c12023-09-22 03:58:59 +0000161func makeLibFlags(deps PathDeps) []string {
Dan Albert06feee92021-03-19 15:06:02 -0700162 var libFlags []string
163
164 // Collect library/crate flags
Wen-yi Chu41326c12023-09-22 03:58:59 +0000165 for _, lib := range deps.RLibs {
166 libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String())
Dan Albert06feee92021-03-19 15:06:02 -0700167 }
Wen-yi Chu41326c12023-09-22 03:58:59 +0000168 for _, lib := range deps.DyLibs {
169 libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String())
Dan Albert06feee92021-03-19 15:06:02 -0700170 }
Wen-yi Chu41326c12023-09-22 03:58:59 +0000171 for _, proc_macro := range deps.ProcMacros {
172 libFlags = append(libFlags, "--extern "+proc_macro.CrateName+"="+proc_macro.Path.String())
Dan Albert06feee92021-03-19 15:06:02 -0700173 }
174
175 for _, path := range deps.linkDirs {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000176 libFlags = append(libFlags, "-L "+path)
Dan Albert06feee92021-03-19 15:06:02 -0700177 }
178
179 return libFlags
180}
181
Wen-yi Chu41326c12023-09-22 03:58:59 +0000182func rustEnvVars(ctx ModuleContext, deps PathDeps) []string {
Dan Albert06feee92021-03-19 15:06:02 -0700183 var envVars []string
184
185 // libstd requires a specific environment variable to be set. This is
186 // not officially documented and may be removed in the future. See
187 // https://github.com/rust-lang/rust/blob/master/library/std/src/env.rs#L866.
188 if ctx.RustModule().CrateName() == "std" {
189 envVars = append(envVars, "STD_ENV_ARCH="+config.StdEnvArch[ctx.RustModule().Arch().ArchType])
190 }
191
192 if len(deps.SrcDeps) > 0 {
Matthew Maurercd416532023-11-20 17:52:01 +0000193 moduleGenDir := ctx.RustModule().compiler.cargoOutDir()
Dan Albert06feee92021-03-19 15:06:02 -0700194 // We must calculate an absolute path for OUT_DIR since Rust's include! macro (which normally consumes this)
195 // assumes that paths are relative to the source file.
Wen-yi Chu41326c12023-09-22 03:58:59 +0000196 var outDirPrefix string
197 if !filepath.IsAbs(moduleGenDir.String()) {
Sam Delmericoa588d152023-06-16 10:28:04 -0400198 // If OUT_DIR is not absolute, we use $$PWD to generate an absolute path (os.Getwd() returns '/')
Wen-yi Chu41326c12023-09-22 03:58:59 +0000199 outDirPrefix = "$$PWD/"
Sam Delmericoa588d152023-06-16 10:28:04 -0400200 } else {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000201 // If OUT_DIR is absolute, then moduleGenDir will be an absolute path, so we don't need to set this to anything.
202 outDirPrefix = ""
Dan Albert06feee92021-03-19 15:06:02 -0700203 }
Wen-yi Chu41326c12023-09-22 03:58:59 +0000204 envVars = append(envVars, "OUT_DIR="+filepath.Join(outDirPrefix, moduleGenDir.String()))
Peter Collingbourne0dcd62e2023-03-31 23:05:16 -0700205 } else {
206 // TODO(pcc): Change this to "OUT_DIR=" after fixing crates to not rely on this value.
207 envVars = append(envVars, "OUT_DIR=out")
Dan Albert06feee92021-03-19 15:06:02 -0700208 }
209
Matthew Maurer34609fa2023-06-26 21:10:13 +0000210 envVars = append(envVars, "ANDROID_RUST_VERSION="+config.GetRustVersion(ctx))
211
Matthew Maurercd416532023-11-20 17:52:01 +0000212 if ctx.RustModule().compiler.cargoEnvCompat() {
Matthew Maurer34609fa2023-06-26 21:10:13 +0000213 if bin, ok := ctx.RustModule().compiler.(*binaryDecorator); ok {
214 envVars = append(envVars, "CARGO_BIN_NAME="+bin.getStem(ctx))
215 }
216 envVars = append(envVars, "CARGO_CRATE_NAME="+ctx.RustModule().CrateName())
217 envVars = append(envVars, "CARGO_PKG_NAME="+ctx.RustModule().CrateName())
Matthew Maurercd416532023-11-20 17:52:01 +0000218 pkgVersion := ctx.RustModule().compiler.cargoPkgVersion()
Matthew Maurer34609fa2023-06-26 21:10:13 +0000219 if pkgVersion != "" {
220 envVars = append(envVars, "CARGO_PKG_VERSION="+pkgVersion)
Ivan Lozanof4455622023-07-28 12:42:20 -0400221
222 // Ensure the version is in the form of "x.y.z" (approximately semver compliant).
223 //
224 // For our purposes, we don't care to enforce that these are integers since they may
225 // include other characters at times (e.g. sometimes the patch version is more than an integer).
226 if strings.Count(pkgVersion, ".") == 2 {
227 var semver_parts = strings.Split(pkgVersion, ".")
228 envVars = append(envVars, "CARGO_PKG_VERSION_MAJOR="+semver_parts[0])
229 envVars = append(envVars, "CARGO_PKG_VERSION_MINOR="+semver_parts[1])
230 envVars = append(envVars, "CARGO_PKG_VERSION_PATCH="+semver_parts[2])
231 }
Matthew Maurer34609fa2023-06-26 21:10:13 +0000232 }
233 }
234
Sam Delmericoc5cf9902023-10-23 21:40:13 +0000235 if ctx.Darwin() {
236 envVars = append(envVars, "ANDROID_RUST_DARWIN=true")
237 }
238
Dan Albert06feee92021-03-19 15:06:02 -0700239 return envVars
240}
241
Wen-yi Chu41326c12023-09-22 03:58:59 +0000242func transformSrctoCrate(ctx ModuleContext, main android.Path, deps PathDeps, flags Flags,
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400243 outputFile android.WritablePath, crateType string) buildOutput {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700244
245 var inputs android.Paths
Colin Cross004bd3f2023-10-02 11:39:17 -0700246 var implicits android.Paths
247 var orderOnly android.Paths
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400248 var output buildOutput
Dan Albert06feee92021-03-19 15:06:02 -0700249 var rustcFlags, linkFlags []string
Wen-yi Chu41326c12023-09-22 03:58:59 +0000250 var earlyLinkFlags string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400251
252 output.outputFile = outputFile
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800253 crateName := ctx.RustModule().CrateName()
ThiƩbaud Weksteen1f7f70f2020-06-24 11:32:48 +0200254 targetTriple := ctx.toolchain().RustTriple()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700255
Wen-yi Chu41326c12023-09-22 03:58:59 +0000256 envVars := rustEnvVars(ctx, deps)
257
Ivan Lozanoffee3342019-08-27 12:03:00 -0700258 inputs = append(inputs, main)
259
Chris Wailese3da5ea2024-02-16 11:34:26 -0800260 if ctx.Config().Eng() {
261 // Per https://doc.rust-lang.org/rustc/codegen-options/index.html#codegen-units
262 // incremental building implies codegen-units=256
263 incrementalPath := android.PathForModuleOut(ctx, "rustc-incremental").String()
264 flags.GlobalRustFlags = append(flags.GlobalRustFlags, "-C incremental="+incrementalPath)
265
266 } else {
267 flags.GlobalRustFlags = append(flags.GlobalRustFlags, "-C codegen-units=1")
268
269 if !(ctx.RustModule().Rlib() || ctx.RustModule().ProcMacro()) {
270 flags.GlobalRustFlags = append(flags.GlobalRustFlags, "-C lto=thin")
271 }
272 }
273
Ivan Lozanoffee3342019-08-27 12:03:00 -0700274 // Collect rustc flags
Ivan Lozanof1c84332019-09-20 11:00:37 -0700275 rustcFlags = append(rustcFlags, flags.GlobalRustFlags...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700276 rustcFlags = append(rustcFlags, flags.RustFlags...)
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400277 rustcFlags = append(rustcFlags, "--crate-type="+crateType)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800278 if crateName != "" {
279 rustcFlags = append(rustcFlags, "--crate-name="+crateName)
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700280 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700281 if targetTriple != "" {
282 rustcFlags = append(rustcFlags, "--target="+targetTriple)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700283 linkFlags = append(linkFlags, "-target "+targetTriple)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700284 }
Matthew Maurerbb3add12020-06-25 09:34:12 -0700285
286 // Suppress an implicit sysroot
287 rustcFlags = append(rustcFlags, "--sysroot=/dev/null")
288
Chris Wailes6d12db42023-02-24 16:58:18 -0800289 // Disallow experimental features
Colin Crossf96b0012023-10-26 14:01:51 -0700290 modulePath := ctx.ModuleDir()
Chris Wailes6d12db42023-02-24 16:58:18 -0800291 if !(android.IsThirdPartyPath(modulePath) || strings.HasPrefix(modulePath, "prebuilts")) {
Chris Wailes547bfdd2023-05-31 11:53:44 -0700292 rustcFlags = append(rustcFlags, "-Zallow-features=\"\"")
Chris Wailesd9781fd2021-12-03 17:17:28 -0800293 }
294
Ivan Lozanof1c84332019-09-20 11:00:37 -0700295 // Collect linker flags
A. Cody Schuffelenf29ca582023-07-13 19:03:39 -0700296 if !ctx.Darwin() {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000297 earlyLinkFlags = "-Wl,--as-needed"
A. Cody Schuffelenf29ca582023-07-13 19:03:39 -0700298 }
299
Wen-yi Chu41326c12023-09-22 03:58:59 +0000300 linkFlags = append(linkFlags, flags.GlobalLinkFlags...)
301 linkFlags = append(linkFlags, flags.LinkFlags...)
302
303 // Check if this module needs to use the bootstrap linker
304 if ctx.RustModule().Bootstrap() && !ctx.RustModule().InRecovery() && !ctx.RustModule().InRamdisk() && !ctx.RustModule().InVendorRamdisk() {
305 dynamicLinker := "-Wl,-dynamic-linker,/system/bin/bootstrap/linker"
306 if ctx.toolchain().Is64Bit() {
307 dynamicLinker += "64"
308 }
309 linkFlags = append(linkFlags, dynamicLinker)
310 }
311
312 libFlags := makeLibFlags(deps)
313
Ivan Lozanoffee3342019-08-27 12:03:00 -0700314 // Collect dependencies
Wen-yi Chu41326c12023-09-22 03:58:59 +0000315 implicits = append(implicits, rustLibsToPaths(deps.RLibs)...)
316 implicits = append(implicits, rustLibsToPaths(deps.DyLibs)...)
317 implicits = append(implicits, rustLibsToPaths(deps.ProcMacros)...)
Colin Cross004bd3f2023-10-02 11:39:17 -0700318 implicits = append(implicits, deps.StaticLibs...)
319 implicits = append(implicits, deps.SharedLibDeps...)
Wen-yi Chu41326c12023-09-22 03:58:59 +0000320 implicits = append(implicits, deps.srcProviderFiles...)
Colin Cross004bd3f2023-10-02 11:39:17 -0700321 implicits = append(implicits, deps.AfdoProfiles...)
Wen-yi Chu41326c12023-09-22 03:58:59 +0000322
Colin Cross004bd3f2023-10-02 11:39:17 -0700323 implicits = append(implicits, deps.CrtBegin...)
324 implicits = append(implicits, deps.CrtEnd...)
Wen-yi Chu41326c12023-09-22 03:58:59 +0000325
Colin Cross004bd3f2023-10-02 11:39:17 -0700326 orderOnly = append(orderOnly, deps.SharedLibs...)
Sam Delmerico51d6d1c2023-03-28 16:54:00 -0400327
Ivan Lozano43845682020-07-09 21:03:28 -0400328 if len(deps.SrcDeps) > 0 {
Matthew Maurercd416532023-11-20 17:52:01 +0000329 moduleGenDir := ctx.RustModule().compiler.cargoOutDir()
Ivan Lozano43845682020-07-09 21:03:28 -0400330 var outputs android.WritablePaths
331
332 for _, genSrc := range deps.SrcDeps {
Ivan Lozano10735d92020-07-22 09:14:47 -0400333 if android.SuffixInList(outputs.Strings(), genSubDir+genSrc.Base()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400334 ctx.PropertyErrorf("srcs",
335 "multiple source providers generate the same filename output: "+genSrc.Base())
336 }
Ivan Lozano10735d92020-07-22 09:14:47 -0400337 outputs = append(outputs, android.PathForModuleOut(ctx, genSubDir+genSrc.Base()))
Ivan Lozano43845682020-07-09 21:03:28 -0400338 }
339
340 ctx.Build(pctx, android.BuildParams{
Wen-yi Chu41326c12023-09-22 03:58:59 +0000341 Rule: cp,
ThiƩbaud Weksteenee6a89b2021-02-25 16:30:57 +0100342 Description: "cp " + moduleGenDir.Path().Rel(),
Ivan Lozano43845682020-07-09 21:03:28 -0400343 Outputs: outputs,
344 Inputs: deps.SrcDeps,
345 Args: map[string]string{
346 "outDir": moduleGenDir.String(),
347 },
348 })
349 implicits = append(implicits, outputs.Paths()...)
Ivan Lozano43845682020-07-09 21:03:28 -0400350 }
351
Ivan Lozanobae62be2020-07-21 13:28:27 -0400352 if flags.Clippy {
353 clippyFile := android.PathForModuleOut(ctx, outputFile.Base()+".clippy")
Wen-yi Chu41326c12023-09-22 03:58:59 +0000354 ctx.Build(pctx, android.BuildParams{
Colin Cross004bd3f2023-10-02 11:39:17 -0700355 Rule: clippyDriver,
356 Description: "clippy " + main.Rel(),
357 Output: clippyFile,
358 ImplicitOutputs: nil,
359 Inputs: inputs,
360 Implicits: implicits,
361 OrderOnly: orderOnly,
Wen-yi Chu41326c12023-09-22 03:58:59 +0000362 Args: map[string]string{
363 "rustcFlags": strings.Join(rustcFlags, " "),
364 "libFlags": strings.Join(libFlags, " "),
365 "clippyFlags": strings.Join(flags.ClippyFlags, " "),
366 "envVars": strings.Join(envVars, " "),
367 },
368 })
Ivan Lozanobae62be2020-07-21 13:28:27 -0400369 // Declare the clippy build as an implicit dependency of the original crate.
370 implicits = append(implicits, clippyFile)
371 }
372
Wen-yi Chu41326c12023-09-22 03:58:59 +0000373 ctx.Build(pctx, android.BuildParams{
Colin Cross004bd3f2023-10-02 11:39:17 -0700374 Rule: rustc,
375 Description: "rustc " + main.Rel(),
376 Output: outputFile,
377 Inputs: inputs,
378 Implicits: implicits,
379 OrderOnly: orderOnly,
Wen-yi Chu41326c12023-09-22 03:58:59 +0000380 Args: map[string]string{
Colin Cross004bd3f2023-10-02 11:39:17 -0700381 "rustcFlags": strings.Join(rustcFlags, " "),
382 "earlyLinkFlags": earlyLinkFlags,
383 "linkFlags": strings.Join(linkFlags, " "),
384 "libFlags": strings.Join(libFlags, " "),
385 "crtBegin": strings.Join(deps.CrtBegin.Strings(), " "),
386 "crtEnd": strings.Join(deps.CrtEnd.Strings(), " "),
387 "envVars": strings.Join(envVars, " "),
Wen-yi Chu41326c12023-09-22 03:58:59 +0000388 },
389 })
Ivan Lozanoffee3342019-08-27 12:03:00 -0700390
Sasha Smundaka76acba2022-04-18 20:12:56 -0700391 if flags.EmitXrefs {
392 kytheFile := android.PathForModuleOut(ctx, outputFile.Base()+".kzip")
Wen-yi Chu41326c12023-09-22 03:58:59 +0000393 ctx.Build(pctx, android.BuildParams{
394 Rule: kytheExtract,
395 Description: "Xref Rust extractor " + main.Rel(),
396 Output: kytheFile,
397 Inputs: inputs,
398 Implicits: implicits,
Colin Cross004bd3f2023-10-02 11:39:17 -0700399 OrderOnly: orderOnly,
Wen-yi Chu41326c12023-09-22 03:58:59 +0000400 Args: map[string]string{
401 "rustcFlags": strings.Join(rustcFlags, " "),
Colin Cross004bd3f2023-10-02 11:39:17 -0700402 "linkFlags": strings.Join(linkFlags, " "),
Wen-yi Chu41326c12023-09-22 03:58:59 +0000403 "libFlags": strings.Join(libFlags, " "),
Colin Cross004bd3f2023-10-02 11:39:17 -0700404 "crtBegin": strings.Join(deps.CrtBegin.Strings(), " "),
405 "crtEnd": strings.Join(deps.CrtEnd.Strings(), " "),
Wen-yi Chu41326c12023-09-22 03:58:59 +0000406 "envVars": strings.Join(envVars, " "),
407 },
408 })
Sasha Smundaka76acba2022-04-18 20:12:56 -0700409 output.kytheFile = kytheFile
410 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400411 return output
412}
Dan Albert06feee92021-03-19 15:06:02 -0700413
Wen-yi Chu41326c12023-09-22 03:58:59 +0000414func Rustdoc(ctx ModuleContext, main android.Path, deps PathDeps,
415 flags Flags) android.ModuleOutPath {
Dan Albert06feee92021-03-19 15:06:02 -0700416
417 rustdocFlags := append([]string{}, flags.RustdocFlags...)
418 rustdocFlags = append(rustdocFlags, "--sysroot=/dev/null")
419
Dan Albertb433bf72021-04-27 17:12:02 -0700420 // Build an index for all our crates. -Z unstable options is required to use
421 // this flag.
422 rustdocFlags = append(rustdocFlags, "-Z", "unstable-options", "--enable-index-page")
423
Dan Albert06feee92021-03-19 15:06:02 -0700424 targetTriple := ctx.toolchain().RustTriple()
425
426 // Collect rustc flags
427 if targetTriple != "" {
428 rustdocFlags = append(rustdocFlags, "--target="+targetTriple)
429 }
430
431 crateName := ctx.RustModule().CrateName()
Dan Albertb433bf72021-04-27 17:12:02 -0700432 rustdocFlags = append(rustdocFlags, "--crate-name "+crateName)
Dan Albert06feee92021-03-19 15:06:02 -0700433
Wen-yi Chu41326c12023-09-22 03:58:59 +0000434 rustdocFlags = append(rustdocFlags, makeLibFlags(deps)...)
Dan Albert06feee92021-03-19 15:06:02 -0700435 docTimestampFile := android.PathForModuleOut(ctx, "rustdoc.timestamp")
Dan Albertb433bf72021-04-27 17:12:02 -0700436
Chris Wailesb2703ad2021-07-30 13:25:42 -0700437 // Silence warnings about renamed lints for third-party crates
Colin Crossf96b0012023-10-26 14:01:51 -0700438 modulePath := ctx.ModuleDir()
Chris Wailesb2703ad2021-07-30 13:25:42 -0700439 if android.IsThirdPartyPath(modulePath) {
Chris Wailes7b3eb242023-02-14 16:09:49 -0800440 rustdocFlags = append(rustdocFlags, " -A warnings")
Chris Wailesb2703ad2021-07-30 13:25:42 -0700441 }
Chris Wailes9953a192021-07-28 12:07:16 -0700442
Dan Albertb433bf72021-04-27 17:12:02 -0700443 // Yes, the same out directory is used simultaneously by all rustdoc builds.
444 // This is what cargo does. The docs for individual crates get generated to
445 // a subdirectory named for the crate, and rustdoc synchronizes writes to
446 // shared pieces like the index and search data itself.
447 // https://github.com/rust-lang/rust/blob/master/src/librustdoc/html/render/write_shared.rs#L144-L146
448 docDir := android.PathForOutput(ctx, "rustdoc")
Dan Albert06feee92021-03-19 15:06:02 -0700449
Wen-yi Chu41326c12023-09-22 03:58:59 +0000450 ctx.Build(pctx, android.BuildParams{
451 Rule: rustdoc,
452 Description: "rustdoc " + main.Rel(),
453 Output: docTimestampFile,
454 Input: main,
455 Implicit: ctx.RustModule().UnstrippedOutputFile(),
456 Args: map[string]string{
457 "rustdocFlags": strings.Join(rustdocFlags, " "),
458 "outDir": docDir.String(),
459 "envVars": strings.Join(rustEnvVars(ctx, deps), " "),
460 },
461 })
Dan Albert06feee92021-03-19 15:06:02 -0700462
463 return docTimestampFile
464}