1+ use std:: env;
2+ use std:: path:: PathBuf ;
3+
14fn main ( ) {
5+ if cfg ! ( target_os = "windows" ) {
6+ download_prebuilt ( ) ;
7+ return ;
8+ }
9+
210 let mut config = cmake:: Config :: new ( "." ) ;
311 config
412 . define ( "BUILD_SHARED_LIBS" , "OFF" )
@@ -15,3 +23,90 @@ fn main() {
1523 #[ cfg( target_os = "macos" ) ]
1624 println ! ( "cargo:rustc-link-lib=dylib=c++" ) ;
1725}
26+
27+ fn download_prebuilt ( ) {
28+ let url = static_lib_url ( ) ;
29+ println ! ( "Downloading prebuilt lib from {}" , url) ;
30+
31+ let out_dir = static_lib_dir ( ) ;
32+ std:: fs:: create_dir_all ( & out_dir) . unwrap ( ) ;
33+ println ! ( "cargo:rustc-link-search={}" , out_dir. display( ) ) ;
34+
35+ match std:: fs:: read_to_string ( static_checksum_path ( ) ) {
36+ Ok ( c) if c. trim ( ) == url => {
37+ println ! ( "Using cached prebuilt lib" ) ;
38+ return ;
39+ }
40+ _ => { }
41+ } ;
42+
43+ let curl = std:: process:: Command :: new ( "curl" )
44+ . arg ( "-L" )
45+ . arg ( "-o" )
46+ . arg ( out_dir. join ( "sui.lib.gz" ) )
47+ . arg ( url)
48+ . status ( )
49+ . expect ( "Failed to download prebuilt lib" ) ;
50+ assert ! ( curl. success( ) ) ;
51+
52+ std:: fs:: write ( static_checksum_path ( ) , url) . unwrap ( ) ;
53+ }
54+
55+ fn static_lib_name ( ) -> & ' static str {
56+ let target_os = env:: var ( "CARGO_CFG_TARGET_OS" ) . unwrap ( ) ;
57+ if target_os == "windows" {
58+ "sui.lib"
59+ } else {
60+ "libsui.a"
61+ }
62+ }
63+
64+ fn static_lib_path ( ) -> PathBuf {
65+ static_lib_dir ( ) . join ( static_lib_name ( ) )
66+ }
67+
68+ fn static_lib_dir ( ) -> PathBuf {
69+ build_dir ( ) . join ( "out" )
70+ }
71+
72+ fn static_checksum_path ( ) -> PathBuf {
73+ let mut t = static_lib_path ( ) ;
74+ t. set_extension ( "sum" ) ;
75+ t
76+ }
77+
78+ fn build_dir ( ) -> PathBuf {
79+ let root = env:: current_dir ( ) . unwrap ( ) ;
80+
81+ let out_dir = env:: var_os ( "OUT_DIR" ) . expect (
82+ "The 'OUT_DIR' environment is not set (it should be something like \
83+ 'target/debug/sui-{hash}').",
84+ ) ;
85+ let out_dir_abs = root. join ( out_dir) ;
86+
87+ // This would be target/debug or target/release
88+ out_dir_abs
89+ . parent ( )
90+ . unwrap ( )
91+ . parent ( )
92+ . unwrap ( )
93+ . parent ( )
94+ . unwrap ( )
95+ . to_path_buf ( )
96+ }
97+
98+
99+ fn static_lib_url ( ) -> String {
100+ let default_base = "https://github.com/littledivy/sui/releases/download" ;
101+ let base =
102+ env:: var ( "SUI_MIRROR" ) . unwrap_or_else ( |_| default_base. into ( ) ) ;
103+ let version = env:: var ( "CARGO_PKG_VERSION" ) . unwrap ( ) ;
104+ let target = env:: var ( "TARGET" ) . unwrap ( ) ;
105+ let target_os = env:: var ( "CARGO_CFG_TARGET_OS" ) . unwrap ( ) ;
106+ // Note: we always use the release build on windows.
107+ if target_os == "windows" {
108+ return format ! ( "{}/v{}/sui_release_{}.lib.gz" , base, version, target) ;
109+ }
110+
111+ unimplemented ! ( )
112+ }
0 commit comments