Skip to content

Commit

Permalink
chore: remove wasm cfgs
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Jan 30, 2024
1 parent c4cda2d commit ce09bd7
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 115 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ jobs:
- uses: taiki-e/install-action@cargo-hack
- uses: Swatinem/rust-cache@v2
- name: cargo hack
run: cargo hack check --feature-powerset --depth 1 --all-targets
run: cargo hack check --feature-powerset --depth 2 --all-targets --skip rustls,openssl

clippy:
name: clippy
Expand Down
36 changes: 16 additions & 20 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,8 @@ rustdoc-args = ["--cfg", "docsrs"]
all-features = true

[dependencies]
alloy-primitives = { version = "0.6", default-features = false, features = [
"std",
"serde",
"rand",
] }
alloy-json-abi = { version = "0.6", default-features = false, features = ["std", "serde_json"] }
alloy-primitives = { version = "0.6", features = ["serde", "rand"] }
alloy-json-abi = { version = "0.6", features = ["serde_json"] }

solang-parser = { version = "=0.3.3", default-features = false }

Expand All @@ -38,7 +34,6 @@ serde_json = "1.0"
tracing = "0.1.37"

tiny-keccak = { version = "2.0.2", default-features = false }
sha2 = { version = "0.10.7", default-features = false, optional = true }
md-5 = "0.10.5"

semver = { version = "1.0", features = ["serde"] }
Expand All @@ -56,20 +51,22 @@ dunce = "1.0"
rayon = "1.7"
path-slash = "0.2.1"
cfg-if = "1.0.0"
memmap2 = "0.9.0"

# async
futures-util = { version = "0.3", optional = true }
tokio = { version = "1.32", features = ["rt-multi-thread"], optional = true }

# project-util
tempfile = { version = "3.8.0", optional = true }
fs_extra = { version = "1.3.0", optional = true }
rand = { version = "0.8", optional = true }
futures-util = { version = "0.3.28", optional = true }
memmap2 = "0.9.0"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
# svm
home = "0.5.5"
svm = { package = "svm-rs", version = "0.3", default-features = false, features = [
"blocking",
], optional = true }
svm-builds = { package = "svm-rs-builds", version = "0.3", optional = true }
tokio = { version = "1.32", features = ["rt-multi-thread"] }
svm = { package = "svm-rs", version = "0.3", default-features = false, optional = true }
svm-builds = { package = "svm-rs-builds", version = "0.3", default-features = false, optional = true }
sha2 = { version = "0.10.7", default-features = false, optional = true }

[dev-dependencies]
criterion = { version = "0.5", features = ["async_tokio"] }
Expand Down Expand Up @@ -107,12 +104,11 @@ default = ["rustls"]

full = ["async", "svm-solc"]

async = ["tokio/process", "tokio/io-util", "tokio/fs", "tokio/time", "futures-util"]
svm-solc = ["svm", "svm-builds", "sha2"]
async = ["dep:futures-util", "dep:tokio", "tokio/fs", "tokio/process", "tokio/io-util"]
svm-solc = ["dep:svm", "dep:svm-builds", "dep:sha2", "dep:tokio"]

# Utilities for creating and testing project workspaces
project-util = ["tempfile", "fs_extra", "rand"]
# Utilities for creating and testing project workspaces.
project-util = ["dep:tempfile", "dep:fs_extra", "dep:rand"]

tests = []
rustls = ["svm?/rustls"]
openssl = ["svm?/openssl"]
84 changes: 36 additions & 48 deletions src/compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,27 +64,23 @@ pub static SUPPORTS_BASE_PATH: Lazy<VersionReq> =
pub static SUPPORTS_INCLUDE_PATH: Lazy<VersionReq> =
Lazy::new(|| VersionReq::parse(">=0.8.8").unwrap());

#[cfg(any(test, feature = "tests"))]
use std::sync::Mutex;

#[cfg(any(test, feature = "tests"))]
static LOCK: Lazy<Mutex<()>> = Lazy::new(|| Mutex::new(()));

/// take the lock in tests, we use this to enforce that
/// a test does not run while a compiler version is being installed
///
/// This ensures that only one thread installs a missing `solc` exe.
/// Instead of taking this lock in `Solc::blocking_install`, the lock should be taken before
/// installation is detected.
#[cfg(any(test, feature = "tests"))]
pub(crate) fn take_solc_installer_lock() -> std::sync::MutexGuard<'static, ()> {
#[cfg(feature = "svm-solc")]
#[cfg(test)]
pub(crate) fn take_solc_installer_lock() -> impl Drop {
static LOCK: Lazy<std::sync::Mutex<()>> = Lazy::new(|| std::sync::Mutex::new(()));
LOCK.lock().unwrap()
}

/// A list of upstream Solc releases, used to check which version
/// we should download.
/// The boolean value marks whether there was an error accessing the release list
#[cfg(all(feature = "svm-solc", not(target_arch = "wasm32")))]
#[cfg(feature = "svm-solc")]
pub static RELEASES: Lazy<(svm::Releases, Vec<Version>, bool)> =
Lazy::new(|| match serde_json::from_str::<svm::Releases>(svm_builds::RELEASE_LIST_JSON) {
Ok(releases) => {
Expand Down Expand Up @@ -159,14 +155,12 @@ impl Default for Solc {
if let Ok(solc) = std::env::var("SOLC_PATH") {
return Solc::new(solc);
}
#[cfg(not(target_arch = "wasm32"))]

if let Some(solc) = Solc::svm_global_version()
.and_then(|vers| Solc::find_svm_installed_version(vers.to_string()).ok())
.flatten()
{
if let Some(solc) = Solc::svm_global_version()
.and_then(|vers| Solc::find_svm_installed_version(vers.to_string()).ok())
.flatten()
{
return solc;
}
return solc;
}

Solc::new(SOLC)
Expand Down Expand Up @@ -220,9 +214,8 @@ impl Solc {
/// Returns the directory in which [svm](https://github.com/roynalnaruto/svm-rs) stores all versions
///
/// This will be:
/// `~/.svm` on unix, if it exists
/// - `~/.svm` on unix, if it exists
/// - $XDG_DATA_HOME (~/.local/share/svm) if the svm folder does not exist.
#[cfg(not(target_arch = "wasm32"))]
pub fn svm_home() -> Option<PathBuf> {
match home::home_dir().map(|dir| dir.join(".svm")) {
Some(dir) => {
Expand All @@ -241,15 +234,13 @@ impl Solc {
///
/// This will read the version string (eg: "0.8.9") that the `~/.svm/.global_version` file
/// contains
#[cfg(not(target_arch = "wasm32"))]
pub fn svm_global_version() -> Option<Version> {
let home = Self::svm_home()?;
let version = std::fs::read_to_string(home.join(".global_version")).ok()?;
Version::parse(&version).ok()
}

/// Returns the list of all solc instances installed at `SVM_HOME`
#[cfg(not(target_arch = "wasm32"))]
pub fn installed_versions() -> Vec<SolcVersion> {
Self::svm_home()
.map(|home| {
Expand All @@ -264,7 +255,7 @@ impl Solc {

/// Returns the list of all versions that are available to download and marking those which are
/// already installed.
#[cfg(all(feature = "svm-solc", not(target_arch = "wasm32")))]
#[cfg(feature = "svm-solc")]
pub fn all_versions() -> Vec<SolcVersion> {
let mut all_versions = Self::installed_versions();
let mut uniques = all_versions
Expand Down Expand Up @@ -297,7 +288,6 @@ impl Solc {
/// assert_eq!(solc, Some(Solc::new("~/.svm/0.8.9/solc-0.8.9")));
/// # Ok::<_, Box<dyn std::error::Error>>(())
/// ```
#[cfg(not(target_arch = "wasm32"))]
pub fn find_svm_installed_version(version: impl AsRef<str>) -> Result<Option<Self>> {
let version = version.as_ref();
let solc = Self::svm_home()
Expand All @@ -324,7 +314,7 @@ impl Solc {
/// assert_eq!(solc, Solc::new("~/.svm/0.8.9/solc-0.8.9"));
/// # Ok::<_, Box<dyn std::error::Error>>(())
/// ```
#[cfg(all(not(target_arch = "wasm32"), feature = "svm-solc"))]
#[cfg(feature = "svm-solc")]
pub fn find_or_install_svm_version(version: impl AsRef<str>) -> Result<Self> {
let version = version.as_ref();
if let Some(solc) = Solc::find_svm_installed_version(version)? {
Expand All @@ -348,7 +338,7 @@ impl Solc {
/// to build it, and returns it.
///
/// If the required compiler version is not installed, it also proceeds to install it.
#[cfg(all(feature = "svm-solc", not(target_arch = "wasm32")))]
#[cfg(feature = "svm-solc")]
pub fn detect_version(source: &Source) -> Result<Version> {
// detects the required solc version
let sol_version = Self::source_version_req(source)?;
Expand All @@ -359,9 +349,9 @@ impl Solc {
/// used to build it, and returns it.
///
/// If the required compiler version is not installed, it also proceeds to install it.
#[cfg(all(feature = "svm-solc", not(target_arch = "wasm32")))]
#[cfg(feature = "svm-solc")]
pub fn ensure_installed(sol_version: &VersionReq) -> Result<Version> {
#[cfg(any(test, feature = "tests"))]
#[cfg(test)]
let _lock = take_solc_installer_lock();

// load the local / remote versions
Expand Down Expand Up @@ -429,33 +419,33 @@ impl Solc {
/// # Ok(())
/// # }
/// ```
#[cfg(all(feature = "svm-solc", not(target_arch = "wasm32")))]
#[cfg(feature = "svm-solc")]
pub async fn install(version: &Version) -> std::result::Result<Self, svm::SolcVmError> {
trace!("installing solc version \"{}\"", version);
crate::report::solc_installation_start(version);
let result = svm::install(version).await;
crate::report::solc_installation_success(version);
result.map(Solc::new)
match svm::install(version).await {
Ok(path) => {
crate::report::solc_installation_success(version);
Ok(Solc::new(path))
}
Err(err) => {
crate::report::solc_installation_error(version, &err.to_string());
Err(err)
}
}
}

/// Blocking version of `Self::install`
#[cfg(all(feature = "svm-solc", not(target_arch = "wasm32")))]
#[cfg(feature = "svm-solc")]
pub fn blocking_install(version: &Version) -> std::result::Result<Self, svm::SolcVmError> {
use crate::utils::RuntimeOrHandle;

trace!("blocking installing solc version \"{}\"", version);
crate::report::solc_installation_start(version);
// the async version `svm::install` is used instead of `svm::blocking_intsall`
// The async version `svm::install` is used instead of `svm::blocking_intsall`
// because the underlying `reqwest::blocking::Client` does not behave well
// in tokio rt. see https://github.com/seanmonstar/reqwest/issues/1017
cfg_if::cfg_if! {
if #[cfg(target_arch = "wasm32")] {
let installation = svm::blocking_install(version);
} else {
let installation = RuntimeOrHandle::new().block_on(svm::install(version));
}
};
match installation {
// inside of a Tokio runtime. See: https://github.com/seanmonstar/reqwest/issues/1017
match RuntimeOrHandle::new().block_on(svm::install(version)) {
Ok(path) => {
crate::report::solc_installation_success(version);
Ok(Solc::new(path))
Expand All @@ -469,7 +459,7 @@ impl Solc {

/// Verify that the checksum for this version of solc is correct. We check against the SHA256
/// checksum from the build information published by [binaries.soliditylang.org](https://binaries.soliditylang.org/)
#[cfg(all(feature = "svm-solc", not(target_arch = "wasm32")))]
#[cfg(feature = "svm-solc")]
pub fn verify_checksum(&self) -> Result<()> {
let version = self.version_short()?;
let mut version_path = svm::version_path(version.to_string().as_str());
Expand Down Expand Up @@ -876,8 +866,7 @@ mod tests {
}

#[test]
// This test might be a bit hard to maintain
#[cfg(all(feature = "svm-solc", not(target_arch = "wasm32")))]
#[cfg(feature = "svm-solc")]
fn test_detect_version() {
for (pragma, expected) in [
// pinned
Expand All @@ -901,9 +890,8 @@ mod tests {
#[test]
#[cfg(feature = "full")]
fn test_find_installed_version_path() {
// this test does not take the lock by default, so we need to manually
// add it here.
let _lock = LOCK.lock();
// This test does not take the lock by default, so we need to manually add it here.
let _lock = take_solc_installer_lock();
let ver = "0.8.6";
let version = Version::from_str(ver).unwrap();
if utils::installed_versions(svm::SVM_DATA_DIR.as_path())
Expand All @@ -918,7 +906,7 @@ mod tests {
}

#[test]
#[cfg(all(feature = "svm-solc", not(target_arch = "wasm32")))]
#[cfg(feature = "svm-solc")]
fn can_install_solc_in_tokio_rt() {
let version = Version::from_str("0.8.6").unwrap();
let rt = tokio::runtime::Runtime::new().unwrap();
Expand Down
5 changes: 3 additions & 2 deletions src/compile/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ pub struct ProjectCompiler<'a, T: ArtifactOutput> {
impl<'a, T: ArtifactOutput> ProjectCompiler<'a, T> {
/// Create a new `ProjectCompiler` to bootstrap the compilation process of the project's
/// sources.
#[cfg(all(feature = "svm-solc", not(target_arch = "wasm32")))]
#[cfg(feature = "svm-solc")]
pub fn new(project: &'a Project<T>) -> Result<Self> {
Self::with_sources(project, project.paths.read_input_files()?)
}
Expand All @@ -141,7 +141,7 @@ impl<'a, T: ArtifactOutput> ProjectCompiler<'a, T> {
///
/// Multiple (`Solc` -> `Sources`) pairs can be compiled in parallel if the `Project` allows
/// multiple `jobs`, see [`crate::Project::set_solc_jobs()`].
#[cfg(all(feature = "svm-solc", not(target_arch = "wasm32")))]
#[cfg(feature = "svm-solc")]
pub fn with_sources(project: &'a Project<T>, sources: Sources) -> Result<Self> {
let graph = Graph::resolve_sources(&project.paths, sources)?;
let (versions, edges) = graph.into_sources_by_version(project.offline)?;
Expand Down Expand Up @@ -472,6 +472,7 @@ impl FilteredCompilerSources {
}

#[cfg(test)]
#[cfg(all(feature = "project-util", feature = "svm-solc"))]
fn sources(&self) -> &VersionedFilteredSources {
match self {
FilteredCompilerSources::Sequential(v) => v,
Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub enum SolcError {
{2:?}"#
)]
FailedResolveImport(Box<SolcError>, PathBuf, PathBuf),
#[cfg(all(feature = "svm-solc", not(target_arch = "wasm32")))]
#[cfg(feature = "svm-solc")]
#[error(transparent)]
SvmError(#[from] svm::SolcVmError),
#[error("no contracts found at \"{0}\"")]
Expand Down
Loading

0 comments on commit ce09bd7

Please sign in to comment.