helper.pl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. #!/usr/bin/env perl
  2. use strict;
  3. use warnings;
  4. use Getopt::Long;
  5. use File::Find 'find';
  6. use File::Basename 'basename';
  7. use File::Glob 'bsd_glob';
  8. sub read_file {
  9. my $f = shift;
  10. open my $fh, "<", $f or die "FATAL: read_rawfile() cannot open file '$f': $!";
  11. binmode $fh;
  12. return do { local $/; <$fh> };
  13. }
  14. sub write_file {
  15. my ($f, $data) = @_;
  16. die "FATAL: write_file() no data" unless defined $data;
  17. open my $fh, ">", $f or die "FATAL: write_file() cannot open file '$f': $!";
  18. binmode $fh;
  19. print $fh $data or die "FATAL: write_file() cannot write to '$f': $!";
  20. close $fh or die "FATAL: write_file() cannot close '$f': $!";
  21. return;
  22. }
  23. sub sanitize_comments {
  24. my($content) = @_;
  25. $content =~ s{/\*(.*?)\*/}{my $x=$1; $x =~ s/\w/x/g; "/*$x*/";}egs;
  26. return $content;
  27. }
  28. sub check_source {
  29. my @all_files = (
  30. bsd_glob("Makefile*"),
  31. bsd_glob("*.{h,c,sh,pl}"),
  32. bsd_glob("*/*.{h,c,sh,pl}"),
  33. );
  34. my $fails = 0;
  35. for my $file (sort @all_files) {
  36. my $troubles = {};
  37. my $lineno = 1;
  38. my $content = read_file($file);
  39. $content = sanitize_comments $content;
  40. push @{$troubles->{crlf_line_end}}, '?' if $content =~ /\r/;
  41. for my $l (split /\n/, $content) {
  42. push @{$troubles->{merge_conflict}}, $lineno if $l =~ /^(<<<<<<<|=======|>>>>>>>)([^<=>]|$)/;
  43. push @{$troubles->{trailing_space}}, $lineno if $l =~ / $/;
  44. push @{$troubles->{tab}}, $lineno if $l =~ /\t/ && basename($file) !~ /^makefile/i;
  45. push @{$troubles->{non_ascii_char}}, $lineno if $l =~ /[^[:ascii:]]/;
  46. push @{$troubles->{cpp_comment}}, $lineno if $file =~ /\.(c|h)$/ && ($l =~ /\s\/\// || $l =~ /\/\/\s/);
  47. # we prefer using XMALLOC, XFREE, XREALLOC, XCALLOC ...
  48. push @{$troubles->{unwanted_malloc}}, $lineno if $file =~ /^[^\/]+\.c$/ && $l =~ /\bmalloc\s*\(/;
  49. push @{$troubles->{unwanted_realloc}}, $lineno if $file =~ /^[^\/]+\.c$/ && $l =~ /\brealloc\s*\(/;
  50. push @{$troubles->{unwanted_calloc}}, $lineno if $file =~ /^[^\/]+\.c$/ && $l =~ /\bcalloc\s*\(/;
  51. push @{$troubles->{unwanted_free}}, $lineno if $file =~ /^[^\/]+\.c$/ && $l =~ /\bfree\s*\(/;
  52. # and we probably want to also avoid the following
  53. push @{$troubles->{unwanted_memcpy}}, $lineno if $file =~ /^[^\/]+\.c$/ && $l =~ /\bmemcpy\s*\(/;
  54. push @{$troubles->{unwanted_memset}}, $lineno if $file =~ /^[^\/]+\.c$/ && $l =~ /\bmemset\s*\(/;
  55. push @{$troubles->{unwanted_memcpy}}, $lineno if $file =~ /^[^\/]+\.c$/ && $l =~ /\bmemcpy\s*\(/;
  56. push @{$troubles->{unwanted_memmove}}, $lineno if $file =~ /^[^\/]+\.c$/ && $l =~ /\bmemmove\s*\(/;
  57. push @{$troubles->{unwanted_memcmp}}, $lineno if $file =~ /^[^\/]+\.c$/ && $l =~ /\bmemcmp\s*\(/;
  58. push @{$troubles->{unwanted_strcmp}}, $lineno if $file =~ /^[^\/]+\.c$/ && $l =~ /\bstrcmp\s*\(/;
  59. push @{$troubles->{unwanted_strcpy}}, $lineno if $file =~ /^[^\/]+\.c$/ && $l =~ /\bstrcpy\s*\(/;
  60. push @{$troubles->{unwanted_strncpy}}, $lineno if $file =~ /^[^\/]+\.c$/ && $l =~ /\bstrncpy\s*\(/;
  61. push @{$troubles->{unwanted_clock}}, $lineno if $file =~ /^[^\/]+\.c$/ && $l =~ /\bclock\s*\(/;
  62. push @{$troubles->{unwanted_qsort}}, $lineno if $file =~ /^[^\/]+\.c$/ && $l =~ /\bqsort\s*\(/;
  63. push @{$troubles->{sizeof_no_brackets}}, $lineno if $file =~ /^[^\/]+\.c$/ && $l =~ /\bsizeof\s*[^\(]/;
  64. if ($file =~ m|^[^\/]+\.c$| && $l =~ /^static(\s+[a-zA-Z0-9_]+)+\s+([a-zA-Z0-9_]+)\s*\(/) {
  65. my $funcname = $2;
  66. # static functions should start with s_
  67. push @{$troubles->{staticfunc_name}}, "$lineno($funcname)" if $funcname !~ /^s_/;
  68. }
  69. $lineno++;
  70. }
  71. for my $k (sort keys %$troubles) {
  72. warn "[$k] $file line:" . join(",", @{$troubles->{$k}}) . "\n";
  73. $fails++;
  74. }
  75. }
  76. warn( $fails > 0 ? "check-source: FAIL $fails\n" : "check-source: PASS\n" );
  77. return $fails;
  78. }
  79. sub check_comments {
  80. my $fails = 0;
  81. my $first_comment = <<'MARKER';
  82. /* LibTomMath, multiple-precision integer library -- Tom St Denis */
  83. /* SPDX-License-Identifier: Unlicense */
  84. MARKER
  85. #my @all_files = (bsd_glob("*.{h,c}"), bsd_glob("*/*.{h,c}"));
  86. my @all_files = (bsd_glob("*.{h,c}"));
  87. for my $f (@all_files) {
  88. my $txt = read_file($f);
  89. if ($txt !~ /\Q$first_comment\E/s) {
  90. warn "[first_comment] $f\n";
  91. $fails++;
  92. }
  93. }
  94. warn( $fails > 0 ? "check-comments: FAIL $fails\n" : "check-comments: PASS\n" );
  95. return $fails;
  96. }
  97. sub check_doc {
  98. my $fails = 0;
  99. my $tex = read_file('doc/bn.tex');
  100. my $tmh = read_file('tommath.h');
  101. my @functions = $tmh =~ /\n\s*[a-zA-Z0-9_* ]+?(mp_[a-z0-9_]+)\s*\([^\)]+\)\s*;/sg;
  102. my @macros = $tmh =~ /\n\s*#define\s+([a-z0-9_]+)\s*\([^\)]+\)/sg;
  103. for my $n (sort @functions) {
  104. (my $nn = $n) =~ s/_/\\_/g; # mp_sub_d >> mp\_sub\_d
  105. if ($tex !~ /index\Q{$nn}\E/) {
  106. warn "[missing_doc_for_function] $n\n";
  107. $fails++
  108. }
  109. }
  110. for my $n (sort @macros) {
  111. (my $nn = $n) =~ s/_/\\_/g; # mp_iszero >> mp\_iszero
  112. if ($tex !~ /index\Q{$nn}\E/) {
  113. warn "[missing_doc_for_macro] $n\n";
  114. $fails++
  115. }
  116. }
  117. warn( $fails > 0 ? "check_doc: FAIL $fails\n" : "check-doc: PASS\n" );
  118. return $fails;
  119. }
  120. sub prepare_variable {
  121. my ($varname, @list) = @_;
  122. my $output = "$varname=";
  123. my $len = length($output);
  124. foreach my $obj (sort @list) {
  125. $len = $len + length $obj;
  126. $obj =~ s/\*/\$/;
  127. if ($len > 100) {
  128. $output .= "\\\n";
  129. $len = length $obj;
  130. }
  131. $output .= $obj . ' ';
  132. }
  133. $output =~ s/ $//;
  134. return $output;
  135. }
  136. sub prepare_msvc_files_xml {
  137. my ($all, $exclude_re, $targets) = @_;
  138. my $last = [];
  139. my $depth = 2;
  140. # sort files in the same order as visual studio (ugly, I know)
  141. my @parts = ();
  142. for my $orig (@$all) {
  143. my $p = $orig;
  144. $p =~ s|/|/~|g;
  145. $p =~ s|/~([^/]+)$|/$1|g;
  146. my @l = map { sprintf "% -99s", $_ } split /\//, $p;
  147. push @parts, [ $orig, join(':', @l) ];
  148. }
  149. my @sorted = map { $_->[0] } sort { $a->[1] cmp $b->[1] } @parts;
  150. my $files = "<Files>\r\n";
  151. for my $full (@sorted) {
  152. my @items = split /\//, $full; # split by '/'
  153. $full =~ s|/|\\|g; # replace '/' bt '\'
  154. shift @items; # drop first one (src)
  155. pop @items; # drop last one (filename.ext)
  156. my $current = \@items;
  157. if (join(':', @$current) ne join(':', @$last)) {
  158. my $common = 0;
  159. $common++ while ($last->[$common] && $current->[$common] && $last->[$common] eq $current->[$common]);
  160. my $back = @$last - $common;
  161. if ($back > 0) {
  162. $files .= ("\t" x --$depth) . "</Filter>\r\n" for (1..$back);
  163. }
  164. my $fwd = [ @$current ]; splice(@$fwd, 0, $common);
  165. for my $i (0..scalar(@$fwd) - 1) {
  166. $files .= ("\t" x $depth) . "<Filter\r\n";
  167. $files .= ("\t" x $depth) . "\tName=\"$fwd->[$i]\"\r\n";
  168. $files .= ("\t" x $depth) . "\t>\r\n";
  169. $depth++;
  170. }
  171. $last = $current;
  172. }
  173. $files .= ("\t" x $depth) . "<File\r\n";
  174. $files .= ("\t" x $depth) . "\tRelativePath=\"$full\"\r\n";
  175. $files .= ("\t" x $depth) . "\t>\r\n";
  176. if ($full =~ $exclude_re) {
  177. for (@$targets) {
  178. $files .= ("\t" x $depth) . "\t<FileConfiguration\r\n";
  179. $files .= ("\t" x $depth) . "\t\tName=\"$_\"\r\n";
  180. $files .= ("\t" x $depth) . "\t\tExcludedFromBuild=\"true\"\r\n";
  181. $files .= ("\t" x $depth) . "\t\t>\r\n";
  182. $files .= ("\t" x $depth) . "\t\t<Tool\r\n";
  183. $files .= ("\t" x $depth) . "\t\t\tName=\"VCCLCompilerTool\"\r\n";
  184. $files .= ("\t" x $depth) . "\t\t\tAdditionalIncludeDirectories=\"\"\r\n";
  185. $files .= ("\t" x $depth) . "\t\t\tPreprocessorDefinitions=\"\"\r\n";
  186. $files .= ("\t" x $depth) . "\t\t/>\r\n";
  187. $files .= ("\t" x $depth) . "\t</FileConfiguration>\r\n";
  188. }
  189. }
  190. $files .= ("\t" x $depth) . "</File>\r\n";
  191. }
  192. $files .= ("\t" x --$depth) . "</Filter>\r\n" for (@$last);
  193. $files .= "\t</Files>";
  194. return $files;
  195. }
  196. sub patch_file {
  197. my ($content, @variables) = @_;
  198. for my $v (@variables) {
  199. if ($v =~ /^([A-Z0-9_]+)\s*=.*$/si) {
  200. my $name = $1;
  201. $content =~ s/\n\Q$name\E\b.*?[^\\]\n/\n$v\n/s;
  202. }
  203. else {
  204. die "patch_file failed: " . substr($v, 0, 30) . "..";
  205. }
  206. }
  207. return $content;
  208. }
  209. sub process_makefiles {
  210. my $write = shift;
  211. my $changed_count = 0;
  212. my @o = map { my $x = $_; $x =~ s/\.c$/.o/; $x } bsd_glob("*.c");
  213. my @all = bsd_glob("*.{c,h}");
  214. my $var_o = prepare_variable("OBJECTS", @o);
  215. (my $var_obj = $var_o) =~ s/\.o\b/.obj/sg;
  216. # update OBJECTS + HEADERS in makefile*
  217. for my $m (qw/ Makefile.in /) {
  218. my $old = read_file($m);
  219. my $new = $m eq 'makefile.msvc' ? patch_file($old, $var_obj)
  220. : patch_file($old, $var_o);
  221. if ($old ne $new) {
  222. write_file($m, $new) if $write;
  223. warn "changed: $m\n";
  224. $changed_count++;
  225. }
  226. }
  227. if ($write) {
  228. return 0; # no failures
  229. }
  230. else {
  231. warn( $changed_count > 0 ? "check-makefiles: FAIL $changed_count\n" : "check-makefiles: PASS\n" );
  232. return $changed_count;
  233. }
  234. }
  235. sub draw_func
  236. {
  237. my ($deplist, $depmap, $out, $indent, $funcslist) = @_;
  238. my @funcs = split ',', $funcslist;
  239. # try this if you want to have a look at a minimized version of the callgraph without all the trivial functions
  240. #if ($deplist =~ /$funcs[0]/ || $funcs[0] =~ /BN_MP_(ADD|SUB|CLEAR|CLEAR_\S+|DIV|MUL|COPY|ZERO|GROW|CLAMP|INIT|INIT_\S+|SET|ABS|CMP|CMP_D|EXCH)_C/) {
  241. if ($deplist =~ /$funcs[0]/) {
  242. return $deplist;
  243. } else {
  244. $deplist = $deplist . $funcs[0];
  245. }
  246. if ($indent == 0) {
  247. } elsif ($indent >= 1) {
  248. print {$out} '| ' x ($indent - 1) . '+--->';
  249. }
  250. print {$out} $funcs[0] . "\n";
  251. shift @funcs;
  252. my $olddeplist = $deplist;
  253. foreach my $i (@funcs) {
  254. $deplist = draw_func($deplist, $depmap, $out, $indent + 1, ${$depmap}{$i}) if exists ${$depmap}{$i};
  255. }
  256. return $olddeplist;
  257. }
  258. sub update_dep
  259. {
  260. #open class file and write preamble
  261. open(my $class, '>', 'tommath_class.h') or die "Couldn't open tommath_class.h for writing\n";
  262. print {$class} << 'EOS';
  263. /* LibTomMath, multiple-precision integer library -- Tom St Denis */
  264. /* SPDX-License-Identifier: Unlicense */
  265. #if !(defined(LTM1) && defined(LTM2) && defined(LTM3))
  266. #define LTM_INSIDE
  267. #if defined(LTM2)
  268. # define LTM3
  269. #endif
  270. #if defined(LTM1)
  271. # define LTM2
  272. #endif
  273. #define LTM1
  274. #if defined(LTM_ALL)
  275. EOS
  276. foreach my $filename (glob 'bn*.c') {
  277. my $define = $filename;
  278. print "Processing $filename\n";
  279. # convert filename to upper case so we can use it as a define
  280. $define =~ tr/[a-z]/[A-Z]/;
  281. $define =~ tr/\./_/;
  282. print {$class} "# define $define\n";
  283. # now copy text and apply #ifdef as required
  284. my $apply = 0;
  285. open(my $src, '<', $filename);
  286. open(my $out, '>', 'tmp');
  287. # first line will be the #ifdef
  288. my $line = <$src>;
  289. if ($line =~ /include/) {
  290. print {$out} $line;
  291. } else {
  292. print {$out} << "EOS";
  293. #include "tommath_private.h"
  294. #ifdef $define
  295. /* LibTomMath, multiple-precision integer library -- Tom St Denis */
  296. /* SPDX-License-Identifier: Unlicense */
  297. $line
  298. EOS
  299. $apply = 1;
  300. }
  301. while (<$src>) {
  302. if ($_ !~ /tommath\.h/) {
  303. print {$out} $_;
  304. }
  305. }
  306. if ($apply == 1) {
  307. print {$out} "#endif\n";
  308. }
  309. close $src;
  310. close $out;
  311. unlink $filename;
  312. rename 'tmp', $filename;
  313. }
  314. print {$class} "#endif\n#endif\n";
  315. # now do classes
  316. my %depmap;
  317. foreach my $filename (glob 'bn*.c') {
  318. my $content;
  319. if ($filename =~ "bn_deprecated.c") {
  320. open(my $src, '<', $filename) or die "Can't open source file!\n";
  321. read $src, $content, -s $src;
  322. close $src;
  323. } else {
  324. my $cc = $ENV{'CC'} || 'gcc';
  325. $content = `$cc -E -x c -DLTM_ALL $filename`;
  326. $content =~ s/^# 1 "$filename".*?^# 2 "$filename"//ms;
  327. }
  328. # convert filename to upper case so we can use it as a define
  329. $filename =~ tr/[a-z]/[A-Z]/;
  330. $filename =~ tr/\./_/;
  331. print {$class} "#if defined($filename)\n";
  332. my $list = $filename;
  333. # strip comments
  334. $content =~ s{/\*.*?\*/}{}gs;
  335. # scan for mp_* and make classes
  336. my @deps = ();
  337. foreach my $line (split /\n/, $content) {
  338. while ($line =~ /(fast_)?(s_)?mp\_[a-z_0-9]*((?=\;)|(?=\())|(?<=\()mp\_[a-z_0-9]*(?=\()/g) {
  339. my $a = $&;
  340. next if $a eq "mp_err";
  341. $a =~ tr/[a-z]/[A-Z]/;
  342. $a = 'BN_' . $a . '_C';
  343. push @deps, $a;
  344. }
  345. }
  346. @deps = sort(@deps);
  347. foreach my $a (@deps) {
  348. if ($list !~ /$a/) {
  349. print {$class} "# define $a\n";
  350. }
  351. $list = $list . ',' . $a;
  352. }
  353. $depmap{$filename} = $list;
  354. print {$class} "#endif\n\n";
  355. }
  356. print {$class} << 'EOS';
  357. #ifdef LTM_INSIDE
  358. #undef LTM_INSIDE
  359. #ifdef LTM3
  360. # define LTM_LAST
  361. #endif
  362. #include "tommath_superclass.h"
  363. #include "tommath_class.h"
  364. #else
  365. # define LTM_LAST
  366. #endif
  367. EOS
  368. close $class;
  369. #now let's make a cool call graph...
  370. open(my $out, '>', 'callgraph.txt');
  371. foreach (sort keys %depmap) {
  372. draw_func("", \%depmap, $out, 0, $depmap{$_});
  373. print {$out} "\n\n";
  374. }
  375. close $out;
  376. return 0;
  377. }
  378. sub generate_def {
  379. my @files = split /\n/, `git ls-files`;
  380. @files = grep(/\.c/, @files);
  381. @files = map { my $x = $_; $x =~ s/^bn_|\.c$//g; $x; } @files;
  382. @files = grep(!/mp_radix_smap/, @files);
  383. push(@files, qw(mp_set_int mp_set_long mp_set_long_long mp_get_int mp_get_long mp_get_long_long mp_init_set_int));
  384. my $files = join("\n ", sort(grep(/^mp_/, @files)));
  385. write_file "tommath.def", "; libtommath
  386. ;
  387. ; Use this command to produce a 32-bit .lib file, for use in any MSVC version
  388. ; lib -machine:X86 -name:libtommath.dll -def:tommath.def -out:tommath.lib
  389. ; Use this command to produce a 64-bit .lib file, for use in any MSVC version
  390. ; lib -machine:X64 -name:libtommath.dll -def:tommath.def -out:tommath.lib
  391. ;
  392. EXPORTS
  393. $files
  394. ";
  395. return 0;
  396. }
  397. sub die_usage {
  398. die <<"MARKER";
  399. usage: $0 -s OR $0 --check-source
  400. $0 -o OR $0 --check-comments
  401. $0 -m OR $0 --check-makefiles
  402. $0 -a OR $0 --check-all
  403. $0 -u OR $0 --update-files
  404. MARKER
  405. }
  406. GetOptions( "s|check-source" => \my $check_source,
  407. "o|check-comments" => \my $check_comments,
  408. "m|check-makefiles" => \my $check_makefiles,
  409. "d|check-doc" => \my $check_doc,
  410. "a|check-all" => \my $check_all,
  411. "u|update-files" => \my $update_files,
  412. "h|help" => \my $help
  413. ) or die_usage;
  414. my $failure;
  415. $failure ||= check_source() if $check_all || $check_source;
  416. $failure ||= check_comments() if $check_all || $check_comments;
  417. $failure ||= check_doc() if $check_doc; # temporarily excluded from --check-all
  418. $failure ||= process_makefiles(0) if $check_all || $check_makefiles;
  419. $failure ||= process_makefiles(1) if $update_files;
  420. $failure ||= update_dep() if $update_files;
  421. $failure ||= generate_def() if $update_files;
  422. die_usage unless defined $failure;
  423. exit $failure ? 1 : 0;