util/bin/createpackage.pl
changeset 7 f7bc934e204c
equal deleted inserted replaced
3:41300fa6a67c 7:f7bc934e204c
       
     1 #!/usr/bin/perl
       
     2 #############################################################################
       
     3 ##
       
     4 ## Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     5 ## All rights reserved.
       
     6 ## Contact: Nokia Corporation (qt-info@nokia.com)
       
     7 ##
       
     8 ## This file is part of the S60 port of the Qt Toolkit.
       
     9 ##
       
    10 ## $QT_BEGIN_LICENSE:LGPL$
       
    11 ## No Commercial Usage
       
    12 ## This file contains pre-release code and may not be distributed.
       
    13 ## You may use this file in accordance with the terms and conditions
       
    14 ## contained in the Technology Preview License Agreement accompanying
       
    15 ## this package.
       
    16 ##
       
    17 ## GNU Lesser General Public License Usage
       
    18 ## Alternatively, this file may be used under the terms of the GNU Lesser
       
    19 ## General Public License version 2.1 as published by the Free Software
       
    20 ## Foundation and appearing in the file LICENSE.LGPL included in the
       
    21 ## packaging of this file.  Please review the following information to
       
    22 ## ensure the GNU Lesser General Public License version 2.1 requirements
       
    23 ## will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    24 ##
       
    25 ## In addition, as a special exception, Nokia gives you certain additional
       
    26 ## rights.  These rights are described in the Nokia Qt LGPL Exception
       
    27 ## version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    28 ##
       
    29 ## If you have questions regarding the use of this file, please contact
       
    30 ## Nokia at qt-info@nokia.com.
       
    31 ##
       
    32 ##
       
    33 ##
       
    34 ##
       
    35 ##
       
    36 ##
       
    37 ##
       
    38 ##
       
    39 ## $QT_END_LICENSE$
       
    40 ##
       
    41 #############################################################################
       
    42 
       
    43 ############################################################################################
       
    44 #
       
    45 # Convenience script for creating signed packages you can install on your phone.
       
    46 #
       
    47 ############################################################################################
       
    48 
       
    49 use strict;
       
    50 
       
    51 # use a command-line parsing module
       
    52 use Getopt::Long;
       
    53 # Use file name parsing module
       
    54 use File::Basename;
       
    55 # Use File::Spec services mainly rel2abs
       
    56 use File::Spec;
       
    57 # Use File::Path - to make stub sis target directory
       
    58 use File::Path;
       
    59 # use CWD abs_bath, which is exported only on request
       
    60 use Cwd 'abs_path';
       
    61 
       
    62 
       
    63 sub Usage() {
       
    64     print <<ENDUSAGESTRING;
       
    65 
       
    66 ==============================================================================================
       
    67 Convenience script for creating signed packages you can install on your phone.
       
    68 
       
    69 Usage: createpackage.pl [options] templatepkg [target]-[platform] [certificate key [passphrase]]
       
    70 
       
    71 Where supported optiobns are as follows:
       
    72      [-i|install]            = Install the package right away using PC suite
       
    73      [-p|preprocess]         = Only preprocess the template .pkg file.
       
    74      [-c|certfile=<file>]    = The file containing certificate information for signing.
       
    75                                The file can have several certificates, each specified in
       
    76                                separate line. The certificate, key and passphrase in line
       
    77                                must be ';' separated. Lines starting with '#' are treated
       
    78                                as a comments. Also empty lines are ignored. The paths in
       
    79                                <file> can be absolute or relative to <file>.
       
    80      [-u|unsigned]           = Preserves the unsigned package
       
    81 Where parameters are as follows:
       
    82      templatepkg             = Name of .pkg file template
       
    83      target                  = Either debug or release
       
    84      platform                = One of the supported platform
       
    85                                winscw | gcce | armv5 | armv6 | armv7
       
    86      certificate             = The certificate file used for signing
       
    87      key                     = The certificate's private key file
       
    88      passphrase              = The certificate's private key file's passphrase
       
    89 
       
    90 Example:
       
    91      createpackage.pl fluidlauncher_template.pkg release-armv5
       
    92 
       
    93 Example with certfile:
       
    94      createpackage.pl -c=mycerts.txt fluidlauncher_template.pkg release-armv5
       
    95 
       
    96      Content of 'mycerts.txt' must be something like this:
       
    97         # This is comment line, also the empty lines are ignored
       
    98         rd.cer;rd-key.pem
       
    99         .\\cert\\mycert.cer;.\\cert\\mykey.key;yourpassword
       
   100         X:\\QtS60\\s60installs\\selfsigned.cer;X:\\QtS60\\s60installs\\selfsigned.key
       
   101 
       
   102 If no certificate and key files are provided, either a RnD certificate or
       
   103 a self-signed certificate from QtDir\\src\\s60installs directory is used.
       
   104 ==============================================================================================
       
   105 
       
   106 ENDUSAGESTRING
       
   107 
       
   108     exit();
       
   109 }
       
   110 
       
   111 # Read given options
       
   112 my $install = "";
       
   113 my $preprocessonly = "";
       
   114 my $certfile = "";
       
   115 my $preserveUnsigned = "";
       
   116 my $stub = "";
       
   117 
       
   118 unless (GetOptions('i|install' => \$install,
       
   119                    'p|preprocess' => \$preprocessonly,
       
   120                    'c|certfile=s' => \$certfile,
       
   121                    'u|unsigned' => \$preserveUnsigned,
       
   122                    's|stub' => \$stub,)){
       
   123     Usage();
       
   124 }
       
   125 
       
   126 my $certfilepath = abs_path(dirname($certfile));
       
   127 
       
   128 # Read params to variables
       
   129 my $templatepkg = $ARGV[0];
       
   130 my $targetplatform = lc $ARGV[1];
       
   131 
       
   132 my @tmpvalues = split('-', $targetplatform);
       
   133 my $target = $tmpvalues[0];
       
   134 my $platform = $tmpvalues[1];;
       
   135 
       
   136 # Convert visual target to real target (debug->udeb and release->urel)
       
   137 $target =~ s/debug/udeb/i;
       
   138 $target =~ s/release/urel/i;
       
   139 
       
   140 my $certificate = $ARGV[2];
       
   141 my $key = $ARGV[3];
       
   142 my $passphrase = $ARGV[4];
       
   143 
       
   144 # Generate output pkg basename (i.e. file name without extension)
       
   145 my $pkgoutputbasename = $templatepkg;
       
   146 my $preservePkgOutput = "";
       
   147 $pkgoutputbasename =~ s/_template/_$targetplatform/g;
       
   148 if ($pkgoutputbasename eq $templatepkg) {
       
   149     $preservePkgOutput = "1";
       
   150 }
       
   151 $pkgoutputbasename =~ s/\.pkg//g;
       
   152 $pkgoutputbasename = lc($pkgoutputbasename);
       
   153 
       
   154 # Store output file names to variables
       
   155 my $pkgoutput = lc($pkgoutputbasename.".pkg");
       
   156 my $sisoutputbasename = lc($pkgoutputbasename);
       
   157 $sisoutputbasename =~ s/_$targetplatform//g;
       
   158 my $unsigned_sis_name = $sisoutputbasename."_unsigned.sis";
       
   159 my $signed_sis_name = $sisoutputbasename.".sis";
       
   160 my $stub_sis_name = $sisoutputbasename."_stub.sis";
       
   161 
       
   162 # Store some utility variables
       
   163 my $scriptpath = dirname(__FILE__);
       
   164 my $certtext = $certificate;
       
   165 my $certpath = $scriptpath;
       
   166 $certpath =~ s-^(.*[^\\])$-$1\\-o;          # ensure path ends with a backslash
       
   167 $certpath =~ s-/-\\-go;                     # for those working with UNIX shells
       
   168 $certpath =~ s-bin\\$-src\\s60installs\\-;  # certificates are one step up in hierarcy
       
   169 
       
   170 # Check some pre-conditions and print error messages if needed.
       
   171 unless (length($templatepkg)) {
       
   172     print "\nError: Template PKG filename is not defined!\n";
       
   173     Usage();
       
   174 }
       
   175 
       
   176 # If the pkg file is not actually a template, there is no need for plaform or target.
       
   177 if ($templatepkg =~ m/_template\.pkg/i) {
       
   178     unless (length($platform) && length($target)) {
       
   179         print "\nError: Platform or target is not defined!\n";
       
   180         Usage();
       
   181     }
       
   182 }
       
   183 
       
   184 # Check template exist
       
   185 stat($templatepkg);
       
   186 unless( -e _ ) {
       
   187     print "\nError: Package description file '$templatepkg' does not exist!\n";
       
   188     Usage();
       
   189 }
       
   190 
       
   191 # Check certifcate preconditions and set default certificate variables if needed
       
   192 if (length($certificate)) {
       
   193     unless(length($key)) {
       
   194         print "\nError: Custom certificate key file parameter missing.!\n";
       
   195         Usage();
       
   196     }
       
   197 } else {
       
   198     #If no certificate is given, check default options
       
   199     $certtext = "RnD";
       
   200     $certificate = $certpath."rd.cer";
       
   201     $key = $certpath."rd-key.pem";
       
   202 
       
   203     stat($certificate);
       
   204     unless( -e _ ) {
       
   205         $certtext = "Self Signed";
       
   206         $certificate = $certpath."selfsigned.cer";
       
   207         $key = $certpath."selfsigned.key";
       
   208     }
       
   209 }
       
   210 
       
   211 # Read the certificates from file to two dimensional array
       
   212 my @certificates;
       
   213 if (length($certfile)) {
       
   214     open CERTFILE, "<$certfile" or die $!;
       
   215     while(<CERTFILE>){
       
   216         s/#.*//;                            # ignore comments by erasing them
       
   217         next if /^(\s)*$/;                  # skip blank lines
       
   218         chomp;                              # remove trailing newline characters
       
   219         my @certinfo = split(';', $_);      # split row to certinfo
       
   220 
       
   221         # Trim spaces
       
   222         for(@certinfo) {
       
   223             s/^\s+//;
       
   224             s/\s+$//;
       
   225         }
       
   226 
       
   227         # Do some validation
       
   228         unless(scalar(@certinfo) >= 2 && scalar(@certinfo) <= 3 && length($certinfo[0]) && length($certinfo[1]) ) {
       
   229             print "\nError: $certfile line '$_' does not contain valid information!\n";
       
   230             Usage();
       
   231         }
       
   232 
       
   233         push @certificates, [@certinfo];    # push data to two dimensional array
       
   234     }
       
   235 }
       
   236 
       
   237 # Remove any existing .sis packages
       
   238 unlink $unsigned_sis_name;
       
   239 unlink $signed_sis_name;
       
   240 if (!$preservePkgOutput) {
       
   241     unlink $pkgoutput;
       
   242 }
       
   243 
       
   244 # Preprocess PKG
       
   245 local $/;
       
   246 # read template file
       
   247 open( TEMPLATE, $templatepkg) or die "Error '$templatepkg': $!\n";
       
   248 $_=<TEMPLATE>;
       
   249 close (TEMPLATE);
       
   250 
       
   251 # replace the PKG variables
       
   252 s/\$\(PLATFORM\)/$platform/gm;
       
   253 s/\$\(TARGET\)/$target/gm;
       
   254 
       
   255 #write the output
       
   256 open( OUTPUT, ">$pkgoutput" ) or die "Error '$pkgoutput' $!\n";
       
   257 print OUTPUT $_;
       
   258 close OUTPUT;
       
   259 
       
   260 if ($preprocessonly) {
       
   261     exit;
       
   262 }
       
   263 
       
   264 if($stub) {
       
   265     if(!($ENV{EPOCROOT})) { die("EPOCROOT must be set to create stub sis files"); }
       
   266     my $systeminstall = "$ENV{EPOCROOT}epoc32/data/z/system/install";
       
   267     mkpath($systeminstall);
       
   268     my $stub_sis_name = $systeminstall."/".$stub_sis_name;
       
   269     # Create stub SIS.
       
   270     system ("makesis -s $pkgoutput $stub_sis_name");
       
   271 } else {
       
   272     # Create SIS.
       
   273     system ("makesis $pkgoutput $unsigned_sis_name");
       
   274     print("\n");
       
   275 
       
   276     # Sign SIS with certificate info given as an argument.
       
   277     system ("signsis $unsigned_sis_name $signed_sis_name $certificate $key $passphrase");
       
   278 
       
   279     # Check if creating signed SIS Succeeded
       
   280     stat($signed_sis_name);
       
   281     if( -e _ ) {
       
   282         my $targetInsert = "";
       
   283         if ($targetplatform ne "-") {
       
   284             $targetInsert = "for $targetplatform ";
       
   285         }
       
   286         print ("Successfully created $signed_sis_name ${targetInsert}using certificate: $certtext!\n");
       
   287 
       
   288         # Sign with additional certificates & keys
       
   289         for my $row ( @certificates ) {
       
   290             # Get certificate absolute file names, relative paths are relative to certfilepath
       
   291             my $abscert = File::Spec->rel2abs( $row->[0], $certfilepath);
       
   292             my $abskey = File::Spec->rel2abs( $row->[1], $certfilepath);
       
   293 
       
   294             system ("signsis $signed_sis_name $signed_sis_name $abscert $abskey $row->[2]");
       
   295             print ("\tAdditionally signed the SIS with certificate: $row->[0]!\n");
       
   296         }
       
   297 
       
   298         # remove temporary pkg and unsigned sis
       
   299         if (!$preservePkgOutput) {
       
   300             unlink $pkgoutput;
       
   301         }
       
   302         if (!$preserveUnsigned) {
       
   303             unlink $unsigned_sis_name;
       
   304         }
       
   305 
       
   306         # Install the sis if requested
       
   307         if ($install) {
       
   308             print ("\nInstalling $signed_sis_name...\n");
       
   309             system ("$signed_sis_name");
       
   310         }
       
   311     } else {
       
   312         # Lets leave the generated PKG for problem solving purposes
       
   313         print ("\nSIS creation failed!\n");
       
   314     }
       
   315 }
       
   316 
       
   317 #end of file