cross-plat-dev-utils/patch_upstream.pl
author Mike Kinghan <mikek@symbian.org>
Thu, 25 Nov 2010 13:59:07 +0000
changeset 40 68f68128601f
parent 8 b260811257d2
permissions -rwxr-xr-x
1) Add the sbsv1 components from sftools/dev/build to make the linux_build package independent of the obsolete buildtools package. 2) Enhance romnibus.pl so that it generate the symbol file for the built rom when invoked by Raptor 3) Make the maksym.pl tool portable for Linux as well as Windows. 4) Remove the of armasm2as.pl from the e32tools component in favour of the copy now exported from sbsv1/e32util.

#!/usr/bin/perl
# Copyright (c) 2010 Symbian Foundation Ltd
# This component and the accompanying materials are made available
# under the terms of the License "Eclipse Public License v1.0"
# which accompanies this distribution, and is available
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
#
# Initial Contributors:
# Mike Kinghan, mikek@symbian.org for Symbian Foundation Ltd - initial contribution.

# Script to get a copy of upstream package at the baseline revision in
# ../baseline.txt and patch it to be the same as this package.

use strict;
use get_baseline;
use perl_run;
use places;
use get_hostplatform_dir;
use check_os;
use File::Spec;
use Cwd 'abs_path';
use Getopt::Long;


my $help =0;
my $baseline_dir = '';
my $patch_in = '';

sub usage(*);
sub usage_error($);
sub help();
sub get_old_baseline_dir($);

GetOptions("help=i" => \$help, "patch-file=s" => \$patch_in,
	"baseline-dir=s" => \$baseline_dir) or usage_error("Usage error");

if ($help) {
	help();
}
if (!$baseline_dir) {
	usage_error("Need -b, --baseline-dir");
}
print ">>> Testing for patch\n";
my $patch_test = "patch --version";
print ">>> Executing: $patch_test\n";
my $rc = system($patch_test);
die "*** Error: can't execute the patch tool ***", if ($rc);
$baseline_dir = abs_path($baseline_dir);
perl_run("get_upstream.pl $baseline_dir") and die $!;
my $epocroot = get_epocroot();
if (!$patch_in) {
	my $baseline_rev = get_baseline();
	$patch_in = File::Spec->catfile(get_pkg_dir(),"cross-plat-dev-utils",
		"patch-files","diffs","patch-$baseline_rev.patch");
}
die "*** Error: can't find patch file \"$patch_in\" ***", unless ( -f $patch_in);
$baseline_dir = File::Spec->catfile("$baseline_dir","build");
my $pnum = compute_pnum($patch_in);
my $patch_cmd = "patch -d $baseline_dir -p$pnum < $patch_in";
print ">>> Executing: $patch_cmd\n";
exit system($patch_cmd) >> 8;

sub usage(*)
{
	local *OUT = shift;
	print OUT "This script gets a copy of upstream package at the " .
		" baseline revision in ../baseline.txt and patches it to be the same ".
		"as this package\n" .
		"Options:\n" .
		"-h, --help:              Display this help and exit\n" .
		"-p, --patch-file FILE:   Apply the patch file FILE. " .
			"Default ./patch-files/diffs/patch-N, where N is the baseline revision\n" .
		"-b, --baseline-dir DIR:  The upstream baseline will be cloned into " .
			"DIR if this does not seem to have been done already. Otherwise " .
			"DIR/build will be udated to the baseline revision before patching\n" .
		"*** It is assumed that the command 'patch' will invoke the patch tool " .
		"***\n";
}

sub usage_error($)
{
	my $diag = shift;
	print STDERR "*** $diag ***\n";
	usage(*STDERR);
	exit 1;
}

sub help()
{
	usage(*STDOUT);
	exit 0;
}

sub get_old_baseline_dir($)
{
	my $patch_file = shift;
	open PATCH,"<$patch_file" or die $!;
	my $line = <PATCH>;
	if ($line !~ /^## diff generated by diff_upstream.pl/) {
		print "!!! Warning: patch file was not created by diff_upstream.pl. " .
			"proceeding at risk !!!\n";
	} else {
		$line = <PATCH>;
	} 
	close PATCH;
	die "*** Error: \"$patch_file\" is empty ***", unless ($line);
	my @words = split(/ /,$line);
	if (@words < 3) {
		die "*** Error: can't make sense of \"$patch_file\" as a patch file ***";
	}
	my $old_baseline_file = $words[-2];
	my $path_punct = os_is_windows() ? '\\' : '/';
	my @old_baseline_parts = split(/$path_punct/,$old_baseline_file);
	while($old_baseline_parts[-1] ne 'build') {
		pop @old_baseline_parts;
	}
	my $old_base_line_dir = join($path_punct,@old_baseline_parts);
	print ">>> Patch-file seems to have been diffed from \"$old_base_line_dir\"\n";
	return $old_base_line_dir;
}

sub compute_pnum($)
{
	my $patch_file = shift;
	my $old_base_line_dir = get_old_baseline_dir($patch_file);
	my $path_punct = os_is_windows() ? '\\' : '\/';
	my @old_base_line_dir_parts = split(/$path_punct/,$old_base_line_dir);
	return @old_base_line_dir_parts;
}