1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
|
#!/usr/bin/perl -Tw
#-
# Copyright (c) 2003 Dag-Erling Coïdan Smørgrav
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer
# in this position and unchanged.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. The name of the author may not be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# $FreeBSD$
#
use strict;
use Fcntl qw(:DEFAULT :flock);
use POSIX;
use Getopt::Long;
my $VERSION = "2.0";
my $COPYRIGHT = "Copyright (c) 2003 Dag-Erling Smørgrav. " .
"All rights reserved.";
my $arch; # Target architecture
my $branch; # CVS branch to checkou
my $clean; # Clean before building
my $date; # Date of sources to check out
my $jobs; # Number of paralell jobs
my $logfile; # Path to log file
my $machine; # Target machine
my $repository; # Location of CVS repository
my $sandbox; # Location of sandbox
my $update; # Update sources before building
my $verbose; # Verbose mode
my %cmds = (
'world' => 0,
'generic' => 0,
'lint' => 0
);
sub message(@) {
my $msg = join(' ', "TB ---", @_);
chomp($msg);
print("$msg\n");
}
sub warning(@) {
my $msg = join(' ', @_);
chomp($msg);
warn("$msg\n");
return undef;
}
sub error(@) {
my $msg = join(' ', "ERROR:", @_);
chomp($msg);
die("$msg\n");
return undef;
}
#
# Open and lock a file reliably
#
sub open_locked($;$$) {
my $fn = shift; # File name
my $flags = shift; # Open flags
my $mode = shift; # File mode
local *FILE; # File handle
my (@sb1, @sb2); # File status
for (;; close(FILE)) {
sysopen(FILE, $fn, $flags || O_RDONLY, $mode || 0640)
or last;
if (!(@sb1 = stat(FILE))) {
# Huh? shouldn't happen
warning("$fn: stat(): $!");
last;
}
if (!flock(FILE, LOCK_EX|LOCK_NB)) {
# A failure here means the file can't be locked, or
# something really weird happened, so just give up.
warning("$fn: flock(): $!");
last;
}
if (!(@sb2 = stat($fn))) {
# File was pulled from under our feet, though it may
# reappear in the next pass
next;
}
if ($sb1[0] != $sb2[0] || $sb1[1] != $sb2[1]) {
# File changed under our feet, try again
next;
}
return *FILE{IO};
}
close(FILE);
return undef;
}
#
# Remove a directory and all its subdirectories
#
sub remove_dir($);
sub remove_dir($) {
my $dir = shift;
if (!-d $dir) {
message("removing $dir")
if ($verbose);
return (unlink($dir) || $! == ENOENT);
}
local *DIR;
opendir(DIR, $dir)
or return warning("$dir: $!");
foreach my $ent (readdir(DIR)) {
next if ($ent eq '.' || $ent eq '..');
$ent =~ m/(.*)/;
if (!remove_dir("$dir/$1")) {
closedir(DIR);
return undef;
}
}
closedir(DIR)
or return warning("$dir: $!");
message("rmdir $dir")
if ($verbose);
return rmdir($dir);
}
sub make_dir($);
sub make_dir($) {
my $dir = shift;
if (!-d $dir && $dir =~ m|^(\S*)/([^\s/]+)$|) {
make_dir($1)
or return undef;
message("mkdir $dir");
mkdir("$dir")
or return undef;
}
return 1;
}
sub cd($) {
my $dir = shift;
message("cd $dir");
chdir($dir)
or error("$dir: $!");
}
#
# Spawn a child and wait for it to finish
#
sub spawn($@) {
my $cmd = shift; # Command to run
my @args = @_; # Arguments
message($cmd, @args);
my $pid = fork();
if (!defined($pid)) {
return warning("fork(): $!");
} elsif ($pid == 0) {
exec($cmd, @args);
die("child: exec(): $!\n");
}
if (waitpid($pid, 0) == -1) {
return warning("waitpid(): $!\n");
} elsif ($? & 0xff) {
return warning("$cmd caught signal ", $? & 0x7f, "\n");
} elsif ($? >> 8) {
return warning("$cmd returned exit code ", $? >> 8, "\n");
}
return 1;
}
sub make($) {
my $target = shift;
return spawn('/usr/bin/make',
($jobs > 1) ? "-j$jobs" : "-B",
$target);
}
sub logstage($) {
my $msg = shift;
chomp($msg);
print(STDERR strftime("TB --- %Y-%m-%d %H:%M:%S - $msg\n", localtime()));
}
sub sigwarn {
logstage(shift);
}
sub sigdie {
logstage(shift);
logstage("tinderbox aborted");
exit(1);
}
sub usage() {
print(STDERR "This is the FreeBSD tinderbox script, version $VERSION.
$COPYRIGHT
Usage:
$0 [options] [parameters] command [...]
Options:
-c, --clean Clean sandbox before building
-u, --update Update sources before building
-v, --verbose Verbose mode
Parameters:
-a, --arch=ARCH Target architecture
-b, --branch=BRANCH CVS branch to check out
-d, --date=DATE Date of sources to check out
-j, --jobs=NUM Maximum number of paralell jobs
-l, --logfile=FILE Path to log file
-r, --repository=DIR Location of CVS repository
-s, --sandbox=DIR Location of sandbox
Commands:
world Build the world
generic Build the GENERIC kernel
lint Build the LINT kernel
Report bugs to <des\@freebsd.org>.
");
exit(1);
}
MAIN:{
$ENV{'PATH'} = '';
$ENV{'TZ'} = "GMT";
tzset();
# Set defaults
$arch = `/usr/bin/uname -m`;
chomp($arch);
$branch = "CURRENT";
$jobs = 0;
$repository = "/home/ncvs";
$sandbox = "$ENV{'HOME'}/tinderbox";
# Get options
{Getopt::Long::Configure("auto_abbrev", "bundling");}
GetOptions(
"a|arch=s" => \$arch,
"b|branch=s" => \$branch,
"c|clean" => \$clean,
"d|date=s" => \$date,
"j|jobs=i" => \$jobs,
"l|logfile=s" => \$logfile,
"m|machine=s" => \$machine,
"r|repository=s" => \$repository,
"s|sandbox=s" => \$sandbox,
"u|update" => \$update,
"v|verbose" => \$verbose,
) or usage();
if ($jobs < 0) {
error("invalid number of jobs");
}
if ($branch !~ m|^(\w+)$|) {
error("invalid source branch");
}
$branch = $1;
if ($arch !~ m|^(\w+)$|) {
error("invalid target architecture");
}
$arch = $1;
if (!defined($machine)) {
$machine = $arch;
}
if ($machine !~ m|^(\w+)$|) {
error("invalid target machine");
}
$machine = $1;
if (!@ARGV) {
usage();
}
# Find out what we're expected to do
foreach my $cmd (@ARGV) {
if (!exists($cmds{$cmd})) {
error("unrecognized command: '$cmd'");
}
$cmds{$cmd} = 1;
}
# Take control of our sandbox
if ($sandbox !~ m|^(/[\w/-]+)$|) {
error("invalid sandbox directory");
}
$sandbox = "$1/$branch/$arch/$machine";
make_dir($sandbox)
or error("$sandbox: $!");
my $lockfile = open_locked("$sandbox/lock", O_RDWR|O_CREAT);
if (!defined($lockfile)) {
error("unable to lock sandbox");
}
truncate($lockfile, 0);
print($lockfile "$$\n");
# Open logfile
open(STDIN, '<', "/dev/null")
or error("/dev/null: $!\n");
if (defined($logfile)) {
if ($logfile !~ m|([\w./-]+)$|) {
error("invalid log file name");
}
$logfile = $1;
unlink($logfile);
open(STDOUT, '>', $logfile)
or error("$logfile: $!");
}
open(STDERR, ">&STDOUT");
$| = 1;
logstage("starting $branch tinderbox run for $arch/$machine");
$SIG{__DIE__} = \&sigdie;
$SIG{__WARN__} = \&sigwarn;
# Clean up remains from old runs
if ($clean) {
logstage("cleaning up sandbox");
remove_dir("$sandbox/src")
or error("unable to remove old source directory");
remove_dir("$sandbox/obj")
or error("unable to remove old object directory");
make_dir("$sandbox/obj")
or error("$sandbox/obj: $!");
}
# Check out new source tree
if ($update) {
cd("$sandbox");
logstage("checking out sources");
my @cvsargs = (
"-f",
"-R",
$verbose ? "-q" : "-Q",
"-d$repository",
);
if (-d "$sandbox/src") {
push(@cvsargs, "update", "-Pd");
} else {
push(@cvsargs, "checkout", "-P");
};
push(@cvsargs, ($branch eq 'CURRENT') ? "-A" : "-r$branch")
if defined($branch);
push(@cvsargs, "-D$date")
if defined($date);
push(@cvsargs, "src");
spawn('/usr/bin/cvs', @cvsargs)
or error("unable to check out the source tree");
}
# Prepare environment for make(1);
cd("$sandbox/src");
%ENV = (
'TZ' => "GMT",
'PATH' => "/usr/bin:/usr/sbin:/bin:/sbin",
'__MAKE_CONF' => "/dev/null",
'MAKEOBJDIRPREFIX' => "$sandbox/obj",
'TARGET_ARCH' => $arch,
'TARGET_MACHINE' => $machine,
'CFLAGS' => "-O -pipe",
'NO_CPU_CFLAGS' => "YES",
'COPTFLAGS' => "-O -pipe",
'NO_CPU_COPTFLAGS' => "YES",
'MAKE_KERBEROS4' => "YES",
'MAKE_KERBEROS5' => "YES",
);
# Build the world
if ($cmds{'world'}) {
logstage("building world");
make('buildworld')
or error("failed to build world");
}
# Build GENERIC if requested
if ($cmds{'generic'}) {
logstage("building generic kernel");
spawn('/usr/bin/make', 'buildkernel', 'KERNCONF=GENERIC')
or error("failed to build generic kernel");
}
# Build LINT if requested
if ($cmds{'lint'} && ! -f "$sandbox/src/sys/$machine/conf/NOTES") {
logstage("no NOTES, skipping lint kernel");
$cmds{'lint'} = 0;
}
if ($cmds{'lint'}) {
logstage("building lint kernel");
cd("$sandbox/src/sys/$machine/conf");
make('LINT')
or error("failed to generate lint config");
cd("$sandbox/src");
spawn('/usr/bin/make', 'buildkernel', 'KERNCONF=LINT')
or error("failed to build lint kernel");
}
# Exiting releases the lock file
logstage("tinderbox run completed");
exit(0);
}
|