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