makemake.pl

#!/usr/bin/perl

###
### makemake.pl -> executes a make every time a (set of files) changed on disk
###

$DEFAULT_SECONDS = 2;
@DEFAULT_MAKEFILES = ("Makefile", "makefile");
$DEFAULT_TARGET = "all";

###
### do not change anything below
###

use Getopt::Std;
getopts("s:m:ht:");
if($opt_h) {
  print "Usage is:\nmakemake.pl [-m makefile] [-t target] [-s secs] [-h] filenam
e*\n";
  exit(0);
}

if($opt_m) {
  $makefile = $opt_m;
} else {
  $makefile = ""; 
  foreach $m (@DEFAULT_MAKEFILES) {
    if(-r $m) {
      $makefile = $m;
      last;
    }
  }
  if($makefile eq "") {
    printf "Cannot find default makefiles\n";
    exit(-1);
  }
}

if($opt_t) {
  $target = $opt_t;
} else {
  $target = $DEFAULT_TARGET;
}
 
if($opt_s) {
  $seconds = $opt_s; 
} else {
  $seconds = $DEFAULT_SECONDS;
}

print "Compiling with make $makefile every $seconds secs (target is $target)\n";
print "File(s): ";
@files = @ARGV;
foreach $f (@files) {
  print "$f ";
  $filetimes{$f} = (stat($f))[9];
}
print "\n";


while(true) {
  sleep($seconds);

  $recomp = 0;
  foreach $f (@files) {
      $t = (stat($f))[9];
      if($t != $filetimes{$f}) {
          print "---> Recompile (due to $f)\n";
          $filetimes{$f} = $t;  # adjust the modified file time
          $recomp = 1;
      }
  }

  if($recomp == 1) {
    print "Recompiling ... \n";
    $cmd = "make -m $makefile $target";
    ### print "$cmd\nDone!\n";
    system($cmd);
  } else {
    print "Looping ... \n";
  }
}

exit(0);
### fine lavori