Merge branch 'master' of https://Governor-Tarkin@bitbucket.org/Governor-Tarkin/swg...
[swg-src.git] / tools / recompileAllScripts.pl
blobbd7e5776bfa4d3f6c841bbfdf44d4569c54b01b6
1 #! /usr/bin/perl
3 use strict;
4 use warnings;
6 # =====================================================================
8 sub fatal
10 my $message = $_[0];
11 die $message;
14 sub perforceGetOpenFiles
16 my @files;
17 open(P4, "p4 -ztag opened -c default |");
19 while (<P4>)
21 chomp;
22 push (@files, $_) if (s/^\.\.\. depotFile //);
25 close(P4);
27 return @files;
30 sub perforceSubmit
32 # find out what files are open
33 my @files = perforceGetOpenFiles();
35 my $tmpfile = "submit.tmp";
37 # submit all the open files
38 open(TMP, ">" . $tmpfile);
40 print TMP "Change:\tnew\n";
41 print TMP "\nDescription:\n";
43 foreach (@_)
45 print TMP "\t", $_, "\n";
48 print TMP "\nFiles:\n";
49 foreach (@files)
51 print TMP "\t", $_, "\n";
54 close(TMP);
56 my $result = system("p4 submit -i < $tmpfile");
57 fatal "p4 submit failed" if ($result != 0);
58 unlink($tmpfile);
61 sub perforceWhere
63 # find out where a perforce file resides on the local machine
64 my $result;
66 open(P4, "p4 where $_[0] |");
67 $_ = <P4>;
68 chomp;
69 my @where = split;
70 $result = $where[2];
71 close(P4);
74 return $result;
77 sub findFiles
79 local $_;
81 my @paths = @_;
82 my @files = ();
84 while (@paths)
86 my $path = shift(@paths);
87 opendir(DIR, $path) || die "could not open dir $path";
88 while ($_ = readdir(DIR))
90 next if ($_ eq "." || $_ eq "..");
92 my $new = $path . "/" . $_;
93 if (-d $new)
95 push(@paths, $new);
97 else
99 push (@files, $new);
102 closedir(DIR);
105 return @files;
108 # =====================================================================
110 die "uasge: perl recompileAllScripts.pl branch\n" if (@ARGV != 1);
111 my $branch = shift;
113 my $depotScriptBaseDir = "//depot/swg/$branch/dsrc/sku.0/sys.server/compiled/game/script";
114 my $localScriptBaseDir = perforceWhere($depotScriptBaseDir);
115 my $depotClassBaseDir = "//depot/swg/$branch/data/sku.0/sys.server/compiled/game/script";
116 my $localClassBaseDir = perforceWhere($depotClassBaseDir);
117 my $depotJavaSourceDir = "//depot/swg/$branch/dsrc/sku.0/sys.server/compiled/game";
118 my $localJavaSourceDir = perforceWhere($depotJavaSourceDir);
119 my $depotJavaDestinationDir = "//depot/swg/$branch/data/sku.0/sys.server/compiled/game";
120 my $localJavaDestinationDir = perforceWhere($depotJavaDestinationDir);
121 my $pythonPreprocessor = perforceWhere("//depot/swg/$branch/exe/shared/script_prep.py");
123 my $perforceEditClassFiles = 1;
124 my $removeClassFiles = 1;
125 my $removeJavaFilesBefore = 1;
126 my $removeJavaFilesAfter = 1;
127 my $preprocess = 1;
128 my $preprocessExisting = 0;
129 my $batchPreprocess = 1;
130 my $verifyJavaFilesFromScriptFiles = 1;
131 my $compile = 1;
132 my $batchCompile = 1;
133 my $verifyClassFilesFromJavaFiles = 1;
134 my $updatePerforce = 1;
135 my $submitToPerforce = 0;
137 # =====================================================================
139 print STDERR "Running on branch $branch\n";
141 if ($perforceEditClassFiles)
143 print STDERR "Opening all .class files for edit...\n";
144 system("p4 edit $depotClassBaseDir/... > /dev/null") == 0 || die "could not edit class files\n";
145 system("p4 lock $depotClassBaseDir/... > /dev/null") == 0 || die "could not lock class files\n";
148 if ($removeClassFiles)
150 print STDERR "Deleting all .class files...\n";
151 foreach (findFiles($localClassBaseDir))
153 die "file does not end in .class" if (!/\.class$/);
154 unlink($_) || die "could not remove file $_\n";
158 if ($removeJavaFilesBefore)
160 print STDERR "Removing temporary java files...\n";
161 my @paths = ();
162 opendir(DIR, $localScriptBaseDir) || die "could not open dir $localScriptBaseDir";
163 while ($_ = readdir(DIR))
165 next if ($_ eq "." || $_ eq "..");
166 my $new = $localScriptBaseDir . "/" . $_;
167 push(@paths, $new) if (-d $new);
169 close(DIR);
170 foreach my $java (grep(/\.java$/, findFiles(@paths)))
172 unlink($java) || die "could not remove temporary java file $java\n";
176 if ($preprocess || $verifyJavaFilesFromScriptFiles)
178 my @files = grep(/\.script(lib)?$/, findFiles($localScriptBaseDir));
180 if ($preprocess)
182 my $start = time;
184 unlink("pythonPreprocessorStdout.log");
185 unlink("pythonPreprocessorStderr.log");
187 if ($batchPreprocess)
189 print STDERR "Batch preprocessing all scripts...\n";
190 open(PYTHON, "| xargs --max-procs=2 python2 $pythonPreprocessor -nocompile >> pythonPreprocessorStdout.log 2>> pythonPreprocessorStderr.log");
191 print PYTHON join("\n", @files), "\n";
192 close(PYTHON);
194 else
196 print STDERR "Single preprocessing all scripts...\n";
198 my $files = scalar(@files);
199 my $count = 1;
201 foreach my $script (@files)
203 print STDERR sprintf("%3d", (($count * 100) / $files)), "% $count/$files $script\n";
205 my $java = $script;
206 $java =~ s/\.script(lib)?/\.java/;
207 if ($preprocessExisting || !-e $java)
209 system("python2 $pythonPreprocessor -nocompile $script >> pythonPreprocessorStdout.log 2>> pythonPreprocessorStderr.log");
211 $count += 1;
215 print STDERR "preprocessing: ", time - $start, "s\n";
218 die "script files failed preprocessing\n" if (-s "pythonPreprocessorStderr.log");
220 if ($verifyJavaFilesFromScriptFiles)
222 my $die = 0;
223 foreach my $script (@files)
225 my $java = $script;
226 $java =~ s/\.script(lib)?/\.java/;
227 if (!-e $java)
229 print STDERR "could not find java file $java from script file $script\n";
230 $die = 1;
233 die "missing java files\n" if ($die);
237 if ($compile || $verifyClassFilesFromJavaFiles)
239 my @files = grep(/\.java$/, findFiles($localScriptBaseDir));
240 my $files = scalar(@files);
242 if ($compile)
244 my $start = time;
245 unlink("javac.log");
247 if ($batchCompile)
249 print STDERR "Batch java compiling all the scripts...\n";
251 open(JAVAC, "| xargs javac -classpath $localJavaDestinationDir -d $localJavaDestinationDir -sourcepath $localJavaSourceDir -g -deprecation >> javac.log 2>&1");
252 print JAVAC join("\n", @files), "\n";
253 close(JAVAC);
255 else
257 print STDERR "Single java compiling all the scripts...\n";
258 my $count = 1;
259 my $errors = 0;
261 foreach my $java (@files)
263 print STDERR sprintf("%3d", (($count * 100) / $files)), "% $count/$files $errors $java\n";
264 $errors += 1 if (system("javac -classpath $localJavaDestinationDir -d $localJavaDestinationDir -sourcepath $localJavaSourceDir -g -deprecation $java >> javac.log 2>&1") != 0);
265 $count += 1;
269 print STDERR "compile: ", time - $start, "s\n";
272 die "java files failed compilation\n" if (-s "javac.log");
274 if ($verifyClassFilesFromJavaFiles)
276 my $die = 0;
277 foreach my $java (@files)
279 my $class = $java;
280 $class =~ s/\.java/\.class/;
281 $class =~ s/$localScriptBaseDir/$localClassBaseDir/;
282 if (!-e $class)
284 print STDERR "could not find class file $class for java file $java\n";
285 $die = 1;
288 die "missing class files\n" if ($die);
292 if ($removeJavaFilesAfter)
294 print STDERR "Removing temporary java files...\n";
295 my @paths = ();
296 opendir(DIR, $localScriptBaseDir) || die "could not open dir $localScriptBaseDir";
297 while ($_ = readdir(DIR))
299 next if ($_ eq "." || $_ eq "..");
300 my $new = $localScriptBaseDir . "/" . $_;
301 push(@paths, $new) if (-d $new);
303 close(DIR);
304 foreach my $java (grep(/\.java$/, findFiles(@paths)))
306 unlink($java) || die "could not remove temporary java file $java\n";
310 if ($updatePerforce)
312 print STDERR "Searching for class files to delete...\n";
313 my @openedFiles = perforceGetOpenFiles();
314 my @deleted;
315 foreach (grep(/\.class$/, @openedFiles))
317 s/$depotClassBaseDir/$localClassBaseDir/;
318 push (@deleted, $_) if (! -e $_);
321 if (@deleted)
323 print join("\n", @deleted), "\n";
325 print STDERR "Deleting old .class files...\n";
326 open(P4, "| p4 -x - revert > /dev/null");
327 print P4 join("\n", @deleted), "\n";
328 close(P4);
330 open(P4, "| p4 -x - delete > /dev/null");
331 print P4 join("\n", @deleted), "\n";
332 close(P4);
335 print STDERR "Searching for class files to add...\n";
336 open(P4, "| p4 -x - add 2> /dev/null > /dev/null");
337 foreach (findFiles($localClassBaseDir))
339 die "file does not end in .class" if (!/\.class$/);
340 print $_, "\n";
341 print P4 $_, "\n";
343 close(P4);
345 print STDERR "Reverting unchanged files...\n";
346 system("p4 revert -a > /dev/null");
348 if ($submitToPerforce)
350 print STDERR "Submitting to perforce...\n";
351 perforceSubmit("[automated]", "- Recompile all scripts");
355 exit 0;