clean.pl 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/usr/bin/perl
  2. use Cwd;
  3. our $msg;
  4. clean_build_files();
  5. defined($msg) and print $msg;
  6. 1;
  7. sub recurse_dirs {
  8. foreach my $i (@_) {
  9. my $orig_dir = cwd;
  10. unless (-d $i && chdir $i) {
  11. $msg = "clean.pl: could not enter '$i'.\n";
  12. return 0;
  13. }
  14. print "Entering '$i'\n";
  15. clean_build_files($i) or return 0;
  16. print "Leaving '$i'\n";
  17. unless (chdir $orig_dir) {
  18. $msg = "clean.pl: original directory disappeared.\n";
  19. return 0;
  20. }
  21. }
  22. return 1;
  23. }
  24. sub delete_file {
  25. if (-f $_[0]) {
  26. print "Deleting '$_[0]'.\n";
  27. unless (unlink $_[0]) {
  28. $msg = "clean.pl: could not delete '$_[0]'.\n";
  29. return 0;
  30. }
  31. }
  32. return 1;
  33. }
  34. sub clean_build_files {
  35. # recurse build directories
  36. opendir (my $dir, '.') or die "Cannot read current directory.\n";
  37. my @dirs = grep { -d $_ && not $_ =~ /^\./ } readdir($dir);
  38. closedir ($dir);
  39. recurse_dirs(@dirs) or return 0;
  40. ## Clean up build files ##
  41. my @files;
  42. # remove object files
  43. @files = <*.o>;
  44. foreach my $i (@files) {
  45. delete_file($i) or return 0;
  46. }
  47. # remove exe files
  48. @files = <*.exe*>;
  49. foreach my $i (@files) {
  50. delete_file($i) or return 0;
  51. }
  52. return 1;
  53. }