cross-plat-dev-utils/patch_upstream.pl
changeset 2 39c28ec933dd
child 6 787612182dd0
equal deleted inserted replaced
1:820b22e13ff1 2:39c28ec933dd
       
     1 #!/usr/bin/perl
       
     2 # Copyright (c) 2010 Symbian Foundation Ltd
       
     3 # This component and the accompanying materials are made available
       
     4 # under the terms of the License "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 # Mike Kinghan, mikek@symbian.org for Symbian Foundation Ltd - initial contribution.
       
    10 
       
    11 # Script to get a copy of upstream package at the baseline revision in
       
    12 # ../baseline.txt and patch it to be the same as this package.
       
    13 
       
    14 use strict;
       
    15 use get_baseline;
       
    16 use perl_run;
       
    17 use set_epocroot;
       
    18 use get_hostplatform_dir;
       
    19 use check_os;
       
    20 use File::Spec;
       
    21 use File::Temp 'tempfile';
       
    22 use Cwd 'abs_path';
       
    23 use Getopt::Long;
       
    24 
       
    25 
       
    26 my $help =0;
       
    27 my $baseline_dir = '';
       
    28 my $patch_in = '';
       
    29 
       
    30 sub usage(*);
       
    31 sub usage_error($);
       
    32 sub help();
       
    33 sub get_old_baseline_dir($);
       
    34 sub regen_patch_file($);
       
    35 
       
    36 GetOptions("help=i" => \$help, "patch-file=s" => \$patch_in,
       
    37 	"baseline-dir=s" => \$baseline_dir) or usage_error("Usage error");
       
    38 
       
    39 if ($help) {
       
    40 	help();
       
    41 }
       
    42 if (!$baseline_dir) {
       
    43 	usage_error("Need -b, --baseline-dir");
       
    44 }
       
    45 print ">>> Testing for patch\n";
       
    46 my $patch_test = "patch --version";
       
    47 print ">>> Executing: $patch_test\n";
       
    48 my $rc = system($patch_test);
       
    49 die "*** Error: can't execute the patch tool ***", if ($rc);
       
    50 $baseline_dir = abs_path($baseline_dir);
       
    51 perl_run("get_upstream.pl $baseline_dir") and die $!;
       
    52 set_epocroot();
       
    53 my $epocroot = $ENV{'EPOCROOT'};
       
    54 if (!$patch_in) {
       
    55 	my $baseline_rev = get_baseline();
       
    56 	$patch_in = File::Spec->catfile("$epocroot","build","cross-plat-dev-utils",
       
    57 		"patch-files","diffs","patch-$baseline_rev.patch");
       
    58 }
       
    59 die "*** Error: can't find patch file \"$patch_in\" ***", unless ( -f $patch_in);
       
    60 $baseline_dir = File::Spec->catfile("$baseline_dir","build");
       
    61 my $pnum = compute_pnum($patch_in);
       
    62 my $patch_cmd = "patch -d $baseline_dir -p$pnum < $patch_in";
       
    63 print ">>> Executing: $patch_cmd\n";
       
    64 exit system($patch_cmd) >> 8;
       
    65 
       
    66 sub usage(*)
       
    67 {
       
    68 	local *OUT = shift;
       
    69 	print OUT "This script gets a copy of upstream package at the " .
       
    70 		" baseline revision in ../baseline.txt and patches it to be the same ".
       
    71 		"as this package\n" .
       
    72 		"Options:\n" .
       
    73 		"-h, --help:              Display this help and exit\n" .
       
    74 		"-p, --patch-file FILE:   Apply the patch file FILE. " .
       
    75 			"Default ./patch-files/diffs/patch-N, where N is the baseline revision\n" .
       
    76 		"-b, --baseline-dir DIR:  The upstream baseline will be cloned into " .
       
    77 			"DIR if this does not seem to have been done already. Otherwise " .
       
    78 			"DIR/build will be udated to the baseline revision before patching\n" .
       
    79 		"*** It is assumed that the command 'patch' will invoke the patch tool " .
       
    80 		"***\n";
       
    81 }
       
    82 
       
    83 sub usage_error($)
       
    84 {
       
    85 	my $diag = shift;
       
    86 	print STDERR "*** $diag ***\n";
       
    87 	usage(*STDERR);
       
    88 	exit 1;
       
    89 }
       
    90 
       
    91 sub help()
       
    92 {
       
    93 	usage(*STDOUT);
       
    94 	exit 0;
       
    95 }
       
    96 
       
    97 sub get_old_baseline_dir($)
       
    98 {
       
    99 	my $patch_file = shift;
       
   100 	open PATCH,"<$patch_file" or die $!;
       
   101 	my $line = <PATCH>;
       
   102 	if ($line !~ /^## diff generated by diff_upstream.pl/) {
       
   103 		print "!!! Warning: patch file was not created by diff_upstream.pl. " .
       
   104 			"proceeding at risk !!!\n";
       
   105 	} else {
       
   106 		$line = <PATCH>;
       
   107 	} 
       
   108 	close PATCH;
       
   109 	die "*** Error: \"$patch_file\" is empty ***", unless ($line);
       
   110 	my @words = split(/ /,$line);
       
   111 	if (@words < 3) {
       
   112 		die "*** Error: can't make sense of \"$patch_file\" as a patch file ***";
       
   113 	}
       
   114 	my $old_baseline_file = $words[-2];
       
   115 	my $path_punct = os_is_windows() ? '\\' : '/';
       
   116 	my @old_baseline_parts = split(/$path_punct/,$old_baseline_file);
       
   117 	while($old_baseline_parts[-1] ne 'build') {
       
   118 		pop @old_baseline_parts;
       
   119 	}
       
   120 	my $old_base_line_dir = join($path_punct,@old_baseline_parts);
       
   121 	print ">>> Patch-file seems to have been diffed from \"$old_base_line_dir\"\n";
       
   122 	return $old_base_line_dir;
       
   123 }
       
   124 
       
   125 sub compute_pnum($)
       
   126 {
       
   127 	my $patch_file = shift;
       
   128 	my $old_base_line_dir = get_old_baseline_dir($patch_file);
       
   129 	my $path_punct = os_is_windows() ? '\\' : '\/';
       
   130 	my @old_base_line_dir_parts = split(/$path_punct/,$old_base_line_dir);
       
   131 	return @old_base_line_dir_parts;
       
   132 }
       
   133 
       
   134