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 ( |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 18 | "fmt" |
Ivan Lozano | 1776a2a | 2020-11-11 10:59:52 -0500 | [diff] [blame] | 19 | "path/filepath" |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 20 | "strings" |
| 21 | |
| 22 | "github.com/google/blueprint" |
| 23 | |
| 24 | "android/soong/android" |
Sam Delmerico | 476e876 | 2023-09-21 06:21:21 +0000 | [diff] [blame^] | 25 | cc_config "android/soong/cc/config" |
ThiƩbaud Weksteen | 71512f3 | 2020-11-03 15:17:51 +0100 | [diff] [blame] | 26 | "android/soong/rust/config" |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | var ( |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 30 | zip = pctx.AndroidStaticRule("zip", |
| 31 | blueprint.RuleParams{ |
| 32 | Command: "cat $out.rsp | tr ' ' '\\n' | tr -d \\' | sort -u > ${out}.tmp && ${SoongZipCmd} -o ${out} -C $$OUT_DIR -l ${out}.tmp", |
| 33 | CommandDeps: []string{"${SoongZipCmd}"}, |
| 34 | Rspfile: "$out.rsp", |
| 35 | RspfileContent: "$in", |
| 36 | }) |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 37 | |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 38 | cpDir = pctx.AndroidStaticRule("cpDir", |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 39 | blueprint.RuleParams{ |
| 40 | Command: "cp `cat $outDir.rsp` $outDir", |
| 41 | Rspfile: "${outDir}.rsp", |
| 42 | RspfileContent: "$in", |
| 43 | }, |
| 44 | "outDir") |
Sasha Smundak | a76acba | 2022-04-18 20:12:56 -0700 | [diff] [blame] | 45 | |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 46 | cp = pctx.AndroidStaticRule("cp", |
| 47 | blueprint.RuleParams{ |
| 48 | Command: "rm -f $out && cp $in $out", |
| 49 | Description: "cp $out", |
| 50 | }) |
| 51 | |
Sasha Smundak | a76acba | 2022-04-18 20:12:56 -0700 | [diff] [blame] | 52 | // Cross-referencing: |
| 53 | _ = pctx.SourcePathVariable("rustExtractor", |
| 54 | "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/rust_extractor") |
| 55 | _ = pctx.VariableFunc("kytheCorpus", |
| 56 | func(ctx android.PackageVarContext) string { return ctx.Config().XrefCorpusName() }) |
| 57 | _ = pctx.VariableFunc("kytheCuEncoding", |
| 58 | func(ctx android.PackageVarContext) string { return ctx.Config().XrefCuEncoding() }) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 59 | ) |
| 60 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 61 | type buildOutput struct { |
Joel Galenson | fa04938 | 2021-01-14 16:03:18 -0800 | [diff] [blame] | 62 | outputFile android.Path |
Sasha Smundak | a76acba | 2022-04-18 20:12:56 -0700 | [diff] [blame] | 63 | kytheFile android.Path |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 64 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 65 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 66 | func init() { |
| 67 | pctx.HostBinToolVariable("SoongZipCmd", "soong_zip") |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 70 | func TransformSrcToBinary(ctx ModuleContext, c compiler, mainSrc android.Path, deps PathDeps, flags Flags, |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 71 | outputFile android.WritablePath) buildOutput { |
Pirama Arumuga Nainar | f77913f | 2021-10-25 15:37:43 -0700 | [diff] [blame] | 72 | flags.GlobalRustFlags = append(flags.GlobalRustFlags, "-C lto=thin") |
Ivan Lozano | 31b095d | 2019-11-20 10:14:33 -0800 | [diff] [blame] | 73 | |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 74 | return transformSrctoCrate(ctx, c, mainSrc, deps, flags, outputFile, "bin") |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 77 | func TransformSrctoRlib(ctx ModuleContext, c compiler, mainSrc android.Path, deps PathDeps, flags Flags, |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 78 | outputFile android.WritablePath) buildOutput { |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 79 | return transformSrctoCrate(ctx, c, mainSrc, deps, flags, outputFile, "rlib") |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 82 | func TransformSrctoDylib(ctx ModuleContext, c compiler, mainSrc android.Path, deps PathDeps, flags Flags, |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 83 | outputFile android.WritablePath) buildOutput { |
Chris Wailes | 5f78840 | 2023-03-02 16:06:01 -0800 | [diff] [blame] | 84 | flags.GlobalRustFlags = append(flags.GlobalRustFlags, "-C lto=thin") |
| 85 | |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 86 | return transformSrctoCrate(ctx, c, mainSrc, deps, flags, outputFile, "dylib") |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 89 | func TransformSrctoStatic(ctx ModuleContext, c compiler, mainSrc android.Path, deps PathDeps, flags Flags, |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 90 | outputFile android.WritablePath) buildOutput { |
Pirama Arumuga Nainar | f77913f | 2021-10-25 15:37:43 -0700 | [diff] [blame] | 91 | flags.GlobalRustFlags = append(flags.GlobalRustFlags, "-C lto=thin") |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 92 | return transformSrctoCrate(ctx, c, mainSrc, deps, flags, outputFile, "staticlib") |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 95 | func TransformSrctoShared(ctx ModuleContext, c compiler, mainSrc android.Path, deps PathDeps, flags Flags, |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 96 | outputFile android.WritablePath) buildOutput { |
Pirama Arumuga Nainar | f77913f | 2021-10-25 15:37:43 -0700 | [diff] [blame] | 97 | flags.GlobalRustFlags = append(flags.GlobalRustFlags, "-C lto=thin") |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 98 | return transformSrctoCrate(ctx, c, mainSrc, deps, flags, outputFile, "cdylib") |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 101 | func TransformSrctoProcMacro(ctx ModuleContext, c compiler, mainSrc android.Path, deps PathDeps, |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 102 | flags Flags, outputFile android.WritablePath) buildOutput { |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 103 | return transformSrctoCrate(ctx, c, mainSrc, deps, flags, outputFile, "proc-macro") |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | func rustLibsToPaths(libs RustLibraries) android.Paths { |
| 107 | var paths android.Paths |
| 108 | for _, lib := range libs { |
| 109 | paths = append(paths, lib.Path) |
| 110 | } |
| 111 | return paths |
| 112 | } |
| 113 | |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 114 | func makeLibFlags(deps PathDeps, ruleCmd *android.RuleBuilderCommand) []string { |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 115 | var libFlags []string |
| 116 | |
| 117 | // Collect library/crate flags |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 118 | for _, lib := range deps.Rlibs.ToListDirect() { |
| 119 | libPath := ruleCmd.PathForInput(lib.Path) |
| 120 | libFlags = append(libFlags, "--extern "+lib.CrateName+"="+libPath) |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 121 | } |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 122 | for _, lib := range deps.Dylibs.ToListDirect() { |
| 123 | libPath := ruleCmd.PathForInput(lib.Path) |
| 124 | libFlags = append(libFlags, "--extern "+lib.CrateName+"="+libPath) |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 125 | } |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 126 | for _, procMacro := range deps.ProcMacros.ToListDirect() { |
| 127 | procMacroPath := ruleCmd.PathForInput(procMacro.Path) |
| 128 | libFlags = append(libFlags, "--extern "+procMacro.CrateName+"="+procMacroPath) |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | for _, path := range deps.linkDirs { |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 132 | libFlags = append(libFlags, "-L "+ruleCmd.PathForInput(path)) |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | return libFlags |
| 136 | } |
| 137 | |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 138 | func collectImplicits(deps PathDeps) android.Paths { |
| 139 | depPaths := android.Paths{} |
| 140 | depPaths = append(depPaths, rustLibsToPaths(deps.Rlibs.ToList())...) |
| 141 | depPaths = append(depPaths, rustLibsToPaths(deps.Dylibs.ToList())...) |
| 142 | depPaths = append(depPaths, rustLibsToPaths(deps.ProcMacros.ToList())...) |
| 143 | depPaths = append(depPaths, deps.AfdoProfiles...) |
| 144 | depPaths = append(depPaths, deps.WholeStaticLibs...) |
| 145 | depPaths = append(depPaths, deps.SrcDeps...) |
| 146 | depPaths = append(depPaths, deps.srcProviderFiles...) |
| 147 | depPaths = append(depPaths, deps.LibDeps...) |
| 148 | depPaths = append(depPaths, deps.linkObjects...) |
| 149 | depPaths = append(depPaths, deps.BuildToolSrcDeps...) |
| 150 | return depPaths |
| 151 | } |
| 152 | |
| 153 | func rustEnvVars(ctx ModuleContext, deps PathDeps, cmd *android.RuleBuilderCommand) []string { |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 154 | var envVars []string |
| 155 | |
| 156 | // libstd requires a specific environment variable to be set. This is |
| 157 | // not officially documented and may be removed in the future. See |
| 158 | // https://github.com/rust-lang/rust/blob/master/library/std/src/env.rs#L866. |
| 159 | if ctx.RustModule().CrateName() == "std" { |
| 160 | envVars = append(envVars, "STD_ENV_ARCH="+config.StdEnvArch[ctx.RustModule().Arch().ArchType]) |
| 161 | } |
| 162 | |
| 163 | if len(deps.SrcDeps) > 0 { |
| 164 | moduleGenDir := ctx.RustModule().compiler.CargoOutDir() |
| 165 | // We must calculate an absolute path for OUT_DIR since Rust's include! macro (which normally consumes this) |
| 166 | // assumes that paths are relative to the source file. |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 167 | var outDir string |
| 168 | if filepath.IsAbs(moduleGenDir.String()) { |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 169 | // If OUT_DIR is absolute, then moduleGenDir will be an absolute path, so we don't need to set this to anything. |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 170 | outDir = moduleGenDir.String() |
| 171 | } else if moduleGenDir.Valid() { |
| 172 | // If OUT_DIR is not absolute, we use $$PWD to generate an absolute path (os.Getwd() returns '/') |
| 173 | outDir = filepath.Join("$$PWD/", cmd.PathForInput(moduleGenDir.Path())) |
| 174 | } else { |
| 175 | outDir = "$$PWD/" |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 176 | } |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 177 | envVars = append(envVars, "OUT_DIR="+outDir) |
Peter Collingbourne | 0dcd62e | 2023-03-31 23:05:16 -0700 | [diff] [blame] | 178 | } else { |
| 179 | // TODO(pcc): Change this to "OUT_DIR=" after fixing crates to not rely on this value. |
| 180 | envVars = append(envVars, "OUT_DIR=out") |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Matthew Maurer | 34609fa | 2023-06-26 21:10:13 +0000 | [diff] [blame] | 183 | envVars = append(envVars, "ANDROID_RUST_VERSION="+config.GetRustVersion(ctx)) |
| 184 | |
| 185 | if ctx.RustModule().compiler.CargoEnvCompat() { |
| 186 | if bin, ok := ctx.RustModule().compiler.(*binaryDecorator); ok { |
| 187 | envVars = append(envVars, "CARGO_BIN_NAME="+bin.getStem(ctx)) |
| 188 | } |
| 189 | envVars = append(envVars, "CARGO_CRATE_NAME="+ctx.RustModule().CrateName()) |
| 190 | envVars = append(envVars, "CARGO_PKG_NAME="+ctx.RustModule().CrateName()) |
| 191 | pkgVersion := ctx.RustModule().compiler.CargoPkgVersion() |
| 192 | if pkgVersion != "" { |
| 193 | envVars = append(envVars, "CARGO_PKG_VERSION="+pkgVersion) |
Ivan Lozano | f445562 | 2023-07-28 12:42:20 -0400 | [diff] [blame] | 194 | |
| 195 | // Ensure the version is in the form of "x.y.z" (approximately semver compliant). |
| 196 | // |
| 197 | // For our purposes, we don't care to enforce that these are integers since they may |
| 198 | // include other characters at times (e.g. sometimes the patch version is more than an integer). |
| 199 | if strings.Count(pkgVersion, ".") == 2 { |
| 200 | var semver_parts = strings.Split(pkgVersion, ".") |
| 201 | envVars = append(envVars, "CARGO_PKG_VERSION_MAJOR="+semver_parts[0]) |
| 202 | envVars = append(envVars, "CARGO_PKG_VERSION_MINOR="+semver_parts[1]) |
| 203 | envVars = append(envVars, "CARGO_PKG_VERSION_PATCH="+semver_parts[2]) |
| 204 | } |
Matthew Maurer | 34609fa | 2023-06-26 21:10:13 +0000 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
Sam Delmerico | 476e876 | 2023-09-21 06:21:21 +0000 | [diff] [blame^] | 208 | envVars = append(envVars, "AR="+cmd.PathForInput( |
| 209 | cc_config.ClangPath(ctx, "bin/llvm-ar")), |
| 210 | ) |
Matthew Maurer | 34609fa | 2023-06-26 21:10:13 +0000 | [diff] [blame] | 211 | |
A. Cody Schuffelen | f29ca58 | 2023-07-13 19:03:39 -0700 | [diff] [blame] | 212 | if ctx.Darwin() { |
| 213 | envVars = append(envVars, "ANDROID_RUST_DARWIN=true") |
| 214 | } |
| 215 | |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 216 | return envVars |
| 217 | } |
| 218 | |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 219 | func transformSrctoCrate(ctx ModuleContext, comp compiler, main android.Path, deps PathDeps, flags Flags, |
Ivan Lozano | 7b0781d | 2021-11-03 15:30:18 -0400 | [diff] [blame] | 220 | outputFile android.WritablePath, crateType string) buildOutput { |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 221 | |
| 222 | var inputs android.Paths |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 223 | var output buildOutput |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 224 | var rustcFlags, linkFlags []string |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 225 | var earlyLinkFlags []string |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 226 | |
| 227 | output.outputFile = outputFile |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 228 | crateName := ctx.RustModule().CrateName() |
ThiƩbaud Weksteen | 1f7f70f | 2020-06-24 11:32:48 +0200 | [diff] [blame] | 229 | targetTriple := ctx.toolchain().RustTriple() |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 230 | |
| 231 | inputs = append(inputs, main) |
| 232 | |
| 233 | // Collect rustc flags |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 234 | rustcFlags = append(rustcFlags, flags.GlobalRustFlags...) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 235 | rustcFlags = append(rustcFlags, flags.RustFlags...) |
Ivan Lozano | 7b0781d | 2021-11-03 15:30:18 -0400 | [diff] [blame] | 236 | rustcFlags = append(rustcFlags, "--crate-type="+crateType) |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 237 | if crateName != "" { |
| 238 | rustcFlags = append(rustcFlags, "--crate-name="+crateName) |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 239 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 240 | if targetTriple != "" { |
| 241 | rustcFlags = append(rustcFlags, "--target="+targetTriple) |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 242 | linkFlags = append(linkFlags, "-target "+targetTriple) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 243 | } |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame] | 244 | |
| 245 | // Suppress an implicit sysroot |
| 246 | rustcFlags = append(rustcFlags, "--sysroot=/dev/null") |
| 247 | |
Chris Wailes | d9781fd | 2021-12-03 17:17:28 -0800 | [diff] [blame] | 248 | // Enable incremental compilation if requested by user |
| 249 | if ctx.Config().IsEnvTrue("SOONG_RUSTC_INCREMENTAL") { |
| 250 | incrementalPath := android.PathForOutput(ctx, "rustc").String() |
Chris Wailes | 6d12db4 | 2023-02-24 16:58:18 -0800 | [diff] [blame] | 251 | rustcFlags = append(rustcFlags, "-Cincremental="+incrementalPath) |
| 252 | } |
| 253 | |
| 254 | // Disallow experimental features |
| 255 | modulePath := android.PathForModuleSrc(ctx).String() |
| 256 | if !(android.IsThirdPartyPath(modulePath) || strings.HasPrefix(modulePath, "prebuilts")) { |
Chris Wailes | 547bfdd | 2023-05-31 11:53:44 -0700 | [diff] [blame] | 257 | rustcFlags = append(rustcFlags, "-Zallow-features=\"\"") |
Chris Wailes | d9781fd | 2021-12-03 17:17:28 -0800 | [diff] [blame] | 258 | } |
| 259 | |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 260 | // Collect linker flags |
A. Cody Schuffelen | f29ca58 | 2023-07-13 19:03:39 -0700 | [diff] [blame] | 261 | if !ctx.Darwin() { |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 262 | earlyLinkFlags = append(earlyLinkFlags, "-Wl,--as-needed") |
A. Cody Schuffelen | f29ca58 | 2023-07-13 19:03:39 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 265 | // Collect dependencies |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 266 | var linkImplicits android.Paths |
| 267 | implicits := collectImplicits(deps) |
| 268 | toolImplicits := android.Concat(deps.BuildToolDeps) |
Peter Collingbourne | e7c71c3 | 2023-03-31 20:21:19 -0700 | [diff] [blame] | 269 | linkImplicits = append(linkImplicits, deps.CrtBegin...) |
| 270 | linkImplicits = append(linkImplicits, deps.CrtEnd...) |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 271 | implicits = append(implicits, comp.compilationSourcesAndData(ctx)...) |
Sam Delmerico | 51d6d1c | 2023-03-28 16:54:00 -0400 | [diff] [blame] | 272 | |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 273 | if len(deps.SrcDeps) > 0 { |
ThiƩbaud Weksteen | ee6a89b | 2021-02-25 16:30:57 +0100 | [diff] [blame] | 274 | moduleGenDir := ctx.RustModule().compiler.CargoOutDir() |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 275 | var outputs android.WritablePaths |
| 276 | |
| 277 | for _, genSrc := range deps.SrcDeps { |
Ivan Lozano | 10735d9 | 2020-07-22 09:14:47 -0400 | [diff] [blame] | 278 | if android.SuffixInList(outputs.Strings(), genSubDir+genSrc.Base()) { |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 279 | ctx.PropertyErrorf("srcs", |
| 280 | "multiple source providers generate the same filename output: "+genSrc.Base()) |
| 281 | } |
Ivan Lozano | 10735d9 | 2020-07-22 09:14:47 -0400 | [diff] [blame] | 282 | outputs = append(outputs, android.PathForModuleOut(ctx, genSubDir+genSrc.Base())) |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | ctx.Build(pctx, android.BuildParams{ |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 286 | Rule: cpDir, |
ThiƩbaud Weksteen | ee6a89b | 2021-02-25 16:30:57 +0100 | [diff] [blame] | 287 | Description: "cp " + moduleGenDir.Path().Rel(), |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 288 | Outputs: outputs, |
| 289 | Inputs: deps.SrcDeps, |
| 290 | Args: map[string]string{ |
| 291 | "outDir": moduleGenDir.String(), |
| 292 | }, |
| 293 | }) |
| 294 | implicits = append(implicits, outputs.Paths()...) |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 295 | } |
| 296 | |
Ivan Lozano | bae62be | 2020-07-21 13:28:27 -0400 | [diff] [blame] | 297 | if flags.Clippy { |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 298 | clippyRule := getRuleBuilder(ctx, pctx, false, "clippy") |
| 299 | clippyCmd := clippyRule.Command() |
Ivan Lozano | bae62be | 2020-07-21 13:28:27 -0400 | [diff] [blame] | 300 | clippyFile := android.PathForModuleOut(ctx, outputFile.Base()+".clippy") |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 301 | clippyDepInfoFile := android.PathForModuleOut(ctx, outputFile.Base()+".clippy.d.raw") |
| 302 | clippyDepFile := android.PathForModuleOut(ctx, outputFile.Base()+".clippy.d") |
| 303 | |
| 304 | clippyCmd. |
| 305 | Flags(rustEnvVars(ctx, deps, clippyCmd)). |
Sam Delmerico | 476e876 | 2023-09-21 06:21:21 +0000 | [diff] [blame^] | 306 | Tool(config.RustPath(ctx, "bin/clippy-driver")). |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 307 | Flag("--emit metadata"). |
| 308 | FlagWithOutput("-o ", clippyFile). |
| 309 | FlagWithOutput("--emit dep-info=", clippyDepInfoFile). |
| 310 | Inputs(inputs). |
| 311 | Flags(makeLibFlags(deps, clippyCmd)). |
| 312 | Flags(rustcFlags). |
| 313 | Flags(flags.ClippyFlags). |
| 314 | ImplicitTools(toolImplicits). |
| 315 | Implicits(implicits) |
| 316 | |
| 317 | depfileCreationCmd := clippyRule.Command() |
| 318 | depfileCreationCmd. |
| 319 | Flag(fmt.Sprintf( |
| 320 | `grep "^%s:" %s >`, |
| 321 | depfileCreationCmd.PathForOutput(clippyFile), |
| 322 | depfileCreationCmd.PathForOutput(clippyDepInfoFile), |
| 323 | )). |
| 324 | DepFile(clippyDepFile) |
| 325 | |
| 326 | clippyRule.BuildWithUnescapedNinjaVars("clippy", "clippy "+main.Rel()) |
| 327 | |
Ivan Lozano | bae62be | 2020-07-21 13:28:27 -0400 | [diff] [blame] | 328 | // Declare the clippy build as an implicit dependency of the original crate. |
| 329 | implicits = append(implicits, clippyFile) |
| 330 | } |
| 331 | |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 332 | sboxDirectory := "rustc" |
| 333 | rustSboxOutputFile := android.PathForModuleOut(ctx, sboxDirectory, outputFile.Base()) |
| 334 | depFile := android.PathForModuleOut(ctx, sboxDirectory, rustSboxOutputFile.Base()+".d") |
| 335 | depInfoFile := android.PathForModuleOut(ctx, sboxDirectory, rustSboxOutputFile.Base()+".d.raw") |
| 336 | var rustcImplicitOutputs android.WritablePaths |
| 337 | |
| 338 | sandboxedCompilation := comp.crateRoot(ctx) != nil |
| 339 | rustcRule := getRuleBuilder(ctx, pctx, sandboxedCompilation, sboxDirectory) |
| 340 | rustcCmd := rustcRule.Command() |
| 341 | |
| 342 | linkFlags = append(linkFlags, flags.GlobalLinkFlags...) |
| 343 | linkFlags = append(linkFlags, flags.LinkFlags...) |
| 344 | linkFlags = append(linkFlags, rustcCmd.PathsForInputs(deps.linkObjects)...) |
| 345 | |
| 346 | // Check if this module needs to use the bootstrap linker |
| 347 | if ctx.RustModule().Bootstrap() && !ctx.RustModule().InRecovery() && !ctx.RustModule().InRamdisk() && !ctx.RustModule().InVendorRamdisk() { |
| 348 | dynamicLinker := "-Wl,-dynamic-linker,/system/bin/bootstrap/linker" |
| 349 | if ctx.toolchain().Is64Bit() { |
| 350 | dynamicLinker += "64" |
| 351 | } |
| 352 | linkFlags = append(linkFlags, dynamicLinker) |
| 353 | } |
| 354 | |
| 355 | libFlags := makeLibFlags(deps, rustcCmd) |
| 356 | |
Peter Collingbourne | e7c71c3 | 2023-03-31 20:21:19 -0700 | [diff] [blame] | 357 | usesLinker := crateType == "bin" || crateType == "dylib" || crateType == "cdylib" || crateType == "proc-macro" |
| 358 | if usesLinker { |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 359 | rustSboxOutputFile = android.PathForModuleOut(ctx, sboxDirectory, rustSboxOutputFile.Base()+".rsp") |
| 360 | rustcImplicitOutputs = android.WritablePaths{ |
| 361 | android.PathForModuleOut(ctx, sboxDirectory, rustSboxOutputFile.Base()+".whole.a"), |
| 362 | android.PathForModuleOut(ctx, sboxDirectory, rustSboxOutputFile.Base()+".a"), |
| 363 | } |
Peter Collingbourne | e7c71c3 | 2023-03-31 20:21:19 -0700 | [diff] [blame] | 364 | } |
| 365 | |
Sam Delmerico | 476e876 | 2023-09-21 06:21:21 +0000 | [diff] [blame^] | 366 | clangTools := android.Paths{ |
| 367 | cc_config.ClangPath(ctx, "bin/llvm-ar"), |
| 368 | } |
| 369 | if ctx.Config().BuildOS != android.Darwin { |
| 370 | clangTools = append(clangTools, |
| 371 | cc_config.ClangPath(ctx, "lib/libc++.so"), |
| 372 | ) |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 373 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 374 | |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 375 | rustcCmd. |
| 376 | Flags(rustEnvVars(ctx, deps, rustcCmd)). |
Sam Delmerico | 476e876 | 2023-09-21 06:21:21 +0000 | [diff] [blame^] | 377 | Tool(config.RustPath(ctx, "bin/rustc")). |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 378 | FlagWithInput("-C linker=", android.PathForSource(ctx, "build", "soong", "scripts", "mkcratersp.py")). |
| 379 | Flag("--emit link"). |
| 380 | Flag("-o"). |
| 381 | Output(rustSboxOutputFile). |
| 382 | FlagWithOutput("--emit dep-info=", depInfoFile). |
| 383 | Inputs(inputs). |
| 384 | Flags(libFlags). |
Sam Delmerico | 476e876 | 2023-09-21 06:21:21 +0000 | [diff] [blame^] | 385 | Implicits(clangTools). |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 386 | ImplicitTools(toolImplicits). |
| 387 | Implicits(implicits). |
| 388 | Flags(rustcFlags). |
| 389 | ImplicitOutputs(rustcImplicitOutputs) |
| 390 | |
| 391 | depfileCreationCmd := rustcRule.Command() |
| 392 | depfileCreationCmd. |
| 393 | Flag(fmt.Sprintf( |
| 394 | `grep "^%s:" %s >`, |
| 395 | depfileCreationCmd.PathForOutput(rustSboxOutputFile), |
| 396 | depfileCreationCmd.PathForOutput(depInfoFile), |
| 397 | )). |
| 398 | DepFile(depFile) |
| 399 | |
| 400 | if !usesLinker { |
Peter Collingbourne | e7c71c3 | 2023-03-31 20:21:19 -0700 | [diff] [blame] | 401 | ctx.Build(pctx, android.BuildParams{ |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 402 | Rule: cp, |
| 403 | Input: rustSboxOutputFile, |
| 404 | Output: outputFile, |
| 405 | }) |
| 406 | } else { |
| 407 | // TODO: delmerico - separate rustLink into its own rule |
| 408 | // mkcratersp.py hardcodes paths to files within the sandbox, so |
| 409 | // those need to be renamed/symlinked to something in the rustLink sandbox |
| 410 | // if we want to separate the rules |
| 411 | linkerSboxOutputFile := android.PathForModuleOut(ctx, sboxDirectory, outputFile.Base()) |
Sam Delmerico | 476e876 | 2023-09-21 06:21:21 +0000 | [diff] [blame^] | 412 | clangTools := android.Paths{ |
| 413 | cc_config.ClangPath(ctx, "bin/clang++"), |
| 414 | cc_config.ClangPath(ctx, "bin/lld"), |
| 415 | cc_config.ClangPath(ctx, "bin/ld.lld"), |
| 416 | } |
| 417 | if ctx.Config().BuildOS != android.Darwin { |
| 418 | clangTools = append(clangTools, |
| 419 | cc_config.ClangPath(ctx, "bin/clang++.real"), |
| 420 | cc_config.ClangPath(ctx, "lib/libc++.so"), |
| 421 | ) |
| 422 | } |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 423 | rustLinkCmd := rustcRule.Command() |
| 424 | rustLinkCmd. |
Sam Delmerico | 476e876 | 2023-09-21 06:21:21 +0000 | [diff] [blame^] | 425 | Tool(cc_config.ClangPath(ctx, "bin/clang++")). |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 426 | Flag("-o"). |
| 427 | Output(linkerSboxOutputFile). |
| 428 | Inputs(deps.CrtBegin). |
| 429 | Flags(earlyLinkFlags). |
| 430 | FlagWithInput("@", rustSboxOutputFile). |
| 431 | Flags(linkFlags). |
| 432 | Inputs(deps.CrtEnd). |
Sam Delmerico | 476e876 | 2023-09-21 06:21:21 +0000 | [diff] [blame^] | 433 | ImplicitTools(clangTools). |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 434 | ImplicitTools(toolImplicits). |
| 435 | Implicits(rustcImplicitOutputs.Paths()). |
| 436 | Implicits(implicits). |
| 437 | Implicits(linkImplicits) |
| 438 | ctx.Build(pctx, android.BuildParams{ |
| 439 | Rule: cp, |
| 440 | Input: linkerSboxOutputFile, |
| 441 | Output: outputFile, |
Peter Collingbourne | e7c71c3 | 2023-03-31 20:21:19 -0700 | [diff] [blame] | 442 | }) |
| 443 | } |
| 444 | |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 445 | rustcRule.BuildWithUnescapedNinjaVars("rustc", "rustc "+main.Rel()) |
| 446 | |
Sasha Smundak | a76acba | 2022-04-18 20:12:56 -0700 | [diff] [blame] | 447 | if flags.EmitXrefs { |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 448 | kytheRule := getRuleBuilder(ctx, pctx, false, "kythe") |
| 449 | kytheCmd := kytheRule.Command() |
Sasha Smundak | a76acba | 2022-04-18 20:12:56 -0700 | [diff] [blame] | 450 | kytheFile := android.PathForModuleOut(ctx, outputFile.Base()+".kzip") |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 451 | kytheCmd. |
| 452 | Flag("KYTHE_CORPUS=${kytheCorpus}"). |
| 453 | FlagWithOutput("KYTHE_OUTPUT_FILE=", kytheFile). |
| 454 | FlagWithInput("KYTHE_VNAMES=", android.PathForSource(ctx, "build", "soong", "vnames.json")). |
| 455 | Flag("KYTHE_KZIP_ENCODING=${kytheCuEncoding}"). |
| 456 | Flag("KYTHE_CANONICALIZE_VNAME_PATHS=prefer-relative"). |
| 457 | Tool(ctx.Config().PrebuiltBuildTool(ctx, "rust_extractor")). |
| 458 | Flags(rustEnvVars(ctx, deps, kytheCmd)). |
Sam Delmerico | 476e876 | 2023-09-21 06:21:21 +0000 | [diff] [blame^] | 459 | Tool(config.RustPath(ctx, "bin/rustc")). |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 460 | Flag("-C linker=true"). |
| 461 | Inputs(inputs). |
| 462 | Flags(makeLibFlags(deps, kytheCmd)). |
| 463 | Flags(rustcFlags). |
| 464 | ImplicitTools(toolImplicits). |
| 465 | Implicits(implicits) |
| 466 | kytheRule.BuildWithUnescapedNinjaVars("kythe", "Xref Rust extractor "+main.Rel()) |
Sasha Smundak | a76acba | 2022-04-18 20:12:56 -0700 | [diff] [blame] | 467 | output.kytheFile = kytheFile |
| 468 | } |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 469 | return output |
| 470 | } |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 471 | |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 472 | func Rustdoc(ctx ModuleContext, main android.Path, deps PathDeps, flags Flags) android.ModuleOutPath { |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 473 | rustdocRule := getRuleBuilder(ctx, pctx, false, "rustdoc") |
| 474 | rustdocCmd := rustdocRule.Command() |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 475 | |
| 476 | rustdocFlags := append([]string{}, flags.RustdocFlags...) |
| 477 | rustdocFlags = append(rustdocFlags, "--sysroot=/dev/null") |
| 478 | |
Dan Albert | b433bf7 | 2021-04-27 17:12:02 -0700 | [diff] [blame] | 479 | // Build an index for all our crates. -Z unstable options is required to use |
| 480 | // this flag. |
| 481 | rustdocFlags = append(rustdocFlags, "-Z", "unstable-options", "--enable-index-page") |
| 482 | |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 483 | targetTriple := ctx.toolchain().RustTriple() |
| 484 | |
| 485 | // Collect rustc flags |
| 486 | if targetTriple != "" { |
| 487 | rustdocFlags = append(rustdocFlags, "--target="+targetTriple) |
| 488 | } |
| 489 | |
| 490 | crateName := ctx.RustModule().CrateName() |
Dan Albert | b433bf7 | 2021-04-27 17:12:02 -0700 | [diff] [blame] | 491 | rustdocFlags = append(rustdocFlags, "--crate-name "+crateName) |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 492 | |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 493 | rustdocFlags = append(rustdocFlags, makeLibFlags(deps, rustdocCmd)...) |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 494 | docTimestampFile := android.PathForModuleOut(ctx, "rustdoc.timestamp") |
Dan Albert | b433bf7 | 2021-04-27 17:12:02 -0700 | [diff] [blame] | 495 | |
Chris Wailes | b2703ad | 2021-07-30 13:25:42 -0700 | [diff] [blame] | 496 | // Silence warnings about renamed lints for third-party crates |
| 497 | modulePath := android.PathForModuleSrc(ctx).String() |
| 498 | if android.IsThirdPartyPath(modulePath) { |
Chris Wailes | 7b3eb24 | 2023-02-14 16:09:49 -0800 | [diff] [blame] | 499 | rustdocFlags = append(rustdocFlags, " -A warnings") |
Chris Wailes | b2703ad | 2021-07-30 13:25:42 -0700 | [diff] [blame] | 500 | } |
Chris Wailes | 9953a19 | 2021-07-28 12:07:16 -0700 | [diff] [blame] | 501 | |
Dan Albert | b433bf7 | 2021-04-27 17:12:02 -0700 | [diff] [blame] | 502 | // Yes, the same out directory is used simultaneously by all rustdoc builds. |
| 503 | // This is what cargo does. The docs for individual crates get generated to |
| 504 | // a subdirectory named for the crate, and rustdoc synchronizes writes to |
| 505 | // shared pieces like the index and search data itself. |
| 506 | // https://github.com/rust-lang/rust/blob/master/src/librustdoc/html/render/write_shared.rs#L144-L146 |
| 507 | docDir := android.PathForOutput(ctx, "rustdoc") |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 508 | |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 509 | rustdocCmd. |
| 510 | Flags(rustEnvVars(ctx, deps, rustdocCmd)). |
Sam Delmerico | 476e876 | 2023-09-21 06:21:21 +0000 | [diff] [blame^] | 511 | Tool(config.RustPath(ctx, "bin/rustdoc")). |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 512 | Flags(rustdocFlags). |
| 513 | Input(main). |
| 514 | Flag("-o "+docDir.String()). |
| 515 | FlagWithOutput("&& touch ", docTimestampFile). |
| 516 | Implicit(ctx.RustModule().UnstrippedOutputFile()) |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 517 | |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 518 | rustdocRule.BuildWithUnescapedNinjaVars("rustdoc", "rustdoc "+main.Rel()) |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 519 | return docTimestampFile |
| 520 | } |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame] | 521 | |
| 522 | func getRuleBuilder(ctx android.ModuleContext, pctx android.PackageContext, sbox bool, sboxDirectory string) *android.RuleBuilder { |
| 523 | r := android.NewRuleBuilder(pctx, ctx) |
| 524 | if sbox { |
| 525 | r = r.Sbox( |
| 526 | android.PathForModuleOut(ctx, sboxDirectory), |
| 527 | android.PathForModuleOut(ctx, sboxDirectory+".sbox.textproto"), |
| 528 | ).SandboxInputs() |
| 529 | } |
| 530 | return r |
| 531 | } |