imgtools/imaker/buildrom_plugins/plugincommon.pm
changeset 607 378360dbbdba
parent 596 9f25be3da657
equal deleted inserted replaced
591:22486c9c7b15 607:378360dbbdba
    19 
    19 
    20 package plugincommon;
    20 package plugincommon;
    21 
    21 
    22 use strict;
    22 use strict;
    23 use warnings;
    23 use warnings;
       
    24 use Cwd;
       
    25 use File::Basename;
    24 
    26 
    25 use constant FILESPECSTATEMENT => qr/^\s*(\S+?)\s*[=\s]\s*(?:"(.+?)"|(\S+))\s+(?:"(.+?)"|(\S+))(\s+.+?)?\s*$/;
    27 use constant FILESPECSTATEMENT => qr/^\s*(\S+?)\s*[=\s]\s*(?:"(.+?)"|(\S+))\s+(?:"(.+?)"|(\S+))(\s+.+?)?\s*$/;
    26 
    28 
    27 use constant FILESPECKEYWORD =>
    29 use constant FILESPECKEYWORD =>
    28     qr/^(?:data|file|primary|secondary|variant|device|extension|dll|filecompress|fileuncompress)/i;
    30     qr/^(?:data|file|primary|secondary|variant|device|extension|dll|filecompress|fileuncompress)/i;
    31     qr/^(?:data|file|primary|secondary|variant|device|extension|dll|filecompress|fileuncompress|BITMAP|AUTO-BITMAP|COMPRESSED-BITMAP)/i;
    33     qr/^(?:data|file|primary|secondary|variant|device|extension|dll|filecompress|fileuncompress|BITMAP|AUTO-BITMAP|COMPRESSED-BITMAP)/i;
    32 
    34 
    33 use constant ROFSFILESPECKEYWORD => qr/^(?:data|file|filecompress|fileuncompress)/i;
    35 use constant ROFSFILESPECKEYWORD => qr/^(?:data|file|filecompress|fileuncompress)/i;
    34 
    36 
    35 use constant ROFSBITMAPFILESPECKEYWORD => qr/^(?:data|file|filecompress|fileuncompress|BITMAP|AUTO-BITMAP|COMPRESSED-BITMAP)/i;
    37 use constant ROFSBITMAPFILESPECKEYWORD => qr/^(?:data|file|filecompress|fileuncompress|BITMAP|AUTO-BITMAP|COMPRESSED-BITMAP)/i;
       
    38 
       
    39 use constant HIDESTATEMENT => qr/^\s*(hide\S*?)\s*[=\s]\s*(?:"(.+?)"|(\S+))()\s*$/;
       
    40 
       
    41 use constant DIRECTORYKEYWORD => qr/^(?:alias|hide|rename)/i;
    36 
    42 
    37 
    43 
    38 BEGIN
    44 BEGIN
    39 {
    45 {
    40     use Exporter();
    46     use Exporter();
    41     our ($VERSION, @ISA, @EXPORT);
    47     our ($VERSION, @ISA, @EXPORT);
    42     $VERSION = 1.00;
    48     $VERSION = 1.00;
    43     @ISA     = qw(Exporter);
    49     @ISA     = qw(Exporter);
    44     @EXPORT  = qw(
    50     @EXPORT  = qw(
    45         FILESPECSTATEMENT FILESPECKEYWORD FILEBITMAPSPECKEYWORD ROFSFILESPECKEYWORD ROFSBITMAPFILESPECKEYWORD
    51         FILESPECSTATEMENT FILESPECKEYWORD FILEBITMAPSPECKEYWORD ROFSFILESPECKEYWORD ROFSBITMAPFILESPECKEYWORD
    46         &dprint &plugin_init &plugin_start &plugin_end &parse_keyline &parse_obyline
    52         HIDESTATEMENT DIRECTORYKEYWORD
       
    53         &abspath &dprint &plugin_reset &plugin_init &plugin_start &plugin_end &parse_keyline &parse_obyline
    47         &is_entry &get_type_from_entry &get_source_from_entry &get_target_from_entry
    54         &is_entry &get_type_from_entry &get_source_from_entry &get_target_from_entry
    48         $gPluginname $gLogfile $gDebug $gHandlestr
    55         $gPluginname $gImgid $gLogfile $gWorkdir $gDebug $gFeatvar $gHandlestr
    49         $gLine $gLnum $gRomid $gKeyword $gSource $gTarget $gAttrib);
    56         $gLine $gLnum $gRomid $gRomidCmp $gKeyword $gSource $gTarget $gAttrib $gSrcCmp $gTgtCmp);
    50     $| = 1;
    57     $| = 1;
    51 }
    58 }
    52 
    59 
    53 our ($gPluginname, $gLogfile, $gDebug, $gHandlestr) = ("", "", "", 0);
    60 our ($gPluginname, $gImgid, $gLogfile, $gWorkdir, $gDebug, $gFeatvar, $gHandlestr);
    54 our ($gLine, $gLnum, $gRomid, $gKeyword, $gSource, $gTarget, $gAttrib) = ("", 0, 0, "", "", "", "");
    61 our ($gLine, $gLnum, $gRomid, $gRomidCmp, $gKeyword, $gSource, $gTarget, $gAttrib, $gSrcCmp, $gTgtCmp);
    55 my  $duration = 0;
    62 my  $duration = 0;
    56 
    63 
    57 sub dprint($$)
    64 sub abspath($)
    58 {
    65 {
    59     my ($log, $str) = @_;
    66     my $path = shift();
    60     $str =~ s/\n//g;
    67     eval { local $SIG{__DIE__}; $path = Cwd::abs_path($path) };
    61     $str = ($log < 0 ? "Warning: " : "") . "$str\n";
    68     return($path);
       
    69 }
       
    70 
       
    71 sub dprint($$;$)
       
    72 {
       
    73     my ($log, $str, $nonl) = @_;
       
    74     ($str = ($log < 0 ? "Warning: " : "") . $str) =~ s/\n//g;
       
    75     $str .= "\n" if !$nonl;
    62     $log = abs($log);
    76     $log = abs($log);
    63     print($str) if (($log == 1) && !$gDebug) || (($log == 2) && $gDebug) || ($log > 2);
    77     print($str) if (($log == 1) && !$gDebug) || (($log == 2) && $gDebug) || ($log > 2);
    64     print(LOG $str) if $gLogfile && ($log > 1);
    78     print(LOG $str) if $gLogfile && ($log > 1);
    65 }
    79 }
    66 
    80 
    67 sub plugin_init($$;$)
    81 sub plugin_reset()
    68 {
    82 {
    69     ($gPluginname, $gDebug, my $start) = @_;
    83     ($gLine, $gLnum, $gRomid, $gRomidCmp, $gKeyword, $gSource, $gTarget, $gAttrib, $gSrcCmp, $gTgtCmp) =
    70     $gDebug = "" if !defined($gDebug);
    84         ("", 0, 0, 0, "", "", "", "", "", "");
    71     $gDebug =~ s/^(?:(.*?);|(.*))//;
    85 }
    72     $gLogfile = (defined($1) ? $1 : $2);
    86 
    73     my $warn = "";
    87 sub plugin_init($$$)
       
    88 {
       
    89     my ($pluginfo, $opt, $start) = @_;
       
    90     $gPluginname = "$pluginfo->{name}.pm";
       
    91     plugin_reset();
       
    92     ($gImgid, $gLogfile, $gWorkdir, $gDebug, $gFeatvar, my $warn) = (0, "", undef, 0, "", "");
       
    93     foreach (split(/;+/, $opt)) {
       
    94         if    (s/^\s*-i//i) { $gImgid   = (/^CORE$/ ? 0 : (/^ROFS(\d)$/ ? $1 : -1)) }
       
    95         elsif (s/^\s*-l//i) { $gLogfile = abspath(File::Basename::dirname($_)) . "/" . File::Basename::basename($_) }
       
    96         elsif (s/^\s*-w//i) { $gWorkdir = abspath($_)  }
       
    97         elsif (s/^\s*-d//i) { $gDebug   = ($_ ? 1 : 0) }
       
    98         elsif (s/^\s*-f//i) { $gFeatvar = $_ }
       
    99         else  { $warn .= ", `$_'" }
       
   100     }
       
   101     $warn = "Unknown parameter(s):$warn." if ($warn =~ s/^,//);
    74     (open(LOG, ">>$gLogfile") or
   102     (open(LOG, ">>$gLogfile") or
    75         ($warn = "Can't write to `$gLogfile'.", $gLogfile = "")) if $gLogfile;
   103         (($warn .= ($warn ? " " : "") . "Can't write to `$gLogfile'."), $gLogfile = "")) if $gLogfile;
    76     dprint(3, "$gPluginname: " . ($start ? "-" x (77 - length($gPluginname)) :
   104     dprint(3, "$gPluginname: " . ($start ? "-" x (77 - length($gPluginname)) :
    77         "Initializing; logfile = `$gLogfile', debug = " . ($gDebug ? 1 : 0)));
   105         "Initializing; $pluginfo->{invocation}, image id = " . ($gImgid < 0 ? "d" : $gImgid) .
       
   106         ", logfile = `$gLogfile'" . (defined($gWorkdir) ? ", workdir = `$gWorkdir'" : "") .
       
   107         ", debug = $gDebug" . ($gFeatvar ne "" ? ", feature variant = `$gFeatvar'" : "")));
    78     dprint(-3, $warn) if $warn;
   108     dprint(-3, $warn) if $warn;
    79     close(LOG) if !$start;
   109     close(LOG) if !$start;
    80 }
   110 }
    81 
   111 
    82 sub plugin_start($$)
   112 sub plugin_start($$)
    83 {
   113 {
    84     $duration = time();
   114     $duration = time();
    85     plugin_init(shift(), shift(), 1);
   115     plugin_init(shift(), shift(), 1);
    86     ($gHandlestr, $gLnum, $gRomid) = ("REM handled $gPluginname:", 0, 0);
   116     $gHandlestr = "REM handled $gPluginname:";
    87 }
   117 }
    88 
   118 
    89 sub plugin_end()
   119 sub plugin_end()
    90 {
   120 {
    91     my $msg = "$gPluginname: Duration: " . (time() - $duration) . " seconds ";
   121     my $msg = "$gPluginname: Duration: " . (time() - $duration) . " seconds ";
    92     dprint(3, $msg . "-" x (79 - length($msg)));
   122     dprint(3, $msg . "-" x (79 - length($msg)));
    93     close(LOG);
   123     close(LOG);
    94 }
   124 }
    95 
   125 
    96 sub get_keyline($)
   126 sub parse_keyline(;$)
    97 {
   127 {
    98     my $quote = shift();
   128     ($gLine = shift()) =~ s/^\s+|\s+$//g if defined($_[0]);
       
   129     return(-1) if ($gLine !~ FILESPECSTATEMENT) && ($gLine !~ HIDESTATEMENT);
    99     ($gKeyword, $gSource, $gTarget, $gAttrib) =
   130     ($gKeyword, $gSource, $gTarget, $gAttrib) =
   100         ($1, defined($2) ? ($quote ? "\"$2\"" : $2) : $3, defined($4) ? ($quote ? "\"$4\"" : $4) : $5, defined($6) ? $6 : "");
   131         ($1, defined($2) ? $2 : $3, defined($4) ? $4 : $5, defined($6) ? $6 : "");
   101 }
   132     ($gSrcCmp, $gTgtCmp) = (lc($gSource), lc($gTarget));
   102 
   133     ($gTgtCmp = ($gKeyword =~ DIRECTORYKEYWORD ? $gSrcCmp : $gTgtCmp)) =~ tr/\\/\//;
   103 sub parse_keyline($;$)
   134     $gTgtCmp =~ s/^\/+//;
   104 {
   135     return(2);
   105     ($gLine = shift()) =~ s/^\s+|\s+$//g;
   136 }
   106     get_keyline(shift()), return(1) if $gLine =~ FILESPECSTATEMENT;
   137 
   107     return(0);
   138 sub parse_obyline($)
   108 }
       
   109 
       
   110 sub parse_obyline($;$)
       
   111 {
   139 {
   112     ($gLine = shift()) =~ s/^\s+|\s+$//g;
   140     ($gLine = shift()) =~ s/^\s+|\s+$//g;
   113     $gLnum++;
   141     $gLnum++;
   114     $gRomid = $1, return(2) if $gLine =~ /^REM\s+ROM_IMAGE\[(\d+)\]/i;
   142     if ($gLine =~ /^REM\s+(ROM|DATA)_IMAGE\[(\d+)\]/i) {
   115     return(-1) if $gLine eq "" || $gLine =~ /^(?:#|REM\s)/i;
   143         $gRomid = $2;
   116     return(parse_keyline($gLine, shift()));
   144         $gRomidCmp = ($gImgid >= 0 ? ($1 eq "ROM" ? ($2 != 1 ? $2 : 0) : 99) : ($1 eq "DATA" ? -$2 - 1 : 99));
       
   145         return(1);
       
   146     }
       
   147     return(0) if ($gLine =~ /^(?:#|REM\s)/i) || ($gLine eq "");
       
   148     return(parse_keyline());
   117 }
   149 }
   118 
   150 
   119 
   151 
   120 ###############################################################################
   152 ###############################################################################
   121 #
   153 #