dep.pl 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/usr/bin/perl
  2. #
  3. # Walk through source, add labels and make classes
  4. #
  5. #use strict;
  6. my %deplist;
  7. #open class file and write preamble
  8. open(CLASS, ">tommath_class.h") or die "Couldn't open tommath_class.h for writing\n";
  9. print CLASS "#if !(defined(LTM1) && defined(LTM2) && defined(LTM3))\n#if defined(LTM2)\n#define LTM3\n#endif\n#if defined(LTM1)\n#define LTM2\n#endif\n#define LTM1\n\n#if defined(LTM_ALL)\n";
  10. foreach my $filename (glob "bn*.c") {
  11. my $define = $filename;
  12. print "Processing $filename\n";
  13. # convert filename to upper case so we can use it as a define
  14. $define =~ tr/[a-z]/[A-Z]/;
  15. $define =~ tr/\./_/;
  16. print CLASS "#define $define\n";
  17. # now copy text and apply #ifdef as required
  18. my $apply = 0;
  19. open(SRC, "<$filename");
  20. open(OUT, ">tmp");
  21. # first line will be the #ifdef
  22. my $line = <SRC>;
  23. if ($line =~ /include/) {
  24. print OUT $line;
  25. } else {
  26. print OUT "#include <tommath.h>\n#ifdef $define\n$line";
  27. $apply = 1;
  28. }
  29. while (<SRC>) {
  30. if (!($_ =~ /tommath\.h/)) {
  31. print OUT $_;
  32. }
  33. }
  34. if ($apply == 1) {
  35. print OUT "#endif\n";
  36. }
  37. close SRC;
  38. close OUT;
  39. unlink($filename);
  40. rename("tmp", $filename);
  41. }
  42. print CLASS "#endif\n\n";
  43. # now do classes
  44. foreach my $filename (glob "bn*.c") {
  45. open(SRC, "<$filename") or die "Can't open source file!\n";
  46. # convert filename to upper case so we can use it as a define
  47. $filename =~ tr/[a-z]/[A-Z]/;
  48. $filename =~ tr/\./_/;
  49. print CLASS "#if defined($filename)\n";
  50. my $list = $filename;
  51. # scan for mp_* and make classes
  52. while (<SRC>) {
  53. my $line = $_;
  54. while ($line =~ m/(fast_)*(s_)*mp\_[a-z_0-9]*/) {
  55. $line = $';
  56. # now $& is the match, we want to skip over LTM keywords like
  57. # mp_int, mp_word, mp_digit
  58. if (!($& eq "mp_digit") && !($& eq "mp_word") && !($& eq "mp_int")) {
  59. my $a = $&;
  60. $a =~ tr/[a-z]/[A-Z]/;
  61. $a = "BN_" . $a . "_C";
  62. if (!($list =~ /$a/)) {
  63. print CLASS " #define $a\n";
  64. }
  65. $list = $list . "," . $a;
  66. }
  67. }
  68. }
  69. @deplist{$filename} = $list;
  70. print CLASS "#endif\n\n";
  71. close SRC;
  72. }
  73. print CLASS "#ifdef LTM3\n#define LTM_LAST\n#endif\n#include <tommath_superclass.h>\n#include <tommath_class.h>\n#else\n#define LTM_LAST\n#endif\n";
  74. close CLASS;
  75. #now let's make a cool call graph...
  76. open(OUT,">callgraph.txt");
  77. $indent = 0;
  78. foreach (keys %deplist) {
  79. $list = "";
  80. draw_func(@deplist{$_});
  81. print OUT "\n\n";
  82. }
  83. close(OUT);
  84. sub draw_func()
  85. {
  86. my @funcs = split(",", $_[0]);
  87. if ($list =~ /@funcs[0]/) {
  88. return;
  89. } else {
  90. $list = $list . @funcs[0];
  91. }
  92. if ($indent == 0) { }
  93. elsif ($indent >= 1) { print OUT "| " x ($indent - 1) . "+--->"; }
  94. print OUT @funcs[0] . "\n";
  95. shift @funcs;
  96. my $temp = $list;
  97. foreach my $i (@funcs) {
  98. ++$indent;
  99. draw_func(@deplist{$i});
  100. --$indent;
  101. }
  102. $list = $temp;
  103. }