Use #error in openssl/srp.h
[openssl.git] / util / mk1mf.pl
1 #!/usr/bin/env 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 (if the target is not "copy").
6 # If the target is "copy", then it tries to create a makefile that can be
7 # safely used with the -j flag and that is compatible with the top-level
8 # Makefile, in the sense that it uses the same options and assembler files etc.
9
10 use Cwd;
11
12 $INSTALLTOP="/usr/local/ssl";
13 $OPENSSLDIR="/usr/local/ssl";
14 $OPTIONS="";
15 $ssl_version="";
16 $banner="\t\@echo Building OpenSSL";
17
18 my $no_static_engine = 1;
19 my $engines = "";
20 my @engines_obj = "";
21 my $otherlibs = "";
22 local $zlib_opt = 0;    # 0 = no zlib, 1 = static, 2 = dynamic
23 local $zlib_lib = "";
24 local $perl_asm = 0;    # 1 to autobuild asm files from perl scripts
25
26 local $fips_canister_path = "";
27 my $fips_premain_dso_exe_path = "";
28 my $fips_premain_c_path = "";
29 my $fips_sha1_exe_path = "";
30
31 local $fipscanisterbuild = 0;
32
33 my $fipscanisteronly = 0;
34
35 my $fipslibdir = "";
36 my $baseaddr = "";
37
38 my $ex_l_libs = "";
39
40 my $build_targets = "lib exe";
41 my $libs_dep = "\$(O_CRYPTO) \$(O_SSL)";
42
43 # Options to import from top level Makefile
44
45 my %mf_import = (
46         VERSION        => \$ssl_version,
47         OPTIONS        => \$OPTIONS,
48         INSTALLTOP     => \$INSTALLTOP,
49         OPENSSLDIR     => \$OPENSSLDIR,
50         PLATFORM       => \$mf_platform,
51         CC             => \$mf_cc,
52         CFLAG          => \$mf_cflag,
53         DEPFLAG        => \$mf_depflag,
54         CPUID_OBJ      => \$mf_cpuid_asm,
55         BN_ASM         => \$mf_bn_asm,
56         DES_ENC        => \$mf_des_asm,
57         AES_ENC        => \$mf_aes_asm,
58         BF_ENC         => \$mf_bf_asm,
59         CAST_ENC       => \$mf_cast_asm,
60         RC4_ENC        => \$mf_rc4_asm,
61         RC5_ENC        => \$mf_rc5_asm,
62         MD5_ASM_OBJ    => \$mf_md5_asm,
63         SHA1_ASM_OBJ   => \$mf_sha_asm,
64         RMD160_ASM_OBJ => \$mf_rmd_asm,
65         WP_ASM_OBJ     => \$mf_wp_asm,
66         CMLL_ENC       => \$mf_cm_asm,
67         MODES_ASM_OBJ  => \$mf_modes_asm,
68         ENGINES_ASM_OBJ=> \$mf_engines_asm,
69         PERLASM_SCHEME => \$mf_perlasm_scheme,
70         FIPSCANISTERONLY  => \$mf_fipscanisteronly,
71         FIPSCANISTERINTERNAL  => \$mf_fipscanisterinternal,
72         EC_ASM         => \$mf_ec_asm,
73 );
74
75 open(IN,"<Makefile") || die "unable to open Makefile!\n";
76 while(<IN>) {
77     my ($mf_opt, $mf_ref);
78     while (($mf_opt, $mf_ref) = each %mf_import) {
79         if (/^$mf_opt\s*=\s*(.*)$/ && !defined($$mfref)) {
80            $$mf_ref = $1;
81         }
82     }
83 }
84 close(IN);
85
86 $debug = 1 if $mf_platform =~ /^debug-/;
87
88 if ($mf_fipscanisterinternal eq "y") {
89         $fips = 1;
90         $fipscanisterbuild = 1;
91         $fipscanisteronly = 1;
92 }
93
94
95 die "Makefile is not the toplevel Makefile!\n" if $ssl_version eq "";
96
97 $infile="MINFO";
98
99 %ops=(
100         "VC-WIN32",   "Microsoft Visual C++ [4-6] - Windows NT or 9X",
101         "VC-WIN64I",  "Microsoft C/C++ - Win64/IA-64",
102         "VC-WIN64A",  "Microsoft C/C++ - Win64/x64",
103         "VC-CE",   "Microsoft eMbedded Visual C++ 3.0 - Windows CE ONLY",
104         "VC-NT",   "Microsoft Visual C++ [4-6] - Windows NT ONLY",
105         "Mingw32", "GNU C++ - Windows NT or 9x",
106         "Mingw32-files", "Create files with DOS copy ...",
107         "BC-NT",   "Borland C++ 4.5 - Windows NT",
108         "linux-elf","Linux elf",
109         "ultrix-mips","DEC mips ultrix",
110         "FreeBSD","FreeBSD distribution",
111         "OS2-EMX", "EMX GCC OS/2",
112         "netware-clib", "CodeWarrior for NetWare - CLib - with WinSock Sockets",
113         "netware-clib-bsdsock", "CodeWarrior for NetWare - CLib - with BSD Sockets",
114         "netware-libc", "CodeWarrior for NetWare - LibC - with WinSock Sockets",
115         "netware-libc-bsdsock", "CodeWarrior for NetWare - LibC - with BSD Sockets",
116         "default","cc under unix",
117         "auto", "auto detect from top level Makefile",
118         "copy", "copy from top level Makefile"
119         );
120
121 $platform="";
122 my $xcflags="";
123 foreach (@ARGV)
124         {
125         if (!&read_options && !defined($ops{$_}))
126                 {
127                 print STDERR "unknown option - $_\n";
128                 print STDERR "usage: perl mk1mf.pl [options] [system]\n";
129                 print STDERR "\nwhere [system] can be one of the following\n";
130                 foreach $i (sort keys %ops)
131                 { printf STDERR "\t%-10s\t%s\n",$i,$ops{$i}; }
132                 print STDERR <<"EOF";
133 and [options] can be one of
134         no-md2 no-md4 no-md5 no-sha no-mdc2     - Skip this digest
135         no-ripemd
136         no-rc2 no-rc4 no-rc5 no-idea no-des     - Skip this symetric cipher
137         no-bf no-cast no-aes no-camellia no-seed
138         no-rsa no-dsa no-dh                     - Skip this public key cipher
139         no-ssl3                                 - Skip this version of SSL
140         just-ssl                                - remove all non-ssl keys/digest
141         no-asm                                  - No x86 asm
142         no-srp                                  - No SRP
143         no-ec                                   - No EC
144         no-engine                               - No engine
145         no-hw                                   - No hw
146         nasm                                    - Use NASM for x86 asm
147         nw-nasm                                 - Use NASM x86 asm for NetWare
148         nw-mwasm                                - Use Metrowerks x86 asm for NetWare
149         gaswin                                  - Use GNU as with Mingw32
150         no-socks                                - No socket code
151         no-err                                  - No error strings
152         dll/shlib                               - Build shared libraries (MS)
153         debug                                   - Debug build
154         profile                                 - Profiling build
155         gcc                                     - Use Gcc (unix)
156
157 Values that can be set
158 TMP=tmpdir OUT=outdir SRC=srcdir BIN=binpath INC=header-outdir CC=C-compiler
159
160 -L<ex_lib_path> -l<ex_lib>                      - extra library flags (unix)
161 -<ex_cc_flags>                                  - extra 'cc' flags,
162                                                   added (MS), or replace (unix)
163 EOF
164                 exit(1);
165                 }
166         $platform=$_;
167         }
168 foreach (grep(!/^$/, split(/ /, $OPTIONS)))
169         {
170         print STDERR "unknown option - $_\n" if !&read_options;
171         }
172
173 $no_static_engine = 0 if (!$shlib);
174
175 $no_mdc2=1 if ($no_des);
176
177 $no_ssl3=1 if ($no_md5);
178 $no_ssl3=1 if ($no_rsa && $no_dh);
179
180 $out_def="out";
181 $inc_def="outinc";
182 $tmp_def="tmp";
183
184 $perl="perl" unless defined $perl;
185 $mkdir="-mkdir" unless defined $mkdir;
186
187 ($ssl,$crypto)=("ssl","crypto");
188 $ranlib="echo ranlib";
189
190 $cc=(defined($VARS{'CC'}))?$VARS{'CC'}:'cc';
191 $src_dir=(defined($VARS{'SRC'}))?$VARS{'SRC'}: $platform eq 'copy' ? getcwd() : '.';
192 $bin_dir=(defined($VARS{'BIN'}))?$VARS{'BIN'}:'';
193
194 # $bin_dir.=$o causes a core dump on my sparc :-(
195
196
197 $NT=0;
198
199 push(@INC,"util/pl","pl");
200
201 if ($platform eq "auto" || $platform eq 'copy') {
202         $orig_platform = $platform;
203         $platform = $mf_platform;
204         print STDERR "Imported platform $mf_platform\n";
205 }
206
207 if (($platform =~ /VC-(.+)/))
208         {
209         $FLAVOR=$1;
210         $NT = 1 if $1 eq "NT";
211         require 'VC-32.pl';
212         }
213 elsif ($platform eq "Mingw32")
214         {
215         require 'Mingw32.pl';
216         }
217 elsif ($platform eq "Mingw32-files")
218         {
219         require 'Mingw32f.pl';
220         }
221 elsif ($platform eq "BC-NT")
222         {
223         $bc=1;
224         require 'BC-32.pl';
225         }
226 elsif ($platform eq "FreeBSD")
227         {
228         require 'unix.pl';
229         $cflags='-DTERMIO -D_ANSI_SOURCE -O2 -fomit-frame-pointer';
230         }
231 elsif ($platform eq "linux-elf")
232         {
233         require "unix.pl";
234         require "linux.pl";
235         $unix=1;
236         }
237 elsif ($platform eq "ultrix-mips")
238         {
239         require "unix.pl";
240         require "ultrix.pl";
241         $unix=1;
242         }
243 elsif ($platform eq "OS2-EMX")
244         {
245         $wc=1;
246         require 'OS2-EMX.pl';
247         }
248 elsif (($platform eq "netware-clib") || ($platform eq "netware-libc") ||
249        ($platform eq "netware-clib-bsdsock") || ($platform eq "netware-libc-bsdsock"))
250         {
251         $LIBC=1 if $platform eq "netware-libc" || $platform eq "netware-libc-bsdsock";
252         $BSDSOCK=1 if ($platform eq "netware-libc-bsdsock") || ($platform eq "netware-clib-bsdsock");
253         require 'netware.pl';
254         }
255 else
256         {
257         require "unix.pl";
258
259         $unix=1;
260         $cflags.=' -DTERMIO';
261         }
262
263 $out_dir=(defined($VARS{'OUT'}))?$VARS{'OUT'}:$out_def.($debug?".dbg":"");
264 $tmp_dir=(defined($VARS{'TMP'}))?$VARS{'TMP'}:$tmp_def.($debug?".dbg":"");
265 $inc_dir=(defined($VARS{'INC'}))?$VARS{'INC'}:$inc_def;
266
267 $bin_dir=$bin_dir.$o unless ((substr($bin_dir,-1,1) eq $o) || ($bin_dir eq ''));
268
269 $cflags= "$xcflags$cflags" if $xcflags ne "";
270
271 $cflags.=" -DOPENSSL_NO_IDEA" if $no_idea;
272 $cflags.=" -DOPENSSL_NO_AES"  if $no_aes;
273 $cflags.=" -DOPENSSL_NO_CAMELLIA"  if $no_camellia;
274 $cflags.=" -DOPENSSL_NO_SEED" if $no_seed;
275 $cflags.=" -DOPENSSL_NO_RC2"  if $no_rc2;
276 $cflags.=" -DOPENSSL_NO_RC4"  if $no_rc4;
277 $cflags.=" -DOPENSSL_NO_RC5"  if $no_rc5;
278 $cflags.=" -DOPENSSL_NO_MD2"  if $no_md2;
279 $cflags.=" -DOPENSSL_NO_MD4"  if $no_md4;
280 $cflags.=" -DOPENSSL_NO_MD5"  if $no_md5;
281 $cflags.=" -DOPENSSL_NO_RMD160" if $no_ripemd;
282 $cflags.=" -DOPENSSL_NO_MDC2" if $no_mdc2;
283 $cflags.=" -DOPENSSL_NO_BF"  if $no_bf;
284 $cflags.=" -DOPENSSL_NO_CAST" if $no_cast;
285 $cflags.=" -DOPENSSL_NO_DES"  if $no_des;
286 $cflags.=" -DOPENSSL_NO_RSA"  if $no_rsa;
287 $cflags.=" -DOPENSSL_NO_DSA"  if $no_dsa;
288 $cflags.=" -DOPENSSL_NO_DH"   if $no_dh;
289 $cflags.=" -DOPENSSL_NO_WHIRLPOOL"   if $no_whirlpool;
290 $cflags.=" -DOPENSSL_NO_SOCK" if $no_sock;
291 $cflags.=" -DOPENSSL_NO_SSL3" if $no_ssl3;
292 $cflags.=" -DOPENSSL_NO_TLSEXT" if $no_tlsext;
293 $cflags.=" -DOPENSSL_NO_SRP" if $no_srp;
294 $cflags.=" -DOPENSSL_NO_CMS" if $no_cms;
295 $cflags.=" -DOPENSSL_NO_ERR"  if $no_err;
296 $cflags.=" -DOPENSSL_NO_EC"   if $no_ec;
297 $cflags.=" -DOPENSSL_NO_GOST" if $no_gost;
298 $cflags.=" -DOPENSSL_NO_ENGINE"   if $no_engine;
299 $cflags.=" -DOPENSSL_NO_HW"   if $no_hw;
300 $cflags.=" -DOPENSSL_FIPS"    if $fips;
301 $cflags.=" -DOPENSSL_NO_JPAKE"    if $no_jpake;
302 $cflags.=" -DOPENSSL_NO_EC2M"    if $no_ec2m;
303 $cflags.= " -DZLIB" if $zlib_opt;
304 $cflags.= " -DZLIB_SHARED" if $zlib_opt == 2;
305
306 if ($no_static_engine)
307         {
308         $cflags .= " -DOPENSSL_NO_STATIC_ENGINE";
309         }
310 else
311         {
312         $cflags .= " -DOPENSSL_NO_DYNAMIC_ENGINE";
313         }
314
315 #$cflags.=" -DRSAref"  if $rsaref ne "";
316
317 ## if ($unix)
318 ##      { $cflags="$c_flags" if ($c_flags ne ""); }
319 ##else
320         { $cflags="$c_flags$cflags" if ($c_flags ne ""); }
321
322 if ($orig_platform eq 'copy') {
323     $cflags = $mf_cflag;
324     $cc = $mf_cc;
325 }
326
327 $ex_libs="$l_flags$ex_libs" if ($l_flags ne "");
328
329
330 %shlib_ex_cflags=("SSL" => " -DOPENSSL_BUILD_SHLIBSSL",
331                   "CRYPTO" => " -DOPENSSL_BUILD_SHLIBCRYPTO");
332
333 if ($msdos)
334         {
335         $banner ="\t\@echo Make sure you have run 'perl Configure $platform' in the\n";
336         $banner.="\t\@echo top level directory, if you don't have perl, you will\n";
337         $banner.="\t\@echo need to probably edit crypto/bn/bn.h, check the\n";
338         $banner.="\t\@echo documentation for details.\n";
339         }
340
341 # have to do this to allow $(CC) under unix
342 $link="$bin_dir$link" if ($link !~ /^\$/);
343
344 $INSTALLTOP =~ s|/|$o|g;
345 $OPENSSLDIR =~ s|/|$o|g;
346
347 #############################################
348 # We parse in input file and 'store' info for later printing.
349 open(IN,"<$infile") || die "unable to open $infile:$!\n";
350 $_=<IN>;
351 for (;;)
352         {
353         s/\s*$//; # was chop, didn't work in mixture of perls for Windows...
354
355         ($key,$val)=/^([^=]+)=(.*)/;
356         if ($key eq "RELATIVE_DIRECTORY")
357                 {
358                 if ($lib ne "")
359                         {
360                         if ($fips && $dir =~ /^fips/)
361                                 {
362                                 $uc = "FIPS";
363                                 }
364                         else
365                                 {
366                                 $uc=$lib;
367                                 $uc =~ s/^lib(.*)\.a/$1/;
368                                 $uc =~ tr/a-z/A-Z/;
369                                 }
370                         if (($uc ne "FIPS") || $fipscanisterbuild)
371                                 {
372                                 $lib_nam{$uc}=$uc;
373                                 $lib_obj{$uc}.=$libobj." ";
374                                 }
375                         }
376                 last if ($val eq "FINISHED");
377                 $lib="";
378                 $libobj="";
379                 $dir=$val;
380                 }
381
382         if ($key eq "ZLIB_INCLUDE")
383                 { $cflags .= " $val" if $val ne "";}
384
385         if ($key eq "LIBZLIB")
386                 { $zlib_lib = "$val" if $val ne "";}
387
388         if ($key eq "EX_LIBS")
389                 { $ex_libs .= " $val" if $val ne "";}
390
391         # There was a condition here before:
392         #       !$fipscanisteronly || $dir =~ /^fips/
393         # It currently fills no function and needs to be rewritten anyway, so
394         # removed for now.
395         if ($dir eq "test" && $key eq "EXE")
396                 {
397                 foreach my $t (split /\s+/, $val) {
398                         $test.=&var_add($dir,$t, 0) if $t; }
399                 }
400
401         if ($key eq "EXE_OBJ")
402                 { $e_exe.=&var_add($dir,$val, 0); }
403
404         if ($key eq "LIB")
405                 {
406                 $lib=$val;
407                 $lib =~ s/^.*\/([^\/]+)$/$1/;
408                 }
409         if ($key eq "LIBNAME" && $no_static_engine)
410                 {
411                 $lib=$val;
412                 $lib =~ s/^.*\/([^\/]+)$/$1/;
413                 $otherlibs .= " $lib";
414                 }
415
416         if ($key eq "HEADER")
417                 { $header.=&var_add($dir,$val, 1); }
418
419         if ($key eq "LIBOBJ")
420             {
421             if ($dir ne "engines" || !$no_static_engine)
422                 { $libobj=&var_add($dir,$val, 0); }
423             else
424                 { push(@engines_obj,split(/\s+/,&var_add($dir,$val,0))); }
425             }
426         if ($key eq "LIBNAMES" && $dir eq "engines" && $no_static_engine)
427                 { $engines.=$val }
428
429         if ($key eq "FIPS_EX_OBJ")
430                 { 
431                 $fips_ex_obj=&var_add("crypto",$val,0);
432                 }
433
434         if ($key eq "FIPSLIBDIR")
435                 {
436                 $fipslibdir=$val;
437                 $fipslibdir =~ s/\/$//;
438                 $fipslibdir =~ s/\//$o/g;
439                 }
440
441         if ($key eq "BASEADDR")
442                 { $baseaddr=$val;}
443
444         if (!($_=<IN>))
445                 { $_="RELATIVE_DIRECTORY=FINISHED\n"; }
446         }
447 close(IN);
448
449 if ($orig_platform eq 'copy')
450         {
451         # Remove opensslconf.h so it doesn't get updated if we configure a
452         # different branch.
453         $header =~ s/[^ ]+\/opensslconf.h//;
454         }
455
456 if ($fips)
457         {
458
459         foreach (split " ", $fips_ex_obj)
460                 {
461                 $fips_exclude_obj{$1} = 1 if (/\/([^\/]*)$/);
462                 }
463         foreach (split " ",
464                 "$mf_cpuid_asm $mf_aes_asm $mf_sha_asm $mf_bn_asm " .
465                 "$mf_des_asm $mf_modes_asm")
466                 {
467                 s/\.o//;
468                 $fips_exclude_obj{$_} = 1;
469                 }
470         my @ltmp = split " ", $lib_obj{"CRYPTO"};
471
472
473         $lib_obj{"CRYPTO"} = "";
474
475         foreach(@ltmp)
476                 {
477                 if (/\/([^\/]*)$/ && exists $fips_exclude_obj{$1})
478                         {
479                         if ($fipscanisterbuild)
480                                 {
481                                 $lib_obj{"FIPS"} .= "$_ ";
482                                 }
483                         }
484                 elsif (!$fipscanisteronly)
485                         {
486                         $lib_obj{"CRYPTO"} .= "$_ ";
487                         }
488                 }
489
490         }
491
492 if ($fipscanisterbuild)
493         {
494         $fips_canister_path = "\$(LIB_D)${o}fipscanister.lib" if $fips_canister_path eq "";
495         $fips_premain_c_path = "\$(LIB_D)${o}fips_premain.c";
496         }
497 else
498         {
499         if ($fips_canister_path eq "")
500                 {
501                 $fips_canister_path = "\$(FIPSLIB_D)${o}fipscanister.lib";
502                 }
503
504         if ($fips_premain_c_path eq "")
505                 {
506                 $fips_premain_c_path = "\$(FIPSLIB_D)${o}fips_premain.c";
507                 }
508         }
509
510 if ($fips)
511         {
512         if ($fips_sha1_exe_path eq "")
513                 {
514                 $fips_sha1_exe_path =
515                         "\$(BIN_D)${o}fips_standalone_sha1$exep";
516                 }
517         }
518         else
519         {
520         $fips_sha1_exe_path = "";
521         }
522
523 if ($fips_premain_dso_exe_path eq "")
524         {
525         $fips_premain_dso_exe_path = "\$(BIN_D)${o}fips_premain_dso$exep";
526         }
527
528 #       $ex_build_targets .= "\$(BIN_D)${o}\$(E_PREMAIN_DSO)$exep" if ($fips);
529
530 if ($fips)
531         {
532         if (!$shlib)
533                 {
534                 $build_targets .= " \$(LIB_D)$o$crypto_compat \$(PREMAIN_DSO_EXE)";
535                 $ex_l_libs .= " \$(O_FIPSCANISTER)";
536                 $ex_libs_dep .= " \$(O_FIPSCANISTER)" if $fipscanisterbuild;
537                 }
538         if ($fipscanisterbuild)
539                 {
540                 $fipslibdir = "\$(LIB_D)";
541                 }
542         else
543                 {
544                 if ($fipslibdir eq "")
545                         {
546                         open (IN, "util/fipslib_path.txt") || fipslib_error();
547                         $fipslibdir = <IN>;
548                         chomp $fipslibdir;
549                         close IN;
550                         }
551                 fips_check_files($fipslibdir,
552                                 "fipscanister.lib", "fipscanister.lib.sha1",
553                                 "fips_premain.c", "fips_premain.c.sha1");
554                 }
555         }
556
557 if ($fipscanisteronly)
558         {
559         $build_targets = "\$(O_FIPSCANISTER) \$(T_EXE)";
560         $libs_dep = "";
561         }
562
563 $cp2 = $cp unless defined $cp2;
564
565 $extra_install= <<"EOF";
566         \$(CP) \"include${o}openssl${o}*.\[ch\]\" \"\$(INSTALLTOP)${o}include${o}openssl\"
567         \$(CP) \"\$(BIN_D)$o\$(E_EXE)$exep \$(INSTALLTOP)${o}bin\"
568         \$(MKDIR) \"\$(OPENSSLDIR)\"
569         \$(CP) apps${o}openssl.cnf \"\$(OPENSSLDIR)\"
570 EOF
571
572 if ($fipscanisteronly)
573         {
574         $extra_install = <<"EOF";
575         \$(CP) \"\$(O_FIPSCANISTER)\" \"\$(INSTALLTOP)${o}lib\"
576         \$(CP) \"\$(O_FIPSCANISTER).sha1\" \"\$(INSTALLTOP)${o}lib\"
577         \$(CP2) \"fips${o}fips_premain.c\" \"\$(INSTALLTOP)${o}lib\"
578         \$(CP) \"fips${o}fips_premain.c.sha1\" \"\$(INSTALLTOP)${o}lib\"
579         \$(CP) \"include${o}openssl${o}fips.h\" \"\$(INSTALLTOP)${o}include${o}openssl\"
580         \$(CP) \"include${o}openssl${o}fips_rand.h\" \"\$(INSTALLTOP)${o}include${o}openssl\"
581         \$(CP) "\$(BIN_D)${o}fips_standalone_sha1$exep" \"\$(INSTALLTOP)${o}bin\"
582         \$(CP) \"util${o}fipslink.pl\" \"\$(INSTALLTOP)${o}bin\"
583 EOF
584         }
585 elsif ($shlib)
586         {
587         $extra_install .= <<"EOF";
588         \$(CP) \"\$(O_SSL)\" \"\$(INSTALLTOP)${o}bin\"
589         \$(CP) \"\$(O_CRYPTO)\" \"\$(INSTALLTOP)${o}bin\"
590         \$(CP) \"\$(L_SSL)\" \"\$(INSTALLTOP)${o}lib\"
591         \$(CP) \"\$(L_CRYPTO)\" \"\$(INSTALLTOP)${o}lib\"
592 EOF
593         if ($no_static_engine)
594                 {
595                 $extra_install .= <<"EOF"
596         \$(MKDIR) \"\$(INSTALLTOP)${o}lib${o}engines\"
597         \$(CP) \"\$(E_SHLIB)\" \"\$(INSTALLTOP)${o}lib${o}engines\"
598 EOF
599                 }
600         }
601 else
602         {
603         $extra_install .= <<"EOF";
604         \$(CP) \"\$(O_SSL)\" \"\$(INSTALLTOP)${o}lib\"
605         \$(CP) \"\$(O_CRYPTO)\" \"\$(INSTALLTOP)${o}lib\"
606 EOF
607         $ex_libs .= " $zlib_lib" if $zlib_opt == 1;
608         }
609
610 my $asm_def = $orig_platform eq 'copy' ? "" : "ASM=$bin_dir$asm";
611
612 $defs= <<"EOF";
613 # N.B. You MUST use -j on FreeBSD.
614 # This makefile has been automatically generated from the OpenSSL distribution.
615 # This single makefile will build the complete OpenSSL distribution and
616 # by default leave the 'intertesting' output files in .${o}out and the stuff
617 # that needs deleting in .${o}tmp.
618 # The file was generated by running 'make makefile.one', which
619 # does a 'make files', which writes all the environment variables from all
620 # the makefiles to the file call MINFO.  This file is used by
621 # util${o}mk1mf.pl to generate makefile.one.
622 # The 'makefile per directory' system suites me when developing this
623 # library and also so I can 'distribute' indervidual library sections.
624 # The one monster makefile better suits building in non-unix
625 # environments.
626
627 EOF
628
629 $defs .= $preamble if defined $preamble;
630
631 $defs.= <<"EOF";
632 INSTALLTOP=$INSTALLTOP
633 OPENSSLDIR=$OPENSSLDIR
634
635 # Set your compiler options
636 PLATFORM=$platform
637 CC=$bin_dir${cc}
638 CFLAG=$cflags
639 APP_CFLAG=$app_cflag
640 LIB_CFLAG=$lib_cflag
641 SHLIB_CFLAG=$shl_cflag
642 APP_EX_OBJ=$app_ex_obj
643 SHLIB_EX_OBJ=$shlib_ex_obj
644 # add extra libraries to this define, for solaris -lsocket -lnsl would
645 # be added
646 EX_LIBS=$ex_libs
647
648 # The OpenSSL directory
649 SRC_D=$src_dir
650
651 LINK=$link
652 LFLAGS=$lflags
653 RSC=$rsc
654 FIPSLINK=\$(PERL) util${o}fipslink.pl
655
656 # The output directory for everything interesting
657 OUT_D=$out_dir
658 # The output directory for all the temporary muck
659 TMP_D=$tmp_dir
660
661 PERL=$perl
662 PERLASM_SCHEME=$mf_perlasm_scheme
663 CP=$cp
664 CP2=$cp2
665 RM=$rm
666 RANLIB=$ranlib
667 MKDIR=$mkdir
668 MKLIB=$bin_dir$mklib
669 MLFLAGS=$mlflags
670 $asm_def
671
672 # FIPS validated module and support file locations
673
674 E_PREMAIN_DSO=fips_premain_dso
675
676 FIPSLIB_D=$fipslibdir
677 BASEADDR=$baseaddr
678 FIPS_PREMAIN_SRC=$fips_premain_c_path
679 O_FIPSCANISTER=$fips_canister_path
680 FIPS_SHA1_EXE=$fips_sha1_exe_path
681 PREMAIN_DSO_EXE=$fips_premain_dso_exe_path
682
683 ######################################################
684 # You should not need to touch anything below this point
685 ######################################################
686
687 E_EXE=openssl
688 SSL=$ssl
689 CRYPTO=$crypto
690
691 # BIN_D  - Binary output directory
692 # TEST_D - Binary test file output directory
693 # LIB_D  - library output directory
694 # ENG_D  - dynamic engine output directory
695 # Note: if you change these point to different directories then uncomment out
696 # the lines around the 'NB' comment below.
697
698 BIN_D=\$(OUT_D)
699 TEST_D=\$(OUT_D)
700 LIB_D=\$(OUT_D)
701 ENG_D=\$(OUT_D)
702
703 # INCL_D - local library directory
704 # OBJ_D  - temp object file directory
705 OBJ_D=\$(TMP_D)
706 INCL_D=\$(TMP_D)
707
708 O_SSL=     \$(LIB_D)$o$plib\$(SSL)$shlibp
709 O_CRYPTO=  \$(LIB_D)$o$plib\$(CRYPTO)$shlibp
710 SO_SSL=    $plib\$(SSL)$so_shlibp
711 SO_CRYPTO= $plib\$(CRYPTO)$so_shlibp
712 L_SSL=     \$(LIB_D)$o$plib\$(SSL)$libp
713 L_CRYPTO=  \$(LIB_D)$o$plib\$(CRYPTO)$libp
714
715 L_LIBS= \$(L_SSL) \$(L_CRYPTO) $ex_l_libs
716
717 ######################################################
718 # Don't touch anything below this point
719 ######################################################
720
721 INC=-I\$(SRC_D)${o}include -I\$(INCL_D) -I\$(SRC_D)${o}crypto${o}include
722 APP_CFLAGS=\$(INC) \$(CFLAG) \$(APP_CFLAG)
723 LIB_CFLAGS=\$(INC) \$(CFLAG) \$(LIB_CFLAG)
724 SHLIB_CFLAGS=\$(INC) \$(CFLAG) \$(LIB_CFLAG) \$(SHLIB_CFLAG)
725 LIBS_DEP=$libs_dep
726
727 #############################################
728 EOF
729
730 $rules=<<"EOF";
731 all: banner \$(TMP_D) \$(BIN_D) \$(TEST_D) \$(LIB_D) headers \$(FIPS_SHA1_EXE) $build_targets
732
733 banner:
734 $banner
735
736 \$(TMP_D):
737         \$(MKDIR) \"\$(TMP_D)\"
738 # NB: uncomment out these lines if BIN_D, TEST_D and LIB_D are different
739 #\$(BIN_D):
740 #       \$(MKDIR) \$(BIN_D)
741 #
742 #\$(TEST_D):
743 #       \$(MKDIR) \$(TEST_D)
744
745 \$(LIB_D):
746         \$(MKDIR) \"\$(LIB_D)\"
747
748 # This needs to be invoked once, when the makefile is first constructed, or
749 # after cleaning.
750 init: \$(TMP_D) \$(LIB_D) \$(BIN_D) \$(TEST_D) headers
751
752 headers: \$(HEADER)
753
754 lib: \$(LIBS_DEP) \$(E_SHLIB)
755
756 exe: \$(T_EXE) \$(BIN_D)$o\$(E_EXE)$exep
757
758 install: all
759         \$(MKDIR) \"\$(INSTALLTOP)\"
760         \$(MKDIR) \"\$(INSTALLTOP)${o}bin\"
761         \$(MKDIR) \"\$(INSTALLTOP)${o}include\"
762         \$(MKDIR) \"\$(INSTALLTOP)${o}include${o}openssl\"
763         \$(MKDIR) \"\$(INSTALLTOP)${o}lib\"
764 $extra_install
765
766 clean:
767         \$(RM) \$(TMP_D)$o*.*
768
769 vclean:
770         \$(RM) \$(TMP_D)$o*.*
771         \$(RM) \$(OUT_D)$o*.*
772
773 reallyclean:
774         \$(RM) -rf \$(TMP_D)
775         \$(RM) -rf \$(BIN_D)
776         \$(RM) -rf \$(TEST_D)
777         \$(RM) -rf \$(LIB_D)
778
779 EOF
780
781 if ($orig_platform ne 'copy')
782         {
783         $rules .= <<"EOF";
784 test: \$(T_EXE)
785         cd \$(BIN_D)
786         ..${o}ms${o}test
787
788 EOF
789         }
790
791 my $platform_cpp_symbol = "MK1MF_PLATFORM_$platform";
792 $platform_cpp_symbol =~ s/-/_/g;
793 if (open(IN,"crypto/buildinf.h"))
794         {
795         # Remove entry for this platform in existing file buildinf.h.
796
797         my $old_buildinf_h = "";
798         while (<IN>)
799                 {
800                 if (/^\#ifdef $platform_cpp_symbol$/)
801                         {
802                         while (<IN>) { last if (/^\#endif/); }
803                         }
804                 else
805                         {
806                         $old_buildinf_h .= $_;
807                         }
808                 }
809         close(IN);
810
811         open(OUT,">crypto/buildinf.h") || die "Can't open buildinf.h";
812         print OUT $old_buildinf_h;
813         close(OUT);
814         }
815
816 open (OUT,">>crypto/buildinf.h") || die "Can't open buildinf.h";
817 printf OUT <<EOF;
818 #ifdef $platform_cpp_symbol
819   /* auto-generated/updated by util/mk1mf.pl for crypto/cversion.c */
820   #define CFLAGS "compiler: $cc $cflags"
821   #define PLATFORM "$platform"
822 EOF
823 printf OUT "  #define DATE \"%s\"\n", scalar gmtime();
824 printf OUT "#endif\n";
825 close(OUT);
826
827 # Strip off trailing ' '
828 foreach (keys %lib_obj) { $lib_obj{$_}=&clean_up_ws($lib_obj{$_}); }
829 $test=&clean_up_ws($test);
830 $e_exe=&clean_up_ws($e_exe);
831 $header=&clean_up_ws($header);
832
833 $defs.=&do_defs("HEADER",$header,"\$(INCL_D)","");
834 $rules.=&do_copy_rule("\$(INCL_D)",$header,"");
835
836 $defs.=&do_defs("T_OBJ",$test,"\$(OBJ_D)",$obj);
837 $rules.=&do_compile_rule("\$(OBJ_D)",$test,"\$(APP_CFLAGS)");
838
839 $defs.=&do_defs("E_OBJ",$e_exe,"\$(OBJ_D)",$obj);
840 $rules.=&do_compile_rule("\$(OBJ_D)",$e_exe,'-DMONOLITH $(APP_CFLAGS)');
841
842 # Special case rules for fips_start and fips_end fips_premain_dso
843
844 if ($fips)
845         {
846         if ($fipscanisterbuild)
847                 {
848                 $rules.=&cc_compile_target("\$(OBJ_D)${o}fips_start$obj",
849                         "fips${o}fips_canister.c",
850                         "-DFIPS_START \$(SHLIB_CFLAGS)");
851                 $rules.=&cc_compile_target("\$(OBJ_D)${o}fips_end$obj",
852                         "fips${o}fips_canister.c", "\$(SHLIB_CFLAGS)");
853                 }
854         $rules.=&cc_compile_target("\$(OBJ_D)${o}fips_standalone_sha1$obj",
855                 "fips${o}sha${o}fips_standalone_sha1.c",
856                 "\$(APP_CFLAGS)");
857         $rules.=&cc_compile_target("\$(OBJ_D)${o}\$(E_PREMAIN_DSO)$obj",
858                 "fips${o}fips_premain.c",
859                 "-DFINGERPRINT_PREMAIN_DSO_LOAD \$(APP_CFLAGS)");
860         }
861
862 sub fix_asm
863         {
864         my($asm, $dir) = @_;
865
866         $asm = " $asm";
867         $asm =~ s/\s+/ $dir\//g;
868         $asm =~ s/\.o//g;
869         $asm =~ s/^ //;
870
871         return $asm . ' ';
872         }
873
874 if ($orig_platform eq 'copy') {
875         $lib_obj{CRYPTO} .= fix_asm($mf_md5_asm, 'crypto/md5');
876         $lib_obj{CRYPTO} .= fix_asm($mf_bn_asm, 'crypto/bn');
877         # cpuid is included by the crypto dir
878         #$lib_obj{CRYPTO} .= fix_asm($mf_cpuid_asm, 'crypto');
879         # AES asm files end up included by the aes dir itself
880         #$lib_obj{CRYPTO} .= fix_asm($mf_aes_asm, 'crypto/aes');
881         $lib_obj{CRYPTO} .= fix_asm($mf_sha_asm, 'crypto/sha');
882         $lib_obj{CRYPTO} .= fix_asm($mf_engines_asm, 'engines');
883         $lib_obj{CRYPTO} .= fix_asm($mf_rc4_asm, 'crypto/rc4');
884         $lib_obj{CRYPTO} .= fix_asm($mf_modes_asm, 'crypto/modes');
885         $lib_obj{CRYPTO} .= fix_asm($mf_ec_asm, 'crypto/ec');
886 }
887
888 foreach (values %lib_nam)
889         {
890         $lib_obj=$lib_obj{$_};
891         local($slib)=$shlib;
892
893         $defs.=&do_defs(${_}."OBJ",$lib_obj,"\$(OBJ_D)",$obj);
894         $lib=($slib)?" \$(SHLIB_CFLAGS)".$shlib_ex_cflags{$_}:" \$(LIB_CFLAGS)";
895         $rules.=&do_compile_rule("\$(OBJ_D)",$lib_obj{$_},$lib);
896         }
897
898 # hack to add version info on MSVC
899 if (($platform eq "VC-WIN32") || ($platform eq "VC-WIN64A")
900         || ($platform eq "VC-WIN64I") || ($platform eq "VC-NT")) {
901     $rules.= <<"EOF";
902 \$(OBJ_D)\\\$(CRYPTO).res: ms\\version32.rc
903         \$(RSC) /fo"\$(OBJ_D)\\\$(CRYPTO).res" /d CRYPTO ms\\version32.rc
904
905 \$(OBJ_D)\\\$(SSL).res: ms\\version32.rc
906         \$(RSC) /fo"\$(OBJ_D)\\\$(SSL).res" /d SSL ms\\version32.rc
907
908 EOF
909 }
910
911 $defs.=&do_defs("T_EXE",$test,"\$(TEST_D)",$exep);
912 foreach (split(/\s+/,$test))
913         {
914         my $t_libs;
915         $t=&bname($_);
916         my $ltype;
917         # Check to see if test program is FIPS
918         if ($fips && /fips/)
919                 {
920                 # If fips perform static link to 
921                 # $(O_FIPSCANISTER)
922                 $t_libs = "\$(O_FIPSCANISTER)";
923                 $ltype = 2;
924                 }
925         else
926                 {
927                 $t_libs = "\$(L_LIBS)";
928                 $ltype = 0;
929                 }
930
931         $tt="\$(OBJ_D)${o}$t${obj}";
932         $rules.=&do_link_rule("\$(TEST_D)$o$t$exep",$tt,"\$(LIBS_DEP)","$t_libs \$(EX_LIBS)", $ltype);
933         }
934
935 $defs.=&do_defs("E_SHLIB",$engines . $otherlibs,"\$(ENG_D)",$shlibp);
936
937 foreach (split(/\s+/,$engines))
938         {
939         my $engine = $_;
940         my @objs   = grep {/e_$engine/} @engines_obj;
941         $rules.=&do_compile_rule("\$(OBJ_D)",join(" ",@objs),$lib);
942         map {$_=~s/.*\/([^\/]+)$/\$(OBJ_D)${o}$1$obj/} @objs;
943         $rules.= &do_lib_rule(join(" ",@objs),"\$(ENG_D)$o$engine$shlibp","",$shlib,"");
944         }
945
946
947
948 $rules.= &do_lib_rule("\$(SSLOBJ)","\$(O_SSL)",$ssl,$shlib,"\$(SO_SSL)");
949 #$rules.= &do_lib_rule("\$(CRYPTOOBJ)","\$(O_CRYPTO)",$crypto,$shlib,"\$(SO_CRYPTO)");
950
951 foreach (split(" ",$otherlibs))
952         {
953         my $uc = $_;
954         $uc =~ tr /a-z/A-Z/;    
955         $rules.= &do_lib_rule("\$(${uc}OBJ)","\$(ENG_D)$o$_$shlibp", "", $shlib, "");
956
957         }
958
959 if ($fips)
960         {
961         if ($shlib)
962                 {
963                 $rules.= &do_lib_rule("\$(CRYPTOOBJ) \$(O_FIPSCANISTER)",
964                                 "\$(O_CRYPTO)", "$crypto",
965                                 $shlib, "\$(SO_CRYPTO)", "\$(BASEADDR)");
966                 }
967         else
968                 {
969                 $rules.= &do_lib_rule("\$(CRYPTOOBJ)",
970                         "\$(O_CRYPTO)",$crypto,$shlib,"\$(SO_CRYPTO)", "");
971                 $rules.= &do_lib_rule("\$(CRYPTOOBJ) \$(O_FIPSCANISTER)",
972                         "\$(LIB_D)$o$crypto_compat",$crypto,$shlib,"\$(SO_CRYPTO)", "");
973                 }
974         }
975         else
976         {
977         $rules.= &do_lib_rule("\$(CRYPTOOBJ)","\$(O_CRYPTO)",$crypto,$shlib,
978                                                         "\$(SO_CRYPTO)");
979         }
980
981 if ($fips)
982         {
983         if ($fipscanisterbuild)
984                 {
985                 $rules.= &do_rlink_rule("\$(O_FIPSCANISTER)",
986                                         "\$(OBJ_D)${o}fips_start$obj",
987                                         "\$(FIPSOBJ)",
988                                         "\$(OBJ_D)${o}fips_end$obj",
989                                         "\$(FIPS_SHA1_EXE)", "");
990                 # FIXME
991                 $rules.=&do_link_rule("\$(FIPS_SHA1_EXE)",
992                                         "\$(OBJ_D)${o}fips_standalone_sha1$obj \$(OBJ_D)${o}sha1dgst$obj $sha1_asm_obj",
993                                         "","\$(EX_LIBS)", 1);
994                 }
995         else
996                 {
997                 $rules.=&do_link_rule("\$(FIPS_SHA1_EXE)",
998                                         "\$(OBJ_D)${o}fips_standalone_sha1$obj \$(O_FIPSCANISTER)",
999                                         "","", 1);
1000
1001                 }
1002         $rules.=&do_link_rule("\$(PREMAIN_DSO_EXE)","\$(OBJ_D)${o}\$(E_PREMAIN_DSO)$obj \$(CRYPTOOBJ) \$(O_FIPSCANISTER)","","\$(EX_LIBS)", 1);
1003         
1004         }
1005
1006 $rules.=&do_link_rule("\$(BIN_D)$o\$(E_EXE)$exep","\$(E_OBJ)","\$(LIBS_DEP)","\$(L_LIBS) \$(EX_LIBS)", ($fips && !$shlib) ? 2 : 0);
1007
1008 $rules .= get_tests('test/Makefile') if $orig_platform eq 'copy';
1009
1010 print $defs;
1011
1012 if ($platform eq "linux-elf") {
1013     print <<"EOF";
1014 # Generate perlasm output files
1015 %.cpp:
1016         (cd \$(\@D)/..; PERL=perl make -f Makefile asm/\$(\@F))
1017 EOF
1018 }
1019 print "###################################################################\n";
1020 print $rules;
1021
1022 ###############################################
1023 # strip off any trailing .[och] and append the relative directory
1024 # also remembering to do nothing if we are in one of the dropped
1025 # directories
1026 sub var_add
1027         {
1028         local($dir,$val,$keepext)=@_;
1029         local(@a,$_,$ret);
1030
1031         return("") if $no_engine && $dir =~ /\/engine/;
1032         return("") if $no_hw   && $dir =~ /\/hw/;
1033         return("") if $no_idea && $dir =~ /\/idea/;
1034         return("") if $no_aes  && $dir =~ /\/aes/;
1035         return("") if $no_camellia  && $dir =~ /\/camellia/;
1036         return("") if $no_seed && $dir =~ /\/seed/;
1037         return("") if $no_rc2  && $dir =~ /\/rc2/;
1038         return("") if $no_rc4  && $dir =~ /\/rc4/;
1039         return("") if $no_rc5  && $dir =~ /\/rc5/;
1040         return("") if $no_rsa  && $dir =~ /\/rsa/;
1041         return("") if $no_rsa  && $dir =~ /^rsaref/;
1042         return("") if $no_dsa  && $dir =~ /\/dsa/;
1043         return("") if $no_dh   && $dir =~ /\/dh/;
1044         return("") if $no_ec   && $dir =~ /\/ec/;
1045         return("") if $no_gost   && $dir =~ /\/ccgost/;
1046         return("") if $no_cms  && $dir =~ /\/cms/;
1047         return("") if $no_jpake  && $dir =~ /\/jpake/;
1048         return("") if !$fips   && $dir =~ /^fips/;
1049         if ($no_des && $dir =~ /\/des/)
1050                 {
1051                 if ($val =~ /read_pwd/)
1052                         { return("$dir/read_pwd "); }
1053                 else
1054                         { return(""); }
1055                 }
1056         return("") if $no_mdc2 && $dir =~ /\/mdc2/;
1057         return("") if $no_sock && $dir =~ /\/proxy/;
1058         return("") if $no_bf   && $dir =~ /\/bf/;
1059         return("") if $no_cast && $dir =~ /\/cast/;
1060         return("") if $no_whirlpool && $dir =~ /\/whrlpool/;
1061
1062         $val =~ s/^\s*(.*)\s*$/$1/;
1063         @a=split(/\s+/,$val);
1064         grep(s/\.[och]$//,@a) unless $keepext;
1065
1066         @a=grep(!/^e_.*_3d$/,@a) if $no_des;
1067         @a=grep(!/^e_.*_d$/,@a) if $no_des;
1068         @a=grep(!/^e_.*_ae$/,@a) if $no_idea;
1069         @a=grep(!/^e_.*_i$/,@a) if $no_aes;
1070         @a=grep(!/^e_.*_r2$/,@a) if $no_rc2;
1071         @a=grep(!/^e_.*_r5$/,@a) if $no_rc5;
1072         @a=grep(!/^e_.*_bf$/,@a) if $no_bf;
1073         @a=grep(!/^e_.*_c$/,@a) if $no_cast;
1074         @a=grep(!/^e_rc4$/,@a) if $no_rc4;
1075         @a=grep(!/^e_camellia$/,@a) if $no_camellia;
1076         @a=grep(!/^e_seed$/,@a) if $no_seed;
1077
1078         #@a=grep(!/(^s3_)|(^s23_)/,@a) if $no_ssl3;
1079
1080         @a=grep(!/(_sock$)|(_acpt$)|(_conn$)|(^pxy_)/,@a) if $no_sock;
1081
1082         @a=grep(!/(^md2)|(_md2$)/,@a) if $no_md2;
1083         @a=grep(!/(^md4)|(_md4$)/,@a) if $no_md4;
1084         @a=grep(!/(^md5)|(_md5$)/,@a) if $no_md5;
1085         @a=grep(!/(rmd)|(ripemd)/,@a) if $no_ripemd;
1086
1087         @a=grep(!/(^d2i_r_)|(^i2d_r_)/,@a) if $no_rsa;
1088         @a=grep(!/(^p_open$)|(^p_seal$)/,@a) if $no_rsa;
1089         @a=grep(!/(^pem_seal$)/,@a) if $no_rsa;
1090
1091         @a=grep(!/(m_dss$)|(m_dss1$)/,@a) if $no_dsa;
1092         @a=grep(!/(^d2i_s_)|(^i2d_s_)|(_dsap$)/,@a) if $no_dsa;
1093
1094         @a=grep(!/^n_pkey$/,@a) if $no_rsa || $no_rc4;
1095
1096         @a=grep(!/_dhp$/,@a) if $no_dh;
1097
1098         @a=grep(!/_mdc2$/,@a) if $no_mdc2;
1099
1100         @a=grep(!/(srp)/,@a) if $no_srp;
1101
1102         @a=grep(!/^engine$/,@a) if $no_engine;
1103         @a=grep(!/^hw$/,@a) if $no_hw;
1104         @a=grep(!/(^rsa$)|(^genrsa$)/,@a) if $no_rsa;
1105         @a=grep(!/(^dsa$)|(^gendsa$)|(^dsaparam$)/,@a) if $no_dsa;
1106         @a=grep(!/(^dh$)|(^gendh$)/,@a) if $no_dh;
1107
1108         grep($_="$dir/$_",@a);
1109         @a=grep(!/(^|\/)s_/,@a) if $no_sock;
1110         @a=grep(!/(^|\/)bio_sock/,@a) if $no_sock;
1111         $ret=join(' ',@a)." ";
1112         return($ret);
1113         }
1114
1115 # change things so that each 'token' is only separated by one space
1116 sub clean_up_ws
1117         {
1118         local($w)=@_;
1119
1120         $w =~ s/^\s*(.*)\s*$/$1/;
1121         $w =~ s/\s+/ /g;
1122         return($w);
1123         }
1124
1125 sub do_defs
1126         {
1127         local($var,$files,$location,$postfix)=@_;
1128         local($_,$ret,$pf);
1129         local(*OUT,$tmp,$t);
1130
1131         $files =~ s/\//$o/g if $o ne '/';
1132         $ret="$var="; 
1133         $n=1;
1134         $Vars{$var}.="";
1135         foreach (split(/ /,$files))
1136                 {
1137                 $orig=$_;
1138                 $_=&bname($_) unless /^\$/;
1139                 if ($n++ == 2)
1140                         {
1141                         $n=0;
1142                         $ret.="\\\n\t";
1143                         }
1144                 if (($_ =~ /bss_file/) && ($postfix eq ".h"))
1145                         { $pf=".c"; }
1146                 else    { $pf=$postfix; }
1147                 if ($_ =~ /BN_ASM/)     { $t="$_ "; }
1148                 elsif ($_ =~ /BNCO_ASM/){ $t="$_ "; }
1149                 elsif ($_ =~ /AES_ASM/){ $t="$_ "; }
1150                 elsif ($_ =~ /DES_ENC/) { $t="$_ "; }
1151                 elsif ($_ =~ /BF_ENC/)  { $t="$_ "; }
1152                 elsif ($_ =~ /CAST_ENC/){ $t="$_ "; }
1153                 elsif ($_ =~ /RC4_ENC/) { $t="$_ "; }
1154                 elsif ($_ =~ /RC5_ENC/) { $t="$_ "; }
1155                 elsif ($_ =~ /MD5_ASM/) { $t="$_ "; }
1156                 elsif ($_ =~ /SHA1_ASM/){ $t="$_ "; }
1157                 elsif ($_ =~ /RMD160_ASM/){ $t="$_ "; }
1158                 elsif ($_ =~ /WHIRLPOOL_ASM/){ $t="$_ "; }
1159                 elsif ($_ =~ /CPUID_ASM/){ $t="$_ "; }
1160                 else    { $t="$location${o}$_$pf "; }
1161
1162                 $Vars{$var}.="$t ";
1163                 $ret.=$t;
1164                 }
1165         # hack to add version info on MSVC
1166         if ($shlib && (($platform eq "VC-WIN32") || ($platfrom eq "VC-WIN64I") || ($platform eq "VC-WIN64A") || ($platform eq "VC-NT")))
1167                 {
1168                 if ($var eq "CRYPTOOBJ")
1169                         { $ret.="\$(OBJ_D)\\\$(CRYPTO).res "; }
1170                 elsif ($var eq "SSLOBJ")
1171                         { $ret.="\$(OBJ_D)\\\$(SSL).res "; }
1172                 }
1173         chomp($ret);
1174         $ret.="\n\n";
1175         return($ret);
1176         }
1177
1178 # return the name with the leading path removed
1179 sub bname
1180         {
1181         local($ret)=@_;
1182         $ret =~ s/^.*[\\\/]([^\\\/]+)$/$1/;
1183         return($ret);
1184         }
1185
1186 # return the leading path
1187 sub dname
1188         {
1189         my $ret=shift;
1190         $ret =~ s/(^.*)[\\\/][^\\\/]+$/$1/;
1191         return($ret);
1192         }
1193
1194 ##############################################################
1195 # do a rule for each file that says 'compile' to new direcory
1196 # compile the files in '$files' into $to
1197 sub do_compile_rule
1198         {
1199         local($to,$files,$ex)=@_;
1200         local($ret,$_,$n,$d,$s);
1201
1202         $files =~ s/\//$o/g if $o ne '/';
1203         foreach (split(/\s+/,$files))
1204                 {
1205                 $n=&bname($_);
1206                 $d=&dname($_);
1207                 if (-f "${_}.c")
1208                         {
1209                         $ret.=&cc_compile_target("$to${o}$n$obj","${_}.c",$ex)
1210                         }
1211                 elsif (-f ($s="${d}${o}asm${o}${n}.pl") or
1212                        ($s=~s/sha256/sha512/ and -f $s) or
1213                        -f ($s="${d}${o}${n}.pl"))
1214                         {
1215                         $ret.=&perlasm_compile_target("$to${o}$n$obj",$s,$n);
1216                         }
1217                 elsif (-f ($s="${d}${o}asm${o}${n}.S") or
1218                        -f ($s="${d}${o}${n}.S"))
1219                         {
1220                         $ret.=&Sasm_compile_target("$to${o}$n$obj",$s,$n);
1221                         }
1222                 elsif (defined &special_compile_target and
1223                        ($s=special_compile_target($_)))
1224                         {
1225                         $ret.=$s;
1226                         }
1227                 else    { die "no rule for $_"; }
1228                 }
1229         return($ret);
1230         }
1231
1232 ##############################################################
1233 # do a rule for each file that says 'compile' to new direcory
1234 sub perlasm_compile_target
1235         {
1236         my($target,$source,$bname)=@_;
1237
1238         return platform_perlasm_compile_target($target, $source, $bname)
1239             if defined &platform_perlasm_compile_target;
1240
1241         my($ret);
1242         $bname =~ s/(.*)\.[^\.]$/$1/;
1243         $ret ="\$(TMP_D)$o$bname$asm_suffix: $source\n";
1244         $ret.="\t\$(PERL) $source $asmtype \$(CFLAG) >\$\@\n";
1245         if ($fipscanisteronly)
1246                 {
1247                 $ret .= "\t\$(PERL) util$o.pl . \$@ norunasm \$(CFLAG)\n";
1248                 }
1249         $ret .= "\n";
1250         $ret.="$target: \$(TMP_D)$o$bname$asm_suffix\n";
1251         $ret.="\t\$(ASM) $afile\$\@ \$(TMP_D)$o$bname$asm_suffix\n\n";
1252         return($ret);
1253         }
1254
1255 sub Sasm_compile_target
1256         {
1257         my($target,$source,$bname)=@_;
1258         my($ret);
1259
1260         $bname =~ s/(.*)\.[^\.]$/$1/;
1261         $ret ="\$(TMP_D)$o$bname.asm: $source\n";
1262         $ret.="\t\$(CC) -E \$(CFLAG) $source >\$\@\n";
1263         $ret.="\t\$(PERL) util\\fipsas.pl . \$@ norunasm \$(CFLAG)\n" if $fipscanisteronly;
1264         $ret.="\n";
1265         $ret.="$target: \$(TMP_D)$o$bname.asm\n";
1266         $ret.="\t\$(ASM) $afile\$\@ \$(TMP_D)$o$bname.asm\n\n";
1267         return($ret);
1268         }
1269
1270 sub cc_compile_target
1271         {
1272         local($target,$source,$ex_flags)=@_;
1273         local($ret);
1274         
1275         $ex_flags.=" -DMK1MF_BUILD -D$platform_cpp_symbol" if ($source =~ /cversion/);
1276         $target =~ s/\//$o/g if $o ne "/";
1277         $source =~ s/\//$o/g if $o ne "/";
1278         $ret ="$target: \$(SRC_D)$o$source\n\t";
1279         $ret.="\$(CC)";
1280         $ret.= " -MMD" if $orig_platform eq "copy";
1281         $ret.= " ${ofile}$target $ex_flags -c \$(SRC_D)$o$source\n\n";
1282         $target =~ s/\.o$/.d/;
1283         $ret.=".sinclude \"$target\"\n\n" if $orig_platform eq "copy";
1284         return($ret);
1285         }
1286
1287 ##############################################################
1288 sub do_asm_rule
1289         {
1290         local($target,$src)=@_;
1291         local($ret,@s,@t,$i);
1292
1293         $target =~ s/\//$o/g if $o ne "/";
1294         $src =~ s/\//$o/g if $o ne "/";
1295
1296         @t=split(/\s+/,$target);
1297         @s=split(/\s+/,$src);
1298
1299
1300         for ($i=0; $i<=$#s; $i++)
1301                 {
1302                 my $objfile = $t[$i];
1303                 my $srcfile = $s[$i];
1304
1305                 if ($perl_asm == 1)
1306                         {
1307                         my $plasm = $objfile;
1308                         $plasm =~ s/${obj}/.pl/;
1309                         $ret.="$srcfile: $plasm\n";
1310                         $ret.="\t\$(PERL) $plasm $asmtype \$(CFLAG) >$srcfile\n\n";
1311                         }
1312
1313                 $ret.="$objfile: $srcfile\n";
1314                 $ret.="\t\$(ASM) $afile$objfile \$(SRC_D)$o$srcfile\n\n";
1315                 }
1316         return($ret);
1317         }
1318
1319 sub do_shlib_rule
1320         {
1321         local($n,$def)=@_;
1322         local($ret,$nn);
1323         local($t);
1324
1325         ($nn=$n) =~ tr/a-z/A-Z/;
1326         $ret.="$n.dll: \$(${nn}OBJ)\n";
1327         if ($vc && $w32)
1328                 {
1329                 $ret.="\t\$(MKSHLIB) $efile$n.dll $def @<<\n  \$(${nn}OBJ_F)\n<<\n";
1330                 }
1331         $ret.="\n";
1332         return($ret);
1333         }
1334
1335 # do a rule for each file that says 'copy' to new direcory on change
1336 sub do_copy_rule
1337         {
1338         local($to,$files,$p)=@_;
1339         local($ret,$_,$n,$pp);
1340         
1341         $files =~ s/\//$o/g if $o ne '/';
1342         foreach (split(/\s+/,$files))
1343                 {
1344                 $n=&bname($_);
1345                 if ($n =~ /bss_file/)
1346                         { $pp=".c"; }
1347                 else    { $pp=$p; }
1348                 $ret.="$to${o}$n$pp: \$(SRC_D)$o$_$pp\n\t\$(PERL) \$(SRC_D)${o}util${o}copy-if-different.pl \"\$(SRC_D)$o$_$pp\" \"$to${o}$n$pp\"\n\n";
1349                 }
1350         return($ret);
1351         }
1352
1353 # Options picked up from the OPTIONS line in the top level Makefile
1354 # generated by Configure.
1355
1356 sub read_options
1357         {
1358         # Many options are handled in a similar way. In particular
1359         # no-xxx sets zero or more scalars to 1.
1360         # Process these using the %valid_options hash containing the option
1361         # name and reference to the scalars to set. In some cases the option
1362         # needs no special handling and can be ignored: this is done by
1363         # setting the value to 0.
1364
1365         my %valid_options = (
1366                 "no-rc2" => \$no_rc2,
1367                 "no-rc4" => \$no_rc4,
1368                 "no-rc5" => \$no_rc5,
1369                 "no-idea" => \$no_idea,
1370                 "no-aes" => \$no_aes,
1371                 "no-camellia" => \$no_camellia,
1372                 "no-seed" => \$no_seed,
1373                 "no-des" => \$no_des,
1374                 "no-bf" => \$no_bf,
1375                 "no-cast" => \$no_cast,
1376                 "no-md2" => \$no_md2,
1377                 "no-md4" => \$no_md4,
1378                 "no-md5" => \$no_md5,
1379                 "no-ripemd" => \$no_ripemd,
1380                 "no-mdc2" => \$no_mdc2,
1381                 "no-whirlpool" => \$no_whirlpool,
1382                 "no-patents" => 
1383                         [\$no_rc2, \$no_rc4, \$no_rc5, \$no_idea, \$no_rsa],
1384                 "no-rsa" => \$no_rsa,
1385                 "no-dsa" => \$no_dsa,
1386                 "no-dh" => \$no_dh,
1387                 "no-asm" => \$no_asm,
1388                 "nasm" => \$nasm,
1389                 "nw-nasm" => \$nw_nasm,
1390                 "nw-mwasm" => \$nw_mwasm,
1391                 "gaswin" => \$gaswin,
1392                 "no-ssl3" => \$no_ssl3,
1393                 "no-ssl3-method" => 0,
1394                 "no-tlsext" => \$no_tlsext,
1395                 "no-srp" => \$no_srp,
1396                 "no-cms" => \$no_cms,
1397                 "no-jpake" => \$no_jpake,
1398                 "no-ec2m" => \$no_ec2m,
1399                 "no-ec_nistp_64_gcc_128" => 0,
1400                 "no-err" => \$no_err,
1401                 "no-sock" => \$no_sock,
1402                 "no-ec" => \$no_ec,
1403                 "no-gost" => \$no_gost,
1404                 "no-engine" => \$no_engine,
1405                 "no-hw" => \$no_hw,
1406                 "just-ssl" =>
1407                         [\$no_rc2, \$no_idea, \$no_des, \$no_bf, \$no_cast,
1408                           \$no_md2, \$no_mdc2, \$no_dsa, \$no_dh,
1409                           \$no_err, \$no_ripemd, \$no_rc5,
1410                           \$no_aes, \$no_camellia, \$no_seed, \$no_srp],
1411                 "rsaref" => 0,
1412                 "gcc" => \$gcc,
1413                 "debug" => \$debug,
1414                 "profile" => \$profile,
1415                 "shlib" => \$shlib,
1416                 "dll" => \$shlib,
1417                 "shared" => 0,
1418                 "no-sctp" => 0,
1419                 "no-srtp" => 0,
1420                 "no-gmp" => 0,
1421                 "no-rfc3779" => 0,
1422                 "no-montasm" => 0,
1423                 "no-shared" => 0,
1424                 "no-store" => 0,
1425                 "no-zlib" => 0,
1426                 "no-zlib-dynamic" => 0,
1427                 "no-ssl-trace" => 0,
1428                 "no-unit-test" => 0,
1429                 "no-deprecated" => 0,
1430                 "no-ocb" => 0,
1431                 "fips" => \$fips,
1432                 "fipscanisterbuild" => [\$fips, \$fipscanisterbuild],
1433                 "fipscanisteronly" => [\$fips, \$fipscanisterbuild, \$fipscanisteronly],
1434                 "fipscheck" => [\$fips, \$fipscanisterbuild, \$fipscanisteronly],
1435                 );
1436
1437         if (exists $valid_options{$_})
1438                 {
1439                 my $r = $valid_options{$_};
1440                 if ( ref $r eq "SCALAR")
1441                         { $$r = 1;}
1442                 elsif ( ref $r eq "ARRAY")
1443                         {
1444                         my $r2;
1445                         foreach $r2 (@$r)
1446                                 {
1447                                 $$r2 = 1;
1448                                 }
1449                         }
1450                 }
1451         elsif (/^no-comp$/) { $xcflags = "-DOPENSSL_NO_COMP $xcflags"; }
1452         elsif (/^enable-zlib$/) { $zlib_opt = 1 if $zlib_opt == 0 }
1453         elsif (/^enable-zlib-dynamic$/)
1454                 {
1455                 $zlib_opt = 2;
1456                 }
1457         elsif (/^no-static-engine/)
1458                 {
1459                 $no_static_engine = 1;
1460                 }
1461         elsif (/^enable-static-engine/)
1462                 {
1463                 $no_static_engine = 0;
1464                 }
1465         # There are also enable-xxx options which correspond to
1466         # the no-xxx. Since the scalars are enabled by default
1467         # these can be ignored.
1468         elsif (/^enable-/)
1469                 {
1470                 my $t = $_;
1471                 $t =~ s/^enable/no/;
1472                 if (exists $valid_options{$t})
1473                         {return 1;}
1474                 return 0;
1475                 }
1476         # experimental-xxx is mostly like enable-xxx, but opensslconf.v
1477         # will still set OPENSSL_NO_xxx unless we set OPENSSL_EXPERIMENTAL_xxx.
1478         # (No need to fail if we don't know the algorithm -- this is for adventurous users only.)
1479         elsif (/^experimental-/)
1480                 {
1481                 my $algo, $ALGO;
1482                 ($algo = $_) =~ s/^experimental-//;
1483                 ($ALGO = $algo) =~ tr/[a-z]/[A-Z]/;
1484
1485                 $xcflags="-DOPENSSL_EXPERIMENTAL_$ALGO $xcflags";
1486                 
1487                 }
1488         elsif (/^([^=]*)=(.*)$/){ $VARS{$1}=$2; }
1489         elsif (/^-[lL].*$/)     { $l_flags.="$_ "; }
1490         elsif ((!/^-help/) && (!/^-h/) && (!/^-\?/) && /^-.*$/)
1491                 { $c_flags.="$_ "; }
1492         else { return(0); }
1493         return(1);
1494         }
1495
1496 sub fipslib_error
1497         {
1498         print STDERR "***FIPS module directory sanity check failed***\n";
1499         print STDERR "FIPS module build failed, or was deleted\n";
1500         print STDERR "Please rebuild FIPS module.\n"; 
1501         exit 1;
1502         }
1503
1504 sub fips_check_files
1505         {
1506         my $dir = shift @_;
1507         my $ret = 1;
1508         if (!-d $dir)
1509                 {
1510                 print STDERR "FIPS module directory $dir does not exist\n";
1511                 fipslib_error();
1512                 }
1513         foreach (@_)
1514                 {
1515                 if (!-f "$dir${o}$_")
1516                         {
1517                         print STDERR "FIPS module file $_ does not exist!\n";
1518                         $ret = 0;
1519                         }
1520                 }
1521         fipslib_error() if ($ret == 0);
1522         }