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