Configurations/windows-makefile.tmpl: expand environments early.
[openssl.git] / Configurations / windows-makefile.tmpl
1 ##
2 ## Makefile for OpenSSL
3 ##
4 ## {- join("\n## ", @autowarntext) -}
5 {-
6  our $objext = $target{obj_extension} || ".obj";
7  our $depext = $target{dep_extension} || ".d";
8  our $exeext = $target{exe_extension} || ".exe";
9  our $libext = $target{lib_extension} || ".lib";
10  our $shlibext = $target{shared_extension} || ".dll";
11  our $shlibextimport = $target{shared_import_extension} || ".lib";
12  our $dsoext = $target{dso_extension} || ".dll";
13
14  my $win_installenv =
15      $target{build_scheme}->[2] eq "VC-W32" ?
16      "ProgramFiles(x86)" : "ProgramW6432";
17  my $win_commonenv =
18      $target{build_scheme}->[2] eq "VC-W32"
19      ? "CommonProgramFiles(x86)" : "CommonProgramW6432";
20  our $win_installroot =
21      defined($ENV{$win_installenv})
22      ? $win_installenv : 'ProgramFiles';
23  our $win_commonroot =
24      defined($ENV{$win_commonenv})
25      ? $win_commonenv : 'CommonProgramFiles';
26
27  # expand variables early
28  $win_installroot = $ENV{$win_installroot};
29  $win_commonroot = $ENV{$win_commonroot};
30
31  sub shlib {
32      return () if $disabled{shared};
33      my $lib = shift;
34      return $unified_info{sharednames}->{$lib} . $shlibext;
35  }
36
37  sub shlib_import {
38      return () if $disabled{shared};
39      my $lib = shift;
40      return $lib . $shlibextimport;
41  }
42
43  sub dso {
44      my $dso = shift;
45
46      return $dso . $dsoext;
47  }
48  '';
49 -}
50
51 PLATFORM={- $config{target} -}
52 SRCDIR={- $config{sourcedir} -}
53 BLDDIR={- $config{builddir} -}
54
55 VERSION={- $config{version} -}
56 MAJOR={- $config{major} -}
57 MINOR={- $config{minor} -}
58
59 SHLIB_VERSION_NUMBER={- $config{shlib_version_number} -}
60
61 LIBS={- join(" ", map { $_.$libext } @{$unified_info{libraries}}) -}
62 SHLIBS={- join(" ", map { shlib($_) } @{$unified_info{libraries}}) -}
63 ENGINES={- join(" ", map { dso($_) } @{$unified_info{engines}}) -}
64 PROGRAMS={- join(" ", map { $_.$exeext } grep { !m|^test\\| } @{$unified_info{programs}}) -}
65 TESTPROGS={- join(" ", map { $_.$exeext } grep { m|^test\\| } @{$unified_info{programs}}) -}
66 SCRIPTS={- join(" ", @{$unified_info{scripts}}) -}
67
68 {- output_off() if $disabled{makedepend}; "" -}
69 DEPS={- join(" ", map { (my $x = $_) =~ s|\.o$|$depext|; $x; }
70                   grep { $unified_info{sources}->{$_}->[0] =~ /\.c$/ }
71                   keys %{$unified_info{sources}}); -}
72 {- output_on() if $disabled{makedepend}; "" -}
73
74 # Do not edit these manually. Use Configure with --prefix or --openssldir
75 # to change this!  Short explanation in the top comment in Configure
76 INSTALLTOP={- # $prefix is used in the OPENSSLDIR perl snippet
77               #
78               our $prefix = $config{prefix} || "$win_installroot\\OpenSSL";
79               $prefix -}
80 OPENSSLDIR={- #
81               # The logic here is that if no --openssldir was given,
82               # OPENSSLDIR will get the value from $prefix plus "/ssl".
83               # If --openssldir was given and the value is an absolute
84               # path, OPENSSLDIR will get its value without change.
85               # If the value from --openssldir is a relative path,
86               # OPENSSLDIR will get $prefix with the --openssldir
87               # value appended as a subdirectory.
88               #
89               use File::Spec::Functions;
90               our $openssldir =
91                   $config{openssldir} ?
92                       (file_name_is_absolute($config{openssldir}) ?
93                            $config{openssldir}
94                            : catdir($prefix, $config{openssldir}))
95                       : "$win_commonroot\\SSL";
96               $openssldir -}
97 LIBDIR={- our $libdir = $config{libdir} || "lib";
98           $libdir -}
99 ENGINESDIR={- use File::Spec::Functions;
100               our $enginesdir = catdir($prefix,$libdir,"engines");
101               $enginesdir -}
102
103 CC={- $target{cc} -}
104 CFLAGS={- join(" ",(map { "-D".$_} @{$target{defines}}, @{$config{defines}})) -} {- join(" ", quotify_l("-DENGINESDIR=\"$enginesdir\"", "-DOPENSSLDIR=\"$openssldir\"")) -} {- $target{cflags} -} {- $config{cflags} -}
105 COUTFLAG={- $target{coutflag} || "/Fo" -}
106 RC={- $target{rc} || "rc" -}
107 RCOUTFLAG={- $target{rcoutflag} || "/fo" -}
108 LD={- $target{ld} || "link" -}
109 LDFLAGS={- $target{lflags} -}
110 LDOUTFLAG={- $target{loutflag} || "/out:" -}
111 EX_LIBS={- $target{ex_libs} -}
112 LIB_CFLAGS={- join(" ", $target{lib_cflags}, $target{shared_cflag}) || "" -}
113 LIB_LDFLAGS={- $target{shared_ldflag} || "" -}
114 DSO_CFLAGS={- join(" ", $target{dso_cflags}, $target{shared_cflag}) || "" -}
115 DSO_LDFLAGS={- join(" ", $target{dso_lflags}, $target{shared_ldflag}) || "" -}
116 BIN_CFLAGS={- $target{bin_cflags} -}
117 BIN_LDFLAGS={- $target{bin_lflags} -}
118
119 PERL={- $config{perl} -}
120
121 AR={- $target{ar} -}
122 ARFLAGS= {- $target{arflags} -}
123 AROUTFLAG={- $target{aroutflag} || "/out:" -}
124
125 AS={- $target{as} -}
126 ASFLAGS={- $target{asflags} -}
127 ASOUTFLAG={- $target{asoutflag} -}
128 PERLASM_SCHEME= {- $target{perlasm_scheme} -}
129
130 PROCESSOR= {- $config{processor} -}
131
132 # The main targets ###################################################
133
134 all: configdata.pm build_libs_nodep build_engines_nodep build_apps_nodep depend
135
136 build_libs: configdata.pm build_libs_nodep depend
137 build_libs_nodep: $(LIBS)
138 build_engines: configdata.pm build_engines_nodep depend
139 build_engines_nodep: $(ENGINES)
140 build_apps: configdata.pm build_apps_nodep depend
141 build_apps_nodep: $(PROGRAMS) $(SCRIPTS)
142 build_tests: configdata.pm build_tests_nodep depend
143 build_tests_nodep: $(TESTPROGS)
144
145 test: tests
146 tests: build_tests_nodep build_apps_nodep build_engines_nodep depend
147         @rem {- output_off() if $disabled{tests}; "" -}
148         set SRCTOP=$(SRCDIR)
149         set BLDTOP=$(BLDDIR)
150         set PERL=$(PERL)
151         $(PERL) $(SRCDIR)\test\run_tests.pl $(TESTS)
152         @rem {- if ($disabled{tests}) { output_on(); } else { output_off(); } "" -}
153         @echo "Tests are not supported with your chosen Configure options"
154         @rem {- output_on() if !$disabled{tests}; "" -}
155
156 list-tests:
157         @set TOP=$(SRCDIR)
158         @set PERL=$(PERL)
159         @$(PERL) $(SRCDIR)\test\run_tests.pl list
160
161 install: install_sw install_ssldirs install_docs
162
163 uninstall: uninstall_docs uninstall_sw
164
165 libclean:
166         $(PERL) -e "map { m/(.*)\.dll$$/; unlink glob """$$1.*""" } @ARGV" $(SHLIBS)
167         del /Q /F $(LIBS)
168         del lib.pdb
169
170 clean: libclean
171         del /Q /F $(PROGRAMS) $(TESTPROGS) $(ENGINES) $(SCRIPTS)
172         del /Q /S /F *.asm
173         del /Q /S /F *.d
174         del /Q /S /F *.obj
175         del /Q /S /F *.pdb
176         del /Q /S /F *.exp
177         del /Q /S /F engines\*.ilk
178         del /Q /S /F engines\*.lib
179
180 depend:
181
182 # Install helper targets #############################################
183
184 install_sw: all install_dev install_engines install_runtime
185
186 uninstall_sw: uninstall_runtime uninstall_engines uninstall_dev
187
188 install_docs:
189
190 uninstall_docs:
191
192 install_ssldirs:
193         @$(PERL) $(SRCDIR)\util\mkdir-p.pl "$(DESTDIR)$(OPENSSLDIR)\certs"
194         @$(PERL) $(SRCDIR)\util\mkdir-p.pl "$(DESTDIR)$(OPENSSLDIR)\private"
195
196 install_dev:
197         @if "$(INSTALLTOP)"=="" ( echo INSTALLTOP should not be empty & exit 1 )
198         @echo *** Installing development files
199         @$(PERL) $(SRCDIR)\util\mkdir-p.pl "$(DESTDIR)$(INSTALLTOP)\include\openssl"
200         @$(PERL) $(SRCDIR)\util\copy.pl $(SRCDIR)\include\openssl\*.h \
201                                        "$(DESTDIR)$(INSTALLTOP)\include\openssl"
202         @$(PERL) $(SRCDIR)\util\copy.pl $(BLDDIR)\include\openssl\*.h \
203                                        "$(DESTDIR)$(INSTALLTOP)\include\openssl"
204         @$(PERL) $(SRCDIR)\util\mkdir-p.pl "$(DESTDIR)$(INSTALLTOP)\$(LIBDIR)"
205         @$(PERL) $(SRCDIR)\util\copy.pl $(LIBS) \
206                                        "$(DESTDIR)$(INSTALLTOP)\$(LIBDIR)"
207
208 uninstall_dev:
209
210 install_engines:
211         @if "$(INSTALLTOP)"=="" ( echo INSTALLTOP should not be empty & exit 1 )
212         @echo *** Installing engines
213         @$(PERL) $(SRCDIR)\util\mkdir-p.pl "$(DESTDIR)$(ENGINESDIR)"
214         @if not "$(ENGINES)"=="" \
215          $(PERL) $(SRCDIR)\util\copy.pl $(ENGINES) "$(DESTDIR)$(ENGINESDIR)"
216
217 uninstall_engines:
218
219 install_runtime:
220         @if "$(INSTALLTOP)"=="" ( echo INSTALLTOP should not be empty & exit 1 )
221         @echo *** Installing runtime files
222         @$(PERL) $(SRCDIR)\util\mkdir-p.pl "$(DESTDIR)$(INSTALLTOP)\bin"
223         @if not "$(SHLIBS)"=="" \
224          $(PERL) $(SRCDIR)\util\copy.pl $(SHLIBS) "$(DESTDIR)$(INSTALLTOP)\bin"
225         @$(PERL) $(SRCDIR)\util\copy.pl $(PROGRAMS) "$(DESTDIR)$(INSTALLTOP)\bin"
226
227 uninstall_runtime:
228
229 # Building targets ###################################################
230
231 configdata.pm: {- $config{build_file_template} -} $(SRCDIR)\Configure
232         @echo "Detected changed: $?"
233         @echo "Reconfiguring..."
234         $(PERL) $(SRCDIR)\Configure reconf
235         @echo "**************************************************"
236         @echo "***                                            ***"
237         @echo "***   Please run the same make command again   ***"
238         @echo "***                                            ***"
239         @echo "**************************************************"
240         @exit 1
241
242 {-
243  use File::Basename;
244  use File::Spec::Functions qw/:DEFAULT abs2rel rel2abs/;
245
246  # Helper function to figure out dependencies on libraries
247  # It takes a list of library names and outputs a list of dependencies
248  sub compute_lib_depends {
249      if ($disabled{shared}) {
250          return map { $_.$libext } @_;
251      }
252      return map { shlib_import($_) } @_;
253  }
254
255   sub generatesrc {
256       my %args = @_;
257       (my $target = $args{src}) =~ s/\.[sS]$/.asm/;
258       my $generator = join(" ", @{$args{generator}});
259       my $generator_incs = join("", map { " -I".$_ } @{$args{generator_incs}});
260       my $incs = join("", map { " /I ".$_ } @{$args{incs}});
261       my $deps = join(" ", @{$args{generator_deps}}, @{$args{deps}});
262
263       if ($target !~ /\.asm$/) {
264           return <<"EOF";
265 $target: $args{generator}->[0] $deps
266         \$(PERL)$generator_incs $generator > \$@
267 EOF
268       } else {
269           if ($args{generator}->[0] =~ /\.pl$/) {
270               $generator = '$(PERL)'.$generator_incs.' '.$generator;
271           } elsif ($args{generator}->[0] =~ /\.S$/) {
272               $generator = undef;
273           } else {
274               die "Generator type for $src unknown: $generator\n";
275           }
276
277           if (defined($generator)) {
278               # If the target is named foo.S in build.info, we want to
279               # end up generating foo.s in two steps.
280               if ($args{src} =~ /\.S$/) {
281                    return <<"EOF";
282 $target: $args{generator}->[0] $deps
283         set ASM=\$(AS)
284         set CC=\$(CC)
285         $generator \$@.S
286         \$(CC) \$(CFLAGS) $incs /EP /C \$@.S > \$@.i && move /Y \$@.i \$@
287         del /Q \$@.S
288 EOF
289               }
290               # Otherwise....
291               return <<"EOF";
292 $target: $args{generator}->[0] $deps
293         set ASM=\$(AS)
294         set CC=\$(CC)
295         $generator \$@
296 EOF
297           }
298           return <<"EOF";
299 $target: $args{generator}->[0] $deps
300         \$(CC) \$(CFLAGS) $incs /EP /C $args{generator}->[0] > \$@.i && move /Y \$@.i \$@
301 EOF
302       }
303   }
304
305  sub src2obj {
306      my %args = @_;
307      my $obj = $args{obj};
308      my @srcs = map { (my $x = $_) =~ s/\.s$/.asm/; $x
309                     } ( @{$args{srcs}} );
310      my $srcs = join(" ",  @srcs);
311      my $deps = join(" ", @srcs, @{$args{deps}});
312      my $incs = join("", map { " /I ".$_ } @{$args{incs}});
313      unless ($disabled{zlib}) {
314          if ($withargs{zlib_include}) {
315              $incs .= " /I ".$withargs{zlib_include};
316          }
317      }
318      my $ecflags = { lib => '$(LIB_CFLAGS)',
319                      dso => '$(DSO_CFLAGS)',
320                      bin => '$(BIN_CFLAGS)' } -> {$args{intent}};
321      my $makedepprog = $config{makedepprog};
322      if ($srcs[0] =~ /\.asm$/) {
323          return <<"EOF";
324 $obj$objext: $deps
325         \$(AS) \$(ASFLAGS) \$(ASOUTFLAG)\$\@ $srcs
326 EOF
327      }
328      return <<"EOF"     if (!$disabled{makedepend});
329 $obj$depext: $deps
330         \$(CC) \$(CFLAGS) $ecflags$inc /Zs /showIncludes $srcs 2>&1 | \\
331             \$(PERL) -n << > $obj$depext
332 chomp;
333 s/^Note: including file: *//;
334 \$\$collect{\$\$_} = 1;
335 END { print '$obj$objext: ',join(" ", sort keys \%collect),"\\n" }
336 <<
337 $obj$objext: $obj$depext
338         \$(CC) \$(CFLAGS) $ecflags$incs -c \$(COUTFLAG)\$\@ @<<
339 $srcs
340 <<
341 EOF
342     return <<"EOF"      if ($disabled{makedepend});
343 $obj$objext: $deps
344         \$(CC) \$(CFLAGS) $ecflags$incs -c \$(COUTFLAG)\$\@ $srcs
345 EOF
346  }
347
348  # On Unix, we build shlibs from static libs, so we're ignoring the
349  # object file array.  We *know* this routine is only called when we've
350  # configure 'shared'.
351  sub libobj2shlib {
352      my %args = @_;
353      my $lib = $args{lib};
354      my $shlib = $args{shlib};
355      (my $mkdef_key = $lib) =~ s/^lib//i;
356      my $objs = join("\n", map { $_.$objext } @{$args{objs}});
357      my $linklibs = join("",
358                          map { "\n$_" } compute_lib_depends(@{$args{deps}}));
359      my $deps = join(" ",
360                      (map { $_.$objext } @{$args{objs}}),
361                      compute_lib_depends(@{$args{deps}}));
362      my $ordinalsfile = defined($args{ordinals}) ? $args{ordinals}->[1] : "";
363      my $mkdef_pl = abs2rel(rel2abs(catfile($config{sourcedir},
364                                             "util", "mkdef.pl")),
365                             rel2abs($config{builddir}));
366      my $mkrc_pl = abs2rel(rel2abs(catfile($config{sourcedir},
367                                            "util", "mkrc.pl")),
368                            rel2abs($config{builddir}));
369      my $target = shlib_import($lib);
370      return <<"EOF"
371 $target: $deps $ordinalsfile $mkdef_pl
372         \$(PERL) $mkdef_pl "$mkdef_key" 32 > $shlib.def
373         \$(PERL) -i.tmp -pe "s|^LIBRARY\\s+${mkdef_key}32|LIBRARY $shlib|;" $shlib.def
374         DEL $shlib.def.tmp
375         \$(PERL) $mkrc_pl $shlib$shlibext > $shlib.rc
376         \$(RC) \$(RCOUTFLAG)$shlib.res $shlib.rc
377         \$(LD) \$(LDFLAGS) \$(LIB_LDFLAGS) \\
378                 /implib:\$@ \$(LDOUTFLAG)$shlib$shlibext /def:$shlib.def @<< || (DEL /Q \$(\@B).* $shlib.* && EXIT 1)
379 $objs $shlib.res$linklibs \$(EX_LIBS)
380 <<
381         DEL /F apps\\$shlib$shlibext
382         DEL /F test\\$shlib$shlibext
383         COPY $shlib$shlibext apps
384         COPY $shlib$shlibext test
385 EOF
386  }
387  sub obj2dso {
388      my %args = @_;
389      my $dso = $args{lib};
390      my $dso_n = basename($dso);
391      my $objs = join("\n", map { $_.$objext } @{$args{objs}});
392      my $linklibs = join("",
393                          map { "\n$_" } compute_lib_depends(@{$args{deps}}));
394      my $deps = join(" ",
395                      (map { $_.$objext } @{$args{objs}}),
396                      compute_lib_depends(@{$args{deps}}));
397      return <<"EOF";
398 $dso$dsoext: $deps
399         \$(LD) \$(LDFLAGS) \$(DSO_LDFLAGS) \$(LDOUTFLAG)$dso$dsoext /def:<< @<<
400 LIBRARY         $dso_n
401 EXPORTS
402     bind_engine         @1
403     v_check             @2
404 <<
405 $objs$linklibs \$(EX_LIBS)
406 <<
407 EOF
408  }
409  sub obj2lib {
410      # Because static libs and import libs are both named the same in native
411      # Windows, we can't have both.  We skip the static lib in that case,
412      # as the shared libs are what we use anyway.
413      return "" unless $disabled{"shared"};
414
415      my %args = @_;
416      my $lib = $args{lib};
417      my $objs = join("\n", map { $_.$objext } @{$args{objs}});
418      my $deps = join(" ", map { $_.$objext } @{$args{objs}});
419      return <<"EOF";
420 $lib$libext: $deps
421         \$(AR) \$(ARFLAGS) \$(AROUTFLAG)$lib$libext @<<
422 \$\?
423 <<
424 EOF
425  }
426  sub obj2bin {
427      my %args = @_;
428      my $bin = $args{bin};
429      my $objs = join("\n", map { $_.$objext } @{$args{objs}});
430      my $linklibs = join("",
431                          map { "\n$_" } compute_lib_depends(@{$args{deps}}));
432      my $deps = join(" ",
433                      (map { $_.$objext } @{$args{objs}}),
434                      compute_lib_depends(@{$args{deps}}));
435      return <<"EOF";
436 $bin$exeext: $deps
437         \$(LD) \$(LDFLAGS) \$(BIN_LDFLAGS) \$(LDOUTFLAG)$bin$exeext @<<
438 $objs setargv.obj$linklibs \$(EX_LIBS)
439 <<
440 EOF
441   }
442   sub in2script {
443       my %args = @_;
444       my $script = $args{script};
445       my $sources = join(" ", @{$args{sources}});
446       my $dofile = abs2rel(rel2abs(catfile($config{sourcedir},
447                                            "util", "dofile.pl")),
448                            rel2abs($config{builddir}));
449       return <<"EOF";
450 $script: $sources
451         \$(PERL) "-I\$(BLDDIR)" -Mconfigdata "$dofile" \\
452             "-o$target{build_file}" $sources > "$script"
453 EOF
454   }
455   ""    # Important!  This becomes part of the template result.
456 -}