Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/resolc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ clap = { workspace = true }
hex = { workspace = true }
inkwell = { workspace = true }
log = { workspace = true }
once_cell = { workspace = true }
path-slash = { workspace = true }
rayon = { workspace = true, optional = true }
semver = { workspace = true }
Expand Down
16 changes: 9 additions & 7 deletions crates/resolc/src/process/native_process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
use std::io::Write;
use std::path::PathBuf;
use std::process::Command;
use std::sync::LazyLock;
use std::sync::OnceLock;

use once_cell::sync::OnceCell;
use revive_common::deserialize_from_slice;
use revive_common::EXIT_CODE_SUCCESS;
use revive_solc_json_interface::standard_json::output::error::source_location::SourceLocation;
Expand All @@ -16,8 +17,12 @@ use super::Input;
use super::Output;
use super::Process;

/// The overriden executable name used when the compiler is run as a library.
pub static EXECUTABLE: OnceCell<PathBuf> = OnceCell::new();
/// The default executable path, lazily initialized from the current binary.
static DEFAULT_EXECUTABLE: LazyLock<PathBuf> =
LazyLock::new(|| std::env::current_exe().expect("Should have an executable"));

/// Override for the executable path, used when the compiler is run as a library.
pub static EXECUTABLE: OnceLock<PathBuf> = OnceLock::new();

pub struct NativeProcess;

Expand Down Expand Up @@ -61,10 +66,7 @@ impl Process for NativeProcess {
I: Serialize,
O: DeserializeOwned,
{
let executable = EXECUTABLE
.get()
.cloned()
.unwrap_or_else(|| std::env::current_exe().expect("Should have an executable"));
let executable = EXECUTABLE.get().unwrap_or(&DEFAULT_EXECUTABLE);
let mut command = Command::new(executable.as_path());
command.stdin(std::process::Stdio::piped());
command.stdout(std::process::Stdio::piped());
Expand Down
36 changes: 14 additions & 22 deletions crates/resolc/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ use std::collections::BTreeMap;
use std::collections::BTreeSet;
use std::collections::HashMap;
use std::fmt::Display;
use std::path::PathBuf;
use std::sync::Mutex;

use once_cell::sync::Lazy;
use revive_common::MetadataHash;
use revive_llvm_context::initialize_llvm;
use revive_llvm_context::DebugConfig;
Expand All @@ -24,16 +22,19 @@ use revive_solc_json_interface::SolcStandardJsonInputSettingsSelection;
use revive_solc_json_interface::SolcStandardJsonInputSource;
use revive_solc_json_interface::SolcStandardJsonOutput;
use revive_solc_json_interface::SolcStandardJsonOutputErrorHandler;
use std::sync::LazyLock;

use crate::project::Project;
use crate::solc::solc_compiler::SolcCompiler;
use crate::solc::Compiler;

static PVM_BLOB_CACHE: Lazy<Mutex<HashMap<CachedBlob, Vec<u8>>>> = Lazy::new(Default::default);
static EVM_BLOB_CACHE: Lazy<Mutex<HashMap<CachedBlob, Vec<u8>>>> = Lazy::new(Default::default);
static EVM_RUNTIME_BLOB_CACHE: Lazy<Mutex<HashMap<CachedBlob, Vec<u8>>>> =
Lazy::new(Default::default);
static YUL_IR_CACHE: Lazy<Mutex<HashMap<CachedBlob, String>>> = Lazy::new(Default::default);
static PVM_BLOB_CACHE: LazyLock<Mutex<HashMap<CachedBlob, Vec<u8>>>> =
LazyLock::new(Default::default);
static EVM_BLOB_CACHE: LazyLock<Mutex<HashMap<CachedBlob, Vec<u8>>>> =
LazyLock::new(Default::default);
static EVM_RUNTIME_BLOB_CACHE: LazyLock<Mutex<HashMap<CachedBlob, Vec<u8>>>> =
LazyLock::new(Default::default);
static YUL_IR_CACHE: LazyLock<Mutex<HashMap<CachedBlob, String>>> = LazyLock::new(Default::default);

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

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

let _ = crate::process::native_process::EXECUTABLE
.set(PathBuf::from(crate::r#const::DEFAULT_EXECUTABLE_NAME));

let solc = SolcCompiler::new(SolcCompiler::DEFAULT_EXECUTABLE_NAME.to_owned())?;
let solc_version = solc.version()?;

Expand Down Expand Up @@ -146,9 +144,6 @@ pub fn build_solidity_with_options_evm(
check_dependencies();
inkwell::support::enable_llvm_pretty_stack_trace();
initialize_llvm(PolkaVMTarget::PVM, crate::DEFAULT_EXECUTABLE_NAME, &[]);
let _ = crate::process::native_process::EXECUTABLE
.set(PathBuf::from(crate::r#const::DEFAULT_EXECUTABLE_NAME));

let solc = SolcCompiler::new(SolcCompiler::DEFAULT_EXECUTABLE_NAME.to_owned())?;
let mut input = SolcStandardJsonInput::try_from_solidity_sources(
None,
Expand Down Expand Up @@ -204,9 +199,6 @@ pub fn build_solidity_and_detect_missing_libraries<T: ToString>(

inkwell::support::enable_llvm_pretty_stack_trace();
initialize_llvm(PolkaVMTarget::PVM, crate::DEFAULT_EXECUTABLE_NAME, &[]);
let _ = crate::process::native_process::EXECUTABLE
.set(PathBuf::from(crate::r#const::DEFAULT_EXECUTABLE_NAME));

let solc = SolcCompiler::new(SolcCompiler::DEFAULT_EXECUTABLE_NAME.to_owned())?;
let solc_version = solc.version()?;
let mut input = SolcStandardJsonInput::try_from_solidity_sources(
Expand Down Expand Up @@ -249,9 +241,6 @@ pub fn build_yul<T: ToString + Display>(
inkwell::support::enable_llvm_pretty_stack_trace();
initialize_llvm(PolkaVMTarget::PVM, crate::DEFAULT_EXECUTABLE_NAME, &[]);

let _ = crate::process::native_process::EXECUTABLE
.set(PathBuf::from(crate::r#const::DEFAULT_EXECUTABLE_NAME));

let mut build = Project::try_from_yul_sources(
sources
.iter()
Expand Down Expand Up @@ -298,9 +287,6 @@ pub fn build_yul_standard_json(
inkwell::support::enable_llvm_pretty_stack_trace();
initialize_llvm(PolkaVMTarget::PVM, crate::DEFAULT_EXECUTABLE_NAME, &[]);

let _ = crate::process::native_process::EXECUTABLE
.set(PathBuf::from(crate::r#const::DEFAULT_EXECUTABLE_NAME));

let solc = SolcCompiler::new(SolcCompiler::DEFAULT_EXECUTABLE_NAME.to_owned())?;
let mut output = solc.validate_yul_standard_json(&mut solc_input, &mut vec![])?;
if output.has_errors() {
Expand Down Expand Up @@ -427,6 +413,8 @@ pub fn sources<T: ToString>(sources: &[(T, T)]) -> BTreeMap<String, SolcStandard
}

/// Checks if the required executables are present in `${PATH}`.
/// Also initializes the resolc executable path for subprocess spawning,
/// since `std::env::current_exe()` returns the test runner binary in tests.
fn check_dependencies() {
for executable in [
crate::r#const::DEFAULT_EXECUTABLE_NAME,
Expand All @@ -439,6 +427,10 @@ fn check_dependencies() {
"The `{executable}` executable not found in ${{PATH}}"
);
}

let _ = crate::process::native_process::EXECUTABLE.set(
which::which(crate::r#const::DEFAULT_EXECUTABLE_NAME).expect("resolc should be in ${PATH}"),
);
}

/// The internal EVM bytecode compile helper.
Expand Down
Loading