rustc_plugin/
cli.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
use std::{
  env, fs,
  path::PathBuf,
  process::{exit, Command, Stdio},
};

use cargo_metadata::camino::Utf8Path;

use super::plugin::{RustcPlugin, PLUGIN_ARGS};
use crate::CrateFilter;

pub const RUN_ON_ALL_CRATES: &str = "RUSTC_PLUGIN_ALL_TARGETS";
pub const SPECIFIC_CRATE: &str = "SPECIFIC_CRATE";
pub const SPECIFIC_TARGET: &str = "SPECIFIC_TARGET";
pub const CARGO_VERBOSE: &str = "CARGO_VERBOSE";
pub const CARGO_ENCODED_RUSTFLAGS: &str = "CARGO_ENCODED_RUSTFLAGS";

fn prior_rustflags() -> Result<Vec<String>, std::env::VarError> {
  use std::env::{var, VarError};
  var(CARGO_ENCODED_RUSTFLAGS)
    .map(|flags| flags.split('\x1f').map(str::to_string).collect())
    .or_else(|err| {
      if matches!(err, VarError::NotPresent) {
        var("RUSTFLAGS")
          .map(|flags| flags.split_whitespace().map(str::to_string).collect())
      } else {
        Err(err)
      }
    })
    .or_else(|err| {
      matches!(err, VarError::NotPresent)
        .then(Vec::new)
        .ok_or(err)
    })
}

/// The top-level function that should be called in your user-facing binary.
pub fn cli_main<T: RustcPlugin>(plugin: T) {
  if env::args().any(|arg| arg == "-V") {
    println!("{}", plugin.version());
    return;
  }

  let metadata = cargo_metadata::MetadataCommand::new()
    .no_deps()
    .other_options(["--all-features".to_string(), "--offline".to_string()])
    .exec()
    .unwrap();
  let plugin_subdir = format!("plugin-{}", env!("RUSTC_CHANNEL"));
  let target_dir = metadata.target_directory.join(plugin_subdir);

  let args = plugin.args(&target_dir);

  let mut cmd = Command::new("cargo");
  cmd.stdout(Stdio::inherit()).stderr(Stdio::inherit());

  let mut path = env::current_exe()
    .expect("current executable path invalid")
    .with_file_name(plugin.driver_name().as_ref());

  let exec_hash = {
    use std::hash::{Hash, Hasher};
    let mut hasher = std::collections::hash_map::DefaultHasher::new();
    path
      .metadata()
      .unwrap()
      .modified()
      .unwrap()
      .hash(&mut hasher);
    std::env::current_exe()
      .unwrap()
      .metadata()
      .unwrap()
      .modified()
      .unwrap()
      .hash(&mut hasher);
    plugin.hash_config(&args.args, &mut hasher);
    hasher.finish()
  };

  if cfg!(windows) {
    path.set_extension("exe");
  }

  let mut prior_rustflags = prior_rustflags().unwrap();

  prior_rustflags.push(format!("{}\x1f{exec_hash:x}", crate::EXEC_HASH_ARG));

  cmd
    .env(
      if matches!(args.filter, CrateFilter::AllCrates) {
        "RUSTC_WRAPPER"
      } else {
        "RUSTC_WORKSPACE_WRAPPER"
      },
      path,
    )
    .args(["check", "--target-dir"])
    .env(CARGO_ENCODED_RUSTFLAGS, prior_rustflags.join("\x1f"))
    .arg(&target_dir);

  if env::var(CARGO_VERBOSE).is_ok() {
    cmd.arg("-vv");
  } else {
    cmd.arg("-q");
  }

  let workspace_members = metadata
    .workspace_members
    .iter()
    .map(|pkg_id| {
      metadata
        .packages
        .iter()
        .find(|pkg| &pkg.id == pkg_id)
        .unwrap()
    })
    .collect::<Vec<_>>();

  match args.filter {
    CrateFilter::CrateContainingFile(file_path) => {
      only_run_on_file(&mut cmd, file_path, &workspace_members, &target_dir);
    }
    CrateFilter::AllCrates | CrateFilter::OnlyWorkspace => {
      cmd.arg("--all");
      match args.filter {
        CrateFilter::AllCrates => {
          cmd.env(RUN_ON_ALL_CRATES, "");
        }
        CrateFilter::OnlyWorkspace => {}
        CrateFilter::CrateContainingFile(_) => unreachable!(),
      }
    }
  }

  let args_str = serde_json::to_string(&args.args).unwrap();
  log::debug!("{PLUGIN_ARGS}={args_str}");
  cmd.env(PLUGIN_ARGS, args_str);

  // HACK: if running on the rustc codebase, this env var needs to exist
  // for the code to compile
  if workspace_members.iter().any(|pkg| pkg.name == "rustc-main") {
    cmd.env("CFG_RELEASE", "");
  }

  plugin.modify_cargo(&mut cmd, &args.args);

  let exit_status = cmd.status().expect("failed to wait for cargo?");

  exit(exit_status.code().unwrap_or(-1));
}

fn only_run_on_file(
  cmd: &mut Command,
  file_path: PathBuf,
  workspace_members: &[&cargo_metadata::Package],
  target_dir: &Utf8Path,
) {
  // We compare this against canonicalized paths, so it must be canonicalized too
  let file_path = file_path.canonicalize().unwrap();

  // Find the package and target that corresponds to a given file path
  let mut matching = workspace_members
    .iter()
    .filter_map(|pkg| {
      let targets = pkg
        .targets
        .iter()
        .filter(|target| {
          let src_path = target.src_path.canonicalize().unwrap();
          log::trace!("Package {} has src path {}", pkg.name, src_path.display());
          file_path.starts_with(src_path.parent().unwrap())
        })
        .collect::<Vec<_>>();

      let target = (match targets.len() {
        0 => None,
        1 => Some(targets[0]),
        _ => {
          // If there are multiple targets that match a given directory, e.g. `examples/whatever.rs`, then
          // find the target whose name matches the file stem
          let stem = file_path.file_stem().unwrap().to_string_lossy();
          let name_matches_stem = targets
            .clone()
            .into_iter()
            .find(|target| target.name == stem);

          // Otherwise we're in a special case, e.g. "main.rs" corresponds to the bin target.
          name_matches_stem.or_else(|| {
            let only_bin = targets
              .iter()
              .all(|target| !target.kind.contains(&"lib".into()));
            // TODO: this is a pile of hacks, and it seems like there is no reliable way to say
            // which target a file will correspond to given only its filename. For example,
            // if you have src/foo.rs it could either be imported by src/main.rs, or src/lib.rs, or
            // even both!
            if only_bin {
              targets
                .into_iter()
                .find(|target| target.kind.contains(&"bin".into()))
            } else {
              let kind = (if stem == "main" { "bin" } else { "lib" }).to_string();
              targets
                .into_iter()
                .find(|target| target.kind.contains(&kind))
            }
          })
        }
      })?;

      Some((pkg, target))
    })
    .collect::<Vec<_>>();
  let (pkg, target) = match matching.len() {
    0 => panic!("Could not find target for path: {}", file_path.display()),
    1 => matching.remove(0),
    _ => panic!("Too many matching targets: {matching:?}"),
  };

  // Add compile filter to specify the target corresponding to the given file
  cmd.arg("-p").arg(format!("{}:{}", pkg.name, pkg.version));

  enum CompileKind {
    Lib,
    Bin,
    ProcMacro,
  }

  // kind string should be one of the ones listed here:
  // https://doc.rust-lang.org/cargo/reference/cargo-targets.html#the-crate-type-field
  let kind_str = &target.kind[0];
  let kind = match kind_str.as_str() {
    "lib" | "rlib" | "dylib" | "staticlib" | "cdylib" => CompileKind::Lib,
    "bin" => CompileKind::Bin,
    "proc-macro" => CompileKind::ProcMacro,
    _ => unreachable!("unexpected cargo crate type: {kind_str}"),
  };

  match kind {
    CompileKind::Lib => {
      // If the rmeta files were previously generated for the lib (e.g. by running the plugin
      // on a reverse-dep), then we have to remove them or else Cargo will memoize the plugin.
      let deps_dir = target_dir.join("debug").join("deps");
      if let Ok(entries) = fs::read_dir(deps_dir) {
        let prefix = format!("lib{}", pkg.name.replace('-', "_"));
        for entry in entries {
          let path = entry.unwrap().path();
          if let Some(file_name) = path.file_name() {
            if file_name.to_string_lossy().starts_with(&prefix) {
              fs::remove_file(path).unwrap();
            }
          }
        }
      }

      cmd.arg("--lib");
    }
    CompileKind::Bin => {
      cmd.args(["--bin", &target.name]);
    }
    CompileKind::ProcMacro => {}
  }

  cmd.env(SPECIFIC_CRATE, pkg.name.replace('-', "_"));
  cmd.env(SPECIFIC_TARGET, kind_str);

  log::debug!(
    "Package: {}, target kind {}, target name {}",
    pkg.name,
    kind_str,
    target.name
  );
}

#[cfg(test)]
mod tests {
  use std::ffi::OsStr;

  use crate::cli::{prior_rustflags, CARGO_ENCODED_RUSTFLAGS};

  fn with_var<K: AsRef<OsStr>, V: AsRef<OsStr>, R>(
    k: K,
    v: V,
    f: impl FnOnce() -> R,
  ) -> R {
    let k_ref = k.as_ref();
    std::env::set_var(k_ref, v);
    let result = f();
    // XXX does not restore any old values (because I'm lazy)
    std::env::remove_var(k_ref);
    result
  }

  #[test]
  fn rustflags_test() {
    with_var("RUSTFLAGS", "space double_space  tab  end", || {
      assert_eq!(
        prior_rustflags(),
        Ok(vec![
          "space".to_string(),
          "double_space".to_string(),
          "tab".to_string(),
          "end".to_string()
        ]),
        "whitespace works"
      );
      with_var(CARGO_ENCODED_RUSTFLAGS, "override", || {
        assert_eq!(prior_rustflags(), Ok(vec!["override".to_string()]))
      })
    });
  }
}