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