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 ( |
| 28 | _ = pctx.SourcePathVariable("rustcCmd", "${config.RustBin}/rustc") |
| 29 | rustc = pctx.AndroidStaticRule("rustc", |
| 30 | blueprint.RuleParams{ |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 31 | Command: "$envVars $rustcCmd " + |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 32 | "-C linker=${config.RustLinker} " + |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 33 | "-C link-args=\"${crtBegin} ${config.RustLinkerArgs} ${linkFlags} ${crtEnd}\" " + |
Chih-Hung Hsieh | 29aa9fd | 2020-08-13 15:46:21 -0700 | [diff] [blame] | 34 | "--emit link -o $out --emit dep-info=$out.d.raw $in ${libFlags} $rustcFlags" + |
| 35 | " && grep \"^$out:\" $out.d.raw > $out.d", |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 36 | CommandDeps: []string{"$rustcCmd"}, |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 37 | // Rustc deps-info writes out make compatible dep files: https://github.com/rust-lang/rust/issues/7633 |
Chih-Hung Hsieh | 29aa9fd | 2020-08-13 15:46:21 -0700 | [diff] [blame] | 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. |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 42 | Deps: blueprint.DepsGCC, |
| 43 | Depfile: "$out.d", |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 44 | }, |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 45 | "rustcFlags", "linkFlags", "libFlags", "crtBegin", "crtEnd", "envVars") |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 46 | |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 47 | _ = pctx.SourcePathVariable("rustdocCmd", "${config.RustBin}/rustdoc") |
| 48 | rustdoc = pctx.AndroidStaticRule("rustdoc", |
| 49 | blueprint.RuleParams{ |
Dan Albert | b433bf7 | 2021-04-27 17:12:02 -0700 | [diff] [blame] | 50 | Command: "$envVars $rustdocCmd $rustdocFlags $in -o $outDir && " + |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 51 | "touch $out", |
| 52 | CommandDeps: []string{"$rustdocCmd"}, |
| 53 | }, |
| 54 | "rustdocFlags", "outDir", "envVars") |
| 55 | |
Thiébaud Weksteen | 92f703b | 2020-06-22 13:28:02 +0200 | [diff] [blame] | 56 | _ = pctx.SourcePathVariable("clippyCmd", "${config.RustBin}/clippy-driver") |
| 57 | clippyDriver = pctx.AndroidStaticRule("clippy", |
| 58 | blueprint.RuleParams{ |
Ivan Lozano | bae62be | 2020-07-21 13:28:27 -0400 | [diff] [blame] | 59 | Command: "$envVars $clippyCmd " + |
Thiébaud Weksteen | 92f703b | 2020-06-22 13:28:02 +0200 | [diff] [blame] | 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. |
Thiébaud Weksteen | 94c8325 | 2021-04-07 16:00:19 +0200 | [diff] [blame] | 62 | "--emit metadata -o $out --emit dep-info=$out.d.raw $in ${libFlags} " + |
| 63 | "$rustcFlags $clippyFlags" + |
| 64 | " && grep \"^$out:\" $out.d.raw > $out.d", |
Thiébaud Weksteen | 92f703b | 2020-06-22 13:28:02 +0200 | [diff] [blame] | 65 | CommandDeps: []string{"$clippyCmd"}, |
Thiébaud Weksteen | 94c8325 | 2021-04-07 16:00:19 +0200 | [diff] [blame] | 66 | Deps: blueprint.DepsGCC, |
| 67 | Depfile: "$out.d", |
Thiébaud Weksteen | 92f703b | 2020-06-22 13:28:02 +0200 | [diff] [blame] | 68 | }, |
Ivan Lozano | bae62be | 2020-07-21 13:28:27 -0400 | [diff] [blame] | 69 | "rustcFlags", "libFlags", "clippyFlags", "envVars") |
Thiébaud Weksteen | 92f703b | 2020-06-22 13:28:02 +0200 | [diff] [blame] | 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 | |
| 79 | cp = pctx.AndroidStaticRule("cp", |
| 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() }) |
| 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 ` + |
| 104 | `-C linker=${config.RustLinker} ` + |
| 105 | `-C link-args="${crtBegin} ${config.RustLinkerArgs} ${linkFlags} ${crtEnd}" ` + |
| 106 | `$in ${libFlags} $rustcFlags`, |
| 107 | CommandDeps: []string{"$rustExtractor", "$kytheVnames"}, |
| 108 | Rspfile: "${out}.rsp", |
| 109 | RspfileContent: "$in", |
| 110 | }, |
| 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 | |
Thiébaud Weksteen | 1f7f70f | 2020-06-24 11:32:48 +0200 | [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 { |
Pirama Arumuga Nainar | f77913f | 2021-10-25 15:37:43 -0700 | [diff] [blame] | 125 | flags.GlobalRustFlags = append(flags.GlobalRustFlags, "-C lto=thin") |
Ivan Lozano | 31b095d | 2019-11-20 10:14:33 -0800 | [diff] [blame] | 126 | |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [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 | |
Thiébaud Weksteen | 1f7f70f | 2020-06-24 11:32:48 +0200 | [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 { |
| 132 | return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "rlib") |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Thiébaud Weksteen | 1f7f70f | 2020-06-24 11:32:48 +0200 | [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 | 5f78840 | 2023-03-02 16:06:01 -0800 | [diff] [blame] | 137 | flags.GlobalRustFlags = append(flags.GlobalRustFlags, "-C lto=thin") |
| 138 | |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [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 | |
Thiébaud Weksteen | 1f7f70f | 2020-06-24 11:32:48 +0200 | [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 { |
Pirama Arumuga Nainar | f77913f | 2021-10-25 15:37:43 -0700 | [diff] [blame] | 144 | flags.GlobalRustFlags = append(flags.GlobalRustFlags, "-C lto=thin") |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [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 | |
Thiébaud Weksteen | 1f7f70f | 2020-06-24 11:32:48 +0200 | [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 { |
Pirama Arumuga Nainar | f77913f | 2021-10-25 15:37:43 -0700 | [diff] [blame] | 150 | flags.GlobalRustFlags = append(flags.GlobalRustFlags, "-C lto=thin") |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [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 | |
Thiébaud Weksteen | 1f7f70f | 2020-06-24 11:32:48 +0200 | [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 { |
| 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 | |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 167 | func makeLibFlags(deps PathDeps) []string { |
| 168 | var libFlags []string |
| 169 | |
| 170 | // Collect library/crate flags |
| 171 | for _, lib := range deps.RLibs { |
| 172 | libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String()) |
| 173 | } |
| 174 | for _, lib := range deps.DyLibs { |
| 175 | libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String()) |
| 176 | } |
| 177 | for _, proc_macro := range deps.ProcMacros { |
| 178 | libFlags = append(libFlags, "--extern "+proc_macro.CrateName+"="+proc_macro.Path.String()) |
| 179 | } |
| 180 | |
| 181 | for _, path := range deps.linkDirs { |
| 182 | libFlags = append(libFlags, "-L "+path) |
| 183 | } |
| 184 | |
| 185 | return libFlags |
| 186 | } |
| 187 | |
| 188 | func rustEnvVars(ctx ModuleContext, deps PathDeps) []string { |
| 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 { |
| 199 | moduleGenDir := ctx.RustModule().compiler.CargoOutDir() |
| 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. |
| 202 | var outDirPrefix string |
| 203 | if !filepath.IsAbs(moduleGenDir.String()) { |
| 204 | // If OUT_DIR is not absolute, we use $$PWD to generate an absolute path (os.Getwd() returns '/') |
| 205 | outDirPrefix = "$$PWD/" |
| 206 | } else { |
| 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 = "" |
| 209 | } |
| 210 | envVars = append(envVars, "OUT_DIR="+filepath.Join(outDirPrefix, moduleGenDir.String())) |
| 211 | } |
| 212 | |
| 213 | return envVars |
| 214 | } |
| 215 | |
Thiébaud Weksteen | 1f7f70f | 2020-06-24 11:32:48 +0200 | [diff] [blame] | 216 | func transformSrctoCrate(ctx ModuleContext, main android.Path, deps PathDeps, flags Flags, |
Ivan Lozano | 7b0781d | 2021-11-03 15:30:18 -0400 | [diff] [blame] | 217 | outputFile android.WritablePath, crateType string) buildOutput { |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 218 | |
| 219 | var inputs android.Paths |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 220 | var implicits android.Paths |
Sam Delmerico | 51d6d1c | 2023-03-28 16:54:00 -0400 | [diff] [blame^] | 221 | var orderOnly android.Paths |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 222 | var output buildOutput |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 223 | var rustcFlags, linkFlags []string |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 224 | var implicitOutputs android.WritablePaths |
| 225 | |
| 226 | output.outputFile = outputFile |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 227 | crateName := ctx.RustModule().CrateName() |
Thiébaud Weksteen | 1f7f70f | 2020-06-24 11:32:48 +0200 | [diff] [blame] | 228 | targetTriple := ctx.toolchain().RustTriple() |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 229 | |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 230 | envVars := rustEnvVars(ctx, deps) |
Thiébaud Weksteen | 71512f3 | 2020-11-03 15:17:51 +0100 | [diff] [blame] | 231 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 232 | inputs = append(inputs, main) |
| 233 | |
| 234 | // Collect rustc flags |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 235 | rustcFlags = append(rustcFlags, flags.GlobalRustFlags...) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 236 | rustcFlags = append(rustcFlags, flags.RustFlags...) |
Ivan Lozano | 7b0781d | 2021-11-03 15:30:18 -0400 | [diff] [blame] | 237 | rustcFlags = append(rustcFlags, "--crate-type="+crateType) |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 238 | if crateName != "" { |
| 239 | rustcFlags = append(rustcFlags, "--crate-name="+crateName) |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 240 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 241 | if targetTriple != "" { |
| 242 | rustcFlags = append(rustcFlags, "--target="+targetTriple) |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 243 | linkFlags = append(linkFlags, "-target "+targetTriple) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 244 | } |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame] | 245 | |
| 246 | // Suppress an implicit sysroot |
| 247 | rustcFlags = append(rustcFlags, "--sysroot=/dev/null") |
| 248 | |
Chris Wailes | d9781fd | 2021-12-03 17:17:28 -0800 | [diff] [blame] | 249 | // Enable incremental compilation if requested by user |
| 250 | if ctx.Config().IsEnvTrue("SOONG_RUSTC_INCREMENTAL") { |
| 251 | incrementalPath := android.PathForOutput(ctx, "rustc").String() |
| 252 | |
Chris Wailes | 6d12db4 | 2023-02-24 16:58:18 -0800 | [diff] [blame] | 253 | rustcFlags = append(rustcFlags, "-Cincremental="+incrementalPath) |
| 254 | } |
| 255 | |
| 256 | // Disallow experimental features |
| 257 | modulePath := android.PathForModuleSrc(ctx).String() |
| 258 | if !(android.IsThirdPartyPath(modulePath) || strings.HasPrefix(modulePath, "prebuilts")) { |
Chris Wailes | 7d6e843 | 2023-03-06 12:27:47 -0800 | [diff] [blame] | 259 | rustcFlags = append(rustcFlags, "-Zallow-features=\"default_alloc_error_handler,custom_inner_attributes,mixed_integer_ops\"") |
Chris Wailes | d9781fd | 2021-12-03 17:17:28 -0800 | [diff] [blame] | 260 | } |
| 261 | |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 262 | // Collect linker flags |
| 263 | linkFlags = append(linkFlags, flags.GlobalLinkFlags...) |
| 264 | linkFlags = append(linkFlags, flags.LinkFlags...) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 265 | |
Ivan Lozano | a226863 | 2021-07-22 10:52:06 -0400 | [diff] [blame] | 266 | // Check if this module needs to use the bootstrap linker |
| 267 | if ctx.RustModule().Bootstrap() && !ctx.RustModule().InRecovery() && !ctx.RustModule().InRamdisk() && !ctx.RustModule().InVendorRamdisk() { |
| 268 | dynamicLinker := "-Wl,-dynamic-linker,/system/bin/bootstrap/linker" |
| 269 | if ctx.toolchain().Is64Bit() { |
| 270 | dynamicLinker += "64" |
| 271 | } |
| 272 | linkFlags = append(linkFlags, dynamicLinker) |
| 273 | } |
| 274 | |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 275 | libFlags := makeLibFlags(deps) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 276 | |
| 277 | // Collect dependencies |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 278 | implicits = append(implicits, rustLibsToPaths(deps.RLibs)...) |
| 279 | implicits = append(implicits, rustLibsToPaths(deps.DyLibs)...) |
| 280 | implicits = append(implicits, rustLibsToPaths(deps.ProcMacros)...) |
| 281 | implicits = append(implicits, deps.StaticLibs...) |
Ivan Lozano | ec6e991 | 2021-01-21 15:23:29 -0500 | [diff] [blame] | 282 | implicits = append(implicits, deps.SharedLibDeps...) |
Ivan Lozano | 9d74a52 | 2020-12-01 09:25:22 -0500 | [diff] [blame] | 283 | implicits = append(implicits, deps.srcProviderFiles...) |
Yi Kong | 46c6e59 | 2022-01-20 22:55:00 +0800 | [diff] [blame] | 284 | implicits = append(implicits, deps.AfdoProfiles...) |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 285 | |
Colin Cross | fe605e1 | 2022-01-23 20:46:16 -0800 | [diff] [blame] | 286 | implicits = append(implicits, deps.CrtBegin...) |
| 287 | implicits = append(implicits, deps.CrtEnd...) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 288 | |
Sam Delmerico | 51d6d1c | 2023-03-28 16:54:00 -0400 | [diff] [blame^] | 289 | orderOnly = append(orderOnly, deps.SharedLibs...) |
| 290 | |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 291 | if len(deps.SrcDeps) > 0 { |
Thiébaud Weksteen | ee6a89b | 2021-02-25 16:30:57 +0100 | [diff] [blame] | 292 | moduleGenDir := ctx.RustModule().compiler.CargoOutDir() |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 293 | var outputs android.WritablePaths |
| 294 | |
| 295 | for _, genSrc := range deps.SrcDeps { |
Ivan Lozano | 10735d9 | 2020-07-22 09:14:47 -0400 | [diff] [blame] | 296 | if android.SuffixInList(outputs.Strings(), genSubDir+genSrc.Base()) { |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 297 | ctx.PropertyErrorf("srcs", |
| 298 | "multiple source providers generate the same filename output: "+genSrc.Base()) |
| 299 | } |
Ivan Lozano | 10735d9 | 2020-07-22 09:14:47 -0400 | [diff] [blame] | 300 | outputs = append(outputs, android.PathForModuleOut(ctx, genSubDir+genSrc.Base())) |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | ctx.Build(pctx, android.BuildParams{ |
| 304 | Rule: cp, |
Thiébaud Weksteen | ee6a89b | 2021-02-25 16:30:57 +0100 | [diff] [blame] | 305 | Description: "cp " + moduleGenDir.Path().Rel(), |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 306 | Outputs: outputs, |
| 307 | Inputs: deps.SrcDeps, |
| 308 | Args: map[string]string{ |
| 309 | "outDir": moduleGenDir.String(), |
| 310 | }, |
| 311 | }) |
| 312 | implicits = append(implicits, outputs.Paths()...) |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 313 | } |
| 314 | |
Ivan Lozano | 6d14ed1 | 2022-04-12 10:29:43 -0400 | [diff] [blame] | 315 | envVars = append(envVars, "ANDROID_RUST_VERSION="+config.GetRustVersion(ctx)) |
Thiébaud Weksteen | 9997ea7 | 2021-02-22 10:52:22 +0100 | [diff] [blame] | 316 | |
Ivan Lozano | a9a1fc0 | 2021-08-11 15:13:43 -0400 | [diff] [blame] | 317 | if ctx.RustModule().compiler.CargoEnvCompat() { |
| 318 | if _, ok := ctx.RustModule().compiler.(*binaryDecorator); ok { |
| 319 | envVars = append(envVars, "CARGO_BIN_NAME="+strings.TrimSuffix(outputFile.Base(), outputFile.Ext())) |
| 320 | } |
| 321 | envVars = append(envVars, "CARGO_CRATE_NAME="+ctx.RustModule().CrateName()) |
| 322 | pkgVersion := ctx.RustModule().compiler.CargoPkgVersion() |
| 323 | if pkgVersion != "" { |
| 324 | envVars = append(envVars, "CARGO_PKG_VERSION="+pkgVersion) |
| 325 | } |
| 326 | } |
| 327 | |
Ivan Lozano | bae62be | 2020-07-21 13:28:27 -0400 | [diff] [blame] | 328 | if flags.Clippy { |
| 329 | clippyFile := android.PathForModuleOut(ctx, outputFile.Base()+".clippy") |
| 330 | ctx.Build(pctx, android.BuildParams{ |
| 331 | Rule: clippyDriver, |
| 332 | Description: "clippy " + main.Rel(), |
| 333 | Output: clippyFile, |
| 334 | ImplicitOutputs: nil, |
| 335 | Inputs: inputs, |
| 336 | Implicits: implicits, |
Sam Delmerico | 51d6d1c | 2023-03-28 16:54:00 -0400 | [diff] [blame^] | 337 | OrderOnly: orderOnly, |
Ivan Lozano | bae62be | 2020-07-21 13:28:27 -0400 | [diff] [blame] | 338 | Args: map[string]string{ |
| 339 | "rustcFlags": strings.Join(rustcFlags, " "), |
| 340 | "libFlags": strings.Join(libFlags, " "), |
| 341 | "clippyFlags": strings.Join(flags.ClippyFlags, " "), |
| 342 | "envVars": strings.Join(envVars, " "), |
| 343 | }, |
| 344 | }) |
| 345 | // Declare the clippy build as an implicit dependency of the original crate. |
| 346 | implicits = append(implicits, clippyFile) |
| 347 | } |
| 348 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 349 | ctx.Build(pctx, android.BuildParams{ |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 350 | Rule: rustc, |
| 351 | Description: "rustc " + main.Rel(), |
| 352 | Output: outputFile, |
| 353 | ImplicitOutputs: implicitOutputs, |
| 354 | Inputs: inputs, |
| 355 | Implicits: implicits, |
Sam Delmerico | 51d6d1c | 2023-03-28 16:54:00 -0400 | [diff] [blame^] | 356 | OrderOnly: orderOnly, |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 357 | Args: map[string]string{ |
| 358 | "rustcFlags": strings.Join(rustcFlags, " "), |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 359 | "linkFlags": strings.Join(linkFlags, " "), |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 360 | "libFlags": strings.Join(libFlags, " "), |
Colin Cross | fe605e1 | 2022-01-23 20:46:16 -0800 | [diff] [blame] | 361 | "crtBegin": strings.Join(deps.CrtBegin.Strings(), " "), |
| 362 | "crtEnd": strings.Join(deps.CrtEnd.Strings(), " "), |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 363 | "envVars": strings.Join(envVars, " "), |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 364 | }, |
| 365 | }) |
| 366 | |
Sasha Smundak | a76acba | 2022-04-18 20:12:56 -0700 | [diff] [blame] | 367 | if flags.EmitXrefs { |
| 368 | kytheFile := android.PathForModuleOut(ctx, outputFile.Base()+".kzip") |
| 369 | ctx.Build(pctx, android.BuildParams{ |
| 370 | Rule: kytheExtract, |
| 371 | Description: "Xref Rust extractor " + main.Rel(), |
| 372 | Output: kytheFile, |
| 373 | Inputs: inputs, |
| 374 | Implicits: implicits, |
Sam Delmerico | 51d6d1c | 2023-03-28 16:54:00 -0400 | [diff] [blame^] | 375 | OrderOnly: orderOnly, |
Sasha Smundak | a76acba | 2022-04-18 20:12:56 -0700 | [diff] [blame] | 376 | Args: map[string]string{ |
| 377 | "rustcFlags": strings.Join(rustcFlags, " "), |
| 378 | "linkFlags": strings.Join(linkFlags, " "), |
| 379 | "libFlags": strings.Join(libFlags, " "), |
| 380 | "crtBegin": strings.Join(deps.CrtBegin.Strings(), " "), |
| 381 | "crtEnd": strings.Join(deps.CrtEnd.Strings(), " "), |
| 382 | "envVars": strings.Join(envVars, " "), |
| 383 | }, |
| 384 | }) |
| 385 | output.kytheFile = kytheFile |
| 386 | } |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 387 | return output |
| 388 | } |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 389 | |
| 390 | func Rustdoc(ctx ModuleContext, main android.Path, deps PathDeps, |
| 391 | flags Flags) android.ModuleOutPath { |
| 392 | |
| 393 | rustdocFlags := append([]string{}, flags.RustdocFlags...) |
| 394 | rustdocFlags = append(rustdocFlags, "--sysroot=/dev/null") |
| 395 | |
Dan Albert | b433bf7 | 2021-04-27 17:12:02 -0700 | [diff] [blame] | 396 | // Build an index for all our crates. -Z unstable options is required to use |
| 397 | // this flag. |
| 398 | rustdocFlags = append(rustdocFlags, "-Z", "unstable-options", "--enable-index-page") |
| 399 | |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 400 | targetTriple := ctx.toolchain().RustTriple() |
| 401 | |
| 402 | // Collect rustc flags |
| 403 | if targetTriple != "" { |
| 404 | rustdocFlags = append(rustdocFlags, "--target="+targetTriple) |
| 405 | } |
| 406 | |
| 407 | crateName := ctx.RustModule().CrateName() |
Dan Albert | b433bf7 | 2021-04-27 17:12:02 -0700 | [diff] [blame] | 408 | rustdocFlags = append(rustdocFlags, "--crate-name "+crateName) |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 409 | |
| 410 | rustdocFlags = append(rustdocFlags, makeLibFlags(deps)...) |
| 411 | docTimestampFile := android.PathForModuleOut(ctx, "rustdoc.timestamp") |
Dan Albert | b433bf7 | 2021-04-27 17:12:02 -0700 | [diff] [blame] | 412 | |
Chris Wailes | b2703ad | 2021-07-30 13:25:42 -0700 | [diff] [blame] | 413 | // Silence warnings about renamed lints for third-party crates |
| 414 | modulePath := android.PathForModuleSrc(ctx).String() |
| 415 | if android.IsThirdPartyPath(modulePath) { |
Chris Wailes | 7b3eb24 | 2023-02-14 16:09:49 -0800 | [diff] [blame] | 416 | rustdocFlags = append(rustdocFlags, " -A warnings") |
Chris Wailes | b2703ad | 2021-07-30 13:25:42 -0700 | [diff] [blame] | 417 | } |
Chris Wailes | 9953a19 | 2021-07-28 12:07:16 -0700 | [diff] [blame] | 418 | |
Dan Albert | b433bf7 | 2021-04-27 17:12:02 -0700 | [diff] [blame] | 419 | // Yes, the same out directory is used simultaneously by all rustdoc builds. |
| 420 | // This is what cargo does. The docs for individual crates get generated to |
| 421 | // a subdirectory named for the crate, and rustdoc synchronizes writes to |
| 422 | // shared pieces like the index and search data itself. |
| 423 | // https://github.com/rust-lang/rust/blob/master/src/librustdoc/html/render/write_shared.rs#L144-L146 |
| 424 | docDir := android.PathForOutput(ctx, "rustdoc") |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 425 | |
| 426 | ctx.Build(pctx, android.BuildParams{ |
| 427 | Rule: rustdoc, |
| 428 | Description: "rustdoc " + main.Rel(), |
| 429 | Output: docTimestampFile, |
| 430 | Input: main, |
Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 431 | Implicit: ctx.RustModule().UnstrippedOutputFile(), |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 432 | Args: map[string]string{ |
| 433 | "rustdocFlags": strings.Join(rustdocFlags, " "), |
| 434 | "outDir": docDir.String(), |
| 435 | "envVars": strings.Join(rustEnvVars(ctx, deps), " "), |
| 436 | }, |
| 437 | }) |
| 438 | |
| 439 | return docTimestampFile |
| 440 | } |