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