commsfwtools/preparedefaultcommsdatabase/defaultcommdb/group/createcommdbs.pl
changeset 0 dfb7c4ff071f
child 12 8b5d60ce1e94
equal deleted inserted replaced
-1:000000000000 0:dfb7c4ff071f
       
     1 # Copyright (c) 1999-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 # All rights reserved.
       
     3 # This component and the accompanying materials are made available
       
     4 # under the terms of "Eclipse Public License v1.0"
       
     5 # which accompanies this distribution, and is available
       
     6 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 #
       
     8 # Initial Contributors:
       
     9 # Nokia Corporation - initial contribution.
       
    10 #
       
    11 # Contributors:
       
    12 #
       
    13 # Description:
       
    14 #
       
    15 
       
    16 #!perl
       
    17 
       
    18 use strict;
       
    19 use File::Copy;
       
    20 use File::Path;
       
    21 use File::stat;
       
    22 use Getopt::Long;
       
    23 use Cwd;
       
    24 use FindBin;
       
    25 
       
    26 
       
    27 if ($^O =~ /MSWin32/)
       
    28 {
       
    29   require Win32::Mutex;
       
    30   require Win32::File;
       
    31 }
       
    32 
       
    33 
       
    34 $::emulatorCfg = 'emulator.cfg';
       
    35 $::targetCfg = 'target.cfg';
       
    36 
       
    37 GetOptions (
       
    38            'command=s' => \$::command,
       
    39            'platform=s' => \$::platform,
       
    40            'variant=s' => \$::variant,
       
    41            'targetcfg=s' => \$::targetCfg,
       
    42            'emulatorcfg=s' => \$::emulatorCfg,
       
    43            'sourceDir=s' => \$::sourceDir,
       
    44            'platsec!' => \$::platsec);
       
    45 
       
    46 #
       
    47 # Constants.
       
    48 #
       
    49 
       
    50 my $epocRoot = GetEpocRoot();
       
    51 my $EpocWinsStore = 'c:\defaultcommdb';
       
    52 my $emptyDatabase = 'empty.txt';
       
    53 
       
    54 my $emulatorstore = $epocRoot . 'epoc32/winscw/c/defaultcommdb';
       
    55 
       
    56 my $release = $epocRoot . 'epoc32/release/';
       
    57 my $winscedUREL = $release . 'wins/urel/ced.exe';
       
    58 my $winscwcedUREL = $release . 'winscw/urel/ced.exe';
       
    59 my $winscedUDEB = $release . 'wins/udeb/ced.exe';
       
    60 my $winscwcedUDEB = $release . 'winscw/udeb/ced.exe';
       
    61 
       
    62 # Linux static copies
       
    63 my $bindir = $FindBin::Bin.'/../bin/';
       
    64 my @bincopies=(
       
    65 	# plat       var     source                  destination
       
    66 	['emulator', 'both', $bindir.'commsdb.dat',  $emulatorstore.'/commsdb.dat'],
       
    67 	['emulator', 'udeb', $bindir.'cccccc00.cre', $epocRoot.'epoc32/release/winscw/udeb/z/private/10202be9/cccccc00.cre'],
       
    68 	['emulator', 'urel', $bindir.'cccccc00.cre', $epocRoot.'epoc32/release/winscw/urel/z/private/10202be9/cccccc00.cre'],
       
    69 	['target',   'both', $bindir.'cccccc00.cre', $epocRoot.'epoc32/data/z/private/10202be9/cccccc00.cre']
       
    70 	);
       
    71 
       
    72 #
       
    73 # Main.
       
    74 #
       
    75 unless ($::command and $::platform and $::variant and (scalar(@ARGV) == 0)) 
       
    76 {
       
    77 	die "CreateCommdbs Error: Invalid arguments\n";
       
    78 }
       
    79 
       
    80 my $platType;
       
    81 if ($::platform =~ /^(wins|wincw)/i) 
       
    82 {
       
    83 	$platType = 'emulator';
       
    84 }
       
    85 else 
       
    86 {
       
    87 	$platType = 'target';
       
    88 }
       
    89 
       
    90 #
       
    91 # Check OS, and perform copies of key binaries from source if dynamic generation in unsupported
       
    92 #
       
    93 
       
    94 if (!($^O =~ /MSWin32/))
       
    95 	{
       
    96 	foreach my $bincopy (@bincopies)
       
    97 		{
       
    98 		my ($copyplat, $copyvar, $source, $destination) = @$bincopy;
       
    99 
       
   100 		if (($copyplat eq $platType) and (($copyvar eq lc($::variant)) or ($copyvar eq "both")))
       
   101 			{
       
   102 			if (($::command eq "build") and not (-e $destination))
       
   103 				{
       
   104 				# Existing files are considered to be current
       
   105 				print ("Warning: createcommdbs.pl: \'$destination\' generated by a copy from source as building is not supported on this platform.\n");
       
   106 				print ("Copy \'$source\' to \'$destination\'\n");
       
   107 				CopyFile($source, $destination);
       
   108 				}
       
   109 			elsif (($::command eq "clean") and (-e $destination))
       
   110 				{
       
   111 				unlink ($destination)
       
   112 				}
       
   113 			elsif ($::command eq "releasables")
       
   114 				{
       
   115 				print ("$destination\n");
       
   116 				}
       
   117 			}
       
   118 		}
       
   119 	exit;
       
   120 	}
       
   121 
       
   122 my $cccccc00Root;
       
   123 my $cccccc00NameCre;
       
   124 my $cccccc00Nametxt;
       
   125 
       
   126 $cccccc00Root = "/private/10202be9/";
       
   127 $cccccc00NameCre = "cccccc00.cre";
       
   128 $cccccc00Nametxt = "cccccc00.txt";
       
   129 
       
   130 my $data = 
       
   131 {
       
   132 	target => 
       
   133 	{
       
   134 		sourceCfg => "$::sourceDir/$::targetCfg",
       
   135 		cedCfg => "$EpocWinsStore\\$::targetCfg",
       
   136 		emptyDb => "$::sourceDir/$emptyDatabase",
       
   137 		destinationCdb => $emulatorstore . '/CommsDb.dat',
       
   138 	},
       
   139 	emulator => 
       
   140 	{
       
   141 		sourceCfg => "$::sourceDir\\$::emulatorCfg",
       
   142 		cedCfg => "$EpocWinsStore\\$::emulatorCfg",
       
   143 		emptyDb => "$::sourceDir\\$emptyDatabase",
       
   144 		destinationCdb => $emulatorstore . '/CommsDb.dat',
       
   145 	}
       
   146 };
       
   147 
       
   148 my $ced;
       
   149 my $cedPlatfrom;
       
   150 my $cedVariant;
       
   151 my $emulatorstore;
       
   152 my $CopiedCfg;
       
   153 my $CedLog;
       
   154 my $winsCdb;
       
   155 my $CdbEmpty;
       
   156 my $romCdb;
       
   157 my $attrib;
       
   158 my $cdbIn;
       
   159 my $cdbOut;
       
   160 my $cdbDatPath;
       
   161 my $cdbDatFile;
       
   162 my $cdbDatExt;
       
   163 my $zDrive;
       
   164 my $zDriveCre;
       
   165 
       
   166 #
       
   167 # Establish location of ced.exe
       
   168 #
       
   169 if ($platType =~ 'emulator')
       
   170 {
       
   171 	$cedPlatfrom = $::platform;
       
   172 	$cedVariant = $::variant;
       
   173 }
       
   174 elsif (($platType =~ 'target') && (-e $winscwcedUREL))
       
   175 {
       
   176 	$cedPlatfrom = 'WINSCW';
       
   177 	$cedVariant = 'UREL';
       
   178 }
       
   179 elsif (($platType =~ 'target') && (-e $winscwcedUDEB))
       
   180 {
       
   181 	$cedPlatfrom = 'WINSCW';
       
   182 	$cedVariant = 'UDEB';
       
   183 }
       
   184 elsif (($platType =~ 'target') && (-e $winscedUREL))
       
   185 {
       
   186 	$cedPlatfrom = 'WINS';
       
   187 	$cedVariant = 'UREL';
       
   188 }
       
   189 elsif (($platType =~ 'target') && (-e $winscedUDEB))
       
   190 {
       
   191 	$cedPlatfrom = 'WINS';
       
   192 	$cedVariant = 'UDEB';
       
   193 }
       
   194 
       
   195 $cdbIn = '/c/defaultcommdb/';
       
   196 $cdbOut = '/c/defaultcommdb/';
       
   197 
       
   198 $CopiedCfg = $epocRoot . 'epoc32/' . $cedPlatfrom . $cdbIn . $platType . '.cfg';
       
   199 $CedLog = $CopiedCfg . '.log';
       
   200 
       
   201 if  ($ENV{DEFAULTCOMMSDATDATAFILE})
       
   202 {
       
   203 	($cdbDatPath, $cdbDatFile, $cdbDatExt) = SplitFileName($ENV{DEFAULTCOMMSDATDATAFILE});
       
   204 }
       
   205 else
       
   206 {
       
   207 	$cdbDatPath = $epocRoot. 'epoc32/' . $cedPlatfrom . $cdbOut;
       
   208 	$cdbDatFile = 'CommsDb';
       
   209 	$cdbDatExt = '.dat';
       
   210 }
       
   211 
       
   212 $zDrive = $release . $cedPlatfrom . '/' . $cedVariant . '/z';
       
   213 $zDriveCre = $zDrive . $cccccc00Root . $cccccc00NameCre;
       
   214 $CdbEmpty = $zDrive . $cccccc00Root . $cccccc00Nametxt;
       
   215 
       
   216 if ($::command eq 'build') 
       
   217 {
       
   218 	$ced = $release . $cedPlatfrom . '/' . $cedVariant . '/ced.exe';
       
   219 	$emulatorstore = $epocRoot . 'epoc32/' . $cedPlatfrom . $cdbIn;
       
   220 	$winsCdb = $epocRoot . 'epoc32/' . $cedPlatfrom . '/c' . $cccccc00Root . "persists/" . $cccccc00NameCre;
       
   221 	$romCdb = $epocRoot . 'epoc32/' . 'data/z' . $cccccc00Root . $cccccc00NameCre; 
       
   222 	
       
   223 	if ($platType =~ 'emulator')
       
   224 	{
       
   225 		$CopiedCfg = $emulatorstore . 'emulator.cfg';
       
   226 	}
       
   227 	else
       
   228 	{
       
   229 		$CopiedCfg = $emulatorstore . 'target.cfg';
       
   230 	}
       
   231 	
       
   232 	PreBuild();
       
   233 }
       
   234 elsif ($::command eq 'clean')
       
   235 {
       
   236 	Clean();
       
   237 }
       
   238 elsif ($::command eq 'releasables') 
       
   239 {
       
   240 	Releasables();
       
   241 }
       
   242 
       
   243 #
       
   244 # Subs.
       
   245 #
       
   246 
       
   247 sub PreBuild
       
   248 {
       
   249 	if ($^O =~ /MSWin32/)
       
   250 	{
       
   251 		# We cannot have more than one instance of this script's activites running at the same time on Windows, so
       
   252 		# we safeguarded everything with a Windows mutex.  Mutex name needs to be unique for every build,
       
   253 		# so we add the current working directory and EPOCROOT to the mutex name
       
   254 
       
   255 		my $drive = Win32::GetCwd();
       
   256 		$drive =~ s/^(\D:).*/$1/;
       
   257 		my $mutexname = $drive.lc($ENV{'EPOCROOT'});
       
   258 		$mutexname =~ s/[:\\\/]+/_/g;
       
   259 
       
   260 		my $mutex = Win32::Mutex->new(1,"CEDMUTEX_".$mutexname);
       
   261 		die "Unable to create mutex, createcommdbs.pl not run" if not $mutex;
       
   262 		if ($mutex->wait())
       
   263 		{
       
   264 			Build();
       
   265 			$mutex->release();
       
   266 		}
       
   267 		else
       
   268 		{
       
   269 			die "Unable to access mutex, createcommdbs.pl not run";
       
   270 		}
       
   271 	}
       
   272 	else
       
   273 	{
       
   274 		Build();
       
   275 	}
       
   276 }
       
   277 
       
   278 sub Build
       
   279 {
       
   280 	print("\n**Constructing $::platform $::variant database**\n");
       
   281 	print("**$winsCdb\n\n");
       
   282 	# delete old log file if it exists
       
   283 	DeleteFile($CedLog);
       
   284 
       
   285 	#Check for empty cenrep initialisation  .txt file and store if missing
       
   286 	if (!(-e $CdbEmpty))
       
   287 	{
       
   288 		print("\nCopying $data->{$platType}->{emptyDb} to $CdbEmpty\n");
       
   289 		CopyFile($data->{$platType}->{emptyDb}, $CdbEmpty);
       
   290 		print("Remove ReadOnly attribute! from $CdbEmpty \n\n");
       
   291 		if ($^O =~ /MSWin32/)
       
   292 		{
       
   293 			no strict;  # Not nice this, but avoids compilation errors on Linux
       
   294 			Win32::File::SetAttributes($CdbEmpty, NORMAL) || die $!;
       
   295 		}
       
   296 	}
       
   297 
       
   298 	# Ensure CFG file exists
       
   299 	if (-e $data->{$platType}->{sourceCfg})
       
   300 	{
       
   301 		print("Copying $data->{$platType}->{sourceCfg} ... \n");
       
   302 		CopyIfYounger($data->{$platType}->{sourceCfg}, $CopiedCfg);
       
   303 		print("copied successfully to $CopiedCfg\n\n");
       
   304 
       
   305 		# Ensure replacement file is more up-to-date than existing file
       
   306 		if (FileIsYounger($CopiedCfg, $data->{$platType}->{destinationCdb}))
       
   307 		{
       
   308 			print("$CopiedCfg newer than $data->{$platType}->{destinationCdb}\n");
       
   309 			RunCed();
       
   310 
       
   311 			if ($@)
       
   312 			{
       
   313 				die $@;
       
   314 			}
       
   315 			print("\n");
       
   316 		}
       
   317 		else 
       
   318 		{
       
   319 			print("$data->{$platType}->{sourceCfg} older than $data->{$platType}->{destinationCdb}\n");
       
   320 			print("Construction aborted.\n");
       
   321 		}
       
   322 	}
       
   323 	else 
       
   324 	{
       
   325 		print("CreateCommdbs Error: $data->{$platType}->{sourceCfg} does not exist, construction aborted.\n");
       
   326 	}
       
   327 
       
   328 	print("Deleting $CopiedCfg\n");
       
   329 	DeleteFile($CopiedCfg);
       
   330 	
       
   331 }
       
   332 
       
   333 
       
   334 sub Clean 
       
   335 {
       
   336 	print("$data->{$platType}->{destinationCdb}\n");
       
   337 	DeleteFile($data->{$platType}->{destinationCdb});
       
   338 	print("Deleting $CopiedCfg\n");
       
   339 	DeleteFile($CopiedCfg);
       
   340 	print("Deleting $CedLog\n");
       
   341 	DeleteFile($CedLog);
       
   342 	print("Deleting $cdbDatPath$cdbDatFile$cdbDatExt\n");
       
   343 	DeleteFile("$cdbDatPath$cdbDatFile$cdbDatExt");
       
   344 }
       
   345 
       
   346 sub Releasables 
       
   347 {
       
   348 	print("$data->{$platType}->{destinationCdb}\n");
       
   349 	print("$CdbEmpty\n");
       
   350 }
       
   351 
       
   352 sub RunCed 
       
   353 {
       
   354 	print("sub RunCed\n");
       
   355 
       
   356 	print("Running $ced -Mconsole -dJustInTime=0 -dtextshell -- -i $data->{$platType}->{cedCfg} -d -o $data->{$platType}->{cedCfg}.log\n");
       
   357 
       
   358 	system ("$ced -Mconsole -dJustInTime=0 -dtextshell -- -i $data->{$platType}->{cedCfg} -d -o $data->{$platType}->{cedCfg}.log");
       
   359 
       
   360 	CheckCedLog("$CopiedCfg.log");
       
   361 	CheckEpocLog();
       
   362 
       
   363 	#copy to output location
       
   364 	print("Copy $winsCdb to $data->{$platType}->{destinationCdb}\n");
       
   365 	CopyFile($winsCdb, $data->{$platType}->{destinationCdb});
       
   366 
       
   367 	#copy to z drive
       
   368 	print("Copy $winsCdb to $zDriveCre\n");
       
   369 	CopyFile($winsCdb, $zDriveCre);
       
   370 
       
   371 	#print("Copy $winsCdb to $romCdb\n");
       
   372 	#CopyFile($winsCdb, $romCdb);
       
   373 }
       
   374 
       
   375 sub CheckCedLog 
       
   376 {
       
   377 	print("sub CheckCedLog\n");
       
   378 	my $log = shift;
       
   379 	open (LOG, $log) or die "CreateCommdbs Error: Couldn't open \"$log\" for reading: $!\n";
       
   380 	
       
   381 	my @log = reverse <LOG>;
       
   382 	close (LOG);
       
   383 	unless ($log[0] eq "SUCCESS\n") 
       
   384 	{
       
   385 		die "CreateCommdbs Error: CED failed to generate commdb, see \"$log\" for details\n";
       
   386 	}
       
   387 }
       
   388 
       
   389 sub CheckEpocLog
       
   390 {
       
   391 	print("sub CheckEpocLog\n");
       
   392 	my $log = $ENV{'TEMP'}."\\epocwind.out";
       
   393 	unless (open (LOG, $log))
       
   394 	{
       
   395 		print "CreateCommdbs Error: Couldn't open \"$log\" for reading: $!\n";
       
   396 		return;
       
   397 	}
       
   398 	
       
   399 	foreach my $line (<LOG>) 
       
   400 	{
       
   401 		if ($line =~ m/ced\.exe.+panic/i)
       
   402 		{
       
   403 			print "CreateCommdbs Error: CED panic detected, see \"$log\" for details\n";
       
   404 		}
       
   405 	}
       
   406 
       
   407 	close(LOG);
       
   408 }
       
   409 
       
   410 sub CopyFile 
       
   411 {
       
   412 	my $file1 = shift;
       
   413 	my $file2 = shift;
       
   414 	(my $path) = SplitFileName($file2);
       
   415 	
       
   416 	unless (-e $path) 
       
   417 	{
       
   418 		mkpath ($path) or die "CreateCommdbs Error: Couldn't make path \"$path\": $!\n";
       
   419 	}
       
   420 	
       
   421 	if (-e $file2 and not -w $file2) 
       
   422 	{
       
   423 		system "attrib -r $file2";
       
   424 	}
       
   425 	copy ($file1, $file2) or die "CreateCommdbs Error: Couldn't copy \"$file1\" to  \"$file2\": $!\n";
       
   426 }
       
   427 
       
   428 sub MoveFile 
       
   429 {
       
   430 	my $file1 = shift;
       
   431 	my $file2 = shift;
       
   432 	(my $path) = SplitFileName($file2);
       
   433 	
       
   434 	unless (-e $path) 
       
   435 	{
       
   436 		mkpath ($path) or die "CreateCommdbs Error: Couldn't make path \"$path\": $!\n";
       
   437 	}
       
   438 	move ($file1, $file2) or die "CreateCommdbs Error: Couldn't move \"$file1\" to  \"$file2\": $!\n";
       
   439 }
       
   440 
       
   441 sub DeleteFile 
       
   442 {
       
   443 	my $file = shift;
       
   444 	if (-e $file) 
       
   445 	{
       
   446 		unlink ($file) or die "CreateCommdbs Error: Couldn't delete \"$file\": $!\n";
       
   447 	}
       
   448 }
       
   449 
       
   450 sub SplitFileName 
       
   451 {
       
   452 	my $fileName = shift;
       
   453 	my $path = '';
       
   454 	my $base = '';
       
   455 	my $ext = '';
       
   456 	
       
   457 	if ($fileName =~ /^(.*\/)/) 
       
   458 	{
       
   459 		$path = $1;
       
   460 	}
       
   461 	if ($fileName =~ /\/?([^\/]*?)(\.[^\/\.]*)?$/) 
       
   462 	{
       
   463 		$base = $1;
       
   464 	}
       
   465 	if ($fileName =~ /(\.[^\/\.]*)$/o) 
       
   466 	{
       
   467 		$ext =  $1;
       
   468 	}
       
   469 	
       
   470 	die unless ($fileName eq "$path$base$ext");
       
   471 	return ($path, $base, $ext);
       
   472 }
       
   473 
       
   474 sub CopyIfYounger 
       
   475 {
       
   476 	my $from = shift;
       
   477 	my $to = shift;
       
   478 	unless (-e $from) 
       
   479 	{
       
   480 		die "Error: \"$from\" not found\n";
       
   481 	}
       
   482 	
       
   483 	if (FileIsYounger($from, $to)) 
       
   484 	{
       
   485 		CopyFile($from, $to);
       
   486 	}
       
   487 }
       
   488 
       
   489 sub FileIsYounger 
       
   490 {
       
   491 	my $file1 = shift;
       
   492 	my $file2 = shift;
       
   493 	return (FileModifiedTime($file1) > FileModifiedTime($file2));
       
   494 }
       
   495 
       
   496 sub FileModifiedTime 
       
   497 {
       
   498 	my $file = shift;
       
   499 	if (-e $file) 
       
   500 	{
       
   501 		my $st = stat($file) or return 0;
       
   502 		return $st->mtime;
       
   503 	}
       
   504 	return 0;
       
   505 }
       
   506 
       
   507 sub RenameFile
       
   508 {
       
   509 	my $file1 = shift;
       
   510 	my $file2 = shift;
       
   511 	
       
   512 	rename ($file1, $file2) or die "installCommdb Error: Couldn't rename \"$file1\" to \"$file2\": $!\n";
       
   513 }
       
   514 
       
   515 sub GetEpocRoot
       
   516 {
       
   517   my $path = $ENV{EPOCROOT};
       
   518 
       
   519   # replace to forward slashes
       
   520   $path =~ s/\\/\//g;
       
   521 
       
   522   # append trailing slash if one isn't already present
       
   523   if($path =~ m/.*\/$/)
       
   524   {
       
   525     return $path;
       
   526   }
       
   527   else
       
   528   {
       
   529     return $path."\/";
       
   530   }
       
   531 }