-
Notifications
You must be signed in to change notification settings - Fork 214
Add fe build --from-metadata to rebuild from a metadata.json input #1513
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -306,6 +306,137 @@ pub fn build( | |
| } | ||
| } | ||
|
|
||
| /// `fe build --from-metadata`: rebuild from a `metadata.json` recompilation | ||
| /// input instead of a source tree. Reads the metadata (from a file, or stdin | ||
| /// for `-`), materializes the recorded project into a temporary directory, | ||
| /// and runs the normal ingot build on it. Exits the process on failure. | ||
| pub fn build_from_metadata( | ||
| input: &Utf8Path, | ||
| contract: Option<&str>, | ||
| optimize: Option<&str>, | ||
| emit: &[BuildEmit], | ||
| out_dir: Option<&Utf8PathBuf>, | ||
| profile: &str, | ||
| use_recovery_mode: bool, | ||
| ) { | ||
| let emit = EmitSelection::from_requested(emit); | ||
|
|
||
| let metadata = match crate::metadata_input::read_metadata(input) { | ||
| Ok(metadata) => metadata, | ||
| Err(err) => { | ||
| eprintln!("Error: {err}"); | ||
| std::process::exit(1); | ||
| } | ||
| }; | ||
| if let Err(err) = crate::metadata_input::validate_metadata(&metadata) { | ||
| eprintln!("Error: {err}"); | ||
| std::process::exit(1); | ||
| } | ||
|
|
||
| // Exact reproduction is only guaranteed with the same compiler version. | ||
| if let Some(version) = metadata["compiler"]["version"].as_str() { | ||
| let running = env!("CARGO_PKG_VERSION"); | ||
| if version != running { | ||
| eprintln!( | ||
| "Warning: metadata was produced by fe {version}, but this is fe {running}; \ | ||
| the rebuilt bytecode may differ from the original" | ||
| ); | ||
| } | ||
| } | ||
| // Non-release builds share a package version; the recorded commit pins the | ||
| // exact source revision. | ||
| if let (Some(recorded), Some(running)) = ( | ||
| metadata["compiler"]["commit"].as_str(), | ||
| option_env!("FE_GIT_HASH").filter(|hash| !hash.is_empty()), | ||
| ) && recorded != running | ||
| { | ||
| eprintln!( | ||
| "Warning: metadata was produced by fe commit {recorded}, but this is fe commit \ | ||
| {running}; the rebuilt bytecode may differ from the original" | ||
| ); | ||
| } | ||
|
|
||
| // The metadata's optimizer level is the default; an explicit `-O` wins. | ||
| let recorded_level = metadata["settings"]["optimizer"]["level"].as_str(); | ||
| let level = match (optimize, recorded_level) { | ||
| (Some(flag), Some(recorded)) if flag != recorded => { | ||
| eprintln!( | ||
| "Warning: -O {flag} overrides optimizer level {recorded} recorded in the \ | ||
| metadata; the rebuilt bytecode will not match the verified artifact" | ||
| ); | ||
| flag | ||
| } | ||
| (Some(flag), _) => flag, | ||
| (None, Some(recorded)) => recorded, | ||
| (None, None) => "1", | ||
| }; | ||
| let opt_level: OptLevel = match level.parse() { | ||
| Ok(level) => level, | ||
| Err(err) => { | ||
| eprintln!("Error: {err}"); | ||
| std::process::exit(1); | ||
| } | ||
| }; | ||
|
|
||
| // Default target: the contract recorded in `compilationTarget`; an | ||
| // explicit `--contract` overrides it. | ||
| let contract = contract.map(str::to_string).or_else(|| { | ||
| metadata["settings"]["compilationTarget"] | ||
| .as_object() | ||
| .and_then(|target| target.values().next()) | ||
| .and_then(|name| name.as_str()) | ||
| .map(str::to_string) | ||
|
Comment on lines
+383
to
+388
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When metadata comes from Useful? React with 👍 / 👎. |
||
| }); | ||
|
|
||
| let temp = match tempfile::Builder::new() | ||
| .prefix("fe-from-metadata") | ||
| .tempdir() | ||
| { | ||
| Ok(temp) => temp, | ||
| Err(err) => { | ||
| eprintln!("Error: Failed to create temporary project directory: {err}"); | ||
| std::process::exit(1); | ||
| } | ||
| }; | ||
| let Some(temp_root) = Utf8Path::from_path(temp.path()) else { | ||
| eprintln!("Error: temporary project directory path is not valid UTF-8"); | ||
| std::process::exit(1); | ||
| }; | ||
| let root_dir = match crate::metadata_input::reconstruct_project(&metadata, temp_root) { | ||
| Ok(dir) => dir, | ||
| Err(err) => { | ||
| eprintln!("Error: {err}"); | ||
| std::process::exit(1); | ||
| } | ||
| }; | ||
|
|
||
| let out_dir = out_dir.cloned().unwrap_or_else(|| Utf8PathBuf::from("out")); | ||
|
|
||
| let mut db = DriverDataBase::default(); | ||
| db.compiler_options() | ||
| .set_recovery_mode(&mut db) | ||
| .to(use_recovery_mode); | ||
| db.compilation_settings() | ||
| .set_profile(&mut db) | ||
| .to(profile.into()); | ||
|
|
||
| let had_errors = build_directory( | ||
| &mut db, | ||
| &root_dir, | ||
| None, | ||
| contract.as_deref(), | ||
| opt_level, | ||
| emit, | ||
| Some(&out_dir), | ||
| None, | ||
| ); | ||
|
|
||
| drop(temp); | ||
| if had_errors { | ||
| std::process::exit(1); | ||
| } | ||
| } | ||
|
|
||
| #[allow(clippy::too_many_arguments)] | ||
| fn build_file( | ||
| db: &mut DriverDataBase, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.