Author: jhoblitt
Date: Wed Oct 19 02:44:09 2005
New Revision: 9512

Modified:
   trunk/CREDITS
   trunk/lib/Parrot/Configure/Step.pm
Log:
add chec_progs() to Parrot::Configure::Step


Modified: trunk/CREDITS
==============================================================================
--- trunk/CREDITS       (original)
+++ trunk/CREDITS       Wed Oct 19 02:44:09 2005
@@ -228,6 +228,7 @@ D: updated version of parrotbench.pl
 N: Joshua Hoblitt
 E: [EMAIL PROTECTED]
 D: bug wrangling, doc & makefile cleanups, and Pod tests
+D: mucking around with Configure
 
 N: Josh Wilmes
 

Modified: trunk/lib/Parrot/Configure/Step.pm
==============================================================================
--- trunk/lib/Parrot/Configure/Step.pm  (original)
+++ trunk/lib/Parrot/Configure/Step.pm  Wed Oct 19 02:44:09 2005
@@ -26,7 +26,10 @@ package Parrot::Configure::Step;
 use strict;
 use Exporter;
 use Carp;
+use File::Basename qw( basename );
 use File::Copy ();
+use File::Spec;
+
 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
 
 @ISA = qw(Exporter);
@@ -34,11 +37,13 @@ use vars qw(@ISA @EXPORT @EXPORT_OK %EXP
 @EXPORT = ();
 
 @EXPORT_OK = qw(prompt genfile copy_if_diff move_if_diff integrate
-                cc_gen cc_build cc_run cc_clean cc_run_capture capture_output);
+                cc_gen cc_build cc_run cc_clean cc_run_capture capture_output
+                check_progs);
 
 %EXPORT_TAGS = (
         inter => [qw(prompt integrate)],
-        auto  => [qw(cc_gen cc_build cc_run cc_clean cc_run_capture)],
+        auto  => [qw(cc_gen cc_build cc_run cc_clean cc_run_capture
+                  capture_output check_progs)],
         gen   => [qw(genfile copy_if_diff move_if_diff)]
                );
 
@@ -463,6 +468,46 @@ sub capture_output {
     return $output;
 }
 
+=item C<check_progs([$programs])>
+
+Where C<$programs> may be either a scalar with the name of a single program or
+an array ref of programs to search the current C<PATH> for.  The first matching
+program name is returned or C<undef> on failure.  Note: this function only
+returns the name of the program and not its complete path.
+
+This function is similar to C<autoconf>'s C<AC_CHECK_PROGS> macro.
+
+=cut
+
+sub check_progs {
+    my $progs = shift;
+
+    $progs = [$progs] unless ref $progs eq 'ARRAY';
+    my $verbose = Configure::Data->get('verbose');
+
+    print "checking for program: ", join(" or ", @$progs), "\n" if $verbose;
+    foreach my $prog (@$progs) {
+        # try relative path first in case it's not in the path
+        return $prog if -x $prog;
+
+        my $util = basename($prog);
+        # use the first word in the string to ignore any options
+        ($util) = $util =~ /(\w+)/;
+        foreach my $dir (File::Spec->path) {
+            my $path = File::Spec->catfile($dir, $util);
+
+            if ($verbose) {
+                print "trying: $path\n";
+                print "$path is executable\n" if -x $path;
+            }
+
+            return $prog if -x $path;
+        }
+    }
+
+    return;
+}
+
 =back
 
 =head1 SEE ALSO

Reply via email to