3
3
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
4
5
5
mod common {
6
- use std:: env;
7
- use std:: path:: PathBuf ;
6
+ use std:: { env, fs , io } ;
7
+ use std:: path:: { Path , PathBuf } ;
8
8
9
9
lazy_static ! {
10
10
pub static ref OUTDIR_PATH : PathBuf = PathBuf :: from( env:: var( "OUT_DIR" ) . unwrap( ) ) . join( "gecko" ) ;
11
11
}
12
12
13
- pub const STRUCTS_DEBUG_FILE : & ' static str = "structs_debug.rs" ;
14
- pub const STRUCTS_RELEASE_FILE : & ' static str = "structs_release.rs" ;
15
- pub const BINDINGS_FILE : & ' static str = "bindings.rs" ;
16
-
17
- #[ derive( Clone , Copy , PartialEq ) ]
18
- pub enum BuildType {
19
- Debug ,
20
- Release ,
21
- }
22
-
23
- pub fn structs_file ( build_type : BuildType ) -> & ' static str {
24
- match build_type {
25
- BuildType :: Debug => STRUCTS_DEBUG_FILE ,
26
- BuildType :: Release => STRUCTS_RELEASE_FILE
13
+ /// Copy contents of one directory into another.
14
+ /// It currently only does a shallow copy.
15
+ pub fn copy_dir < P , Q , F > ( from : P , to : Q , callback : F ) -> io:: Result < ( ) >
16
+ where P : AsRef < Path > , Q : AsRef < Path > , F : Fn ( & Path ) {
17
+ let to = to. as_ref ( ) ;
18
+ for entry in from. as_ref ( ) . read_dir ( ) ? {
19
+ let entry = entry?;
20
+ let path = entry. path ( ) ;
21
+ callback ( & path) ;
22
+ fs:: copy ( & path, to. join ( entry. file_name ( ) ) ) ?;
27
23
}
24
+ Ok ( ( ) )
28
25
}
29
26
}
30
27
@@ -39,9 +36,28 @@ mod bindings {
39
36
use std:: fs:: { self , File } ;
40
37
use std:: io:: { Read , Write } ;
41
38
use std:: path:: { Path , PathBuf } ;
39
+ use std:: process:: { Command , exit} ;
42
40
use std:: sync:: Mutex ;
43
41
use std:: time:: SystemTime ;
44
42
use super :: common:: * ;
43
+ use super :: super :: PYTHON ;
44
+
45
+ const STRUCTS_DEBUG_FILE : & ' static str = "structs_debug.rs" ;
46
+ const STRUCTS_RELEASE_FILE : & ' static str = "structs_release.rs" ;
47
+ const BINDINGS_FILE : & ' static str = "bindings.rs" ;
48
+
49
+ #[ derive( Clone , Copy , PartialEq ) ]
50
+ enum BuildType {
51
+ Debug ,
52
+ Release ,
53
+ }
54
+
55
+ fn structs_file ( build_type : BuildType ) -> & ' static str {
56
+ match build_type {
57
+ BuildType :: Debug => STRUCTS_DEBUG_FILE ,
58
+ BuildType :: Release => STRUCTS_RELEASE_FILE
59
+ }
60
+ }
45
61
46
62
lazy_static ! {
47
63
static ref INCLUDE_RE : Regex = Regex :: new( r#"#include\s*"(.+?)""# ) . unwrap( ) ;
@@ -60,11 +76,6 @@ mod bindings {
60
76
pub static ref LAST_MODIFIED : Mutex <SystemTime > =
61
77
Mutex :: new( get_modified_time( & env:: current_exe( ) . unwrap( ) )
62
78
. expect( "Failed to get modified time of executable" ) ) ;
63
- static ref BINDING_DISTDIR_PATH : PathBuf = {
64
- let path = DISTDIR_PATH . join( "rust_bindings/style" ) ;
65
- fs:: create_dir_all( & path) . expect( "Fail to create bindings dir in dist" ) ;
66
- path
67
- } ;
68
79
}
69
80
70
81
fn get_modified_time ( file : & Path ) -> Option < SystemTime > {
@@ -237,8 +248,6 @@ mod bindings {
237
248
}
238
249
let bytes = result. into_bytes ( ) ;
239
250
File :: create ( & out_file) . unwrap ( ) . write_all ( & bytes) . expect ( "Unable to write output" ) ;
240
- File :: create ( & BINDING_DISTDIR_PATH . join ( file) ) . unwrap ( )
241
- . write_all ( & bytes) . expect ( "Unable to write output to binding dist" ) ;
242
251
}
243
252
244
253
fn get_arc_types ( ) -> Vec < String > {
@@ -276,7 +285,7 @@ mod bindings {
276
285
}
277
286
}
278
287
279
- pub fn generate_structs ( build_type : BuildType ) {
288
+ fn generate_structs ( build_type : BuildType ) {
280
289
let mut builder = Builder :: get_initial_builder ( build_type)
281
290
. enable_cxx_namespaces ( )
282
291
. with_codegen_config ( CodegenConfig {
@@ -565,7 +574,7 @@ mod bindings {
565
574
write_binding_file ( builder, structs_file ( build_type) , & fixups) ;
566
575
}
567
576
568
- pub fn setup_logging ( ) {
577
+ fn setup_logging ( ) -> bool {
569
578
use log;
570
579
571
580
struct BuildLogger {
@@ -594,20 +603,23 @@ mod bindings {
594
603
}
595
604
}
596
605
597
- log :: set_logger ( |log_level| {
598
- log_level . set ( log:: LogLevelFilter :: Debug ) ;
599
- Box :: new ( BuildLogger {
600
- file : env :: var ( "STYLO_BUILD_LOG" ) . ok ( ) . and_then ( |path| {
601
- fs:: File :: create ( path) . ok ( ) . map ( Mutex :: new)
602
- } ) ,
603
- filter : env :: var ( "STYLO_BUILD_FILTER" ) . ok ( )
604
- . unwrap_or_else ( || "bindgen" . to_owned ( ) ) ,
606
+ if let Ok ( path ) = env :: var ( "STYLO_BUILD_LOG" ) {
607
+ log:: set_logger ( |log_level| {
608
+ log_level . set ( log :: LogLevelFilter :: Debug ) ;
609
+ Box :: new ( BuildLogger {
610
+ file : fs:: File :: create ( path) . ok ( ) . map ( Mutex :: new) ,
611
+ filter : env :: var ( "STYLO_BUILD_FILTER" ) . ok ( )
612
+ . unwrap_or_else ( || "bindgen" . to_owned ( ) ) ,
613
+ } )
605
614
} )
606
- } )
607
- . expect ( "Failed to set logger." ) ;
615
+ . expect ( "Failed to set logger." ) ;
616
+ true
617
+ } else {
618
+ false
619
+ }
608
620
}
609
621
610
- pub fn generate_bindings ( ) {
622
+ fn generate_bindings ( ) {
611
623
let mut builder = Builder :: get_initial_builder ( BuildType :: Release )
612
624
. disable_name_namespacing ( )
613
625
. with_codegen_config ( CodegenConfig {
@@ -816,52 +828,70 @@ mod bindings {
816
828
}
817
829
write_binding_file ( builder, BINDINGS_FILE , & Vec :: new ( ) ) ;
818
830
}
819
- }
820
831
821
- #[ cfg( not( feature = "bindgen" ) ) ]
822
- mod bindings {
823
- use std:: fs;
824
- use std:: path:: { Path , PathBuf } ;
825
- use super :: common:: * ;
826
-
827
- lazy_static ! {
828
- static ref BINDINGS_PATH : PathBuf = Path :: new( file!( ) ) . parent( ) . unwrap( ) . join( "gecko_bindings" ) ;
832
+ fn generate_atoms ( ) {
833
+ let script = Path :: new ( file ! ( ) ) . parent ( ) . unwrap ( ) . join ( "binding_tools" ) . join ( "regen_atoms.py" ) ;
834
+ println ! ( "cargo:rerun-if-changed={}" , script. display( ) ) ;
835
+ let status = Command :: new ( & * PYTHON )
836
+ . arg ( & script)
837
+ . arg ( DISTDIR_PATH . as_os_str ( ) )
838
+ . arg ( OUTDIR_PATH . as_os_str ( ) )
839
+ . status ( )
840
+ . unwrap ( ) ;
841
+ if !status. success ( ) {
842
+ exit ( 1 ) ;
843
+ }
829
844
}
830
845
831
- pub fn setup_logging ( ) { }
846
+ pub fn generate ( ) {
847
+ use std:: thread;
848
+ macro_rules! run_tasks {
849
+ ( $( $task: expr, ) +) => {
850
+ if setup_logging( ) {
851
+ $( $task; ) +
852
+ } else {
853
+ let threads = vec![ $( thread:: spawn( || $task) ) ,+] ;
854
+ for thread in threads. into_iter( ) {
855
+ thread. join( ) . unwrap( ) ;
856
+ }
857
+ }
858
+ }
859
+ }
860
+ run_tasks ! {
861
+ generate_structs( BuildType :: Debug ) ,
862
+ generate_structs( BuildType :: Release ) ,
863
+ generate_bindings( ) ,
864
+ generate_atoms( ) ,
865
+ }
832
866
833
- pub fn generate_structs ( build_type : BuildType ) {
834
- let file = structs_file ( build_type) ;
835
- let source = BINDINGS_PATH . join ( file) ;
836
- println ! ( "cargo:rerun-if-changed={}" , source. display( ) ) ;
837
- fs:: copy ( source, OUTDIR_PATH . join ( file) ) . unwrap ( ) ;
867
+ // Copy all generated files to dist for the binding package
868
+ let path = DISTDIR_PATH . join ( "rust_bindings/style" ) ;
869
+ if path. exists ( ) {
870
+ fs:: remove_dir_all ( & path) . expect ( "Fail to remove binding dir in dist" ) ;
871
+ }
872
+ fs:: create_dir_all ( & path) . expect ( "Fail to create bindings dir in dist" ) ;
873
+ copy_dir ( & * OUTDIR_PATH , & path, |_| { } ) . expect ( "Fail to copy generated files to dist dir" ) ;
838
874
}
875
+ }
876
+
877
+ #[ cfg( not( feature = "bindgen" ) ) ]
878
+ mod bindings {
879
+ use std:: path:: Path ;
880
+ use super :: common:: * ;
839
881
840
- pub fn generate_bindings ( ) {
841
- let source = BINDINGS_PATH . join ( BINDINGS_FILE ) ;
842
- println ! ( "cargo:rerun-if-changed={}" , source. display( ) ) ;
843
- fs:: copy ( source, OUTDIR_PATH . join ( BINDINGS_FILE ) ) . unwrap ( ) ;
882
+ pub fn generate ( ) {
883
+ let dir = Path :: new ( file ! ( ) ) . parent ( ) . unwrap ( ) . join ( "gecko/generated" ) ;
884
+ println ! ( "cargo:rerun-if-changed={}" , dir. display( ) ) ;
885
+ copy_dir ( & dir, & * OUTDIR_PATH , |path| {
886
+ println ! ( "cargo:rerun-if-changed={}" , path. display( ) ) ;
887
+ } ) . expect ( "Fail to copy generated files to out dir" ) ;
844
888
}
845
889
}
846
890
847
891
pub fn generate ( ) {
848
892
use self :: common:: * ;
849
- use std:: { env , fs , thread } ;
893
+ use std:: fs ;
850
894
println ! ( "cargo:rerun-if-changed=build_gecko.rs" ) ;
851
895
fs:: create_dir_all ( & * OUTDIR_PATH ) . unwrap ( ) ;
852
- bindings:: setup_logging ( ) ;
853
- if env:: var ( "STYLO_BUILD_LOG" ) . is_ok ( ) {
854
- bindings:: generate_structs ( BuildType :: Debug ) ;
855
- bindings:: generate_structs ( BuildType :: Release ) ;
856
- bindings:: generate_bindings ( ) ;
857
- } else {
858
- let threads = vec ! [
859
- thread:: spawn( || bindings:: generate_structs( BuildType :: Debug ) ) ,
860
- thread:: spawn( || bindings:: generate_structs( BuildType :: Release ) ) ,
861
- thread:: spawn( || bindings:: generate_bindings( ) ) ,
862
- ] ;
863
- for t in threads. into_iter ( ) {
864
- t. join ( ) . unwrap ( ) ;
865
- }
866
- }
896
+ bindings:: generate ( ) ;
867
897
}
0 commit comments