@@ -116,22 +116,26 @@ static VERIFIER: LazyLock<Option<SignatureVerifier>> = LazyLock::new(|| {
116
116
. ok ( )
117
117
} ) ;
118
118
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 " )
121
121
. map ( PathBuf :: from)
122
122
. 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
124
124
// Environment variable `CACHE_DIR` overrides this
125
125
// Recommendation: set env "CACHE_DIR = { value = ".cache", relative = true }" in
126
126
// `.cargo/config.toml` in your project
127
127
let cache_dir = env:: var ( "CACHE_DIR" )
128
128
. map ( PathBuf :: from)
129
129
. unwrap_or ( base_dir. join ( ".cache" ) ) ;
130
+
130
131
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 ( ) ;
132
135
}
133
- Ok ( cache_dir)
134
- }
136
+
137
+ cache_dir
138
+ } ) ;
135
139
136
140
/// Downloads a tarball from the specified URL into the `.cache` directory.
137
141
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<
228
232
fs:: create_dir_all ( & extract_output_base_dir) ?;
229
233
}
230
234
231
- let cache_dir = make_cache_dir ( ) ?;
232
235
let mut options = vec ! [ ] ;
233
236
234
237
// Download NGINX only if NGX_VERSION is set.
235
238
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 ( ) ) ?;
237
240
let output_base_dir: PathBuf = env:: var ( "OUT_DIR" ) . unwrap ( ) . into ( ) ;
238
241
extract_archive ( & archive_path, & output_base_dir) ?
239
242
} else {
@@ -246,7 +249,7 @@ pub fn prepare(source_dir: &Path, build_dir: &Path) -> io::Result<(PathBuf, Vec<
246
249
continue ;
247
250
} ;
248
251
249
- let archive_path = get_archive ( & cache_dir , source, & requested) ?;
252
+ let archive_path = get_archive ( & CACHE_DIR , source, & requested) ?;
250
253
let output_dir = extract_archive ( & archive_path, & extract_output_base_dir) ?;
251
254
let output_dir = output_dir. to_string_lossy ( ) ;
252
255
options. push ( format ! ( "--with-{name}={output_dir}" ) ) ;
0 commit comments