@@ -3,23 +3,62 @@ use std::{
3
3
path:: { Path , PathBuf } ,
4
4
} ;
5
5
6
+ use semver:: { Comparator , Op , Version } ;
7
+
6
8
fn main ( ) {
7
- let version = format ! (
8
- "{}.{}.{}" ,
9
- libcamera_sys:: LIBCAMERA_VERSION_MAJOR ,
10
- libcamera_sys:: LIBCAMERA_VERSION_MINOR ,
11
- libcamera_sys:: LIBCAMERA_VERSION_PATCH
9
+ let libcamera_version = Version :: new (
10
+ libcamera_sys:: LIBCAMERA_VERSION_MAJOR as _ ,
11
+ libcamera_sys:: LIBCAMERA_VERSION_MINOR as _ ,
12
+ libcamera_sys:: LIBCAMERA_VERSION_PATCH as _ ,
12
13
) ;
13
14
14
- let versioned_files = Path :: new ( "versioned_files" ) . join ( & version) ;
15
+ let versioned_files = Path :: new ( "versioned_files" ) ;
16
+ let mut candidates = std:: fs:: read_dir ( versioned_files)
17
+ . unwrap ( )
18
+ . filter_map ( |entry| entry. ok ( ) )
19
+ . filter_map ( |entry| {
20
+ let path = entry. path ( ) ;
21
+ let version = Version :: parse ( path. file_name ( ) ?. to_str ( ) ?) . ok ( ) ?;
15
22
16
- if std:: fs:: metadata ( & versioned_files) . is_err ( ) {
17
- panic ! ( "Unsupported version of libcamera detected: {version}" ) ;
18
- }
23
+ Some ( ( version, path) )
24
+ } )
25
+ . collect :: < Vec < _ > > ( ) ;
26
+ candidates. sort_unstable_by_key ( |( version, _) | version. clone ( ) ) ;
27
+
28
+ // Filter to only compatible versions
29
+ let matching = candidates. iter ( ) . filter ( |( candidate, _) | {
30
+ #[ cfg( feature = "libcamera_semver_versioning" ) ]
31
+ let op = Op :: Caret ;
32
+ #[ cfg( not( feature = "libcamera_semver_versioning" ) ) ]
33
+ let op = Op :: Exact ;
34
+
35
+ let comparator = Comparator {
36
+ op,
37
+ major : candidate. major ,
38
+ minor : Some ( candidate. minor ) ,
39
+ patch : Some ( candidate. patch ) ,
40
+ pre : Default :: default ( ) ,
41
+ } ;
42
+
43
+ comparator. matches ( & libcamera_version)
44
+ } ) ;
45
+
46
+ // And take the most recent compatible version
47
+ let ( _, selected_version) = match matching. max_by_key ( |( version, _) | version. clone ( ) ) {
48
+ Some ( v) => v,
49
+ None => panic ! (
50
+ "Unsupported version of libcamera detected: {libcamera_version}\n supported versions are: \n {}" ,
51
+ candidates
52
+ . iter( )
53
+ . map( |( v, _) | format!( "\t {v}" ) )
54
+ . collect:: <Vec <_>>( )
55
+ . join( "\n " )
56
+ ) ,
57
+ } ;
19
58
20
59
let out_path = PathBuf :: from ( env:: var ( "OUT_DIR" ) . unwrap ( ) ) ;
21
60
22
61
for file in [ "controls.rs" , "properties.rs" ] {
23
- std:: fs:: copy ( versioned_files . join ( file) , out_path. join ( file) ) . unwrap ( ) ;
62
+ std:: fs:: copy ( selected_version . join ( file) , out_path. join ( file) ) . unwrap ( ) ;
24
63
}
25
64
}
0 commit comments