[cage] Add some karma aliases for myself
[parrot.git] / tools / dev / lib_deps.pl
blob970035efce428a44aaaa3285d95120b348d637a2
1 #! perl
2 ################################################################################
3 # Copyright (C) 2001-2003, Parrot Foundation.
4 # $Id$
5 ################################################################################
7 =head1 NAME
9 tools/dev/lib_deps.pl - List libc dependencies
11 =head1 SYNOPSIS
13 % perl tools/dev/lib_deps.pl [object | source] file1 [file2 ...]
15 =head1 DESCRIPTION
17 This script is intended to give us an idea of what C<libc> functions
18 this build depends upon.
20 =head2 Options
22 =over 4
24 =item C<object>
26 In C<object> mode, it expects a list of all parrot's object files. It
27 runs C<nm> on each and determines what external functions are being
28 called. Note that it assumes a gnu-ish C<nm>.
30 =item C<source>
32 In C<source> mode, it uses a the C<cxref> program
33 (L<http://www.gedanken.demon.co.uk/cxref/>) to extract information from
34 the program source about what functions are being called, what includes
35 are used, etc. This mode is potentially more thorough, but a bit more
36 magical and therefore less conclusive.
38 =back
40 =cut
42 ################################################################################
44 use strict;
45 use warnings;
46 use File::Find;
47 use File::Spec;
49 my %defined_in;
50 my %referenced_in;
51 my %ansi_c89_symbol;
52 my %ansi_c89_header;
54 my ( $mode, @files ) = @ARGV;
56 if ( $mode !~ /^(source|object)$/ || !@files ) {
57 die "Usage: $0 object <object files..>\n" . " $0 source <source files..>\n";
60 while (<DATA>) {
61 next if /^\s*#/;
62 chomp;
63 my ( $symbol, $file ) = /(\S+)\s+(\S+)/;
64 $ansi_c89_symbol{$symbol} = $file unless ( $symbol eq "UNDEF" );
65 push @{ $ansi_c89_header{$file} }, $symbol;
68 if ( $mode eq "object" ) {
69 do_object();
71 else {
72 do_source();
75 exit(0);
77 ##############################################################################
79 sub do_source {
81 if ( $files[0] eq "all_source" ) {
83 # do a little "find" action for now.
85 @files = ();
86 File::Find::find(
88 wanted => sub {
89 /^.*\.[ch]\z/s
90 && push @files, $File::Find::name;
93 '.'
97 # note: need to run this a second time so the database is built.
98 # should just use the build process to do it the first time.
99 my $devnull = File::Spec->devnull;
100 my $cmd = "cxref -raw -Iinclude -xref @files";
101 print "Running cxref (pass 1)\n";
102 system("$cmd > $devnull 2>$devnull");
103 print "Running cxref (pass 2)\n";
104 open( my $F, '<', "$cmd 2>$devnull|" )
105 || die "Can't run $cmd.\n";
107 my %external_calls;
108 my %internal_calls;
109 my %variable_visible;
110 my %system_include;
111 my ( $file, $function, $variable );
112 while (<$F>) {
114 if (/----------------------------------------/) {
115 undef $file if defined($file);
116 next;
119 if (/^INCLUDES : '(.*)' \[System file\]/) {
120 next if ( $1 =~ /^include\// );
121 $system_include{$1}{$file}++;
122 next;
125 if ( !$file && /^FILE : '(.*)'$/ ) {
126 $file = $1;
127 next;
130 # skip anything between files.
131 next unless $file;
133 # beginning of function block
134 if (/FUNCTION : (.*) \[(.*)\]/) {
135 $function = $1;
136 my $function_scope = $2;
138 next;
141 # end of function block
142 if ( $function && /^\s*$/ ) {
143 undef $function;
144 next;
147 # beginning of variable block
148 if (/VARIABLE : (.*) \[(.*)\]/) {
149 $variable = $1;
150 my $variable_scope = $2;
151 if ( $variable_scope eq "Local" ) {
152 $variable_visible{$file}{$1}++;
154 else {
155 $variable_visible{"ALL"}{$1}++;
158 next;
161 # end of variable block
162 if ( $variable && /^\s*$/ ) {
163 undef $variable;
164 next;
167 if ($function) {
168 if (/Calls (.*) : (.*)/) {
170 # calling another function within parrot.
171 $internal_calls{$1}{"$file:$function"}++
172 unless ( $variable_visible{$file}{$1}
173 || $variable_visible{ALL}{$1} );
175 elsif (/Calls (.*)/) {
177 # calling a function outside of parrot!
178 $external_calls{$1}{"$file:$function"}++
179 unless ( $variable_visible{$file}{$1}
180 || $variable_visible{ALL}{$1} );
185 close($F);
187 # filter out things that start with _. Probably internal libc stuff.
188 my @external_calls = grep { !/^_/ } sort keys %external_calls;
189 my @internal_calls = grep { !/^_/ } sort keys %internal_calls;
190 my @non_ansi_external_calls = grep { !exists( $ansi_c89_symbol{$_} ) } @external_calls;
192 printf(
193 "Found %d functions which are defined and called within the %d supplied source files.\n",
194 scalar(@internal_calls), scalar(@files) );
195 printf( "Found %d external functions which were called.\n", scalar(@external_calls) );
196 printf( "Of these, %d are not defined by ANSI C89:\n", scalar(@non_ansi_external_calls) );
198 foreach (@non_ansi_external_calls) {
199 print " $_:\n";
200 foreach ( sort keys %{ $external_calls{$_} } ) {
201 print " $_\n";
205 print "\nThe following non-ansi system includes are used:\n";
206 foreach my $include ( sort keys %system_include ) {
207 if ( !exists( $ansi_c89_header{$include} ) ) {
208 print " $include, included by:\n";
209 foreach my $file ( sort keys %{ $system_include{$include} } ) {
210 print " $file\n";
216 sub do_object {
217 foreach my $obj (@files) {
218 open( my $F, '<', "nm -a $obj|" ) || die "Can't run nm on $obj\n";
220 while (<$F>) {
221 chomp;
223 my ( $type, $symbol ) = /^........ (\S) (.*)/;
225 if ( $type eq 'U' ) {
226 $defined_in{$symbol} ||= undef;
227 push @{ $referenced_in{$symbol} }, $obj;
229 else {
230 $defined_in{$symbol} .= "$obj ";
234 close($F);
237 # omit symbols which begin with _. These are likely to be internal
238 # variables used by libc macros.
239 my @symbols = grep { !/^_/ } sort keys %defined_in;
241 my @external_symbols = sort grep { !defined( $defined_in{$_} ) } @symbols;
242 my @internal_symbols = sort grep { defined( $defined_in{$_} ) } @symbols;
243 my @non_ansi_external_symbols = grep { !exists( $ansi_c89_symbol{$_} ) } @external_symbols;
245 printf(
246 "Found %d symbols defined within the %d supplied object files.\n",
247 scalar(@internal_symbols),
248 scalar(@files)
250 printf( "Found %d external symbols\n", scalar(@external_symbols) );
251 printf( "Of these, %d are not defined by ANSI C89:\n", scalar(@non_ansi_external_symbols) );
253 print " $_ (in " . ( join ',', @{ $referenced_in{$_} } ) . ")\n"
254 foreach (@non_ansi_external_symbols);
257 __END__
258 # The following symbols are available in a C89 Hosted Implementation
259 # (not sure if I got this right- it came from a C99 reference, so some 99isms
260 # might have slipped in)
261 abort stdlib.h
262 abs stdlib.h
263 acos math.h
264 acosf math.h
265 acosh math.h
266 acoshf math.h
267 acoshl math.h
268 acosl math.h
269 arg complex.h
270 asctime time.h
271 asin math.h
272 asinf math.h
273 asinh math.h
274 asinhf math.h
275 asinhl math.h
276 asinl math.h
277 atan math.h
278 atan2 math.h
279 atan2f math.h
280 atan2l math.h
281 atanf math.h
282 atanh math.h
283 atanhf math.h
284 atanhl math.h
285 atanl math.h
286 atexit stdlib.h
287 atof stdlib.h
288 atoi stdlib.h
289 atol stdlib.h
290 atoll stdlib.h
291 bsearch stdlib.h
292 cabs complex.h
293 cabsf complex.h
294 cabsl complex.h
295 cacos complex.h
296 cacosf complex.h
297 cacosh complex.h
298 cacoshf complex.h
299 cacoshl complex.h
300 cacosl complex.h
301 calloc stdlib.h
302 carg complex.h
303 cargf complex.h
304 cargl complex.h
305 casin complex.h
306 casinf complex.h
307 casinh complex.h
308 casinhf complex.h
309 casinhl complex.h
310 casinl complex.h
311 catan complex.h
312 catanf complex.h
313 catanh complex.h
314 catanhf complex.h
315 catanhl complex.h
316 catanl complex.h
317 cbrt math.h
318 cbrtf math.h
319 cbrtl math.h
320 ccos complex.h
321 ccosf complex.h
322 ccosh complex.h
323 ccoshf complex.h
324 ccoshl complex.h
325 ccosl complex.h
326 ceil math.h
327 ceilf math.h
328 ceill math.h
329 cexp complex.h
330 cexpf complex.h
331 cexpl complex.h
332 cimag complex.h
333 cimagf complex.h
334 cimagl complex.h
335 clearerr stdio.h
336 clock time.h
337 clog complex.h
338 clogf complex.h
339 clogl complex.h
340 conj complex.h
341 conjf complex.h
342 conjl complex.h
343 copysign math.h
344 copysignf math.h
345 copysignl math.h
346 cos math.h
347 cosf math.h
348 cosh math.h
349 coshf math.h
350 coshl math.h
351 cosl math.h
352 cpow complex.h
353 cpowf complex.h
354 cpowl complex.h
355 cproj complex.h
356 cprojf complex.h
357 cprojl complex.h
358 creal complex.h
359 crealf complex.h
360 creall complex.h
361 csin complex.h
362 csinf complex.h
363 csinh complex.h
364 csinhf complex.h
365 csinhl complex.h
366 csinl complex.h
367 csqrt complex.h
368 csqrtf complex.h
369 csqrtl complex.h
370 ctan complex.h
371 ctanf complex.h
372 ctanh complex.h
373 ctanhf complex.h
374 ctanhl complex.h
375 ctanl complex.h
376 ctime time.h
377 difftime time.h
378 div stdlib.h
379 erf math.h
380 erfc math.h
381 erfcf math.h
382 erfcl math.h
383 erff math.h
384 erfl math.h
385 errno errno.h
386 exit stdlib.h
387 exp math.h
388 exp2 math.h
389 exp2f math.h
390 exp2l math.h
391 expf math.h
392 expl math.h
393 expm1 math.h
394 expm1f math.h
395 expm1l math.h
396 fabs math.h
397 fabsf math.h
398 fabsl math.h
399 fclose stdio.h
400 fdim math.h
401 fdimf math.h
402 fdiml math.h
403 feof stdio.h
404 ferror stdio.h
405 fflush stdio.h
406 fgetc stdio.h
407 fgetpos stdio.h
408 fgets stdio.h
409 floor math.h
410 floorf math.h
411 floorl math.h
412 fma math.h
413 fmaf math.h
414 fmal math.h
415 fmax math.h
416 fmaxf math.h
417 fmaxl math.h
418 fmin math.h
419 fminf math.h
420 fminl math.h
421 fmod math.h
422 fmodf math.h
423 fmodl math.h
424 fopen stdio.h
425 fpclassify math.h
426 fprintf stdio.h
427 fputc stdio.h
428 fputs stdio.h
429 fread stdio.h
430 free stdlib.h
431 freopen stdio.h
432 frexp math.h
433 frexpf math.h
434 frexpl math.h
435 fscanf stdio.h
436 fseek stdio.h
437 fsetpos stdio.h
438 ftell stdio.h
439 fwrite stdio.h
440 getc stdio.h
441 getchar stdio.h
442 getenv stdlib.h
443 gets stdio.h
444 gmtime time.h
445 hypot math.h
446 hypotf math.h
447 hypotl math.h
448 ilogb math.h
449 ilogbf math.h
450 ilogbl math.h
451 imag complex.h
452 isalnum ctype.h
453 isalpha ctype.h
454 isblank ctype.h
455 iscntrl ctype.h
456 isdigit ctype.h
457 isfinite math.h
458 isgraph ctype.h
459 isgreater math.h
460 isgreatereq math.h
461 isinf math.h
462 isless math.h
463 islessequal math.h
464 islessgreat math.h
465 islower ctype.h
466 isnan math.h
467 isnormal math.h
468 isprint ctype.h
469 ispunct ctype.h
470 isspace ctype.h
471 isunordered math.h
472 isupper ctype.h
473 isxdigit ctype.h
474 labs stdlib.h
475 ldexp math.h
476 ldexpf math.h
477 ldexpl math.h
478 ldiv stdlib.h
479 lgamma math.h
480 lgammaf math.h
481 lgammal math.h
482 llabs stdlib.h
483 llrint math.h
484 llrintf math.h
485 llrintl math.h
486 llround math.h
487 llroundf math.h
488 llroundl math.h
489 localeconv locale.h
490 localtime time.h
491 log math.h
492 log10 math.h
493 log10f math.h
494 log10l math.h
495 log1p math.h
496 log1pf math.h
497 log1pl math.h
498 log2 math.h
499 log2f math.h
500 log2l math.h
501 logb math.h
502 logbf math.h
503 logbl math.h
504 logf math.h
505 logl math.h
506 longjmp setjmp.h
507 lrint math.h
508 lrintf math.h
509 lrintl math.h
510 lround math.h
511 lroundf math.h
512 lroundl math.h
513 malloc stdlib.h
514 mblen stdlib.h
515 mbstowcs stdlib.h
516 mbtowc stdlib.h
517 memchr string.h
518 memcmp string.h
519 memcpy string.h
520 memmove string.h
521 memset string.h
522 mktime time.h
523 modf math.h
524 modff math.h
525 modfl math.h
526 nan math.h
527 nanf math.h
528 nanl math.h
529 nearbyint math.h
530 nearbyintf math.h
531 nearbyintl math.h
532 nextafter math.h
533 nextafterf math.h
534 nextafterl math.h
535 nexttoward math.h
536 nexttowardf math.h
537 nexttowardl math.h
538 perror stdio.h
539 pow math.h
540 printf stdio.h
541 putc stdio.h
542 putchar stdio.h
543 puts stdio.h
544 qsort stdlib.h
545 raise signal.h
546 rand stdlib.h
547 real complex.h
548 realloc stdlib.h
549 remainder math.h
550 remainderf math.h
551 remainderl math.h
552 remove stdio.h
553 remquo math.h
554 remquof math.h
555 remquol math.h
556 rename stdio.h
557 rewind stdio.h
558 rint math.h
559 rintf math.h
560 rintl math.h
561 round math.h
562 roundf math.h
563 roundl math.h
564 scalbln math.h
565 scalblnf math.h
566 scalblnl math.h
567 scalbn math.h
568 scalbnf math.h
569 scalbnl math.h
570 scanf stdio.h
571 setbuf stdio.h
572 setlocale locale.h
573 setvbuf stdio.h
574 signal signal.h
575 signbit math.h
576 sin math.h
577 sinf math.h
578 sinh math.h
579 sinhf math.h
580 sinhl math.h
581 sinl math.h
582 sprintf stdio.h
583 sqrt math.h
584 sqrtf math.h
585 sqrtl math.h
586 srand stdlib.h
587 sscanf stdio.h
588 stderr stdio.h
589 stdin stdio.h
590 stdout stdio.h
591 strcat string.h
592 strchr string.h
593 strcmp string.h
594 strcoll string.h
595 strcpy string.h
596 strcspn string.h
597 strerror string.h
598 strftime time.h
599 strlen string.h
600 strncat string.h
601 strncmp string.h
602 strncpy string.h
603 strpbrk string.h
604 strrchr string.h
605 strspn string.h
606 strstr string.h
607 strtod stdlib.h
608 strtof stdlib.h
609 strtok string.h
610 strtol stdlib.h
611 strtold stdlib.h
612 strtoll stdlib.h
613 strtoul stdlib.h
614 strtoull stdlib.h
615 strxfrm string.h
616 system stdlib.h
617 tan math.h
618 tanf math.h
619 tanh math.h
620 tanhf math.h
621 tanhl math.h
622 tanl math.h
623 tgamma math.h
624 tgammaf math.h
625 tgammal math.h
626 time time.h
627 tmpfile stdio.h
628 tmpnam stdio.h
629 tolower ctype.h
630 toupper ctype.h
631 trunc math.h
632 truncf math.h
633 truncl math.h
634 ungetc stdio.h
635 vfprintf stdio.h
636 vfscanf stdio.h
637 vprintf stdio.h
638 vscanf stdio.h
639 vsprintf stdio.h
640 vsscanf stdio.h
641 UNDEF assert.h
642 UNDEF stdarg.h
644 # Local Variables:
645 # mode: cperl
646 # cperl-indent-level: 4
647 # fill-column: 100
648 # End:
649 # vim: expandtab shiftwidth=4: