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