6f0881a2999e572d7e257efb3a8702e2932e292b
[openssl.git] / util / mk1mf.pl
1 #!/usr/local/bin/perl
2 # A bit of an evil hack but it post processes the file ../MINFO which
3 # is generated by `make files` in the top directory.
4 # This script outputs one mega makefile that has no shell stuff or any
5 # funny stuff
6 #
7
8 $INSTALLTOP="/usr/local/ssl";
9
10 $ssl_version="0.8.2";
11
12 $infile="MINFO";
13
14 %ops=(
15         "VC-WIN32",   "Microsoft Visual C++ 4.[01] - Windows NT [34].x",
16         "VC-W31-16",  "Microsoft Visual C++ 1.52 - Windows 3.1 - 286",
17         "VC-WIN16",   "Alias for VC-W31-32",
18         "VC-W31-32",  "Microsoft Visual C++ 1.52 - Windows 3.1 - 386+",
19         "VC-MSDOS","Microsoft Visual C++ 1.52 - MSDOS",
20         "BC-NT",   "Borland C++ 4.5 - Windows NT  - PROBABLY NOT WORKING",
21         "BC-W31",  "Borland C++ 4.5 - Windows 3.1 - PROBABLY NOT WORKING",
22         "BC-MSDOS","Borland C++ 4.5 - MSDOS",
23         "linux-elf","Linux elf",
24         "FreeBSD","FreeBSD distribution",
25         "default","cc under unix",
26         );
27
28 $type="";
29 foreach (@ARGV)
30         {
31         if    (/^no-rc2$/)      { $no_rc2=1; }
32         elsif (/^no-rc4$/)      { $no_rc4=1; }
33         elsif (/^no-rc5$/)      { $no_rc5=1; }
34         elsif (/^no-idea$/)     { $no_idea=1; }
35         elsif (/^no-des$/)      { $no_des=1; }
36         elsif (/^no-bf$/)       { $no_bf=1; }
37         elsif (/^no-cast$/)     { $no_cast=1; }
38         elsif (/^no-md2$/)      { $no_md2=1; }
39         elsif (/^no-md5$/)      { $no_md5=1; }
40         elsif (/^no-sha$/)      { $no_sha=1; }
41         elsif (/^no-sha1$/)     { $no_sha1=1; }
42         elsif (/^no-rmd160$/)   { $no_rmd160=1; }
43         elsif (/^no-mdc2$/)     { $no_mdc2=1; }
44         elsif (/^no-patents$/)  { $no_rc2=$no_rc4=$no_rc5=$no_idea=$no_rsa=1; }
45         elsif (/^no-rsa$/)      { $no_rsa=1; }
46         elsif (/^no-dsa$/)      { $no_dsa=1; }
47         elsif (/^no-dh$/)       { $no_dh=1; }
48         elsif (/^no-asm$/)      { $no_asm=1; }
49         elsif (/^no-ssl2$/)     { $no_ssl2=1; }
50         elsif (/^no-ssl3$/)     { $no_ssl3=1; }
51         elsif (/^no-err$/)      { $no_err=1; }
52         elsif (/^no-sock$/)     { $no_sock=1; }
53
54         elsif (/^just-ssl$/)    { $no_rc2=$no_idea=$no_des=$no_bf=$no_cast=1;
55                                   $no_md2=$no_sha=$no_mdc2=$no_dsa=$no_dh=1;
56                                   $no_ssl2=$no_err=1; }
57
58         elsif (/^rsaref$/)      { $rsaref=1; }
59         elsif (/^gcc$/)         { $gcc=1; }
60         elsif (/^debug$/)       { $debug=1; }
61         elsif (/^shlib$/)       { $shlib=1; }
62         elsif (/^dll$/)         { $shlib=1; }
63         elsif (/^([^=]*)=(.*)$/){ $VARS{$1}=$2; }
64         elsif (/^-[lL].*$/)     { $l_flags.="$_ "; }
65         elsif ((!/^-help/) && (!/^-h/) && (!/^-\?/) && /^-.*$/)
66                 { $c_flags.="$_ "; }
67         else
68                 {
69                 if (!defined($ops{$_}))
70                         {
71                         print STDERR "unknown option - $_\n";
72                         print STDERR "usage: perl mk1mf.pl [system] [options]\n";
73                         print STDERR "\nwhere [system] can be one of the following\n";
74                         foreach $i (sort keys %ops)
75                                 { printf STDERR "\t%-10s\t%s\n",$i,$ops{$i}; }
76                         print STDERR <<"EOF";
77 and [options] can be one of
78         no-md2 no-md5 no-sha no-sha1 no-mdc2 no-rmd160 - Skip this digest
79         no-rc2 no-rc4 no-idea no-des no-bf no-cast - Skip this symetric cipher
80         no-rc5
81         no-rsa no-dsa no-dh                     - Skip this public key cipher
82         no-ssl2 no-ssl3                         - Skip this version of SSL
83         just-ssl                                - remove all non-ssl keys/digest
84         no-asm                                  - No x86 asm
85         no-socks                                - No socket code
86         no-err                                  - No error strings
87         dll/shlib                               - Build shared libraries (MS)
88         debug                                   - Debug build
89         gcc                                     - Use Gcc (unix)
90         rsaref                                  - Build to require RSAref
91
92 Values that can be set
93 TMP=tmpdir OUT=outdir SRC=srcdir BIN=binpath INC=header-outdir CC=C-compiler
94
95 -L<ex_lib_path> -l<ex_lib>                      - extra library flags (unix)
96 -<ex_cc_flags>                                  - extra 'cc' flags,
97                                                   added (MS), or replace (unix)
98 EOF
99                         exit(1);
100                         }
101                 $type=$_;
102                 }
103         }
104
105 $no_mdc2=1 if ($no_des);
106
107 $no_ssl3=1 if ($no_md5 || $no_sha1);
108 $no_ssl3=1 if ($no_rsa && $no_dh);
109
110 $no_ssl2=1 if ($no_md5 || $no_rsa);
111 $no_ssl2=1 if ($no_rsa);
112
113 $out_def="out";
114 $inc_def="outinc";
115 $tmp_def="tmp";
116
117
118 ($ssl,$crypto)=("ssl","crypto");
119 $RSAglue="RSAglue";
120 $ranlib="echo ranlib";
121
122 $cc=(defined($VARS{'CC'}))?$VARS{'CC'}:'cc';
123 $src_dir=(defined($VARS{'SRC'}))?$VARS{'SRC'}:'.';
124 $bin_dir=(defined($VARS{'BIN'}))?$VARS{'BIN'}:'';
125
126 # $bin_dir.=$o causes a core dump on my sparc :-(
127
128 push(@INC,"util/pl","pl");
129 if ($type eq "VC-MSDOS")
130         {
131         $asmbits=16;
132         $msdos=1;
133         require 'VC-16.pl';
134         }
135 elsif ($type eq "VC-W31-16")
136         {
137         $asmbits=16;
138         $msdos=1; $win16=1;
139         require 'VC-16.pl';
140         }
141 elsif (($type eq "VC-W31-32") || ($type eq "VC-WIN16"))
142         {
143         $asmbits=32;
144         $msdos=1; $win16=1;
145         require 'VC-16.pl';
146         }
147 elsif (($type eq "VC-WIN32") || ($type eq "VC-NT"))
148         {
149         require 'VC-32.pl';
150         }
151 elsif ($type eq "BC-NT")
152         {
153         $bc=1;
154         require 'BC-32.pl';
155         }
156 elsif ($type eq "BC-W31")
157         {
158         $bc=1;
159         $msdos=1; $w16=1;
160         require 'BC-16.pl';
161         }
162 elsif ($type eq "BC-Q16")
163         {
164         $msdos=1; $w16=1; $shlib=0; $qw=1;
165         require 'BC-16.pl';
166         }
167 elsif ($type eq "BC-MSDOS")
168         {
169         $asmbits=16;
170         $msdos=1;
171         require 'BC-16.pl';
172         }
173 elsif ($type eq "FreeBSD")
174         {
175         require 'unix.pl';
176         $cflags='-DTERMIO -D_ANSI_SOURCE -O2 -fomit-frame-pointer';
177         }
178 elsif ($type eq "linux-elf")
179         {
180         require "unix.pl";
181         require "linux.pl";
182         $unix=1;
183         }
184 else
185         {
186         require "unix.pl";
187
188         $unix=1;
189         $cflags.=' -DTERMIO';
190         }
191
192 $out_dir=(defined($VARS{'OUT'}))?$VARS{'OUT'}:$out_def.($debug?".dbg":"");
193 $tmp_dir=(defined($VARS{'TMP'}))?$VARS{'TMP'}:$tmp_def.($debug?".dbg":"");
194 $inc_dir=(defined($VARS{'INC'}))?$VARS{'INC'}:$inc_def;
195
196 $bin_dir=$bin_dir.$o unless ((substr($bin_dir,-1,1) eq $o) || ($bin_dir eq ''));
197
198 $cflags.=" -DNO_IDEA" if $no_idea;
199 $cflags.=" -DNO_RC2"  if $no_rc2;
200 $cflags.=" -DNO_RC4"  if $no_rc4;
201 $cflags.=" -DNO_RC5"  if $no_rc5;
202 $cflags.=" -DNO_MD2"  if $no_md2;
203 $cflags.=" -DNO_MD5"  if $no_md5;
204 $cflags.=" -DNO_SHA"  if $no_sha;
205 $cflags.=" -DNO_SHA1" if $no_sha1;
206 $cflags.=" -DNO_RMD160" if $no_rmd160;
207 $cflags.=" -DNO_MDC2" if $no_mdc2;
208 $cflags.=" -DNO_BLOWFISH"  if $no_bf;
209 $cflags.=" -DNO_CAST" if $no_cast;
210 $cflags.=" -DNO_DES"  if $no_des;
211 $cflags.=" -DNO_RSA"  if $no_rsa;
212 $cflags.=" -DNO_DSA"  if $no_dsa;
213 $cflags.=" -DNO_DH"   if $no_dh;
214 $cflags.=" -DNO_SOCK" if $no_sock;
215 $cflags.=" -DNO_SSL2" if $no_ssl2;
216 $cflags.=" -DNO_SSL3" if $no_ssl3;
217 $cflags.=" -DNO_ERR"  if $no_err;
218 $cflags.=" -DRSAref"  if $rsaref ne "";
219
220 if ($unix)
221         { $cflags="$c_flags" if ($c_flags ne ""); }
222 else    { $cflags="$c_flags$cflags" if ($c_flags ne ""); }
223
224 $ex_libs="$l_flags$ex_libs" if ($l_flags ne "");
225
226 if ($ranlib ne "")
227         {
228         $ranlib="\$(SRC_D)$o$ranlib";
229         }
230
231 if ($msdos)
232         {
233         $banner ="\t\@echo Make sure you have run 'perl Configure $type' in the\n";
234         $banner.="\t\@echo top level directory, if you don't have perl, you will\n";
235         $banner.="\t\@echo need to probably edit crypto/bn/bn.h, check the\n";
236         $banner.="\t\@echo documentation for details.\n";
237         }
238
239 # have to do this to allow $(CC) under unix
240 $link="$bin_dir$link" if ($link !~ /^\$/);
241
242 $INSTALLTOP =~ s|/|$o|g;
243
244 $defs= <<"EOF";
245 # This makefile has been automatically generated from the SSLeay distribution.
246 # This single makefile will build the complete SSLeay distribution and
247 # by default leave the 'intertesting' output files in .${o}out and the stuff
248 # that needs deleting in .${o}tmp.
249 # The file was generated by running 'make makefile.one', which
250 # does a 'make files', which writes all the environment variables from all
251 # the makefiles to the file call MINFO.  This file is used by
252 # util${o}mk1mf.pl to generate makefile.one.
253 # The 'makefile per directory' system suites me when developing this
254 # library and also so I can 'distribute' indervidual library sections.
255 # The one monster makefile better suits building in non-unix
256 # environments.
257
258 INSTALLTOP=$INSTALLTOP
259
260 # Set your compiler options
261 CC=$bin_dir${cc}
262 CFLAG=$cflags
263 APP_CFLAG=$app_cflag
264 LIB_CFLAG=$lib_cflag
265 SHLIB_CFLAG=$shl_cflag
266 APP_EX_OBJ=$app_ex_obj
267 SHLIB_EX_OBJ=$shlib_ex_obj
268 # add extra libraries to this define, for solaris -lsocket -lnsl would
269 # be added
270 EX_LIBS=$ex_libs
271
272 # The SSLeay directory
273 SRC_D=$src_dir
274
275 LINK=$link
276 LFLAGS=$lflags
277
278 BN_MULW_OBJ=$bn_mulw_obj
279 BN_MULW_SRC=$bn_mulw_src
280 DES_ENC_OBJ=$des_enc_obj
281 DES_ENC_SRC=$des_enc_src
282 DES_CRYPT_OBJ=$des_crypt_obj
283 DES_CRYPT_SRC=$des_crypt_src
284 BF_ENC_OBJ=$bf_enc_obj
285 BF_ENC_SRC=$bf_enc_src
286 CAST_ENC_OBJ=$cast_enc_obj
287 CAST_ENC_SRC=$cast_enc_src
288 RC4_ENC_OBJ=$rc4_enc_obj
289 RC4_ENC_SRC=$rc4_enc_src
290 RC5_ENC_OBJ=$rc5_enc_obj
291 RC5_ENC_SRC=$rc5_enc_src
292 MD5_ASM_OBJ=$md5_asm_obj
293 MD5_ASM_SRC=$md5_asm_src
294 SHA1_ASM_OBJ=$sha1_asm_obj
295 SHA1_ASM_SRC=$sha1_asm_src
296 RMD160_ASM_OBJ=$rmd160_asm_obj
297 RMD160_ASM_SRC=$rmd160_asm_src
298
299 # The output directory for everything intersting
300 OUT_D=$out_dir
301 # The output directory for all the temporary muck
302 TMP_D=$tmp_dir
303 # The output directory for the header files
304 INC_D=$inc_dir
305
306 CP=$cp
307 RM=$rm
308 RANLIB=$ranlib
309 MKDIR=mkdir
310 MKLIB=$bin_dir$mklib
311 MLFLAGS=$mlflags
312 ASM=$bin_dir$asm
313
314 ######################################################
315 # You should not need to touch anything below this point
316 ######################################################
317
318 E_EXE=ssleay
319 SSL=$ssl
320 CRYPTO=$crypto
321 RSAGLUE=$RSAglue
322
323 # BIN_D  - Binary output directory
324 # TEST_D - Binary test file output directory
325 # LIB_D  - library output directory
326 BIN_D=\$(OUT_D)
327 TEST_D=\$(OUT_D)
328 LIB_D=\$(OUT_D)
329
330 # INCL_D - local library directory
331 # OBJ_D  - temp object file directory
332 OBJ_D=\$(TMP_D)
333 INCL_D=\$(TMP_D)
334
335 O_SSL=     \$(LIB_D)$o$plib\$(SSL)$shlibp
336 O_CRYPTO=  \$(LIB_D)$o$plib\$(CRYPTO)$shlibp
337 O_RSAGLUE= \$(LIB_D)$o$plib\$(RSAGLUE)$libp
338 SO_SSL=    $plib\$(SSL)$so_shlibp
339 SO_CRYPTO= $plib\$(CRYPTO)$so_shlibp
340 L_SSL=     \$(LIB_D)$o\$(SSL)$libp
341 L_CRYPTO=  \$(LIB_D)$o\$(CRYPTO)$libp
342
343 L_LIBS= \$(L_SSL) \$(L_CRYPTO)
344 #L_LIBS= \$(O_SSL) \$(O_RSAGLUE) -lrsaref \$(O_CRYPTO)
345
346 ######################################################
347 # Don't touch anything below this point
348 ######################################################
349
350 INC=-I\$(INC_D) -I\$(INCL_D)
351 APP_CFLAGS=\$(INC) \$(CFLAG) \$(APP_CFLAG)
352 LIB_CFLAGS=\$(INC) \$(CFLAG) \$(LIB_CFLAG)
353 SHLIB_CFLAGS=\$(INC) \$(CFLAG) \$(LIB_CFLAG) \$(SHLIB_CFLAG)
354 LIBS_DEP=\$(O_CRYPTO) \$(O_RSAGLUE) \$(O_SSL)
355
356 #############################################
357 EOF
358
359 $rules=<<"EOF";
360 all: banner \$(TMP_D) \$(BIN_D) \$(TEST_D) \$(LIB_D) \$(INC_D) headers lib exe
361
362 banner:
363 $banner
364
365 \$(TMP_D):
366         \$(MKDIR) \$(TMP_D)
367
368 \$(BIN_D):
369         \$(MKDIR) \$(BIN_D)
370
371 \$(TEST_D):
372         \$(MKDIR) \$(TEST_D)
373
374 \$(LIB_D):
375         \$(MKDIR) \$(LIB_D)
376
377 \$(INC_D):
378         \$(MKDIR) \$(INC_D)
379
380 headers: \$(HEADER) \$(EXHEADER)
381
382 lib: \$(LIBS_DEP)
383
384 exe: \$(T_EXE) \$(BIN_D)$o\$(E_EXE)$exep
385
386 install:
387         \$(MKDIR) \$(INSTALLTOP)
388         \$(MKDIR) \$(INSTALLTOP)${o}bin
389         \$(MKDIR) \$(INSTALLTOP)${o}include
390         \$(MKDIR) \$(INSTALLTOP)${o}lib
391         \$(CP) \$(INC_D)${o}*.\[ch\] \$(INSTALLTOP)${o}include
392         \$(CP) \$(BIN_D)$o\$(E_EXE)$exep \$(INSTALLTOP)${o}bin
393         \$(CP) \$(O_SSL) \$(INSTALLTOP)${o}lib
394         \$(CP) \$(O_CRYPTO) \$(INSTALLTOP)${o}lib
395
396 clean:
397         \$(RM) \$(TMP_D)$o*.*
398
399 vclean:
400         \$(RM) \$(TMP_D)$o*.*
401         \$(RM) \$(OUT_D)$o*.*
402
403 EOF
404
405 #############################################
406 # We parse in input file and 'store' info for later printing.
407 open(IN,"<$infile") || die "unable to open $infile:$!\n";
408 $_=<IN>;
409 for (;;)
410         {
411         chop;
412
413         ($key,$val)=/^([^=]+)=(.*)/;
414         if ($key eq "RELATIVE_DIRECTORY")
415                 {
416                 if ($lib ne "")
417                         {
418                         $uc=$lib;
419                         $uc =~ s/^lib(.*)\.a/$1/;
420                         $uc =~ tr/a-z/A-Z/;
421                         $lib_nam{$uc}=$uc;
422                         $lib_obj{$uc}.=$libobj." ";
423                         }
424                 last if ($val eq "FINISHED");
425                 $lib="";
426                 $libobj="";
427                 $dir=$val;
428                 }
429
430         if ($key eq "TEST")
431                 { $test.=&var_add($dir,$val); }
432
433         if (($key eq "PROGS") || ($key eq "E_OBJ"))
434                 { $e_exe.=&var_add($dir,$val); }
435
436         if ($key eq "LIB")
437                 {
438                 $lib=$val;
439                 $lib =~ s/^.*\/([^\/]+)$/$1/;
440                 }
441
442         if ($key eq "EXHEADER")
443                 { $exheader.=&var_add($dir,$val); }
444
445         if ($key eq "HEADER")
446                 { $header.=&var_add($dir,$val); }
447
448         if ($key eq "LIBOBJ")
449                 { $libobj=&var_add($dir,$val); }
450
451         if (!($_=<IN>))
452                 { $_="RELATIVE_DIRECTORY=FINISHED\n"; }
453         }
454 close(IN);
455
456 # Strip of trailing ' '
457 foreach (keys %lib_obj) { $lib_obj{$_}=&clean_up_ws($lib_obj{$_}); }
458 $test=&clean_up_ws($test);
459 $e_exe=&clean_up_ws($e_exe);
460 $exheader=&clean_up_ws($exheader);
461 $header=&clean_up_ws($header);
462
463 # First we strip the exheaders from the headers list
464 foreach (split(/\s+/,$exheader)){ $h{$_}=1; }
465 foreach (split(/\s+/,$header))  { $h.=$_." " unless $h{$_}; }
466 chop($h); $header=$h;
467
468 $defs.=&do_defs("HEADER",$header,"\$(INCL_D)",".h");
469 $rules.=&do_copy_rule("\$(INCL_D)",$header,".h");
470
471 $defs.=&do_defs("EXHEADER",$exheader,"\$(INC_D)",".h");
472 $rules.=&do_copy_rule("\$(INC_D)",$exheader,".h");
473
474 $defs.=&do_defs("T_OBJ",$test,"\$(OBJ_D)",$obj);
475 $rules.=&do_compile_rule("\$(OBJ_D)",$test,"\$(APP_CFLAGS)");
476
477 $defs.=&do_defs("E_OBJ",$e_exe,"\$(OBJ_D)",$obj);
478 $rules.=&do_compile_rule("\$(OBJ_D)",$e_exe,'-DMONOLITH $(APP_CFLAGS)');
479
480 foreach (values %lib_nam)
481         {
482         $lib_obj=$lib_obj{$_};
483         local($slib)=$shlib;
484
485         $slib=0 if ($_ eq "RSAGLUE");
486
487         if (($_ eq "SSL") && $no_ssl2 && $no_ssl3)
488                 {
489                 $rules.="\$(O_SSL):\n\n"; 
490                 next;
491                 }
492
493         if (($_ eq "RSAGLUE") && $no_rsa)
494                 {
495                 $rules.="\$(O_RSAGLUE):\n\n"; 
496                 next;
497                 }
498
499         if (($bn_mulw_obj ne "") && ($_ eq "CRYPTO"))
500                 {
501                 $lib_obj =~ s/\s\S*\/bn_mulw\S*/ \$(BN_MULW_OBJ)/;
502                 $rules.=&do_asm_rule($bn_mulw_obj,$bn_mulw_src);
503                 }
504         if (($des_enc_obj ne "") && ($_ eq "CRYPTO"))
505                 {
506                 $lib_obj =~ s/\s\S*des_enc\S*/ \$(DES_ENC_OBJ)/;
507                 $lib_obj =~ s/\s\S*\/fcrypt_b\S*\s*/ /;
508                 $rules.=&do_asm_rule($des_enc_obj,$des_enc_src);
509                 }
510         if (($bf_enc_obj ne "") && ($_ eq "CRYPTO"))
511                 {
512                 $lib_obj =~ s/\s\S*\/bf_enc\S*/ \$(BF_ENC_OBJ)/;
513                 $rules.=&do_asm_rule($bf_enc_obj,$bf_enc_src);
514                 }
515         if (($cast_enc_obj ne "") && ($_ eq "CRYPTO"))
516                 {
517                 $lib_obj =~ s/(\s\S*\/c_enc\S*)/ \$(CAST_ENC_OBJ)/;
518                 $rules.=&do_asm_rule($cast_enc_obj,$cast_enc_src);
519                 }
520         if (($rc4_enc_obj ne "") && ($_ eq "CRYPTO"))
521                 {
522                 $lib_obj =~ s/\s\S*\/rc4_enc\S*/ \$(RC4_ENC_OBJ)/;
523                 $rules.=&do_asm_rule($rc4_enc_obj,$rc4_enc_src);
524                 }
525         if (($rc5_enc_obj ne "") && ($_ eq "CRYPTO"))
526                 {
527                 $lib_obj =~ s/\s\S*\/rc5_enc\S*/ \$(RC5_ENC_OBJ)/;
528                 $rules.=&do_asm_rule($rc5_enc_obj,$rc5_enc_src);
529                 }
530         if (($md5_asm_obj ne "") && ($_ eq "CRYPTO"))
531                 {
532                 $lib_obj =~ s/\s(\S*\/md5_dgst\S*)/ $1 \$(MD5_ASM_OBJ)/;
533                 $rules.=&do_asm_rule($md5_asm_obj,$md5_asm_src);
534                 }
535         if (($sha1_asm_obj ne "") && ($_ eq "CRYPTO"))
536                 {
537                 $lib_obj =~ s/\s(\S*\/sha1dgst\S*)/ $1 \$(SHA1_ASM_OBJ)/;
538                 $rules.=&do_asm_rule($sha1_asm_obj,$sha1_asm_src);
539                 }
540         if (($rmd160_asm_obj ne "") && ($_ eq "CRYPTO"))
541                 {
542                 $lib_obj =~ s/\s(\S*\/rmd_dgst\S*)/ $1 \$(RMD160_ASM_OBJ)/;
543                 $rules.=&do_asm_rule($rmd160_asm_obj,$rmd160_asm_src);
544                 }
545         $defs.=&do_defs(${_}."OBJ",$lib_obj,"\$(OBJ_D)",$obj);
546         $lib=($slib)?" \$(SHLIB_CFLAGS)":" \$(LIB_CFLAGS)";
547         $rules.=&do_compile_rule("\$(OBJ_D)",$lib_obj{$_},$lib);
548         }
549
550 $defs.=&do_defs("T_EXE",$test,"\$(TEST_D)",$exep);
551 foreach (split(/\s+/,$test))
552         {
553         $t=&bname($_);
554         $tt="\$(OBJ_D)${o}$t${obj}";
555         $rules.=&do_link_rule("\$(TEST_D)$o$t$exep",$tt,"\$(LIBS_DEP)","\$(L_LIBS) \$(EX_LIBS)");
556         }
557
558 $rules.= &do_lib_rule("\$(SSLOBJ)","\$(O_SSL)",$ssl,$shlib,"\$(SO_SSL)");
559 $rules.= &do_lib_rule("\$(RSAGLUEOBJ)","\$(O_RSAGLUE)",$RSAglue,0,"")
560         unless $no_rsa;
561 $rules.= &do_lib_rule("\$(CRYPTOOBJ)","\$(O_CRYPTO)",$crypto,$shlib,"\$(SO_CRYPTO)");
562
563 $rules.=&do_link_rule("\$(BIN_D)$o\$(E_EXE)$exep","\$(E_OBJ)","\$(LIBS_DEP)","\$(L_LIBS) \$(EX_LIBS)");
564
565 print $defs;
566 print "###################################################################\n";
567 print $rules;
568
569 ###############################################
570 # strip off any trailing .[och] and append the relative directory
571 # also remembering to do nothing if we are in one of the dropped
572 # directories
573 sub var_add
574         {
575         local($dir,$val)=@_;
576         local(@a,$_,$ret);
577
578         return("") if $no_idea && $dir =~ /\/idea/;
579         return("") if $no_rc2  && $dir =~ /\/rc2/;
580         return("") if $no_rc4  && $dir =~ /\/rc4/;
581         return("") if $no_rc5  && $dir =~ /\/rc5/;
582         return("") if $no_rsa  && $dir =~ /\/rsa/;
583         return("") if $no_rsa  && $dir =~ /^rsaref/;
584         return("") if $no_dsa  && $dir =~ /\/dsa/;
585         return("") if $no_dh   && $dir =~ /\/dh/;
586         if ($no_des && $dir =~ /\/des/)
587                 {
588                 if ($val =~ /read_pwd/)
589                         { return("$dir/read_pwd "); }
590                 else
591                         { return(""); }
592                 }
593         return("") if $no_mdc2 && $dir =~ /\/mdc2/;
594         return("") if $no_sock && $dir =~ /\/proxy/;
595         return("") if $no_bf   && $dir =~ /\/bf/;
596         return("") if $no_cast && $dir =~ /\/cast/;
597
598         $val =~ s/^\s*(.*)\s*$/$1/;
599         @a=split(/\s+/,$val);
600         grep(s/\.[och]$//,@a);
601
602         @a=grep(!/^e_.*_3d$/,@a) if $no_des;
603         @a=grep(!/^e_.*_d$/,@a) if $no_des;
604         @a=grep(!/^e_.*_i$/,@a) if $no_idea;
605         @a=grep(!/^e_.*_r2$/,@a) if $no_rc2;
606         @a=grep(!/^e_.*_r5$/,@a) if $no_rc5;
607         @a=grep(!/^e_.*_bf$/,@a) if $no_bf;
608         @a=grep(!/^e_.*_c$/,@a) if $no_cast;
609         @a=grep(!/^e_rc4$/,@a) if $no_rc4;
610
611         @a=grep(!/(^s2_)|(^s23_)/,@a) if $no_ssl2;
612         @a=grep(!/(^s3_)|(^s23_)/,@a) if $no_ssl3;
613
614         @a=grep(!/(_sock$)|(_acpt$)|(_conn$)|(^pxy_)/,@a) if $no_sock;
615
616         @a=grep(!/(^md2)|(_md2$)/,@a) if $no_md2;
617         @a=grep(!/(^md5)|(_md5$)/,@a) if $no_md5;
618
619         @a=grep(!/(^d2i_r_)|(^i2d_r_)/,@a) if $no_rsa;
620         @a=grep(!/(^p_open$)|(^p_seal$)/,@a) if $no_rsa;
621         @a=grep(!/(^pem_seal$)/,@a) if $no_rsa;
622
623         @a=grep(!/(m_dss$)|(m_dss1$)/,@a) if $no_dsa;
624         @a=grep(!/(^d2i_s_)|(^i2d_s_)|(_dsap$)/,@a) if $no_dsa;
625
626         @a=grep(!/^n_pkey$/,@a) if $no_rsa || $no_rc4;
627
628         @a=grep(!/_dhp$/,@a) if $no_dh;
629
630         @a=grep(!/(^sha[^1])|(_sha$)|(m_dss$)/,@a) if $no_sha;
631         @a=grep(!/(^sha1)|(_sha1$)|(m_dss1$)/,@a) if $no_sha1;
632         @a=grep(!/_mdc2$/,@a) if $no_mdc2;
633
634         @a=grep(!/(^rsa$)|(^genrsa$)|(^req$)|(^ca$)/,@a) if $no_rsa;
635         @a=grep(!/(^dsa$)|(^gendsa$)|(^dsaparam$)/,@a) if $no_dsa;
636         @a=grep(!/^gendsa$/,@a) if $no_sha1;
637         @a=grep(!/(^dh$)|(^gendh$)/,@a) if $no_dh;
638
639         @a=grep(!/(^dh)|(_sha1$)|(m_dss1$)/,@a) if $no_sha1;
640
641         grep($_="$dir/$_",@a);
642         @a=grep(!/(^|\/)s_/,@a) if $no_sock;
643         @a=grep(!/(^|\/)bio_sock/,@a) if $no_sock;
644         $ret=join(' ',@a)." ";
645         return($ret);
646         }
647
648 # change things so that each 'token' is only separated by one space
649 sub clean_up_ws
650         {
651         local($w)=@_;
652
653         $w =~ s/^\s*(.*)\s*$/$1/;
654         $w =~ s/\s+/ /g;
655         return($w);
656         }
657
658 sub do_defs
659         {
660         local($var,$files,$location,$postfix)=@_;
661         local($_,$ret,$pf);
662         local(*OUT,$tmp,$t);
663
664         $files =~ s/\//$o/g if $o ne '/';
665         $ret="$var="; 
666         $n=1;
667         $Vars{$var}.="";
668         foreach (split(/ /,$files))
669                 {
670                 $orig=$_;
671                 $_=&bname($_) unless /^\$/;
672                 if ($n++ == 2)
673                         {
674                         $n=0;
675                         $ret.="\\\n\t";
676                         }
677                 if (($_ =~ /bss_file/) && ($postfix eq ".h"))
678                         { $pf=".c"; }
679                 else    { $pf=$postfix; }
680                 if ($_ =~ /BN_MULW/)    { $t="$_ "; }
681                 elsif ($_ =~ /DES_ENC/) { $t="$_ "; }
682                 elsif ($_ =~ /BF_ENC/)  { $t="$_ "; }
683                 elsif ($_ =~ /CAST_ENC/){ $t="$_ "; }
684                 elsif ($_ =~ /RC4_ENC/) { $t="$_ "; }
685                 elsif ($_ =~ /RC5_ENC/) { $t="$_ "; }
686                 elsif ($_ =~ /MD5_ASM/) { $t="$_ "; }
687                 elsif ($_ =~ /SHA1_ASM/){ $t="$_ "; }
688                 elsif ($_ =~ /RMD160_ASM/){ $t="$_ "; }
689                 else    { $t="$location${o}$_$pf "; }
690
691                 $Vars{$var}.="$t ";
692                 $ret.=$t;
693                 }
694         chop($ret);
695         $ret.="\n\n";
696         return($ret);
697         }
698
699 # return the name with the leading path removed
700 sub bname
701         {
702         local($ret)=@_;
703         $ret =~ s/^.*[\\\/]([^\\\/]+)$/$1/;
704         return($ret);
705         }
706
707 # do a rule for each file that says 'copy' to new direcory on change
708 sub do_copy_rule
709         {
710         local($to,$files,$p)=@_;
711         local($ret,$_,$n,$pp);
712         
713         $files =~ s/\//$o/g if $o ne '/';
714         foreach (split(/\s+/,$files))
715                 {
716                 $n=&bname($_);
717                 if ($n =~ /bss_file/)
718                         { $pp=".c"; }
719                 else    { $pp=$p; }
720                 $ret.="$to${o}$n$pp: \$(SRC_D)$o$_$pp\n\t\$(CP) \$(SRC_D)$o$_$pp $to${o}$n$pp\n\n";
721                 }
722         return($ret);
723         }
724
725 ##############################################################
726 # do a rule for each file that says 'compile' to new direcory
727 # compile the files in '$files' into $to
728 sub do_compile_rule
729         {
730         local($to,$files,$ex)=@_;
731         local($ret,$_,$n);
732         
733         $files =~ s/\//$o/g if $o ne '/';
734         foreach (split(/\s+/,$files))
735                 {
736                 $n=&bname($_);
737                 $ret.=&cc_compile_target("$to${o}$n$obj","${_}.c",$ex)
738                 }
739         return($ret);
740         }
741
742 ##############################################################
743 # do a rule for each file that says 'compile' to new direcory
744 sub cc_compile_target
745         {
746         local($target,$source,$ex_flags)=@_;
747         local($ret);
748         
749         # EAY EAY
750         $ex_flags.=' -DCFLAGS="\"$(CC) $(CFLAG)\""' if ($source =~ /cversion/);
751         $target =~ s/\//$o/g if $o ne "/";
752         $source =~ s/\//$o/g if $o ne "/";
753         $ret ="$target: \$(SRC_D)$o$source\n\t";
754         $ret.="\$(CC) ${ofile}$target $ex_flags -c \$(SRC_D)$o$source\n\n";
755         return($ret);
756         }
757
758 ##############################################################
759 sub do_asm_rule
760         {
761         local($target,$src)=@_;
762         local($ret,@s,@t,$i);
763
764         $target =~ s/\//$o/g if $o ne "/";
765         $src =~ s/\//$o/g if $o ne "/";
766
767         @s=split(/\s+/,$src);
768         @t=split(/\s+/,$target);
769
770         for ($i=0; $i<=$#s; $i++)
771                 {
772                 $ret.="$t[$i]: $s[$i]\n";
773                 $ret.="\t\$(ASM) $afile$t[$i] \$(SRC_D)$o$s[$i]\n\n";
774                 }
775         return($ret);
776         }
777
778 sub do_shlib_rule
779         {
780         local($n,$def)=@_;
781         local($ret,$nn);
782         local($t);
783
784         ($nn=$n) =~ tr/a-z/A-Z/;
785         $ret.="$n.dll: \$(${nn}OBJ)\n";
786         if ($vc && $w32)
787                 {
788                 $ret.="\t\$(MKSHLIB) $efile$n.dll $def @<<\n  \$(${nn}OBJ_F)\n<<\n";
789                 }
790         $ret.="\n";
791         return($ret);
792         }
793