Debian Codegraph @

cassandane/Cassandane/Cyrus/LibCyrus.pm

raw
1 #!/usr/bin/perl
2 #
3 # Copyright (c) 2011-2024 FastMail Pty Ltd. All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
7 # are met:
8 #
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 #
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in
14 # the documentation and/or other materials provided with the
15 # distribution.
16 #
17 # 3. The name "Fastmail Pty Ltd" must not be used to
18 # endorse or promote products derived from this software without
19 # prior written permission. For permission or any legal
20 # details, please contact
21 # FastMail Pty Ltd
22 # PO Box 234
23 # Collins St West 8007
24 # Victoria
25 # Australia
26 #
27 # 4. Redistributions of any form whatsoever must retain the following
28 # acknowledgment:
29 # "This product includes software developed by Fastmail Pty. Ltd."
30 #
31 # FASTMAIL PTY LTD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
32 # INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
33 # EVENT SHALL OPERA SOFTWARE AUSTRALIA BE LIABLE FOR ANY SPECIAL, INDIRECT
34 # OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
35 # USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
36 # TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
37 # OF THIS SOFTWARE.
38 #
39
40 package Cassandane::Cyrus::LibCyrus;
41 use strict;
42 use warnings;
43 use v5.010;
44 use Cwd qw(abs_path);
45 use Data::Dumper;
46 use DateTime;
47 use File::Copy;
48 use File::Find;
49
50 use lib '.';
51 use base qw(Cassandane::Cyrus::TestCase);
52 use Cassandane::Util::Log;
53 use Cassandane::Util::Slurp;
54
55 $Data::Dumper::Sortkeys = 1;
56
57 my $examples_dir = abs_path('../doc/examples/libcyrus');
58
59 sub new
60 {
61 my $class = shift;
62
63 my $self = $class->SUPER::new({}, @_);
64
65 my $cassini = Cassandane::Cassini->instance();
66 my $rootdir = $cassini->val('cassandane', 'rootdir', '/var/tmp/cass');
67 my $findmnt = $cassini->val('paths', 'findmnt', '/usr/bin/findmnt');
68 my $rootdir_mount_opts = qx{$findmnt -n -o OPTIONS --target $rootdir};
69
70 $self->{rootdir_is_noexec} = (defined $rootdir_mount_opts
71 && $rootdir_mount_opts =~ m/\bnoexec\b/);
72
73 return $self;
74 }
75
76 sub set_up
77 {
78 my ($self) = @_;
79 $self->SUPER::set_up();
80 }
81
82 sub tear_down
83 {
84 my ($self) = @_;
85 $self->SUPER::tear_down();
86 }
87
88 sub run_pkgconfig
89 {
90 my ($self, %params) = @_;
91 state $counter = 0;
92
93 $counter++;
94
95 my $outfname = $self->{instance}->{basedir} . "/pkg-config$counter.out";
96 my $errfname = $self->{instance}->{basedir} . "/pkg-config$counter.err";
97
98 my $cassini = Cassandane::Cassini->instance();
99 my $pkgconfig = $cassini->val('paths', 'pkg-config', '/usr/bin/pkg-config');
100
101 $self->{instance}->run_command({
102 cyrus => 0,
103 redirects => {
104 stdout => $outfname,
105 stderr => $errfname,
106 },
107 },
108 $pkgconfig, @{$params{options}}, @{$params{packages}},
109 );
110
111 my $val = slurp_file($outfname);
112 chomp $val;
113
114 return $val;
115 }
116
117 sub find_tests
118 {
119 my ($dir) = @_;
120
121 my @tests;
122
123 find(
124 sub {
125 my $file = $File::Find::name;
126
127 return unless $file =~ s/\.c$//;
128 return unless -f "$file.c";
129 $file =~ s/^$dir\/?//;
130 push @tests, "test_$file";
131 },
132 $dir,
133 );
134
135 return @tests;
136 }
137
138 sub list_tests
139 {
140 my @tests;
141
142 @tests = find_tests($examples_dir);
143
144 return @tests;
145 }
146
147 sub find_deps
148 {
149 my ($source) = @_;
150 my @deps;
151
152 open my $fh, '<', $source or die "read $source: $!";
153 while (<$fh>) {
154 if (m/\bDEPS:([-\sA-Za-z_]+)/) {
155 if (get_verbose() > 1) {
156 xlog "found DEPS at line $.: $_";
157 }
158 push @deps, split(q{ }, $1);
159 }
160 }
161 close $fh;
162
163 if (get_verbose() > 1) {
164 xlog "parsed dependencies: " . Dumper \@deps;
165 }
166 return @deps;
167 }
168
169 sub run_test
170 {
171 my ($self) = @_;
172
173 my $cassini = Cassandane::Cassini->instance();
174
175 my $name = $self->name();
176 $name =~ s/^test_//;
177
178 my $orig_source = "$examples_dir/$name.c";
179 copy($orig_source, $self->{instance}->{basedir})
180 or die "copy $orig_source: $!";
181
182 my @deps = find_deps($orig_source)
183 or die "couldn't determine libcyrus dependencies for test_$name";
184
185 my $cflags = $self->run_pkgconfig(packages => \@deps,
186 options => [ '--cflags' ]);
187 my $ldflags = $self->run_pkgconfig(packages => \@deps,
188 options => [ '--libs-only-L',
189 '--libs-only-other' ]);
190 my $ldlibs = $self->run_pkgconfig(packages => \@deps,
191 options => [ '--libs-only-l' ]);
192
193 my $makeerr = $self->{instance}->{basedir} . "/make.err";
194 my $make = $cassini->val('paths', 'make', '/usr/bin/make');
195
196 my $warnopts = '-Wall -Wextra -Werror';
197 my $otheropts = '-g -O0 -fdiagnostics-color=always';
198
199 eval {
200 $self->{instance}->run_command({
201 cyrus => 0,
202 redirects => {
203 stderr => $makeerr,
204 },
205 },
206 $make,
207 "CFLAGS=$warnopts $otheropts $cflags",
208 "LDFLAGS=$ldflags",
209 "LDLIBS=$ldlibs",
210 '-C', $self->{instance}->{basedir}, # n.b. not cyrus's -C
211 $name,
212 );
213 };
214 if ($@) {
215 xlog "make failed:\n" . slurp_file($makeerr);
216 die $@;
217 }
218
219 my $exe = $self->{instance}->{basedir} . "/$name";
220
221 if ($self->{rootdir_is_noexec} || ! -x $exe) {
222 xlog $self, "$exe is not executable, won't try to run it";
223 }
224 else {
225 my $runerr = $self->{instance}->{basedir} . "/$name.err";
226 my @cmd;
227
228 if ($cassini->bool_val('valgrind', 'enabled')) {
229 push @cmd, $self->{instance}->_valgrind_setup($name);
230 }
231
232 push @cmd, $exe, '-C', $self->{instance}->_imapd_conf();
233
234 eval {
235 $self->{instance}->run_command({
236 cyrus => 0,
237 redirects => {
238 stderr => $runerr,
239 },
240 },
241 @cmd,
242 );
243 };
244 if ($@) {
245 xlog "$name failed:\n" . slurp_file($runerr);
246 die $@;
247 }
248 }
249 }
250
251 1;