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