]>
Commit | Line | Data |
---|---|---|
ddceb393 MG |
1 | package Gruntmaster::Daemon::Format; |
2 | ||
3 | use 5.014000; | |
4 | use strict; | |
5 | use warnings; | |
6 | use parent qw/Exporter/; | |
9577371b | 7 | no if $] > 5.017011, warnings => 'experimental::smartmatch'; |
ddceb393 | 8 | |
cd1a16d1 | 9 | use Digest::SHA qw/sha256_hex/; |
61470035 | 10 | use Expect; |
ddceb393 | 11 | use File::Basename qw/fileparse/; |
cd1a16d1 | 12 | use File::Copy qw/cp/; |
c40a2dd0 | 13 | use File::Slurp qw/read_file write_file/; |
ddceb393 | 14 | use List::MoreUtils qw/natatime/; |
f5e29130 | 15 | use Log::Log4perl qw/get_logger/; |
fdb99417 | 16 | use String::ShellQuote qw/shell_quote/; |
c40a2dd0 | 17 | use Try::Tiny; |
ddceb393 | 18 | |
99d37110 | 19 | our $VERSION = '5999.000_004'; |
fdb99417 | 20 | our @EXPORT_OK = qw/prepare_files stopvms/; |
ddceb393 MG |
21 | |
22 | ################################################## | |
23 | ||
61470035 | 24 | our (%vm, %pid); |
fdb99417 MG |
25 | |
26 | sub runvm { | |
0f817fa6 | 27 | my ($name, $arg) = @_; |
fdb99417 | 28 | return unless $ENV{GRUNTMASTER_VM}; |
0f817fa6 MG |
29 | my $cmd = $ENV{GRUNTMASTER_VM}; |
30 | $cmd .= ' ' . $arg if $arg; | |
31 | get_logger->trace("Starting VM $name ($cmd)"); | |
559c4eec | 32 | $vm{$name} = Expect->new; |
61470035 | 33 | $vm{$name}->raw_pty(1); |
559c4eec MG |
34 | $vm{$name}->log_stdout(0); |
35 | $vm{$name}->spawn($cmd); | |
61470035 | 36 | $vm{$name}->expect(5, '# ') or get_logger->logdie("Error while starting VM $name: ". $vm{$name}->error); |
fdb99417 MG |
37 | } |
38 | ||
61470035 MG |
39 | sub stopvms { |
40 | kill KILL => $_->pid for values %vm; | |
41 | %vm = %pid = (); | |
42 | } | |
43 | ||
44 | sub execlist_finish { | |
45 | my ($vm, $kill) = @_; | |
46 | ||
47 | if ($vm{$vm}) { | |
7e81b08a | 48 | warn "Cannot kill VM\n" if $kill; |
61470035 MG |
49 | $vm{$vm}->expect(5, '# '); |
50 | } else { | |
51 | kill KILL => $pid{$vm} if $kill; | |
52 | waitpid $pid{$vm}, 0; | |
53 | } | |
54 | return if $kill; | |
55 | ||
56 | my $er = "exec-result-$vm"; | |
57 | die "gruntmaster-exec died\n" if -z $er; | |
58 | my ($excode, $exmsg) = read_file $er; | |
59 | unlink $er; | |
60 | chomp ($excode, $exmsg); ## no critic (ProhibitParensWithBuiltins) | |
61 | get_logger->trace("Exec result from $vm: $excode $exmsg"); | |
62 | die [$excode, $exmsg] if $excode; ## no critic (RequireCarping) | |
63 | } | |
fdb99417 | 64 | |
c40a2dd0 | 65 | sub execlist { |
fdb99417 MG |
66 | my ($vm, @args) = @_; |
67 | my $er = "exec-result-$vm"; | |
68 | if ($vm{$vm}) { | |
69 | my $cmd = ">$er " . shell_quote 'gruntmaster-exec', @args; | |
70 | get_logger->trace("Running in VM $vm: $cmd"); | |
559c4eec | 71 | $vm{$vm}->send($cmd, "\n"); |
c40a2dd0 | 72 | } else { |
61470035 MG |
73 | $pid{$vm} = fork // die "Cannot fork\n"; |
74 | unless ($pid{$vm}) { | |
99d37110 | 75 | open STDOUT, '>', $er or die "Cannot open $er\n"; |
e61f1a82 | 76 | get_logger->trace("Running: gruntmaster-exec @args"); |
fdb99417 MG |
77 | exec 'gruntmaster-exec', @args; |
78 | } | |
c40a2dd0 MG |
79 | } |
80 | } | |
81 | ||
ddceb393 MG |
82 | sub mkrun{ |
83 | my $format = shift; | |
84 | sub{ | |
d16a999c | 85 | local *__ANON__ = 'mkrun_runner'; |
ddceb393 | 86 | my ($name, %args) = @_; |
d16a999c | 87 | get_logger->trace("Running $name..."); |
99d37110 | 88 | my $basename = fileparse $name, qr/[.][^.]*/s; |
42ce8ba9 | 89 | my @args = ('--sudo'); |
13b1661a | 90 | push @args, '--keep-stderr' if $ENV{TEST_VERBOSE}; |
c40a2dd0 MG |
91 | push @args, '--timeout', $args{timeout} if $args{timeout}; |
92 | push @args, '--mlimit', $args{mlimit} if $args{mlimit}; | |
93 | push @args, '--olimit', $args{olimit} if $args{olimit}; | |
94 | my @fds = exists $args{fds} ? @{$args{fds}} : (); | |
95 | my $it = natatime 2, @fds; | |
96 | while (my ($fd, $file) = $it->()) { | |
97 | push @args, "--fd=$fd $file"; | |
ddceb393 | 98 | } |
61470035 MG |
99 | execlist $basename, @args, '--', "./$basename", @{$args{args}}; |
100 | execlist_finish $basename unless $args{nonblocking} | |
ddceb393 MG |
101 | } |
102 | } | |
103 | ||
ad77b7d3 MG |
104 | sub prepare{ |
105 | my ($name, $format) = @_; | |
ad77b7d3 MG |
106 | get_logger->trace("Preparing file $name..."); |
107 | ||
c40a2dd0 | 108 | try { |
fdb99417 | 109 | execlist prog => '--fd=1 >>errors', '--fd=2 >>errors', 'gruntmaster-compile', $format, $name; |
61470035 | 110 | execlist_finish 'prog'; |
c40a2dd0 | 111 | } catch { |
36ecc8ea MG |
112 | my $exmsg = $_->[1]; |
113 | die "Compile error ($exmsg)\n" | |
c40a2dd0 MG |
114 | } finally { |
115 | $Gruntmaster::Daemon::errors .= read_file 'errors'; | |
837911b3 | 116 | $Gruntmaster::Daemon::errors .= "\n" if -s 'errors'; |
c40a2dd0 MG |
117 | unlink 'errors'; |
118 | }; | |
ad77b7d3 MG |
119 | } |
120 | ||
121 | sub prepare_files{ | |
122 | my $meta = shift; | |
0f817fa6 MG |
123 | if ($meta->{runner} eq 'Interactive') { |
124 | runvm ver => '-serial unix:vm.sock,nowait,server'; | |
125 | runvm prog => '-serial unix:vm.sock,nowait'; | |
126 | } else { | |
127 | runvm $_ for keys %{$meta->{files}}; | |
128 | } | |
ad77b7d3 | 129 | |
759d3ca5 | 130 | for my $file (values %{$meta->{files}}) { |
ad77b7d3 MG |
131 | my ($format, $name, $content) = @{$file}{qw/format name content/}; |
132 | ||
133 | $file->{run} = mkrun($format); | |
134 | write_file $name, $content; | |
cd1a16d1 MG |
135 | if ($ENV{GRUNTMASTER_CCACHE}) { |
136 | my $key = lc sha256_hex($content) . '-' . $format; | |
137 | my $cachefn = "$ENV{GRUNTMASTER_CCACHE}/$key"; | |
138 | my $exefn = fileparse $name, qr/[.][^.]*/s; | |
139 | if (cp $cachefn, $exefn) { | |
140 | get_logger->trace("File $name found in compilation cache") | |
141 | } else { | |
142 | prepare $name, $format; | |
143 | cp $exefn, $cachefn | |
144 | } | |
145 | } else { | |
146 | prepare $name, $format | |
147 | } | |
ad77b7d3 MG |
148 | } |
149 | } | |
150 | ||
bc372959 MG |
151 | 1; |
152 | __END__ | |
153 | ||
154 | =encoding utf-8 | |
155 | ||
156 | =head1 NAME | |
157 | ||
158 | Gruntmaster::Daemon::Format - Utility functions for handling source files | |
159 | ||
160 | =head1 SYNOPSIS | |
161 | ||
162 | use Gruntmaster::Daemon::Format qw/prepare_files/; | |
163 | prepare_files { files => { | |
164 | prog => { | |
165 | name => 'prog.pl', | |
166 | format => 'PERL', | |
167 | content => 'print "Hello, world!"' | |
168 | }, | |
169 | ver => { | |
170 | name => 'ver.cpp', | |
171 | format => 'CPP', | |
172 | content => ... | |
173 | }, | |
174 | }}; | |
175 | ||
176 | =head1 DESCRIPTION | |
177 | ||
178 | Gruntmaster::Daemon::Format exports utility functions for handling source files. | |
179 | ||
180 | =over | |
181 | ||
182 | =item B<prepare_files> I<$meta> | |
183 | ||
184 | Compiles all the source files in C<< $meta->{files} >>. | |
185 | ||
186 | =back | |
187 | ||
188 | =head1 AUTHOR | |
189 | ||
190 | Marius Gavrilescu E<lt>marius@ieval.roE<gt> | |
191 | ||
192 | =head1 COPYRIGHT AND LICENSE | |
193 | ||
194 | Copyright (C) 2014 by Marius Gavrilescu | |
195 | ||
196 | This library is free software: you can redistribute it and/or modify | |
197 | it under the terms of the GNU Affero General Public License as published by | |
198 | the Free Software Foundation, either version 3 of the License, or | |
199 | (at your option) any later version. | |
200 | ||
201 | ||
202 | =cut |