#!/usr/bin/env perl # # make2csv - translate make -p output to CSV format # # $Id$ use warnings; use strict; use Getopt::Std; use Text::CSV_XS; my %opt; getopts( 'h?dt:', \%opt ); HELP_MESSAGE() if $opt{'h'} || $opt{'?'}; my $sep = $opt{'t'} // ','; my $csvh = new Text::CSV_XS( { sep_char => $sep, eol => $/ } ) or die "the CSV writer doesn't work (not installed?)\n"; doit(); exit(0); sub HELP_MESSAGE { print STDERR <) { chomp; if (/\\$/) { $line .= $_ . ' '; next; } $_ = $line . $_; $line = ''; if (/^#/) { # comment - ignore, does not affect $state dinfo('comment'); next; } elsif (/^\t/) { # a recipe line if ( $state eq 'outside' ) { oomph('ignoring recipe line outside rule'); } elsif ( $state eq 'rule' ) { $csvh->print( *STDOUT, [ $_, '@' . ++$r ] ) for @targets; $state = 'recipe'; } } elsif ( my ( $targets, $dependencies ) = /^([^:=\[]+):([^=].*|)\s*$/ ) { # a rule header # note: "make[2]: Nothing to be done for `all'." isn't @targets = split( ' ', $targets ); my @dependencies = split( ' ', $dependencies ); # TO DO: this isn't exact on special kinds of rules foreach my $t (@targets) { if ( !@dependencies ) { $csvh->print( *STDOUT, [$t] ); } else { foreach my $d (@dependencies) { $csvh->print( *STDOUT, [ $t, $d ] ); } } } $state = 'rule'; } else { # some other kind of line, probably assignment or comment $state = 'outside'; @targets = (); } dinfo($state); } if ( $line ne '' ) { oomph('ignoring last line because file ends with \\'); } }