-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "kani" configuration key to enable conditional compilation in bui…
…ld scripts (#2297) We now pass `--cfg=kani` to build scripts, which allow users to use constructs such as `if cfg!(kani)` to conditionally compile their build scripts. The build script may have logic that is not redundant to Kani, or even unsupported. Users can now change how their build works based on conditional compilation. Co-authored-by: Kareem Khazem <karkhaz@amazon.com>
- Loading branch information
Showing
10 changed files
with
119 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Copyright Kani Contributors | ||
# SPDX-License-Identifier: Apache-2.0 OR MIT | ||
[package] | ||
name = "build-rs-conditional" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright Kani Contributors | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
//! Verify that build scripts can check if they are running under `kani`. | ||
|
||
fn main() { | ||
if cfg!(kani) { | ||
println!("cargo:rustc-env=RUNNING_KANI=Yes"); | ||
} else { | ||
println!("cargo:rustc-env=RUNNING_KANI=No"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#!/usr/bin/env bash | ||
# Copyright Kani Contributors | ||
# SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
||
set +e | ||
|
||
TMP_DIR="/tmp/build-rs" | ||
|
||
rm -rf ${TMP_DIR} | ||
cp -r . ${TMP_DIR} | ||
pushd ${TMP_DIR} > /dev/null | ||
|
||
|
||
echo "[TEST] Run verification..." | ||
cargo kani --concrete-playback=inplace -Z concrete-playback | ||
|
||
echo "[TEST] Run playback..." | ||
cargo kani playback -Z concrete-playback --lib -- check_kani | ||
|
||
echo "[TEST] Run test..." | ||
cargo test --lib | ||
|
||
# Cleanup | ||
popd > /dev/null | ||
rm -r ${TMP_DIR} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Copyright Kani Contributors | ||
# SPDX-License-Identifier: Apache-2.0 OR MIT | ||
script: build_rs.sh | ||
expected: expected |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[TEST] Run verification... | ||
Checking harness verify::check_kani... | ||
Complete - 1 successfully verified harnesses, 0 failures, 1 total | ||
|
||
[TEST] Run playback... | ||
running 1 test\ | ||
test verify::kani_concrete_playback_check_kani_\ | ||
\ | ||
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 1 filtered out | ||
|
||
[TEST] Run test... | ||
running 1 test\ | ||
test test::check_not_kani ... ok\ | ||
\ | ||
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright Kani Contributors | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
//! This tests ensures that build scripts are able to conditionally check if they are running under | ||
//! Kani (in both verification and playback mode). | ||
|
||
#[cfg(kani)] | ||
mod verify { | ||
/// Running `cargo kani` should verify that "RUNNING_KANI" is equals to "Yes" | ||
#[kani::proof] | ||
fn check_kani() { | ||
assert_eq!(env!("RUNNING_KANI"), "Yes"); | ||
// Add a dummy cover so playback generates a test that should succeed. | ||
kani::cover!(kani::any()); | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
/// Running `cargo test` should check that "RUNNING_KANI" is "No". | ||
#[test] | ||
fn check_not_kani() { | ||
assert_eq!(env!("RUNNING_KANI"), "No"); | ||
} | ||
} |