imgtools/imaker/buildrom_plugins/plugincommon.pm
changeset 596 9f25be3da657
parent 584 56dd7656a965
--- a/imgtools/imaker/buildrom_plugins/plugincommon.pm	Fri Jun 18 13:49:03 2010 +0300
+++ b/imgtools/imaker/buildrom_plugins/plugincommon.pm	Thu Jun 24 10:35:05 2010 +0300
@@ -21,6 +21,8 @@
 
 use strict;
 use warnings;
+use Cwd;
+use File::Basename;
 
 use constant FILESPECSTATEMENT => qr/^\s*(\S+?)\s*[=\s]\s*(?:"(.+?)"|(\S+))\s+(?:"(.+?)"|(\S+))(\s+.+?)?\s*$/;
 
@@ -34,6 +36,10 @@
 
 use constant ROFSBITMAPFILESPECKEYWORD => qr/^(?:data|file|filecompress|fileuncompress|BITMAP|AUTO-BITMAP|COMPRESSED-BITMAP)/i;
 
+use constant HIDESTATEMENT => qr/^\s*(hide\S*?)\s*[=\s]\s*(?:"(.+?)"|(\S+))()\s*$/;
+
+use constant DIRECTORYKEYWORD => qr/^(?:alias|hide|rename)/i;
+
 
 BEGIN
 {
@@ -43,38 +49,62 @@
     @ISA     = qw(Exporter);
     @EXPORT  = qw(
         FILESPECSTATEMENT FILESPECKEYWORD FILEBITMAPSPECKEYWORD ROFSFILESPECKEYWORD ROFSBITMAPFILESPECKEYWORD
-        &dprint &plugin_init &plugin_start &plugin_end &parse_keyline &parse_obyline
+        HIDESTATEMENT DIRECTORYKEYWORD
+        &abspath &dprint &plugin_reset &plugin_init &plugin_start &plugin_end &parse_keyline &parse_obyline
         &is_entry &get_type_from_entry &get_source_from_entry &get_target_from_entry
-        $gPluginname $gLogfile $gDebug $gHandlestr
-        $gLine $gLnum $gRomid $gKeyword $gSource $gTarget $gAttrib);
+        $gPluginname $gImgid $gLogfile $gWorkdir $gDebug $gFeatvar $gHandlestr
+        $gLine $gLnum $gRomid $gRomidCmp $gKeyword $gSource $gTarget $gAttrib $gSrcCmp $gTgtCmp);
     $| = 1;
 }
 
-our ($gPluginname, $gLogfile, $gDebug, $gHandlestr) = ("", "", "", 0);
-our ($gLine, $gLnum, $gRomid, $gKeyword, $gSource, $gTarget, $gAttrib) = ("", 0, 0, "", "", "", "");
+our ($gPluginname, $gImgid, $gLogfile, $gWorkdir, $gDebug, $gFeatvar, $gHandlestr);
+our ($gLine, $gLnum, $gRomid, $gRomidCmp, $gKeyword, $gSource, $gTarget, $gAttrib, $gSrcCmp, $gTgtCmp);
 my  $duration = 0;
 
-sub dprint($$)
+sub abspath($)
 {
-    my ($log, $str) = @_;
-    $str =~ s/\n//g;
-    $str = ($log < 0 ? "Warning: " : "") . "$str\n";
+    my $path = shift();
+    eval { local $SIG{__DIE__}; $path = Cwd::abs_path($path) };
+    return($path);
+}
+
+sub dprint($$;$)
+{
+    my ($log, $str, $nonl) = @_;
+    ($str = ($log < 0 ? "Warning: " : "") . $str) =~ s/\n//g;
+    $str .= "\n" if !$nonl;
     $log = abs($log);
     print($str) if (($log == 1) && !$gDebug) || (($log == 2) && $gDebug) || ($log > 2);
     print(LOG $str) if $gLogfile && ($log > 1);
 }
 
-sub plugin_init($$;$)
+sub plugin_reset()
+{
+    ($gLine, $gLnum, $gRomid, $gRomidCmp, $gKeyword, $gSource, $gTarget, $gAttrib, $gSrcCmp, $gTgtCmp) =
+        ("", 0, 0, 0, "", "", "", "", "", "");
+}
+
+sub plugin_init($$$)
 {
-    ($gPluginname, $gDebug, my $start) = @_;
-    $gDebug = "" if !defined($gDebug);
-    $gDebug =~ s/^(?:(.*?);|(.*))//;
-    $gLogfile = (defined($1) ? $1 : $2);
-    my $warn = "";
+    my ($pluginfo, $opt, $start) = @_;
+    $gPluginname = "$pluginfo->{name}.pm";
+    plugin_reset();
+    ($gImgid, $gLogfile, $gWorkdir, $gDebug, $gFeatvar, my $warn) = (0, "", undef, 0, "", "");
+    foreach (split(/;+/, $opt)) {
+        if    (s/^\s*-i//i) { $gImgid   = (/^CORE$/ ? 0 : (/^ROFS(\d)$/ ? $1 : -1)) }
+        elsif (s/^\s*-l//i) { $gLogfile = abspath(File::Basename::dirname($_)) . "/" . File::Basename::basename($_) }
+        elsif (s/^\s*-w//i) { $gWorkdir = abspath($_)  }
+        elsif (s/^\s*-d//i) { $gDebug   = ($_ ? 1 : 0) }
+        elsif (s/^\s*-f//i) { $gFeatvar = $_ }
+        else  { $warn .= ", `$_'" }
+    }
+    $warn = "Unknown parameter(s):$warn." if ($warn =~ s/^,//);
     (open(LOG, ">>$gLogfile") or
-        ($warn = "Can't write to `$gLogfile'.", $gLogfile = "")) if $gLogfile;
+        (($warn .= ($warn ? " " : "") . "Can't write to `$gLogfile'."), $gLogfile = "")) if $gLogfile;
     dprint(3, "$gPluginname: " . ($start ? "-" x (77 - length($gPluginname)) :
-        "Initializing; logfile = `$gLogfile', debug = " . ($gDebug ? 1 : 0)));
+        "Initializing; $pluginfo->{invocation}, image id = " . ($gImgid < 0 ? "d" : $gImgid) .
+        ", logfile = `$gLogfile'" . (defined($gWorkdir) ? ", workdir = `$gWorkdir'" : "") .
+        ", debug = $gDebug" . ($gFeatvar ne "" ? ", feature variant = `$gFeatvar'" : "")));
     dprint(-3, $warn) if $warn;
     close(LOG) if !$start;
 }
@@ -83,7 +113,7 @@
 {
     $duration = time();
     plugin_init(shift(), shift(), 1);
-    ($gHandlestr, $gLnum, $gRomid) = ("REM handled $gPluginname:", 0, 0);
+    $gHandlestr = "REM handled $gPluginname:";
 }
 
 sub plugin_end()
@@ -93,27 +123,29 @@
     close(LOG);
 }
 
-sub get_keyline($)
+sub parse_keyline(;$)
 {
-    my $quote = shift();
+    ($gLine = shift()) =~ s/^\s+|\s+$//g if defined($_[0]);
+    return(-1) if ($gLine !~ FILESPECSTATEMENT) && ($gLine !~ HIDESTATEMENT);
     ($gKeyword, $gSource, $gTarget, $gAttrib) =
-        ($1, defined($2) ? ($quote ? "\"$2\"" : $2) : $3, defined($4) ? ($quote ? "\"$4\"" : $4) : $5, defined($6) ? $6 : "");
+        ($1, defined($2) ? $2 : $3, defined($4) ? $4 : $5, defined($6) ? $6 : "");
+    ($gSrcCmp, $gTgtCmp) = (lc($gSource), lc($gTarget));
+    ($gTgtCmp = ($gKeyword =~ DIRECTORYKEYWORD ? $gSrcCmp : $gTgtCmp)) =~ tr/\\/\//;
+    $gTgtCmp =~ s/^\/+//;
+    return(2);
 }
 
-sub parse_keyline($;$)
-{
-    ($gLine = shift()) =~ s/^\s+|\s+$//g;
-    get_keyline(shift()), return(1) if $gLine =~ FILESPECSTATEMENT;
-    return(0);
-}
-
-sub parse_obyline($;$)
+sub parse_obyline($)
 {
     ($gLine = shift()) =~ s/^\s+|\s+$//g;
     $gLnum++;
-    $gRomid = $1, return(2) if $gLine =~ /^REM\s+ROM_IMAGE\[(\d+)\]/i;
-    return(-1) if $gLine eq "" || $gLine =~ /^(?:#|REM\s)/i;
-    return(parse_keyline($gLine, shift()));
+    if ($gLine =~ /^REM\s+(ROM|DATA)_IMAGE\[(\d+)\]/i) {
+        $gRomid = $2;
+        $gRomidCmp = ($gImgid >= 0 ? ($1 eq "ROM" ? ($2 != 1 ? $2 : 0) : 99) : ($1 eq "DATA" ? -$2 - 1 : 99));
+        return(1);
+    }
+    return(0) if ($gLine =~ /^(?:#|REM\s)/i) || ($gLine eq "");
+    return(parse_keyline());
 }