diff --git a/Cargo.lock b/Cargo.lock index 216b4aa0b..f93a87c30 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2885,6 +2885,7 @@ dependencies = [ "byteorder", "bytes", "cc", + "cfg_aliases", "chrono", "clap", "codspeed-divan-compat", diff --git a/Cargo.toml b/Cargo.toml index 8005fb895..1752f07bc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -125,6 +125,9 @@ rouille = { version = "3.6", optional = true, default-features = false, features syslog = { version = "7", optional = true } version-compare = { version = "0.1.1", optional = true } +[build-dependencies] +cfg_aliases = "0.2.1" + [dev-dependencies] assert_cmd = "2.0.13" cc = "1.0" diff --git a/build.rs b/build.rs new file mode 100644 index 000000000..1630d17a0 --- /dev/null +++ b/build.rs @@ -0,0 +1,39 @@ +use cfg_aliases::cfg_aliases; + +fn main() { + cfg_aliases! { + // All remote cache backends + any_cache_remote: { + any( + feature = "azure", + feature = "gcs", + feature = "gha", + feature = "memcached", + feature = "redis", + feature = "s3", + feature = "webdav", + feature = "oss", + feature = "cos" + ) + }, + // HTTP-based remote cache backends (excludes memcached and redis) + any_http_remote: { + any( + feature = "azure", + feature = "gcs", + feature = "gha", + feature = "s3", + feature = "webdav", + feature = "oss", + feature = "cos" + ) + }, + // Distributed compilation features + any_dist: { + any( + feature = "dist-client", + feature = "dist-server" + ) + }, + } +} diff --git a/src/cache/cache.rs b/src/cache/cache.rs index 906c69f5f..375f33747 100644 --- a/src/cache/cache.rs +++ b/src/cache/cache.rs @@ -30,17 +30,7 @@ use crate::cache::oss::OSSCache; use crate::cache::redis::RedisCache; #[cfg(feature = "s3")] use crate::cache::s3::S3Cache; -#[cfg(any( - feature = "azure", - feature = "gcs", - feature = "gha", - feature = "memcached", - feature = "redis", - feature = "s3", - feature = "webdav", - feature = "oss", - feature = "cos" -))] +#[cfg(any_cache_remote)] use crate::cache::utils::normalize_key; #[cfg(feature = "webdav")] use crate::cache::webdav::WebdavCache; @@ -135,33 +125,13 @@ pub trait Storage: Send + Sync { } /// Wrapper for opendal::Operator that adds basedirs support -#[cfg(any( - feature = "azure", - feature = "gcs", - feature = "gha", - feature = "memcached", - feature = "redis", - feature = "s3", - feature = "webdav", - feature = "oss", - feature = "cos" -))] +#[cfg(any_cache_remote)] pub struct RemoteStorage { operator: opendal::Operator, basedirs: Vec>, } -#[cfg(any( - feature = "azure", - feature = "gcs", - feature = "gha", - feature = "memcached", - feature = "redis", - feature = "s3", - feature = "webdav", - feature = "oss", - feature = "cos" -))] +#[cfg(any_cache_remote)] impl RemoteStorage { pub fn new(operator: opendal::Operator, basedirs: Vec>) -> Self { Self { operator, basedirs } @@ -169,17 +139,7 @@ impl RemoteStorage { } /// Implement storage for operator. -#[cfg(any( - feature = "azure", - feature = "gcs", - feature = "gha", - feature = "memcached", - feature = "redis", - feature = "s3", - feature = "webdav", - feature = "oss", - feature = "cos" -))] +#[cfg(any_cache_remote)] #[async_trait] impl Storage for RemoteStorage { async fn get(&self, key: &str) -> Result { @@ -283,17 +243,7 @@ impl Storage for RemoteStorage { /// Build a single cache storage from CacheType /// Helper function used by storage_from_config for both single and multi-level caches -#[cfg(any( - feature = "azure", - feature = "gcs", - feature = "gha", - feature = "memcached", - feature = "redis", - feature = "s3", - feature = "webdav", - feature = "oss", - feature = "cos" -))] +#[cfg(any_cache_remote)] pub fn build_single_cache( cache_type: &CacheType, basedirs: &[Vec], @@ -493,17 +443,7 @@ pub fn storage_from_config( config: &Config, pool: &tokio::runtime::Handle, ) -> Result> { - #[cfg(any( - feature = "azure", - feature = "gcs", - feature = "gha", - feature = "memcached", - feature = "redis", - feature = "s3", - feature = "webdav", - feature = "oss", - feature = "cos" - ))] + #[cfg(any_cache_remote)] if let Some(cache_type) = &config.cache { debug!("Configuring single cache from CacheType"); return build_single_cache(cache_type, &config.basedirs, pool); diff --git a/src/cache/mod.rs b/src/cache/mod.rs index 643b07333..1b027d2be 100644 --- a/src/cache/mod.rs +++ b/src/cache/mod.rs @@ -39,15 +39,7 @@ pub(crate) mod utils; #[cfg(feature = "webdav")] pub mod webdav; -#[cfg(any( - feature = "azure", - feature = "gcs", - feature = "gha", - feature = "s3", - feature = "webdav", - feature = "oss", - feature = "cos" -))] +#[cfg(any_http_remote)] pub(crate) mod http_client; pub use crate::cache::cache::*; diff --git a/src/config.rs b/src/config.rs index aeeca7f2f..353c02095 100644 --- a/src/config.rs +++ b/src/config.rs @@ -18,7 +18,7 @@ use crate::util::normalize_win_path; use directories::ProjectDirs; use fs::File; use fs_err as fs; -#[cfg(any(feature = "dist-client", feature = "dist-server"))] +#[cfg(any_dist)] use serde::ser::Serializer; use serde::{ Deserialize, Serialize, @@ -128,10 +128,10 @@ pub fn parse_size(val: &str) -> Option { u64::from_str(val).ok().map(|size| size * multiplier) } -#[cfg(any(feature = "dist-client", feature = "dist-server"))] +#[cfg(any_dist)] #[derive(Clone, Debug, PartialEq, Eq)] pub struct HTTPUrl(reqwest::Url); -#[cfg(any(feature = "dist-client", feature = "dist-server"))] +#[cfg(any_dist)] impl Serialize for HTTPUrl { fn serialize(&self, serializer: S) -> StdResult where @@ -140,7 +140,7 @@ impl Serialize for HTTPUrl { serializer.serialize_str(self.0.as_str()) } } -#[cfg(any(feature = "dist-client", feature = "dist-server"))] +#[cfg(any_dist)] impl<'a> Deserialize<'a> for HTTPUrl { fn deserialize(deserializer: D) -> StdResult where @@ -152,7 +152,7 @@ impl<'a> Deserialize<'a> for HTTPUrl { Ok(HTTPUrl(url)) } } -#[cfg(any(feature = "dist-client", feature = "dist-server"))] +#[cfg(any_dist)] fn parse_http_url(url: &str) -> Result { use std::net::SocketAddr; let url = if let Ok(sa) = url.parse::() { @@ -170,7 +170,7 @@ fn parse_http_url(url: &str) -> Result { } Ok(url) } -#[cfg(any(feature = "dist-client", feature = "dist-server"))] +#[cfg(any_dist)] impl HTTPUrl { pub fn from_url(u: reqwest::Url) -> Self { HTTPUrl(u) @@ -621,9 +621,9 @@ impl Default for DistAuth { #[serde(deny_unknown_fields)] pub struct DistConfig { pub auth: DistAuth, - #[cfg(any(feature = "dist-client", feature = "dist-server"))] + #[cfg(any_dist)] pub scheduler_url: Option, - #[cfg(not(any(feature = "dist-client", feature = "dist-server")))] + #[cfg(not(any_dist))] pub scheduler_url: Option, pub cache_dir: PathBuf, pub toolchains: Vec, @@ -2204,13 +2204,13 @@ key_prefix = "cosprefix" auth: DistAuth::Token { token: "secrettoken".to_owned() }, - #[cfg(any(feature = "dist-client", feature = "dist-server"))] + #[cfg(any_dist)] scheduler_url: Some( parse_http_url("http://1.2.3.4:10600") .map(|url| { HTTPUrl::from_url(url) }) .expect("Scheduler url must be valid url str") ), - #[cfg(not(any(feature = "dist-client", feature = "dist-server")))] + #[cfg(not(any_dist))] scheduler_url: Some("http://1.2.3.4:10600".to_owned()), cache_dir: PathBuf::from("/home/user/.cache/sccache-dist-client"), toolchains: vec![], diff --git a/src/dist/mod.rs b/src/dist/mod.rs index 6bc1024aa..45643b815 100644 --- a/src/dist/mod.rs +++ b/src/dist/mod.rs @@ -28,16 +28,16 @@ use std::sync::Mutex; use crate::errors::*; -#[cfg(any(feature = "dist-client", feature = "dist-server"))] +#[cfg(any_dist)] mod cache; #[cfg(feature = "dist-client")] pub mod client_auth; -#[cfg(any(feature = "dist-client", feature = "dist-server"))] +#[cfg(any_dist)] pub mod http; #[cfg(test)] mod test; -#[cfg(any(feature = "dist-client", feature = "dist-server"))] +#[cfg(any_dist)] pub use crate::dist::cache::TcCache; // TODO: paths (particularly outputs, which are accessed by an unsandboxed program) diff --git a/src/util.rs b/src/util.rs index 2720c1fa6..4868b30c5 100644 --- a/src/util.rs +++ b/src/util.rs @@ -943,7 +943,7 @@ pub fn daemonize() -> Result<()> { /// --- /// /// More details could be found at https://github.com/mozilla/sccache/pull/1563 -#[cfg(any(feature = "dist-server", feature = "dist-client"))] +#[cfg(any_dist)] pub fn new_reqwest_blocking_client() -> reqwest::blocking::Client { reqwest::blocking::Client::builder() .pool_max_idle_per_host(0)