Skip to content

Commit 45caad4

Browse files
authored
Add --use-local-toolchain to Kani setup (rust-lang#3056)
Adds `--use-local-toolchain` to Kani's setup flow, which accepts a local toolchain and then uses that to finish the Kani setup. Some notes: 1. Why? This is mainly for installing GPG verified toolchains. 2. This is missing some cleanup and refactoring work, like ensuring that the user defined toolchain matches the one that Kani was built against etc. Marked as Todo, for later. Resolves [rust-lang#3058](model-checking/kani#3058) By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.
1 parent 12768f2 commit 45caad4

File tree

10 files changed

+236
-47
lines changed

10 files changed

+236
-47
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ desktop.ini
1616
Session.vim
1717
.cproject
1818
.idea
19+
toolchains/
1920
*.iml
2021
.vscode
2122
.project

Cargo.lock

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ dependencies = [
3333
"memchr",
3434
]
3535

36+
[[package]]
37+
name = "ansi_term"
38+
version = "0.12.1"
39+
source = "registry+https://github.com/rust-lang/crates.io-index"
40+
checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2"
41+
dependencies = [
42+
"winapi",
43+
]
44+
3645
[[package]]
3746
name = "anstream"
3847
version = "0.6.13"
@@ -87,6 +96,17 @@ version = "1.0.80"
8796
source = "registry+https://github.com/rust-lang/crates.io-index"
8897
checksum = "5ad32ce52e4161730f7098c077cd2ed6229b5804ccf99e5366be1ab72a98b4e1"
8998

99+
[[package]]
100+
name = "atty"
101+
version = "0.2.14"
102+
source = "registry+https://github.com/rust-lang/crates.io-index"
103+
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
104+
dependencies = [
105+
"hermit-abi",
106+
"libc",
107+
"winapi",
108+
]
109+
90110
[[package]]
91111
name = "autocfg"
92112
version = "1.1.0"
@@ -125,7 +145,7 @@ version = "0.47.0"
125145
dependencies = [
126146
"anyhow",
127147
"cargo_metadata",
128-
"clap",
148+
"clap 4.5.1",
129149
"which",
130150
]
131151

@@ -167,6 +187,21 @@ version = "1.0.0"
167187
source = "registry+https://github.com/rust-lang/crates.io-index"
168188
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
169189

190+
[[package]]
191+
name = "clap"
192+
version = "2.34.0"
193+
source = "registry+https://github.com/rust-lang/crates.io-index"
194+
checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c"
195+
dependencies = [
196+
"ansi_term",
197+
"atty",
198+
"bitflags 1.3.2",
199+
"strsim 0.8.0",
200+
"textwrap",
201+
"unicode-width",
202+
"vec_map",
203+
]
204+
170205
[[package]]
171206
name = "clap"
172207
version = "4.5.1"
@@ -186,7 +221,7 @@ dependencies = [
186221
"anstream",
187222
"anstyle",
188223
"clap_lex",
189-
"strsim",
224+
"strsim 0.11.0",
190225
]
191226

192227
[[package]]
@@ -392,6 +427,15 @@ version = "0.4.1"
392427
source = "registry+https://github.com/rust-lang/crates.io-index"
393428
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
394429

430+
[[package]]
431+
name = "hermit-abi"
432+
version = "0.1.19"
433+
source = "registry+https://github.com/rust-lang/crates.io-index"
434+
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
435+
dependencies = [
436+
"libc",
437+
]
438+
395439
[[package]]
396440
name = "home"
397441
version = "0.5.9"
@@ -437,7 +481,7 @@ dependencies = [
437481
name = "kani-compiler"
438482
version = "0.47.0"
439483
dependencies = [
440-
"clap",
484+
"clap 4.5.1",
441485
"cprover_bindings",
442486
"home",
443487
"itertools",
@@ -460,7 +504,7 @@ version = "0.47.0"
460504
dependencies = [
461505
"anyhow",
462506
"cargo_metadata",
463-
"clap",
507+
"clap 4.5.1",
464508
"comfy-table",
465509
"console",
466510
"glob",
@@ -487,6 +531,7 @@ name = "kani-verifier"
487531
version = "0.47.0"
488532
dependencies = [
489533
"anyhow",
534+
"clap 2.34.0",
490535
"home",
491536
"os_info",
492537
]
@@ -505,7 +550,7 @@ dependencies = [
505550
name = "kani_metadata"
506551
version = "0.47.0"
507552
dependencies = [
508-
"clap",
553+
"clap 4.5.1",
509554
"cprover_bindings",
510555
"serde",
511556
"strum 0.26.1",
@@ -1050,6 +1095,12 @@ dependencies = [
10501095
"serde",
10511096
]
10521097

1098+
[[package]]
1099+
name = "strsim"
1100+
version = "0.8.0"
1101+
source = "registry+https://github.com/rust-lang/crates.io-index"
1102+
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
1103+
10531104
[[package]]
10541105
name = "strsim"
10551106
version = "0.11.0"
@@ -1127,6 +1178,15 @@ dependencies = [
11271178
"windows-sys",
11281179
]
11291180

1181+
[[package]]
1182+
name = "textwrap"
1183+
version = "0.11.0"
1184+
source = "registry+https://github.com/rust-lang/crates.io-index"
1185+
checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
1186+
dependencies = [
1187+
"unicode-width",
1188+
]
1189+
11301190
[[package]]
11311191
name = "thiserror"
11321192
version = "1.0.57"
@@ -1305,6 +1365,12 @@ version = "0.1.0"
13051365
source = "registry+https://github.com/rust-lang/crates.io-index"
13061366
checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d"
13071367

1368+
[[package]]
1369+
name = "vec_map"
1370+
version = "0.8.2"
1371+
source = "registry+https://github.com/rust-lang/crates.io-index"
1372+
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
1373+
13081374
[[package]]
13091375
name = "version_check"
13101376
version = "0.9.4"

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ include = ["/src", "/build.rs", "/rust-toolchain.toml", "/LICENSE-*", "/README.m
1919
[dependencies]
2020
anyhow = "1"
2121
home = "0.5"
22+
clap = "2.33.3"
2223
os_info = { version = "3", default-features = false }
2324

2425
[[bin]]

kani-compiler/build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ macro_rules! path_str {
2020
/// kani-compiler with nightly only. We also link to the rustup rustc_driver library for now.
2121
pub fn main() {
2222
// Add rustup to the rpath in order to properly link with the correct rustc version.
23+
24+
// This is for dev purposes only, if dev point/search toolchain in .rustup/toolchains/
2325
let rustup_home = env::var("RUSTUP_HOME").unwrap();
2426
let rustup_tc = env::var("RUSTUP_TOOLCHAIN").unwrap();
2527
let rustup_lib = path_str!([&rustup_home, "toolchains", &rustup_tc, "lib"]);

kani-driver/src/assess/scan.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
use std::collections::HashSet;
55
use std::path::Path;
66
use std::path::PathBuf;
7-
use std::process::Command;
87
use std::time::Instant;
98

109
use anyhow::Result;
1110
use cargo_metadata::Package;
1211

12+
use crate::session::setup_cargo_command;
1313
use crate::session::KaniSession;
1414

1515
use super::metadata::AssessMetadata;
@@ -168,7 +168,8 @@ fn invoke_assess(
168168
) -> Result<()> {
169169
let dir = manifest.parent().expect("file not in a directory?");
170170
let log = std::fs::File::create(logfile)?;
171-
let mut cmd = Command::new("cargo");
171+
172+
let mut cmd = setup_cargo_command()?;
172173
cmd.arg("kani");
173174
// Use of options before 'assess' subcommand is a hack, these should be factored out.
174175
// TODO: --only-codegen should be outright an option to assess. (perhaps tests too?)

kani-driver/src/call_cargo.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
use crate::args::VerificationArgs;
55
use crate::call_single_file::to_rustc_arg;
66
use crate::project::Artifact;
7-
use crate::session::KaniSession;
8-
use crate::{session, util};
7+
use crate::session::{setup_cargo_command, KaniSession};
8+
use crate::util;
99
use anyhow::{bail, Context, Result};
1010
use cargo_metadata::diagnostic::{Diagnostic, DiagnosticLevel};
1111
use cargo_metadata::{Message, Metadata, MetadataCommand, Package, Target};
@@ -109,9 +109,8 @@ impl KaniSession {
109109
let mut failed_targets = vec![];
110110
for package in packages {
111111
for verification_target in package_targets(&self.args, package) {
112-
let mut cmd = Command::new("cargo");
113-
cmd.arg(session::toolchain_shorthand())
114-
.args(&cargo_args)
112+
let mut cmd = setup_cargo_command()?;
113+
cmd.args(&cargo_args)
115114
.args(vec!["-p", &package.name])
116115
.args(&verification_target.to_args())
117116
.args(&pkg_args)

kani-driver/src/concrete_playback/playback.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::args::common::Verbosity;
77
use crate::args::playback_args::{CargoPlaybackArgs, KaniPlaybackArgs, MessageFormat};
88
use crate::call_cargo::cargo_config_args;
99
use crate::call_single_file::base_rustc_flags;
10-
use crate::session::{lib_playback_folder, InstallType};
10+
use crate::session::{lib_playback_folder, setup_cargo_command, InstallType};
1111
use crate::{session, util};
1212
use anyhow::Result;
1313
use std::ffi::OsString;
@@ -17,8 +17,7 @@ use std::process::Command;
1717
use tracing::debug;
1818

1919
pub fn playback_cargo(args: CargoPlaybackArgs) -> Result<()> {
20-
let install = InstallType::new()?;
21-
cargo_test(&install, args)
20+
cargo_test(args)
2221
}
2322

2423
pub fn playback_standalone(args: KaniPlaybackArgs) -> Result<()> {
@@ -93,9 +92,10 @@ fn build_test(install: &InstallType, args: &KaniPlaybackArgs) -> Result<PathBuf>
9392
}
9493

9594
/// Invokes cargo test using Kani compiler and the provided arguments.
96-
/// TODO: This should likely be inside KaniSession, but KaniSession requires `VerificationArgs` today.
97-
/// For now, we just use InstallType directly.
98-
fn cargo_test(install: &InstallType, args: CargoPlaybackArgs) -> Result<()> {
95+
fn cargo_test(args: CargoPlaybackArgs) -> Result<()> {
96+
let install = InstallType::new()?;
97+
let mut cmd = setup_cargo_command()?;
98+
9999
let rustc_args = base_rustc_flags(lib_playback_folder()?);
100100
let mut cargo_args: Vec<OsString> = vec!["test".into()];
101101

@@ -123,9 +123,7 @@ fn cargo_test(install: &InstallType, args: CargoPlaybackArgs) -> Result<()> {
123123
}
124124

125125
// Arguments that will only be passed to the target package.
126-
let mut cmd = Command::new("cargo");
127-
cmd.arg(session::toolchain_shorthand())
128-
.args(&cargo_args)
126+
cmd.args(&cargo_args)
129127
.env("RUSTC", &install.kani_compiler()?)
130128
// Use CARGO_ENCODED_RUSTFLAGS instead of RUSTFLAGS is preferred. See
131129
// https://doc.rust-lang.org/cargo/reference/environment-variables.html

kani-driver/src/session.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,3 +379,25 @@ fn init_logger(args: &VerificationArgs) {
379379
);
380380
tracing::subscriber::set_global_default(subscriber).unwrap();
381381
}
382+
383+
// Setup the default version of cargo being run, based on the type/mode of installation for kani
384+
// If kani is being run in developer mode, then we use the one provided by rustup as we can assume that the developer will have rustup installed
385+
// For release versions of Kani, we use a version of cargo that's in the toolchain that's been symlinked during `cargo-kani` setup. This will allow
386+
// Kani to remove the runtime dependency on rustup later on.
387+
pub fn setup_cargo_command() -> Result<Command> {
388+
let install_type = InstallType::new()?;
389+
390+
let cmd = match install_type {
391+
InstallType::DevRepo(_) => {
392+
let mut cmd = Command::new("cargo");
393+
cmd.arg(self::toolchain_shorthand());
394+
cmd
395+
}
396+
InstallType::Release(kani_dir) => {
397+
let cargo_path = kani_dir.join("toolchain").join("bin").join("cargo");
398+
Command::new(cargo_path)
399+
}
400+
};
401+
402+
Ok(cmd)
403+
}

0 commit comments

Comments
 (0)