11use std:: path:: PathBuf ;
22
3- use crate :: artifact_names:: static_lib_name;
3+ use super :: cygpath:: get_windows_path;
4+ use crate :: artifact_names:: { dynamic_lib_name, static_lib_name} ;
45use crate :: external_deps:: cc:: cc;
56use crate :: external_deps:: llvm:: llvm_ar;
67use crate :: path_helpers:: path;
7- use crate :: targets:: is_msvc;
8+ use crate :: targets:: { is_darwin , is_msvc, is_windows } ;
89
910/// Builds a static lib (`.lib` on Windows MSVC and `.a` for the rest) with the given name.
1011#[ track_caller]
@@ -25,3 +26,33 @@ pub fn build_native_static_lib(lib_name: &str) -> PathBuf {
2526 llvm_ar ( ) . obj_to_ar ( ) . output_input ( & lib_path, & obj_file) . run ( ) ;
2627 path ( lib_path)
2728}
29+
30+ /// Builds a dynamic lib (`.dll` on Windows, `.dylib` on OSX and `.so` for Linux) with the given
31+ /// name.
32+ #[ track_caller]
33+ pub fn build_native_dynamic_lib ( lib_name : & str ) -> PathBuf {
34+ let obj_file = if is_msvc ( ) { format ! ( "{lib_name}" ) } else { format ! ( "{lib_name}.o" ) } ;
35+ let src = format ! ( "{lib_name}.c" ) ;
36+ let lib_path = dynamic_lib_name ( lib_name) ;
37+ if is_msvc ( ) {
38+ cc ( ) . arg ( "-c" ) . out_exe ( & obj_file) . input ( src) . run ( ) ;
39+ } else {
40+ cc ( ) . arg ( "-v" ) . arg ( "-c" ) . out_exe ( & obj_file) . input ( src) . run ( ) ;
41+ } ;
42+ let obj_file = if is_msvc ( ) { format ! ( "{lib_name}.obj" ) } else { format ! ( "{lib_name}.o" ) } ;
43+ if is_msvc ( ) {
44+ let mut out_arg = "-out" . to_owned ( ) ;
45+ out_arg. push_str ( & get_windows_path ( & lib_path) ) ;
46+ cc ( ) . input ( & obj_file) . args ( & [ "-link" , "-dll" , & out_arg] ) . run ( ) ;
47+ } else if is_darwin ( ) {
48+ cc ( ) . out_exe ( & lib_path)
49+ . input ( & obj_file)
50+ . args ( & [ "-shared" , & format ! ( "-Wl,--out-implib={lib_path}.a" ) ] )
51+ . run ( ) ;
52+ } else if is_windows ( ) {
53+ cc ( ) . out_exe ( & lib_path) . input ( & obj_file) . args ( & [ "-dynamiclib" , "-Wl,-dylib" ] ) . run ( ) ;
54+ } else {
55+ cc ( ) . out_exe ( & lib_path) . input ( & obj_file) . arg ( "-shared" ) . run ( ) ;
56+ }
57+ path ( lib_path)
58+ }
0 commit comments