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