Make sure all tests are actually run, plus some fixups for things that
[openssl.git] / util / pl / unix.pl
1 #!/usr/local/bin/perl
2 #
3 # unix.pl - the standard unix makefile stuff.
4 #
5
6 $o='/';
7 $cp='/bin/cp';
8 $rm='/bin/rm -f';
9
10 # C compiler stuff
11
12 if ($gcc)
13         {
14         $cc='gcc';
15         if ($debug)
16                 { $cflags="-g2 -ggdb"; }
17         else
18                 { $cflags="-O3 -fomit-frame-pointer"; }
19         }
20 else
21         {
22         $cc='cc';
23         if ($debug)
24                 { $cflags="-g"; }
25         else
26                 { $cflags="-O"; }
27         }
28 $obj='.o';
29 $ofile='-o ';
30
31 # EXE linking stuff
32 $link='${CC}';
33 $lflags='${CFLAG}';
34 $efile='-o ';
35 $exep='';
36 $ex_libs="";
37
38 # static library stuff
39 $mklib='ar r';
40 $mlflags='';
41 $ranlib=&which("ranlib") or $ranlib="true";
42 $plib='lib';
43 $libp=".a";
44 $shlibp=".a";
45 $lfile='';
46
47 $asm='as';
48 $afile='-o ';
49 $bn_asm_obj="";
50 $bn_asm_src="";
51 $des_enc_obj="";
52 $des_enc_src="";
53 $bf_enc_obj="";
54 $bf_enc_src="";
55
56 %perl1 = (
57           'md5-x86_64' => 'crypto/md5',
58           'x86_64-mont' => 'crypto/bn',
59           'x86_64-mont5' => 'crypto/bn',
60           'x86_64-gf2m' => 'crypto/bn',
61           'modexp512-x86_64' => 'crypto/bn',
62           'aes-x86_64' => 'crypto/aes',
63           'vpaes-x86_64' => 'crypto/aes',
64           'bsaes-x86_64' => 'crypto/aes',
65           'aesni-x86_64' => 'crypto/aes',
66           'aesni-sha1-x86_64' => 'crypto/aes',
67           'sha1-x86_64' => 'crypto/sha',
68           'e_padlock-x86_64' => 'engines',
69           'rc4-x86_64' => 'crypto/rc4',
70           'rc4-md5-x86_64' => 'crypto/rc4',
71           'ghash-x86_64' => 'crypto/modes',
72           'aesni-gcm-x86_64' => 'crypto/modes',
73          );
74
75 # If I were feeling more clever, these could probably be extracted
76 # from makefiles.
77 sub platform_perlasm_compile_target
78         {
79         local($target, $source, $bname) = @_;
80
81         for $p (keys %perl1)
82                 {
83                 if ($target eq "\$(OBJ_D)/$p.o")
84                         {
85                         return << "EOF";
86 \$(TMP_D)/$p.s: $perl1{$p}/asm/$p.pl
87         \$(PERL) $perl1{$p}/asm/$p.pl \$(PERLASM_SCHEME) > \$@
88 EOF
89                         }
90                 }
91         if ($target eq '$(OBJ_D)/x86_64cpuid.o')
92                 {
93                 return << 'EOF';
94 $(TMP_D)/x86_64cpuid.s: crypto/x86_64cpuid.pl
95         $(PERL) crypto/x86_64cpuid.pl $(PERLASM_SCHEME) > $@
96 EOF
97                 }
98         elsif ($target eq '$(OBJ_D)/sha256-x86_64.o')
99                 {
100                 return << 'EOF';
101 $(TMP_D)/sha256-x86_64.s: crypto/sha/asm/sha512-x86_64.pl
102         $(PERL) crypto/sha/asm/sha512-x86_64.pl $(PERLASM_SCHEME) $@
103 EOF
104                 }
105         elsif ($target eq '$(OBJ_D)/sha512-x86_64.o')
106                 {
107                 return << 'EOF';
108 $(TMP_D)/sha512-x86_64.s: crypto/sha/asm/sha512-x86_64.pl
109         $(PERL) crypto/sha/asm/sha512-x86_64.pl $(PERLASM_SCHEME) $@
110 EOF
111                 }
112         elsif ($target eq '$(OBJ_D)/sha512-x86_64.o')
113                 {
114                 return << 'EOF';
115 $(TMP_D)/sha512-x86_64.s: crypto/sha/asm/sha512-x86_64.pl
116         $(PERL) crypto/sha/asm/sha512-x86_64.pl $(PERLASM_SCHEME) $@
117 EOF
118                 }
119
120         die $target;
121         }
122
123 sub special_compile_target
124         {
125         local($target) = @_;
126
127         if ($target eq 'crypto/bn/x86_64-gcc')
128                 {
129                 return << "EOF";
130 \$(TMP_D)/x86_64-gcc.o: crypto/bn/asm/x86_64-gcc.c
131         \$(CC) \$(CFLAGS) -c -o \$@ crypto/bn/asm/x86_64-gcc.c
132 EOF
133                 }
134         return undef;
135         }
136
137 sub do_lib_rule
138         {
139         local($obj,$target,$name,$shlib)=@_;
140         local($ret,$_,$Name);
141
142         $target =~ s/\//$o/g if $o ne '/';
143         $target="$target";
144         ($Name=$name) =~ tr/a-z/A-Z/;
145
146         $ret.="$target: \$(${Name}OBJ)\n";
147         $ret.="\t\$(RM) $target\n";
148         $ret.="\t\$(MKLIB) $target \$(${Name}OBJ)\n";
149         $ret.="\t\$(RANLIB) $target\n\n";
150         }
151
152 sub do_link_rule
153         {
154         local($target,$files,$dep_libs,$libs)=@_;
155         local($ret,$_);
156
157         $file =~ s/\//$o/g if $o ne '/';
158         $n=&bname($target);
159         $ret.="$target: $files $dep_libs\n";
160         $ret.="\t\$(LINK) ${efile}$target \$(LFLAGS) $files $libs\n\n";
161         return($ret);
162         }
163
164 sub which
165         {
166         my ($name)=@_;
167         my $path;
168         foreach $path (split /:/, $ENV{PATH})
169                 {
170                 if (-x "$path/$name")
171                         {
172                         return "$path/$name";
173                         }
174                 }
175         }
176
177 sub fixtests
178   {
179   my ($str, $tests) = @_;
180
181   foreach my $t (keys %$tests)
182     {
183     $str =~ s/(\.\/)?\$\($t\)/\$(TEST_D)\/$tests->{$t}/g;
184     }
185
186   return $str;
187   }
188
189 sub fixdeps
190   {
191   my ($str) = @_;
192
193   my @t = split(/\s+/, $str);
194   $str = '';
195   foreach my $t (@t)
196     {
197     $str .= ' ' if $str ne '';
198     if ($t =~ /^[^\/]+$/)
199       {
200       $str .= '$(TEST_D)/' . $t;
201       }
202     else
203       {
204       $str .= $t;
205       }
206     }
207
208   return $str;
209   }
210
211 sub fixrules
212   {
213   my ($str) = @_;
214
215   # Compatible with -j...
216   $str =~ s/^(\s+@?)/$1cd \$(TEST_D) && /;
217   return $str;
218
219   # Compatible with not -j.
220   my @t = split("\n", $str);
221   $str = '';
222   my $prev;
223   foreach my $t (@t)
224     {
225     $t =~ s/^\s+//;
226     if (!$prev)
227       {
228       if ($t =~ /^@/)
229         {
230         $t =~ s/^@/\@cd \$(TEST_D) && /;
231         }
232       elsif ($t !~ /^\s*#/)
233         {
234         $t = 'cd $(TEST_D) && ' . $t;
235         }
236       }
237     $str .= "\t$t\n";
238     $prev = $t =~/\\$/;
239     }
240   return $str;
241 }
242
243 sub copy_scripts
244   {
245   my ($sed, $src, @targets) = @_;
246
247   my $s = '';
248   foreach my $t (@targets)
249     {
250     # Copy first so we get file modes...
251     $s .= "\$(TEST_D)/$t: \$(SRC_D)/$src/$t\n\tcp \$(SRC_D)/$src/$t \$(TEST_D)/$t\n";
252     $s .= "\tsed -e 's/\\.\\.\\/apps/..\\/\$(OUT_D)/' -e 's/\\.\\.\\/util/..\\/\$(TEST_D)/' < \$(SRC_D)/$src/$t > \$(TEST_D)/$t\n" if $sed;
253     $s .= "\n";
254     }
255   return $s;
256   }
257
258 sub get_tests
259   {
260   my ($makefile) = @_;
261
262   open(M, $makefile) || die "Can't open $makefile: $!";
263   my %targets;
264   my %deps;
265   my %tests;
266   my %alltests;
267   while (my $line = <M>)
268     {
269     chomp $line;
270     while ($line =~ /^(.*)\\$/)
271       {
272       $line = $1 . <M>;
273       }
274
275     if ($line =~ /^alltests:(.*)$/)
276       {
277       my @t = split(/\s+/, $1);
278       foreach my $t (@t)
279         {
280         $targets{$t} = '';
281         $alltests{$t} = undef;
282         }
283       }
284
285     if (($line =~ /^(?<t>\S+):(?<d>.*)$/ && exists $targets{$1})
286         || $line =~ /^(?<t>test_(ss|gen) .*):(?<d>.*)/)
287       {
288       my $t = $+{t};
289       $deps{$t} = $+{d};
290       $deps{$t} =~ s/#.*$//;
291       for (;;)
292         {
293         $line = <M>;
294         chomp $line;
295         last if $line eq '';
296         $targets{$t} .= "$line\n";
297         }
298       next;
299       }
300
301     if ($line =~ /^(\S+TEST)=\s*(\S+)$/)
302       {
303       $tests{$1} = $2;
304       next;
305       }
306     }
307
308   delete $alltests{test_jpake} if $no_jpake;
309   delete $targets{test_ige} if $no_ige;
310   delete $alltests{test_md2} if $no_md2;
311   delete $alltests{test_rc5} if $no_rc5;
312
313   my $tests;
314   foreach my $t (keys %tests)
315     {
316     $tests .= "$t = $tests{$t}\n";
317     }
318
319   my $each;
320   foreach my $t (keys %targets)
321     {
322     next if $t eq '';
323
324     my $d = $deps{$t};
325     $d =~ s/\.\.\/apps/\$(BIN_D)/g;
326     $d =~ s/\.\.\/util/\$(TEST_D)/g;
327     $d = fixtests($d, \%tests);
328     $d = fixdeps($d);
329
330     my $r = $targets{$t};
331     $r =~ s/\.\.\/apps/..\/\$(BIN_D)/g;
332     $r =~ s/\.\.\/util/..\/\$(TEST_D)/g;
333     $r =~ s/\.\.\/(\S+)/\$(SRC_D)\/$1/g;
334     $r = fixrules($r);
335
336     next if $r eq '';
337
338     $t =~ s/\s+/ \$(TEST_D)\//g;
339
340     $each .= "$t: test_scripts $d\n\t\@echo '$t test started'\n$r\t\@echo '$t test done'\n\n";
341     }
342
343   # FIXME: Might be a clever way to figure out what needs copying
344   my @copies = ( 'bctest',
345                  'testgen',
346                  'cms-test.pl',
347                  'tx509',
348                  'test.cnf',
349                  'testenc',
350                  'tocsp',
351                  'testca',
352                  'CAss.cnf',
353                  'testtsa',
354                  'CAtsa.cnf',
355                  'Uss.cnf',
356                  'P1ss.cnf',
357                  'P2ss.cnf',
358                  'tcrl',
359                  'tsid',
360                  'treq',
361                  'tpkcs7',
362                  'tpkcs7d',
363                  'testcrl.pem',
364                  'testx509.pem',
365                  'v3-cert1.pem',
366                  'v3-cert2.pem',
367                  'testreq2.pem',
368                  'testp7.pem',
369                  'pkcs7-1.pem',
370                  'trsa',
371                  'testrsa.pem',
372                  'testsid.pem',
373                  'testss',
374                );
375   my $copies = copy_scripts(1, 'test', @copies);
376   $copies .= copy_scripts(0, 'test', ('smcont.txt'));
377
378   my @utils = ( 'shlib_wrap.sh',
379                 'opensslwrap.sh',
380               );
381   $copies .= copy_scripts(1, 'util', @utils);
382
383   my @apps = ( 'CA.sh',
384                'openssl.cnf',
385              );
386   $copies .= copy_scripts(1, 'apps', @apps);
387
388   $copies .= copy_scripts(1, 'crypto/evp', ('evptests.txt'));
389
390   $scripts = "test_scripts: \$(TEST_D)/CA.sh \$(TEST_D)/opensslwrap.sh \$(TEST_D)/openssl.cnf \$(TEST_D)/shlib_wrap.sh ocsp smime\n";
391   $scripts .= "\nocsp:\n\tcp -R test/ocsp-tests \$(TEST_D)\n";
392   $scripts .= "\smime:\n\tcp -R test/smime-certs \$(TEST_D)\n";
393
394   my $all = 'test: ' . join(' ', keys %alltests);
395
396   return "$scripts\n$copies\n$tests\n$all\n\n$each";
397   }
398
399 1;