automatic-makefile-copy.pl 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #!/usr/local/bin/perl
  2. eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
  3. if 0; #$running_under_some_shell
  4. # Set the variable $File::Find::dont_use_nlink if you're using AFS,
  5. # since AFS cheats.
  6. use strict;
  7. #get arguments and needed infos
  8. my $dir_workspace;
  9. if(@ARGV[0])
  10. {
  11. $dir_workspace=@ARGV[0];
  12. }
  13. else
  14. {
  15. print "Please tell me the main directory of your C++ Library: ";
  16. chomp ($dir_workspace=<STDIN>);
  17. print "main directory: $dir_workspace !\n";
  18. }
  19. my $dir_template;
  20. if(@ARGV[1])
  21. {
  22. $dir_template=@ARGV[1];
  23. }
  24. else
  25. {
  26. print "Please tell me the main directory of your Makefile templates C++/Make Directory: ";
  27. chomp ($dir_template=<STDIN>);
  28. print "Makefile template directory: $dir_template !\n";
  29. }
  30. my $is_sublib;
  31. if(@ARGV[2])
  32. {
  33. $is_sublib=@ARGV[2];
  34. }
  35. else
  36. {
  37. print "Is your library is a sublibrary? (yes/no): ";
  38. chomp ($is_sublib=<STDIN>);
  39. print "Sub-Library: $is_sublib !\n";
  40. }
  41. $is_sublib = ($is_sublib =~ /yes/i);
  42. my $scriptmode;
  43. if(@ARGV[3])
  44. {
  45. $scriptmode=@ARGV[3];
  46. }
  47. else
  48. {
  49. print "Scriptmode (copy commands only)? (yes/no): ";
  50. chomp ($scriptmode=<STDIN>);
  51. print "Scriptmode: $scriptmode !\n";
  52. }
  53. $scriptmode = ($scriptmode =~ /yes/i);
  54. my $simulation;
  55. if(@ARGV[4])
  56. {
  57. $simulation=@ARGV[4];
  58. }
  59. else
  60. {
  61. print "Please tell me if you want to do a simulation? (yes/no): ";
  62. chomp ($simulation=<STDIN>);
  63. print "Simulation: $simulation !\n";
  64. }
  65. $simulation = ($simulation =~ /yes/i);
  66. #help
  67. if(!$scriptmode)
  68. {
  69. print "Makefile distribution tool for Libraries and its Sub-Libraries.\n";
  70. print "argument 1: Path to your Library\n";
  71. print "argument 2: Path to your Makefile Templates\n";
  72. print "argument 3: Distribute Makefiles in a Sub-Library (yes/no)\n";
  73. print "argument 4: Script Mode (copy commands only) (yes/no)\n";
  74. print "argument 5: Simulation (yes/no)\n\n";
  75. }
  76. #real work
  77. &searchDirectory($dir_workspace);
  78. sub searchDirectory {
  79. my $dir;
  80. my @lines;
  81. my $subdir;
  82. my $line;
  83. $dir = $_[0];
  84. #buggy if on, with $last_dir ASK ERIC
  85. if(!$scriptmode)
  86. {
  87. print "current directory:$dir \n";
  88. }
  89. checkdirectory($dir);
  90. # check for permission
  91. if(-x $dir) {
  92. # search any sub directories
  93. @lines = `cd $dir; ls -l`;
  94. foreach $line (@lines) {
  95. if($line =~ /^d/) {
  96. $line =~ /\s+(\S+)$/;
  97. $subdir = $dir."/".$1;
  98. searchDirectory($subdir);
  99. }
  100. }
  101. }
  102. }
  103. sub checkdirectory{
  104. my $dir;
  105. my $last_dir;
  106. #important: files to copy and directories to handle/ignore
  107. my @filenames=(["/progs/Makefile.inc"],["/tests/Makefile.inc"],["/sublib/Makefile.inc","/sublib/Makefile"]);
  108. #directories to handle
  109. my @dirnames=("progs","tests");
  110. #directories to ignore
  111. my @dirnames_ignore=("CVS","BUILD","templates","doc");
  112. #get current directory and extract last part of the current directory
  113. $dir = $_[0];
  114. if ( $dir =~ /\/([^\/]+)\/?$/ )
  115. {
  116. $last_dir=$1;
  117. } else {
  118. die ("unable to parse $dir\n");
  119. }
  120. # print "$last_dir\n";
  121. #compare to template specs
  122. my $count=0;
  123. my $found=0;
  124. #check ignored directories
  125. foreach my $possible_dir (@dirnames_ignore)
  126. {
  127. if ($is_sublib)
  128. {
  129. #sublib case
  130. }
  131. else #maindirectory is ignored
  132. {
  133. if (($found==0) && ($dir eq $dir_workspace))
  134. {
  135. if(!$scriptmode)
  136. {
  137. print "subdirectory_type: Ignored Main Directory\n";
  138. }
  139. $found=1;
  140. }
  141. }
  142. if (($found==0) && ($dir =~ /$dir_template/))
  143. {
  144. if(!$scriptmode)
  145. {
  146. print "subdirectory_type: Ignored Template Directory\n";
  147. }
  148. $found=1;
  149. }
  150. if (($found==0) && ($dir =~ /$possible_dir/))
  151. {
  152. if(!$scriptmode)
  153. {
  154. print "subdirectory_type: Ignored Subdirectory $possible_dir\n";
  155. }
  156. $found=1;
  157. }
  158. }
  159. #check if last directory corresponds to correct directories and do sth.
  160. foreach my $possible_dir (@dirnames)
  161. {
  162. # print "$found $last_dir $possible_dir\n";
  163. if(($found==0) && ($last_dir eq $possible_dir))
  164. {
  165. if(!$scriptmode)
  166. {
  167. print "subdirectory_type: $last_dir\n";
  168. }
  169. #do what has to be done
  170. foreach my $filename_entry(@{$filenames[$count]})
  171. {
  172. my $filename_tmp="$dir_template$filename_entry";
  173. if($simulation)
  174. {
  175. if($scriptmode)
  176. {
  177. print "cp $filename_tmp $dir\n";
  178. }
  179. else
  180. {
  181. print "simulation: cp $filename_tmp $dir\n";
  182. }
  183. }
  184. else
  185. {
  186. if($scriptmode)
  187. {
  188. print "cp $filename_tmp $dir\n";
  189. }
  190. else
  191. {
  192. print "copying: cp $filename_tmp $dir\n";
  193. }
  194. `cp $filename_tmp $dir`;
  195. }
  196. }
  197. $found=1;
  198. }
  199. $count++;
  200. }
  201. #normal library case
  202. if ($found==0)
  203. {
  204. if(!$scriptmode)
  205. {
  206. print "subdirectory_type: sublibrary\n";
  207. }
  208. foreach my $filename_entry(@{$filenames[$count]})
  209. {
  210. my $filename_tmp="$dir_template$filename_entry";
  211. if($simulation)
  212. {
  213. if($scriptmode)
  214. {
  215. print "cp $filename_tmp $dir\n";
  216. }
  217. else
  218. {
  219. print "simulation: cp $filename_tmp $dir\n";
  220. }
  221. }
  222. else
  223. {
  224. if($scriptmode)
  225. {
  226. print "cp $filename_tmp $dir\n";
  227. }
  228. else
  229. {
  230. print "copying: cp $filename_tmp $dir\n";
  231. }
  232. `cp $filename_tmp $dir`;
  233. }
  234. }
  235. $found=1;
  236. }
  237. if(!$scriptmode)
  238. {
  239. print "\n";
  240. }
  241. }