1+ use serde:: Deserialize ;
2+ use std:: path:: PathBuf ;
3+ use std:: process:: Command ;
4+
15fn main ( ) {
26 println ! ( "cargo:rerun-if-changed=src/gtk_helper.c" ) ;
37 println ! ( "cargo:rerun-if-changed=src/cocoa_helper.m" ) ;
8+ println ! ( "cargo:rerun-if-changed=src/win32_helper.c" ) ;
49
510 #[ cfg( target_os = "macos" ) ]
611 compile_cocoa_helper ( ) ;
712
8- #[ cfg( not ( any ( target_os = "macos" , target_os = "windows" ) ) ) ]
13+ #[ cfg( target_os = "linux" ) ]
914 compile_gtk_helper ( ) ;
15+
16+ #[ cfg( target_os = "windows" ) ]
17+ compile_win32_helper ( ) ;
1018}
1119
12- #[ cfg( not ( any ( target_os = "macos" , target_os = "windows" ) ) ) ]
20+ #[ cfg( target_os = "linux" ) ]
1321fn compile_gtk_helper ( ) {
1422 let cflags = std:: process:: Command :: new ( "pkg-config" )
1523 . args ( & [ "--cflags" , "gtk+-3.0" ] )
@@ -31,3 +39,53 @@ fn compile_cocoa_helper() {
3139 build. file ( "src/cocoa_helper.m" ) ;
3240 build. compile ( "cocoa" ) ;
3341}
42+
43+ #[ cfg( target_os = "windows" ) ]
44+ fn compile_win32_helper ( ) {
45+ let wv_sys_path = find_wv_sys_path ( ) ;
46+ let mut build = cc:: Build :: new ( ) ;
47+ build. file ( "src/win32_helper.c" ) ;
48+ build. include ( & wv_sys_path. join ( "libs\\ include" ) ) ;
49+ build. include ( & wv_sys_path. join ( "webview\\ core\\ include" ) ) ;
50+ build. compile ( "windows" ) ;
51+ }
52+
53+ #[ derive( Debug , Deserialize ) ]
54+ struct Metadata {
55+ packages : Vec < Package > ,
56+ }
57+ #[ derive( Debug , Deserialize ) ]
58+ struct Package {
59+ name : String ,
60+ manifest_path : PathBuf ,
61+ }
62+
63+ fn find_wv_sys_path ( ) -> PathBuf {
64+ let output = Command :: new ( "cargo" )
65+ . arg ( "metadata" )
66+ . output ( )
67+ . expect ( "Failed to run cargo metadata" ) ;
68+
69+ let metadata: Metadata =
70+ serde_json:: from_slice ( & output. stdout ) . expect ( "Failed to parse cargo metadata" ) ;
71+
72+ let wv_sys_pkg = metadata
73+ . packages
74+ . into_iter ( )
75+ . find ( |pkg| pkg. name == "wv-sys" )
76+ . expect ( "Could not find 'wv-sys' package in metadata" ) ;
77+
78+ let path = wv_sys_pkg
79+ . manifest_path
80+ . parent ( )
81+ . expect ( "manifest_path has no parent" ) ;
82+
83+ if !path. exists ( ) {
84+ panic ! (
85+ "Failed to find headers at: {}. wv-sys internal layout may have changed." ,
86+ path. display( )
87+ ) ;
88+ }
89+
90+ path. to_path_buf ( )
91+ }
0 commit comments