Changeset 8170 for inc

Show
Ignore:
Timestamp:
12/11/05 02:54:59 (3 years ago)
Author:
pjf
Message:

General improvement of Win32/Cygwin GHC detection code.

* Makefile.PL::has_ghc_package() now requires a third argument, being

the 'ghc' command (with path if applicable).

In inc::Module::Install::Pugs:

* assert_ghc now goes looking for GHC on Win32/Cygwin systems.

This performs lexical sorting of version names, and will bite
us if part of a GHC version string contains a two-digits.

* assert_ghc_pkg now caches its findings, rather than re-searching

every time.

* assert_ghc_pkg doesn't trust can_run() on cygwin if we're clearly

trying to work with a Windows-land program. See
https://rt.cpan.org/Ticket/Display.html?id=16375 for details.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • inc/Module/Install/Pugs.pm

    r8127 r8170  
    66use File::Basename; 
    77use IPC::Open3 'open3'; 
     8use Carp; 
    89 
    910sub WritePugs { 
     
    168169    my $self = shift; 
    169170    my $ghc = $self->can_run($ENV{GHC} || ( 'ghc' . $Config{_exe} ) ); 
    170     my $ghcver = `$ghc --version`; 
    171     ($ghcver =~ /Glasgow.*\bversion\s*(\S+)/s) or die << '.'; 
     171 
     172    # This local subroutine returns the version of ghc passed to it. 
     173 
     174    my $test_ghc_ver = sub {  
     175        (`$_[0] --version` =~ /Glasgow.*\bversion\s*(\S+)/s)[0];  
     176    }; 
     177 
     178    my ($ghc_version) = $test_ghc_ver->($ghc); 
     179 
     180    if (!$ghc_version and (    $Config{osname} eq "cygwin"  
     181                            or $Config{osname} eq "MSWin32" 
     182                          ) 
     183       ) { 
     184 
     185        # Looks like we're on a Windows-ish system, without GHC 
     186        # in our path.   Let's hunt around for it. 
     187 
     188        my $ghc_root = "$ENV{SYSTEMDRIVE}/ghc"; 
     189 
     190        warn "*** ghc not found in path.  Looking in $ghc_root\n"; 
     191 
     192        if (-d $ghc_root) { 
     193            # Looks like we've found a GHC directory.  Find the latest 
     194            # ghc inside that.  Sorted from lexically highest to lowest. 
     195 
     196            # XXX - This will bite us should GHC contain a two-digit 
     197            #       revision.  Eg, 6.9.0 vs 6.10.0 
     198 
     199            my @ghc_choices = reverse glob(qq{$ghc_root/ghc-*}); 
     200 
     201            GHC_TEST: 
     202            for my $ghc_dir (@ghc_choices) { 
     203                my $ghc_candidate = qq{$ghc_dir/bin/ghc.exe}; 
     204                if ($ghc_version = $test_ghc_ver->($ghc_candidate)) { 
     205                    $ghc = $ghc_candidate; 
     206                    warn "*** Found $ghc\n"; 
     207                } 
     208            } 
     209        } 
     210    } 
     211 
     212    $ghc_version or die << '.'; 
    172213*** Cannot find a runnable 'ghc' from path. 
    173214*** Please install GHC from http://haskell.org/ghc/. 
    174215. 
    175216 
    176     my $ghc_version = $1; 
    177217    unless ($ghc_version =~ /^(\d)\.(\d+)/ and $1 >= 6 and $2 >= 4) { 
    178218        die << "."; 
     
    196236    chomp $ghc_flags; 
    197237 
    198     return ($ghc, $ghc_version, $ghc_flags, $self->assert_ghc_pkg); 
     238    return ($ghc, $ghc_version, $ghc_flags, $self->assert_ghc_pkg($ghc)); 
    199239} 
    200240 
     
    205245} 
    206246 
     247=head2 assert_ghc_pkg 
     248 
     249Assert that we have F<ghc_pkg> installed.  This caches its result, 
     250any further calls to ghc_pkg This method expects to 
     251be called with a path (relative, absolute, or a command in 
     252C<$ENV{PATH}> that can be used to execute F<ghc>. 
     253 
     254=cut 
     255 
    207256sub assert_ghc_pkg { 
    208257    my $self = shift; 
     258 
     259    # Return immediately if we've cached this. 
     260    return $self->{ghc_pkg} if $self->{ghc_pkg}; 
     261 
     262    my $ghc  = shift || $self->{ghc}  
     263        or croak "assert_ghc_pkg not cached, and called without path to ghc"; 
     264 
    209265    my $ghc_pkg = $ENV{GHC_PKG}; 
    210266 
    211267    unless($ghc_pkg) { 
    212         $ghc_pkg = ($ENV{GHC} || 'ghc'); 
     268        $ghc_pkg = $ghc; 
    213269        $ghc_pkg =~ s/\bghc(?=[^\\\/]*$)/ghc-pkg/  # ghc-6.5 => ghc-pkg-6.5 
    214270            or $ghc_pkg = 'ghc-pkg'; # fallback if !/^ghc/ 
    215         $ghc_pkg = $self->can_run($ghc_pkg) || $self->can_run('ghc-pkg'); 
     271 
     272 
     273        my $ghc_exe = $self->can_run($ghc_pkg) || $self->can_run('ghc-pkg'); 
     274 
     275        # This above fails under cygwin with a Win32-flavoured ghc-pkg. 
     276        # https://rt.cpan.org/Ticket/Display.html?id=16375 fixes this, 
     277        # but we can't rely upon everyone having it.  As such, we have 
     278        # a very special cygwin work-around.  Ugh! 
     279 
     280 
     281        if (not $ghc_exe and $Config{osname} eq 'cygwin') { 
     282 
     283            warn "*** ghc-pkg not found in path.  Testing $ghc_pkg\n"; 
     284 
     285            # If the file exists, and it looks like it's in windows 
     286            # land, and it has an executable extension... 
     287 
     288            if ( -f $ghc_pkg  
     289                and $ghc_pkg =~ m{^(?:/cygdrive|[A-Za-z]:)/.*$Config{_exe}$} 
     290            ) { 
     291 
     292                # Smells like a Windows executable called from cygwin-land. 
     293                # Keep it. 
     294 
     295                $ghc_exe = $ghc_pkg; 
     296 
     297            } 
     298        } 
     299 
     300        # Our ghc-pkg is whatever executable we've found (which could be 
     301        # undef, if we didn't find anything). 
     302 
     303        $ghc_pkg = $ghc_exe; 
     304 
    216305    } 
    217306 
     
    219308        unless $ghc_pkg; 
    220309 
    221     return $ghc_pkg; 
     310    return $self->{ghc_pkg} = $ghc_pkg; 
    222311} 
    223312