r8619@datura: manu | 2008-05-05 16:49:49 +0200
[manu/RT-Extension-SearchResults-XLS.git] / inc / Module / Install / Can.pm
1 package Module::Install::Can;
2
3 use strict;
4 use Module::Install::Base;
5 use Config ();
6 ### This adds a 5.005 Perl version dependency.
7 ### This is a bug and will be fixed.
8 use File::Spec ();
9 use ExtUtils::MakeMaker ();
10
11 use vars qw{$VERSION $ISCORE @ISA};
12 BEGIN {
13         $VERSION = '0.72';
14         $ISCORE  = 1;
15         @ISA     = qw{Module::Install::Base};
16 }
17
18 # check if we can load some module
19 ### Upgrade this to not have to load the module if possible
20 sub can_use {
21         my ($self, $mod, $ver) = @_;
22         $mod =~ s{::|\\}{/}g;
23         $mod .= '.pm' unless $mod =~ /\.pm$/i;
24
25         my $pkg = $mod;
26         $pkg =~ s{/}{::}g;
27         $pkg =~ s{\.pm$}{}i;
28
29         local $@;
30         eval { require $mod; $pkg->VERSION($ver || 0); 1 };
31 }
32
33 # check if we can run some command
34 sub can_run {
35         my ($self, $cmd) = @_;
36
37         my $_cmd = $cmd;
38         return $_cmd if (-x $_cmd or $_cmd = MM->maybe_command($_cmd));
39
40         for my $dir ((split /$Config::Config{path_sep}/, $ENV{PATH}), '.') {
41                 my $abs = File::Spec->catfile($dir, $_[1]);
42                 return $abs if (-x $abs or $abs = MM->maybe_command($abs));
43         }
44
45         return;
46 }
47
48 # can we locate a (the) C compiler
49 sub can_cc {
50         my $self   = shift;
51         my @chunks = split(/ /, $Config::Config{cc}) or return;
52
53         # $Config{cc} may contain args; try to find out the program part
54         while (@chunks) {
55                 return $self->can_run("@chunks") || (pop(@chunks), next);
56         }
57
58         return;
59 }
60
61 # Fix Cygwin bug on maybe_command();
62 if ( $^O eq 'cygwin' ) {
63         require ExtUtils::MM_Cygwin;
64         require ExtUtils::MM_Win32;
65         if ( ! defined(&ExtUtils::MM_Cygwin::maybe_command) ) {
66                 *ExtUtils::MM_Cygwin::maybe_command = sub {
67                         my ($self, $file) = @_;
68                         if ($file =~ m{^/cygdrive/}i and ExtUtils::MM_Win32->can('maybe_command')) {
69                                 ExtUtils::MM_Win32->maybe_command($file);
70                         } else {
71                                 ExtUtils::MM_Unix->maybe_command($file);
72                         }
73                 }
74         }
75 }
76
77 1;
78
79 __END__
80
81 =pod
82
83 =head1 NAME
84
85 Module::Install::Can - Utility functions for capability detection
86
87 =head1 DESCRIPTION
88
89 C<Module::Install::Can> contains a number of functions for authors to use
90 when creating customised smarter installers. The functions simplify
91 standard tests so that you can express your dependencies and conditions
92 much more simply, and make your installer much easier to maintain.
93
94 =head1 COMMANDS
95
96 =head2 can_use
97
98   can_use('Module::Name');
99   can_use('Module::Name', 1.23);
100
101 The C<can_use> function tests the ability to load a specific named
102 module. Currently it will also actually load the module in the
103 process, although this may change in the future.
104
105 Takes an optional second param of a version number. The currently
106 installed version of the module will be tested to make sure it is
107 equal to or greater than the specified version.
108
109 Returns true if the module can be loaded, or false (in both scalar or
110 list context) if not.
111
112 =head2 can_run
113
114   can_run('cvs');
115
116 The C<can_run> function tests the ability to run a named command or
117 program on the local system.
118
119 Returns true if so, or false (both in scalar and list context) if not.
120
121 =head2 can_cc
122
123   can_cc();
124
125 The C<can_cc> function test the ability to locate a C compiler on the
126 local system. Returns true if the C compiler can be found, or false
127 (both in scalar and list context) if not.
128
129 =head1 TO DO
130
131 Currently, the use of a C<can_foo> command in a single problem domain
132 (for example C<can_use>) results in the inclusion of additional
133 functionality from different problem domains (for example C<can_run>).
134
135 This module should ultimately be broken up, and the individual
136 functions redestributed to different domain-specific extensions.
137
138 =head1 AUTHORS
139
140 Audrey Tang E<lt>autrijus@autrijus.orgE<gt>
141
142 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
143
144 =head1 SEE ALSO
145
146 L<Module::Install>, L<Class::Inspector>
147
148 =head1 COPYRIGHT
149
150 Copyright 2006 Audrey Tang, Adam Kennedy.
151
152 This program is free software; you can redistribute it and/or modify it
153 under the same terms as Perl itself.
154
155 See L<http://www.perl.com/perl/misc/Artistic.html>
156
157 =cut