@@ -5,8 +5,13 @@ use std::{
5
5
} ;
6
6
7
7
use serde:: Deserialize ;
8
+ use serde_json:: Value ;
8
9
9
- use crate :: error:: BinaryError ;
10
+ use crate :: {
11
+ error:: { BinaryError , Error } ,
12
+ parse:: MetadataList ,
13
+ utils:: merge_default,
14
+ } ;
10
15
11
16
/// The extension of the binary archive.
12
17
/// Support for different extensions is enabled using features.
@@ -25,11 +30,16 @@ enum Extension {
25
30
// Pkg,
26
31
}
27
32
28
- // TODO: Change follow and global for includes
33
+ #[ derive( Debug , Deserialize ) ]
34
+ #[ serde( untagged) ]
35
+ pub enum Binary {
36
+ Url ( UrlBinary ) ,
37
+ Follow ( FollowBinary ) ,
38
+ }
29
39
30
40
/// Represents one location from where to download library binaries.
31
41
#[ derive( Debug , Deserialize ) ]
32
- pub struct Binary {
42
+ pub struct UrlBinary {
33
43
/// The url from which to download the archived binaries. It suppports:
34
44
///
35
45
/// - Web urls, in the form `http[s]://website/archive.ext`.
@@ -47,67 +57,44 @@ pub struct Binary {
47
57
/// A list of relative paths inside the binary archive that point to a folder containing
48
58
/// package config files. These directories will be prepended to the `PKG_CONFIG_PATH` when
49
59
/// compiling the affected libraries.
50
- pkg_paths : Vec < String > ,
60
+ paths : Option < Vec < String > > ,
61
+ ///
62
+ provides : Option < Vec < String > > ,
63
+ }
51
64
52
- /// Controls if the paths from this binary apply to all packages or just to this one.
53
- global : Option < bool > ,
54
- /// The `system-deps` formatted name of another library which has binaries specified.
55
- /// This library will alias the configuration of the followed one. If `url` is specified
56
- /// alongside this field, it will no longer follow the original configuration.
57
- _follows : Option < String > ,
65
+ #[ derive( Debug , Deserialize ) ]
66
+ pub struct FollowBinary {
67
+ follows : String ,
58
68
}
59
69
60
- impl Binary {
61
- pub fn paths ( & self , name : & str ) -> Result < HashSet < PathBuf > , BinaryError > {
62
- // TODO: Set this binary to follow
63
- //if let Some(follows) = self.follows {
64
- // follow_list.insert(name.clone(), follows);
65
- //}
70
+ pub trait BinaryMetadataListExt {
71
+ fn paths ( & self , package : & str ) -> Result < HashSet < PathBuf > , Error > ;
72
+ }
66
73
74
+ impl BinaryMetadataListExt for MetadataList {
75
+ fn paths ( & self , package : & str ) -> Result < HashSet < PathBuf > , Error > {
67
76
// The binaries are stored in the target dir set by `system_deps_meta`.
77
+
68
78
// If they are specific to a dependency, they live in a subfolder.
69
- let mut dst = PathBuf :: from ( & crate :: BUILD_TARGET_DIR ) ;
70
- if !name. is_empty ( ) {
71
- dst. push ( name) ;
72
- } ;
79
+ let binary_list: HashMap < String , Binary > = self . get ( package, merge_binary) ?;
73
80
74
- // Only download the binaries if there isn't already a valid copy
75
- if !check_valid_dir ( & dst, self . checksum . as_deref ( ) ) ? {
76
- download ( & self . url , & dst) ?;
81
+ match binary_list. get ( package) . unwrap ( ) {
82
+ Binary :: Url ( bin) => {
83
+ let dst = Path :: new ( & crate :: BUILD_TARGET_DIR ) . join ( package) ;
84
+ // Only download the binaries if there isn't already a valid copy
85
+ if !check_valid_dir ( & dst, bin. checksum . as_deref ( ) ) ? {
86
+ download ( & bin. url , & dst) ?;
87
+ }
88
+ Ok ( bin. paths . iter ( ) . flatten ( ) . map ( |p| dst. join ( p) ) . collect ( ) )
89
+ }
90
+ Binary :: Follow ( dep) => self . paths ( & dep. follows ) ,
77
91
}
78
-
79
- Ok ( self . pkg_paths . iter ( ) . map ( |p| dst. join ( p) ) . collect ( ) )
80
- }
81
-
82
- pub fn is_global ( & self ) -> bool {
83
- self . global . unwrap_or_default ( )
84
92
}
85
93
}
86
94
87
95
#[ derive( Debug ) ]
88
96
pub struct BinaryPaths ( HashMap < String , Vec < PathBuf > > ) ;
89
97
90
- impl < I : IntoIterator < Item = ( String , Binary ) > > From < I > for BinaryPaths {
91
- /// Uses the metadata from the cargo manifests and the environment to build a list of urls
92
- /// from where to download binaries for dependencies and adds them to their `PKG_CONFIG_PATH`.
93
- fn from ( binaries : I ) -> Self {
94
- let mut paths: HashMap < String , Vec < PathBuf > > = HashMap :: new ( ) ;
95
-
96
- for ( name, bin) in binaries {
97
- let p = bin. paths ( & name) . unwrap ( ) ;
98
- if bin. is_global ( ) {
99
- paths
100
- . entry ( "" . into ( ) )
101
- . or_default ( )
102
- . extend ( p. iter ( ) . cloned ( ) )
103
- }
104
- paths. entry ( name) . or_default ( ) . extend ( p. into_iter ( ) ) ;
105
- }
106
-
107
- Self ( paths)
108
- }
109
- }
110
-
111
98
impl BinaryPaths {
112
99
pub fn build ( self ) -> String {
113
100
let options = self
@@ -119,7 +106,6 @@ impl BinaryPaths {
119
106
120
107
format ! (
121
108
r#"
122
- /// TODO:
123
109
pub fn get_path(name: &str) -> &[&'static str] {{
124
110
match name {{
125
111
{}
@@ -156,23 +142,6 @@ pub fn get_path(name: &str) -> &[&'static str] {{
156
142
// }),
157
143
// );
158
144
//}
159
-
160
- // Go through the list of follows and if they don't already have binaries,
161
- // link them to the followed one.
162
- //for (from, to) in follow_list {
163
- // if !paths.contains_key(&from) {
164
- // let followed = paths
165
- // .get(to.as_str())
166
- // .unwrap_or_else(|| {
167
- // panic!(
168
- // "The library `{}` tried to follow `{}` but it doesn't exist",
169
- // from, to,
170
- // )
171
- // })
172
- // .clone();
173
- // paths.insert(from, followed);
174
- // };
175
- //}
176
145
}
177
146
178
147
/// Checks if the target directory is valid and if binaries need to be redownloaded.
@@ -314,3 +283,44 @@ fn decompress(file: &[u8], dst: &Path, ext: Extension) -> Result<(), BinaryError
314
283
}
315
284
Ok ( ( ) )
316
285
}
286
+
287
+ pub fn merge_binary ( rhs : Value , lhs : & Value , overwrite : bool ) -> Result < Value , Error > {
288
+ let mut res = merge_default ( rhs, lhs, overwrite) ?;
289
+
290
+ //if overwrite {
291
+ // resolve_follow(&mut res);
292
+ //}
293
+
294
+ Ok ( res)
295
+ }
296
+
297
+ //fn resolve_follow(res: &mut Value) -> Option<()> {
298
+ // let mut follows = Vec::new();
299
+ //
300
+ // for value in res.as_object_mut()?.values_mut() {
301
+ // let Some(provides) = value.get_mut("provides") else {
302
+ // continue;
303
+ // };
304
+ // let provides = provides.take();
305
+ // let Some(arr) = provides.as_array().cloned() else {
306
+ // continue;
307
+ // };
308
+ // follows.push((arr, value.clone()));
309
+ // }
310
+ //
311
+ // let res = res.as_object_mut()?;
312
+ // for (provides, value) in follows {
313
+ // for to in provides {
314
+ // let to = to.as_str()?;
315
+ // let entry = res.entry(to).or_insert(Value::Object(Map::new()));
316
+ // let Some(map) = entry.as_object_mut() else {
317
+ // continue;
318
+ // };
319
+ // let _ = map.entry("follows").or_insert(to.into());
320
+ // }
321
+ // }
322
+ //
323
+ // println!("res {:?}", res);
324
+ //
325
+ // Some(())
326
+ //}
0 commit comments