12 sub forkAndCompileFiles
(\@
$);
19 chomp(my $clexe = `cygpath -u '$ENV{'VS80COMNTOOLS'}/../../VC/bin/cl.exe'`);
22 print STDERR
"Received " . @ARGV . " arguments:\n";
23 foreach my $arg (@ARGV) {
24 print STDERR
"$arg\n";
29 foreach my $arg (@ARGV) {
30 if ($arg =~ /^[\/-](E
|EP
|P
)$/) {
31 print STDERR
"The invoking process wants preprocessed source, so let's hand off this whole command to the real cl.exe\n" if $debug;
32 Exec
("\"$clexe\" \"" . join('" "', @ARGV) . "\"");
33 } elsif ($arg =~ /^@(.*)$/) {
34 chomp($commandFile = `cygpath -u '$1'`);
38 die "No command file specified!" unless $commandFile;
39 die "Couldn't find $commandFile!" unless -f
$commandFile;
43 open(COMMAND
, '<:raw:encoding(UTF16-LE):crlf:utf8', $commandFile) or die "Couldn't open $commandFile!";
45 # The first line of the command file contains all the options to cl.exe plus the first (possibly quoted) filename
46 my $firstLine = <COMMAND
>;
47 $firstLine =~ s/\r?\n$//;
49 # To find the start of the first filename, look for either the last space on the line.
50 # If the filename is quoted, the last character on the line will be a quote, so look for the quote before that.
52 print STDERR
"Last character of first line = '" . substr($firstLine, -1, 1) . "'\n" if $debug;
53 if (substr($firstLine, -1, 1) eq '"') {
54 print STDERR
"First file is quoted\n" if $debug;
55 $firstFileIndex = rindex($firstLine, '"', length($firstLine) - 2);
57 print STDERR
"First file is NOT quoted\n" if $debug;
58 $firstFileIndex = rindex($firstLine, ' ') + 1;
61 my $options = substr($firstLine, 0, $firstFileIndex) . join(' ', @ARGV[1 .. $#ARGV]);
62 my $possibleFirstFile = substr($firstLine, $firstFileIndex);
63 if ($possibleFirstFile =~ /\.(cpp|c)/) {
64 push(@sources, $possibleFirstFile);
66 $options .= " $possibleFirstFile";
69 print STDERR
"######## Found options $options ##########\n" if $debug;
70 print STDERR
"####### Found first source file $sources[0] ########\n" if @sources && $debug;
72 # The rest of the lines of the command file just contain source files, one per line
73 while (my $source = <COMMAND
>) {
77 push(@sources, $source) if length($source);
81 my $numSources = @sources;
82 exit unless $numSources > 0;
85 if ($options =~ s/-j\s*([0-9]+)//) {
88 chomp($numJobs = `num-cpus`);
91 print STDERR
"\n\n####### RUNNING AT MOST $numJobs PARALLEL INSTANCES OF cl.exe ###########\n\n";# if $debug;
93 # Magic determination of job size
94 # The hope is that by splitting the source files up into 2*$numJobs pieces, we
95 # won't suffer too much if one job finishes much more quickly than another.
96 # However, we don't want to split it up too much due to cl.exe overhead, so set
97 # the minimum job size to 5.
98 my $jobSize = POSIX
::ceil
($numSources / (2 * $numJobs));
99 $jobSize = $jobSize < 5 ?
5 : $jobSize;
101 print STDERR
"######## jobSize = $jobSize ##########\n" if $debug;
103 # Sort the source files randomly so that we don't end up with big clumps of large files (aka SVG)
104 sub fisher_yates_shuffle
(\@
)
107 for (my $i = @
{$array}; --$i; ) {
108 my $j = int(rand($i+1));
110 @
{$array}[$i,$j] = @
{$array}[$j,$i];
114 fisher_yates_shuffle
(@sources); # permutes @array in place
120 while (@sources && @children < $numJobs) {
123 my $job = makeJob
(@sources, $jobSize);
124 ($pid, $tmpFile) = forkAndCompileFiles
(@
{$job}, $options);
126 print STDERR
"####### Spawned child with PID $pid and tmpFile $tmpFile ##########\n" if $debug;
127 push(@children, $pid);
128 push(@tmpFiles, $tmpFile);
131 $status |= waitForChild
(@children);
135 $status |= waitForChild
(@children);
139 exit WEXITSTATUS
($status);
144 my ($files, $jobSize) = @_;
147 if (@
{$files} > ($jobSize * 1.5)) {
148 @job = splice(@
{$files}, -$jobSize);
150 # Compile all the remaining files in this job to avoid having a small job later
151 @job = splice(@
{$files});
157 sub forkAndCompileFiles
(\@
$)
159 print STDERR
"######## forkAndCompileFiles()\n" if $debug;
160 my ($files, $options) = @_;
163 foreach my $file (@
{$files}) {
164 print STDERR
"######## $file\n";
168 my (undef, $tmpFile) = File
::Temp
::tempfile
('clcommandXXXXX', DIR
=> File
::Spec
->tmpdir, OPEN
=> 0);
171 die "Fork failed" unless defined($pid);
175 open(TMP
, '>:raw:encoding(UTF16-LE):crlf:utf8', $tmpFile) or die "Couldn't open $tmpFile";
176 print TMP
"$options\n";
177 foreach my $file (@
{$files}) {
182 chomp(my $winTmpFile = `cygpath -m $tmpFile`);
183 Exec
"\"$clexe\" \@\"$winTmpFile\"";
185 return ($pid, $tmpFile);
193 print STDERR
"Exec($command)\n" if $debug;
202 return unless @
{$children};
204 my $deceased = wait();
206 print STDERR
"######## Child with PID $deceased finished ###########\n" if $debug;
207 for (my $i = 0; $i < @
{$children}; $i++) {
208 if ($children->[$i] == $deceased) {
209 splice(@
{$children}, $i, 1);
221 foreach my $file (@
{$tmpFiles}) {