@@ -52,14 +52,17 @@ impl Error for BuildError {}
5252// NOTE: We have to build mgclient and link the rust binary with the same SSL and Crypto libs.
5353
5454fn build_mgclient_macos ( ) -> Result < PathBuf , BuildError > {
55- println ! ( "MacOS detected. We will check if you have either the MacPorts or Homebrew package managers." ) ;
55+ println ! (
56+ "MacOS detected. We will check if you have either the MacPorts or Homebrew package managers."
57+ ) ;
5658 println ! ( "Checking for MacPorts..." ) ;
5759 let output = Command :: new ( "/usr/bin/command" )
5860 . args ( [ "-v" , "port" ] )
5961 . output ( )
6062 . map_err ( |err| BuildError :: IoError ( format ! ( "'/usr/bin/command -v port': {}" , err) ) ) ?
6163 . stdout ;
62- let port_path = String :: from_utf8 ( output) . unwrap ( ) ;
64+ let port_path = String :: from_utf8 ( output)
65+ . map_err ( |err| BuildError :: Unknown ( format ! ( "Invalid UTF-8 in port path: {}" , err) ) ) ?;
6366 if !port_path. is_empty ( ) {
6467 let port_path = & port_path[ ..port_path. len ( ) - 1 ] ;
6568 println ! (
@@ -72,17 +75,19 @@ fn build_mgclient_macos() -> Result<PathBuf, BuildError> {
7275 Command :: new ( port_path)
7376 . args ( [ "installed" , "openssl" ] )
7477 . output ( )
75- . expect ( "Failed to execute shell command 'port installed openssl'" )
78+ . map_err ( |err| BuildError :: IoError ( format ! ( " 'port installed openssl': {}" , err ) ) ) ?
7679 . stdout ,
7780 )
78- . unwrap ( ) ;
81+ . map_err ( |err| BuildError :: Unknown ( format ! ( "Invalid UTF-8 in port output: {}" , err ) ) ) ? ;
7982 if output == "None of the specified ports are installed.\n " {
80- panic ! ( "The openssl port does not seem to be installed! Please install it using 'port install openssl'." ) ;
83+ panic ! (
84+ "The openssl port does not seem to be installed! Please install it using 'port install openssl'."
85+ ) ;
8186 }
8287 let openssl_lib_dir = port_binary_path
8388 . ancestors ( )
8489 . nth ( 2 )
85- . unwrap ( )
90+ . ok_or_else ( || BuildError :: Unknown ( "Unable to find port parent directory" . to_string ( ) ) ) ?
8691 . join ( "libexec" )
8792 . join ( "openssl3" )
8893 . join ( "lib" ) ;
@@ -104,13 +109,14 @@ fn build_mgclient_macos() -> Result<PathBuf, BuildError> {
104109 . output ( )
105110 . map_err ( |err| BuildError :: IoError ( format ! ( "'/usr/bin/command -v brew': {}" , err) ) ) ?
106111 . stdout ;
107- let brew_path = String :: from_utf8 ( output) . unwrap ( ) ;
112+ let brew_path = String :: from_utf8 ( output)
113+ . map_err ( |err| BuildError :: Unknown ( format ! ( "Invalid UTF-8 in brew path: {}" , err) ) ) ?;
108114 if brew_path. is_empty ( ) {
109115 println ! ( "Homebrew not found." ) ;
110- BuildError :: Unknown (
116+ return Err ( BuildError :: Unknown (
111117 "We did not detect either MacPorts or Homebrew on your machine. We cannot proceed."
112118 . to_string ( ) ,
113- ) ;
119+ ) ) ;
114120 } else {
115121 println ! ( "'brew' executable detected at {:?}" , & brew_path) ;
116122 println ! ( "Proceeding with installation assuming Homebrew is your package manager" ) ;
@@ -140,9 +146,15 @@ fn build_mgclient_macos() -> Result<PathBuf, BuildError> {
140146 } )
141147 . collect :: < Vec < PathBuf > > ( ) ;
142148 openssl_dirs. sort_by ( |a, b| {
143- let a_time = a. metadata ( ) . unwrap ( ) . modified ( ) . unwrap ( ) ;
144- let b_time = b. metadata ( ) . unwrap ( ) . modified ( ) . unwrap ( ) ;
145- b_time. cmp ( & a_time)
149+ // If we can't get metadata, treat as older file (sort to end)
150+ let a_time = a. metadata ( ) . ok ( ) . and_then ( |m| m. modified ( ) . ok ( ) ) ;
151+ let b_time = b. metadata ( ) . ok ( ) . and_then ( |m| m. modified ( ) . ok ( ) ) ;
152+ match ( b_time, a_time) {
153+ ( Some ( b) , Some ( a) ) => b. cmp ( & a) ,
154+ ( Some ( _) , None ) => std:: cmp:: Ordering :: Less ,
155+ ( None , Some ( _) ) => std:: cmp:: Ordering :: Greater ,
156+ ( None , None ) => std:: cmp:: Ordering :: Equal ,
157+ }
146158 } ) ;
147159 let openssl_root_path = openssl_dirs[ 0 ] . clone ( ) ;
148160 println ! (
@@ -268,10 +280,14 @@ fn main() -> Result<(), BuildError> {
268280 . header ( format ! ( "{}" , mgclient_export_h. display( ) ) )
269281 . header ( format ! ( "{}" , mgclient_mgvalue_h. display( ) ) )
270282 . clang_arg ( format ! ( "-I{}" , mgclient_out. join( "include" ) . display( ) ) )
271- . parse_callbacks ( Box :: new ( bindgen:: CargoCallbacks ) )
283+ . parse_callbacks ( Box :: new ( bindgen:: CargoCallbacks :: new ( ) ) )
284+ . generate_cstr ( true )
272285 . generate ( )
273286 . expect ( "Unable to generate bindings" ) ;
274- let out_path = PathBuf :: from ( env:: var ( "OUT_DIR" ) . unwrap ( ) ) ;
287+ let out_path =
288+ PathBuf :: from ( env:: var ( "OUT_DIR" ) . map_err ( |_| {
289+ BuildError :: Unknown ( "OUT_DIR environment variable not set" . to_string ( ) )
290+ } ) ?) ;
275291 bindings
276292 . write_to_file ( out_path. join ( "bindings.rs" ) )
277293 . expect ( "Couldn't write bindings!" ) ;
0 commit comments