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