Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package rust |
| 16 | |
| 17 | import ( |
Ivan Lozano | 1776a2a | 2020-11-11 10:59:52 -0500 | [diff] [blame] | 18 | "path/filepath" |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 19 | "strings" |
| 20 | |
| 21 | "github.com/google/blueprint" |
| 22 | |
| 23 | "android/soong/android" |
ThiƩbaud Weksteen | 71512f3 | 2020-11-03 15:17:51 +0100 | [diff] [blame] | 24 | "android/soong/rust/config" |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | var ( |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame^] | 28 | _ = 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 Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 76 | 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 Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 83 | |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame^] | 84 | cp = pctx.AndroidStaticRule("cp", |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 85 | blueprint.RuleParams{ |
| 86 | Command: "cp `cat $outDir.rsp` $outDir", |
| 87 | Rspfile: "${outDir}.rsp", |
| 88 | RspfileContent: "$in", |
| 89 | }, |
| 90 | "outDir") |
Sasha Smundak | a76acba | 2022-04-18 20:12:56 -0700 | [diff] [blame] | 91 | |
| 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 Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame^] | 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 ` + |
| 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 Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 116 | ) |
| 117 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 118 | type buildOutput struct { |
Joel Galenson | fa04938 | 2021-01-14 16:03:18 -0800 | [diff] [blame] | 119 | outputFile android.Path |
Sasha Smundak | a76acba | 2022-04-18 20:12:56 -0700 | [diff] [blame] | 120 | kytheFile android.Path |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 121 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 122 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 123 | func init() { |
| 124 | pctx.HostBinToolVariable("SoongZipCmd", "soong_zip") |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame^] | 127 | func TransformSrcToBinary(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags, |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 128 | outputFile android.WritablePath) buildOutput { |
Pirama Arumuga Nainar | f77913f | 2021-10-25 15:37:43 -0700 | [diff] [blame] | 129 | flags.GlobalRustFlags = append(flags.GlobalRustFlags, "-C lto=thin") |
Ivan Lozano | 31b095d | 2019-11-20 10:14:33 -0800 | [diff] [blame] | 130 | |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame^] | 131 | return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "bin") |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame^] | 134 | func TransformSrctoRlib(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags, |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 135 | outputFile android.WritablePath) buildOutput { |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame^] | 136 | return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "rlib") |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame^] | 139 | func TransformSrctoDylib(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags, |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 140 | outputFile android.WritablePath) buildOutput { |
Chris Wailes | 5f78840 | 2023-03-02 16:06:01 -0800 | [diff] [blame] | 141 | flags.GlobalRustFlags = append(flags.GlobalRustFlags, "-C lto=thin") |
| 142 | |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame^] | 143 | return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "dylib") |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame^] | 146 | func TransformSrctoStatic(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags, |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 147 | outputFile android.WritablePath) buildOutput { |
Pirama Arumuga Nainar | f77913f | 2021-10-25 15:37:43 -0700 | [diff] [blame] | 148 | flags.GlobalRustFlags = append(flags.GlobalRustFlags, "-C lto=thin") |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame^] | 149 | return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "staticlib") |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame^] | 152 | func TransformSrctoShared(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags, |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 153 | outputFile android.WritablePath) buildOutput { |
Pirama Arumuga Nainar | f77913f | 2021-10-25 15:37:43 -0700 | [diff] [blame] | 154 | flags.GlobalRustFlags = append(flags.GlobalRustFlags, "-C lto=thin") |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame^] | 155 | return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "cdylib") |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame^] | 158 | func TransformSrctoProcMacro(ctx ModuleContext, mainSrc android.Path, deps PathDeps, |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 159 | flags Flags, outputFile android.WritablePath) buildOutput { |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame^] | 160 | return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "proc-macro") |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | func 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 Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame^] | 171 | func makeLibFlags(deps PathDeps) []string { |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 172 | var libFlags []string |
| 173 | |
| 174 | // Collect library/crate flags |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame^] | 175 | for _, lib := range deps.RLibs { |
| 176 | libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String()) |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 177 | } |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame^] | 178 | for _, lib := range deps.DyLibs { |
| 179 | libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String()) |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 180 | } |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame^] | 181 | for _, proc_macro := range deps.ProcMacros { |
| 182 | libFlags = append(libFlags, "--extern "+proc_macro.CrateName+"="+proc_macro.Path.String()) |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | for _, path := range deps.linkDirs { |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame^] | 186 | libFlags = append(libFlags, "-L "+path) |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | return libFlags |
| 190 | } |
| 191 | |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame^] | 192 | func rustEnvVars(ctx ModuleContext, deps PathDeps) []string { |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 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. |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame^] | 206 | var outDirPrefix string |
| 207 | if !filepath.IsAbs(moduleGenDir.String()) { |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 208 | // If OUT_DIR is not absolute, we use $$PWD to generate an absolute path (os.Getwd() returns '/') |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame^] | 209 | outDirPrefix = "$$PWD/" |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 210 | } else { |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame^] | 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 = "" |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 213 | } |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame^] | 214 | envVars = append(envVars, "OUT_DIR="+filepath.Join(outDirPrefix, moduleGenDir.String())) |
Peter Collingbourne | 0dcd62e | 2023-03-31 23:05:16 -0700 | [diff] [blame] | 215 | } 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 Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Matthew Maurer | 34609fa | 2023-06-26 21:10:13 +0000 | [diff] [blame] | 220 | 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 Lozano | f445562 | 2023-07-28 12:42:20 -0400 | [diff] [blame] | 231 | |
| 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 Maurer | 34609fa | 2023-06-26 21:10:13 +0000 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame^] | 245 | envVars = append(envVars, "AR=${cc_config.ClangBin}/llvm-ar") |
Matthew Maurer | 34609fa | 2023-06-26 21:10:13 +0000 | [diff] [blame] | 246 | |
A. Cody Schuffelen | f29ca58 | 2023-07-13 19:03:39 -0700 | [diff] [blame] | 247 | if ctx.Darwin() { |
| 248 | envVars = append(envVars, "ANDROID_RUST_DARWIN=true") |
| 249 | } |
| 250 | |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 251 | return envVars |
| 252 | } |
| 253 | |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame^] | 254 | func transformSrctoCrate(ctx ModuleContext, main android.Path, deps PathDeps, flags Flags, |
Ivan Lozano | 7b0781d | 2021-11-03 15:30:18 -0400 | [diff] [blame] | 255 | outputFile android.WritablePath, crateType string) buildOutput { |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 256 | |
| 257 | var inputs android.Paths |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame^] | 258 | var implicits, linkImplicits, linkOrderOnly android.Paths |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 259 | var output buildOutput |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 260 | var rustcFlags, linkFlags []string |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame^] | 261 | var earlyLinkFlags string |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 262 | |
| 263 | output.outputFile = outputFile |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 264 | crateName := ctx.RustModule().CrateName() |
ThiƩbaud Weksteen | 1f7f70f | 2020-06-24 11:32:48 +0200 | [diff] [blame] | 265 | targetTriple := ctx.toolchain().RustTriple() |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 266 | |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame^] | 267 | envVars := rustEnvVars(ctx, deps) |
| 268 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 269 | inputs = append(inputs, main) |
| 270 | |
| 271 | // Collect rustc flags |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 272 | rustcFlags = append(rustcFlags, flags.GlobalRustFlags...) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 273 | rustcFlags = append(rustcFlags, flags.RustFlags...) |
Ivan Lozano | 7b0781d | 2021-11-03 15:30:18 -0400 | [diff] [blame] | 274 | rustcFlags = append(rustcFlags, "--crate-type="+crateType) |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 275 | if crateName != "" { |
| 276 | rustcFlags = append(rustcFlags, "--crate-name="+crateName) |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 277 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 278 | if targetTriple != "" { |
| 279 | rustcFlags = append(rustcFlags, "--target="+targetTriple) |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 280 | linkFlags = append(linkFlags, "-target "+targetTriple) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 281 | } |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame] | 282 | |
| 283 | // Suppress an implicit sysroot |
| 284 | rustcFlags = append(rustcFlags, "--sysroot=/dev/null") |
| 285 | |
Chris Wailes | d9781fd | 2021-12-03 17:17:28 -0800 | [diff] [blame] | 286 | // Enable incremental compilation if requested by user |
| 287 | if ctx.Config().IsEnvTrue("SOONG_RUSTC_INCREMENTAL") { |
| 288 | incrementalPath := android.PathForOutput(ctx, "rustc").String() |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame^] | 289 | |
Chris Wailes | 6d12db4 | 2023-02-24 16:58:18 -0800 | [diff] [blame] | 290 | 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 Wailes | 547bfdd | 2023-05-31 11:53:44 -0700 | [diff] [blame] | 296 | rustcFlags = append(rustcFlags, "-Zallow-features=\"\"") |
Chris Wailes | d9781fd | 2021-12-03 17:17:28 -0800 | [diff] [blame] | 297 | } |
| 298 | |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 299 | // Collect linker flags |
A. Cody Schuffelen | f29ca58 | 2023-07-13 19:03:39 -0700 | [diff] [blame] | 300 | if !ctx.Darwin() { |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame^] | 301 | earlyLinkFlags = "-Wl,--as-needed" |
A. Cody Schuffelen | f29ca58 | 2023-07-13 19:03:39 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame^] | 304 | 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 Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 318 | // Collect dependencies |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame^] | 319 | 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 Collingbourne | e7c71c3 | 2023-03-31 20:21:19 -0700 | [diff] [blame] | 327 | linkImplicits = append(linkImplicits, deps.CrtBegin...) |
| 328 | linkImplicits = append(linkImplicits, deps.CrtEnd...) |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame^] | 329 | |
| 330 | linkOrderOnly = append(linkOrderOnly, deps.linkObjects...) |
Sam Delmerico | 51d6d1c | 2023-03-28 16:54:00 -0400 | [diff] [blame] | 331 | |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 332 | if len(deps.SrcDeps) > 0 { |
ThiƩbaud Weksteen | ee6a89b | 2021-02-25 16:30:57 +0100 | [diff] [blame] | 333 | moduleGenDir := ctx.RustModule().compiler.CargoOutDir() |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 334 | var outputs android.WritablePaths |
| 335 | |
| 336 | for _, genSrc := range deps.SrcDeps { |
Ivan Lozano | 10735d9 | 2020-07-22 09:14:47 -0400 | [diff] [blame] | 337 | if android.SuffixInList(outputs.Strings(), genSubDir+genSrc.Base()) { |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 338 | ctx.PropertyErrorf("srcs", |
| 339 | "multiple source providers generate the same filename output: "+genSrc.Base()) |
| 340 | } |
Ivan Lozano | 10735d9 | 2020-07-22 09:14:47 -0400 | [diff] [blame] | 341 | outputs = append(outputs, android.PathForModuleOut(ctx, genSubDir+genSrc.Base())) |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | ctx.Build(pctx, android.BuildParams{ |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame^] | 345 | Rule: cp, |
ThiƩbaud Weksteen | ee6a89b | 2021-02-25 16:30:57 +0100 | [diff] [blame] | 346 | Description: "cp " + moduleGenDir.Path().Rel(), |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 347 | Outputs: outputs, |
| 348 | Inputs: deps.SrcDeps, |
| 349 | Args: map[string]string{ |
| 350 | "outDir": moduleGenDir.String(), |
| 351 | }, |
| 352 | }) |
| 353 | implicits = append(implicits, outputs.Paths()...) |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 354 | } |
| 355 | |
Ivan Lozano | bae62be | 2020-07-21 13:28:27 -0400 | [diff] [blame] | 356 | if flags.Clippy { |
| 357 | clippyFile := android.PathForModuleOut(ctx, outputFile.Base()+".clippy") |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame^] | 358 | 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 Lozano | bae62be | 2020-07-21 13:28:27 -0400 | [diff] [blame] | 371 | // Declare the clippy build as an implicit dependency of the original crate. |
| 372 | implicits = append(implicits, clippyFile) |
| 373 | } |
| 374 | |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame^] | 375 | rustcOutputFile := outputFile |
Peter Collingbourne | e7c71c3 | 2023-03-31 20:21:19 -0700 | [diff] [blame] | 376 | usesLinker := crateType == "bin" || crateType == "dylib" || crateType == "cdylib" || crateType == "proc-macro" |
| 377 | if usesLinker { |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame^] | 378 | rustcOutputFile = android.PathForModuleOut(ctx, outputFile.Base()+".rsp") |
Peter Collingbourne | e7c71c3 | 2023-03-31 20:21:19 -0700 | [diff] [blame] | 379 | } |
| 380 | |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame^] | 381 | 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 Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 393 | |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame^] | 394 | if usesLinker { |
Peter Collingbourne | e7c71c3 | 2023-03-31 20:21:19 -0700 | [diff] [blame] | 395 | ctx.Build(pctx, android.BuildParams{ |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame^] | 396 | 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 Collingbourne | e7c71c3 | 2023-03-31 20:21:19 -0700 | [diff] [blame] | 408 | }) |
| 409 | } |
| 410 | |
Sasha Smundak | a76acba | 2022-04-18 20:12:56 -0700 | [diff] [blame] | 411 | if flags.EmitXrefs { |
| 412 | kytheFile := android.PathForModuleOut(ctx, outputFile.Base()+".kzip") |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame^] | 413 | 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 Smundak | a76acba | 2022-04-18 20:12:56 -0700 | [diff] [blame] | 425 | output.kytheFile = kytheFile |
| 426 | } |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 427 | return output |
| 428 | } |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 429 | |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame^] | 430 | func Rustdoc(ctx ModuleContext, main android.Path, deps PathDeps, |
| 431 | flags Flags) android.ModuleOutPath { |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 432 | |
| 433 | rustdocFlags := append([]string{}, flags.RustdocFlags...) |
| 434 | rustdocFlags = append(rustdocFlags, "--sysroot=/dev/null") |
| 435 | |
Dan Albert | b433bf7 | 2021-04-27 17:12:02 -0700 | [diff] [blame] | 436 | // 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 Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 440 | 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 Albert | b433bf7 | 2021-04-27 17:12:02 -0700 | [diff] [blame] | 448 | rustdocFlags = append(rustdocFlags, "--crate-name "+crateName) |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 449 | |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame^] | 450 | rustdocFlags = append(rustdocFlags, makeLibFlags(deps)...) |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 451 | docTimestampFile := android.PathForModuleOut(ctx, "rustdoc.timestamp") |
Dan Albert | b433bf7 | 2021-04-27 17:12:02 -0700 | [diff] [blame] | 452 | |
Chris Wailes | b2703ad | 2021-07-30 13:25:42 -0700 | [diff] [blame] | 453 | // Silence warnings about renamed lints for third-party crates |
| 454 | modulePath := android.PathForModuleSrc(ctx).String() |
| 455 | if android.IsThirdPartyPath(modulePath) { |
Chris Wailes | 7b3eb24 | 2023-02-14 16:09:49 -0800 | [diff] [blame] | 456 | rustdocFlags = append(rustdocFlags, " -A warnings") |
Chris Wailes | b2703ad | 2021-07-30 13:25:42 -0700 | [diff] [blame] | 457 | } |
Chris Wailes | 9953a19 | 2021-07-28 12:07:16 -0700 | [diff] [blame] | 458 | |
Dan Albert | b433bf7 | 2021-04-27 17:12:02 -0700 | [diff] [blame] | 459 | // 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 Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 465 | |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame^] | 466 | 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 Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 478 | |
| 479 | return docTimestampFile |
| 480 | } |