| 1 | #!/usr/bin/perl
|
|---|
| 2 |
|
|---|
| 3 | use strict;
|
|---|
| 4 | use warnings;
|
|---|
| 5 | use Path::Class;
|
|---|
| 6 | use autodie;
|
|---|
| 7 | use File::Basename;
|
|---|
| 8 |
|
|---|
| 9 | my $numArgs = scalar(@ARGV);
|
|---|
| 10 |
|
|---|
| 11 | my $folder = dir(".");
|
|---|
| 12 |
|
|---|
| 13 | my $outName = "Makefile";
|
|---|
| 14 |
|
|---|
| 15 | if($numArgs > 1){
|
|---|
| 16 | warn "At most ONE argument is needed for this program. Additional arguments will be ignored.\n";
|
|---|
| 17 | }
|
|---|
| 18 |
|
|---|
| 19 | if($numArgs > 0){
|
|---|
| 20 | if($ARGV[0] eq "help"){
|
|---|
| 21 | printHelp();
|
|---|
| 22 | exit;
|
|---|
| 23 | }else{
|
|---|
| 24 | if((not -d $ARGV[0]) and (not -e $ARGV[0])){
|
|---|
| 25 | print "The file or directory $ARGV[0] doesn't exist!\n";
|
|---|
| 26 | print "Please use a valid file or directory.\n";
|
|---|
| 27 | exit;
|
|---|
| 28 | }else{
|
|---|
| 29 | $folder = dir("$ARGV[0]");
|
|---|
| 30 | }
|
|---|
| 31 | if($numArgs > 1){
|
|---|
| 32 | $outName = $ARGV[1];
|
|---|
| 33 | }
|
|---|
| 34 | }
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | open(my $fileHandle, '>', "$outName") or die "Can't write to file: $!";
|
|---|
| 38 | print $fileHandle "CIVL=civl\n";
|
|---|
| 39 | print $fileHandle "VERIFY=\$(CIVL) verify -enablePrintf=false\n";
|
|---|
| 40 | traverse($folder, $fileHandle);
|
|---|
| 41 | close $fileHandle;
|
|---|
| 42 | print "Makefile generated for $folder successfully\n";
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 | sub printHelp{
|
|---|
| 46 | print "======= HELP =======\n";
|
|---|
| 47 | print "This script is to generate a Makefile for c/cvl/cu files in a given folder.\n";
|
|---|
| 48 | print "Usages:\n";
|
|---|
| 49 | print "perl generate.pl: generate Makefile for all C/CIVL-C program in the CURRENT folder\n";
|
|---|
| 50 | print "perl generate.pl dirname: generate Makefile for all C/CIVL-C program in the given folder\n";
|
|---|
| 51 | print "perl generate.pl dirname filename: generate Makefile for all C/CIVL-C program in the given folder and save it as the given filename\n";
|
|---|
| 52 | print "perl generate.pl help: show this message\n";
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | sub traverse{
|
|---|
| 56 | my ($dir) = $_[0];
|
|---|
| 57 | my ($out) = $_[1];
|
|---|
| 58 | my (@files);
|
|---|
| 59 |
|
|---|
| 60 | print $out "all: ";
|
|---|
| 61 | if(not -d $dir){
|
|---|
| 62 | return;
|
|---|
| 63 | }
|
|---|
| 64 | print "working in $dir\n";
|
|---|
| 65 | return if not -d $dir;
|
|---|
| 66 | while(my $file = $dir->next){
|
|---|
| 67 | next if $file eq "$dir"
|
|---|
| 68 | or $file eq "$dir" . '/..'
|
|---|
| 69 | or $file->basename eq "CIVLREP";
|
|---|
| 70 | #print $file->stringify . "\n";
|
|---|
| 71 | my $target;
|
|---|
| 72 | unless($file->is_dir()){
|
|---|
| 73 | if ($file =~ /\.(c|cvl|cu)/) {
|
|---|
| 74 | my $target = basename("$file", "");
|
|---|
| 75 |
|
|---|
| 76 | # print "file is " . $file->stringify . "\n";
|
|---|
| 77 | push(@files, $target);
|
|---|
| 78 | ($target = $target) =~ s/\.[^.]+$//;
|
|---|
| 79 | # print "target is $target \n";
|
|---|
| 80 | print $out $target . " ";
|
|---|
| 81 | }
|
|---|
| 82 | }
|
|---|
| 83 | }
|
|---|
| 84 | print $out "\n";
|
|---|
| 85 | foreach (@files) {
|
|---|
| 86 | my $target;
|
|---|
| 87 | my $file = $_;
|
|---|
| 88 |
|
|---|
| 89 | ($target = $file) =~ s/\.[^.]+$//;
|
|---|
| 90 | print $out "\n" . $target . ": $file\n";
|
|---|
| 91 | print $out "\t" . "\$(VERIFY) $file\n";
|
|---|
| 92 | }
|
|---|
| 93 | }
|
|---|