Skip to content

Commit 4ca66eb

Browse files
authored
Add flag to the compiler to build std library (no user visible change) (rust-lang#2676)
Parts of the compiler relies on the existence of some constructs that are available in the standard library and Kani library. However, they aren't available when we are actually building the standard library (`std-lib-regression.sh`) and the Kani libraries (when building the sysroot with `cargo build-dev`). Thus, we introduce a flag to inform the compiler that we are currently building the standard library. This flag can then be used to properly enable / disable sysroot dependent code.
1 parent 5a9c933 commit 4ca66eb

File tree

6 files changed

+23
-5
lines changed

6 files changed

+23
-5
lines changed

kani-compiler/src/kani_compiler.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ impl Callbacks for KaniCompiler {
395395
queries.write_json_symtab =
396396
cfg!(feature = "write_json_symtab") || matches.get_flag(parser::WRITE_JSON_SYMTAB);
397397
queries.reachability_analysis = matches.reachability_type();
398+
queries.build_std = matches.get_flag(parser::BUILD_STD);
398399

399400
if let Some(features) = matches.get_many::<String>(parser::UNSTABLE_FEATURE) {
400401
queries.unstable_features = features.cloned().collect::<Vec<_>>();

kani-compiler/src/kani_middle/provide.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
77
use crate::kani_middle::reachability::{collect_reachable_items, filter_crate_items};
88
use crate::kani_middle::stubbing;
9-
use crate::kani_queries::QueryDb;
9+
use crate::kani_queries::{QueryDb, ReachabilityType};
1010
use rustc_hir::def_id::{DefId, LocalDefId};
1111
use rustc_interface;
1212
use rustc_middle::{
@@ -18,9 +18,13 @@ use rustc_middle::{
1818
/// Sets up rustc's query mechanism to apply Kani's custom queries to code from
1919
/// the present crate.
2020
pub fn provide(providers: &mut Providers, queries: &QueryDb) {
21-
providers.optimized_mir = run_mir_passes;
22-
if queries.stubbing_enabled {
23-
providers.collect_and_partition_mono_items = collect_and_partition_mono_items;
21+
if queries.reachability_analysis != ReachabilityType::None && !queries.build_std {
22+
// Don't override queries if we are only compiling our dependencies.
23+
providers.optimized_mir = run_mir_passes;
24+
if queries.stubbing_enabled {
25+
// TODO: Check if there's at least one stub being applied.
26+
providers.collect_and_partition_mono_items = collect_and_partition_mono_items;
27+
}
2428
}
2529
}
2630

kani-compiler/src/kani_queries/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ pub struct QueryDb {
3737
pub reachability_analysis: ReachabilityType,
3838
pub stubbing_enabled: bool,
3939
pub unstable_features: Vec<String>,
40+
/// Flag that indicates that we are currently building the standard library.
41+
/// Note that `kani` library will not be available if this is `true`.
42+
pub build_std: bool,
4043

4144
/// Information about all target harnesses.
4245
pub harnesses_info: HashMap<DefPathHash, PathBuf>,

kani-compiler/src/parser.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ pub const ENABLE_STUBBING: &str = "enable-stubbing";
4949
/// Option name used to define unstable features.
5050
pub const UNSTABLE_FEATURE: &str = "unstable";
5151

52+
/// Option used for building standard library.
53+
pub const BUILD_STD: &str = "build-std";
54+
5255
/// Configure command options for the Kani compiler.
5356
pub fn parser() -> Command {
5457
let app = command!()
@@ -152,6 +155,12 @@ pub fn parser() -> Command {
152155
.help("Enable an unstable feature")
153156
.value_name("UNSTABLE_FEATURE")
154157
.action(ArgAction::Append),
158+
)
159+
.arg(
160+
Arg::new(BUILD_STD)
161+
.long(BUILD_STD)
162+
.help("Enable building the standard library.")
163+
.action(ArgAction::SetTrue),
155164
);
156165
app
157166
}

scripts/std-lib-regression.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ RUST_FLAGS=(
7474
"-Cllvm-args=--goto-c"
7575
"-Cllvm-args=--ignore-global-asm"
7676
"-Cllvm-args=--reachability=pub_fns"
77+
"-Cllvm-args=--build-std"
7778
)
7879
export RUSTFLAGS="${RUST_FLAGS[@]}"
7980
export RUSTC="$KANI_DIR/target/kani/bin/kani-compiler"

tools/build-kani/src/sysroot.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub fn build_lib(bin_folder: &Path) -> Result<()> {
7474
fn build_verification_lib(compiler_path: &Path) -> Result<()> {
7575
let extra_args =
7676
["-Z", "build-std=panic_abort,std,test", "--config", "profile.dev.panic=\"abort\""];
77-
let compiler_args = ["--kani-compiler", "-Cllvm-args=--ignore-global-asm"];
77+
let compiler_args = ["--kani-compiler", "-Cllvm-args=--ignore-global-asm --build-std"];
7878
build_kani_lib(compiler_path, &kani_sysroot_lib(), &extra_args, &compiler_args)
7979
}
8080

0 commit comments

Comments
 (0)