This commit was manufactured by cvs2svn to create branch
[oweals/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 $banner="\t\@echo Building OpenSSL";
12
13 open(IN,"<Makefile.ssl") || die "unable to open Makefile.ssl!\n";
14 while(<IN>) {
15     $ssl_version=$1 if (/^VERSION=(.*)$/);
16     $OPTIONS=$1 if (/^OPTIONS=(.*)$/);
17     $INSTALLTOP=$1 if (/^INSTALLTOP=(.*$)/);
18 }
19 close(IN);
20
21 die "Makefile.ssl is not the toplevel Makefile!\n" if $ssl_version eq "";
22
23 $infile="MINFO";
24
25 %ops=(
26         "VC-WIN32",   "Microsoft Visual C++ [4-6] - Windows NT or 9X",
27         "VC-NT",   "Microsoft Visual C++ [4-6] - Windows NT ONLY",
28         "VC-W31-16",  "Microsoft Visual C++ 1.52 - Windows 3.1 - 286",
29         "VC-WIN16",   "Alias for VC-W31-32",
30         "VC-W31-32",  "Microsoft Visual C++ 1.52 - Windows 3.1 - 386+",
31         "VC-MSDOS","Microsoft Visual C++ 1.52 - MSDOS",
32         "Mingw32", "GNU C++ - Windows NT or 9x",
33         "Mingw32-files", "Create files with DOS copy ...",
34         "BC-NT",   "Borland C++ 4.5 - Windows NT",
35         "BC-W31",  "Borland C++ 4.5 - Windows 3.1 - PROBABLY NOT WORKING",
36         "BC-MSDOS","Borland C++ 4.5 - MSDOS",
37         "linux-elf","Linux elf",
38         "ultrix-mips","DEC mips ultrix",
39         "FreeBSD","FreeBSD distribution",
40         "OS2-EMX", "EMX GCC OS/2",
41         "default","cc under unix",
42         );
43
44 $platform="";
45 foreach (@ARGV)
46         {
47         if (!&read_options && !defined($ops{$_}))
48                 {
49                 print STDERR "unknown option - $_\n";
50                 print STDERR "usage: perl mk1mf.pl [options] [system]\n";
51                 print STDERR "\nwhere [system] can be one of the following\n";
52                 foreach $i (sort keys %ops)
53                 { printf STDERR "\t%-10s\t%s\n",$i,$ops{$i}; }
54                 print STDERR <<"EOF";
55 and [options] can be one of
56         no-md2 no-md4 no-md5 no-sha no-mdc2     - Skip this digest
57         no-ripemd
58         no-rc2 no-rc4 no-rc5 no-idea no-des     - Skip this symetric cipher
59         no-bf no-cast no-aes
60         no-rsa no-dsa no-dh                     - Skip this public key cipher
61         no-ssl2 no-ssl3                         - Skip this version of SSL
62         just-ssl                                - remove all non-ssl keys/digest
63         no-asm                                  - No x86 asm
64         no-krb5                                 - No KRB5
65         no-ec                                   - No EC
66         nasm                                    - Use NASM for x86 asm
67         gaswin                                  - Use GNU as with Mingw32
68         no-socks                                - No socket code
69         no-err                                  - No error strings
70         dll/shlib                               - Build shared libraries (MS)
71         debug                                   - Debug build
72         profile                                 - Profiling build
73         gcc                                     - Use Gcc (unix)
74
75 Values that can be set
76 TMP=tmpdir OUT=outdir SRC=srcdir BIN=binpath INC=header-outdir CC=C-compiler
77
78 -L<ex_lib_path> -l<ex_lib>                      - extra library flags (unix)
79 -<ex_cc_flags>                                  - extra 'cc' flags,
80                                                   added (MS), or replace (unix)
81 EOF
82                 exit(1);
83                 }
84         $platform=$_;
85         }
86 foreach (grep(!/^$/, split(/ /, $OPTIONS)))
87         {
88         print STDERR "unknown option - $_\n" if !&read_options;
89         }
90
91 $no_mdc2=1 if ($no_des);
92
93 $no_ssl3=1 if ($no_md5 || $no_sha);
94 $no_ssl3=1 if ($no_rsa && $no_dh);
95
96 $no_ssl2=1 if ($no_md5);
97 $no_ssl2=1 if ($no_rsa);
98
99 $out_def="out";
100 $inc_def="outinc";
101 $tmp_def="tmp";
102
103 $mkdir="mkdir";
104
105 ($ssl,$crypto)=("ssl","crypto");
106 $ranlib="echo ranlib";
107
108 $cc=(defined($VARS{'CC'}))?$VARS{'CC'}:'cc';
109 $src_dir=(defined($VARS{'SRC'}))?$VARS{'SRC'}:'.';
110 $bin_dir=(defined($VARS{'BIN'}))?$VARS{'BIN'}:'';
111
112 # $bin_dir.=$o causes a core dump on my sparc :-(
113
114 $NT=0;
115
116 push(@INC,"util/pl","pl");
117 if ($platform eq "VC-MSDOS")
118         {
119         $asmbits=16;
120         $msdos=1;
121         require 'VC-16.pl';
122         }
123 elsif ($platform eq "VC-W31-16")
124         {
125         $asmbits=16;
126         $msdos=1; $win16=1;
127         require 'VC-16.pl';
128         }
129 elsif (($platform eq "VC-W31-32") || ($platform eq "VC-WIN16"))
130         {
131         $asmbits=32;
132         $msdos=1; $win16=1;
133         require 'VC-16.pl';
134         }
135 elsif (($platform eq "VC-WIN32") || ($platform eq "VC-NT"))
136         {
137         $NT = 1 if $platform eq "VC-NT";
138         require 'VC-32.pl';
139         }
140 elsif ($platform eq "Mingw32")
141         {
142         require 'Mingw32.pl';
143         }
144 elsif ($platform eq "Mingw32-files")
145         {
146         require 'Mingw32f.pl';
147         }
148 elsif ($platform eq "BC-NT")
149         {
150         $bc=1;
151         require 'BC-32.pl';
152         }
153 elsif ($platform eq "BC-W31")
154         {
155         $bc=1;
156         $msdos=1; $w16=1;
157         require 'BC-16.pl';
158         }
159 elsif ($platform eq "BC-Q16")
160         {
161         $msdos=1; $w16=1; $shlib=0; $qw=1;
162         require 'BC-16.pl';
163         }
164 elsif ($platform eq "BC-MSDOS")
165         {
166         $asmbits=16;
167         $msdos=1;
168         require 'BC-16.pl';
169         }
170 elsif ($platform eq "FreeBSD")
171         {
172         require 'unix.pl';
173         $cflags='-DTERMIO -D_ANSI_SOURCE -O2 -fomit-frame-pointer';
174         }
175 elsif ($platform eq "linux-elf")
176         {
177         require "unix.pl";
178         require "linux.pl";
179         $unix=1;
180         }
181 elsif ($platform eq "ultrix-mips")
182         {
183         require "unix.pl";
184         require "ultrix.pl";
185         $unix=1;
186         }
187 elsif ($platform eq "OS2-EMX")
188         {
189         $wc=1;
190         require 'OS2-EMX.pl';
191         }
192 else
193         {
194         require "unix.pl";
195
196         $unix=1;
197         $cflags.=' -DTERMIO';
198         }
199
200 $out_dir=(defined($VARS{'OUT'}))?$VARS{'OUT'}:$out_def.($debug?".dbg":"");
201 $tmp_dir=(defined($VARS{'TMP'}))?$VARS{'TMP'}:$tmp_def.($debug?".dbg":"");
202 $inc_dir=(defined($VARS{'INC'}))?$VARS{'INC'}:$inc_def;
203
204 $bin_dir=$bin_dir.$o unless ((substr($bin_dir,-1,1) eq $o) || ($bin_dir eq ''));
205
206 $cflags.=" -DOPENSSL_NO_IDEA" if $no_idea;
207 $cflags.=" -DOPENSSL_NO_AES"  if $no_aes;
208 $cflags.=" -DOPENSSL_NO_RC2"  if $no_rc2;
209 $cflags.=" -DOPENSSL_NO_RC4"  if $no_rc4;
210 $cflags.=" -DOPENSSL_NO_RC5"  if $no_rc5;
211 $cflags.=" -DOPENSSL_NO_MD2"  if $no_md2;
212 $cflags.=" -DOPENSSL_NO_MD4"  if $no_md4;
213 $cflags.=" -DOPENSSL_NO_MD5"  if $no_md5;
214 $cflags.=" -DOPENSSL_NO_SHA"  if $no_sha;
215 $cflags.=" -DOPENSSL_NO_SHA1" if $no_sha1;
216 $cflags.=" -DOPENSSL_NO_RIPEMD" if $no_rmd160;
217 $cflags.=" -DOPENSSL_NO_MDC2" if $no_mdc2;
218 $cflags.=" -DOPENSSL_NO_BF"  if $no_bf;
219 $cflags.=" -DOPENSSL_NO_CAST" if $no_cast;
220 $cflags.=" -DOPENSSL_NO_DES"  if $no_des;
221 $cflags.=" -DOPENSSL_NO_RSA"  if $no_rsa;
222 $cflags.=" -DOPENSSL_NO_DSA"  if $no_dsa;
223 $cflags.=" -DOPENSSL_NO_DH"   if $no_dh;
224 $cflags.=" -DOPENSSL_NO_SOCK" if $no_sock;
225 $cflags.=" -DOPENSSL_NO_SSL2" if $no_ssl2;
226 $cflags.=" -DOPENSSL_NO_SSL3" if $no_ssl3;
227 $cflags.=" -DOPENSSL_NO_ERR"  if $no_err;
228 $cflags.=" -DOPENSSL_NO_KRB5" if $no_krb5;
229 $cflags.=" -DOPENSSL_NO_EC"   if $no_ec;
230 #$cflags.=" -DRSAref"  if $rsaref ne "";
231
232 ## if ($unix)
233 ##      { $cflags="$c_flags" if ($c_flags ne ""); }
234 ##else
235         { $cflags="$c_flags$cflags" if ($c_flags ne ""); }
236
237 $ex_libs="$l_flags$ex_libs" if ($l_flags ne "");
238
239 %shlib_ex_cflags=("SSL" => " -DOPENSSL_BUILD_SHLIBSSL",
240                   "CRYPTO" => " -DOPENSSL_BUILD_SHLIBCRYPTO");
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 BNCO_ASM_OBJ=$bnco_asm_obj
293 BNCO_ASM_SRC=$bnco_asm_src
294 DES_ENC_OBJ=$des_enc_obj
295 DES_ENC_SRC=$des_enc_src
296 BF_ENC_OBJ=$bf_enc_obj
297 BF_ENC_SRC=$bf_enc_src
298 CAST_ENC_OBJ=$cast_enc_obj
299 CAST_ENC_SRC=$cast_enc_src
300 RC4_ENC_OBJ=$rc4_enc_obj
301 RC4_ENC_SRC=$rc4_enc_src
302 RC5_ENC_OBJ=$rc5_enc_obj
303 RC5_ENC_SRC=$rc5_enc_src
304 MD5_ASM_OBJ=$md5_asm_obj
305 MD5_ASM_SRC=$md5_asm_src
306 SHA1_ASM_OBJ=$sha1_asm_obj
307 SHA1_ASM_SRC=$sha1_asm_src
308 RMD160_ASM_OBJ=$rmd160_asm_obj
309 RMD160_ASM_SRC=$rmd160_asm_src
310
311 # The output directory for everything intersting
312 OUT_D=$out_dir
313 # The output directory for all the temporary muck
314 TMP_D=$tmp_dir
315 # The output directory for the header files
316 INC_D=$inc_dir
317 INCO_D=$inc_dir${o}openssl
318
319 CP=$cp
320 RM=$rm
321 RANLIB=$ranlib
322 MKDIR=$mkdir
323 MKLIB=$bin_dir$mklib
324 MLFLAGS=$mlflags
325 ASM=$bin_dir$asm
326
327 ######################################################
328 # You should not need to touch anything below this point
329 ######################################################
330
331 E_EXE=openssl
332 SSL=$ssl
333 CRYPTO=$crypto
334
335 # BIN_D  - Binary output directory
336 # TEST_D - Binary test file output directory
337 # LIB_D  - library output directory
338 # Note: if you change these point to different directories then uncomment out
339 # the lines around the 'NB' comment below.
340
341 BIN_D=\$(OUT_D)
342 TEST_D=\$(OUT_D)
343 LIB_D=\$(OUT_D)
344
345 # INCL_D - local library directory
346 # OBJ_D  - temp object file directory
347 OBJ_D=\$(TMP_D)
348 INCL_D=\$(TMP_D)
349
350 O_SSL=     \$(LIB_D)$o$plib\$(SSL)$shlibp
351 O_CRYPTO=  \$(LIB_D)$o$plib\$(CRYPTO)$shlibp
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
359 ######################################################
360 # Don't touch anything below this point
361 ######################################################
362
363 INC=-I\$(INC_D) -I\$(INCL_D)
364 APP_CFLAGS=\$(INC) \$(CFLAG) \$(APP_CFLAG)
365 LIB_CFLAGS=\$(INC) \$(CFLAG) \$(LIB_CFLAG)
366 SHLIB_CFLAGS=\$(INC) \$(CFLAG) \$(LIB_CFLAG) \$(SHLIB_CFLAG)
367 LIBS_DEP=\$(O_CRYPTO) \$(O_SSL)
368
369 #############################################
370 EOF
371
372 $rules=<<"EOF";
373 all: banner \$(TMP_D) \$(BIN_D) \$(TEST_D) \$(LIB_D) \$(INCO_D) headers lib exe
374
375 banner:
376 $banner
377
378 \$(TMP_D):
379         \$(MKDIR) \$(TMP_D)
380 # NB: uncomment out these lines if BIN_D, TEST_D and LIB_D are different
381 #\$(BIN_D):
382 #       \$(MKDIR) \$(BIN_D)
383 #
384 #\$(TEST_D):
385 #       \$(MKDIR) \$(TEST_D)
386
387 \$(LIB_D):
388         \$(MKDIR) \$(LIB_D)
389
390 \$(INCO_D): \$(INC_D)
391         \$(MKDIR) \$(INCO_D)
392
393 \$(INC_D):
394         \$(MKDIR) \$(INC_D)
395
396 headers: \$(HEADER) \$(EXHEADER)
397         @
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 my $platform_cpp_symbol = "MK1MF_PLATFORM_$platform";
424 $platform_cpp_symbol =~ s/-/_/g;
425 if (open(IN,"crypto/buildinf.h"))
426         {
427         # Remove entry for this platform in existing file buildinf.h.
428
429         my $old_buildinf_h = "";
430         while (<IN>)
431                 {
432                 if (/^\#ifdef $platform_cpp_symbol$/)
433                         {
434                         while (<IN>) { last if (/^\#endif/); }
435                         }
436                 else
437                         {
438                         $old_buildinf_h .= $_;
439                         }
440                 }
441         close(IN);
442
443         open(OUT,">crypto/buildinf.h") || die "Can't open buildinf.h";
444         print OUT $old_buildinf_h;
445         close(OUT);
446         }
447
448 open (OUT,">>crypto/buildinf.h") || die "Can't open buildinf.h";
449 printf OUT <<EOF;
450 #ifdef $platform_cpp_symbol
451   /* auto-generated/updated by util/mk1mf.pl for crypto/cversion.c */
452   #define CFLAGS "$cc $cflags"
453   #define PLATFORM "$platform"
454 EOF
455 printf OUT "  #define DATE \"%s\"\n", scalar gmtime();
456 printf OUT "#endif\n";
457 close(OUT);
458
459 #############################################
460 # We parse in input file and 'store' info for later printing.
461 open(IN,"<$infile") || die "unable to open $infile:$!\n";
462 $_=<IN>;
463 for (;;)
464         {
465         chop;
466
467         ($key,$val)=/^([^=]+)=(.*)/;
468         if ($key eq "RELATIVE_DIRECTORY")
469                 {
470                 if ($lib ne "")
471                         {
472                         $uc=$lib;
473                         $uc =~ s/^lib(.*)\.a/$1/;
474                         $uc =~ tr/a-z/A-Z/;
475                         $lib_nam{$uc}=$uc;
476                         $lib_obj{$uc}.=$libobj." ";
477                         }
478                 last if ($val eq "FINISHED");
479                 $lib="";
480                 $libobj="";
481                 $dir=$val;
482                 }
483
484         if ($key eq "TEST")
485                 { $test.=&var_add($dir,$val); }
486
487         if (($key eq "PROGS") || ($key eq "E_OBJ"))
488                 { $e_exe.=&var_add($dir,$val); }
489
490         if ($key eq "LIB")
491                 {
492                 $lib=$val;
493                 $lib =~ s/^.*\/([^\/]+)$/$1/;
494                 }
495
496         if ($key eq "EXHEADER")
497                 { $exheader.=&var_add($dir,$val); }
498
499         if ($key eq "HEADER")
500                 { $header.=&var_add($dir,$val); }
501
502         if ($key eq "LIBOBJ")
503                 { $libobj=&var_add($dir,$val); }
504
505         if (!($_=<IN>))
506                 { $_="RELATIVE_DIRECTORY=FINISHED\n"; }
507         }
508 close(IN);
509
510 # Strip of trailing ' '
511 foreach (keys %lib_obj) { $lib_obj{$_}=&clean_up_ws($lib_obj{$_}); }
512 $test=&clean_up_ws($test);
513 $e_exe=&clean_up_ws($e_exe);
514 $exheader=&clean_up_ws($exheader);
515 $header=&clean_up_ws($header);
516
517 # First we strip the exheaders from the headers list
518 foreach (split(/\s+/,$exheader)){ $h{$_}=1; }
519 foreach (split(/\s+/,$header))  { $h.=$_." " unless $h{$_}; }
520 chop($h); $header=$h;
521
522 $defs.=&do_defs("HEADER",$header,"\$(INCL_D)",".h");
523 $rules.=&do_copy_rule("\$(INCL_D)",$header,".h");
524
525 $defs.=&do_defs("EXHEADER",$exheader,"\$(INCO_D)",".h");
526 $rules.=&do_copy_rule("\$(INCO_D)",$exheader,".h");
527
528 $defs.=&do_defs("T_OBJ",$test,"\$(OBJ_D)",$obj);
529 $rules.=&do_compile_rule("\$(OBJ_D)",$test,"\$(APP_CFLAGS)");
530
531 $defs.=&do_defs("E_OBJ",$e_exe,"\$(OBJ_D)",$obj);
532 $rules.=&do_compile_rule("\$(OBJ_D)",$e_exe,'-DMONOLITH $(APP_CFLAGS)');
533
534 foreach (values %lib_nam)
535         {
536         $lib_obj=$lib_obj{$_};
537         local($slib)=$shlib;
538
539         if (($_ eq "SSL") && $no_ssl2 && $no_ssl3)
540                 {
541                 $rules.="\$(O_SSL):\n\n"; 
542                 next;
543                 }
544
545         if (($bn_asm_obj ne "") && ($_ eq "CRYPTO"))
546                 {
547                 $lib_obj =~ s/\s\S*\/bn_asm\S*/ \$(BN_ASM_OBJ)/;
548                 $rules.=&do_asm_rule($bn_asm_obj,$bn_asm_src);
549                 }
550         if (($bnco_asm_obj ne "") && ($_ eq "CRYPTO"))
551                 {
552                 $lib_obj .= "\$(BNCO_ASM_OBJ)";
553                 $rules.=&do_asm_rule($bnco_asm_obj,$bnco_asm_src);
554                 }
555         if (($des_enc_obj ne "") && ($_ eq "CRYPTO"))
556                 {
557                 $lib_obj =~ s/\s\S*des_enc\S*/ \$(DES_ENC_OBJ)/;
558                 $lib_obj =~ s/\s\S*\/fcrypt_b\S*\s*/ /;
559                 $rules.=&do_asm_rule($des_enc_obj,$des_enc_src);
560                 }
561         if (($bf_enc_obj ne "") && ($_ eq "CRYPTO"))
562                 {
563                 $lib_obj =~ s/\s\S*\/bf_enc\S*/ \$(BF_ENC_OBJ)/;
564                 $rules.=&do_asm_rule($bf_enc_obj,$bf_enc_src);
565                 }
566         if (($cast_enc_obj ne "") && ($_ eq "CRYPTO"))
567                 {
568                 $lib_obj =~ s/(\s\S*\/c_enc\S*)/ \$(CAST_ENC_OBJ)/;
569                 $rules.=&do_asm_rule($cast_enc_obj,$cast_enc_src);
570                 }
571         if (($rc4_enc_obj ne "") && ($_ eq "CRYPTO"))
572                 {
573                 $lib_obj =~ s/\s\S*\/rc4_enc\S*/ \$(RC4_ENC_OBJ)/;
574                 $rules.=&do_asm_rule($rc4_enc_obj,$rc4_enc_src);
575                 }
576         if (($rc5_enc_obj ne "") && ($_ eq "CRYPTO"))
577                 {
578                 $lib_obj =~ s/\s\S*\/rc5_enc\S*/ \$(RC5_ENC_OBJ)/;
579                 $rules.=&do_asm_rule($rc5_enc_obj,$rc5_enc_src);
580                 }
581         if (($md5_asm_obj ne "") && ($_ eq "CRYPTO"))
582                 {
583                 $lib_obj =~ s/\s(\S*\/md5_dgst\S*)/ $1 \$(MD5_ASM_OBJ)/;
584                 $rules.=&do_asm_rule($md5_asm_obj,$md5_asm_src);
585                 }
586         if (($sha1_asm_obj ne "") && ($_ eq "CRYPTO"))
587                 {
588                 $lib_obj =~ s/\s(\S*\/sha1dgst\S*)/ $1 \$(SHA1_ASM_OBJ)/;
589                 $rules.=&do_asm_rule($sha1_asm_obj,$sha1_asm_src);
590                 }
591         if (($rmd160_asm_obj ne "") && ($_ eq "CRYPTO"))
592                 {
593                 $lib_obj =~ s/\s(\S*\/rmd_dgst\S*)/ $1 \$(RMD160_ASM_OBJ)/;
594                 $rules.=&do_asm_rule($rmd160_asm_obj,$rmd160_asm_src);
595                 }
596         $defs.=&do_defs(${_}."OBJ",$lib_obj,"\$(OBJ_D)",$obj);
597         $lib=($slib)?" \$(SHLIB_CFLAGS)".$shlib_ex_cflags{$_}:" \$(LIB_CFLAGS)";
598         $rules.=&do_compile_rule("\$(OBJ_D)",$lib_obj{$_},$lib);
599         }
600
601 $defs.=&do_defs("T_EXE",$test,"\$(TEST_D)",$exep);
602 foreach (split(/\s+/,$test))
603         {
604         $t=&bname($_);
605         $tt="\$(OBJ_D)${o}$t${obj}";
606         $rules.=&do_link_rule("\$(TEST_D)$o$t$exep",$tt,"\$(LIBS_DEP)","\$(L_LIBS) \$(EX_LIBS)");
607         }
608
609 $rules.= &do_lib_rule("\$(SSLOBJ)","\$(O_SSL)",$ssl,$shlib,"\$(SO_SSL)");
610 $rules.= &do_lib_rule("\$(CRYPTOOBJ)","\$(O_CRYPTO)",$crypto,$shlib,"\$(SO_CRYPTO)");
611
612 $rules.=&do_link_rule("\$(BIN_D)$o\$(E_EXE)$exep","\$(E_OBJ)","\$(LIBS_DEP)","\$(L_LIBS) \$(EX_LIBS)");
613
614 print $defs;
615
616 if ($platform eq "linux-elf") {
617     print <<"EOF";
618 # Generate perlasm output files
619 %.cpp:
620         (cd \$(\@D)/..; PERL=perl make -f Makefile.ssl asm/\$(\@F))
621 EOF
622 }
623 print "###################################################################\n";
624 print $rules;
625
626 ###############################################
627 # strip off any trailing .[och] and append the relative directory
628 # also remembering to do nothing if we are in one of the dropped
629 # directories
630 sub var_add
631         {
632         local($dir,$val)=@_;
633         local(@a,$_,$ret);
634
635         return("") if $no_idea && $dir =~ /\/idea/;
636         return("") if $no_aes  && $dir =~ /\/aes/;
637         return("") if $no_rc2  && $dir =~ /\/rc2/;
638         return("") if $no_rc4  && $dir =~ /\/rc4/;
639         return("") if $no_rc5  && $dir =~ /\/rc5/;
640         return("") if $no_rsa  && $dir =~ /\/rsa/;
641         return("") if $no_rsa  && $dir =~ /^rsaref/;
642         return("") if $no_dsa  && $dir =~ /\/dsa/;
643         return("") if $no_dh   && $dir =~ /\/dh/;
644         if ($no_des && $dir =~ /\/des/)
645                 {
646                 if ($val =~ /read_pwd/)
647                         { return("$dir/read_pwd "); }
648                 else
649                         { return(""); }
650                 }
651         return("") if $no_mdc2 && $dir =~ /\/mdc2/;
652         return("") if $no_sock && $dir =~ /\/proxy/;
653         return("") if $no_bf   && $dir =~ /\/bf/;
654         return("") if $no_cast && $dir =~ /\/cast/;
655
656         $val =~ s/^\s*(.*)\s*$/$1/;
657         @a=split(/\s+/,$val);
658         grep(s/\.[och]$//,@a);
659
660         @a=grep(!/^e_.*_3d$/,@a) if $no_des;
661         @a=grep(!/^e_.*_d$/,@a) if $no_des;
662         @a=grep(!/^e_.*_ae$/,@a) if $no_idea;
663         @a=grep(!/^e_.*_i$/,@a) if $no_aes;
664         @a=grep(!/^e_.*_r2$/,@a) if $no_rc2;
665         @a=grep(!/^e_.*_r5$/,@a) if $no_rc5;
666         @a=grep(!/^e_.*_bf$/,@a) if $no_bf;
667         @a=grep(!/^e_.*_c$/,@a) if $no_cast;
668         @a=grep(!/^e_rc4$/,@a) if $no_rc4;
669
670         @a=grep(!/(^s2_)|(^s23_)/,@a) if $no_ssl2;
671         @a=grep(!/(^s3_)|(^s23_)/,@a) if $no_ssl3;
672
673         @a=grep(!/(_sock$)|(_acpt$)|(_conn$)|(^pxy_)/,@a) if $no_sock;
674
675         @a=grep(!/(^md2)|(_md2$)/,@a) if $no_md2;
676         @a=grep(!/(^md4)|(_md4$)/,@a) if $no_md4;
677         @a=grep(!/(^md5)|(_md5$)/,@a) if $no_md5;
678         @a=grep(!/(rmd)|(ripemd)/,@a) if $no_rmd160;
679
680         @a=grep(!/(^d2i_r_)|(^i2d_r_)/,@a) if $no_rsa;
681         @a=grep(!/(^p_open$)|(^p_seal$)/,@a) if $no_rsa;
682         @a=grep(!/(^pem_seal$)/,@a) if $no_rsa;
683
684         @a=grep(!/(m_dss$)|(m_dss1$)/,@a) if $no_dsa;
685         @a=grep(!/(^d2i_s_)|(^i2d_s_)|(_dsap$)/,@a) if $no_dsa;
686
687         @a=grep(!/^n_pkey$/,@a) if $no_rsa || $no_rc4;
688
689         @a=grep(!/_dhp$/,@a) if $no_dh;
690
691         @a=grep(!/(^sha[^1])|(_sha$)|(m_dss$)/,@a) if $no_sha;
692         @a=grep(!/(^sha1)|(_sha1$)|(m_dss1$)/,@a) if $no_sha1;
693         @a=grep(!/_mdc2$/,@a) if $no_mdc2;
694
695         @a=grep(!/(^rsa$)|(^genrsa$)/,@a) if $no_rsa;
696         @a=grep(!/(^dsa$)|(^gendsa$)|(^dsaparam$)/,@a) if $no_dsa;
697         @a=grep(!/^gendsa$/,@a) if $no_sha1;
698         @a=grep(!/(^dh$)|(^gendh$)/,@a) if $no_dh;
699
700         @a=grep(!/(^dh)|(_sha1$)|(m_dss1$)/,@a) if $no_sha1;
701
702         grep($_="$dir/$_",@a);
703         @a=grep(!/(^|\/)s_/,@a) if $no_sock;
704         @a=grep(!/(^|\/)bio_sock/,@a) if $no_sock;
705         $ret=join(' ',@a)." ";
706         return($ret);
707         }
708
709 # change things so that each 'token' is only separated by one space
710 sub clean_up_ws
711         {
712         local($w)=@_;
713
714         $w =~ s/^\s*(.*)\s*$/$1/;
715         $w =~ s/\s+/ /g;
716         return($w);
717         }
718
719 sub do_defs
720         {
721         local($var,$files,$location,$postfix)=@_;
722         local($_,$ret,$pf);
723         local(*OUT,$tmp,$t);
724
725         $files =~ s/\//$o/g if $o ne '/';
726         $ret="$var="; 
727         $n=1;
728         $Vars{$var}.="";
729         foreach (split(/ /,$files))
730                 {
731                 $orig=$_;
732                 $_=&bname($_) unless /^\$/;
733                 if ($n++ == 2)
734                         {
735                         $n=0;
736                         $ret.="\\\n\t";
737                         }
738                 if (($_ =~ /bss_file/) && ($postfix eq ".h"))
739                         { $pf=".c"; }
740                 else    { $pf=$postfix; }
741                 if ($_ =~ /BN_ASM/)     { $t="$_ "; }
742                 elsif ($_ =~ /BNCO_ASM/){ $t="$_ "; }
743                 elsif ($_ =~ /DES_ENC/) { $t="$_ "; }
744                 elsif ($_ =~ /BF_ENC/)  { $t="$_ "; }
745                 elsif ($_ =~ /CAST_ENC/){ $t="$_ "; }
746                 elsif ($_ =~ /RC4_ENC/) { $t="$_ "; }
747                 elsif ($_ =~ /RC5_ENC/) { $t="$_ "; }
748                 elsif ($_ =~ /MD5_ASM/) { $t="$_ "; }
749                 elsif ($_ =~ /SHA1_ASM/){ $t="$_ "; }
750                 elsif ($_ =~ /RMD160_ASM/){ $t="$_ "; }
751                 else    { $t="$location${o}$_$pf "; }
752
753                 $Vars{$var}.="$t ";
754                 $ret.=$t;
755                 }
756         chop($ret);
757         $ret.="\n\n";
758         return($ret);
759         }
760
761 # return the name with the leading path removed
762 sub bname
763         {
764         local($ret)=@_;
765         $ret =~ s/^.*[\\\/]([^\\\/]+)$/$1/;
766         return($ret);
767         }
768
769
770 ##############################################################
771 # do a rule for each file that says 'compile' to new direcory
772 # compile the files in '$files' into $to
773 sub do_compile_rule
774         {
775         local($to,$files,$ex)=@_;
776         local($ret,$_,$n);
777         
778         $files =~ s/\//$o/g if $o ne '/';
779         foreach (split(/\s+/,$files))
780                 {
781                 $n=&bname($_);
782                 $ret.=&cc_compile_target("$to${o}$n$obj","${_}.c",$ex)
783                 }
784         return($ret);
785         }
786
787 ##############################################################
788 # do a rule for each file that says 'compile' to new direcory
789 sub cc_compile_target
790         {
791         local($target,$source,$ex_flags)=@_;
792         local($ret);
793         
794         $ex_flags.=" -DMK1MF_BUILD -D$platform_cpp_symbol" if ($source =~ /cversion/);
795         $target =~ s/\//$o/g if $o ne "/";
796         $source =~ s/\//$o/g if $o ne "/";
797         $ret ="$target: \$(SRC_D)$o$source\n\t";
798         $ret.="\$(CC) ${ofile}$target $ex_flags -c \$(SRC_D)$o$source\n\n";
799         return($ret);
800         }
801
802 ##############################################################
803 sub do_asm_rule
804         {
805         local($target,$src)=@_;
806         local($ret,@s,@t,$i);
807
808         $target =~ s/\//$o/g if $o ne "/";
809         $src =~ s/\//$o/g if $o ne "/";
810
811         @s=split(/\s+/,$src);
812         @t=split(/\s+/,$target);
813
814         for ($i=0; $i<=$#s; $i++)
815                 {
816                 $ret.="$t[$i]: $s[$i]\n";
817                 $ret.="\t\$(ASM) $afile$t[$i] \$(SRC_D)$o$s[$i]\n\n";
818                 }
819         return($ret);
820         }
821
822 sub do_shlib_rule
823         {
824         local($n,$def)=@_;
825         local($ret,$nn);
826         local($t);
827
828         ($nn=$n) =~ tr/a-z/A-Z/;
829         $ret.="$n.dll: \$(${nn}OBJ)\n";
830         if ($vc && $w32)
831                 {
832                 $ret.="\t\$(MKSHLIB) $efile$n.dll $def @<<\n  \$(${nn}OBJ_F)\n<<\n";
833                 }
834         $ret.="\n";
835         return($ret);
836         }
837
838 # do a rule for each file that says 'copy' to new direcory on change
839 sub do_copy_rule
840         {
841         local($to,$files,$p)=@_;
842         local($ret,$_,$n,$pp);
843         
844         $files =~ s/\//$o/g if $o ne '/';
845         foreach (split(/\s+/,$files))
846                 {
847                 $n=&bname($_);
848                 if ($n =~ /bss_file/)
849                         { $pp=".c"; }
850                 else    { $pp=$p; }
851                 $ret.="$to${o}$n$pp: \$(SRC_D)$o$_$pp\n\t\$(CP) \$(SRC_D)$o$_$pp $to${o}$n$pp\n\n";
852                 }
853         return($ret);
854         }
855
856 sub read_options
857         {
858         if    (/^no-rc2$/)      { $no_rc2=1; }
859         elsif (/^no-rc4$/)      { $no_rc4=1; }
860         elsif (/^no-rc5$/)      { $no_rc5=1; }
861         elsif (/^no-idea$/)     { $no_idea=1; }
862         elsif (/^no-aes$/)      { $no_aes=1; }
863         elsif (/^no-des$/)      { $no_des=1; }
864         elsif (/^no-bf$/)       { $no_bf=1; }
865         elsif (/^no-cast$/)     { $no_cast=1; }
866         elsif (/^no-md2$/)      { $no_md2=1; }
867         elsif (/^no-md4$/)      { $no_md4=1; }
868         elsif (/^no-md5$/)      { $no_md5=1; }
869         elsif (/^no-sha$/)      { $no_sha=1; }
870         elsif (/^no-sha1$/)     { $no_sha1=1; }
871         elsif (/^no-ripemd$/)   { $no_ripemd=1; }
872         elsif (/^no-mdc2$/)     { $no_mdc2=1; }
873         elsif (/^no-patents$/)  { $no_rc2=$no_rc4=$no_rc5=$no_idea=$no_rsa=1; }
874         elsif (/^no-rsa$/)      { $no_rsa=1; }
875         elsif (/^no-dsa$/)      { $no_dsa=1; }
876         elsif (/^no-dh$/)       { $no_dh=1; }
877         elsif (/^no-hmac$/)     { $no_hmac=1; }
878         elsif (/^no-aes$/)      { $no_aes=1; }
879         elsif (/^no-asm$/)      { $no_asm=1; }
880         elsif (/^nasm$/)        { $nasm=1; }
881         elsif (/^gaswin$/)      { $gaswin=1; }
882         elsif (/^no-ssl2$/)     { $no_ssl2=1; }
883         elsif (/^no-ssl3$/)     { $no_ssl3=1; }
884         elsif (/^no-err$/)      { $no_err=1; }
885         elsif (/^no-sock$/)     { $no_sock=1; }
886         elsif (/^no-krb5$/)     { $no_krb5=1; }
887         elsif (/^no-ec$/)       { $no_ec=1; }
888
889         elsif (/^just-ssl$/)    { $no_rc2=$no_idea=$no_des=$no_bf=$no_cast=1;
890                                   $no_md2=$no_sha=$no_mdc2=$no_dsa=$no_dh=1;
891                                   $no_ssl2=$no_err=$no_rmd160=$no_rc5=1;
892                                   $no_aes=1; }
893
894         elsif (/^rsaref$/)      { }
895         elsif (/^gcc$/)         { $gcc=1; }
896         elsif (/^debug$/)       { $debug=1; }
897         elsif (/^profile$/)     { $profile=1; }
898         elsif (/^shlib$/)       { $shlib=1; }
899         elsif (/^dll$/)         { $shlib=1; }
900         elsif (/^shared$/)      { } # We just need to ignore it for now...
901         elsif (/^([^=]*)=(.*)$/){ $VARS{$1}=$2; }
902         elsif (/^-[lL].*$/)     { $l_flags.="$_ "; }
903         elsif ((!/^-help/) && (!/^-h/) && (!/^-\?/) && /^-.*$/)
904                 { $c_flags.="$_ "; }
905         else { return(0); }
906         return(1);
907         }