@@ -17,6 +17,35 @@ use std::path::{PathBuf, Path};
1717
1818use build_helper:: output;
1919
20+ fn detect_llvm_link ( llvm_config : & Path ) -> ( & ' static str , Option < & ' static str > ) {
21+ let mut version_cmd = Command :: new ( llvm_config) ;
22+ version_cmd. arg ( "--version" ) ;
23+ let version_output = output ( & mut version_cmd) ;
24+ let mut parts = version_output. split ( '.' ) . take ( 2 )
25+ . filter_map ( |s| s. parse :: < u32 > ( ) . ok ( ) ) ;
26+ if let ( Some ( major) , Some ( minor) ) = ( parts. next ( ) , parts. next ( ) ) {
27+ if major > 3 || ( major == 3 && minor >= 9 ) {
28+ // Force the link mode we want, preferring static by default, but
29+ // possibly overridden by `configure --enable-llvm-link-shared`.
30+ if env:: var_os ( "LLVM_LINK_SHARED" ) . is_some ( ) {
31+ return ( "dylib" , Some ( "--link-shared" ) ) ;
32+ } else {
33+ return ( "static" , Some ( "--link-static" ) ) ;
34+ }
35+ } else if major == 3 && minor == 8 {
36+ // Find out LLVM's default linking mode.
37+ let mut mode_cmd = Command :: new ( llvm_config) ;
38+ mode_cmd. arg ( "--shared-mode" ) ;
39+ if output ( & mut mode_cmd) . trim ( ) == "shared" {
40+ return ( "dylib" , None ) ;
41+ } else {
42+ return ( "static" , None ) ;
43+ }
44+ }
45+ }
46+ ( "static" , None )
47+ }
48+
2049fn main ( ) {
2150 println ! ( "cargo:rustc-cfg=cargobuild" ) ;
2251
@@ -123,39 +152,16 @@ fn main() {
123152 . cpp_link_stdlib ( None ) // we handle this below
124153 . compile ( "librustllvm.a" ) ;
125154
126- // Find out LLVM's default linking mode.
127- let mut cmd = Command :: new ( & llvm_config) ;
128- cmd. arg ( "--shared-mode" ) ;
129- let mut llvm_kind = if output ( & mut cmd) . trim ( ) == "shared" {
130- "dylib"
131- } else {
132- "static"
133- } ;
155+ let ( llvm_kind, llvm_link_arg) = detect_llvm_link ( & llvm_config) ;
134156
135157 // Link in all LLVM libraries, if we're uwring the "wrong" llvm-config then
136158 // we don't pick up system libs because unfortunately they're for the host
137159 // of llvm-config, not the target that we're attempting to link.
138160 let mut cmd = Command :: new ( & llvm_config) ;
139161 cmd. arg ( "--libs" ) ;
140162
141- // Force static linking with "--link-static" if available, or
142- // force "--link-shared" if the configuration requested it.
143- let llvm_link_shared = env:: var_os ( "LLVM_LINK_SHARED" ) . is_some ( ) ;
144- let mut version_cmd = Command :: new ( & llvm_config) ;
145- version_cmd. arg ( "--version" ) ;
146- let version_output = output ( & mut version_cmd) ;
147- let mut parts = version_output. split ( '.' ) ;
148- if let ( Some ( major) , Some ( minor) ) = ( parts. next ( ) . and_then ( |s| s. parse :: < u32 > ( ) . ok ( ) ) ,
149- parts. next ( ) . and_then ( |s| s. parse :: < u32 > ( ) . ok ( ) ) ) {
150- if major > 3 || ( major == 3 && minor >= 9 ) {
151- if llvm_link_shared {
152- cmd. arg ( "--link-shared" ) ;
153- llvm_kind = "dylib" ;
154- } else {
155- cmd. arg ( "--link-static" ) ;
156- llvm_kind = "static" ;
157- }
158- }
163+ if let Some ( link_arg) = llvm_link_arg {
164+ cmd. arg ( link_arg) ;
159165 }
160166
161167 if !is_crossed {
0 commit comments