1
1
use std:: path:: PathBuf ;
2
2
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} ;
4
5
use crate :: external_deps:: cc:: cc;
5
6
use crate :: external_deps:: llvm:: llvm_ar;
6
7
use crate :: path_helpers:: path;
7
- use crate :: targets:: is_msvc;
8
+ use crate :: targets:: { is_darwin, is_msvc, is_windows} ;
9
+
10
+ // FIXME(Oneirical): These native build functions should take a Path-based generic.
8
11
9
12
/// Builds a static lib (`.lib` on Windows MSVC and `.a` for the rest) with the given name.
10
13
#[ track_caller]
@@ -25,3 +28,33 @@ pub fn build_native_static_lib(lib_name: &str) -> PathBuf {
25
28
llvm_ar ( ) . obj_to_ar ( ) . output_input ( & lib_path, & obj_file) . run ( ) ;
26
29
path ( lib_path)
27
30
}
31
+
32
+ /// Builds a dynamic lib. The filename is computed in a target-dependent manner, relying on
33
+ /// [`std::env::consts::DLL_PERFIX`] and [`std::env::consts::DLL_EXTENSION`].
34
+ #[ track_caller]
35
+ pub fn build_native_dynamic_lib ( lib_name : & str ) -> PathBuf {
36
+ let obj_file = if is_msvc ( ) { format ! ( "{lib_name}" ) } else { format ! ( "{lib_name}.o" ) } ;
37
+ let src = format ! ( "{lib_name}.c" ) ;
38
+ let lib_path = dynamic_lib_name ( lib_name) ;
39
+ if is_msvc ( ) {
40
+ cc ( ) . arg ( "-c" ) . out_exe ( & obj_file) . input ( src) . run ( ) ;
41
+ } else {
42
+ cc ( ) . arg ( "-v" ) . arg ( "-c" ) . out_exe ( & obj_file) . input ( src) . run ( ) ;
43
+ } ;
44
+ let obj_file = if is_msvc ( ) { format ! ( "{lib_name}.obj" ) } else { format ! ( "{lib_name}.o" ) } ;
45
+ if is_msvc ( ) {
46
+ let mut out_arg = "-out:" . to_owned ( ) ;
47
+ out_arg. push_str ( & get_windows_path ( & lib_path) ) ;
48
+ cc ( ) . input ( & obj_file) . args ( & [ "-link" , "-dll" , & out_arg] ) . run ( ) ;
49
+ } else if is_darwin ( ) {
50
+ cc ( ) . out_exe ( & lib_path) . input ( & obj_file) . args ( & [ "-dynamiclib" , "-Wl,-dylib" ] ) . run ( ) ;
51
+ } else if is_windows ( ) {
52
+ cc ( ) . out_exe ( & lib_path)
53
+ . input ( & obj_file)
54
+ . args ( & [ "-shared" , & format ! ( "-Wl,--out-implib={lib_path}.a" ) ] )
55
+ . run ( ) ;
56
+ } else {
57
+ cc ( ) . out_exe ( & lib_path) . input ( & obj_file) . arg ( "-shared" ) . run ( ) ;
58
+ }
59
+ path ( lib_path)
60
+ }
0 commit comments