Skip to content

Commit 04881df

Browse files
celinvaltedinski
authored andcommitted
Change cargo rmc script to use RUSTC binary during build (rust-lang#613) (rust-lang#615)
When the crate contains a build script, cargo compiles the build script first without the RUSTFLAGS. Thus, we cannot use rmc-rustc directly, or it will try to compile the build script with rmc enabled. We then fail to generate the script binary. * Add --build-target to cargo rmc
1 parent 87094eb commit 04881df

File tree

6 files changed

+78
-6
lines changed

6 files changed

+78
-6
lines changed

scripts/cargo-rmc

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import rmc
99
import rmc_flags
1010
import os
1111
import pathlib
12+
import platform
1213
import toml
1314

1415
MY_PATH = pathlib.Path(__file__).parent.parent.absolute()
@@ -22,7 +23,7 @@ def main():
2223
rmc.ensure_dependencies_in_path()
2324

2425
if args.gen_c_runnable:
25-
rmc.cargo_build(args.crate, args.target_dir,
26+
rmc.cargo_build(args.crate, args.target_dir, args.build_target,
2627
args.verbose, args.debug, args.mangler, args.dry_run, ["gen-c"])
2728

2829
pattern = os.path.join(args.target_dir, "debug", "deps", "*.symtab.json")
@@ -42,10 +43,13 @@ def main():
4243

4344
rmc.gen_c_postprocess(c_runnable_filename, args.dry_run)
4445

45-
rmc.cargo_build(args.crate, args.target_dir,
46+
rmc.cargo_build(args.crate, args.target_dir, args.build_target,
4647
args.verbose, args.debug, args.mangler, args.dry_run, [])
4748

48-
pattern = os.path.join(args.target_dir, "debug", "deps", "*.symtab.json")
49+
if args.build_target:
50+
pattern = os.path.join(args.target_dir, args.build_target, "debug", "deps", "*.symtab.json")
51+
else:
52+
pattern = os.path.join(args.target_dir, "debug", "deps", "*.symtab.json")
4953
symbol_table_jsons = glob.glob(pattern)
5054

5155
if not args.dry_run:
@@ -108,6 +112,24 @@ def main():
108112

109113
return retcode
110114

115+
116+
def default_build_target():
117+
"""
118+
Returns the default build target based on the current OS.
119+
120+
This function will return None for windows and print a warning.
121+
"""
122+
machine = platform.uname()
123+
if machine.system == "Linux":
124+
return"x86_64-unknown-linux-gnu"
125+
126+
if machine.system == "Darwin":
127+
return "x86_64-apple-darwin"
128+
129+
print("WARNING: No default build target exists for this platform. Please use --build-target to set the correct target")
130+
return None
131+
132+
111133
def parse_args():
112134
# Create parser
113135
def create_parser():
@@ -124,6 +146,8 @@ def parse_args():
124146
config_group.add_argument("--config-toml",
125147
help="Where to read configuration from; defaults to crate's Cargo.toml")
126148
config_group.add_argument("--no-config-toml", action="store_true", help="Do not use any configuration toml")
149+
config_group.add_argument("--build-target", help="Build for the target triple.",
150+
default=default_build_target())
127151

128152
exclude_flags = []
129153
rmc_flags.add_flags(parser, {"default-target": "target"}, exclude_flags=exclude_flags)

scripts/rmc.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,37 @@ def compile_single_rust_file(
189189
return run_cmd(build_cmd, env=build_env, label="compile", verbose=verbose, debug=debug, dry_run=dry_run)
190190

191191
# Generates a symbol table (and some other artifacts) from a rust crate
192-
def cargo_build(crate, target_dir, verbose=False, debug=False, mangler="v0", dry_run=False, symbol_table_passes=[]):
192+
def cargo_build(
193+
crate,
194+
target_dir,
195+
build_target=None,
196+
verbose=False,
197+
debug=False,
198+
mangler="v0",
199+
dry_run=False,
200+
symbol_table_passes=[]):
193201
ensure(os.path.isdir(crate), f"Invalid path to crate: {crate}")
194202

195-
rustflags = rustc_flags(mangler, symbol_table_passes)
203+
def get_config(option):
204+
process = subprocess.run(
205+
[RMC_RUSTC_EXE, option],
206+
universal_newlines=True,
207+
stdout=subprocess.PIPE,
208+
stderr=subprocess.STDOUT,
209+
env=os.environ,
210+
cwd=crate)
211+
if process.returncode != EXIT_CODE_SUCCESS:
212+
raise Exception("[Error] Fail to extract rmc configuration.\n{}".format(
213+
process.stdout))
214+
return process.stdout
215+
216+
rustflags = rustc_flags(mangler, symbol_table_passes) + get_config("--rmc-flags").split()
217+
rustc_path = get_config("--rmc-path").strip()
196218
build_cmd = ["cargo", "build", "--lib", "--target-dir", str(target_dir)]
219+
if build_target:
220+
build_cmd += ["--target", str(build_target)]
197221
build_env = {"RUSTFLAGS": " ".join(rustflags),
198-
"RUSTC": RMC_RUSTC_EXE,
222+
"RUSTC": rustc_path,
199223
"PATH": os.environ["PATH"]
200224
}
201225
if debug:
@@ -204,6 +228,7 @@ def cargo_build(crate, target_dir, verbose=False, debug=False, mangler="v0", dry
204228
build_cmd.append("-v")
205229
if dry_run:
206230
print("{}".format(build_env))
231+
207232
return run_cmd(build_cmd, env=build_env, cwd=crate, label="build", verbose=verbose, debug=debug, dry_run=dry_run)
208233

209234
# Adds information about unwinding to the RMC output

src/test/cargo-rmc/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
target/
12
Cargo.lock
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0 OR MIT
3+
[package]
4+
name = "foo"
5+
version = "0.1.0"
6+
edition = "2021"
7+
8+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
9+
10+
[dependencies]
11+
memchr = { version = "2", default-features = false }
12+
rand = "0.8.4"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[check_dummy.assertion.1] line 8 assertion failed: x > 2: SUCCESS
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
#[no_mangle]
5+
pub fn check_dummy() {
6+
let x = rmc::nondet::<u8>();
7+
rmc::assume(x > 10);
8+
assert!(x > 2);
9+
}

0 commit comments

Comments
 (0)