Subversion Repositories DevTools

Rev

Rev 4941 | Blame | Compare with Previous | Last modification | View Log | RSS feed

########################################################################
# Copyright (C) 1998-2012 Vix Technology, All rights reserved
#
# Module name   : MakeNsisDefs.pl
# Module type   : Makefile system
# Compiler(s)   : Perl
# Environment(s): jats
#
# Description   : Create JATS specific definitions for use within NSIS
#
# Usage         : See GetOptions
#
#......................................................................#

require 5.008_002;
use strict;
use warnings;

use Pod::Usage;
use Getopt::Long;

use JatsError;
use ReadBuildConfig qw(:All);
use JatsMakeInfo;
use ConfigurationFile;
use FileUtils;
use JatsVersionUtils;
use JatsEnv;

#
#   Global variables
#
my  $VERSION = "1.0.0";                     # Update this
our $GBE_MAKE_TARGET;
my $package_name = 'nsis_packager';
my $CurrentYear = 1900 + (localtime())[5];

################################################################################
#   The following strings have been obtained from Simon Davey in an email
#   to David Purdie dated Monday, 16 January 2012 3:40 PM
#
my $CompanyName = 'Vix IP Pty Ltd';
my $Copyright = $CompanyName . ' ' . $CurrentYear;
my $Trademark = $CompanyName;
#
################################################################################

#
#   Global variables - Options
#
my $opt_help = 0;
my $opt_verbose = 0;
my $opt_ofile;
my $opt_installer;
my $opt_package;
my $opt_plain;
my $opt_pkgArch;

#-------------------------------------------------------------------------------
# Function        : Main entry point
#
# Description     : Parse user arguments
#                   Generate metrics
#
# Inputs          : None
#
# Returns         : Error code
#

my $result = GetOptions (
                "help:+"        => \$opt_help,              # flag, multiple use allowed
                "manual:3"      => \$opt_help,              # flag
                "verbose:+"     => \$opt_verbose,           # flag, multiple use allowed
                "outfile:s"     => \$opt_ofile,             # String
                "installer:s"   => \$opt_installer,         # String
                "package:s"     => \$opt_package,           # String
                "pkgArch:s"     => \$opt_pkgArch,           # String
                "plain:+"       => \$opt_plain,             # flag
                );

                #
                #   UPDATE THE DOCUMENTATION AT THE END OF THIS FILE !!!
                #

#
#   Process help and manual options
#
pod2usage(-verbose => 0, -message => "Version: $VERSION") if ($opt_help == 1 || ! $result);
pod2usage(-verbose => 1)  if ( $opt_help == 2 );
pod2usage(-verbose => 2)  if ( $opt_help > 2 );

ErrorConfig( 'name'    =>'MakeNsisDefs',
             'verbose' => $opt_verbose,
            );

#
#   Sanity check
#
Error ('No output file specified') unless ( $opt_ofile );
Error ('No Installer file specified') unless ( $opt_installer );

#
#   Init components
#
InitFileUtils();
EnvImport ('GBE_MAKE_TARGET');

#
#   Locate the base of the JATS build
#   This will be provided by Makefile.gbe
#
Verbose ("Locate project root directory");
ReadMakeInfo();
my $interface_path = "$::ScmRoot/$::ScmInterface";

#
#   Read in the global build variables
#   Allow package name to be manually set
#
ReadBuildConfig( $interface_path );
$ScmBuildPackage = $opt_package if ( $opt_package );

##
##   Show what we have got
##
#DebugDumpData("InterfaceVersion",       $InterfaceVersion);
#DebugDumpData("ScmBuildMachType",       $ScmBuildMachType);
#DebugDumpData("ScmInterfaceVersion",    $ScmInterfaceVersion);
#DebugDumpData("ScmBuildName",           $ScmBuildName);
#DebugDumpData("ScmBuildPackage",        $ScmBuildPackage);
#DebugDumpData("ScmBuildVersion",        $ScmBuildVersion);
#DebugDumpData("ScmBuildProject",        $ScmBuildProject);
#DebugDumpData("ScmBuildVersionFull",    $ScmBuildVersionFull);
#DebugDumpData("ScmBuildPreviousVersion",$ScmBuildPreviousVersion);
#DebugDumpData("ScmSrcDir",              $ScmSrcDir);
#DebugDumpData("ScmLocal",               $ScmLocal);
#DebugDumpData("ScmDeploymentPatch",     $ScmDeploymentPatch);
#DebugDumpData("ScmBuildSrc",            $ScmBuildSrc);
#DebugDumpData("ScmExpert",              $ScmExpert);
#DebugDumpData("ScmAll",                 $ScmAll);
#DebugDumpData("ScmBuildAliases",        \%ScmBuildAliases);
#DebugDumpData("ScmBuildProducts",       \%ScmBuildProducts);
#DebugDumpData("ScmBuildPlatforms",      \%ScmBuildPlatforms);
#DebugDumpData("ScmBuildPkgRules",       \%ScmBuildPkgRules);
#DebugDumpData("BUILDPLATFORMS",         \@BUILDPLATFORMS);
#DebugDumpData("DEFBUILDPLATFORMS",      \@DEFBUILDPLATFORMS);
#DebugDumpData("BUILDTOOLSPATH",         \@BUILDTOOLSPATH);
#DebugDumpData("BUILDPLATFORM_PARTS",    \%BUILDPLATFORM_PARTS);
#DebugDumpData("BUILDINFO",              \%BUILDINFO);
#DebugDumpData("BUILD_KNOWNFILES",       \%BUILD_KNOWNFILES);

#
#   Create the required file and insert data
#
my $fh = ConfigurationFile::New( $opt_ofile, '--Type=NSIS' );
$fh->HeaderSimple( 'MakeNsisDefs',
                   'NSIS Version Definitions' );

writeCmt( $fh, 'Company Information');
writeDef ($fh, 'GBE_COMPANY', $CompanyName);

writeCmt( $fh, 'Build Package Information');
writeDef ($fh, 'GBE_BUILDNAME', $ScmBuildName );
writeDef ($fh, 'GBE_PACKAGE',   $ScmBuildPackage );
writeDef ($fh, 'GBE_VERSION',   $ScmBuildVersion );
writeDef ($fh, 'GBE_VERSION_FULL', $ScmBuildVersionFull );
writeDef ($fh, 'GBE_PROJECT',   $ScmBuildProject );
writeDef ($fh, 'GBE_BUILDDATE', my $ltime = localtime() );
writeDef ($fh, 'GBE_BUILDTIME', strip_zeros(time()) );
writeDef ($fh, 'GBE_PKGARCH', $opt_pkgArch );

writeCmt( $fh, 'Build Version Information');
my ($major, $minor, $patch, $build, $raw_patch) = SplitVersion($ScmBuildVersion);
$build = strip_zeros( $build );
writeDef ($fh, 'GBE_MAJOR', $major );
writeDef ($fh, 'GBE_MINOR', $minor );
writeDef ($fh, 'GBE_PATCH', $patch );
writeDef ($fh, 'GBE_BUILD', $build );

unless ( $opt_plain )
{
    writeCmt( $fh, 'Versioning information for the generated EXE',
                'User MUST provide:',
                '     FileDescription',
                'User CAN provide:',
                '     ProductName',
                '     Comments',
                '     Other Random Fields');
    writeCmd( $fh,  'VIProductVersion', "$major.$minor.$patch.$build");
    writeCmd( $fh,  'VIAddVersionKey',  'CompanyName', $CompanyName);
    writeCmd( $fh,  '#VIAddVersionKey',  'FileDescription', 'Describe the Installer');
    writeCmd( $fh,  'VIAddVersionKey',  'LegalTrademarks', $Trademark);
    writeCmd( $fh,  'VIAddVersionKey',  'LegalCopyright', $Copyright);
    writeCmd( $fh,  'VIAddVersionKey',  'FileVersion', "$major.$minor.$patch.$build");
}

#
#   Provide path information to important parts of the build system
#
writeCmt( $fh, 'Paths to build directories');
writeDef( $fh, 'GBE_ROOT', WindowsPath($ScmRoot) );
writeDef( $fh, 'GBE_INTERFACE', WindowsPath($interface_path) );
writeDef( $fh, 'GBE_LOCAL', WindowsPath("$ScmRoot/$ScmLocal") );

#
#   Some are added at runtime
#
writeCmt( $fh, 'Added at runtime',
               '!define GBE_BINDIR xxx',
               '!define GBE_OBJDIR xxx',
               '!define GBE_LIBDIR xxx',
               '!define GBE_TARGET xxx',
               '!define GBE_TYPE xxx',
               );

#
#   For each link package that is present provide
#       package path
#       package version
#
my $my_entry;
writeCmt( $fh, 'Paths to external dependent packages');
foreach my $entry (@{$ScmBuildPkgRules{$GBE_MAKE_TARGET} })
{
    $my_entry = $entry if ( $entry->{'NAME'} eq $package_name );
    my @comments;
    push (@comments , "Package: $entry->{'NAME'}");
    push (@comments , "Package included via BuildPkgArchive") unless ( $entry->{'TYPE'} eq 'link' );
    writeCmt( $fh, @comments);
    writeDef( $fh, 'GBE_PKG_PATH_' . $entry->{'NAME'}, WindowsPath($entry->{'ROOT'}) ) if ( $entry->{'TYPE'} eq 'link' );
    writeDef( $fh, 'GBE_PKG_VERSION_' . $entry->{'NAME'}, $entry->{'DVERSION'} );
}

#
#   Define the base of the data files provided with this package
#
writeCmt( $fh, "NSIS_PACKAGER data files");
if ( $my_entry )
{
    my $path = ($my_entry->{'TYPE'} eq 'link' ) ? $my_entry->{'ROOT'} : $interface_path;
    writeDef( $fh, 'GBE_NSISDATA', WindowsPath("$path/etc") );
}
else
{
    Error ("Internal: Cannot find myself in referenced packages",
           "Perhaps the package name has been changed",
           "Looking for: $package_name");
}

#
#   Determine if there are any suitable NSIS plugin dirs
#   Scan interface and packages
#
my @nsis_tools;
my @nsis_plugins;
foreach my $entry (@{$ScmBuildPkgRules{$GBE_MAKE_TARGET} })
{
    my $tdir = $entry->{'ROOT'} . '/tools/scripts/nsis';
    push @nsis_tools, $tdir if ( -d $tdir );

    my $pdir = $entry->{'ROOT'} . '/tools/bin/win32/nsis';
    push @nsis_plugins, $pdir if ( -d $pdir );
}
writeCmt( $fh, "Extend NSIS Include Search Path");
writeCmd( $fh, '!addincludedir', WindowsPath($_) ) foreach @nsis_tools;
writeCmd( $fh, '!addplugindir', WindowsPath($_) ) foreach @nsis_plugins;

#
#   Specify the output file
#   This will be written directly into the output package
#
writeCmt( $fh, 'Set output file');
writeCmd( $fh,'OutFile', WindowsPath("\${GBE_BINDIR}/$opt_installer") );

unless ( $opt_plain )
{
    writeCmt( $fh, 'Name, Branding and Caption');
    writeCmd( $fh, 'Name', "$ScmBuildPackage ($ScmBuildVersionFull)" );
    writeCmd( $fh, 'BrandingText', $CompanyName );
    writeCmd( $fh, 'Caption', $ScmBuildPackage );
}

$fh->Write( "\n");
$fh->Close();

#-------------------------------------------------------------------------------
# Function        : writeDef
#
# Description     : Write a Text definition to the output file
#
# Inputs          : $fh         - File Handle
#                   $name       - Name of variable
#                   $text       - Text
#
# Returns         : 
#
sub writeDef
{
    my ($fh, $name, $text ) = @_;
    $fh->WriteLn( "!define $name \"$text\"" );
}

#-------------------------------------------------------------------------------
# Function        : writeCmd
#
# Description     : Write a Cmd to the output file
#
# Inputs          : $fh         - File Handle
#                   $cmd        - Name of command
#                   @text       - text items to quote
#
# Returns         : 
#
sub writeCmd
{
    my ($fh, $cmd, @text ) = @_;
    $fh->Write( $cmd );
    $fh->Write( " \"" . $_ . "\"" ) foreach ( @text );
    $fh->Write( "\n" );
}

#-------------------------------------------------------------------------------
# Function        : writeCmt
#
# Description     : Write a writeCmt block
#
# Inputs          : $fh         - File Handle
#                   @text       - Lines to output in a writeCmt
#
# Returns         : 
#
sub writeCmt
{
    my ($fh, @text ) = @_;

    $fh->Write( "\n");
    $fh->Comment( "\n");
    $fh->Comment( $_ . "\n") foreach ( @text );
    $fh->Comment( "\n");
}


#-------------------------------------------------------------------------------
# Function        : stip_zeros
#
# Description     : Remove leading 0's from a string
#
# Inputs          : A string
#
# Returns         : A string without leading zeros
#
sub strip_zeros
{
    my ($text) = @_;

    $text =~ s~^0*~~;
    $text = '0' unless ( $text );
    return $text;
}

#-------------------------------------------------------------------------------
# Function        : WindowsPath
#
# Description     : Convert a path into one that is suitable for Windows
#                       Must be absolue
#                       Must have \
#
# Inputs          : $path       - Path to clean
#
# Returns         : Cleaned path
#
sub WindowsPath
{
    my ($path) = @_;
    $path = FullPath($path) unless ($path =~ m~^\$~);
    $path =~ tr~/~\\~s;
    return $path;
}