1
1
use std:: env;
2
2
use std:: path:: PathBuf ;
3
+ use std:: process:: Command ;
3
4
4
5
fn main ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
6
+ let out_path = PathBuf :: from ( env:: var ( "OUT_DIR" ) . unwrap ( ) ) ;
7
+
8
+ // Patch lfs.h to remove the lfs_util import because clang fails to locate the
9
+ // libraries for the custom target (especially string.h)
10
+ // Compilation before that succeeds because it's using gcc,
11
+ // which comes as a distribution with these utils.
12
+ // Turns out lfs_utils is not used in lfs.h, and clang properly finds stdint.h and stdbool,
13
+ // but not string.h
14
+ let lfs_h = std:: fs:: read_to_string ( "littlefs/lfs.h" ) . expect ( "Reading lfs.h succeeds" ) ;
15
+ println ! ( "cargo::rerun-if-changed=littlefs/lfs.h" ) ;
16
+ let out_lfs_h = out_path. join ( "lfs.h" ) ;
17
+ std:: fs:: write (
18
+ & out_lfs_h,
19
+ lfs_h. replace (
20
+ r##"#include "lfs_util.h""## ,
21
+ "#include <stdint.h>\n #include <stdbool.h>" ,
22
+ ) ,
23
+ )
24
+ . expect ( "Failed to write lfs.h" ) ;
25
+
26
+ // maybe patch lfs.c to remove the mount check for the block count
27
+ println ! ( "cargo::rerun-if-changed=littlefs/lfs.c" ) ;
28
+ let out_lfs_c = out_path. join ( "lfs.c" ) ;
29
+ if cfg ! ( feature = "unstable-disable-block-count-check" ) {
30
+ println ! ( "cargo::rerun-if-changed=remove-mount-check.patch" ) ;
31
+ assert ! (
32
+ Command :: new( "patch" )
33
+ . args( [
34
+ "littlefs/lfs.c" ,
35
+ "-o" ,
36
+ & format!( "{}" , out_lfs_c. to_str( ) . unwrap( ) ) ,
37
+ "remove-mount-check.patch"
38
+ ] )
39
+ . spawn( )
40
+ . unwrap( )
41
+ . wait( )
42
+ . unwrap( )
43
+ . success( ) ,
44
+ "Failed to apply patch"
45
+ )
46
+ } else {
47
+ std:: fs:: copy ( "littlefs/lfs.c" , out_path. join ( "lfs.c" ) ) . unwrap ( ) ;
48
+ }
49
+
5
50
let mut builder = cc:: Build :: new ( ) ;
6
51
let builder = builder
7
52
. flag ( "-std=c99" )
8
53
. flag ( "-DLFS_NO_DEBUG" )
9
54
. flag ( "-DLFS_NO_WARN" )
10
55
. flag ( "-DLFS_NO_ERROR" )
11
- . file ( "littlefs/lfs.c" )
56
+ . include ( & out_path)
57
+ . include ( "littlefs" )
58
+ . file ( out_lfs_c)
12
59
. file ( "littlefs/lfs_util.c" )
13
60
. file ( "string.c" ) ;
14
61
@@ -29,25 +76,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
29
76
30
77
builder. compile ( "lfs-sys" ) ;
31
78
32
- // Patch lfs.h to remove the lfs_util import because clang fails to locate the
33
- // libraries for the custom target (especially string.h)
34
- // Compilation before that succeeds because it's using gcc,
35
- // which comes as a distribution with these utils.
36
- // Turns out lfs_utils is not used in lfs.h, and clang properly finds stdint.h and stdbool,
37
- // but not string.h
38
- let lfs_h = std:: fs:: read_to_string ( "littlefs/lfs.h" ) . expect ( "Reading lfs.h succeeds" ) ;
39
- println ! ( "cargo::rerun-if-changed=littlefs/lfs.h" ) ;
40
- let out_path = PathBuf :: from ( env:: var ( "OUT_DIR" ) . unwrap ( ) ) ;
41
- let out_lfs_h = out_path. join ( "lfs.h" ) ;
42
- std:: fs:: write (
43
- & out_lfs_h,
44
- lfs_h. replace (
45
- r##"#include "lfs_util.h""## ,
46
- "#include <stdint.h>\n #include <stdbool.h>" ,
47
- ) ,
48
- )
49
- . expect ( "Failed to write lfs.h" ) ;
50
-
51
79
let bindgen = bindgen:: Builder :: default ( )
52
80
. header ( out_lfs_h. into_os_string ( ) . into_string ( ) . unwrap ( ) )
53
81
. clang_arg ( "-std=c99" )
0 commit comments