14
14
import stat
15
15
import subprocess
16
16
import sys
17
+ import sysconfig
17
18
import tempfile
18
19
import textwrap
19
20
import time
@@ -1678,7 +1679,7 @@ def std_lib_dirs_and_libs() -> Optional[Tuple[List[str], ...]]:
1678
1679
# Obtain the library name from the Python version instead of the
1679
1680
# installation directory, in case the user defined a custom
1680
1681
# installation directory.
1681
- python_version = distutils . sysconfig .get_python_version ()
1682
+ python_version = sysconfig .get_python_version ()
1682
1683
libname = "python" + python_version .replace ("." , "" )
1683
1684
# Also add directory containing the Python library to the library
1684
1685
# directories.
@@ -2381,7 +2382,13 @@ def try_compile_tmp(
2381
2382
comp_args = True ,
2382
2383
):
2383
2384
return cls ._try_compile_tmp (
2384
- src_code , tmp_prefix , flags , try_run , output , config .cxx , comp_args
2385
+ src_code ,
2386
+ tmp_prefix ,
2387
+ cls .patch_ldflags (flags ),
2388
+ try_run ,
2389
+ output ,
2390
+ config .cxx ,
2391
+ comp_args ,
2385
2392
)
2386
2393
2387
2394
@classmethod
@@ -2395,9 +2402,58 @@ def try_flags(
2395
2402
comp_args = True ,
2396
2403
):
2397
2404
return cls ._try_flags (
2398
- flag_list , preamble , body , try_run , output , config .cxx , comp_args
2405
+ cls .patch_ldflags (flag_list ),
2406
+ preamble ,
2407
+ body ,
2408
+ try_run ,
2409
+ output ,
2410
+ config .cxx ,
2411
+ comp_args ,
2399
2412
)
2400
2413
2414
+ @staticmethod
2415
+ def patch_ldflags (flag_list : List [str ]) -> List [str ]:
2416
+ lib_dirs = [flag [2 :].lstrip () for flag in flag_list if flag .startswith ("-L" )]
2417
+ flag_idxs : List [int ] = []
2418
+ libs : List [str ] = []
2419
+ for i , flag in enumerate (flag_list ):
2420
+ if flag .startswith ("-l" ):
2421
+ flag_idxs .append (i )
2422
+ libs .append (flag [2 :].lstrip ())
2423
+ if not libs :
2424
+ return flag_list
2425
+ libs = GCC_compiler .linking_patch (lib_dirs , libs )
2426
+ for flag_idx , lib in zip (flag_idxs , libs ):
2427
+ flag_list [flag_idx ] = lib
2428
+ return flag_list
2429
+
2430
+ @staticmethod
2431
+ def linking_patch (lib_dirs : List [str ], libs : List [str ]) -> List [str ]:
2432
+ if sys .platform != "win32" :
2433
+ return [f"-l{ l } " for l in libs ]
2434
+
2435
+ def sort_key (lib ): # type: ignore
2436
+ name , * numbers , extension = lib .split ("." )
2437
+ return (extension == "dll" , tuple (map (int , numbers )))
2438
+
2439
+ patched_lib_ldflags = []
2440
+ for lib in libs :
2441
+ ldflag = f"-l{ lib } "
2442
+ for lib_dir in lib_dirs :
2443
+ lib_dir = lib_dir .strip ('"' )
2444
+ windows_styled_libs = [
2445
+ fname
2446
+ for fname in os .listdir (lib_dir )
2447
+ if not (os .path .isdir (os .path .join (lib_dir , fname )))
2448
+ and fname .split ("." )[0 ] == lib
2449
+ and fname .split ("." )[- 1 ] in ["dll" , "lib" ]
2450
+ ]
2451
+ if windows_styled_libs :
2452
+ selected_lib = sorted (windows_styled_libs , key = sort_key )[- 1 ]
2453
+ ldflag = f'"{ os .path .join (lib_dir , selected_lib )} "'
2454
+ patched_lib_ldflags .append (ldflag )
2455
+ return patched_lib_ldflags
2456
+
2401
2457
@staticmethod
2402
2458
def compile_str (
2403
2459
module_name ,
@@ -2509,7 +2565,7 @@ def compile_str(
2509
2565
cmd .append ("-fvisibility=hidden" )
2510
2566
cmd .extend (["-o" , f"{ path_wrapper } { lib_filename } { path_wrapper } " ])
2511
2567
cmd .append (f"{ path_wrapper } { cppfilename } { path_wrapper } " )
2512
- cmd .extend ([ f"-l { l } " for l in libs ] )
2568
+ cmd .extend (GCC_compiler . linking_patch ( lib_dirs , libs ) )
2513
2569
# print >> sys.stderr, 'COMPILING W CMD', cmd
2514
2570
_logger .debug (f"Running cmd: { ' ' .join (cmd )} " )
2515
2571
0 commit comments