parallelcl 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use File::Basename;
  5. use File::Spec;
  6. use File::Temp;
  7. use POSIX;
  8. sub makeJob(\@$);
  9. sub forkAndCompileFiles(\@$);
  10. sub Exec($);
  11. sub waitForChild(\@);
  12. sub cleanup(\@);
  13. my $debug = 0;
  14. chomp(my $clexe = `cygpath -u '$ENV{'VS80COMNTOOLS'}/../../VC/bin/cl.exe'`);
  15. if ($debug) {
  16. print STDERR "Received " . @ARGV . " arguments:\n";
  17. foreach my $arg (@ARGV) {
  18. print STDERR "$arg\n";
  19. }
  20. }
  21. my $commandFile;
  22. foreach my $arg (@ARGV) {
  23. if ($arg =~ /^[\/-](E|EP|P)$/) {
  24. print STDERR "The invoking process wants preprocessed source, so let's hand off this whole command to the real cl.exe\n" if $debug;
  25. Exec("\"$clexe\" \"" . join('" "', @ARGV) . "\"");
  26. } elsif ($arg =~ /^@(.*)$/) {
  27. chomp($commandFile = `cygpath -u '$1'`);
  28. }
  29. }
  30. die "No command file specified!" unless $commandFile;
  31. die "Couldn't find $commandFile!" unless -f $commandFile;
  32. my @sources;
  33. open(COMMAND, '<:raw:encoding(UTF16-LE):crlf:utf8', $commandFile) or die "Couldn't open $commandFile!";
  34. # The first line of the command file contains all the options to cl.exe plus the first (possibly quoted) filename
  35. my $firstLine = <COMMAND>;
  36. $firstLine =~ s/\r?\n$//;
  37. # To find the start of the first filename, look for either the last space on the line.
  38. # If the filename is quoted, the last character on the line will be a quote, so look for the quote before that.
  39. my $firstFileIndex;
  40. print STDERR "Last character of first line = '" . substr($firstLine, -1, 1) . "'\n" if $debug;
  41. if (substr($firstLine, -1, 1) eq '"') {
  42. print STDERR "First file is quoted\n" if $debug;
  43. $firstFileIndex = rindex($firstLine, '"', length($firstLine) - 2);
  44. } else {
  45. print STDERR "First file is NOT quoted\n" if $debug;
  46. $firstFileIndex = rindex($firstLine, ' ') + 1;
  47. }
  48. my $options = substr($firstLine, 0, $firstFileIndex) . join(' ', @ARGV[1 .. $#ARGV]);
  49. my $possibleFirstFile = substr($firstLine, $firstFileIndex);
  50. if ($possibleFirstFile =~ /\.(cpp|c)/) {
  51. push(@sources, $possibleFirstFile);
  52. } else {
  53. $options .= " $possibleFirstFile";
  54. }
  55. print STDERR "######## Found options $options ##########\n" if $debug;
  56. print STDERR "####### Found first source file $sources[0] ########\n" if @sources && $debug;
  57. # The rest of the lines of the command file just contain source files, one per line
  58. while (my $source = <COMMAND>) {
  59. chomp($source);
  60. $source =~ s/^\s+//;
  61. $source =~ s/\s+$//;
  62. push(@sources, $source) if length($source);
  63. }
  64. close(COMMAND);
  65. my $numSources = @sources;
  66. exit unless $numSources > 0;
  67. my $numJobs;
  68. if ($options =~ s/-j\s*([0-9]+)//) {
  69. $numJobs = $1;
  70. } else {
  71. chomp($numJobs = `num-cpus`);
  72. }
  73. print STDERR "\n\n####### COMPILING $numSources FILES USING AT MOST $numJobs PARALLEL INSTANCES OF cl.exe ###########\n\n";# if $debug;
  74. # Magic determination of job size
  75. # The hope is that by splitting the source files up into 2*$numJobs pieces, we
  76. # won't suffer too much if one job finishes much more quickly than another.
  77. # However, we don't want to split it up too much due to cl.exe overhead, so set
  78. # the minimum job size to 5.
  79. my $jobSize = POSIX::ceil($numSources / (2 * $numJobs));
  80. $jobSize = $jobSize < 5 ? 5 : $jobSize;
  81. print STDERR "######## jobSize = $jobSize ##########\n" if $debug;
  82. # Sort the source files randomly so that we don't end up with big clumps of large files (aka SVG)
  83. sub fisher_yates_shuffle(\@)
  84. {
  85. my ($array) = @_;
  86. for (my $i = @{$array}; --$i; ) {
  87. my $j = int(rand($i+1));
  88. next if $i == $j;
  89. @{$array}[$i,$j] = @{$array}[$j,$i];
  90. }
  91. }
  92. fisher_yates_shuffle(@sources); # permutes @array in place
  93. my @children;
  94. my @tmpFiles;
  95. my $status = 0;
  96. while (@sources) {
  97. while (@sources && @children < $numJobs) {
  98. my $pid;
  99. my $tmpFile;
  100. my $job = makeJob(@sources, $jobSize);
  101. ($pid, $tmpFile) = forkAndCompileFiles(@{$job}, $options);
  102. print STDERR "####### Spawned child with PID $pid and tmpFile $tmpFile ##########\n" if $debug;
  103. push(@children, $pid);
  104. push(@tmpFiles, $tmpFile);
  105. }
  106. $status |= waitForChild(@children);
  107. }
  108. while (@children) {
  109. $status |= waitForChild(@children);
  110. }
  111. cleanup(@tmpFiles);
  112. exit WEXITSTATUS($status);
  113. sub makeJob(\@$)
  114. {
  115. my ($files, $jobSize) = @_;
  116. my @job;
  117. if (@{$files} > ($jobSize * 1.5)) {
  118. @job = splice(@{$files}, -$jobSize);
  119. } else {
  120. # Compile all the remaining files in this job to avoid having a small job later
  121. @job = splice(@{$files});
  122. }
  123. return \@job;
  124. }
  125. sub forkAndCompileFiles(\@$)
  126. {
  127. print STDERR "######## forkAndCompileFiles()\n" if $debug;
  128. my ($files, $options) = @_;
  129. if ($debug) {
  130. foreach my $file (@{$files}) {
  131. print STDERR "######## $file\n";
  132. }
  133. }
  134. my (undef, $tmpFile) = File::Temp::tempfile('clcommandXXXXX', DIR => File::Spec->tmpdir, OPEN => 0);
  135. my $pid = fork();
  136. die "Fork failed" unless defined($pid);
  137. unless ($pid) {
  138. # Child process
  139. open(TMP, '>:raw:encoding(UTF16-LE):crlf:utf8', $tmpFile) or die "Couldn't open $tmpFile";
  140. print TMP "$options\n";
  141. foreach my $file (@{$files}) {
  142. print TMP "$file\n";
  143. }
  144. close(TMP);
  145. chomp(my $winTmpFile = `cygpath -m $tmpFile`);
  146. Exec "\"$clexe\" \@\"$winTmpFile\"";
  147. } else {
  148. return ($pid, $tmpFile);
  149. }
  150. }
  151. sub Exec($)
  152. {
  153. my ($command) = @_;
  154. print STDERR "Exec($command)\n" if $debug;
  155. exec($command);
  156. }
  157. sub waitForChild(\@)
  158. {
  159. my ($children) = @_;
  160. return unless @{$children};
  161. my $deceased = wait();
  162. my $status = $?;
  163. print STDERR "######## Child with PID $deceased finished ###########\n" if $debug;
  164. for (my $i = 0; $i < @{$children}; $i++) {
  165. if ($children->[$i] == $deceased) {
  166. splice(@{$children}, $i, 1);
  167. last;
  168. }
  169. }
  170. return $status;
  171. }
  172. sub cleanup(\@)
  173. {
  174. my ($tmpFiles) = @_;
  175. foreach my $file (@{$tmpFiles}) {
  176. unlink $file;
  177. }
  178. }