@@ -149,6 +149,49 @@ pub fn bin_name(name: &str) -> String {
149149 if is_windows ( ) { format ! ( "{name}.exe" ) } else { name. to_string ( ) }
150150}
151151
152+ fn ar < P : AsRef < str > , P2 : AsRef < str > > ( obj_path : P , lib_path : P2 , caller_line_number : u32 ) {
153+ let mut ar = Command :: new ( env:: var ( "AR" ) . unwrap ( ) ) ;
154+ ar. current_dir ( tmp_dir ( ) ) . arg ( "crus" ) . arg ( lib_path. as_ref ( ) ) . arg ( obj_path. as_ref ( ) ) ;
155+ let output = ar. output ( ) . unwrap ( ) ;
156+ if !output. status . success ( ) {
157+ handle_failed_output ( & ar, output, caller_line_number) ;
158+ }
159+ }
160+
161+ /// Builds a static lib (`.lib` on Windows MSVC and `.a` for the rest) with the given name.
162+ pub fn build_native_static_lib ( lib_name : & str ) -> PathBuf {
163+ let caller_location = std:: panic:: Location :: caller ( ) ;
164+ let caller_line_number = caller_location. line ( ) ;
165+
166+ let obj_file = format ! ( "{lib_name}.o" ) ;
167+ let src = format ! ( "{lib_name}.c" ) ;
168+ let lib_name = if is_msvc ( ) {
169+ let lib_path = format ! ( "lib{lib_name}.lib" ) ;
170+ // First compiling `.c` to `.o`.
171+ cc ( ) . arg ( "-c" ) . out_exe ( lib_name) . input ( src) . run ( ) ;
172+ // Generating `.lib` from `.o`.
173+ let mut msvc_lib = Command :: new ( env:: var ( "MSVC_LIB_PATH" ) . unwrap ( ) ) ;
174+ msvc_lib
175+ . current_dir ( tmp_dir ( ) )
176+ . arg ( "-nologo" )
177+ . arg ( & format ! ( "-out:{}" , cygpath_windows( & lib_path) ) )
178+ . arg ( & obj_file) ;
179+ let output = msvc_lib. output ( ) . unwrap ( ) ;
180+ if !output. status . success ( ) {
181+ handle_failed_output ( & msvc_lib, output, caller_line_number) ;
182+ }
183+ lib_path
184+ } else {
185+ let lib_path = format ! ( "lib{lib_name}.a" ) ;
186+ // First compiling `.c` to `.o`.
187+ cc ( ) . arg ( "-v" ) . arg ( "-c" ) . out_exe ( & obj_file) . input ( src) . run ( ) ;
188+ // Generating `.a` from `.o`.
189+ ar ( obj_file, & lib_path, caller_line_number) ;
190+ lib_path
191+ } ;
192+ tmp_dir ( ) . join ( lib_name)
193+ }
194+
152195/// Use `cygpath -w` on a path to get a Windows path string back. This assumes that `cygpath` is
153196/// available on the platform!
154197#[ track_caller]
0 commit comments