Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into error-recovery-sepa…
Browse files Browse the repository at this point in the history
…rated
  • Loading branch information
Xanewok committed Sep 21, 2023
2 parents c122f29 + 9d651c9 commit 6570a2f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 18 deletions.
30 changes: 21 additions & 9 deletions crates/solidity/outputs/cargo/build/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use anyhow::Result;
use cargo_emit::rerun_if_changed;

use codegen_grammar::Grammar;
use codegen_parser_generator::code_generator::CodeGenerator;
use infra_utils::{cargo::CargoWorkspace, paths::PathExtensions};
use solidity_language::GrammarConstructor;

// Instead of the soure crate calling codegen APIs directly, it invokes this binary, which in turn calls the codegen APIs.
// Instead of the soure crate calling codegen APIs directly in the build script, it invokes this binary, which in turn
// calls the codegen APIs (and hence why it's emitting `cargo:` directives).
// This indirection is needed because:
//
// 1) We want to run codegen, even if there are git conflicts in output files (Cargo fails to build in that case).
Expand All @@ -14,22 +16,32 @@ use solidity_language::GrammarConstructor;
//
fn main() -> Result<()> {
// Generate files in the source crate:

{
let grammar = Grammar::new();
let crate_dir = CargoWorkspace::locate_source_crate("slang_solidity")?;

CodeGenerator::write_source(&crate_dir.join("src/generated"), grammar)?;
}

// Instruct the caller source crate to rebuild itself if the this build crate changes:

{
let build_crate_dir = CargoWorkspace::locate_source_crate("solidity_cargo_build")?;
// This build script is not directly depended on by the caller source crate, so we need to manually
// instruct Cargo on our invocation to rebuild if any of our dependencies (or we) change:
for crate_name in &[
env!("CARGO_CRATE_NAME"),
"codegen_grammar",
"codegen_parser_generator",
"solidity_language",
] {
let crate_dir = CargoWorkspace::locate_source_crate(crate_name)?;

rerun_if_changed!(build_crate_dir.join("Cargo.toml").unwrap_str());
rerun_if_changed!(build_crate_dir.join("src").unwrap_str());
// This is not accurate, but it's good enough for now.
rerun_if_changed!(crate_dir.join("Cargo.toml").unwrap_str());
rerun_if_changed!(crate_dir.join("src").unwrap_str());
// Emitting the `rerun-if-changed` for non-existent files always causes a rebuild, so first check if a build
// script event exists. It's worth noting that adding/removing one will trip this up.
if crate_dir.join("build.rs").exists() {
rerun_if_changed!(crate_dir.join("build.rs").unwrap_str());
}
}

return Ok(());
Ok(())
}
30 changes: 21 additions & 9 deletions crates/solidity/outputs/npm/build/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use anyhow::Result;
use cargo_emit::rerun_if_changed;

use codegen_grammar::Grammar;
use codegen_parser_generator::code_generator::CodeGenerator;
use infra_utils::{cargo::CargoWorkspace, paths::PathExtensions};
use solidity_language::GrammarConstructor;

// Instead of the soure crate calling codegen APIs directly, it invokes this binary, which in turn calls the codegen APIs.
// Instead of the soure crate calling codegen APIs directly in the build script, it invokes this binary, which in turn
// calls the codegen APIs (and hence why it's emitting `cargo:` directives).
// This indirection is needed because:
//
// 1) We want to run codegen, even if there are git conflicts in output files (Cargo fails to build in that case).
Expand All @@ -14,22 +16,32 @@ use solidity_language::GrammarConstructor;
//
fn main() -> Result<()> {
// Generate files in the source crate:

{
let grammar = Grammar::new();
let crate_dir = CargoWorkspace::locate_source_crate("solidity_npm_crate")?;

CodeGenerator::write_source(&crate_dir.join("src/generated"), grammar)?;
}

// Instruct the caller source crate to rebuild itself if the this build crate changes:

{
let build_crate_dir = CargoWorkspace::locate_source_crate("solidity_npm_build")?;
// This build script is not directly depended on by the caller source crate, so we need to manually
// instruct Cargo on our invocation to rebuild if any of our dependencies (or we) change:
for crate_name in &[
env!("CARGO_CRATE_NAME"),
"codegen_grammar",
"codegen_parser_generator",
"solidity_language",
] {
let crate_dir = CargoWorkspace::locate_source_crate(crate_name)?;

rerun_if_changed!(build_crate_dir.join("Cargo.toml").unwrap_str());
rerun_if_changed!(build_crate_dir.join("src").unwrap_str());
// This is not accurate, but it's good enough for now.
rerun_if_changed!(crate_dir.join("Cargo.toml").unwrap_str());
rerun_if_changed!(crate_dir.join("src").unwrap_str());
// Emitting the `rerun-if-changed` for non-existent files always causes a rebuild, so first check if a build
// script event exists. It's worth noting that adding/removing one will trip this up.
if crate_dir.join("build.rs").exists() {
rerun_if_changed!(crate_dir.join("build.rs").unwrap_str());
}
}

return Ok(());
Ok(())
}

0 comments on commit 6570a2f

Please sign in to comment.