| 1 | package Affix::Platform::Windows v1.3.0 { | ||
| 2 | use v5.40; | ||
| 3 | use DynaLoader; | ||
| 4 | use Win32; # Core on Windows | ||
| 5 | use File::Spec; | ||
| 6 | use parent 'Exporter'; | ||
| 7 | our @EXPORT_OK = qw[find_library]; | ||
| 8 | our %EXPORT_TAGS = ( all => \@EXPORT_OK ); | ||
| 9 | |||
| 10 | sub find_msvcrt () { | ||
| 11 | my $version = get_msvcrt_version(); # Assuming _get_build_version is defined elsewhere | ||
| 12 | if ( !$version ) { | ||
| 13 | my @possible_dlls = ( | ||
| 14 | 'msvcrt.dll', | ||
| 15 | |||
| 16 | #~ sprintf( 'msvcr%d.dll', $version * 10 ) | ||
| 17 | ); | ||
| 18 | |||
| 19 | # Search for the DLL in common system directories | ||
| 20 | for my $dll (@possible_dlls) { | ||
| 21 | for my $dir ( Win32::GetFolderPath( Win32::CSIDL_SYSTEM() ), qw[C:/Windows/System32 C:/Windows/SysWOW64] ) { | ||
| 22 | my $file = File::Spec->catfile( $dir, $dll ); | ||
| 23 | return $file if -f $file; | ||
| 24 | } | ||
| 25 | } | ||
| 26 | } | ||
| 27 | my $clibname; | ||
| 28 | if ( $version <= 6 ) { | ||
| 29 | $clibname = 'msvcrt'; | ||
| 30 | } | ||
| 31 | elsif ( $version <= 13 ) { | ||
| 32 | $clibname = sprintf( 'msvcr%d', $version * 10 ); | ||
| 33 | } | ||
| 34 | else { | ||
| 35 | # CRT not directly loadable (see python/cpython#23606) | ||
| 36 | return undef; | ||
| 37 | } | ||
| 38 | |||
| 39 | # Check for debug build | ||
| 40 | my $debug_suffix = '_d'; # Assuming debug suffix is '_d' | ||
| 41 | my $suffixes = join '|', map quotemeta, @DynaLoader::dl_extensions; | ||
| 42 | if ( $debug_suffix =~ /$suffixes/ ) { | ||
| 43 | $clibname .= $debug_suffix; | ||
| 44 | } | ||
| 45 | return "$clibname.dll"; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub get_msvcrt_version() { | ||
| 49 | my $dumpbin = 'dumpbin'; | ||
| 50 | for my $dir ( split ';', $ENV{PATH} ) { | ||
| 51 | my $file = File::Spec->catfile( $dir, $dumpbin ); | ||
| 52 | $file .= '.exe' if $file !~ /\.exe$/i; | ||
| 53 | if ( -f $file ) { | ||
| 54 | open( my $pipe, '-|', $file, '/headers', 'msvcrt.dll' ) or return; | ||
| 55 | my $dumpbin_output; | ||
| 56 | $dumpbin_output .= $_ while <$pipe>; | ||
| 57 | close $pipe; | ||
| 58 | return $1 if $dumpbin_output && $dumpbin_output =~ /FileVersion\s+(\d+\.\d+\.\d+\.\d+)/; | ||
| 59 | } | ||
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | sub find_library($name) { | ||
| 64 | return $name if -f $name; | ||
| 65 | return find_msvcrt() if $name eq 'c' || $name eq 'm'; | ||
| 66 | for my $dir ( split ';', $ENV{PATH} ) { | ||
| 67 | my $file = File::Spec->catfile( $dir, $name ); | ||
| 68 | $file .= '.dll' if $file !~ /\.dll$/i; # Check for ".dll" extension (case-insensitive) | ||
| 69 | return $file if -f $file; | ||
| 70 | } | ||
| 71 | } | ||
| 72 | }; | ||
| 73 | 1; |