aboutsummaryrefslogtreecommitdiff
path: root/validate-manifest.pl
blob: 1315f3e18b3aa9ce47c68cea025a07a0cc596712 (plain)
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
#!/usr/bin/perl

use Digest::MD5 qw(md5_hex);
use Digest::SHA qw(sha1_hex);
use warnings;
use strict;

my ($opt_online, $opt_quiet, $opt_release, $opt_output_normalized);
my ($opt_verbose);
my (@file_args);

# default to online checking of md5sums in .asc files, but don't download
# full tarballs to check
$opt_online = 1;

while (@ARGV)
{
    my $opt = $ARGV[0];
    if ($opt eq "--online") { shift; $opt_online = 1; next; }
    if ($opt eq "--online-full") { shift; $opt_online = 2; next; }
    if ($opt =~ /^--no[_-]?online$/) { shift; $opt_online = 0; next; }
    if ($opt eq "--quiet") { shift; $opt_quiet = 1; next; }
    if ($opt eq "--verbose") { shift; $opt_verbose = 1; next; }
    if ($opt eq "--release") { shift; $opt_release = 1; next; }
    if ($opt =~ /^--output-normalized(=(.*))?$/) { shift; $opt_output_normalized = $2 // shift; next; }
    if ($opt =~ /^--/) { die "Unrecognised option $opt"; }
    # Other arguments are manifest files for validation.
    push @file_args, shift;
}

die "Syntax..." unless $#file_args > -1;

if (defined $opt_output_normalized && $#file_args  != 0)
{
   die "Must have exactly one input file when using --output-normalized"; 
}

sub parse_manifest
{
    my ($mfile) = @_;
    my $res;

    open MFILE,"<$mfile" or die "Could not open $mfile: $!";

    # parser states...
    #   0: at first line, expecting manifest_format=
    #   1: base state, not in component info stanza
    #   2: waiting for more component info
    #   3: in the developer info section
    my $state = 0;
    ### $line == $res->{ctx}{line};
    $res->{ctx}{line_no} = 0;
    $res->{ctx}{errors} = 0;
    $res->{ctx}{filename} = $mfile;
    my $cur_component;
    my $line;
    while ($line = <MFILE>)
    {
        chomp $line;
        $res->{ctx}{line} = $line; # used for error reporting
        $res->{ctx}{line_no}++;

        if ($state == 0)
        {
            if ($line=~/^manifest_format=(.*)$/)
            {
                $res->{mani_ver} = $1;
            } else {
                # if we don't know the manifest version, then default to 1.1
                $res->{mani_ver} = "1.1";
                error ($res, "manifest version line not present")
            }
            $state=1;
            next;
        }
        if ($state == 1 && $line=~/^# Note that for ABE, these parameters are not used/)
        {
            # technically, this line appears exactly once at line 3, but we just
            # ignore this line any time it appears in state 1;
            next;
        }
        if ($state == 1 && $line=~/^([a-z]+)=(.*)/)
        {
            if (defined $res->{flag_info}{$1})
            {
                error ($res, "duplicate flag")
            }
            $res->{flag_info}{$1} = $2;
            next;
        }
        if ($state == 2 && $line=~/^([a-z]+)_([0-9a-z_]+)=(.*)/)
        {
            if (defined $res->{component_info}{$1}{$2})
            {
                error ($res, "duplicate definition")
            }
            if ($1 ne $cur_component)
            {
                error ($res, "found component info for wrong component, expected $cur_component")
            }
            $res->{component_info}{$1}{$2} = $3;
            next;
        }
        if ($state == 1 && $line=~/^ #*$/)
        {
            # OK
            next;
        }
        if ($state == 1 && $line=~/^ # Everything below this line is only for informational purposes for developers$/)
        {
            $state = 3;
            next;
        }
        if ($state <= 2 && $line=~/^$/)
        {
            # empty line marks end of component info section
            undef $cur_component;
            $state = 1;
            next;
        }
        if ($state == 1 && $line=~/^# Component data for (.*)/)
        {
            # expect a comment which introduces the component info
            $cur_component = "$1";
            $state = 2;
            next;
        }

        if ($state == 3 && $line=~/^([a-z]+)_([a-z]+)=(.*)/)
        {
            if (defined $res->{component_info}{$1})
            {
                error ($res, "duplicate component info in developer info section")
            }
            next;
        }
        if ($state == 1 || $state == 2)
        {
            error ($res, "unexpected line");
            next;
        }
    }

    # parsing has finished, so we don't want error() to report line
    # numbers.
    undef $res->{ctx}{line};
    undef $res->{ctx}{line_no};

    if ($state != 3)
    {
        error($res, "didn't find developer info section");
    }
    close MFILE;
    return $res;
}

my $exit_status = 0;

foreach my $file (@file_args)
{
    my $manifest = parse_manifest($file);
    die ("manifest parsing of $file failed") unless defined $manifest;

    check_manifest_version($manifest);
    check_components_list($manifest);
    check_all_flags($manifest);
    check_all_components($manifest);
    check_url_locations($manifest) if $opt_release;
    check_gdbserver($manifest);
    check_manifestid($manifest);
    check_online($manifest) if $opt_online;
    output_normalized($manifest, $opt_output_normalized) if $opt_output_normalized;

    # indicate success in exit code.
    if ($manifest->{ctx}{errors} > 0)
    {
        $exit_status = 1;
    } else {
        my $fn = $manifest->{ctx}{filename};
        print STDERR "PASS: $fn\n" if $opt_verbose;
    }
}

exit $exit_status;

sub error
{
    my ($m, $msg) = @_;
    my $extra = "";
    if (defined $m->{ctx}{line})
    {
        my $in = $m->{ctx}{line};
        my $line_no = $m->{ctx}{line_no};
        $extra= "  at line $line_no: $in\n";
    }
    my $fn = $m->{ctx}{filename};
    $m->{ctx}{errors}++;
    print STDERR "ERROR: $fn: $msg\n$extra" if !$opt_quiet;
}

sub check_all_components
{
    my ($m) = @_;
    foreach my $component (keys %{$m->{component_info}})
    {
        check_component ($m, $component)
    }
}


sub check_url_locations
{
    my ($m) = @_;
    my @valid_urls=(
    "http://snapshots.linaro.org/components/toolchain/infrastructure",
    "http://git.linaro.org/git/toolchain",
    "http://releases.linaro.org/components/toolchain"
    );
    foreach my $component (keys %{$m->{component_info}})
    {
        my $url = $m->{component_info}{$component}{url};
        next unless defined $url;
        my $valid = 0;
        foreach my $url_prefix (@valid_urls)
        {
            my $re = quotemeta($url_prefix);
            $valid = 1 if $url=~/^$re\b/;
        }
        error($m, "Component $component has unexpected url: $url") unless $valid;
    }
}

sub fields_for_component
{
    my ($m, $component) = @_;
    # hash keyed by field name, value is 0 for optional, 1 for required.
    my %fields = ();
    $fields{url} = 1;
    $fields{filespec} = 1;
    $fields{makeflags} = 0;
    if ($component !~ /^(linux|dejagnu|expat|python)$/)
    {
        $fields{configure} = 1;
    }
    if ($component !~ /^e?glibc$/)
    {
        $fields{staticlink} = 1;
    }
    if ($component eq "gcc")
    {
        $fields{stage1_flags} = 1;
        $fields{stage2_flags} = 1;
    }
    my $filespec = $m->{component_info}{$component}{filespec};
    if (!defined $filespec || $filespec=~/\.tar\..z$/)
    {
        # md5sum field was introduced in version 1.2
        $fields{md5sum} = 1 if version_ge($m, "1.2");
    } else {
        $fields{revision} = 1;
        $fields{branch} = 1;
    }
    $fields{mingw_only} = 1 if version_ge($m, "1.4");
    $fields{mingw_extraconf} = 0 if version_ge($m, "1.4");
    return %fields;
}

sub check_component
{
    my ($m, $component) = @_;
    my %fields = fields_for_component($m, $component);
    for my $field (keys %{$m->{component_info}{$component}})
    {
        if (!defined $fields{$field})
        {
            error($m, "unexpected parameter ${component}_${field}")
        }
    }
    for my $field (keys %fields)
    {
        if ($fields{$field} && !defined $m->{component_info}{$component}{$field})
        {
            error($m, "missing parameter ${component}_${field}")
        }
    }
    if ($component eq "gcc" && $m->{component_info}{$component}{configure} ne "")
    {
        error($m, "parameter ${component}_configure not empty")
    }
}

sub check_all_flags
{
    my ($m) = @_;
    my @required_flags=qw(target clibrary);
    my $err;

    push @required_flags,"manifestid" if version_ge($m, "1.3");

    foreach my $flag (@required_flags)
    {
        if (!defined $m->{flag_info}{$flag})
        {
            error ($m, "$flag not defined");
            $err = 1;
        }
    }
    foreach my $flag (keys %{$m->{flag_info}})
    {
        if (!grep (/^$flag$/, @required_flags))
        {
             error ($m, "unknown flag $flag");
        }
    }
    return if $err;
    my $is_glibc = $m->{flag_info}{clibrary} =~ /^e?glibc$/;
    my $is_newlib = $m->{flag_info}{clibrary} =~ /^newlib$/;
    error ($m, "invalid clibrary") if $m->{flag_info}{target}=~/linux/ && !$is_glibc;
    error ($m, "invalid clibrary") if $m->{flag_info}{target}!~/linux/ && !$is_newlib;
    if (!defined $m->{component_info}{$m->{flag_info}{clibrary}})
    {
        error ($m, "No component info for selected clibrary");
    }
}

my %asc_md5_lookup=();

sub check_md5sum_asc_url
{
    my ($m, $component, $exp_md5sum, $url, $filespec) = @_;
    my $asc_md5 = $asc_md5_lookup{$url};
    if (!defined $asc_md5)
    {
        my $asc_file = get("$url");
        if (!defined $asc_file)
        {
            error ($m, "download of $url failed");
            return;
        }
        $asc_md5 = ($asc_file =~ /^([a-f0-9]*)  $filespec/)[0];
        $asc_md5_lookup{$url}=$asc_md5;
    }
    if (!defined $asc_md5)
    {
        error ($m, "Component $component asc file $url has invalid format");
        return;
    }
    if (defined $exp_md5sum && $asc_md5 ne $exp_md5sum)
    {
        error ($m, "$component md5sum did not match asc file, $exp_md5sum/$asc_md5");
    }
}

my %full_md5_lookup;

sub check_md5sum_full_file
{
    my ($m, $component, $exp_md5sum, $url, $filespec) = @_;
    my $real_md5sum=$full_md5_lookup{$url};
    if (!defined $real_md5sum)
    {
        my $full_file = get("$url");
        if (!defined $full_file)
        {
            error ($m, "download of $url failed");
            return;
        }
        $real_md5sum = md5_hex($full_file);
        $full_md5_lookup{$url} = $real_md5sum;
    }
    if (defined $exp_md5sum && $real_md5sum ne $exp_md5sum)
    {
        error ($m, "$component md5sum did not match downloaded file, $exp_md5sum/$real_md5sum");
    }
}

sub check_online
{
    my ($m) = @_;
    use LWP::Simple;
    foreach my $component (keys %{$m->{component_info}})
    {
        my $md5sum = $m->{component_info}{$component}{md5sum};
        my $filespec = $m->{component_info}{$component}{filespec};
        my $full_url = $m->{component_info}{$component}{url} . "/" . $filespec;
        # this is how abe does it, so we mirror that here...
        next if $filespec !~ /\.tar\./;
        check_md5sum_asc_url($m, $component, $md5sum, $full_url.".asc", $filespec);
        if ($opt_online == 2)
        {
            check_md5sum_full_file($m, $component, $md5sum, $full_url, $filespec);
        }
    }
}

sub check_gdbserver
{
    my ($m) = @_;
    my @matching_fields=qw(url branch revision filespec);
    return if !defined $m->{component_info}{gdb};
    return if !defined $m->{component_info}{gdbserver};
    foreach my $field (@matching_fields)
    {
        if ((defined $m->{component_info}{gdb}{$field} !=
               defined $m->{component_info}{gdbserver}{$field})
            || (defined $m->{component_info}{gdb}{$field}
                 && ($m->{component_info}{gdb}{$field} ne
                     $m->{component_info}{gdbserver}{$field})))
        {
            error ($m, "gdb_$field does not match gdbserver_$field");
        }
    }
}

sub check_components_list
{
    my ($m) = @_;
    my @required_components = qw(gmp mpfr mpc dejagnu binutils gcc gdb);
    my @linux_target_components = qw(linux gdbserver);
    my @win32_components = qw(expat python);
    my @valid_clibs = qw(glibc newlib eglibc);

    # in version 1.4 manifests, expat and python are required
    if (version_ge($m, "1.4"))
    {
        push @required_components, @win32_components;
        @win32_components=();
    }

    if (($m->{flag_info}{target}) =~ /linux/)
    {
        push @required_components, @linux_target_components
    }

    my @clibs=();
    foreach my $comp (keys %{$m->{component_info}})
    {
        if (grep /^$comp$/, @required_components)
        {
            # that's ok!
        } elsif (grep /^$comp$/, @win32_components) {
            # that's ok!
            # we could verify that the host is mingw32 here
        } elsif (grep /^$comp$/, @valid_clibs) {
            push @clibs, $comp;
        } else {
            error ($m, "Unexpected component $comp");
        }
    }
    foreach my $comp (@required_components)
    {
        if (!defined $m->{component_info}{$comp}) {
            error ($m, "Component $comp not found.");
        }
    }

    if ($#clibs == -1)
    {
        error ($m, "No C libraries");
    } elsif ($#clibs > 0) {
        error ($m, "Too many C libraries: @clibs");
    }
}

sub check_manifest_version
{
   my ($m) = @_;
   if ($m->{mani_ver} !~ /^1\.[1234]$/)
   {
       error($m, "Unknown manifest version $m->{mani_ver}, treating as 1.4");
       $m->{mani_ver} = "1.4";
   }
}

sub version_ge
{
    my ($m, $compare) = @_;
    my ($compare_major, $compare_minor) = $compare=~/(\d+)\.(\d+)/;
    my ($major, $minor) = $m->{mani_ver}=~/(\d+)\.(\d+)/;

    if ($major > $compare_major ||
        ($major == $compare_major && $minor >= $compare_minor))
    {
        return 1;
    } else {
        return undef;
    }
}

sub calculate_manifest_id
{
    my ($m) = @_;

    # This is used to accumulate the string which will be passed to sha1_hex
    # to calculate the manifest ID.
    my $str = "";

    $str=sprintf("%s%s=%s\n", $str, "manifest_format", $m->{mani_ver});
    foreach my $comp (sort keys %{$m->{component_info}})
    {
        foreach my $field (sort keys %{$m->{component_info}{$comp}})
	{
            my $field_value = $m->{component_info}{$comp}{$field};
            $str=sprintf("%s%s_%s=%s\n", $str, $comp, $field, $field_value);
	}
    }

    foreach my $flag (sort keys %{$m->{flag_info}})
    {
        next if $flag eq "manifestid";
	$str=sprintf("%s%s=%s\n", $str, $flag, $m->{flag_info}{$flag});
    }

    return sha1_hex($str);
}

sub check_manifestid
{
    my ($m) = @_;

    my $calculated_id=calculate_manifest_id($m);
    my $real_id = $m->{flag_info}{manifestid};

    # we don't need to check the manifest id if it is not present.
    return if !defined $real_id;

    if ($calculated_id ne $real_id)
    {
	error($m, "Manifest ID mismatch. Expected $real_id, got $calculated_id.");
    }
}

sub output_normalized
{
    my ($m, $outfile) = @_;

    open OUT,">$outfile" or die "Could not open $outfile: $!";

    printf(OUT "manifest_format=%s\n\n", $m->{mani_ver});
    printf(OUT "# Note that for ABE, these parameters are not used\n");

    foreach my $comp (sort keys %{$m->{component_info}})
    {
	printf(OUT "# Component data for %s\n", $comp);
        foreach my $field (sort keys %{$m->{component_info}{$comp}})
	{
            my $field_value = $m->{component_info}{$comp}{$field};
	    printf(OUT "%s_%s=%s\n", $comp, $field, $field_value);
	}
	printf(OUT "\n");
    }

    printf(OUT "\n");

    foreach my $flag (sort keys %{$m->{flag_info}})
    {
	printf(OUT "%s=%s\n", $flag, $m->{flag_info}{$flag});
    }

print OUT <<EOF;

 ##############################################################################
 # Everything below this line is only for informational purposes for developers
 ##############################################################################
EOF

    close OUT;
}