1+ #![ allow( dead_code) ]
2+
13use io:: Write ;
24use std:: {
35 env, fs,
4- io:: { self , Read } ,
6+ io:: { self , BufWriter , Read } ,
57 path:: { Path , PathBuf } ,
68} ;
79
810/// ONNX Runtime version
11+ ///
12+ /// WARNING: If version is changed, bindings for all platforms will have to be re-generated.
13+ /// To do so, run this:
14+ /// cargo build --package onnxruntime-sys --features generate-bindings
915const ORT_VERSION : & str = "1.5.2" ;
1016
1117/// Base Url from which to download pre-built releases/
@@ -27,53 +33,113 @@ const ORT_ENV_GPU: &str = "ORT_USE_CUDA";
2733/// Subdirectory (of the 'target' directory) into which to extract the prebuilt library.
2834const ORT_PREBUILT_EXTRACT_DIR : & str = "onnxruntime" ;
2935
36+ #[ cfg( feature = "disable-sys-build-script" ) ]
3037fn main ( ) {
31- if !cfg ! ( feature = "disable-bindgen" ) {
32- let libort_install_dir = prepare_libort_dir ( ) ;
33-
34- let lib_dir = libort_install_dir. join ( "lib" ) ;
35- let include_dir = libort_install_dir. join ( "include" ) ;
36- let clang_arg = format ! ( "-I{}" , include_dir. display( ) ) ;
37-
38- println ! ( "Include directory: {:?}" , include_dir) ;
39- println ! ( "Lib directory: {:?}" , lib_dir) ;
40-
41- // Tell cargo to tell rustc to link onnxruntime shared library.
42- println ! ( "cargo:rustc-link-lib=onnxruntime" ) ;
43- println ! ( "cargo:rustc-link-search=native={}" , lib_dir. display( ) ) ;
44-
45- // Tell cargo to invalidate the built crate whenever the wrapper changes
46- println ! ( "cargo:rerun-if-changed=wrapper.h" ) ;
47-
48- println ! ( "cargo:rerun-if-env-changed={}" , ORT_ENV_STRATEGY ) ;
49- println ! ( "cargo:rerun-if-env-changed={}" , ORT_ENV_GPU ) ;
50- println ! ( "cargo:rerun-if-env-changed={}" , ORT_ENV_SYSTEM_LIB_LOCATION ) ;
51-
52- // The bindgen::Builder is the main entry point
53- // to bindgen, and lets you build up options for
54- // the resulting bindings.
55- let bindings = bindgen:: Builder :: default ( )
56- // The input header we would like to generate
57- // bindings for.
58- . header ( "wrapper.h" )
59- // The current working directory is 'onnxruntime-sys'
60- . clang_arg ( clang_arg)
61- // Tell cargo to invalidate the built crate whenever any of the
62- // included header files changed.
63- . parse_callbacks ( Box :: new ( bindgen:: CargoCallbacks ) )
64- // Finish the builder and generate the bindings.
65- . generate ( )
66- // Unwrap the Result and panic on failure.
67- . expect ( "Unable to generate bindings" ) ;
68-
69- // Write the bindings to (source controlled) src/generated/bindings.rs
70- let out_path = PathBuf :: from ( env:: var ( "CARGO_MANIFEST_DIR" ) . unwrap ( ) )
71- . join ( "src" )
72- . join ( "generated" ) ;
73- bindings
74- . write_to_file ( out_path. join ( "bindings.rs" ) )
75- . expect ( "Couldn't write bindings!" ) ;
76- }
38+ println ! ( "Build script disabled!" ) ;
39+
40+ generate_file_including_platform_bindings ( ) . unwrap ( ) ;
41+ }
42+
43+ #[ cfg( not( feature = "disable-sys-build-script" ) ) ]
44+ fn main ( ) {
45+ let libort_install_dir = prepare_libort_dir ( ) ;
46+
47+ let include_dir = libort_install_dir. join ( "include" ) ;
48+ let lib_dir = libort_install_dir. join ( "lib" ) ;
49+
50+ println ! ( "Include directory: {:?}" , include_dir) ;
51+ println ! ( "Lib directory: {:?}" , lib_dir) ;
52+
53+ // Tell cargo to tell rustc to link onnxruntime shared library.
54+ println ! ( "cargo:rustc-link-lib=onnxruntime" ) ;
55+ println ! ( "cargo:rustc-link-search=native={}" , lib_dir. display( ) ) ;
56+
57+ println ! ( "cargo:rerun-if-env-changed={}" , ORT_ENV_STRATEGY ) ;
58+ println ! ( "cargo:rerun-if-env-changed={}" , ORT_ENV_GPU ) ;
59+ println ! ( "cargo:rerun-if-env-changed={}" , ORT_ENV_SYSTEM_LIB_LOCATION ) ;
60+
61+ generate_bindings ( & include_dir) ;
62+
63+ generate_file_including_platform_bindings ( ) . unwrap ( ) ;
64+ }
65+
66+ #[ cfg( not( feature = "generate-bindings" ) ) ]
67+ fn generate_bindings ( _include_dir : & Path ) {
68+ println ! ( "Bindings not generated automatically, using committed files instead." ) ;
69+ println ! ( "Enable with the 'bindgen' cargo feature." ) ;
70+ }
71+
72+ #[ cfg( feature = "generate-bindings" ) ]
73+ fn generate_bindings ( include_dir : & Path ) {
74+ let clang_arg = format ! ( "-I{}" , include_dir. display( ) ) ;
75+
76+ // Tell cargo to invalidate the built crate whenever the wrapper changes
77+ println ! ( "cargo:rerun-if-changed=wrapper.h" ) ;
78+
79+ // The bindgen::Builder is the main entry point
80+ // to bindgen, and lets you build up options for
81+ // the resulting bindings.
82+ let bindings = bindgen:: Builder :: default ( )
83+ // The input header we would like to generate
84+ // bindings for.
85+ . header ( "wrapper.h" )
86+ // The current working directory is 'onnxruntime-sys'
87+ . clang_arg ( clang_arg)
88+ // Tell cargo to invalidate the built crate whenever any of the
89+ // included header files changed.
90+ . parse_callbacks ( Box :: new ( bindgen:: CargoCallbacks ) )
91+ // Finish the builder and generate the bindings.
92+ . generate ( )
93+ // Unwrap the Result and panic on failure.
94+ . expect ( "Unable to generate bindings" ) ;
95+
96+ // Write the bindings to (source controlled) src/generated/<os>/<arch>/bindings.rs
97+ let generated_file = PathBuf :: from ( env:: var ( "CARGO_MANIFEST_DIR" ) . unwrap ( ) )
98+ . join ( "src" )
99+ . join ( "generated" )
100+ . join ( env:: var ( "CARGO_CFG_TARGET_OS" ) . unwrap ( ) )
101+ . join ( env:: var ( "CARGO_CFG_TARGET_ARCH" ) . unwrap ( ) )
102+ . join ( "bindings.rs" ) ;
103+ bindings
104+ . write_to_file ( & generated_file)
105+ . expect ( "Couldn't write bindings!" ) ;
106+ }
107+
108+ fn generate_file_including_platform_bindings ( ) -> Result < ( ) , std:: io:: Error > {
109+ let generic_binding_path = PathBuf :: from ( env:: var ( "CARGO_MANIFEST_DIR" ) . unwrap ( ) )
110+ . join ( "src" )
111+ . join ( "generated" )
112+ . join ( "bindings.rs" ) ;
113+
114+ let mut fh = BufWriter :: new ( fs:: File :: create ( & generic_binding_path) ?) ;
115+
116+ let platform_bindings = PathBuf :: from ( "src" )
117+ . join ( "generated" )
118+ . join ( env:: var ( "CARGO_CFG_TARGET_OS" ) . unwrap ( ) )
119+ . join ( env:: var ( "CARGO_CFG_TARGET_ARCH" ) . unwrap ( ) )
120+ . join ( "bindings.rs" ) ;
121+
122+ // Build a (relative) path, as a string, to the platform-specific bindings.
123+ // Required so that we can escape backslash (Windows path separators) before
124+ // writing to the file.
125+ let include_path = format ! (
126+ "{}{}" ,
127+ std:: path:: MAIN_SEPARATOR ,
128+ platform_bindings. display( )
129+ )
130+ . replace ( r#"\"# , r#"\\"# ) ;
131+ fh. write_all (
132+ format ! (
133+ r#"include!(concat!(
134+ env!("CARGO_MANIFEST_DIR"),
135+ "{}"
136+ ));"# ,
137+ include_path
138+ )
139+ . as_bytes ( ) ,
140+ ) ?;
141+
142+ Ok ( ( ) )
77143}
78144
79145fn download < P : AsRef < Path > > ( source_url : & str , target_file : P ) {
@@ -125,7 +191,7 @@ fn extract_zip(filename: &Path, outpath: &Path) {
125191 let mut archive = zip:: ZipArchive :: new ( buf) . unwrap ( ) ;
126192 for i in 0 ..archive. len ( ) {
127193 let mut file = archive. by_index ( i) . unwrap ( ) ;
128- let outpath = outpath. as_ref ( ) . join ( file. sanitized_name ( ) ) ;
194+ let outpath = outpath. join ( file. sanitized_name ( ) ) ;
129195 if !( & * file. name ( ) ) . ends_with ( '/' ) {
130196 println ! (
131197 "File {} extracted to \" {}\" ({} bytes)" ,
0 commit comments