2021-03-29 10:59:14 +03:00
#[ cfg(windows) ]
fn build_windows ( ) {
cc ::Build ::new ( ) . file ( " src/windows.cc " ) . compile ( " windows " ) ;
2021-04-09 04:58:50 +03:00
println! ( " cargo:rustc-link-lib=WtsApi32 " ) ;
2021-03-29 10:59:14 +03:00
println! ( " cargo:rerun-if-changed=build.rs " ) ;
println! ( " cargo:rerun-if-changed=windows.cc " ) ;
}
#[ cfg(all(windows, feature = " inline " )) ]
fn build_manifest ( ) {
use std ::io ::Write ;
if std ::env ::var ( " PROFILE " ) . unwrap ( ) = = " release " {
let mut res = winres ::WindowsResource ::new ( ) ;
res . set_icon ( " icon.ico " )
. set_language ( winapi ::um ::winnt ::MAKELANGID (
winapi ::um ::winnt ::LANG_ENGLISH ,
winapi ::um ::winnt ::SUBLANG_ENGLISH_US ,
) )
. set_manifest_file ( " manifest.xml " ) ;
match res . compile ( ) {
Err ( e ) = > {
write! ( std ::io ::stderr ( ) , " {} " , e ) . unwrap ( ) ;
std ::process ::exit ( 1 ) ;
}
Ok ( _ ) = > { }
}
}
}
2022-04-24 18:39:15 +03:00
#[ cfg(all(windows, feature = " with_rc " )) ]
fn build_rc_source ( ) {
use simple_rc ::{ generate_with_conf , Config , ConfigItem } ;
generate_with_conf ( & Config {
outfile : " src/rc.rs " . to_owned ( ) ,
confs : vec ! [ ConfigItem {
inc : " resources " . to_owned ( ) ,
exc : vec ! [ ] ,
suppressed_front : " resources " . to_owned ( ) ,
} ] ,
} )
. unwrap ( ) ;
}
2022-09-02 15:41:50 +03:00
fn check_environment ( ) {
// Check env variable
let env_list = vec! [ " LLVM_HOME " , " VCPKG_ROOT " ] ;
for env in env_list . iter ( ) {
if std ::env ::var ( env ) . is_err ( ) {
panic! ( " Missing environment variable: {:?} " , env ) ;
} ;
}
}
2021-03-29 10:59:14 +03:00
fn install_oboe ( ) {
let target_os = std ::env ::var ( " CARGO_CFG_TARGET_OS " ) . unwrap ( ) ;
if target_os ! = " android " {
return ;
}
let mut target_arch = std ::env ::var ( " CARGO_CFG_TARGET_ARCH " ) . unwrap ( ) ;
if target_arch = = " x86_64 " {
target_arch = " x64 " . to_owned ( ) ;
} else if target_arch = = " aarch64 " {
target_arch = " arm64 " . to_owned ( ) ;
} else {
target_arch = " arm " . to_owned ( ) ;
}
2022-05-12 12:55:49 +03:00
let target = format! ( " {} -android " , target_arch ) ;
2021-03-29 10:59:14 +03:00
let vcpkg_root = std ::env ::var ( " VCPKG_ROOT " ) . unwrap ( ) ;
let mut path : std ::path ::PathBuf = vcpkg_root . into ( ) ;
path . push ( " installed " ) ;
path . push ( target ) ;
println! (
" {} " ,
format! (
" cargo:rustc-link-search={} " ,
path . join ( " lib " ) . to_str ( ) . unwrap ( )
)
) ;
println! ( " cargo:rustc-link-lib=oboe " ) ;
println! ( " cargo:rustc-link-lib=c++ " ) ;
println! ( " cargo:rustc-link-lib=OpenSLES " ) ;
// I always got some strange link error with oboe, so as workaround, put oboe.cc into oboe src: src/common/AudioStreamBuilder.cpp
// also to avoid libc++_shared not found issue, cp ndk's libc++_shared.so to jniLibs, e.g.
// ./flutter_hbb/android/app/src/main/jniLibs/arm64-v8a/libc++_shared.so
// let include = path.join("include");
//cc::Build::new().file("oboe.cc").include(include).compile("oboe_wrapper");
}
2022-05-17 14:59:37 +03:00
fn gen_flutter_rust_bridge ( ) {
2022-09-02 09:58:14 +03:00
// Get dependent of flutter
2022-09-02 12:06:40 +03:00
if ! std ::path ::Path ::new ( " ./flutter/.packages " ) . exists ( ) {
2022-09-02 09:58:14 +03:00
std ::process ::Command ::new ( " flutter " )
2022-09-02 12:06:40 +03:00
. args ( [ " pub " , " get " ] )
. current_dir ( " ./flutter " )
. output ( )
2022-09-02 15:41:50 +03:00
. expect ( " Failed to execute flutter pub get " ) ;
2022-09-02 09:58:14 +03:00
} ;
2022-07-27 17:56:28 +03:00
let llvm_path = match std ::env ::var ( " LLVM_HOME " ) {
2022-09-02 15:41:50 +03:00
Ok ( path ) = > Some ( vec! [ path ] ) ,
2022-09-02 12:06:40 +03:00
Err ( _ ) = > panic! ( " Failure to get environments " ) ,
2022-07-27 17:56:28 +03:00
} ;
2022-09-02 12:06:40 +03:00
2022-05-17 14:59:37 +03:00
// Tell Cargo that if the given file changes, to rerun this build script.
2022-05-25 15:50:32 +03:00
println! ( " cargo:rerun-if-changed=src/flutter_ffi.rs " ) ;
2022-05-17 14:59:37 +03:00
// settings for fbr_codegen
let opts = lib_flutter_rust_bridge_codegen ::Opts {
// Path of input Rust code
2022-05-25 15:50:32 +03:00
rust_input : " src/flutter_ffi.rs " . to_string ( ) ,
2022-05-17 14:59:37 +03:00
// Path of output generated Dart code
dart_output : " flutter/lib/generated_bridge.dart " . to_string ( ) ,
2022-06-02 11:13:34 +03:00
// Path of output generated C header
c_output : Some ( vec! [ " flutter/macos/Runner/bridge_generated.h " . to_string ( ) ] ) ,
2022-05-17 14:59:37 +03:00
// for other options lets use default
2022-07-27 17:56:28 +03:00
llvm_path ,
2022-05-17 14:59:37 +03:00
.. Default ::default ( )
} ;
// run fbr_codegen
lib_flutter_rust_bridge_codegen ::frb_codegen ( opts ) . unwrap ( ) ;
}
2021-03-29 10:59:14 +03:00
fn main ( ) {
2022-09-02 15:41:50 +03:00
check_environment ( ) ;
2022-05-12 12:55:49 +03:00
hbb_common ::gen_version ( ) ;
install_oboe ( ) ;
// there is problem with cfg(target_os) in build.rs, so use our workaround
2022-05-25 09:30:19 +03:00
// let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap();
// if target_os == "android" || target_os == "ios" {
2022-06-02 11:13:34 +03:00
gen_flutter_rust_bridge ( ) ;
2022-05-25 09:30:19 +03:00
// return;
// }
2022-04-24 18:39:15 +03:00
#[ cfg(all(windows, feature = " with_rc " )) ]
build_rc_source ( ) ;
2021-03-29 10:59:14 +03:00
#[ cfg(all(windows, feature = " inline " )) ]
build_manifest ( ) ;
#[ cfg(windows) ]
build_windows ( ) ;
#[ cfg(target_os = " macos " ) ]
println! ( " cargo:rustc-link-lib=framework=ApplicationServices " ) ;
}