2024-06-13 06:50:49 -04:00
use std ::env ;
2024-06-13 05:51:10 -04:00
use std ::fmt ::Display ;
2024-06-13 06:50:49 -04:00
use std ::io ::Cursor ;
2024-06-13 05:51:10 -04:00
use std ::path ::PathBuf ;
use std ::{ collections ::HashMap , path ::Path } ;
2024-06-08 11:21:54 -04:00
2024-06-13 05:51:10 -04:00
use serde ::{ Deserialize , Serialize } ;
2024-06-13 06:50:49 -04:00
macro_rules ! build_println {
( $( $tokens : tt ) * ) = > {
println! ( " cargo:warning= \r \x1b [32;1m {} " , format! ( $( $tokens ) * ) )
}
}
2024-06-13 05:51:10 -04:00
#[ derive(Debug) ]
enum OS {
Mac ,
MacIntel ,
Linux ,
Windows ,
}
impl Display for OS {
fn fmt ( & self , f : & mut std ::fmt ::Formatter < '_ > ) -> std ::fmt ::Result {
write! ( f , " {:?} " , self )
}
}
2024-06-08 11:21:54 -04:00
fn main ( ) {
2024-06-13 05:51:10 -04:00
#[ derive(Debug, Serialize, Deserialize) ]
#[ serde(untagged) ]
enum DependencyValue {
String ( String ) ,
Object {
version : String ,
features : Vec < String > ,
} ,
}
#[ derive(Debug, Serialize, Deserialize) ]
struct CargoToml {
dependencies : HashMap < String , DependencyValue > ,
}
let cargo_toml_raw = include_str! ( " Cargo.toml " ) ;
let cargo_toml : CargoToml = toml ::from_str ( cargo_toml_raw ) . unwrap ( ) ;
let version = match cargo_toml
. dependencies
. get ( " flatbuffers " )
. expect ( " Must have dependency flatbuffers " )
{
DependencyValue ::String ( s ) = > s ,
DependencyValue ::Object {
version ,
features : _ ,
} = > version ,
} ;
2024-06-13 06:50:49 -04:00
let executable_path = match env ::consts ::OS {
" windows " = > " target/flatc.exe " ,
_ = > " target/flatc " ,
} ;
2024-06-13 05:51:10 -04:00
if let Some ( ( platform , compiler ) ) = match env ::consts ::OS {
" linux " if env ::consts ::ARCH = = " x86_64 " = > Some ( ( OS ::Linux , " .clang++-15 " ) ) ,
2024-06-13 06:50:49 -04:00
" macos " if env ::consts ::ARCH = = " x86_64 " = > Some ( ( OS ::MacIntel , " " ) ) ,
2024-06-13 11:02:28 -04:00
" macos " if env ::consts ::ARCH = = " aarch64 " = > Some ( ( OS ::Mac , " " ) ) ,
2024-06-13 05:51:10 -04:00
" windows " if env ::consts ::ARCH = = " x86_64 " = > Some ( ( OS ::Windows , " " ) ) ,
2024-06-13 06:50:49 -04:00
_ = > None ,
2024-06-13 05:51:10 -04:00
} {
let url = format! ( " https://github.com/google/flatbuffers/releases/download/v {version} / {platform} .flatc.binary {compiler} .zip " ) ;
2024-06-13 06:50:49 -04:00
if ! Path ::new ( executable_path ) . exists ( ) {
build_println! ( " downloading flatc executable from {url} " ) ;
2024-07-04 08:48:58 -04:00
let response = match reqwest ::blocking ::get ( url ) {
Ok ( response ) = > response ,
Err ( e ) = > panic! ( " network error during build: {e} " ) ,
2024-06-13 05:51:10 -04:00
} ;
2024-07-04 08:48:58 -04:00
let archive = match response . bytes ( ) {
Ok ( archive ) = > archive ,
Err ( e ) = > panic! ( " could not retrieve byte stream during build: {e} " ) ,
2024-06-13 05:51:10 -04:00
} ;
2024-06-13 06:50:49 -04:00
let target_dir = PathBuf ::from ( " target " ) ;
zip_extract ::extract ( Cursor ::new ( archive ) , & target_dir , true )
. expect ( " flatc cannot be unpacked " )
2024-06-13 05:51:10 -04:00
} else {
2024-06-13 06:50:49 -04:00
build_println! ( " cached flatc executable found, not downloading " ) ;
2024-06-13 05:51:10 -04:00
}
2024-06-13 06:50:49 -04:00
} else {
build_println! ( " unsupported platform: {} {}. 'flatc' binary supporting version {} of the library needs to be in system path " , env ::consts ::OS , env ::consts ::ARCH , version ) ;
2024-06-13 05:51:10 -04:00
}
2024-06-13 11:02:28 -04:00
let ( flatc , location ) = match Path ::new ( executable_path ) . exists ( ) {
2024-07-04 08:48:58 -04:00
true = > ( flatc_rust ::Flatc ::from_path ( executable_path ) , " downloaded " ) ,
false = > ( flatc_rust ::Flatc ::from_env_path ( ) , " locally installed " ) ,
2024-06-13 06:50:49 -04:00
} ;
assert! ( flatc . check ( ) . is_ok ( ) ) ;
2024-06-13 11:02:28 -04:00
let version = & flatc . version ( ) . unwrap ( ) ;
build_println! (
2024-07-04 08:48:58 -04:00
" using {location} flatc v{} to compile schema files ({executable_path}) " ,
2024-06-13 11:02:28 -04:00
version . version ( )
) ;
2024-06-13 06:50:49 -04:00
flatc
. run ( flatc_rust ::Args {
extra : & [ " --gen-all " ] ,
inputs : & [
Path ::new ( " include/engine/api/flatbuffers/position.fbs " ) ,
Path ::new ( " include/engine/api/flatbuffers/waypoint.fbs " ) ,
Path ::new ( " include/engine/api/flatbuffers/route.fbs " ) ,
Path ::new ( " include/engine/api/flatbuffers/table.fbs " ) ,
Path ::new ( " include/engine/api/flatbuffers/fbresult.fbs " ) ,
] ,
out_dir : Path ::new ( " target/flatbuffers/ " ) ,
.. Default ::default ( )
} )
. expect ( " flatc failed generated files " ) ;
2024-06-08 11:21:54 -04:00
}