## descrip.mms to build OpenSSL on OpenVMS
##
## {- join("\n## ", @autowarntext) -}
{-
  use File::Spec::Functions qw/:DEFAULT abs2rel rel2abs/;

  # Our prefix, claimed when speaking with the VSI folks Tuesday
  # January 26th 2016
  our $osslprefix = 'OSSL$';
  (our $osslprefix_q = $osslprefix) =~ s/\$/\\\$/;

  our $sover = sprintf "%02d%02d", $config{shlib_major}, $config{shlib_minor};
  our $osslver = sprintf "%02d%02d", split(/\./, $config{version});

  our $sourcedir = $config{sourcedir};
  our $builddir = $config{builddir};
  sub sourcefile {
      catfile($sourcedir, @_);
  }
  sub buildfile {
      catfile($builddir, @_);
  }
  sub sourcedir {
      catdir($sourcedir, @_);
  }
  sub builddir {
      catdir($builddir, @_);
  }
  sub tree {
      (my $x = shift) =~ s|\]$|...]|;
      $x
  }
  sub move {
      my $f = catdir(@_);
      my $b = abs2rel(rel2abs("."),rel2abs($f));
      $sourcedir = catdir($b,$sourcedir)
          if !file_name_is_absolute($sourcedir);
      $builddir = catdir($b,$builddir)
          if !file_name_is_absolute($builddir);
      "";
  }

  # Because we need to make two computations of these data,
  # we store them in arrays for reuse
  our @shlibs = map { $unified_info{sharednames}->{$_} || () } @{$unified_info{libraries}};
  our @install_shlibs = map { $unified_info{sharednames}->{$_} || () } @{$unified_info{install}->{libraries}};
  our @generated = ( ( map { (my $x = $_) =~ s|\.S$|\.s|; $x }
                       grep { defined $unified_info{generate}->{$_} }
                       map { @{$unified_info{sources}->{$_}} }
                       grep { /\.o$/ } keys %{$unified_info{sources}} ),
                     ( grep { /\.h$/ } keys %{$unified_info{generate}} ) );

  # This is a horrible hack, but is needed because recursive inclusion of files
  # in different directories does not work well with HP C.
  my $sd = sourcedir("crypto", "async", "arch");
  foreach (grep /\[\.crypto\.async\.arch\].*\.o$/, keys %{$unified_info{sources}}) {
      (my $x = $_) =~ s|\.o$|.OBJ|;
      $unified_info{before}->{$x}
          = qq(arch_include = F\$PARSE("$sd","A.;",,,"SYNTAX_ONLY") - "A.;"
        define arch 'arch_include');
      $unified_info{after}->{$x}
          = qq(deassign arch);
  }
  my $sd1 = sourcedir("ssl","record");
  my $sd2 = sourcedir("ssl","statem");
  $unified_info{before}->{"[.test]heartbeat_test.OBJ"}
      = $unified_info{before}->{"[.test]ssltest_old.OBJ"}
      = qq(record_include = F\$PARSE("$sd1","A.;",,,"SYNTAX_ONLY") - "A.;"
        define record 'record_include'
        statem_include = F\$PARSE("$sd2","A.;",,,"SYNTAX_ONLY") - "A.;"
        define statem 'statem_include');
  $unified_info{after}->{"[.test]heartbeat_test.OBJ"}
      = $unified_info{after}->{"[.test]ssltest.OBJ"}
      = qq(deassign statem
        deassign record);
  foreach (grep /^\[\.ssl\.(?:record|statem)\].*\.o$/, keys %{$unified_info{sources}}) {
      (my $x = $_) =~ s|\.o$|.OBJ|;
      $unified_info{before}->{$x}
          = qq(record_include = F\$PARSE("$sd1","A.;",,,"SYNTAX_ONLY") - "A.;"
        define record 'record_include'
        statem_include = F\$PARSE("$sd2","A.;",,,"SYNTAX_ONLY") - "A.;"
        define statem 'statem_include');
      $unified_info{after}->{$x}
          = qq(deassign statem
        deassign record);
  }
  # This makes sure things get built in the order they need
  # to. You're welcome.
  sub dependmagic {
      my $target = shift;

      return "$target : build_generated\n\t\pipe \$(MMS) \$(MMSQUALIFIERS) depend && \$(MMS) \$(MMSQUALIFIERS) _$target\n_$target";
  }
  #use Data::Dumper;
  #print STDERR "DEBUG: before:\n", Dumper($unified_info{before});
  #print STDERR "DEBUG: after:\n", Dumper($unified_info{after});
  "";
-}
PLATFORM={- $config{target} -}
OPTIONS={- $config{options} -}
CONFIGURE_ARGS=({- join(", ",quotify_l(@{$config{perlargv}})) -})
SRCDIR={- $config{sourcedir} -}
BLDDIR={- $config{builddir} -}

# Allow both V and VERBOSE to indicate verbosity.  This only applies
# to testing.
VERBOSE=$(V)

VERSION={- $config{version} -}
MAJOR={- $config{major} -}
MINOR={- $config{minor} -}
SHLIB_VERSION_NUMBER={- $config{shlib_version_number} -}
SHLIB_VERSION_HISTORY={- $config{shlib_version_history} -}
SHLIB_MAJOR={- $config{shlib_major} -}
SHLIB_MINOR={- $config{shlib_minor} -}
SHLIB_TARGET={- $target{shared_target} -}

EXE_EXT=.EXE
LIB_EXT=.OLB
SHLIB_EXT=.EXE
OBJ_EXT=.OBJ
DEP_EXT=.D

LIBS={- join(", ", map { "-\n\t".$_.".OLB" } @{$unified_info{libraries}}) -}
SHLIBS={- join(", ", map { "-\n\t".$_.".EXE" } @shlibs) -}
ENGINES={- join(", ", map { "-\n\t".$_.".EXE" } @{$unified_info{engines}}) -}
PROGRAMS={- join(", ", map { "-\n\t".$_.".EXE" } @{$unified_info{programs}}) -}
SCRIPTS={- join(", ", map { "-\n\t".$_ } @{$unified_info{scripts}}) -}
{- output_off() if $disabled{makedepend}; "" -}
DEPS={- our @deps = map { (my $x = $_) =~ s|\.o$|\$(DEP_EXT)|; $x; }
                    grep { $unified_info{sources}->{$_}->[0] =~ /\.c$/ }
                    keys %{$unified_info{sources}};
        join(", ", map { "-\n\t".$_ } @deps); -}
{- output_on() if $disabled{makedepend}; "" -}
GENERATED_MANDATORY={- join(", ", map { "-\n\t".$_ } @{$unified_info{depends}->{""}} ) -}
GENERATED={- join(", ", map { "-\n\t".$_ } @generated) -}

INSTALL_LIBS={- join(", ", map { "-\n\t".$_.".OLB" } @{$unified_info{install}->{libraries}}) -}
INSTALL_SHLIBS={- join(", ", map { "-\n\t".$_.".EXE" } @install_shlibs) -}
INSTALL_ENGINES={- join(", ", map { "-\n\t".$_.".EXE" } @{$unified_info{install}->{engines}}) -}
INSTALL_PROGRAMS={- join(", ", map { "-\n\t".$_.".EXE" } @{$unified_info{install}->{programs}}) -}
{- output_off() if $disabled{apps}; "" -}
BIN_SCRIPTS=[.tools]c_rehash.pl
MISC_SCRIPTS=[.apps]CA.pl, [.apps]tsget.pl
{- output_on() if $disabled{apps}; "" -}

APPS_OPENSSL={- use File::Spec::Functions;
                catfile("apps","openssl") -}

# DESTDIR is for package builders so that they can configure for, say,
# SYS$COMMON:[OPENSSL] and yet have everything installed in STAGING:[USER].
# In that case, configure with --prefix=SYS$COMMON:[OPENSSL] and then run
# MMS with /MACROS=(DESTDIR=STAGING:[USER]).  The result will end up in
# STAGING:[USER.OPENSSL].
# Normally it is left empty.
DESTDIR=

# Do not edit this manually. Use Configure --prefix=DIR to change this!
INSTALLTOP={- our $installtop =
                  catdir($config{prefix}) || "SYS\$COMMON:[OPENSSL]";
              $installtop -}
SYSTARTUP={- catdir($installtop, '[.SYS$STARTUP]'); -}
# This is the standard central area to store certificates, private keys...
OPENSSLDIR={- catdir($config{openssldir}) or
              $config{prefix} ? catdir($config{prefix},"COMMON")
                              : "SYS\$COMMON:[OPENSSL-COMMON]" -}
# The same, but for C
OPENSSLDIR_C={- $osslprefix -}DATAROOT:[000000]
# Where installed engines reside, for C
ENGINESDIR_C={- $osslprefix -}ENGINES{- $sover.$target{pointer_size} -}:

CC= {- $target{cc} -}
CFLAGS= /DEFINE=({- join(",", @{$target{defines}}, @{$config{defines}},"OPENSSLDIR=\"\"\"\$(OPENSSLDIR_C)\"\"\"","ENGINESDIR=\"\"\"\$(ENGINESDIR_C)\"\"\"") -}) {- $target{cflags} -} {- $config{cflags} -}
CFLAGS_Q=$(CFLAGS)
DEPFLAG= /DEFINE=({- join(",", @{$config{depdefines}}) -})
LDFLAGS= {- $target{lflags} -}
EX_LIBS= {- $target{ex_libs} ? ",".$target{ex_libs} : "" -}{- $config{ex_libs} ? ",".$config{ex_libs} : "" -}
LIB_CFLAGS={- $target{lib_cflags} || "" -}
DSO_CFLAGS={- $target{dso_cflags} || "" -}
BIN_CFLAGS={- $target{bin_cflags} || "" -}

PERL={- $config{perl} -}

# We let the C compiler driver to take care of .s files. This is done in
# order to be excused from maintaining a separate set of architecture
# dependent assembler flags. E.g. if you throw -mcpu=ultrasparc at SPARC
# gcc, then the driver will automatically translate it to -xarch=v8plus
# and pass it down to assembler.
AS={- $target{as} -}
ASFLAG={- $target{asflags} -}

# .FIRST and .LAST are special targets with MMS and MMK.
# The defines in there are for C.  includes that look like
# this:
#
#    #include <openssl/foo.h>
#    #include "internal/bar.h"
#
# will use the logical names to find the files.  Expecting
# DECompHP C to find files in subdirectories of whatever was
# given with /INCLUDE is a fantasy, unfortunately.
NODEBUG=@
.FIRST :
        $(NODEBUG) openssl_inc1 = F$PARSE("[.include.openssl]","A.;",,,"syntax_only") - "A.;"
        $(NODEBUG) openssl_inc2 = F$PARSE("{- catdir($config{sourcedir},"[.include.openssl]") -}","A.;",,,"SYNTAX_ONLY") - "A.;"
        $(NODEBUG) internal_inc1 = F$PARSE("[.crypto.include.internal]","A.;",,,"SYNTAX_ONLY") - "A.;"
        $(NODEBUG) internal_inc2 = F$PARSE("{- catdir($config{sourcedir},"[.include.internal]") -}","A.;",,,"SYNTAX_ONLY") - "A.;"
        $(NODEBUG) internal_inc3 = F$PARSE("{- catdir($config{sourcedir},"[.crypto.include.internal]") -}","A.;",,,"SYNTAX_ONLY") - "A.;"
        $(NODEBUG) DEFINE openssl 'openssl_inc1','openssl_inc2'
        $(NODEBUG) DEFINE internal 'internal_inc1','internal_inc2','internal_inc3'
        $(NODEBUG) staging_dir = "$(DESTDIR)"
        $(NODEBUG) staging_instdir = ""
        $(NODEBUG) staging_datadir = ""
        $(NODEBUG) IF staging_dir .NES. "" THEN -
                staging_instdir = F$PARSE("A.;",staging_dir,"[]",,"SYNTAX_ONLY")
        $(NODEBUG) IF staging_instdir - "]A.;" .NES. staging_instdir THEN -
                staging_instdir = staging_instdir - "]A.;" + ".OPENSSL-INSTALL]"
        $(NODEBUG) IF staging_instdir - "A.;" .NES. staging_instdir THEN -
                staging_instdir = staging_instdir - "A.;" + "[OPENSSL-INSTALL]"
        $(NODEBUG) IF staging_dir .NES. "" THEN -
                staging_datadir = F$PARSE("A.;",staging_dir,"[]",,"SYNTAX_ONLY")
        $(NODEBUG) IF staging_datadir - "]A.;" .NES. staging_datadir THEN -
                staging_datadir = staging_datadir - "]A.;" + ".OPENSSL-COMMON]"
        $(NODEBUG) IF staging_datadir - "A.;" .NES. staging_datadir THEN -
                staging_datadir = staging_datadir - "A.;" + "[OPENSSL-COMMON]"
        $(NODEBUG) !
        $(NODEBUG) ! Installation logical names
        $(NODEBUG) !
        $(NODEBUG) installtop = F$PARSE(staging_instdir,"$(INSTALLTOP)","[]A.;",,"SYNTAX_ONLY,NO_CONCEAL") - ".][000000" - "[000000." - "][" - "]A.;" + ".]"
        $(NODEBUG) datatop = F$PARSE(staging_datadir,"$(OPENSSLDIR)","[]A.;",,"SYNTAX_ONLY,NO_CONCEAL") - ".][000000" - "[000000." - "][" - "]A.;" + ".]"
        $(NODEBUG) DEFINE ossl_installroot 'installtop'
        $(NODEBUG) DEFINE ossl_dataroot 'datatop'
        $(NODEBUG) !
        $(NODEBUG) ! Figure out the architecture
        $(NODEBUG) !
        $(NODEBUG) arch = f$edit( f$getsyi( "arch_name"), "upcase")
        $(NODEBUG) !
        $(NODEBUG) ! Set up logical names for the libraries, so LINK and
        $(NODEBUG) ! running programs can use them.
        $(NODEBUG) !
        $(NODEBUG) {- join("\n\t\$(NODEBUG) ", map { "DEFINE ".uc($_)." 'F\$ENV(\"DEFAULT\")'".uc($_)."\$(SHLIB_EXT)" } map { $unified_info{sharednames}->{$_} || () } @{$unified_info{libraries}}) || "!" -}

.LAST :
        $(NODEBUG) {- join("\n\t\$(NODEBUG) ", map { "DEASSIGN ".uc($_) } map { $unified_info{sharednames}->{$_} || () } @{$unified_info{libraries}}) || "!" -}
        $(NODEBUG) DEASSIGN ossl_dataroot
        $(NODEBUG) DEASSIGN ossl_installroot
        $(NODEBUG) DEASSIGN internal
        $(NODEBUG) DEASSIGN openssl
.DEFAULT :
        @ ! MMS cannot handle no actions...

# The main targets ###################################################

{- dependmagic('all'); -} : build_libs_nodep, build_engines_nodep, build_programs_nodep
{- dependmagic('build_libs'); -} : build_libs_nodep
{- dependmagic('build_engines'); -} : build_engines_nodep
{- dependmagic('build_programs'); -} : build_programs_nodep

build_generated : $(GENERATED_MANDATORY)
build_libs_nodep : $(LIBS), $(SHLIBS)
build_engines_nodep : $(ENGINES)
build_programs_nodep : $(PROGRAMS), $(SCRIPTS)

# Kept around for backward compatibility
build_apps build_tests : build_programs

test : tests
{- dependmagic('tests'); -} : build_programs_nodep, build_engines_nodep
        @ ! {- output_off() if $disabled{tests}; "" -}
        SET DEFAULT [.test]{- move("test") -}
        CREATE/DIR [.test-runs]
        DEFINE SRCTOP {- sourcedir() -}
        DEFINE BLDTOP {- builddir() -}
        DEFINE RESULT_D {- builddir(qw(test test-runs)) -}
        DEFINE OPENSSL_ENGINES {- builddir("engines") -}
        DEFINE OPENSSL_DEBUG_MEMORY "on"
        IF "$(VERBOSE)" .NES. "" THEN DEFINE VERBOSE "$(VERBOSE)"
        $(PERL) {- sourcefile("test", "run_tests.pl") -} $(TESTS)
        DEASSIGN OPENSSL_DEBUG_MEMORY
        DEASSIGN OPENSSL_ENGINES
        DEASSIGN BLDTOP
        DEASSIGN SRCTOP
        SET DEFAULT [-]{- move("..") -}
        @ ! {- if ($disabled{tests}) { output_on(); } else { output_off(); } "" -}
        @ WRITE SYS$OUTPUT "Tests are not supported with your chosen Configure options"
        @ ! {- output_on() if !$disabled{tests}; "" -}

list-tests :
        @ ! {- output_off() if $disabled{tests}; "" -}
        @ DEFINE SRCTOP {- sourcedir() -}
        @ $(PERL) {- sourcefile("test", "run_tests.pl") -} list
        @ DEASSIGN SRCTOP
        @ ! {- if ($disabled{tests}) { output_on(); } else { output_off(); } "" -}
        @ WRITE SYS$OUTPUT "Tests are not supported with your chosen Configure options"
        @ ! {- output_on() if !$disabled{tests}; "" -}

install : install_sw install_ssldirs install_docs
        @ WRITE SYS$OUTPUT ""
        @ WRITE SYS$OUTPUT "######################################################################"
        @ WRITE SYS$OUTPUT ""
        @ IF "$(DESTDIR)" .EQS. "" THEN -
             PIPE ( WRITE SYS$OUTPUT "Installation complete" ; -
                    WRITE SYS$OUTPUT "" ; -
                    WRITE SYS$OUTPUT "Run @$(SYSTARTUP)openssl_startup{- $osslver -} to set up logical names" ; -
                    WRITE SYS$OUTPUT "then run @$(SYSTARTUP)openssl_utils{- $osslver -} to define commands" ; -
                    WRITE SYS$OUTPUT "" )
        @ IF "$(DESTDIR)" .NES. "" THEN -
             PIPE ( WRITE SYS$OUTPUT "Staging installation complete" ; -
                    WRITE SYS$OUTPUT "" ; -
                    WRITE SYS$OUTPUT "Finish or package in such a way that the contents of the directory tree" ; -
                    WRITE SYS$OUTPUT staging_instdir ; -
                    WRITE SYS$OUTPUT "ends up in $(INSTALLTOP)," ; -
                    WRITE SYS$OUTPUT "and that the contents of the contents of the directory tree" ; -
                    WRITE SYS$OUTPUT staging_datadir ; -
                    WRITE SYS$OUTPUT "ends up in $(OPENSSLDIR)" ; -
                    WRITE SYS$OUTPUT "" ; -
                    WRITE SYS$OUTPUT "When in its final destination," ; -
                    WRITE SYS$OUTPUT "Run @$(SYSTARTUP)openssl_startup{- $osslver -} to set up logical names" ; -
                    WRITE SYS$OUTPUT "then run @$(SYSTARTUP)openssl_utils{- $osslver -} to define commands" ; -
                    WRITE SYS$OUTPUT "" )

check_install :
        spawn/nolog @ossl_installroot:[SYSTEST]openssl_ivp{- $osslver -}.com

uninstall : uninstall_docs uninstall_sw

# Because VMS wants the generation number (or *) to delete files, we can't
# use $(LIBS), $(PROGRAMS), $(GENERATED) and $(ENGINES)directly.
libclean :
        {- join("\n\t", map { "- DELETE $_.OLB;*" } @{$unified_info{libraries}}) || "@ !" -}
        {- join("\n\t", map { "- DELETE $_.EXE;*,$_.MAP;*,$_.OPT;*" } @shlibs) || "@ !" -}

clean : libclean
        {- join("\n\t", map { "- DELETE $_.EXE;*,$_.OPT;*" } @{$unified_info{programs}}) || "@ !" -}
        {- join("\n\t", map { "- DELETE $_.EXE;*,$_.OPT;*" } @{$unified_info{engines}}) || "@ !" -}
        {- join("\n\t", map { "- DELETE $_;*" } @{$unified_info{scripts}}) || "@ !" -}
        {- join("\n\t", map { "- DELETE $_;*" } @generated) || "@ !" -}
        - DELETE [...]*.MAP;*
        - DELETE [...]*.D;*
        - DELETE [...]*.OBJ;*,*.LIS;*
        - DELETE []CXX$DEMANGLER_DB.;*
        - DELETE [.VMS]openssl_startup.com;*
        - DELETE [.VMS]openssl_shutdown.com;*
        - DELETE []vmsconfig.pm;*

distclean : clean
        - DELETE configdata.pm;*
        - DELETE descrip.mms;*

depend : descrip.mms
descrip.mms : FORCE
	@ ! {- output_off() if $disabled{makedepend}; "" -}
        @ $(PERL) -pe "if (/^# DO NOT DELETE.*/) { exit(0); }" -
                < descrip.mms > descrip.mms-new
        @ OPEN/APPEND DESCRIP descrip.mms-new
        @ WRITE DESCRIP "# DO NOT DELETE THIS LINE -- make depend depends on it."
        {- join("\n\t", map { "\@ IF F\$SEARCH(\"$_\") .NES. \"\" THEN TYPE $_ /OUTPUT=DESCRIP:" } @deps); -}
        @ CLOSE DESCRIP
        @ PIPE ( $(PERL) -e "use File::Compare qw/compare_text/; my $x = compare_text(""descrip.mms"",""descrip.mms-new""); exit(0x10000000 + ($x == 0));" || -
                 RENAME descrip.mms-new descrip.mms )
        @ IF F$SEARCH("descrip.mms-new") .NES. "" THEN DELETE descrip.mms-new;*
        -@ SPAWN/OUTPUT=NLA0: PURGE/NOLOG descrip.mms
	@ ! {- output_on() if $disabled{makedepend}; "" -}

# Install helper targets #############################################

install_sw : all install_shared _install_dev_ns -
             install_engines _install_runtime_ns -
             install_startup install_ivp

uninstall_sw : uninstall_shared _uninstall_dev_ns -
               uninstall_engines _uninstall_runtime_ns -
               uninstall_startup uninstall_ivp

install_docs : install_html_docs

uninstall_docs : uninstall_html_docs

install_ssldirs : check_INSTALLTOP
        - CREATE/DIR/PROT=(S:RWED,O:RWE,G:RE,W:RE) OSSL_DATAROOT:[000000]
        IF F$SEARCH("OSSL_DATAROOT:[000000]CERTS.DIR;1") .EQS. "" THEN -
                CREATE/DIR/PROT=(S:RWED,O:RWE,G:RE,W:RE) OSSL_DATAROOT:[CERTS]
        IF F$SEARCH("OSSL_DATAROOT:[000000]PRIVATE.DIR;1") .EQS. "" THEN -
                CREATE/DIR/PROT=(S:RWED,O:RWE,G,W) OSSL_DATAROOT:[PRIVATE]
        IF F$SEARCH("OSSL_DATAROOT:[000000]MISC.DIR;1") .EQS. "" THEN -
                CREATE/DIR/PROT=(S:RWED,O:RWE,G,W) OSSL_DATAROOT:[MISC]
        COPY/PROT=W:RE $(MISC_SCRIPTS) OSSL_DATAROOT:[MISC]
        @ ! Install configuration file
        COPY/PROT=W:R {- sourcefile("apps", "openssl-vms.cnf") -} -
                ossl_dataroot:[000000]openssl.cnf-dist
        IF F$SEARCH("OSSL_DATAROOT:[000000]openssl.cnf") .EQS. "" THEN -
                COPY/PROT=W:R {- sourcefile("apps", "openssl-vms.cnf") -} -
                        ossl_dataroot:[000000]openssl.cnf

install_shared : check_INSTALLTOP
        @ {- output_off() if $disabled{shared}; "" -} !
        @ WRITE SYS$OUTPUT "*** Installing shareable images"
        @ ! Install shared (runtime) libraries
        - CREATE/DIR ossl_installroot:[LIB.'arch']
        {- join("\n        ",
                map { "COPY/PROT=W:R $_.EXE ossl_installroot:[LIB.'arch']" }
                @install_shlibs) -}
        @ {- output_on() if $disabled{shared}; "" -} !

_install_dev_ns : check_INSTALLTOP
        @ WRITE SYS$OUTPUT "*** Installing development files"
        @ ! Install header files
        - CREATE/DIR ossl_installroot:[include.openssl]
        COPY/PROT=W:R openssl:*.h ossl_installroot:[include.openssl]
        @ ! Install static (development) libraries
        - CREATE/DIR ossl_installroot:[LIB.'arch']
        {- join("\n        ",
                map { "COPY/PROT=W:R $_.OLB ossl_installroot:[LIB.'arch']" }
                @{$unified_info{install}->{libraries}}) -}

install_dev : install_shared _install_dev_ns

_install_runtime_ns : check_INSTALLTOP
        @ ! Install the main program
        - CREATE/DIR ossl_installroot:[EXE.'arch']
        COPY/PROT=W:RE [.APPS]openssl.EXE -
                ossl_installroot:[EXE.'arch']openssl{- $osslver -}.EXE
        @ ! Install scripts
        COPY/PROT=W:RE $(BIN_SCRIPTS) ossl_installroot:[EXE]
        @ ! {- output_on() if $disabled{apps}; "" -}

install_runtime : install_shared _install_runtime_ns

install_engines : check_INSTALLTOP
        @ {- output_off() unless scalar @{$unified_info{engines}}; "" -} !
        @ WRITE SYS$OUTPUT "*** Installing engines"
        - CREATE/DIR ossl_installroot:[ENGINES{- $sover.$target{pointer_size} -}.'arch']
        {- join("\n        ",
                map { "COPY/PROT=W:RE $_.EXE ossl_installroot:[ENGINES$sover$target{pointer_size}.'arch']" }
                @{$unified_info{install}->{engines}}) -}
        @ {- output_on() unless scalar @{$unified_info{engines}}; "" -} !

install_startup : [.VMS]openssl_startup.com [.VMS]openssl_shutdown.com -
                 [.VMS]openssl_utils.com, check_INSTALLTOP
        - CREATE/DIR ossl_installroot:[SYS$STARTUP]
        COPY/PROT=W:RE [.VMS]openssl_startup.com -
                ossl_installroot:[SYS$STARTUP]openssl_startup{- $osslver -}.com
        COPY/PROT=W:RE [.VMS]openssl_shutdown.com -
                ossl_installroot:[SYS$STARTUP]openssl_shutdown{- $osslver -}.com
        COPY/PROT=W:RE [.VMS]openssl_utils.com -
                ossl_installroot:[SYS$STARTUP]openssl_utils{- $osslver -}.com

install_ivp : [.VMS]openssl_ivp.com check_INSTALLTOP
        - CREATE/DIR ossl_installroot:[SYSTEST]
        COPY/PROT=W:RE [.VMS]openssl_ivp.com -
                ossl_installroot:[SYSTEST]openssl_ivp{- $osslver -}.com

[.VMS]openssl_startup.com : vmsconfig.pm {- sourcefile("VMS", "openssl_startup.com.in") -}
        - CREATE/DIR [.VMS]
        $(PERL) "-I." "-Mvmsconfig" {- sourcefile("util", "dofile.pl") -} -
                {- sourcefile("VMS", "openssl_startup.com.in") -} -
                > [.VMS]openssl_startup.com

[.VMS]openssl_utils.com : vmsconfig.pm {- sourcefile("VMS", "openssl_utils.com.in") -}
        - CREATE/DIR [.VMS]
        $(PERL) "-I." "-Mvmsconfig" {- sourcefile("util", "dofile.pl") -} -
                {- sourcefile("VMS", "openssl_utils.com.in") -} -
                > [.VMS]openssl_utils.com

[.VMS]openssl_shutdown.com : vmsconfig.pm {- sourcefile("VMS", "openssl_shutdown.com.in") -}
        - CREATE/DIR [.VMS]
        $(PERL) "-I." "-Mvmsconfig" {- sourcefile("util", "dofile.pl") -} -
                {- sourcefile("VMS", "openssl_shutdown.com.in") -} -
                > [.VMS]openssl_shutdown.com

[.VMS]openssl_ivp.com : vmsconfig.pm {- sourcefile("VMS", "openssl_ivp.com.in") -}
        - CREATE/DIR [.VMS]
        $(PERL) "-I." "-Mvmsconfig" {- sourcefile("util", "dofile.pl") -} -
                {- sourcefile("VMS", "openssl_ivp.com.in") -} -
                > [.VMS]openssl_ivp.com

vmsconfig.pm : configdata.pm
        OPEN/WRITE/SHARE=READ CONFIG []vmsconfig.pm
        WRITE CONFIG "package vmsconfig;"
        WRITE CONFIG "use strict; use warnings;"
        WRITE CONFIG "use Exporter;"
        WRITE CONFIG "our @ISA = qw(Exporter);"
        WRITE CONFIG "our @EXPORT = qw(%config %target %withargs %unified_info %disabled);"
        WRITE CONFIG "our %config = ("
        WRITE CONFIG "  target => '","{- $config{target} -}","',"
        WRITE CONFIG "  version => '","{- $config{version} -}","',"
        WRITE CONFIG "  shlib_major => '","{- $config{shlib_major} -}","',"
        WRITE CONFIG "  shlib_minor => '","{- $config{shlib_minor} -}","',"
        WRITE CONFIG "  no_shared => '","{- $disabled{shared} -}","',"
        WRITE CONFIG "  INSTALLTOP => '$(INSTALLTOP)',"
        WRITE CONFIG "  OPENSSLDIR => '$(OPENSSLDIR)',"
        WRITE CONFIG "  pointer_size => '","{- $target{pointer_size} -}","',"
        WRITE CONFIG ");"
        WRITE CONFIG "our %target = ();"
        WRITE CONFIG "our %disabled = ();"
        WRITE CONFIG "our %withargs = ();"
        WRITE CONFIG "our %unified_info = ();"
        WRITE CONFIG "1;"
        CLOSE CONFIG

install_html_docs : check_INSTALLTOP
        sourcedir = F$PARSE("{- $sourcedir -}A.;","[]") - "]A.;" + ".DOC]"
        $(PERL) {- sourcefile("util", "process_docs.pl") -} -
                --sourcedir='sourcedir' --destdir=ossl_installroot:[HTML] -
                --type=html

check_INSTALLTOP :
        @ IF "$(INSTALLTOP)" .EQS. "" THEN -
                WRITE SYS$ERROR "INSTALLTOP should not be empty"
        @ IF "$(INSTALLTOP)" .EQS. "" THEN -
                EXIT %x10000002

# Helper targets #####################################################

# Developer targets ##################################################

debug_logicals :
        SH LOGICAL/PROC openssl,internal,ossl_installroot,ossl_dataroot

# Building targets ###################################################

configdata.pm : $(SRCDIR)Configure $(SRCDIR)config.com {- join(" ", @{$config{build_file_templates}}, @{$config{build_infos}}, @{$config{conf_files}}) -}
        @ WRITE SYS$OUTPUT "Reconfiguring..."
        perl $(SRCDIR)Configure reconf
        @ WRITE SYS$OUTPUT "*************************************************"
        @ WRITE SYS$OUTPUT "***                                           ***"
        @ WRITE SYS$OUTPUT "***   Please run the same mms command again   ***"
        @ WRITE SYS$OUTPUT "***                                           ***"
        @ WRITE SYS$OUTPUT "*************************************************"
        @ PIPE ( EXIT %X10000000 )

{-
  use File::Basename;
  use File::Spec::Functions qw/abs2rel rel2abs catfile catdir/;

  sub generatesrc {
      my %args = @_;
      my $generator = join(" ", @{$args{generator}});
      my $generator_incs = join("", map { ' "-I'.$_.'"' } @{$args{generator_incs}});
      my $deps = join(", -\n\t\t", @{$args{generator_deps}}, @{$args{deps}});

      if ($args{src} !~ /\.[sS]$/) {
          if ($args{generator}->[0] =~ m|^.*\.in$|) {
	      my $dofile = abs2rel(rel2abs(catfile($config{sourcedir},
                                                   "util", "dofile.pl")),
                                   rel2abs($config{builddir}));
              return <<"EOF";
$args{src} : $args{generator}->[0] $deps
	\$(PERL) "-I\$(BLDDIR)" "-Mconfigdata" $dofile \\
	    "-o$target{build_file}" $generator > \$@
EOF
	  } else {
              return <<"EOF";
$args{src} : $args{generator}->[0] $deps
	\$(PERL)$generator_incs $generator > \$@
EOF
	  }
      } else {
          die "No method to generate assembler source present.\n";
      }
  }

  sub src2obj {
      my %args = @_;
      my $obj = $args{obj};
      my $deps = join(", -\n\t\t", @{$args{srcs}}, @{$args{deps}});

      # Because VMS C isn't very good at combining a /INCLUDE path with
      # #includes having a relative directory (like '#include "../foo.h"),
      # the best choice is to move to the first source file's intended
      # directory before compiling, and make sure to write the object file
      # in the correct position (important when the object tree is other
      # than the source tree).
      my $forward = dirname($args{srcs}->[0]);
      my $backward = abs2rel(rel2abs("."), rel2abs($forward));
      my $objd = abs2rel(rel2abs(dirname($obj)), rel2abs($forward));
      my $objn = basename($obj);
      my $srcs =
          join(", ",
               map { abs2rel(rel2abs($_), rel2abs($forward)) } @{$args{srcs}});
      my $ecflags = { lib => '$(LIB_CFLAGS)',
                      dso => '$(DSO_CFLAGS)',
                      bin => '$(BIN_CFLAGS)' } -> {$args{intent}};
      my $incs_on = "\@ !";
      my $incs_off = "\@ !";
      my $incs = "";
      my @incs = ();
      push @incs, @{$args{incs}} if @{$args{incs}};
      unless ($disabled{zlib}) {
          # GNV$ZLIB_INCLUDE is the standard logical name for later zlib
          # incarnations.
          push @incs, ($withargs{zlib_include} || 'GNV$ZLIB_INCLUDE:');
      }
      if (@incs) {
          $incs_on =
              "DEFINE tmp_includes "
              .join(",-\n\t\t\t", map {
                                      file_name_is_absolute($_)
                                      ? $_ : catdir($backward,$_)
                                  } @incs);
          $incs_off = "DEASSIGN tmp_includes";
          $incs = " /INCLUDE=(tmp_includes:)";
      }
      my $before = $unified_info{before}->{$obj.".OBJ"} || "\@ !";
      my $after = $unified_info{after}->{$obj.".OBJ"} || "\@ !";
      my $depbuild = $disabled{makedepend} ? ""
          : " /MMS=(FILE=${objd}${objn}.tmp-D,TARGET=$obj.OBJ)";

      return <<"EOF";
$obj.OBJ : $deps
        ${before}
        SET DEFAULT $forward
        $incs_on
        \$(CC) \$(CFLAGS)${ecflags}${incs}${depbuild} /OBJECT=${objd}${objn}.OBJ /REPOSITORY=$backward $srcs
        $incs_off
        SET DEFAULT $backward
        ${after}
        \@ PIPE ( \$(PERL) -e "use File::Compare qw/compare_text/; my \$x = compare_text(""$obj.D"",""$obj.tmp-D""); exit(0x10000000 + (\$x == 0));" || -
                 RENAME $obj.tmp-D $obj.d )
        \@ IF F\$SEARCH("$obj.tmp-D") .NES. "" THEN DELETE $obj.tmp-D;*
        - PURGE $obj.OBJ
EOF
  }
  sub libobj2shlib {
      my %args = @_;
      my $lib = $args{lib};
      my $shlib = $args{shlib};
      my $libd = dirname($lib);
      my $libn = basename($lib);
      (my $mkdef_key = $libn) =~ s/^${osslprefix_q}lib([^0-9]*)\d*/$1/i;
      my @deps = map {
          $disabled{shared} ? $_.".OLB"
              : $unified_info{sharednames}->{$_}.".EXE"; } @{$args{deps}};
      my $deps = join(", -\n\t\t", @deps);
      my $shlib_target = $disabled{shared} ? "" : $target{shared_target};
      my $ordinalsfile = defined($args{ordinals}) ? $args{ordinals}->[1] : "";
      my $engine_opt = abs2rel(rel2abs(catfile($config{sourcedir},
                                               "VMS", "engine.opt")),
                               rel2abs($config{builddir}));
      my $mkdef_pl = abs2rel(rel2abs(catfile($config{sourcedir},
                                             "util", "mkdef.pl")),
                             rel2abs($config{builddir}));
      my $translatesyms_pl = abs2rel(rel2abs(catfile($config{sourcedir},
                                                     "VMS", "translatesyms.pl")),
                                     rel2abs($config{builddir}));
      # The "[]" hack is because in .OPT files, each line inherits the
      # previous line's file spec as default, so if no directory spec
      # is present in the current line and the previous line has one that
      # doesn't apply, you're in for a surprise.
      my $write_opt =
          join("\n\t", map { my $x = $_ =~ /\[/ ? $_ : "[]".$_;
                             $x =~ s|(\.EXE)|$1/SHARE|;
                             $x =~ s|(\.OLB)|$1/LIB|;
                             "WRITE OPT_FILE \"$x\"" } @deps)
          || "\@ !";
      return <<"EOF";
$shlib.EXE : $lib.OLB $deps $ordinalsfile
        \$(PERL) $mkdef_pl "$mkdef_key" "VMS" > $shlib.SYMVEC-tmp
        \$(PERL) $translatesyms_pl \$(BLDDIR)CXX\$DEMANGLER_DB. < $shlib.SYMVEC-tmp > $shlib.SYMVEC
        DELETE $shlib.SYMVEC-tmp;*
        OPEN/WRITE/SHARE=READ OPT_FILE $shlib.OPT
        WRITE OPT_FILE "IDENTIFICATION=""V$config{version}"""
        TYPE $shlib.SYMVEC /OUTPUT=OPT_FILE:
        WRITE OPT_FILE "$lib.OLB/LIBRARY"
        $write_opt
        CLOSE OPT_FILE
        LINK /MAP=$shlib.MAP /FULL/SHARE=$shlib.EXE $shlib.OPT/OPT \$(EX_LIBS)
        DELETE $shlib.SYMVEC;*
        PURGE $shlib.EXE,$shlib.OPT,$shlib.MAP
EOF
  }
  sub obj2dso {
      my %args = @_;
      my $lib = $args{lib};
      my $libd = dirname($lib);
      my $libn = basename($lib);
      (my $libn_nolib = $libn) =~ s/^lib//;
      my @objs = map { "$_.OBJ" } @{$args{objs}};
      my @deps = map {
          $disabled{shared} ? $_.".OLB"
              : $unified_info{sharednames}->{$_}.".EXE"; } @{$args{deps}};
      my $deps = join(", -\n\t\t", @objs, @deps);
      my $shlib_target = $disabled{shared} ? "" : $target{shared_target};
      my $engine_opt = abs2rel(rel2abs(catfile($config{sourcedir},
                                               "VMS", "engine.opt")),
                               rel2abs($config{builddir}));
      # The "[]" hack is because in .OPT files, each line inherits the
      # previous line's file spec as default, so if no directory spec
      # is present in the current line and the previous line has one that
      # doesn't apply, you're in for a surprise.
      my $write_opt1 =
          join(",-\"\n\t", map { my $x = $_ =~ /\[/ ? $_ : "[]".$_;
                                 "WRITE OPT_FILE \"$x" } @objs).
          "\"";
      my $write_opt2 =
          join("\n\t", map { my $x = $_ =~ /\[/ ? $_ : "[]".$_;
                             $x =~ s|(\.EXE)|$1/SHARE|;
                             $x =~ s|(\.OLB)|$1/LIB|;
                             "WRITE OPT_FILE \"$x\"" } @deps)
          || "\@ !";
      return <<"EOF";
$lib.EXE : $deps
        OPEN/WRITE/SHARE=READ OPT_FILE $lib.OPT
        TYPE $engine_opt /OUTPUT=OPT_FILE:
        $write_opt1
        $write_opt2
        CLOSE OPT_FILE
        LINK /MAP=$lib.MAP /FULL/SHARE=$lib.EXE $lib.OPT/OPT \$(EX_LIBS)
        - PURGE $lib.EXE,$lib.OPT,$lib.MAP
EOF
  }
  sub obj2lib {
      my %args = @_;
      my $lib = $args{lib};
      my $objs = join(", -\n\t\t", map { $_.".OBJ" } (@{$args{objs}}));
      my $fill_lib = join("\n\t", (map { "LIBRARY/REPLACE $lib.OLB $_.OBJ" }
                                    @{$args{objs}}));
      return <<"EOF";
$lib.OLB : $objs
        LIBRARY/CREATE/OBJECT $lib.OLB
        $fill_lib
        - PURGE $lib.OLB
EOF
  }
  sub obj2bin {
      my %args = @_;
      my $bin = $args{bin};
      my $bind = dirname($bin);
      my $binn = basename($bin);
      my @objs = map { "$_.OBJ" } @{$args{objs}};
      my @deps = map {
          $disabled{shared} ? $_.".OLB"
              : $unified_info{sharednames}->{$_}.".EXE"; } @{$args{deps}};
      my $deps = join(", -\n\t\t", @objs, @deps);
      # The "[]" hack is because in .OPT files, each line inherits the
      # previous line's file spec as default, so if no directory spec
      # is present in the current line and the previous line has one that
      # doesn't apply, you're in for a surprise.
      my $write_opt1 =
          join(",-\"\n\t", map { my $x = $_ =~ /\[/ ? $_ : "[]".$_;
                                 "WRITE OPT_FILE \"$x" } @objs).
          "\"";
      my $write_opt2 =
          join("\n\t", map { my $x = $_ =~ /\[/ ? $_ : "[]".$_;
                             $x =~ s|(\.EXE)|$1/SHARE|;
                             $x =~ s|(\.OLB)|$1/LIB|;
                             "WRITE OPT_FILE \"$x\"" } @deps)
          || "\@ !";
      return <<"EOF";
$bin.EXE : $deps
        OPEN/WRITE/SHARE=READ OPT_FILE $bin.OPT
        $write_opt1
        $write_opt2
        CLOSE OPT_FILE
        LINK/EXEC=$bin.EXE \$(LDFLAGS) $bin.OPT/OPT \$(EX_LIBS)
        - PURGE $bin.EXE,$bin.OPT
EOF
  }
  sub in2script {
      my %args = @_;
      my $script = $args{script};
      return "" if grep { $_ eq $script } @{$args{sources}}; # No overwrite!
      my $sources = join(" ", @{$args{sources}});
      my $dofile = abs2rel(rel2abs(catfile($config{sourcedir},
                                           "util", "dofile.pl")),
                           rel2abs($config{builddir}));
      return <<"EOF";
$script : $sources
        \$(PERL) "-I\$(BLDDIR)" "-Mconfigdata" $dofile -
	    "-o$target{build_file}" $sources > $script
        SET FILE/PROT=(S:RWED,O:RWED,G:RE,W:RE) $script
        PURGE $script
EOF
  }
  ""    # Important!  This becomes part of the template result.
-}
