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