Get closer to a working single Makefile with test support.
[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   return $str;
215
216   my @t = split("\n", $str);
217   $str = '';
218   foreach my $t (@t)
219     {
220     $t =~ s/^\s+//;
221     if ($t =~ /^@/)
222       {
223       $t =~ s/^@/\@cd \$(TEST_D) && /;
224       }
225     else
226       {
227       $t = 'cd $(TEST_D) && ' . $t;
228       }
229     $str .= "\t$t\n";
230     }
231   return $str;
232 }
233
234 sub get_tests
235   {
236   my ($makefile) = @_;
237
238   open(M, $makefile) || die "Can't open $makefile: $!";
239   my %targets;
240   my %deps;
241   my %tests;
242   while (my $line = <M>)
243     {
244     chomp $line;
245     while ($line =~ /^(.*)\\$/)
246       {
247       $line = $1 . <M>;
248       }
249
250     if ($line =~ /^alltests:(.*)$/)
251       {
252       my @t = split(/\s+/, $1);
253       foreach my $t (@t)
254         {
255         $targets{$t} = '';
256         }
257       }
258
259     if (($line =~ /^(\S+):(.*)$/ && exists $targets{$1})
260         || $line =~ /^(test_ss .*):(.*)/)
261       {
262       my $t = $1;
263       $deps{$t} = $2;
264       $deps{$t} =~ s/#.*$//;
265       for (;;)
266         {
267         $line = <M>;
268         chomp $line;
269         last if $line eq '';
270         $targets{$t} .= "$line\n";
271         }
272       next;
273       }
274
275     if ($line =~ /^(\S+TEST)=\s*(\S+)$/)
276       {
277       $tests{$1} = $2;
278       next;
279       }
280     }
281
282   delete $targets{test_jpake} if $no_jpake;
283   delete $targets{test_ige} if $no_ige;
284   delete $targets{test_md2} if $no_md2;
285   delete $targets{test_rc5} if $no_rc5;
286
287   my $tests;
288   foreach my $t (keys %tests)
289     {
290     $tests .= "$t = $tests{$t}\n";
291     }
292
293   my $all = 'test:';
294   my $each;
295   foreach my $t (keys %targets)
296     {
297     next if $t eq '';
298
299     if ($t =~ /^test_ss/)
300       {
301       $t =~ s/\s+/ \$(TEST_D)\//g;
302       $all .= ' test_ss';
303       }
304     else
305       {
306       $all .= " $t";
307       }
308
309     my $d = $deps{$t};
310     $d =~ s/\.\.\/apps/\$(BIN_D)/g;
311     $d = fixtests($d, \%tests);
312     $d = fixdeps($d);
313
314     my $r = $targets{$t};
315     $r =~ s/\.\.\/apps/\$(BIN_D)/g;
316     $r =~ s/\.\.\/(\S+)/\$(SRC_D)\/$1/g;
317     $r = fixrules($r);
318
319     $each .= "$t: \$(TEST_D) $d\n\tcd \$(TEST_D)\n$r\n";
320     }
321
322   # FIXME: Might be a clever way to figure out what needs copying
323   my $copies = do_copy_rule('$(TEST_D)', 'test/bctest test/evptests.txt test/testgen test/cms-test.pl', '');
324
325   return "$copies\n$tests\n$all\n\n$each";
326   }
327
328 1;