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