| File: | config/inter/yacc.pm |
| Coverage: | 77.9% |
| line | stmt | bran | cond | sub | code |
|---|---|---|---|---|---|
| 1 | # Copyright (C) 2001-2007, Parrot Foundation. | ||||
| 2 | |||||
| 3 - 12 | =head1 NAME config/auto/yacc.pm - parser generator =head1 DESCRIPTION Determines whether C<yacc> is installed and if it's actually C<bison> of at least the user-specified minimum version, defaulting to 2.1. =cut | ||||
| 13 | |||||
| 14 | package inter::yacc; | ||||
| 15 | |||||
| 16 | 3 3 3 | use strict; | |||
| 17 | 3 3 3 | use warnings; | |||
| 18 | |||||
| 19 | |||||
| 20 | 3 3 3 | use base qw(Parrot::Configure::Step); | |||
| 21 | |||||
| 22 | 3 3 3 | use Parrot::Configure::Utils qw( :inter capture_output check_progs ); | |||
| 23 | |||||
| 24 | sub _init { | ||||
| 25 | 6 | my $self = shift; | |||
| 26 | 6 | my %data; | |||
| 27 | 6 | $data{description} = q{Is yacc installed}; | |||
| 28 | 6 | $data{result} = q{}; | |||
| 29 | 6 | return \%data; | |||
| 30 | } | ||||
| 31 | |||||
| 32 | my @yacc_defaults = | ||||
| 33 | defined( $ENV{TEST_YACC} ) | ||||
| 34 | ? $ENV{TEST_YACC} | ||||
| 35 | : ( 'bison -v -y', 'yacc', 'byacc' ); | ||||
| 36 | |||||
| 37 | my $default_required = '2.1'; | ||||
| 38 | |||||
| 39 | sub runstep { | ||||
| 40 | 6 | my ( $self, $conf ) = @_; | |||
| 41 | 6 | my $util = 'yacc'; | |||
| 42 | 6 | my $prompt = "Do you have a parser generator, like bison or yacc?"; | |||
| 43 | |||||
| 44 | 6 | my $verbose = $conf->options->get('verbose'); | |||
| 45 | |||||
| 46 | # undef means we don't have bison... default to not having bison | ||||
| 47 | 6 | $conf->data->set( bison_version => undef ); | |||
| 48 | |||||
| 49 | 6 | unless ( $conf->options->get('maintainer') ) { | |||
| 50 | 2 | $conf->data->set( $util => 'echo' ); | |||
| 51 | 2 | $self->set_result('skipped'); | |||
| 52 | 2 | return 1; | |||
| 53 | } | ||||
| 54 | |||||
| 55 | # precedence of sources for the program: | ||||
| 56 | # default -> probe -> environment -> option -> ask | ||||
| 57 | 4 | my $prog = $conf->options->get($util); | |||
| 58 | 4 | unless ($prog) { | |||
| 59 | 3 | $prog = $ENV{ uc($util) }; | |||
| 60 | } | ||||
| 61 | |||||
| 62 | # never override the user. If a non-existent program is specified then | ||||
| 63 | # the user is responsible for the consequences. | ||||
| 64 | 4 | if ( defined $prog ) { | |||
| 65 | 2 | $conf->data->set( $util => $prog ); | |||
| 66 | 2 | $self->set_result('user defined'); | |||
| 67 | 2 | return 1; | |||
| 68 | } | ||||
| 69 | else { | ||||
| 70 | |||||
| 71 | 2 | $prog = check_progs( [@yacc_defaults], $verbose ); | |||
| 72 | 2 | if ( !$prog ) { | |||
| 73 | 1 | $self->set_result('no yacc program was found'); | |||
| 74 | 1 | return; | |||
| 75 | } | ||||
| 76 | else { | ||||
| 77 | |||||
| 78 | 1 | if ( $conf->options->get('ask') ) { | |||
| 79 | 1 | $prog = prompt( $prompt, $prog ? $prog : $conf->data->get($util) ); | |||
| 80 | } | ||||
| 81 | 1 | my ( $stdout, $stderr, $ret ) = capture_output( $prog, '--version' ); | |||
| 82 | |||||
| 83 | # don't override the user even if the program they provided | ||||
| 84 | # appears to be broken | ||||
| 85 | 1 | if ( $ret == -1 and !$conf->options->get('ask') ) { | |||
| 86 | |||||
| 87 | # fall back to default | ||||
| 88 | 0 | $self->set_result('yacc program does not exist or does not understand --version'); | |||
| 89 | 0 | return; | |||
| 90 | } | ||||
| 91 | elsif ( $stdout =~ /Bison .*? (\d+) \. (\d+) (\w)?/x ) { | ||||
| 92 | |||||
| 93 | # if '--version' returns a string assume that this is bison. | ||||
| 94 | # if this is bison pretending to be yacc | ||||
| 95 | # '--version' doesn't work | ||||
| 96 | # someday we might need to check $3 also. | ||||
| 97 | 1 | my ( $prog_major, $prog_minor, $prog_patch ) = ( $1, $2, $3 ); | |||
| 98 | 1 | my $prog_version = "$1.$2"; | |||
| 99 | 1 | $prog_version .= $3 if $3; | |||
| 100 | |||||
| 101 | # is there a version requirement? | ||||
| 102 | 1 | my $req = $conf->options->get('bison_required'); | |||
| 103 | 1 | unless ( defined $req ) { | |||
| 104 | 1 | $req = $default_required; | |||
| 105 | } | ||||
| 106 | 1 | if ($req) { | |||
| 107 | 1 | my ( $rmajor, $rminor ) = ( $req =~ / ^ (\d+) \. (\d+) (\w)? $ /x ); | |||
| 108 | 1 | if ( !defined $rmajor ) { | |||
| 109 | 0 | $self->set_result("could not understand bison version requirement"); | |||
| 110 | 0 | return; | |||
| 111 | } | ||||
| 112 | elsif ( | ||||
| 113 | $prog_major < $rmajor | ||||
| 114 | or ( $prog_major == $rmajor | ||||
| 115 | and $prog_minor < $rminor ) | ||||
| 116 | ) | ||||
| 117 | { | ||||
| 118 | 0 | $self->set_result( "found bison version $prog_version" | |||
| 119 | . " but at least $rmajor.$rminor is required" ); | ||||
| 120 | 0 | return; | |||
| 121 | } | ||||
| 122 | else { | ||||
| 123 | 1 | 1; # lack an explicit 'else' here | |||
| 124 | } | ||||
| 125 | } | ||||
| 126 | 1 | $conf->data->set( bison_version => $prog_version ); | |||
| 127 | 1 | $self->set_result("bison $prog_version"); | |||
| 128 | 1 | $conf->data->set( $util => $prog ); | |||
| 129 | 1 | return 1; | |||
| 130 | } | ||||
| 131 | else { | ||||
| 132 | 0 | $self->set_result('yacc program does not exist or does not understand --version'); | |||
| 133 | 0 | return; | |||
| 134 | } | ||||
| 135 | } | ||||
| 136 | } | ||||
| 137 | } | ||||
| 138 | |||||
| 139 | 1; | ||||
| 140 | |||||
| 141 | # Local Variables: | ||||
| 142 | # mode: cperl | ||||
| 143 | # cperl-indent-level: 4 | ||||
| 144 | # fill-column: 100 | ||||
| 145 | # End: | ||||
| 146 | # vim: expandtab shiftwidth=4: | ||||