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