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