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