Skip to content

Commit

Permalink
rust-project: add --sysroot-mode to develop-json
Browse files Browse the repository at this point in the history
End-users might want to specify the path to their sysroot in various ways, but
by default they probably just want to use whatever is implied by `rustup`, which
can be found by `rustc --print=sysroot`

This implements a new `develop-json --sysroot=mode=<MODE>` command, where
`<MODE>` might be:

- `rustup` (default): run `rustc --print=sysroot` to figure it out
- `buckconfig`: look in `rust.sysroot_src_path` (or whatever it's called)
- `path:some/path/here`: some random file path to a sysroot
- `cmd:some command here`: a command that is passed to the OS

This should basically allow any needed customization including cases where
buck is using a vendored rustc, which may itself refer to a target name and use
a command.

Signed-off-by: Austin Seipp <aseipp@pobox.com>
  • Loading branch information
thoughtpolice committed Aug 21, 2024
1 parent 9f9d1b7 commit 99cacd8
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
17 changes: 16 additions & 1 deletion integrations/rust-project/src/cli/develop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,29 @@ impl Develop {
}

if let crate::Command::DevelopJson {
sysroot_mode,
args,
log_scuba_to_stdout: _,
} = command
{
let out = Output::Stdout;
let sysroot = SysrootConfig::BuckConfig;
let mode = select_mode(None);

let sysroot = match sysroot_mode {
crate::SysrootMode::BuckConfig => SysrootConfig::BuckConfig,
crate::SysrootMode::Rustup => SysrootConfig::Rustup,
crate::SysrootMode::FullPath(path) => SysrootConfig::Sysroot(path),
crate::SysrootMode::Command(cmd_args) => {
// run 'cmd' and parse the output as a path
println!("Running command: {:?}", cmd_args);
let cmd = cmd_args[0].clone();
let args = cmd_args[1..].to_vec();
let output = std::process::Command::new(cmd).args(args).output().unwrap();
let path = String::from_utf8(output.stdout).unwrap();
SysrootConfig::Sysroot(PathBuf::from(path.trim()))
}
};

let buck = buck::Buck::new(mode);

let develop = Develop {
Expand Down
43 changes: 43 additions & 0 deletions integrations/rust-project/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ enum Command {
#[clap(long, hide = true)]
log_scuba_to_stdout: bool,

#[clap(long, default_value = "rustup")]
sysroot_mode: SysrootMode,

args: JsonArguments,
},
/// Build the saved file's owning target. This is meant to be used by IDEs to provide diagnostics on save.
Expand All @@ -137,6 +140,43 @@ enum Command {
},
}

/// The 'develop-json' command needs to have 3 modes:
/// 1. Static `.buckconfig` setting
/// 2. Absolute path setting
/// 3. Use `rustc --print=sysroot` ("rustup mode")
/// 4. Run a command and take the output from stdout
#[derive(PartialEq, Clone, Debug, Deserialize)]
enum SysrootMode {
Rustup,
Command(Vec<String>),
FullPath(PathBuf),
BuckConfig,
}

impl FromStr for SysrootMode {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
if s == "rustup" {
Ok(SysrootMode::Rustup)
} else if s == "buckconfig" {
Ok(SysrootMode::BuckConfig)
} else if s.starts_with("path:") {
let s = s.trim_start_matches("path:");
Ok(SysrootMode::FullPath(PathBuf::from(s)))
} else if s.starts_with("cmd:") {
let s = s.trim_start_matches("cmd:");
Ok(SysrootMode::Command(
s.split_whitespace()
.map(|s| s.to_string())
.collect::<Vec<String>>(),
))
} else {
Err(anyhow::anyhow!("Invalid mode: {}", s))
}
}
}

#[derive(PartialEq, Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
enum JsonArguments {
Expand Down Expand Up @@ -325,6 +365,7 @@ fn json_args_pass() {
let expected = Opt {
command: Some(Command::DevelopJson {
args,
sysroot_mode: SysrootMode::Rustup,
log_scuba_to_stdout: false,
}),
version: false,
Expand All @@ -341,6 +382,7 @@ fn json_args_pass() {
let expected = Opt {
command: Some(Command::DevelopJson {
args,
sysroot_mode: SysrootMode::Rustup,
log_scuba_to_stdout: false,
}),
version: false,
Expand All @@ -357,6 +399,7 @@ fn json_args_pass() {
let expected = Opt {
command: Some(Command::DevelopJson {
args,
sysroot_mode: SysrootMode::Rustup,
log_scuba_to_stdout: false,
}),
version: false,
Expand Down

0 comments on commit 99cacd8

Please sign in to comment.