Adding release file
[o-du/l2.git] / src / build / common / rlog.pl
1 ################################################################################
2 #   Copyright (c) [2017-2019] [Radisys]                                        #
3 #                                                                              #
4 #   Licensed under the Apache License, Version 2.0 (the "License");            #
5 #   you may not use this file except in compliance with the License.           #
6 #   You may obtain a copy of the License at                                    #
7 #                                                                              #
8 #       http://www.apache.org/licenses/LICENSE-2.0                             #
9 #                                                                              #
10 #   Unless required by applicable law or agreed to in writing, software        #
11 #   distributed under the License is distributed on an "AS IS" BASIS,          #
12 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
13 #   See the License for the specific language governing permissions and        #
14 #   limitations under the License.                                             #
15 ################################################################################
16
17 #!/usr/bin/perl -w
18 #use strict;
19 use POSIX;
20 use Time::HiRes qw(usleep);
21 local $| = 1;
22
23 #if($ARGV[1] eq "INTEL") {
24 #   if ($#ARGV == 3 ) {
25 #      print "Make DB Script ERROR: More than two arument is passed to script\n";
26 #      exit;
27 #   }
28 #} else {
29 #   if ($#ARGV == 4 ) {
30 #      print "Make DB Script ERROR: More than two arument is passed to script\n";
31 #      exit;
32 #   }
33 #}
34 ########################################################### 
35 # Reading File name and creating DB file name.
36 # my $OrgFileName   = $ARGV[0];  # Copied File name in to a varible.
37 # my @spltname      = split('/', $OrgFileName);
38 my $numelem       = $#spltname;
39 my $fileName      = $spltname[$numelem]; 
40
41 #variable declaration.
42 my @arryOfLogId      = 0;
43 my $moduleId         = 0;
44 my $fileId           = 0;
45 my $moduleName       = 0;
46 my $lineNum          = 0;
47 my $test             = 0;
48 my $printString      = 0;
49 my @varibles         = 0;
50 my $logLvl           = 0;
51 my $splArgName       = 0;
52 my $splArgVal        = 0;
53 my $tokenID          = 0;
54 my $hexTokenID       = 0;
55 my $CStart           = 0;
56 my $CPattern         = 0;
57 my $CVarible         = 0;
58 my $CString          = 0;
59 my $CEnd             = 0;
60 my $NewVarible       = 0;
61 my $delimit          = '%8ld';
62 #my $isModuleNameFound = 0;
63 my $greatestTokenID  = 0;
64 my $DBFILE;
65 my $tmpfile;
66 my $tmpfile1;
67 my $dbentry;
68
69 ###########################################################
70 # Declare the subroutines
71 sub trim($);
72 sub ltrim($);
73 sub rtrim($);
74 sub findDup;
75
76 # Perl trim function to remove whitespace from the start and end of the string
77 sub trim($) {
78    my $string = shift;
79    $string =~ s/^\s+//;
80    $string =~ s/\s+$//;
81    return $string;
82 }
83
84 # Left trim function to remove leading whitespace
85 sub ltrim($) {
86    my $string = shift;
87    $string =~ s/^\s+//;
88    return $string;
89 }
90
91 # Right trim function to remove trailing whitespace
92 sub rtrim($) {
93    my $string = shift;
94    $string =~ s/\s+$//;
95    return $string;
96 }
97
98 my $prj = $ARGV[0];
99 my $tmp = $ARGV[0];
100 my @dbFilesDir = ($ARGV[2]);
101
102 # Process the file and generate the log DB for the file
103 #print "Arguments passed are ", @ARGV, "\n";
104 #print "Calling process of the file with argument ", $ARGV[1], "\n";
105 parse_preproc_file($ARGV[1]);
106
107 sub parse_preproc_file {
108     my $OrgFileName = shift;
109     #print "File passed is ", $OrgFileName, "\n";
110     chomp($OrgFileName);
111     my $generatedb = 0;
112     my $dbFileName  = sprintf("%s%s", $OrgFileName,".db");
113     my $logNum           = 0;
114
115     my $isModuleIdFound = 0;
116     my $isFileIdFound = 0;
117     my $isModuleNameFound = 0;
118     $logNum = 0;
119     $fileId = 0;
120
121     # Opening source.i file and DB file        
122     # print "FILE:$OrgFileName\n";
123     `cp -f $OrgFileName $OrgFileName.bak`;
124     if($tmp eq "INTEL") {
125        #print "Removing # for INTEL";
126        system("sed -i '/^#/d' $OrgFileName");
127     } elsif($tmp eq "XEON"){
128        #print "Remove # for XEON";
129        system("sed -i '/stdlib\-bsearch.h/s/^#//g' $OrgFileName");
130        system("sed -i '/^#/d' $OrgFileName");
131        system("sed -i '/stdlib\-bsearch.h/s/^/#/g' $OrgFileName");
132     }else {
133        #print "Don't Remove # for BRCM";
134        #system("sed -i '/^#/d' $OrgFileName");
135     }   
136
137     open FILE, "<$OrgFileName" or die $!;
138     open my $OUTPFILE, '>', "$OrgFileName.tmp" or die "Can't write new file: $!";
139     my $fromFound =0;
140    
141     while(<FILE>) {  
142         if (( $_ =~ /logLev[0-4EHS]/ ) && ( $_ !~  /}[;]*/) && ( $_ !~  /R_LOG_LEVEL/)) { 
143             $fromFound = 1;
144             $concatStr=$_;
145             chomp($concatStr);
146             next;
147         }
148    
149         if( ($_ =~ /}[;]*/) && ($fromFound == 1) ) {  
150             $concatStr =~ s/(.*)(logLev[E0-4HS]) +(.*)/$1$2$3/g;
151             print $OUTPFILE $concatStr;
152             print $OUTPFILE $_;
153             $fromFound = 0;
154             next;
155         }
156
157         if ($fromFound == 1) {
158             chomp($_);
159             $concatStr = $concatStr.$_;
160         } else {
161             #print $OUTPFILE "/* AARTI4*/";
162             $_ =~ s/(.*)(logLev[E0-4HS]) +(.*)/$1$2$3/g;
163             print $OUTPFILE $_;
164         }
165     }
166     close(FILE);
167     close($OUTPFILE);
168     `mv -f $OrgFileName.tmp $OrgFileName`;
169    
170     open(INFILE, "< $OrgFileName") || die("Make DB Script ERROR: Cannot open file $OrgFileName for parse");
171     open $tmpfile, "> $OrgFileName.tmp.i" || die("Data Base ERROR: Cannot Create temporary file");
172     open ($dbfile, '>' , "$OrgFileName.tmp.db") || die("Data Base ERROR: Cannot create the file $dbFileName");
173     ##########################################################
174     # Read each line and create the DB entries
175     while (<INFILE>) {
176         if (($isModuleIdFound != 1) || ($isFileIdFound != 1) || ($isModuleNameFound !=1)) {
177             if (/(\s*.*)RLOG_MODULE_ID(\s*.*)=(\s*.*);/) {
178                 $moduleId = $3;
179                 $isModuleIdFound  = 1;
180             }
181
182             if (/(\s*.*)RLOG_FILE_ID(\s*.*)=(\s*.*);/) {
183                 $fileId = $3;
184                 $isFileIdFound = 1;
185                 #print "FILE $OrgFileName FILE ID $fileId [$1] [$2] [$3]\n";
186             }
187             if (/(\s*.*)*RLOG_MODULE_NAME(\s*.*)=(\s*.*);/) {
188                 $moduleName = $3;
189                 $moduleName = substr($moduleName,1);
190                 $moduleName = substr($moduleName, 0, -1);
191                 $isModuleNameFound = 1;
192             }
193
194             print $tmpfile $_;
195         } elsif (/(\s*.*)(logLev[E0-4HS]\()(\s*.*\,)(\s*\".*\")(\s*.*)/) {
196             $CStart   = $1;
197             $CPattern = $2;
198             $CVarible = $3;
199             $CString  = $4;
200             $CEnd     = $5;
201
202             if( $3 =~ m/_LOGID/ ) {
203                 my    $fmtStr = $CString;
204                 @variables = split(/,/, $CVarible);
205                 my $arrSize = $#variables;
206                 my $line = $variables[$arrSize];
207                 my $file = $variables[$arrSize-1];
208
209                 if ($fileId < 0x3FF) {
210                     $tokenID = ($fileId << 16)|(++$logNum); 
211                     $hexTokenID = sprintf("0x%x", $tokenID);
212                 } else { print "SOME ERROR MODID:$moduleId FILEID:$fileId\n";
213                     return 1;
214                 }
215
216                 $CVarible =~ s/_LOGID/$hexTokenID/;
217
218                 print $tmpfile "$CStart$CPattern$CVarible$CString$CEnd\n";
219                 print $dbfile "$tokenID;$line;$moduleName;$file;$fmtStr\n";
220                 $generatedb = 1;
221             }
222         } else {
223             print $tmpfile $_;
224         }
225         $lineNum = $lineNum + 1;
226     }
227
228     ##########################################################################
229     #Close all files and create final output file
230     close(INFILE);
231     close ($tmpfile);
232     close ($dbfile);
233
234     #close ($DBFILE);
235     #if( $generatedb  eq 1) {
236         `cp -f $OrgFileName.tmp.i $OrgFileName`;
237         `mv -f $OrgFileName.tmp.db $dbFileName`;
238         `dos2unix -q $OrgFileName`;
239         `dos2unix -q $dbFileName`;
240                 `rm -f $OrgFileName.tmp.i`
241     #} else {
242     #    'rm -f tmp.i';
243     #}
244 }