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