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