Skip to content

Commit 0732766

Browse files
authored
recursive processes use the current executable by default (#484)
- Recursive processes spawn the currently running executable by default, removing the annoyance of mixing different compiler builds during contract compilation. - Remove the unused `once_cell` dependency. Signed-off-by: xermicus <bigcyrill@hotmail.com>
1 parent 971ab2c commit 0732766

File tree

4 files changed

+23
-31
lines changed

4 files changed

+23
-31
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/resolc/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ clap = { workspace = true }
2323
hex = { workspace = true }
2424
inkwell = { workspace = true }
2525
log = { workspace = true }
26-
once_cell = { workspace = true }
2726
path-slash = { workspace = true }
2827
rayon = { workspace = true, optional = true }
2928
semver = { workspace = true }

crates/resolc/src/process/native_process.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
use std::io::Write;
44
use std::path::PathBuf;
55
use std::process::Command;
6+
use std::sync::LazyLock;
7+
use std::sync::OnceLock;
68

7-
use once_cell::sync::OnceCell;
89
use revive_common::deserialize_from_slice;
910
use revive_common::EXIT_CODE_SUCCESS;
1011
use revive_solc_json_interface::standard_json::output::error::source_location::SourceLocation;
@@ -16,8 +17,12 @@ use super::Input;
1617
use super::Output;
1718
use super::Process;
1819

19-
/// The overriden executable name used when the compiler is run as a library.
20-
pub static EXECUTABLE: OnceCell<PathBuf> = OnceCell::new();
20+
/// The default executable path, lazily initialized from the current binary.
21+
static DEFAULT_EXECUTABLE: LazyLock<PathBuf> =
22+
LazyLock::new(|| std::env::current_exe().expect("Should have an executable"));
23+
24+
/// Override for the executable path, used when the compiler is run as a library.
25+
pub static EXECUTABLE: OnceLock<PathBuf> = OnceLock::new();
2126

2227
pub struct NativeProcess;
2328

@@ -61,10 +66,7 @@ impl Process for NativeProcess {
6166
I: Serialize,
6267
O: DeserializeOwned,
6368
{
64-
let executable = EXECUTABLE
65-
.get()
66-
.cloned()
67-
.unwrap_or_else(|| std::env::current_exe().expect("Should have an executable"));
69+
let executable = EXECUTABLE.get().unwrap_or(&DEFAULT_EXECUTABLE);
6870
let mut command = Command::new(executable.as_path());
6971
command.stdin(std::process::Stdio::piped());
7072
command.stdout(std::process::Stdio::piped());

crates/resolc/src/test_utils.rs

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ use std::collections::BTreeMap;
44
use std::collections::BTreeSet;
55
use std::collections::HashMap;
66
use std::fmt::Display;
7-
use std::path::PathBuf;
87
use std::sync::Mutex;
98

10-
use once_cell::sync::Lazy;
119
use revive_common::MetadataHash;
1210
use revive_llvm_context::initialize_llvm;
1311
use revive_llvm_context::DebugConfig;
@@ -24,16 +22,19 @@ use revive_solc_json_interface::SolcStandardJsonInputSettingsSelection;
2422
use revive_solc_json_interface::SolcStandardJsonInputSource;
2523
use revive_solc_json_interface::SolcStandardJsonOutput;
2624
use revive_solc_json_interface::SolcStandardJsonOutputErrorHandler;
25+
use std::sync::LazyLock;
2726

2827
use crate::project::Project;
2928
use crate::solc::solc_compiler::SolcCompiler;
3029
use crate::solc::Compiler;
3130

32-
static PVM_BLOB_CACHE: Lazy<Mutex<HashMap<CachedBlob, Vec<u8>>>> = Lazy::new(Default::default);
33-
static EVM_BLOB_CACHE: Lazy<Mutex<HashMap<CachedBlob, Vec<u8>>>> = Lazy::new(Default::default);
34-
static EVM_RUNTIME_BLOB_CACHE: Lazy<Mutex<HashMap<CachedBlob, Vec<u8>>>> =
35-
Lazy::new(Default::default);
36-
static YUL_IR_CACHE: Lazy<Mutex<HashMap<CachedBlob, String>>> = Lazy::new(Default::default);
31+
static PVM_BLOB_CACHE: LazyLock<Mutex<HashMap<CachedBlob, Vec<u8>>>> =
32+
LazyLock::new(Default::default);
33+
static EVM_BLOB_CACHE: LazyLock<Mutex<HashMap<CachedBlob, Vec<u8>>>> =
34+
LazyLock::new(Default::default);
35+
static EVM_RUNTIME_BLOB_CACHE: LazyLock<Mutex<HashMap<CachedBlob, Vec<u8>>>> =
36+
LazyLock::new(Default::default);
37+
static YUL_IR_CACHE: LazyLock<Mutex<HashMap<CachedBlob, String>>> = LazyLock::new(Default::default);
3738

3839
const DEBUG_CONFIG: revive_llvm_context::DebugConfig = DebugConfig::new(None, true);
3940

@@ -78,9 +79,6 @@ pub fn build_solidity_with_options(
7879
inkwell::support::enable_llvm_pretty_stack_trace();
7980
initialize_llvm(PolkaVMTarget::PVM, crate::DEFAULT_EXECUTABLE_NAME, &[]);
8081

81-
let _ = crate::process::native_process::EXECUTABLE
82-
.set(PathBuf::from(crate::r#const::DEFAULT_EXECUTABLE_NAME));
83-
8482
let solc = SolcCompiler::new(SolcCompiler::DEFAULT_EXECUTABLE_NAME.to_owned())?;
8583
let solc_version = solc.version()?;
8684

@@ -146,9 +144,6 @@ pub fn build_solidity_with_options_evm(
146144
check_dependencies();
147145
inkwell::support::enable_llvm_pretty_stack_trace();
148146
initialize_llvm(PolkaVMTarget::PVM, crate::DEFAULT_EXECUTABLE_NAME, &[]);
149-
let _ = crate::process::native_process::EXECUTABLE
150-
.set(PathBuf::from(crate::r#const::DEFAULT_EXECUTABLE_NAME));
151-
152147
let solc = SolcCompiler::new(SolcCompiler::DEFAULT_EXECUTABLE_NAME.to_owned())?;
153148
let mut input = SolcStandardJsonInput::try_from_solidity_sources(
154149
None,
@@ -204,9 +199,6 @@ pub fn build_solidity_and_detect_missing_libraries<T: ToString>(
204199

205200
inkwell::support::enable_llvm_pretty_stack_trace();
206201
initialize_llvm(PolkaVMTarget::PVM, crate::DEFAULT_EXECUTABLE_NAME, &[]);
207-
let _ = crate::process::native_process::EXECUTABLE
208-
.set(PathBuf::from(crate::r#const::DEFAULT_EXECUTABLE_NAME));
209-
210202
let solc = SolcCompiler::new(SolcCompiler::DEFAULT_EXECUTABLE_NAME.to_owned())?;
211203
let solc_version = solc.version()?;
212204
let mut input = SolcStandardJsonInput::try_from_solidity_sources(
@@ -249,9 +241,6 @@ pub fn build_yul<T: ToString + Display>(
249241
inkwell::support::enable_llvm_pretty_stack_trace();
250242
initialize_llvm(PolkaVMTarget::PVM, crate::DEFAULT_EXECUTABLE_NAME, &[]);
251243

252-
let _ = crate::process::native_process::EXECUTABLE
253-
.set(PathBuf::from(crate::r#const::DEFAULT_EXECUTABLE_NAME));
254-
255244
let mut build = Project::try_from_yul_sources(
256245
sources
257246
.iter()
@@ -298,9 +287,6 @@ pub fn build_yul_standard_json(
298287
inkwell::support::enable_llvm_pretty_stack_trace();
299288
initialize_llvm(PolkaVMTarget::PVM, crate::DEFAULT_EXECUTABLE_NAME, &[]);
300289

301-
let _ = crate::process::native_process::EXECUTABLE
302-
.set(PathBuf::from(crate::r#const::DEFAULT_EXECUTABLE_NAME));
303-
304290
let solc = SolcCompiler::new(SolcCompiler::DEFAULT_EXECUTABLE_NAME.to_owned())?;
305291
let mut output = solc.validate_yul_standard_json(&mut solc_input, &mut vec![])?;
306292
if output.has_errors() {
@@ -427,6 +413,8 @@ pub fn sources<T: ToString>(sources: &[(T, T)]) -> BTreeMap<String, SolcStandard
427413
}
428414

429415
/// Checks if the required executables are present in `${PATH}`.
416+
/// Also initializes the resolc executable path for subprocess spawning,
417+
/// since `std::env::current_exe()` returns the test runner binary in tests.
430418
fn check_dependencies() {
431419
for executable in [
432420
crate::r#const::DEFAULT_EXECUTABLE_NAME,
@@ -439,6 +427,10 @@ fn check_dependencies() {
439427
"The `{executable}` executable not found in ${{PATH}}"
440428
);
441429
}
430+
431+
let _ = crate::process::native_process::EXECUTABLE.set(
432+
which::which(crate::r#const::DEFAULT_EXECUTABLE_NAME).expect("resolc should be in ${PATH}"),
433+
);
442434
}
443435

444436
/// The internal EVM bytecode compile helper.

0 commit comments

Comments
 (0)