11use std:: env;
22use std:: path:: PathBuf ;
3+ use std:: process:: Command ;
34
45fn 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+ 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+
550 let mut builder = cc:: Build :: new ( ) ;
651 let builder = builder
752 . flag ( "-std=c99" )
853 . flag ( "-DLFS_NO_DEBUG" )
954 . flag ( "-DLFS_NO_WARN" )
1055 . flag ( "-DLFS_NO_ERROR" )
11- . file ( "littlefs/lfs.c" )
56+ . include ( & out_path)
57+ . include ( "littlefs" )
58+ . file ( out_lfs_c)
1259 . file ( "littlefs/lfs_util.c" )
1360 . file ( "string.c" ) ;
1461
@@ -29,25 +76,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
2976
3077 builder. compile ( "lfs-sys" ) ;
3178
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-
5179 let bindgen = bindgen:: Builder :: default ( )
5280 . header ( out_lfs_h. into_os_string ( ) . into_string ( ) . unwrap ( ) )
5381 . clang_arg ( "-std=c99" )
0 commit comments