Merge pull request #682 from paperchalice/cmake
[libtomcrypt.git] / helper.pl
blobb24551d02d4af78477905b6d121a13dbfe4be177
1 #!/usr/bin/env perl
3 use strict;
4 use warnings;
6 use Getopt::Long;
7 use File::Find 'find';
8 use File::Basename 'basename';
9 use File::Glob 'bsd_glob';
11 sub read_file {
12 my $f = shift;
13 open my $fh, "<", $f or die "FATAL: read_rawfile() cannot open file '$f': $!";
14 binmode $fh;
15 return do { local $/; <$fh> };
18 sub write_file {
19 my ($f, $data) = @_;
20 die "FATAL: write_file() no data" unless defined $data;
21 open my $fh, ">", $f or die "FATAL: write_file() cannot open file '$f': $!";
22 binmode $fh;
23 print $fh $data or die "FATAL: write_file() cannot write to '$f': $!";
24 close $fh or die "FATAL: write_file() cannot close '$f': $!";
25 return;
28 sub check_source {
29 my @all_files = (bsd_glob("makefile*"), bsd_glob("*.sh"), bsd_glob("*.pl"));
30 find({ wanted=>sub { push @all_files, $_ if -f $_ }, no_chdir=>1 }, qw/src tests demos/);
32 my $fails = 0;
33 for my $file (sort @all_files) {
34 next unless $file =~ /\.(c|h|pl|py|sh)$/ || basename($file) =~ /^makefile/i;
35 my $troubles = {};
36 my $lineno = 1;
37 my $content = read_file($file);
38 push @{$troubles->{crlf_line_end}}, '?' if $content =~ /\r/;
39 for my $l (split /\n/, $content) {
40 push @{$troubles->{merge_conflict}}, $lineno if $l =~ /^(<<<<<<<|=======|>>>>>>>)([^<=>]|$)/;
41 push @{$troubles->{trailing_space}}, $lineno if $l =~ / $/;
42 push @{$troubles->{tab}}, $lineno if $l =~ /\t/ && basename($file) !~ /^makefile/i;
43 push @{$troubles->{non_ascii_char}}, $lineno if $l =~ /[^[:ascii:]]/;
44 push @{$troubles->{cpp_comment}}, $lineno if $file =~ /\.(c|h)$/ && ($l =~ /\s\/\// || $l =~ /\/\/\s/);
45 # in ./src we prefer using XMEMCPY, XMALLOC, XFREE ...
46 push @{$troubles->{unwanted_memcpy}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bmemcpy\s*\(/;
47 push @{$troubles->{unwanted_malloc}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bmalloc\s*\(/;
48 push @{$troubles->{unwanted_realloc}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\brealloc\s*\(/;
49 push @{$troubles->{unwanted_calloc}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bcalloc\s*\(/;
50 push @{$troubles->{unwanted_free}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /[^>.]\bfree\s*\(/;
51 push @{$troubles->{unwanted_memset}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bmemset\s*\(/;
52 push @{$troubles->{unwanted_memcpy}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bmemcpy\s*\(/;
53 push @{$troubles->{unwanted_memmove}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bmemmove\s*\(/;
54 push @{$troubles->{unwanted_memcmp}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bmemcmp\s*\(/;
55 push @{$troubles->{unwanted_strcmp}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bstrcmp\s*\(/;
56 push @{$troubles->{unwanted_strcpy}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bstrcpy\s*\(/;
57 push @{$troubles->{unwanted_strlen}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bstrlen\s*\(/;
58 push @{$troubles->{unwanted_strncpy}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bstrncpy\s*\(/;
59 push @{$troubles->{unwanted_clock}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bclock\s*\(/;
60 push @{$troubles->{unwanted_qsort}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bqsort\s*\(/;
61 push @{$troubles->{sizeof_no_brackets}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bsizeof\s*[^\(]/;
62 if ($file =~ m|src/.*\.c$| &&
63 $file !~ m|src/ciphers/.*\.c$| &&
64 $file !~ m|src/math/.+_desc.c$| &&
65 $file !~ m|src/pk/ec25519/tweetnacl.c$| &&
66 $file !~ m|src/stream/sober128/sober128_stream.c$| &&
67 $l =~ /^static(\s+[a-zA-Z0-9_]+)+\s++([^s][a-zA-Z0-9_]+)\s*\(/) {
68 push @{$troubles->{staticfunc_name}}, "$2";
70 if ($file =~ m|src/.*\.[ch]$| && $l =~ /^\s*#\s*define\s+(_[A-Z_][a-zA-Z0-9_]*)\b/) {
71 my $n = $1;
72 push @{$troubles->{invalid_macro_name}}, "$lineno($n)"
73 unless ($file eq 'src/headers/tomcrypt_cfg.h' && $n eq '__has_builtin') ||
74 ($file eq 'src/headers/tomcrypt_cfg.h' && $n eq '_WIN32_WINNT') ||
75 ($file eq 'src/prngs/rng_get_bytes.c' && $n eq '_WIN32_WINNT');
77 $lineno++;
79 for my $k (sort keys %$troubles) {
80 warn "[$k] $file line:" . join(",", @{$troubles->{$k}}) . "\n";
81 $fails++;
85 warn( $fails > 0 ? "check-source: FAIL $fails\n" : "check-source: PASS\n" );
86 return $fails;
89 sub check_defines {
90 my $fails = 0;
91 my $cust_h = read_file("src/headers/tomcrypt_custom.h");
92 my $cryp_c = read_file("src/misc/crypt/crypt.c");
93 $cust_h =~ s|/\*.*?\*/||sg; # remove comments
94 $cryp_c =~ s|/\*.*?\*/||sg; # remove comments
95 my %def = map { $_ => 1 } map { my $x = $_; $x =~ s/^\s*#define\s+(LTC_\S+).*$/$1/; $x } grep { /^\s*#define\s+LTC_\S+/ } split /\n/, $cust_h;
96 for my $d (sort keys %def) {
97 next if $d =~ /^LTC_(DH\d+|ECC\d+|ECC_\S+|MPI|MUTEX_\S+\(x\)|NO_\S+)$/;
98 warn "$d missing in src/misc/crypt/crypt.c\n" and $fails++ if $cryp_c !~ /\Q$d\E/;
100 warn( $fails > 0 ? "check-defines: FAIL $fails\n" : "check-defines: PASS\n" );
101 return $fails;
104 sub check_descriptor {
105 my $which = shift;
106 my $what = shift;
107 my @src;
108 my @descriptors;
109 find({ wanted => sub { push @src, $_ if $_ =~ /\.c$/ }, no_chdir=>1 }, "./src/${which}/");
110 for my $f (@src) {
111 my @n = map { my $x = $_; $x =~ s/^.*?ltc_${what}_descriptor\s+(\S+).*$/$1/; $x } grep { $_ =~ /ltc_${what}_descriptor/ } split /\n/, read_file($f);
112 push @descriptors, @n if @n;
114 my $fails = 0;
115 for my $d (@descriptors) {
116 for my $f ("./src/misc/crypt/crypt_register_all_${which}.c") {
117 my $txt = read_file($f);
118 warn "$d missing in $f\n" and $fails++ if $txt !~ /\Q$d\E/;
121 for my $d (@descriptors) {
122 for my $f ("./tests/test.c") {
123 my $txt = read_file($f);
124 warn "$d missing in $f\n" and $fails++ if $txt !~ /\Q$d\E/;
127 my $name = sprintf("%-17s", "check-${which}:");
128 warn( $fails > 0 ? "${name}FAIL $fails\n" : "${name}PASS\n" );
129 return $fails;
132 sub check_descriptors {
133 my $fails = 0;
134 $fails = $fails + check_descriptor("ciphers", "cipher");
135 $fails = $fails + check_descriptor("hashes", "hash");
136 $fails = $fails + check_descriptor("prngs", "prng");
137 return $fails;
140 sub check_comments {
141 my $fails = 0;
142 my $first_comment = <<'MARKER';
143 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
144 /* SPDX-License-Identifier: Unlicense */
145 MARKER
146 my @all_files;
147 find({ wanted=> sub { push @all_files, $_ if $_ =~ /\.(c|h)$/ }, no_chdir=>1 }, 'demos', 'src', 'tests');
148 for my $f (@all_files) {
149 my $txt = read_file($f);
150 if ($txt !~ /^\Q$first_comment\E/s) {
151 warn "[first_comment] $f\n";
152 $fails++;
155 warn( $fails > 0 ? "check-comments: FAIL $fails\n" : "check-comments: PASS\n" );
156 return $fails;
159 sub prepare_variable {
160 my ($varname, @list) = @_;
161 my $output = "$varname=";
162 my $len = length($output);
163 foreach my $obj (sort @list) {
164 $len = $len + length $obj;
165 $obj =~ s/\*/\$/;
166 if ($len > 100) {
167 $output .= "\\\n";
168 $len = length $obj;
170 $output .= $obj . ' ';
172 $output =~ s/ $//;
173 return $output;
176 sub prepare_msvc_files_xml {
177 my ($all, $exclude_re, $targets) = @_;
178 my $last = [];
179 my $depth = 2;
181 # sort files in the same order as visual studio (ugly, I know)
182 my @parts = ();
183 for my $orig (@$all) {
184 my $p = $orig;
185 $p =~ s|/|/~|g;
186 $p =~ s|/~([^/]+)$|/$1|g;
187 # now we have: 'src/pk/rsa/rsa_verify_hash.c' > 'src/~pk/~rsa/rsa_verify_hash.c'
188 my @l = map { sprintf "% -99s", $_ } split /\//, $p;
189 push @parts, [ $orig, join(':', @l) ];
191 my @sorted = map { $_->[0] } sort { $a->[1] cmp $b->[1] } @parts;
193 my $files = "<Files>\r\n";
194 for my $full (@sorted) {
195 my @items = split /\//, $full; # split by '/'
196 $full =~ s|/|\\|g; # replace '/' bt '\'
197 shift @items; # drop first one (src)
198 pop @items; # drop last one (filename.ext)
199 my $current = \@items;
200 if (join(':', @$current) ne join(':', @$last)) {
201 my $common = 0;
202 $common++ while ($last->[$common] && $current->[$common] && $last->[$common] eq $current->[$common]);
203 my $back = @$last - $common;
204 if ($back > 0) {
205 $files .= ("\t" x --$depth) . "</Filter>\r\n" for (1..$back);
207 my $fwd = [ @$current ]; splice(@$fwd, 0, $common);
208 for my $i (0..scalar(@$fwd) - 1) {
209 $files .= ("\t" x $depth) . "<Filter\r\n";
210 $files .= ("\t" x $depth) . "\tName=\"$fwd->[$i]\"\r\n";
211 $files .= ("\t" x $depth) . "\t>\r\n";
212 $depth++;
214 $last = $current;
216 $files .= ("\t" x $depth) . "<File\r\n";
217 $files .= ("\t" x $depth) . "\tRelativePath=\"$full\"\r\n";
218 $files .= ("\t" x $depth) . "\t>\r\n";
219 if ($full =~ $exclude_re) {
220 for (@$targets) {
221 $files .= ("\t" x $depth) . "\t<FileConfiguration\r\n";
222 $files .= ("\t" x $depth) . "\t\tName=\"$_\"\r\n";
223 $files .= ("\t" x $depth) . "\t\tExcludedFromBuild=\"true\"\r\n";
224 $files .= ("\t" x $depth) . "\t\t>\r\n";
225 $files .= ("\t" x $depth) . "\t\t<Tool\r\n";
226 $files .= ("\t" x $depth) . "\t\t\tName=\"VCCLCompilerTool\"\r\n";
227 $files .= ("\t" x $depth) . "\t\t\tAdditionalIncludeDirectories=\"\"\r\n";
228 $files .= ("\t" x $depth) . "\t\t\tPreprocessorDefinitions=\"\"\r\n";
229 $files .= ("\t" x $depth) . "\t\t/>\r\n";
230 $files .= ("\t" x $depth) . "\t</FileConfiguration>\r\n";
233 ########### aes_enc "hack" disabled - discussion: https://github.com/libtom/libtomcrypt/pull/158
234 # if ($full eq 'src\ciphers\aes\aes.c') { #hack
235 # my %cmd = (
236 # 'Debug|Win32' => [ 'Debug/aes.obj;Debug/aes_enc.obj', 'cl /nologo /MLd /W3 /Gm /GX /ZI /Od /I &quot;src\headers&quot; /I &quot;..\libtommath&quot; /D &quot;_DEBUG&quot; /D &quot;LTM_DESC&quot; /D &quot;WIN32&quot; /D &quot;_MBCS&quot; /D &quot;_LIB&quot; /D &quot;LTC_SOURCE&quot; /D &quot;USE_LTM&quot; /Fp&quot;Debug/libtomcrypt.pch&quot; /YX /Fo&quot;Debug/&quot; /Fd&quot;Debug/&quot; /FD /GZ /c $(InputPath)&#x0D;&#x0A;cl /nologo /DENCRYPT_ONLY /MLd /W3 /Gm /GX /ZI /Od /I &quot;src\headers&quot; /I &quot;..\libtommath&quot; /D &quot;_DEBUG&quot; /D &quot;LTM_DESC&quot; /D &quot;WIN32&quot; /D &quot;_MBCS&quot; /D &quot;_LIB&quot; /D &quot;LTC_SOURCE&quot; /D &quot;USE_LTM&quot; /Fp&quot;Debug/libtomcrypt.pch&quot; /YX /Fo&quot;Debug/aes_enc.obj&quot; /Fd&quot;Debug/&quot; /FD /GZ /c $(InputPath)&#x0D;&#x0A;' ],
237 # 'Release|Win32' => [ 'Release/aes.obj;Release/aes_enc.obj', 'cl /nologo /MLd /W3 /Gm /GX /ZI /Od /I &quot;src\headers&quot; /I &quot;..\libtommath&quot; /D &quot;_DEBUG&quot; /D &quot;LTM_DESC&quot; /D &quot;WIN32&quot; /D &quot;_MBCS&quot; /D &quot;_LIB&quot; /D &quot;LTC_SOURCE&quot; /D &quot;USE_LTM&quot; /Fp&quot;Release/libtomcrypt.pch&quot; /YX /Fo&quot;Release/&quot; /Fd&quot;Release/&quot; /FD /GZ /c $(InputPath)&#x0D;&#x0A;cl /nologo /DENCRYPT_ONLY /MLd /W3 /Gm /GX /ZI /Od /I &quot;src\headers&quot; /I &quot;..\libtommath&quot; /D &quot;_DEBUG&quot; /D &quot;LTM_DESC&quot; /D &quot;WIN32&quot; /D &quot;_MBCS&quot; /D &quot;_LIB&quot; /D &quot;LTC_SOURCE&quot; /D &quot;USE_LTM&quot; /Fp&quot;Release/libtomcrypt.pch&quot; /YX /Fo&quot;Release/aes_enc.obj&quot; /Fd&quot;Release/&quot; /FD /GZ /c $(InputPath)&#x0D;&#x0A;' ],
238 # );
239 # for (@$targets) {
240 # next unless $cmd{$_};
241 # $files .= ("\t" x $depth) . "\t<FileConfiguration\r\n";
242 # $files .= ("\t" x $depth) . "\t\tName=\"$_\"\r\n";
243 # $files .= ("\t" x $depth) . "\t\t>\r\n";
244 # $files .= ("\t" x $depth) . "\t\t<Tool\r\n";
245 # $files .= ("\t" x $depth) . "\t\t\tName=\"VCCustomBuildTool\"\r\n";
246 # $files .= ("\t" x $depth) . "\t\t\tCommandLine=\"$cmd{$_}[1]\"\r\n";
247 # $files .= ("\t" x $depth) . "\t\t\tOutputs=\"$cmd{$_}[0]\"\r\n";
248 # $files .= ("\t" x $depth) . "\t\t/>\r\n";
249 # $files .= ("\t" x $depth) . "\t</FileConfiguration>\r\n";
252 $files .= ("\t" x $depth) . "</File>\r\n";
254 $files .= ("\t" x --$depth) . "</Filter>\r\n" for (@$last);
255 $files .= "\t</Files>";
256 return $files;
259 sub patch_file {
260 my ($content, @variables) = @_;
261 for my $v (@variables) {
262 if ($v =~ /^([A-Z0-9_]+)\s*=.*$/si) {
263 my $name = $1;
264 $content =~ s/\n\Q$name\E\b.*?[^\\]\n/\n$v\n/s;
266 else {
267 die "patch_file failed: " . substr($v, 0, 30) . "..";
270 return $content;
273 sub version_from_tomcrypt_h {
274 my $h = read_file(shift);
275 if ($h =~ /\n#define\s*SCRYPT\s*"([0-9]+)\.([0-9]+)\.([0-9]+)(\S*)"/s) {
276 return "VERSION_PC=$1.$2.$3", "VERSION_LT=1:1", "VERSION=$1.$2.$3$4", "PROJECT_NUMBER=$1.$2.$3$4";
278 else {
279 die "#define SCRYPT not found in tomcrypt.h";
283 sub make_sources_cmake {
284 my ($list, $pub_headers) = @_;
285 my $output = "set(SOURCES\n";
287 foreach my $obj (sort @$list) {
288 $output .= $obj . "\n";
290 $output .= ")\n\n";
292 if ($pub_headers eq "") {
293 return $output;
296 $output .= "set(PUBLIC_HEADERS\n";
298 foreach my $obj (sort @$pub_headers) {
299 $output .= $obj . "\n";
302 $output .= ")\n\nset(PRIVATE_HEADERS src/headers/tomcrypt_private.h)\n";
303 $output .= "set_property(GLOBAL PROPERTY PUBLIC_HEADERS \$\{PUBLIC_HEADERS\}\)\n\n";
305 return $output;
308 sub process_makefiles {
309 my $write = shift;
310 my $changed_count = 0;
311 my @c = ();
312 find({ no_chdir => 1, wanted => sub { push @c, $_ if -f $_ && $_ =~ /\.c$/ && $_ !~ /tab.c$/ } }, 'src');
313 my @h = ();
314 find({ no_chdir => 1, wanted => sub { push @h, $_ if -f $_ && $_ =~ /\.h$/ && $_ !~ /dh_static.h$/ && $_ !~ /tomcrypt_private.h$/ } }, 'src');
315 my @all = ();
316 find({ no_chdir => 1, wanted => sub { push @all, $_ if -f $_ && $_ =~ /\.(c|h)$/ } }, 'src');
317 my @t = qw();
318 find({ no_chdir => 1, wanted => sub { push @t, $_ if $_ =~ /(common|no_prng|_tests?|test).c$/ } }, 'tests');
320 my @o = sort ('src/ciphers/aes/aes_enc.o', 'src/ciphers/aes/aes_enc_desc.o', map { my $x = $_; $x =~ s/\.c$/.o/; $x } @c);
321 my $var_o = prepare_variable("OBJECTS", @o);
322 my $var_h = prepare_variable("HEADERS_PUB", (sort @h));
323 (my $var_obj = $var_o) =~ s/\.o\b/.obj/sg;
325 my @t_srcs = sort (map { my $x = $_; $x =~ s/^tests\///; $x } @t);
326 my $var_to = prepare_variable("TOBJECTS", sort map { my $x = $_; $x =~ s/\.c$/.o/; $x } @t);
327 (my $var_tobj = $var_to) =~ s/\.o\b/.obj/sg;
329 my @ver_version = version_from_tomcrypt_h("src/headers/tomcrypt.h");
331 # update MSVC project files
332 my $msvc_files = prepare_msvc_files_xml(\@all, qr/tab\.c$/, ['Debug|Win32', 'Release|Win32', 'Debug|x64', 'Release|x64']);
333 for my $m (qw/libtomcrypt_VS2008.vcproj/) {
334 my $old = read_file($m);
335 my $new = $old;
336 $new =~ s|<Files>.*</Files>|$msvc_files|s;
337 if ($old ne $new) {
338 write_file($m, $new) if $write;
339 warn "changed: $m\n";
340 $changed_count++;
344 # update OBJECTS + HEADERS in makefile*
345 for my $m (qw/ makefile makefile.shared makefile.unix makefile.mingw makefile.msvc makefile_include.mk doc\/Doxyfile sources.cmake tests\/sources.cmake /) {
346 my $old = read_file($m);
347 my $new = $m eq 'makefile.msvc' ? patch_file($old, $var_obj, $var_h, $var_tobj, @ver_version)
348 : $m eq 'sources.cmake' ? make_sources_cmake(\@all, \@h)
349 : $m eq 'tests/sources.cmake' ? make_sources_cmake(\@t_srcs, "")
350 : patch_file($old, $var_o, $var_h, $var_to, @ver_version);
352 if ($old ne $new) {
353 write_file($m, $new) if $write;
354 warn "changed: $m\n";
355 $changed_count++;
359 if ($write) {
360 return 0; # no failures
362 else {
363 warn( $changed_count > 0 ? "check-makefiles: FAIL $changed_count\n" : "check-makefiles: PASS\n" );
364 return $changed_count;
368 sub die_usage {
369 die <<"MARKER";
370 usage: $0 -s OR $0 --check-source
371 $0 -c OR $0 --check-descriptors
372 $0 -d OR $0 --check-defines
373 $0 -o OR $0 --check-comments
374 $0 -m OR $0 --check-makefiles
375 $0 -a OR $0 --check-all
376 $0 -u OR $0 --update-makefiles
377 $0 --fixupind crypt.ind
378 MARKER
381 GetOptions( "s|check-source" => \my $check_source,
382 "c|check-descriptors" => \my $check_descriptors,
383 "d|check-defines" => \my $check_defines,
384 "o|check-comments" => \my $check_comments,
385 "m|check-makefiles" => \my $check_makefiles,
386 "a|check-all" => \my $check_all,
387 "u|update-makefiles" => \my $update_makefiles,
388 "f|fixupind=s" => \my $fixupind,
389 "h|help" => \my $help
390 ) or die_usage;
392 if ($fixupind) {
393 my $txt = read_file($fixupind);
394 $txt =~ s/^([^\n]*\n)/$1\n\\addcontentsline{toc}{chapter}{Index}\n/s;
395 write_file($fixupind, $txt);
396 exit 0;
399 my $failure;
400 $failure ||= check_source() if $check_all || $check_source;
401 $failure ||= check_defines() if $check_all || $check_defines;
402 $failure ||= check_descriptors() if $check_all || $check_descriptors;
403 $failure ||= check_comments() if $check_all || $check_comments;
404 $failure ||= process_makefiles(0) if $check_all || $check_makefiles;
405 $failure ||= process_makefiles(1) if $update_makefiles;
407 die_usage unless defined $failure;
408 exit $failure ? 1 : 0;