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