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