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