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") |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 29 | rustc = pctx.AndroidStaticRule("rustc", |
| 30 | blueprint.RuleParams{ |
| 31 | Command: "$envVars $rustcCmd " + |
Colin Cross | 004bd3f | 2023-10-02 11:39:17 -0700 | [diff] [blame] | 32 | "-C linker=${config.RustLinker} " + |
| 33 | "-C link-args=\"${crtBegin} ${earlyLinkFlags} ${linkFlags} ${crtEnd}\" " + |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 34 | "--emit link -o $out --emit dep-info=$out.d.raw $in ${libFlags} $rustcFlags" + |
Ivan Lozano | cfeec1c | 2024-03-28 19:27:24 +0000 | [diff] [blame] | 35 | " && grep ^$out: $out.d.raw > $out.d", |
Colin Cross | 004bd3f | 2023-10-02 11:39:17 -0700 | [diff] [blame] | 36 | CommandDeps: []string{"$rustcCmd"}, |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 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 | }, |
Colin Cross | 004bd3f | 2023-10-02 11:39:17 -0700 | [diff] [blame] | 45 | "rustcFlags", "earlyLinkFlags", "linkFlags", "libFlags", "crtBegin", "crtEnd", "envVars") |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 46 | |
| 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 Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 71 | 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 Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 78 | |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 79 | cp = pctx.AndroidStaticRule("cp", |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 80 | blueprint.RuleParams{ |
| 81 | Command: "cp `cat $outDir.rsp` $outDir", |
| 82 | Rspfile: "${outDir}.rsp", |
| 83 | RspfileContent: "$in", |
| 84 | }, |
| 85 | "outDir") |
Sasha Smundak | a76acba | 2022-04-18 20:12:56 -0700 | [diff] [blame] | 86 | |
| 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 Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 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 ` + |
Colin Cross | 004bd3f | 2023-10-02 11:39:17 -0700 | [diff] [blame] | 104 | `-C linker=${config.RustLinker} ` + |
| 105 | `-C link-args="${crtBegin} ${linkFlags} ${crtEnd}" ` + |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 106 | `$in ${libFlags} $rustcFlags`, |
| 107 | CommandDeps: []string{"$rustExtractor", "$kytheVnames"}, |
| 108 | Rspfile: "${out}.rsp", |
| 109 | RspfileContent: "$in", |
| 110 | }, |
Colin Cross | 004bd3f | 2023-10-02 11:39:17 -0700 | [diff] [blame] | 111 | "rustcFlags", "linkFlags", "libFlags", "crtBegin", "crtEnd", "envVars") |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 112 | ) |
| 113 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 114 | type buildOutput struct { |
Joel Galenson | fa04938 | 2021-01-14 16:03:18 -0800 | [diff] [blame] | 115 | outputFile android.Path |
Sasha Smundak | a76acba | 2022-04-18 20:12:56 -0700 | [diff] [blame] | 116 | kytheFile android.Path |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 117 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 118 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 119 | func init() { |
| 120 | pctx.HostBinToolVariable("SoongZipCmd", "soong_zip") |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 123 | func TransformSrcToBinary(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags, |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 124 | outputFile android.WritablePath) buildOutput { |
Chris Wailes | 99180c2 | 2024-02-20 21:39:40 +0000 | [diff] [blame] | 125 | flags.GlobalRustFlags = append(flags.GlobalRustFlags, "-C lto=thin") |
| 126 | |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 127 | return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "bin") |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 130 | func TransformSrctoRlib(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags, |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 131 | outputFile android.WritablePath) buildOutput { |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 132 | return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "rlib") |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 135 | func TransformSrctoDylib(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags, |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 136 | outputFile android.WritablePath) buildOutput { |
Chris Wailes | 99180c2 | 2024-02-20 21:39:40 +0000 | [diff] [blame] | 137 | flags.GlobalRustFlags = append(flags.GlobalRustFlags, "-C lto=thin") |
| 138 | |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 139 | return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "dylib") |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 142 | func TransformSrctoStatic(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags, |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 143 | outputFile android.WritablePath) buildOutput { |
Chris Wailes | 99180c2 | 2024-02-20 21:39:40 +0000 | [diff] [blame] | 144 | flags.GlobalRustFlags = append(flags.GlobalRustFlags, "-C lto=thin") |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 145 | return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "staticlib") |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 148 | func TransformSrctoShared(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags, |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 149 | outputFile android.WritablePath) buildOutput { |
Chris Wailes | 99180c2 | 2024-02-20 21:39:40 +0000 | [diff] [blame] | 150 | flags.GlobalRustFlags = append(flags.GlobalRustFlags, "-C lto=thin") |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 151 | return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "cdylib") |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 154 | func TransformSrctoProcMacro(ctx ModuleContext, mainSrc android.Path, deps PathDeps, |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 155 | flags Flags, outputFile android.WritablePath) buildOutput { |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 156 | return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "proc-macro") |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | func 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 | |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 167 | func makeLibFlags(deps PathDeps) []string { |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 168 | var libFlags []string |
| 169 | |
| 170 | // Collect library/crate flags |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 171 | for _, lib := range deps.RLibs { |
| 172 | libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String()) |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 173 | } |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 174 | for _, lib := range deps.DyLibs { |
| 175 | libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String()) |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 176 | } |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 177 | for _, proc_macro := range deps.ProcMacros { |
| 178 | libFlags = append(libFlags, "--extern "+proc_macro.CrateName+"="+proc_macro.Path.String()) |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | for _, path := range deps.linkDirs { |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 182 | libFlags = append(libFlags, "-L "+path) |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | return libFlags |
| 186 | } |
| 187 | |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 188 | func rustEnvVars(ctx ModuleContext, deps PathDeps) []string { |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 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 { |
Matthew Maurer | cd41653 | 2023-11-20 17:52:01 +0000 | [diff] [blame] | 199 | moduleGenDir := ctx.RustModule().compiler.cargoOutDir() |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 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. |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 202 | var outDirPrefix string |
| 203 | if !filepath.IsAbs(moduleGenDir.String()) { |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 204 | // 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] | 205 | outDirPrefix = "$$PWD/" |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 206 | } else { |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 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 = "" |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 209 | } |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 210 | envVars = append(envVars, "OUT_DIR="+filepath.Join(outDirPrefix, moduleGenDir.String())) |
Peter Collingbourne | 0dcd62e | 2023-03-31 23:05:16 -0700 | [diff] [blame] | 211 | } else { |
| 212 | // TODO(pcc): Change this to "OUT_DIR=" after fixing crates to not rely on this value. |
| 213 | envVars = append(envVars, "OUT_DIR=out") |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Matthew Maurer | 34609fa | 2023-06-26 21:10:13 +0000 | [diff] [blame] | 216 | envVars = append(envVars, "ANDROID_RUST_VERSION="+config.GetRustVersion(ctx)) |
| 217 | |
Matthew Maurer | cd41653 | 2023-11-20 17:52:01 +0000 | [diff] [blame] | 218 | if ctx.RustModule().compiler.cargoEnvCompat() { |
Matthew Maurer | 34609fa | 2023-06-26 21:10:13 +0000 | [diff] [blame] | 219 | if bin, ok := ctx.RustModule().compiler.(*binaryDecorator); ok { |
| 220 | envVars = append(envVars, "CARGO_BIN_NAME="+bin.getStem(ctx)) |
| 221 | } |
| 222 | envVars = append(envVars, "CARGO_CRATE_NAME="+ctx.RustModule().CrateName()) |
| 223 | envVars = append(envVars, "CARGO_PKG_NAME="+ctx.RustModule().CrateName()) |
Matthew Maurer | cd41653 | 2023-11-20 17:52:01 +0000 | [diff] [blame] | 224 | pkgVersion := ctx.RustModule().compiler.cargoPkgVersion() |
Matthew Maurer | 34609fa | 2023-06-26 21:10:13 +0000 | [diff] [blame] | 225 | if pkgVersion != "" { |
| 226 | envVars = append(envVars, "CARGO_PKG_VERSION="+pkgVersion) |
Ivan Lozano | f445562 | 2023-07-28 12:42:20 -0400 | [diff] [blame] | 227 | |
| 228 | // Ensure the version is in the form of "x.y.z" (approximately semver compliant). |
| 229 | // |
| 230 | // For our purposes, we don't care to enforce that these are integers since they may |
| 231 | // include other characters at times (e.g. sometimes the patch version is more than an integer). |
| 232 | if strings.Count(pkgVersion, ".") == 2 { |
| 233 | var semver_parts = strings.Split(pkgVersion, ".") |
| 234 | envVars = append(envVars, "CARGO_PKG_VERSION_MAJOR="+semver_parts[0]) |
| 235 | envVars = append(envVars, "CARGO_PKG_VERSION_MINOR="+semver_parts[1]) |
| 236 | envVars = append(envVars, "CARGO_PKG_VERSION_PATCH="+semver_parts[2]) |
| 237 | } |
Matthew Maurer | 34609fa | 2023-06-26 21:10:13 +0000 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
Sam Delmerico | c5cf990 | 2023-10-23 21:40:13 +0000 | [diff] [blame] | 241 | if ctx.Darwin() { |
| 242 | envVars = append(envVars, "ANDROID_RUST_DARWIN=true") |
| 243 | } |
| 244 | |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 245 | return envVars |
| 246 | } |
| 247 | |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 248 | func transformSrctoCrate(ctx ModuleContext, main android.Path, deps PathDeps, flags Flags, |
Ivan Lozano | 7b0781d | 2021-11-03 15:30:18 -0400 | [diff] [blame] | 249 | outputFile android.WritablePath, crateType string) buildOutput { |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 250 | |
| 251 | var inputs android.Paths |
Colin Cross | 004bd3f | 2023-10-02 11:39:17 -0700 | [diff] [blame] | 252 | var implicits android.Paths |
| 253 | var orderOnly android.Paths |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 254 | var output buildOutput |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 255 | var rustcFlags, linkFlags []string |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 256 | var earlyLinkFlags string |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 257 | |
| 258 | output.outputFile = outputFile |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 259 | crateName := ctx.RustModule().CrateName() |
ThiƩbaud Weksteen | 1f7f70f | 2020-06-24 11:32:48 +0200 | [diff] [blame] | 260 | targetTriple := ctx.toolchain().RustTriple() |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 261 | |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 262 | envVars := rustEnvVars(ctx, deps) |
| 263 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 264 | inputs = append(inputs, main) |
| 265 | |
| 266 | // Collect rustc flags |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 267 | rustcFlags = append(rustcFlags, flags.GlobalRustFlags...) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 268 | rustcFlags = append(rustcFlags, flags.RustFlags...) |
Ivan Lozano | 7b0781d | 2021-11-03 15:30:18 -0400 | [diff] [blame] | 269 | rustcFlags = append(rustcFlags, "--crate-type="+crateType) |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 270 | if crateName != "" { |
| 271 | rustcFlags = append(rustcFlags, "--crate-name="+crateName) |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 272 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 273 | if targetTriple != "" { |
| 274 | rustcFlags = append(rustcFlags, "--target="+targetTriple) |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 275 | linkFlags = append(linkFlags, "-target "+targetTriple) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 276 | } |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame] | 277 | |
| 278 | // Suppress an implicit sysroot |
| 279 | rustcFlags = append(rustcFlags, "--sysroot=/dev/null") |
| 280 | |
Chris Wailes | 99180c2 | 2024-02-20 21:39:40 +0000 | [diff] [blame] | 281 | // Enable incremental compilation if requested by user |
| 282 | if ctx.Config().IsEnvTrue("SOONG_RUSTC_INCREMENTAL") { |
| 283 | incrementalPath := android.PathForOutput(ctx, "rustc").String() |
| 284 | |
| 285 | rustcFlags = append(rustcFlags, "-C incremental="+incrementalPath) |
| 286 | } else { |
| 287 | rustcFlags = append(rustcFlags, "-C codegen-units=1") |
| 288 | } |
| 289 | |
Chris Wailes | 6d12db4 | 2023-02-24 16:58:18 -0800 | [diff] [blame] | 290 | // Disallow experimental features |
Colin Cross | f96b001 | 2023-10-26 14:01:51 -0700 | [diff] [blame] | 291 | modulePath := ctx.ModuleDir() |
Chris Wailes | 6d12db4 | 2023-02-24 16:58:18 -0800 | [diff] [blame] | 292 | if !(android.IsThirdPartyPath(modulePath) || strings.HasPrefix(modulePath, "prebuilts")) { |
Chris Wailes | 547bfdd | 2023-05-31 11:53:44 -0700 | [diff] [blame] | 293 | rustcFlags = append(rustcFlags, "-Zallow-features=\"\"") |
Chris Wailes | d9781fd | 2021-12-03 17:17:28 -0800 | [diff] [blame] | 294 | } |
| 295 | |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 296 | // Collect linker flags |
A. Cody Schuffelen | f29ca58 | 2023-07-13 19:03:39 -0700 | [diff] [blame] | 297 | if !ctx.Darwin() { |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 298 | earlyLinkFlags = "-Wl,--as-needed" |
A. Cody Schuffelen | f29ca58 | 2023-07-13 19:03:39 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 301 | linkFlags = append(linkFlags, flags.GlobalLinkFlags...) |
| 302 | linkFlags = append(linkFlags, flags.LinkFlags...) |
| 303 | |
| 304 | // Check if this module needs to use the bootstrap linker |
| 305 | if ctx.RustModule().Bootstrap() && !ctx.RustModule().InRecovery() && !ctx.RustModule().InRamdisk() && !ctx.RustModule().InVendorRamdisk() { |
| 306 | dynamicLinker := "-Wl,-dynamic-linker,/system/bin/bootstrap/linker" |
| 307 | if ctx.toolchain().Is64Bit() { |
| 308 | dynamicLinker += "64" |
| 309 | } |
| 310 | linkFlags = append(linkFlags, dynamicLinker) |
| 311 | } |
| 312 | |
| 313 | libFlags := makeLibFlags(deps) |
| 314 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 315 | // Collect dependencies |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 316 | implicits = append(implicits, rustLibsToPaths(deps.RLibs)...) |
| 317 | implicits = append(implicits, rustLibsToPaths(deps.DyLibs)...) |
| 318 | implicits = append(implicits, rustLibsToPaths(deps.ProcMacros)...) |
Colin Cross | 004bd3f | 2023-10-02 11:39:17 -0700 | [diff] [blame] | 319 | implicits = append(implicits, deps.StaticLibs...) |
| 320 | implicits = append(implicits, deps.SharedLibDeps...) |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 321 | implicits = append(implicits, deps.srcProviderFiles...) |
Colin Cross | 004bd3f | 2023-10-02 11:39:17 -0700 | [diff] [blame] | 322 | implicits = append(implicits, deps.AfdoProfiles...) |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 323 | |
Colin Cross | 004bd3f | 2023-10-02 11:39:17 -0700 | [diff] [blame] | 324 | implicits = append(implicits, deps.CrtBegin...) |
| 325 | implicits = append(implicits, deps.CrtEnd...) |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 326 | |
Colin Cross | 004bd3f | 2023-10-02 11:39:17 -0700 | [diff] [blame] | 327 | orderOnly = append(orderOnly, deps.SharedLibs...) |
Sam Delmerico | 51d6d1c | 2023-03-28 16:54:00 -0400 | [diff] [blame] | 328 | |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 329 | if len(deps.SrcDeps) > 0 { |
Matthew Maurer | cd41653 | 2023-11-20 17:52:01 +0000 | [diff] [blame] | 330 | moduleGenDir := ctx.RustModule().compiler.cargoOutDir() |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 331 | var outputs android.WritablePaths |
| 332 | |
| 333 | for _, genSrc := range deps.SrcDeps { |
Ivan Lozano | 10735d9 | 2020-07-22 09:14:47 -0400 | [diff] [blame] | 334 | if android.SuffixInList(outputs.Strings(), genSubDir+genSrc.Base()) { |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 335 | ctx.PropertyErrorf("srcs", |
| 336 | "multiple source providers generate the same filename output: "+genSrc.Base()) |
| 337 | } |
Ivan Lozano | 10735d9 | 2020-07-22 09:14:47 -0400 | [diff] [blame] | 338 | outputs = append(outputs, android.PathForModuleOut(ctx, genSubDir+genSrc.Base())) |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | ctx.Build(pctx, android.BuildParams{ |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 342 | Rule: cp, |
ThiƩbaud Weksteen | ee6a89b | 2021-02-25 16:30:57 +0100 | [diff] [blame] | 343 | Description: "cp " + moduleGenDir.Path().Rel(), |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 344 | Outputs: outputs, |
| 345 | Inputs: deps.SrcDeps, |
| 346 | Args: map[string]string{ |
| 347 | "outDir": moduleGenDir.String(), |
| 348 | }, |
| 349 | }) |
| 350 | implicits = append(implicits, outputs.Paths()...) |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 351 | } |
| 352 | |
Ivan Lozano | bae62be | 2020-07-21 13:28:27 -0400 | [diff] [blame] | 353 | if flags.Clippy { |
| 354 | clippyFile := android.PathForModuleOut(ctx, outputFile.Base()+".clippy") |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 355 | ctx.Build(pctx, android.BuildParams{ |
Colin Cross | 004bd3f | 2023-10-02 11:39:17 -0700 | [diff] [blame] | 356 | Rule: clippyDriver, |
| 357 | Description: "clippy " + main.Rel(), |
| 358 | Output: clippyFile, |
| 359 | ImplicitOutputs: nil, |
| 360 | Inputs: inputs, |
| 361 | Implicits: implicits, |
| 362 | OrderOnly: orderOnly, |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 363 | Args: map[string]string{ |
| 364 | "rustcFlags": strings.Join(rustcFlags, " "), |
| 365 | "libFlags": strings.Join(libFlags, " "), |
| 366 | "clippyFlags": strings.Join(flags.ClippyFlags, " "), |
| 367 | "envVars": strings.Join(envVars, " "), |
| 368 | }, |
| 369 | }) |
Ivan Lozano | bae62be | 2020-07-21 13:28:27 -0400 | [diff] [blame] | 370 | // Declare the clippy build as an implicit dependency of the original crate. |
| 371 | implicits = append(implicits, clippyFile) |
| 372 | } |
| 373 | |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 374 | ctx.Build(pctx, android.BuildParams{ |
Colin Cross | 004bd3f | 2023-10-02 11:39:17 -0700 | [diff] [blame] | 375 | Rule: rustc, |
| 376 | Description: "rustc " + main.Rel(), |
| 377 | Output: outputFile, |
| 378 | Inputs: inputs, |
| 379 | Implicits: implicits, |
| 380 | OrderOnly: orderOnly, |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 381 | Args: map[string]string{ |
Colin Cross | 004bd3f | 2023-10-02 11:39:17 -0700 | [diff] [blame] | 382 | "rustcFlags": strings.Join(rustcFlags, " "), |
| 383 | "earlyLinkFlags": earlyLinkFlags, |
| 384 | "linkFlags": strings.Join(linkFlags, " "), |
| 385 | "libFlags": strings.Join(libFlags, " "), |
| 386 | "crtBegin": strings.Join(deps.CrtBegin.Strings(), " "), |
| 387 | "crtEnd": strings.Join(deps.CrtEnd.Strings(), " "), |
| 388 | "envVars": strings.Join(envVars, " "), |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 389 | }, |
| 390 | }) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 391 | |
Sasha Smundak | a76acba | 2022-04-18 20:12:56 -0700 | [diff] [blame] | 392 | if flags.EmitXrefs { |
| 393 | kytheFile := android.PathForModuleOut(ctx, outputFile.Base()+".kzip") |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 394 | ctx.Build(pctx, android.BuildParams{ |
| 395 | Rule: kytheExtract, |
| 396 | Description: "Xref Rust extractor " + main.Rel(), |
| 397 | Output: kytheFile, |
| 398 | Inputs: inputs, |
| 399 | Implicits: implicits, |
Colin Cross | 004bd3f | 2023-10-02 11:39:17 -0700 | [diff] [blame] | 400 | OrderOnly: orderOnly, |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 401 | Args: map[string]string{ |
| 402 | "rustcFlags": strings.Join(rustcFlags, " "), |
Colin Cross | 004bd3f | 2023-10-02 11:39:17 -0700 | [diff] [blame] | 403 | "linkFlags": strings.Join(linkFlags, " "), |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 404 | "libFlags": strings.Join(libFlags, " "), |
Colin Cross | 004bd3f | 2023-10-02 11:39:17 -0700 | [diff] [blame] | 405 | "crtBegin": strings.Join(deps.CrtBegin.Strings(), " "), |
| 406 | "crtEnd": strings.Join(deps.CrtEnd.Strings(), " "), |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 407 | "envVars": strings.Join(envVars, " "), |
| 408 | }, |
| 409 | }) |
Sasha Smundak | a76acba | 2022-04-18 20:12:56 -0700 | [diff] [blame] | 410 | output.kytheFile = kytheFile |
| 411 | } |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 412 | return output |
| 413 | } |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 414 | |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 415 | func Rustdoc(ctx ModuleContext, main android.Path, deps PathDeps, |
| 416 | flags Flags) android.ModuleOutPath { |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 417 | |
| 418 | rustdocFlags := append([]string{}, flags.RustdocFlags...) |
| 419 | rustdocFlags = append(rustdocFlags, "--sysroot=/dev/null") |
| 420 | |
Dan Albert | b433bf7 | 2021-04-27 17:12:02 -0700 | [diff] [blame] | 421 | // Build an index for all our crates. -Z unstable options is required to use |
| 422 | // this flag. |
| 423 | rustdocFlags = append(rustdocFlags, "-Z", "unstable-options", "--enable-index-page") |
| 424 | |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 425 | targetTriple := ctx.toolchain().RustTriple() |
| 426 | |
| 427 | // Collect rustc flags |
| 428 | if targetTriple != "" { |
| 429 | rustdocFlags = append(rustdocFlags, "--target="+targetTriple) |
| 430 | } |
| 431 | |
| 432 | crateName := ctx.RustModule().CrateName() |
Dan Albert | b433bf7 | 2021-04-27 17:12:02 -0700 | [diff] [blame] | 433 | rustdocFlags = append(rustdocFlags, "--crate-name "+crateName) |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 434 | |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 435 | rustdocFlags = append(rustdocFlags, makeLibFlags(deps)...) |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 436 | docTimestampFile := android.PathForModuleOut(ctx, "rustdoc.timestamp") |
Dan Albert | b433bf7 | 2021-04-27 17:12:02 -0700 | [diff] [blame] | 437 | |
Chris Wailes | b2703ad | 2021-07-30 13:25:42 -0700 | [diff] [blame] | 438 | // Silence warnings about renamed lints for third-party crates |
Colin Cross | f96b001 | 2023-10-26 14:01:51 -0700 | [diff] [blame] | 439 | modulePath := ctx.ModuleDir() |
Chris Wailes | b2703ad | 2021-07-30 13:25:42 -0700 | [diff] [blame] | 440 | if android.IsThirdPartyPath(modulePath) { |
Chris Wailes | 7b3eb24 | 2023-02-14 16:09:49 -0800 | [diff] [blame] | 441 | rustdocFlags = append(rustdocFlags, " -A warnings") |
Chris Wailes | b2703ad | 2021-07-30 13:25:42 -0700 | [diff] [blame] | 442 | } |
Chris Wailes | 9953a19 | 2021-07-28 12:07:16 -0700 | [diff] [blame] | 443 | |
Dan Albert | b433bf7 | 2021-04-27 17:12:02 -0700 | [diff] [blame] | 444 | // Yes, the same out directory is used simultaneously by all rustdoc builds. |
| 445 | // This is what cargo does. The docs for individual crates get generated to |
| 446 | // a subdirectory named for the crate, and rustdoc synchronizes writes to |
| 447 | // shared pieces like the index and search data itself. |
| 448 | // https://github.com/rust-lang/rust/blob/master/src/librustdoc/html/render/write_shared.rs#L144-L146 |
| 449 | docDir := android.PathForOutput(ctx, "rustdoc") |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 450 | |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 451 | ctx.Build(pctx, android.BuildParams{ |
| 452 | Rule: rustdoc, |
| 453 | Description: "rustdoc " + main.Rel(), |
| 454 | Output: docTimestampFile, |
| 455 | Input: main, |
| 456 | Implicit: ctx.RustModule().UnstrippedOutputFile(), |
| 457 | Args: map[string]string{ |
| 458 | "rustdocFlags": strings.Join(rustdocFlags, " "), |
| 459 | "outDir": docDir.String(), |
| 460 | "envVars": strings.Join(rustEnvVars(ctx, deps), " "), |
| 461 | }, |
| 462 | }) |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 463 | |
| 464 | return docTimestampFile |
| 465 | } |