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