Show start/end of tests.
[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          );
73
74 # If I were feeling more clever, these could probably be extracted
75 # from makefiles.
76 sub platform_perlasm_compile_target
77         {
78         local($target, $source, $bname) = @_;
79
80         for $p (keys %perl1)
81                 {
82                 if ($target eq "\$(OBJ_D)/$p.o")
83                         {
84                         return << "EOF";
85 \$(TMP_D)/$p.s: $perl1{$p}/asm/$p.pl
86         \$(PERL) $perl1{$p}/asm/$p.pl \$(PERLASM_SCHEME) > \$@
87 EOF
88                         }
89                 }
90         if ($target eq '$(OBJ_D)/x86_64cpuid.o')
91                 {
92                 return << 'EOF';
93 $(TMP_D)/x86_64cpuid.s: crypto/x86_64cpuid.pl
94         $(PERL) crypto/x86_64cpuid.pl $(PERLASM_SCHEME) > $@
95 EOF
96                 }
97         elsif ($target eq '$(OBJ_D)/sha256-x86_64.o')
98                 {
99                 return << 'EOF';
100 $(TMP_D)/sha256-x86_64.s: crypto/sha/asm/sha512-x86_64.pl
101         $(PERL) crypto/sha/asm/sha512-x86_64.pl $(PERLASM_SCHEME) $@
102 EOF
103                 }
104         elsif ($target eq '$(OBJ_D)/sha512-x86_64.o')
105                 {
106                 return << 'EOF';
107 $(TMP_D)/sha512-x86_64.s: crypto/sha/asm/sha512-x86_64.pl
108         $(PERL) crypto/sha/asm/sha512-x86_64.pl $(PERLASM_SCHEME) $@
109 EOF
110                 }
111         elsif ($target eq '$(OBJ_D)/sha512-x86_64.o')
112                 {
113                 return << 'EOF';
114 $(TMP_D)/sha512-x86_64.s: crypto/sha/asm/sha512-x86_64.pl
115         $(PERL) crypto/sha/asm/sha512-x86_64.pl $(PERLASM_SCHEME) $@
116 EOF
117                 }
118
119         die $target;
120         }
121
122 sub special_compile_target
123         {
124         local($target) = @_;
125
126         if ($target eq 'crypto/bn/x86_64-gcc')
127                 {
128                 return << "EOF";
129 \$(TMP_D)/x86_64-gcc.o: crypto/bn/asm/x86_64-gcc.c
130         \$(CC) \$(CFLAGS) -c -o \$@ crypto/bn/asm/x86_64-gcc.c
131 EOF
132                 }
133         return undef;
134         }
135
136 sub do_lib_rule
137         {
138         local($obj,$target,$name,$shlib)=@_;
139         local($ret,$_,$Name);
140
141         $target =~ s/\//$o/g if $o ne '/';
142         $target="$target";
143         ($Name=$name) =~ tr/a-z/A-Z/;
144
145         $ret.="$target: \$(${Name}OBJ)\n";
146         $ret.="\t\$(RM) $target\n";
147         $ret.="\t\$(MKLIB) $target \$(${Name}OBJ)\n";
148         $ret.="\t\$(RANLIB) $target\n\n";
149         }
150
151 sub do_link_rule
152         {
153         local($target,$files,$dep_libs,$libs)=@_;
154         local($ret,$_);
155
156         $file =~ s/\//$o/g if $o ne '/';
157         $n=&bname($target);
158         $ret.="$target: $files $dep_libs\n";
159         $ret.="\t\$(LINK) ${efile}$target \$(LFLAGS) $files $libs\n\n";
160         return($ret);
161         }
162
163 sub which
164         {
165         my ($name)=@_;
166         my $path;
167         foreach $path (split /:/, $ENV{PATH})
168                 {
169                 if (-x "$path/$name")
170                         {
171                         return "$path/$name";
172                         }
173                 }
174         }
175
176 sub fixtests
177   {
178   my ($str, $tests) = @_;
179
180   foreach my $t (keys %$tests)
181     {
182     $str =~ s/(\.\/)?\$\($t\)/\$(TEST_D)\/$tests->{$t}/g;
183     }
184
185   return $str;
186   }
187
188 sub fixdeps
189   {
190   my ($str) = @_;
191
192   my @t = split(/\s+/, $str);
193   $str = '';
194   foreach my $t (@t)
195     {
196     $str .= ' ' if $str ne '';
197     if ($t =~ /^[^\/]+$/)
198       {
199       $str .= '$(TEST_D)/' . $t;
200       }
201     else
202       {
203       $str .= $t;
204       }
205     }
206
207   return $str;
208   }
209
210 sub fixrules
211   {
212   my ($str) = @_;
213
214   # Compatible with -j...
215   $str =~ s/^(\s+@?)/$1cd \$(TEST_D) && /;
216   return $str;
217
218   # Compatible with not -j.
219   my @t = split("\n", $str);
220   $str = '';
221   my $prev;
222   foreach my $t (@t)
223     {
224     $t =~ s/^\s+//;
225     if (!$prev)
226       {
227       if ($t =~ /^@/)
228         {
229         $t =~ s/^@/\@cd \$(TEST_D) && /;
230         }
231       elsif ($t !~ /^\s*#/)
232         {
233         $t = 'cd $(TEST_D) && ' . $t;
234         }
235       }
236     $str .= "\t$t\n";
237     $prev = $t =~/\\$/;
238     }
239   return $str;
240 }
241
242 sub copy_scripts
243   {
244   my ($sed, $src, @targets) = @_;
245
246   my $s = '';
247   foreach my $t (@targets)
248     {
249     # Copy first so we get file modes...
250     $s .= "\$(TEST_D)/$t: \$(SRC_D)/$src/$t\n\tcp \$(SRC_D)/$src/$t \$(TEST_D)/$t\n";
251     $s .= "\tsed -e 's/\\.\\.\\/apps/..\\/\$(OUT_D)/' -e 's/\\.\\.\\/util/..\\/\$(TEST_D)/' < \$(SRC_D)/$src/$t > \$(TEST_D)/$t\n" if $sed;
252     $s .= "\n";
253     }
254   return $s;
255   }
256
257 sub get_tests
258   {
259   my ($makefile) = @_;
260
261   open(M, $makefile) || die "Can't open $makefile: $!";
262   my %targets;
263   my %deps;
264   my %tests;
265   while (my $line = <M>)
266     {
267     chomp $line;
268     while ($line =~ /^(.*)\\$/)
269       {
270       $line = $1 . <M>;
271       }
272
273     if ($line =~ /^alltests:(.*)$/)
274       {
275       my @t = split(/\s+/, $1);
276       foreach my $t (@t)
277         {
278         $targets{$t} = '';
279         }
280       }
281
282     if (($line =~ /^(\S+):(.*)$/ && exists $targets{$1})
283         || $line =~ /^(test_ss .*):(.*)/)
284       {
285       my $t = $1;
286       $deps{$t} = $2;
287       $deps{$t} =~ s/#.*$//;
288       for (;;)
289         {
290         $line = <M>;
291         chomp $line;
292         last if $line eq '';
293         $targets{$t} .= "$line\n";
294         }
295       next;
296       }
297
298     if ($line =~ /^(\S+TEST)=\s*(\S+)$/)
299       {
300       $tests{$1} = $2;
301       next;
302       }
303     }
304
305   delete $targets{test_jpake} if $no_jpake;
306   delete $targets{test_ige} if $no_ige;
307   delete $targets{test_md2} if $no_md2;
308   delete $targets{test_rc5} if $no_rc5;
309
310   my $tests;
311   foreach my $t (keys %tests)
312     {
313     $tests .= "$t = $tests{$t}\n";
314     }
315
316   my $all = 'test:';
317   my $each;
318   foreach my $t (keys %targets)
319     {
320     next if $t eq '';
321
322     if ($t =~ /^test_ss/)
323       {
324       $t =~ s/\s+/ \$(TEST_D)\//g;
325       $all .= ' test_ss';
326       }
327     else
328       {
329       $all .= " $t";
330       }
331
332     my $d = $deps{$t};
333     $d =~ s/\.\.\/apps/\$(BIN_D)/g;
334     $d =~ s/\.\.\/util/\$(TEST_D)/g;
335     $d = fixtests($d, \%tests);
336     $d = fixdeps($d);
337
338     my $r = $targets{$t};
339     $r =~ s/\.\.\/apps/..\/\$(BIN_D)/g;
340     $r =~ s/\.\.\/util/..\/\$(TEST_D)/g;
341     $r =~ s/\.\.\/(\S+)/\$(SRC_D)\/$1/g;
342     $r = fixrules($r);
343
344     $each .= "$t: test_scripts $d\n\t\@echo '$t test started'\n$r\t\@echo '$t test done'\n\n";
345     }
346
347   # FIXME: Might be a clever way to figure out what needs copying
348   my @copies = ( 'bctest',
349                  'evptests.txt',
350                  'testgen',
351                  'cms-test.pl',
352                  'tx509',
353                  'test.cnf',
354                  'testenc',
355                  'tocsp',
356                  'testca',
357                  'CAss.cnf',
358                  'testtsa',
359                  'CAtsa.cnf',
360                  'Uss.cnf',
361                  'P1ss.cnf',
362                  'P2ss.cnf',
363                  'tcrl',
364                  'tsid',
365                  'treq',
366                  'tpkcs7',
367                  'tpkcs7d',
368                  'testcrl.pem',
369                  'testx509.pem',
370                  'v3-cert1.pem',
371                  'v3-cert2.pem',
372                  'testreq2.pem',
373                  'testp7.pem',
374                  'pkcs7-1.pem',
375                  'trsa',
376                  'testrsa.pem',
377                  'testsid.pem',
378                );
379   my $copies = copy_scripts(1, 'test', @copies);
380   $copies .= copy_scripts(0, 'test', ('smcont.txt'));
381
382   my @utils = ( 'shlib_wrap.sh',
383                 'opensslwrap.sh',
384               );
385   $copies .= copy_scripts(1, 'util', @utils);
386
387   my @apps = ( 'CA.sh',
388                'openssl.cnf',
389              );
390   $copies .= copy_scripts(1, 'apps', @apps);
391
392   $scripts = "test_scripts: \$(TEST_D)/CA.sh \$(TEST_D)/opensslwrap.sh \$(TEST_D)/openssl.cnf ocsp smime\n";
393   $scripts .= "\nocsp:\n\tcp -R test/ocsp-tests \$(TEST_D)\n";
394   $scripts .= "\smime:\n\tcp -R test/smime-certs \$(TEST_D)\n";
395
396   return "$scripts\n$copies\n$tests\n$all\n\n$each";
397   }
398
399 1;