Debian Codegraph @

menuselect/contrib/menuselect-dummy

raw
1 #!/usr/bin/perl -w
2
3 # menuselect - a simple drop-in replacement of the batch-mode menuselect
4 # included with Asterisk.
5 #
6 # Copyright (C) 2008 by Tzafrir Cohen <[email protected]>
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 # USA
22
23 # Installation: copy this script to menuselect/menuselect . Copy the
24 # included Makefile as menuselect/Makefile and run:
25 #
26 # make -C makefile dummies
27 #
28 # It takes configuration from build_tools/conf . Sample config file:
29 #
30 # By default all modules will be built (except those marked not be
31 # used by default)
32 #
33 # # exclude: Don't try to build the following modules.
34 # #exclude app_test
35 #
36 # # You can have multiple items in each line, and multiple lines.
37 # # Each item is a perl regular expression that must match the whole
38 # # module name.
39 # #exclude res_config_.*
40 #
41 # # include: syntax is the same as exclude. Overrides exclude and
42 # # modules that are marked as disabled by defualt:
43 # #include res_config_sqlite3 app_skel
44 #
45 # # If you want to make sure some modules will be conifgured to build,
46 # # you can require them. If modules that match any of the 'require'
47 # # pattern are not configured to build, menuselect will panic.
48 # # Same pattern rules apply here. Why would you want that? I have no
49 # # idea.
50 # #require chan_h323 app_directory
51 #
52 # # random - the value for this keyword is a number between 1 and
53 # # 100. The higher it is, more chances not to include each module.
54 # # Writes the list of modules that got hit to
55 # # build_tools/mods_removed_random .
56 # # Note that unlike 'make randomconfig' and such the random
57 # # configuration changes each time you run 'make', thus if a build
58 # # failed you should first read build_tools/mods_removed_random
59 # # before re-running make.
60 # #random 10
61 #
62 # # Anything after a '#' is ignored, and likewise empty lines.
63 # # Naturally.
64
65 use strict;
66 use Getopt::Long;
67
68 # Holds global dependency information. Keys are module names.
69 my %ModInfo = ();
70
71 # extract configuration from kernel modules:
72 my $AutoconfDepsFile = "build_tools/menuselect-deps";
73
74 my $AutoconfOptsFile = "makeopts";
75 my %ConfigureOpts = (); #
76
77 # configuration file to read for some directives:
78 my $ConfFile = "build_tools/conf";
79
80 my $DumpFile = 'build_tools/dump_deps';
81
82 # Modules removed randomly:
83 my $RandomModsFile = "build_tools/mods_removed_random";
84
85 my $MakedepsFile = "menuselect.makedeps";
86
87 my $MakeoptsFile = "menuselect.makeopts";
88
89 # If those modules are not present, the build will fail (PCRE patterns)
90 my @RequiredModules = ();
91
92 my @Subdirs = qw/addons apps bridges cdr cel channels codecs formats funcs main pbx res tests utils/;
93
94 my @XmlCategories = 'cflags';
95
96 # Modules should not bother building (PCRE patterns)
97 my @ExcludedModules = ();
98
99 # Do try building those. Overrides 'exclude' and 'defaultenable: no'
100 my @IncludedModules = ();
101
102 # A chance to rule-out a module randomly.
103 my $RandomKnockoutFactor = 0;
104
105 sub warning($) {
106 my $msg = shift;
107 print STDERR "$0: Warning: $msg\n";
108 }
109
110 # Convert XML syntax to mail-header-like syntax:
111 # <var>value</var> --> Var: value
112 sub extract_xml_key($) {
113 my %attr = ();
114 my $xml_line = shift;
115 if ($xml_line !~ m{^\s*<([a-z_A-Z0-9]+)(\s+([^>]*))?>([^<]*)</\1>}) {
116 warning "parsed empty value from XML line $xml_line";
117 return ('', ''); # warn?
118 }
119 my ($var, $val) = ($1, $4);
120 $var =~ s{^[a-z]}{\u$&};
121 if (defined $3) {
122 my $attr_text = $3;
123 while ($attr_text =~ /^( *([^=]+)="([^"]+)")/) {
124 my ($var, $val) = ($2, $3);
125 $attr_text =~ s/^$1//;
126 $attr{$var} = $val;
127 }
128 }
129 return ($var, $val, %attr);
130 }
131
132 # Get information embedded in source files from a subdirectory.
133 # First parameter is the subdirectory and further ones are the actual
134 # source files.
135 sub get_subdir_module_info {
136 my $subdir = shift;
137 my @files = @_;
138
139 my $dir = uc($subdir);
140
141 foreach my $src (@files) {
142 open SRC,$src or die "Can't read from source file $src: $!\n";
143 $src =~ m|.*/([^/]*)\.c|;
144 my $mod_name = $1;
145 my %data = (
146 Type=>'module',
147 Module=>$mod_name,
148 Dir=> $dir,
149 Avail=>1
150 );
151
152 while (<SRC>) {
153 next unless (m|^/\*\*\* MODULEINFO| .. m|^ *[*]+/|);
154 next unless (m|^[A-Z]| || m|^\s*<|);
155
156 # At this point we can assume we're in the module
157 # info section.
158 chomp;
159 my ($var, $val, %attr) = extract_xml_key($_);
160 foreach (keys %attr) {
161 push @{$data{$_}},($attr{$_});
162 }
163 if ($var =~ /^(Depend|Use)$/i) {
164 # use uppercase for dependency names;
165 $val = uc($val);
166 }
167 if ( ! exists $data{$var} ) {
168 $data{$var} = [$val];
169 } else {
170 push @{$data{$var}},($val);
171 }
172 }
173 close SRC;
174
175 $ModInfo{uc($mod_name)} = \%data;
176 }
177 }
178
179 # extract embedded information in all the source tree.
180 sub extract_subdirs {
181 for my $subdir(@_) {
182 get_subdir_module_info($subdir, <$subdir/*.c> , <$subdir/*.cc>);
183 }
184 }
185
186 # parse a partial XML document that is included as an input
187 # for menuselect in a few places. Naturally a full-fledged XML parsing
188 # will not be done here. A line-based parsing that happens to work will
189 # have to do.
190 sub parse_menuselect_xml_file($) {
191 my $file_name = shift;
192 open XML,$file_name or
193 die "Failed opening XML file $file_name: $!.\n";
194
195 my $header = <XML>;
196 $header =~ /^\s*<category\s+name="MENUSELECT_([^"]+)"\s/;
197 my $category = $1;
198 my $member;
199
200 while(<XML>){
201 next unless (m{^\s*<(/?[a-z]+)[>\s]});
202 my $tag = $1;
203
204 if ($tag eq 'member') {
205 if (! m{^\s*<member\s+name="([^"]+)" displayname="([^"]+)"\s*>}){
206 warning "Bad XML member line: $_ ($file_name:$.)\n";
207 next;
208 }
209 my ($name, $display_name) = ($1, $2);
210
211 $member = {
212 Type => 'XML',
213 Dir => $category,
214 Module => $1,
215 DisplayName => $2,
216 Defaultenabled => ['no'],
217 Avail => 1,
218
219 };
220 } elsif ($tag eq '/member') {
221 $ModInfo{$member->{Module}} = $member;
222 } elsif ($tag eq '/category') {
223 last;
224 } else {
225 if (! m/^\s*<([a-z]+)>([^<]+)</) {
226 warning "(1) Unknown XML line $_ ($file_name:$.)\n";
227 next
228 }
229 my ($key, $val) = extract_xml_key($_);
230 if ($key eq '') {
231 warning "Unknown XML line $_ ($file_name:$.)\n";
232 next
233 }
234 if (! exists $member->{$key}) {
235 $member->{$key} = [];
236 }
237
238 # Make sure dependencies are upper-case.
239 # FIXME: this is not the proper place for such a fix
240 $val = uc($val) if ($key =~ /Depend|Use/);
241
242 # Using "unshift' rather than 'push'.
243 # For a singleton value this makes the action an
244 # override, as only the first value counts.
245 # For a list value, however, it means a reversed
246 # order.
247 unshift @{$member->{$key}}, ($val);
248 }
249 }
250
251
252 close XML;
253 }
254
255 # Dump our data structure to a file.
256 sub dump_deps($) {
257 my $file = shift;
258 open OUTPUT,">$file" or
259 die "cannot open category file $file for writing: $!\n";
260
261 foreach my $mod_name (sort keys %ModInfo) {
262 print OUTPUT "Key: $mod_name\n";
263 my $data = $ModInfo{$mod_name};
264 foreach my $var (sort keys %{$data} ) {
265 my $val = $$data{$var};
266 if (ref($val) eq 'ARRAY') {
267 print OUTPUT $var.": ". (join ", ", @$val)."\n";
268 } else {
269 print OUTPUT "$var: $val\n";
270 }
271 }
272 print OUTPUT "\n";
273 }
274 close OUTPUT;
275 }
276
277 # Get the available libraries that autoconf generated.
278 sub get_autoconf_deps() {
279 open DEPS, $AutoconfDepsFile or
280 die "Failed to open $AutoconfDepsFile. Aborting: $!\n";
281
282 my @deps_list = (<DEPS>);
283 foreach (@deps_list){
284 chomp;
285 my ($lib, $avail_val) = split(/=/);
286 my ($avail, $avail_old) = split(/:/, $avail_val);
287 my $disabled = 0;
288 if ($avail == -1) {
289 $disabled = 1;
290 $avail = 0;
291 }
292 $ModInfo{$lib} = {
293 Type=>'lib', Avail=>$avail, Disabled => $disabled
294 };
295 if (defined $avail_old) {
296 $ModInfo{$lib}{AvailOld} = $avail_old;
297 }
298 # FIXME:
299 if (($avail ne "0") && ($avail ne "1")) {
300 warning "Library $lib has invalid availability ".
301 "value <$avail> (check $AutoconfDepsFile).\n";
302 }
303 }
304 close DEPS;
305 }
306
307 # Get the available libraries that autoconf generated.
308 sub get_autoconf_opts() {
309 open OPTS, $AutoconfOptsFile or
310 die "Failed to open $AutoconfOptsFile. Aborting: $!\n";
311
312 while (<OPTS>) {
313 chomp;
314 next if /^(#|$)/;
315 my ($var, $val) = split /\s*=\s*/, $_, 2;
316 $ConfigureOpts{$var} = $val;
317 }
318 close OPTS;
319 if (not exists $ConfigureOpts{AST_DEVMODE}) {
320 $ConfigureOpts{AST_DEVMODE} = 'no';
321 }
322 }
323
324 # Read our specific config file.
325 #
326 # Its format:
327 #
328 # keyword values
329 #
330 # values are always a spaces-separated list.
331 sub read_conf() {
332 open CONF,$ConfFile or return;
333
334 while (<CONF>) {
335 # remove comments and empty lines:
336 chomp;
337 s/#.*$//;
338 next if /^\s*$/;
339
340 my ($keyword, @value) = split;
341
342 if ($keyword eq 'exclude') {
343 push @ExcludedModules, @value;
344 } elsif ($keyword eq 'include') {
345 push @IncludedModules, @value;
346 } elsif ($keyword eq 'require') {
347 push @RequiredModules, @value;
348 } elsif ($keyword eq 'random') {
349 $RandomKnockoutFactor = $value[0] / 100;
350 } else {
351 warning "unknown keyword $keyword in line $. of $ConfFile.";
352 }
353 }
354 }
355
356 # generate menuselect.makedeps.
357 # In this file menuselect writes dependencies of each module. CFLAGS will
358 # then automatically include for each module the _INCLUDE and LDFLAGS
359 # will include the _LIBS from all the dependencies of the module.
360 sub gen_makedeps() {
361 open MAKEDEPSS, ">$MakedepsFile" or
362 die "Failed to open deps file $MakedepsFile for writing. Aborting: $!\n";
363
364 for my $mod_name (sort keys %ModInfo) {
365 next unless ($ModInfo{$mod_name}{Type} eq 'module');
366
367 my $mod = $ModInfo{$mod_name};
368 my @deps = ();
369
370 # if we have Depend or Use, put their values into
371 # @deps . If we have none, move on.
372 push @deps, @{$mod->{Depend}} if (exists $mod->{Depend});
373 push @deps, @{$mod->{Use}} if (exists $mod->{Use});
374 next unless @deps;
375
376 # TODO: don't print dependencies that are not external libs.
377 # Not done yet until I figure out if this is safe.
378 my $dep = join(' ', @deps);
379 print MAKEDEPSS "MENUSELECT_DEPENDS_".$mod->{Module}."=$dep\n";
380 }
381
382 close MAKEDEPSS;
383 }
384
385 # Set modules from patterns specified by 'exclude' in the configuration file
386 # to exclude modules from building (mark them as unavailable).
387 sub apply_excluded_patterns() {
388 foreach my $pattern (@ExcludedModules) {
389 my @excluded = grep {/^$pattern$/i} (keys %ModInfo);
390 foreach (@excluded) {
391 $ModInfo{$_}{Avail} = 0;
392 }
393 }
394 }
395
396 # Set modules from patterns specified by 'include' in the configuration
397 # file to exclude from building (mark them as available).
398 sub apply_included_patterns() {
399 foreach my $pattern (@IncludedModules) {
400 my @included = grep {/^$pattern$/i} (keys %ModInfo);
401 foreach (@included) {
402 $ModInfo{$_}{Avail} = 1;
403 }
404 }
405 }
406
407 # If user set the "random" config to anything > 0, drop some random
408 # modules. May help expose wrong dependencies.
409 sub apply_random_drop() {
410 return if ($RandomKnockoutFactor <= 0);
411
412 open MODS_LIST, ">$RandomModsFile" or
413 die "Failed to open modules list file $RandomModsFile for writing. Aborting: $!\n";
414 for my $mod (keys %ModInfo) {
415 next unless ($ModInfo{$mod}{Type} eq 'module');
416 next unless (rand() < $RandomKnockoutFactor);
417 $ModInfo{$mod}{Avail} = 0;
418 $ModInfo{$mod}{RandomKill} = 1;
419 print MODS_LIST $ModInfo{$mod}{Module}."\n";
420 }
421
422 close MODS_LIST;
423
424
425 }
426
427 sub check_required_patterns() {
428 my @failed = ();
429 foreach my $pattern (@RequiredModules) {
430 my @required = grep {/^$pattern$/i} (keys %ModInfo);
431 foreach my $mod (@required) {
432 if ((! exists $ModInfo{$mod}{Checked}) ||
433 (! $ModInfo{$mod}{Checked}) )
434 {
435 push @failed, $mod;
436 }
437 }
438 }
439 return unless (@failed);
440
441 my $failed_str = join ' ',@failed;
442 die("Missing dependencies for the following modules: $failed_str\n");
443 }
444
445 # Disable building for modules that were marked in the embedded module
446 # information as disabled for building by default.
447 sub apply_default_enabled() {
448 foreach my $mod (keys %ModInfo) {
449 if ((exists $ModInfo{$mod}{Defaultenabled}) &&
450 $ModInfo{$mod}{Defaultenabled}[0] eq 'no')
451 {
452 $ModInfo{$mod}{Avail} = 0;
453 }
454 }
455 }
456
457 # We found a dependency we don't know about. Warn the user, and add
458 # information about it:
459 sub handle_unknown_dep($$) {
460 my ($dep_mod, $mod) = @_;
461
462 my $mod_info = {
463 Type => 'Unknown',
464 Avail => 0,
465 Checked => 0,
466 };
467 $ModInfo{$dep_mod} = $mod_info;
468
469 warning "Unknown dependency module $dep_mod (for e.g. $mod)\n";
470 }
471
472 # recursively check dependency for a module.
473 #
474 # We run a scan for modules. Modules marked as 'Checked' are ones we
475 # have already fully verified to have proper dependencies.
476 #
477 # We can only use a module or library marked as Avail => 1 (library
478 # available or module not excluded).
479 sub check_module($);
480 sub check_module($) {
481 my $mod = shift;
482
483 # we checked it:
484 if (exists $ModInfo{$mod}{Checked}) {
485 return $ModInfo{$mod}{Checked};
486 }
487 # A library has no dependencies of its own.
488 if ($ModInfo{$mod}{Type} eq 'lib') {
489 return ($ModInfo{$mod}{Avail} || 0);
490 }
491 # An excluded module.
492 if ($ModInfo{$mod}{Avail} == 0) {
493 return 0;
494 }
495 if (! exists $ModInfo{$mod}{Depend}) {
496 $ModInfo{$mod}{Checked} = 1;
497 return 1;
498 }
499
500 my $deps_checked = 1; # may be reset below on failures:
501
502 if (exists $ModInfo{$mod}{Tested}) {
503 # this probably means a circular dependency of some sort.
504 warning "Got to module $mod that is already tested.";
505 }
506 $ModInfo{$mod}{Tested} = 1;
507
508 foreach my $dep_mod (@{$ModInfo{$mod}{Depend}} ) {
509 if (!exists ${ModInfo}{$dep_mod}) {
510 handle_unknown_dep($dep_mod, $mod);
511 return 0;
512 }
513 $deps_checked &= check_module($dep_mod);
514 last if(!$deps_checked) # no point testing further if we failed.
515 }
516
517 $ModInfo{$mod}{Checked} = $deps_checked;
518 return $deps_checked;
519 }
520
521 # The main dependency resolver function.
522 sub resolve_deps() {
523 apply_default_enabled();
524 apply_excluded_patterns();
525 apply_included_patterns();
526
527 foreach my $mod (keys %ModInfo) {
528 check_module($mod);
529 }
530 }
531
532 # generate menuselect.makeopts.
533 # The values in this file obey to different semantics:
534 # 1. For modules, a module will be built unless listed here
535 # 2. For XML values (sounds, CFLAGS) it will be enabled if listed here
536 sub gen_makeopts() {
537 open MAKEDEPS, ">$MakeoptsFile" or
538 die "Failed to open opts file $MakeoptsFile for writing. Aborting: $!\n";
539
540 my %Subdirs;
541 foreach my $mod (sort keys %ModInfo) {
542 next unless ($ModInfo{$mod}{Type} =~ /^(module|XML)$/);
543 if ($ModInfo{$mod}{Type} eq 'XML') {
544 next unless ($ModInfo{$mod}{Checked});
545 } else {
546 next if ($ModInfo{$mod}{Checked});
547 }
548 my $dir = $ModInfo{$mod}{Dir};
549 if (! exists $Subdirs{$dir}) {
550 $Subdirs{$dir} = [];
551 }
552 push @{$Subdirs{$dir}},( $ModInfo{$mod}{Module} );
553 }
554 foreach my $dir (sort keys %Subdirs) {
555 my $deps = join(' ', @{$Subdirs{$dir}});
556 print MAKEDEPS "MENUSELECT_$dir=$deps\n";
557 }
558
559 close MAKEDEPS;
560 }
561
562 # Main function for --check-deps
563 sub check_dependencies() {
564 read_conf();
565
566 extract_subdirs(@Subdirs);
567
568 get_autoconf_opts();
569
570 parse_menuselect_xml_file('build_tools/cflags.xml');
571 if ($ConfigureOpts{AST_DEVMODE} eq 'yes') {
572 parse_menuselect_xml_file('build_tools/cflags-devmode.xml');
573 }
574 parse_menuselect_xml_file('sounds/sounds.xml');
575
576 apply_random_drop();
577
578 get_autoconf_deps();
579
580 #dump_deps('build_tools/dump_deps_before_resolve');
581 resolve_deps();
582
583 # Handy debugging:
584 dump_deps($DumpFile);
585
586 check_required_patterns();
587
588 gen_makedeps();
589
590 gen_makeopts();
591 }
592
593 #
594 # The main program start here
595 #
596
597 sub read_dump() {
598 my %items = ();
599 my $saved_rs = $/;
600 $/ = "\n\n";
601 open DUMP_FILE,$DumpFile or die "Can't read from dump file $DumpFile\n";
602 while (<DUMP_FILE>) {
603 my %item = ();
604 my @item_lines = split /\n\r?/;
605 foreach (@item_lines) {
606 my ($var, $value) = split /: /, $_, 2;
607 $item{$var} = $value;
608 }
609 # FIXME: dependencies are a list. This should not be a
610 # special case.
611 if (exists $item{Depend}) {
612 $item{Depend} = [split /\s*,\s*/,$item{Depend}];
613 }
614 $items{$item{Key}} = \%item;
615 }
616 close DUMP_FILE;
617 $/ = $saved_rs;
618 return \%items;
619 }
620
621 # Explain why a module (read from the dump file) was not enabled.
622 # (We assume here that $item->{Avail} is 0)
623 sub fail_reason($) {
624 my $item = shift;
625 if ($item->{Type} eq 'lib') {
626 return " Not found: system library";
627 } elsif ($item->{Type} eq 'XML') {
628 if ($item->{Defaultenabled} !~ /^y/) {
629 return "Not enabled";
630 } else {
631 return "Missing dependencies";
632 }
633 } elsif ($item->{Type} eq 'module') {
634 if (exists ($item->{Defaultenabled}) &&
635 $item->{Defaultenabled} =~ /^n/) {
636 return "Disabled";
637 } else {
638 return "Missing dependencies";
639 }
640 }
641 }
642
643 sub item_used($) {
644 my $item = shift;
645 my $type = $item->{Type};
646
647 return $item->{Avail} if ($type eq 'lib');
648 return $item->{Checked};
649 }
650
651 sub print_module_status {
652 my $flag_verbose = shift;
653 my $items = read_dump();
654 my %items_matched = ();
655
656 foreach my $pattern (@_) {
657 foreach (keys %$items) {
658 if (/$pattern/i) {
659 $items_matched{$_} = 1;
660 }
661 }
662 }
663
664 my @items_list = sort keys %items_matched;
665
666 foreach my $item_name (@items_list) {
667 my $item = $items->{$item_name};
668 if ($flag_verbose) {
669 printf "%s %-8s %-30s\n",
670 (item_used($item)? 'Y':'n'),
671 $item->{Type},
672 $item->{Key};
673 if (!$item->{Avail}) {
674 my $reason = fail_reason($item);
675 print " $reason\n";
676 }
677 foreach (@{$item->{Depend}}) {
678 my $depmod = $items->{$_};
679 printf(" * %-12s ",$_);
680 print (item_used($depmod)? '': "un");
681 print "available\n";
682 }
683 } else {
684 printf "%s %-8s %-30s",
685 (item_used($item)? 'Y':'n'),
686 $item->{Type},
687 $item->{Key};
688 foreach (@{$item->{Depend}}) {
689 my $depmod = $items->{$_};
690 if (item_used($depmod)) {
691 print "$_ ";
692 } else {
693 printf "[%s] ", $_;
694 }
695 }
696 print "\n";
697 }
698 }
699 }
700
701 sub usage() {
702 print "$0: menuselect reimplementation\n";
703 print "\n";
704 print "Usage:\n";
705 print "$0 # menuselect processing\n";
706 print "$0 -m|--modinfo|--modules-info PATTERN # Status of modules\n";
707 print "$0 -v|--verbose # verbose (modinfo)\n";
708 print "$0 -c|--check-deps # Check for dependencies\n";
709 print "\n";
710 print "PATTERN is a partial perl regex. Use '-m .' to list all.\n";
711 }
712
713 my @module_status = ();
714
715 my $flag_verbose = 0;
716
717 my $action = '';
718
719 my $rc = GetOptions(
720 'modinfo|modules-info|m=s' => \@module_status,
721 'verbose|v' => \$flag_verbose,
722 'check-deps|c:s' => sub { $action = 'check_dependencies'},
723 'help|h' => sub { usage(); exit 0 },
724 );
725 if (!$rc) {
726 usage();
727 exit $rc;
728 }
729
730 if (@module_status) {
731 $action = 'module_status';
732 }
733
734 if ($action eq 'module_status') {
735 print_module_status($flag_verbose, @module_status);
736 exit 0;
737 } elsif ( $action eq 'check_dependencies') {
738 check_dependencies();
739 } else {
740 usage(); exit(1);
741 }