Skip to content

Commit d38d663

Browse files
committed
fix(src): create CACHE_DIR on demand
Also, move default location to the OUT_DIR of nginx-sys. This should fix the build with read-only source directory. Fixes #85.
1 parent 4685304 commit d38d663

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

nginx-src/src/download.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,22 +116,26 @@ static VERIFIER: LazyLock<Option<SignatureVerifier>> = LazyLock::new(|| {
116116
.ok()
117117
});
118118

119-
fn make_cache_dir() -> io::Result<PathBuf> {
120-
let base_dir = env::var("CARGO_MANIFEST_DIR")
119+
static CACHE_DIR: LazyLock<PathBuf> = LazyLock::new(|| {
120+
let base_dir = env::var("OUT_DIR")
121121
.map(PathBuf::from)
122122
.unwrap_or_else(|_| env::current_dir().expect("Failed to get current directory"));
123-
// Choose `.cache` relative to the manifest directory (nginx-src) as the default cache directory
123+
// Choose `.cache` relative to the OUT_DIR of the caller (nginx-sys) as the default cache directory
124124
// Environment variable `CACHE_DIR` overrides this
125125
// Recommendation: set env "CACHE_DIR = { value = ".cache", relative = true }" in
126126
// `.cargo/config.toml` in your project
127127
let cache_dir = env::var("CACHE_DIR")
128128
.map(PathBuf::from)
129129
.unwrap_or(base_dir.join(".cache"));
130+
130131
if !cache_dir.exists() {
131-
fs::create_dir_all(&cache_dir)?;
132+
fs::create_dir_all(&cache_dir)
133+
.map_err(|err| format!("Failed to create {cache_dir:?}: {err}"))
134+
.unwrap();
132135
}
133-
Ok(cache_dir)
134-
}
136+
137+
cache_dir
138+
});
135139

136140
/// Downloads a tarball from the specified URL into the `.cache` directory.
137141
fn download(cache_dir: &Path, url: &str) -> Result<PathBuf, Box<dyn StdError + Send + Sync>> {
@@ -228,12 +232,11 @@ pub fn prepare(source_dir: &Path, build_dir: &Path) -> io::Result<(PathBuf, Vec<
228232
fs::create_dir_all(&extract_output_base_dir)?;
229233
}
230234

231-
let cache_dir = make_cache_dir()?;
232235
let mut options = vec![];
233236

234237
// Download NGINX only if NGX_VERSION is set.
235238
let source_dir = if let Ok(version) = env::var(NGINX_SOURCE.variable) {
236-
let archive_path = get_archive(&cache_dir, &NGINX_SOURCE, version.as_str())?;
239+
let archive_path = get_archive(&CACHE_DIR, &NGINX_SOURCE, version.as_str())?;
237240
let output_base_dir: PathBuf = env::var("OUT_DIR").unwrap().into();
238241
extract_archive(&archive_path, &output_base_dir)?
239242
} else {
@@ -246,7 +249,7 @@ pub fn prepare(source_dir: &Path, build_dir: &Path) -> io::Result<(PathBuf, Vec<
246249
continue;
247250
};
248251

249-
let archive_path = get_archive(&cache_dir, source, &requested)?;
252+
let archive_path = get_archive(&CACHE_DIR, source, &requested)?;
250253
let output_dir = extract_archive(&archive_path, &extract_output_base_dir)?;
251254
let output_dir = output_dir.to_string_lossy();
252255
options.push(format!("--with-{name}={output_dir}"));

0 commit comments

Comments
 (0)