@@ -259,15 +259,32 @@ fn main() {
259
259
cmd. args ( & components) ;
260
260
261
261
for lib in output ( & mut cmd) . split_whitespace ( ) {
262
+ let mut is_static = false ;
262
263
let name = if let Some ( stripped) = lib. strip_prefix ( "-l" ) {
263
264
stripped
264
265
} else if let Some ( stripped) = lib. strip_prefix ( '-' ) {
265
266
stripped
266
267
} else if Path :: new ( lib) . exists ( ) {
267
268
// On MSVC llvm-config will print the full name to libraries, but
268
269
// we're only interested in the name part
269
- let name = Path :: new ( lib) . file_name ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) ;
270
- name. trim_end_matches ( ".lib" )
270
+ // On Unix when we get a static library llvm-config will print the
271
+ // full name and we *are* interested in the path, but we need to
272
+ // handle it separately. For example, when statically linking to
273
+ // libzstd llvm-config will output something like
274
+ // -lrt -ldl -lm -lz /usr/local/lib/libzstd.a -lxml2
275
+ // and we transform the zstd part into
276
+ // cargo:rustc-link-search-native=/usr/local/lib
277
+ // cargo:rustc-link-lib=static=zstd
278
+ let path = Path :: new ( lib) ;
279
+ if lib. ends_with ( ".a" ) {
280
+ is_static = true ;
281
+ println ! ( "cargo:rustc-link-search=native={}" , path. parent( ) . unwrap( ) . display( ) ) ;
282
+ let name = path. file_stem ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) ;
283
+ name. trim_start_matches ( "lib" )
284
+ } else {
285
+ let name = path. file_name ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) ;
286
+ name. trim_end_matches ( ".lib" )
287
+ }
271
288
} else if lib. ends_with ( ".lib" ) {
272
289
// Some MSVC libraries just come up with `.lib` tacked on, so chop
273
290
// that off
@@ -285,7 +302,13 @@ fn main() {
285
302
continue ;
286
303
}
287
304
288
- let kind = if name. starts_with ( "LLVM" ) { llvm_kind } else { "dylib" } ;
305
+ let kind = if name. starts_with ( "LLVM" ) {
306
+ llvm_kind
307
+ } else if is_static {
308
+ "static"
309
+ } else {
310
+ "dylib"
311
+ } ;
289
312
println ! ( "cargo:rustc-link-lib={kind}={name}" ) ;
290
313
}
291
314
0 commit comments