@@ -333,6 +333,7 @@ pub struct Build {
333
333
build_cache : Arc < BuildCache > ,
334
334
inherit_rustflags : bool ,
335
335
link_shared_flag : bool ,
336
+ shared_lib_out_dir : Option < Arc < Path > > ,
336
337
}
337
338
338
339
/// Represents the types of errors that may occur while using cc-rs.
@@ -461,6 +462,7 @@ impl Build {
461
462
build_cache : Arc :: default ( ) ,
462
463
inherit_rustflags : true ,
463
464
link_shared_flag : false ,
465
+ shared_lib_out_dir : None ,
464
466
}
465
467
}
466
468
@@ -1237,6 +1239,14 @@ impl Build {
1237
1239
self
1238
1240
}
1239
1241
1242
+ /// Configures the output directory where shared library will be located.
1243
+ ///
1244
+ /// This option is automatically scraped from the `OUT_DIR` environment variable by build scripts
1245
+ pub fn shared_lib_out_dir < P : AsRef < Path > > ( & mut self , out_dir : P ) -> & mut Build {
1246
+ self . shared_lib_out_dir = Some ( out_dir. as_ref ( ) . into ( ) ) ;
1247
+ self
1248
+ }
1249
+
1240
1250
#[ doc( hidden) ]
1241
1251
pub fn __set_env < A , B > ( & mut self , a : A , b : B ) -> & mut Build
1242
1252
where
@@ -1390,16 +1400,26 @@ impl Build {
1390
1400
Ok ( is_supported)
1391
1401
}
1392
1402
1393
- fn get_canonical_library_names ( name : & str ) -> ( & str , String , String ) {
1394
- let lib_striped = name. strip_prefix ( "lib" ) . unwrap_or ( name) ;
1403
+ /// Get canonical library names for `output`
1404
+ fn get_canonical_library_names < ' a > (
1405
+ & self ,
1406
+ output : & ' a str ,
1407
+ ) -> Result < ( & ' a str , String , String ) , Error > {
1408
+ let lib_striped = output. strip_prefix ( "lib" ) . unwrap_or ( output) ;
1395
1409
let static_striped = lib_striped. strip_suffix ( ".a" ) . unwrap_or ( lib_striped) ;
1396
- let lib_name = lib_striped. strip_suffix ( ".so" ) . unwrap_or ( static_striped) ;
1397
1410
1398
- (
1411
+ let dyn_ext = if self . get_target ( ) ?. env == "msvc" {
1412
+ ".dll"
1413
+ } else {
1414
+ ".so"
1415
+ } ;
1416
+ let lib_name = lib_striped. strip_suffix ( dyn_ext) . unwrap_or ( static_striped) ;
1417
+
1418
+ Ok ( (
1399
1419
lib_name,
1400
1420
format ! ( "lib{lib_name}.a" ) ,
1401
- format ! ( "lib{lib_name}.so " ) ,
1402
- )
1421
+ format ! ( "lib{lib_name}{dyn_ext} " ) ,
1422
+ ) )
1403
1423
}
1404
1424
1405
1425
/// Run the compiler, generating the file `output`
@@ -1418,7 +1438,7 @@ impl Build {
1418
1438
}
1419
1439
}
1420
1440
1421
- let ( lib_name, static_name, dynlib_name) = Self :: get_canonical_library_names ( output) ;
1441
+ let ( lib_name, static_name, dynlib_name) = self . get_canonical_library_names ( output) ? ;
1422
1442
let dst = self . get_out_dir ( ) ?;
1423
1443
1424
1444
let objects = objects_from_files ( & self . files , & dst) ?;
@@ -1428,12 +1448,13 @@ impl Build {
1428
1448
if self . link_shared_flag {
1429
1449
let objects = objects. iter ( ) . map ( |o| o. dst . clone ( ) ) . collect :: < Vec < _ > > ( ) ;
1430
1450
1431
- let mut command = self . try_get_compiler ( ) ?. to_command ( ) ;
1432
- let cmd = command
1433
- . args ( [ "-shared" , "-o" ] )
1434
- . arg ( dst. join ( dynlib_name) )
1435
- . args ( & objects) ;
1436
- run ( cmd, & self . cargo_output ) ?;
1451
+ let mut cmd = self . try_get_compiler ( ) ?. to_command ( ) ;
1452
+ let dynlib_path = dst. join ( & dynlib_name) ;
1453
+ cmd. args ( [ "-shared" , "-o" ] ) . arg ( & dynlib_path) . args ( & objects) ;
1454
+ run ( & mut cmd, & self . cargo_output ) ?;
1455
+ if let Some ( out_dir) = & self . shared_lib_out_dir {
1456
+ fs:: copy ( dynlib_path, out_dir. join ( & dynlib_name) ) ?;
1457
+ }
1437
1458
} else {
1438
1459
self . assemble ( lib_name, & dst. join ( static_name) , & objects) ?;
1439
1460
}
0 commit comments