blob: b1f049d15b6b1f4b00a2189ee58336af7a1646e3 [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")
29 _ = pctx.SourcePathVariable("mkcraterspCmd", "build/soong/scripts/mkcratersp.py")
30 rustc = pctx.AndroidStaticRule("rustc",
31 blueprint.RuleParams{
32 Command: "$envVars $rustcCmd " +
33 "-C linker=$mkcraterspCmd " +
34 "--emit link -o $out --emit dep-info=$out.d.raw $in ${libFlags} $rustcFlags" +
35 " && grep \"^$out:\" $out.d.raw > $out.d",
36 CommandDeps: []string{"$rustcCmd", "$mkcraterspCmd"},
37 // 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 },
45 "rustcFlags", "libFlags", "envVars")
46 rustLink = pctx.AndroidStaticRule("rustLink",
47 blueprint.RuleParams{
48 Command: "${config.RustLinker} -o $out ${crtBegin} ${earlyLinkFlags} @$in ${linkFlags} ${crtEnd}",
49 },
50 "earlyLinkFlags", "linkFlags", "crtBegin", "crtEnd")
51
52 _ = pctx.SourcePathVariable("rustdocCmd", "${config.RustBin}/rustdoc")
53 rustdoc = pctx.AndroidStaticRule("rustdoc",
54 blueprint.RuleParams{
55 Command: "$envVars $rustdocCmd $rustdocFlags $in -o $outDir && " +
56 "touch $out",
57 CommandDeps: []string{"$rustdocCmd"},
58 },
59 "rustdocFlags", "outDir", "envVars")
60
61 _ = pctx.SourcePathVariable("clippyCmd", "${config.RustBin}/clippy-driver")
62 clippyDriver = pctx.AndroidStaticRule("clippy",
63 blueprint.RuleParams{
64 Command: "$envVars $clippyCmd " +
65 // 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.
67 "--emit metadata -o $out --emit dep-info=$out.d.raw $in ${libFlags} " +
68 "$rustcFlags $clippyFlags" +
69 " && grep \"^$out:\" $out.d.raw > $out.d",
70 CommandDeps: []string{"$clippyCmd"},
71 Deps: blueprint.DepsGCC,
72 Depfile: "$out.d",
73 },
74 "rustcFlags", "libFlags", "clippyFlags", "envVars")
75
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
Wen-yi Chu41326c12023-09-22 03:58:59 +000084 cp = pctx.AndroidStaticRule("cp",
Ivan Lozano43845682020-07-09 21:03:28 -040085 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() })
Wen-yi Chu41326c12023-09-22 03:58:59 +000099 _ = 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 ` +
109 `-C linker=true ` +
110 `$in ${libFlags} $rustcFlags`,
111 CommandDeps: []string{"$rustExtractor", "$kytheVnames"},
112 Rspfile: "${out}.rsp",
113 RspfileContent: "$in",
114 },
115 "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
Wen-yi Chu41326c12023-09-22 03:58:59 +0000127func 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
Wen-yi Chu41326c12023-09-22 03:58:59 +0000131 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "bin")
Ivan Lozanoffee3342019-08-27 12:03:00 -0700132}
133
Wen-yi Chu41326c12023-09-22 03:58:59 +0000134func TransformSrctoRlib(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Dan Albert06feee92021-03-19 15:06:02 -0700135 outputFile android.WritablePath) buildOutput {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000136 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "rlib")
Ivan Lozanoffee3342019-08-27 12:03:00 -0700137}
138
Wen-yi Chu41326c12023-09-22 03:58:59 +0000139func 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
Wen-yi Chu41326c12023-09-22 03:58:59 +0000143 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "dylib")
Ivan Lozanoffee3342019-08-27 12:03:00 -0700144}
145
Wen-yi Chu41326c12023-09-22 03:58:59 +0000146func 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")
Wen-yi Chu41326c12023-09-22 03:58:59 +0000149 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "staticlib")
Ivan Lozano52767be2019-10-18 14:49:46 -0700150}
151
Wen-yi Chu41326c12023-09-22 03:58:59 +0000152func 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")
Wen-yi Chu41326c12023-09-22 03:58:59 +0000155 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "cdylib")
Ivan Lozano52767be2019-10-18 14:49:46 -0700156}
157
Wen-yi Chu41326c12023-09-22 03:58:59 +0000158func TransformSrctoProcMacro(ctx ModuleContext, mainSrc android.Path, deps PathDeps,
Dan Albert06feee92021-03-19 15:06:02 -0700159 flags Flags, outputFile android.WritablePath) buildOutput {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000160 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
Wen-yi Chu41326c12023-09-22 03:58:59 +0000171func makeLibFlags(deps PathDeps) []string {
Dan Albert06feee92021-03-19 15:06:02 -0700172 var libFlags []string
173
174 // Collect library/crate flags
Wen-yi Chu41326c12023-09-22 03:58:59 +0000175 for _, lib := range deps.RLibs {
176 libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String())
Dan Albert06feee92021-03-19 15:06:02 -0700177 }
Wen-yi Chu41326c12023-09-22 03:58:59 +0000178 for _, lib := range deps.DyLibs {
179 libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String())
Dan Albert06feee92021-03-19 15:06:02 -0700180 }
Wen-yi Chu41326c12023-09-22 03:58:59 +0000181 for _, proc_macro := range deps.ProcMacros {
182 libFlags = append(libFlags, "--extern "+proc_macro.CrateName+"="+proc_macro.Path.String())
Dan Albert06feee92021-03-19 15:06:02 -0700183 }
184
185 for _, path := range deps.linkDirs {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000186 libFlags = append(libFlags, "-L "+path)
Dan Albert06feee92021-03-19 15:06:02 -0700187 }
188
189 return libFlags
190}
191
Wen-yi Chu41326c12023-09-22 03:58:59 +0000192func rustEnvVars(ctx ModuleContext, deps PathDeps) []string {
Dan Albert06feee92021-03-19 15:06:02 -0700193 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.
Wen-yi Chu41326c12023-09-22 03:58:59 +0000206 var outDirPrefix string
207 if !filepath.IsAbs(moduleGenDir.String()) {
Sam Delmericoa588d152023-06-16 10:28:04 -0400208 // 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 +0000209 outDirPrefix = "$$PWD/"
Sam Delmericoa588d152023-06-16 10:28:04 -0400210 } else {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000211 // If OUT_DIR is absolute, then moduleGenDir will be an absolute path, so we don't need to set this to anything.
212 outDirPrefix = ""
Dan Albert06feee92021-03-19 15:06:02 -0700213 }
Wen-yi Chu41326c12023-09-22 03:58:59 +0000214 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)
Ivan Lozanof4455622023-07-28 12:42:20 -0400231
232 // Ensure the version is in the form of "x.y.z" (approximately semver compliant).
233 //
234 // For our purposes, we don't care to enforce that these are integers since they may
235 // include other characters at times (e.g. sometimes the patch version is more than an integer).
236 if strings.Count(pkgVersion, ".") == 2 {
237 var semver_parts = strings.Split(pkgVersion, ".")
238 envVars = append(envVars, "CARGO_PKG_VERSION_MAJOR="+semver_parts[0])
239 envVars = append(envVars, "CARGO_PKG_VERSION_MINOR="+semver_parts[1])
240 envVars = append(envVars, "CARGO_PKG_VERSION_PATCH="+semver_parts[2])
241 }
Matthew Maurer34609fa2023-06-26 21:10:13 +0000242 }
243 }
244
Wen-yi Chu41326c12023-09-22 03:58:59 +0000245 envVars = append(envVars, "AR=${cc_config.ClangBin}/llvm-ar")
Matthew Maurer34609fa2023-06-26 21:10:13 +0000246
A. Cody Schuffelenf29ca582023-07-13 19:03:39 -0700247 if ctx.Darwin() {
248 envVars = append(envVars, "ANDROID_RUST_DARWIN=true")
249 }
250
Dan Albert06feee92021-03-19 15:06:02 -0700251 return envVars
252}
253
Wen-yi Chu41326c12023-09-22 03:58:59 +0000254func transformSrctoCrate(ctx ModuleContext, main android.Path, deps PathDeps, flags Flags,
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400255 outputFile android.WritablePath, crateType string) buildOutput {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700256
257 var inputs android.Paths
Wen-yi Chu41326c12023-09-22 03:58:59 +0000258 var implicits, linkImplicits, linkOrderOnly android.Paths
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400259 var output buildOutput
Dan Albert06feee92021-03-19 15:06:02 -0700260 var rustcFlags, linkFlags []string
Wen-yi Chu41326c12023-09-22 03:58:59 +0000261 var earlyLinkFlags string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400262
263 output.outputFile = outputFile
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800264 crateName := ctx.RustModule().CrateName()
ThiƩbaud Weksteen1f7f70f2020-06-24 11:32:48 +0200265 targetTriple := ctx.toolchain().RustTriple()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700266
Wen-yi Chu41326c12023-09-22 03:58:59 +0000267 envVars := rustEnvVars(ctx, deps)
268
Ivan Lozanoffee3342019-08-27 12:03:00 -0700269 inputs = append(inputs, main)
270
271 // Collect rustc flags
Ivan Lozanof1c84332019-09-20 11:00:37 -0700272 rustcFlags = append(rustcFlags, flags.GlobalRustFlags...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700273 rustcFlags = append(rustcFlags, flags.RustFlags...)
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400274 rustcFlags = append(rustcFlags, "--crate-type="+crateType)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800275 if crateName != "" {
276 rustcFlags = append(rustcFlags, "--crate-name="+crateName)
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700277 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700278 if targetTriple != "" {
279 rustcFlags = append(rustcFlags, "--target="+targetTriple)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700280 linkFlags = append(linkFlags, "-target "+targetTriple)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700281 }
Matthew Maurerbb3add12020-06-25 09:34:12 -0700282
283 // Suppress an implicit sysroot
284 rustcFlags = append(rustcFlags, "--sysroot=/dev/null")
285
Chris Wailesd9781fd2021-12-03 17:17:28 -0800286 // Enable incremental compilation if requested by user
287 if ctx.Config().IsEnvTrue("SOONG_RUSTC_INCREMENTAL") {
288 incrementalPath := android.PathForOutput(ctx, "rustc").String()
Wen-yi Chu41326c12023-09-22 03:58:59 +0000289
Chris Wailes6d12db42023-02-24 16:58:18 -0800290 rustcFlags = append(rustcFlags, "-Cincremental="+incrementalPath)
291 }
292
293 // Disallow experimental features
294 modulePath := android.PathForModuleSrc(ctx).String()
295 if !(android.IsThirdPartyPath(modulePath) || strings.HasPrefix(modulePath, "prebuilts")) {
Chris Wailes547bfdd2023-05-31 11:53:44 -0700296 rustcFlags = append(rustcFlags, "-Zallow-features=\"\"")
Chris Wailesd9781fd2021-12-03 17:17:28 -0800297 }
298
Ivan Lozanof1c84332019-09-20 11:00:37 -0700299 // Collect linker flags
A. Cody Schuffelenf29ca582023-07-13 19:03:39 -0700300 if !ctx.Darwin() {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000301 earlyLinkFlags = "-Wl,--as-needed"
A. Cody Schuffelenf29ca582023-07-13 19:03:39 -0700302 }
303
Wen-yi Chu41326c12023-09-22 03:58:59 +0000304 linkFlags = append(linkFlags, flags.GlobalLinkFlags...)
305 linkFlags = append(linkFlags, flags.LinkFlags...)
306
307 // Check if this module needs to use the bootstrap linker
308 if ctx.RustModule().Bootstrap() && !ctx.RustModule().InRecovery() && !ctx.RustModule().InRamdisk() && !ctx.RustModule().InVendorRamdisk() {
309 dynamicLinker := "-Wl,-dynamic-linker,/system/bin/bootstrap/linker"
310 if ctx.toolchain().Is64Bit() {
311 dynamicLinker += "64"
312 }
313 linkFlags = append(linkFlags, dynamicLinker)
314 }
315
316 libFlags := makeLibFlags(deps)
317
Ivan Lozanoffee3342019-08-27 12:03:00 -0700318 // Collect dependencies
Wen-yi Chu41326c12023-09-22 03:58:59 +0000319 implicits = append(implicits, rustLibsToPaths(deps.RLibs)...)
320 implicits = append(implicits, rustLibsToPaths(deps.DyLibs)...)
321 implicits = append(implicits, rustLibsToPaths(deps.ProcMacros)...)
322 implicits = append(implicits, deps.AfdoProfiles...)
323 implicits = append(implicits, deps.srcProviderFiles...)
324 implicits = append(implicits, deps.WholeStaticLibs...)
325
326 linkImplicits = append(linkImplicits, deps.LibDeps...)
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700327 linkImplicits = append(linkImplicits, deps.CrtBegin...)
328 linkImplicits = append(linkImplicits, deps.CrtEnd...)
Wen-yi Chu41326c12023-09-22 03:58:59 +0000329
330 linkOrderOnly = append(linkOrderOnly, deps.linkObjects...)
Sam Delmerico51d6d1c2023-03-28 16:54:00 -0400331
Ivan Lozano43845682020-07-09 21:03:28 -0400332 if len(deps.SrcDeps) > 0 {
ThiƩbaud Weksteenee6a89b2021-02-25 16:30:57 +0100333 moduleGenDir := ctx.RustModule().compiler.CargoOutDir()
Ivan Lozano43845682020-07-09 21:03:28 -0400334 var outputs android.WritablePaths
335
336 for _, genSrc := range deps.SrcDeps {
Ivan Lozano10735d92020-07-22 09:14:47 -0400337 if android.SuffixInList(outputs.Strings(), genSubDir+genSrc.Base()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400338 ctx.PropertyErrorf("srcs",
339 "multiple source providers generate the same filename output: "+genSrc.Base())
340 }
Ivan Lozano10735d92020-07-22 09:14:47 -0400341 outputs = append(outputs, android.PathForModuleOut(ctx, genSubDir+genSrc.Base()))
Ivan Lozano43845682020-07-09 21:03:28 -0400342 }
343
344 ctx.Build(pctx, android.BuildParams{
Wen-yi Chu41326c12023-09-22 03:58:59 +0000345 Rule: cp,
ThiƩbaud Weksteenee6a89b2021-02-25 16:30:57 +0100346 Description: "cp " + moduleGenDir.Path().Rel(),
Ivan Lozano43845682020-07-09 21:03:28 -0400347 Outputs: outputs,
348 Inputs: deps.SrcDeps,
349 Args: map[string]string{
350 "outDir": moduleGenDir.String(),
351 },
352 })
353 implicits = append(implicits, outputs.Paths()...)
Ivan Lozano43845682020-07-09 21:03:28 -0400354 }
355
Ivan Lozanobae62be2020-07-21 13:28:27 -0400356 if flags.Clippy {
357 clippyFile := android.PathForModuleOut(ctx, outputFile.Base()+".clippy")
Wen-yi Chu41326c12023-09-22 03:58:59 +0000358 ctx.Build(pctx, android.BuildParams{
359 Rule: clippyDriver,
360 Description: "clippy " + main.Rel(),
361 Output: clippyFile,
362 Inputs: inputs,
363 Implicits: implicits,
364 Args: map[string]string{
365 "rustcFlags": strings.Join(rustcFlags, " "),
366 "libFlags": strings.Join(libFlags, " "),
367 "clippyFlags": strings.Join(flags.ClippyFlags, " "),
368 "envVars": strings.Join(envVars, " "),
369 },
370 })
Ivan Lozanobae62be2020-07-21 13:28:27 -0400371 // Declare the clippy build as an implicit dependency of the original crate.
372 implicits = append(implicits, clippyFile)
373 }
374
Wen-yi Chu41326c12023-09-22 03:58:59 +0000375 rustcOutputFile := outputFile
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700376 usesLinker := crateType == "bin" || crateType == "dylib" || crateType == "cdylib" || crateType == "proc-macro"
377 if usesLinker {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000378 rustcOutputFile = android.PathForModuleOut(ctx, outputFile.Base()+".rsp")
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700379 }
380
Wen-yi Chu41326c12023-09-22 03:58:59 +0000381 ctx.Build(pctx, android.BuildParams{
382 Rule: rustc,
383 Description: "rustc " + main.Rel(),
384 Output: rustcOutputFile,
385 Inputs: inputs,
386 Implicits: implicits,
387 Args: map[string]string{
388 "rustcFlags": strings.Join(rustcFlags, " "),
389 "libFlags": strings.Join(libFlags, " "),
390 "envVars": strings.Join(envVars, " "),
391 },
392 })
Ivan Lozanoffee3342019-08-27 12:03:00 -0700393
Wen-yi Chu41326c12023-09-22 03:58:59 +0000394 if usesLinker {
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700395 ctx.Build(pctx, android.BuildParams{
Wen-yi Chu41326c12023-09-22 03:58:59 +0000396 Rule: rustLink,
397 Description: "rustLink " + main.Rel(),
398 Output: outputFile,
399 Inputs: android.Paths{rustcOutputFile},
400 Implicits: linkImplicits,
401 OrderOnly: linkOrderOnly,
402 Args: map[string]string{
403 "earlyLinkFlags": earlyLinkFlags,
404 "linkFlags": strings.Join(linkFlags, " "),
405 "crtBegin": strings.Join(deps.CrtBegin.Strings(), " "),
406 "crtEnd": strings.Join(deps.CrtEnd.Strings(), " "),
407 },
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700408 })
409 }
410
Sasha Smundaka76acba2022-04-18 20:12:56 -0700411 if flags.EmitXrefs {
412 kytheFile := android.PathForModuleOut(ctx, outputFile.Base()+".kzip")
Wen-yi Chu41326c12023-09-22 03:58:59 +0000413 ctx.Build(pctx, android.BuildParams{
414 Rule: kytheExtract,
415 Description: "Xref Rust extractor " + main.Rel(),
416 Output: kytheFile,
417 Inputs: inputs,
418 Implicits: implicits,
419 Args: map[string]string{
420 "rustcFlags": strings.Join(rustcFlags, " "),
421 "libFlags": strings.Join(libFlags, " "),
422 "envVars": strings.Join(envVars, " "),
423 },
424 })
Sasha Smundaka76acba2022-04-18 20:12:56 -0700425 output.kytheFile = kytheFile
426 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400427 return output
428}
Dan Albert06feee92021-03-19 15:06:02 -0700429
Wen-yi Chu41326c12023-09-22 03:58:59 +0000430func Rustdoc(ctx ModuleContext, main android.Path, deps PathDeps,
431 flags Flags) android.ModuleOutPath {
Dan Albert06feee92021-03-19 15:06:02 -0700432
433 rustdocFlags := append([]string{}, flags.RustdocFlags...)
434 rustdocFlags = append(rustdocFlags, "--sysroot=/dev/null")
435
Dan Albertb433bf72021-04-27 17:12:02 -0700436 // Build an index for all our crates. -Z unstable options is required to use
437 // this flag.
438 rustdocFlags = append(rustdocFlags, "-Z", "unstable-options", "--enable-index-page")
439
Dan Albert06feee92021-03-19 15:06:02 -0700440 targetTriple := ctx.toolchain().RustTriple()
441
442 // Collect rustc flags
443 if targetTriple != "" {
444 rustdocFlags = append(rustdocFlags, "--target="+targetTriple)
445 }
446
447 crateName := ctx.RustModule().CrateName()
Dan Albertb433bf72021-04-27 17:12:02 -0700448 rustdocFlags = append(rustdocFlags, "--crate-name "+crateName)
Dan Albert06feee92021-03-19 15:06:02 -0700449
Wen-yi Chu41326c12023-09-22 03:58:59 +0000450 rustdocFlags = append(rustdocFlags, makeLibFlags(deps)...)
Dan Albert06feee92021-03-19 15:06:02 -0700451 docTimestampFile := android.PathForModuleOut(ctx, "rustdoc.timestamp")
Dan Albertb433bf72021-04-27 17:12:02 -0700452
Chris Wailesb2703ad2021-07-30 13:25:42 -0700453 // Silence warnings about renamed lints for third-party crates
454 modulePath := android.PathForModuleSrc(ctx).String()
455 if android.IsThirdPartyPath(modulePath) {
Chris Wailes7b3eb242023-02-14 16:09:49 -0800456 rustdocFlags = append(rustdocFlags, " -A warnings")
Chris Wailesb2703ad2021-07-30 13:25:42 -0700457 }
Chris Wailes9953a192021-07-28 12:07:16 -0700458
Dan Albertb433bf72021-04-27 17:12:02 -0700459 // Yes, the same out directory is used simultaneously by all rustdoc builds.
460 // This is what cargo does. The docs for individual crates get generated to
461 // a subdirectory named for the crate, and rustdoc synchronizes writes to
462 // shared pieces like the index and search data itself.
463 // https://github.com/rust-lang/rust/blob/master/src/librustdoc/html/render/write_shared.rs#L144-L146
464 docDir := android.PathForOutput(ctx, "rustdoc")
Dan Albert06feee92021-03-19 15:06:02 -0700465
Wen-yi Chu41326c12023-09-22 03:58:59 +0000466 ctx.Build(pctx, android.BuildParams{
467 Rule: rustdoc,
468 Description: "rustdoc " + main.Rel(),
469 Output: docTimestampFile,
470 Input: main,
471 Implicit: ctx.RustModule().UnstrippedOutputFile(),
472 Args: map[string]string{
473 "rustdocFlags": strings.Join(rustdocFlags, " "),
474 "outDir": docDir.String(),
475 "envVars": strings.Join(rustEnvVars(ctx, deps), " "),
476 },
477 })
Dan Albert06feee92021-03-19 15:06:02 -0700478
479 return docTimestampFile
480}