blob: e4046d107e58f8c8d166202cd2ebc7f6e6d26f44 [file] [log] [blame]
rjw6c1fd8f2022-11-30 14:33:01 +08001#!/usr/bin/perl
2#
3# Copyright Statement:
4# --------------------
5# This software is protected by Copyright and the information contained
6# herein is confidential. The software may not be copied and the information
7# contained herein may not be used or disclosed except with the written
8# permission of MediaTek Inc. (C) 2006
9#
10# BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
11# THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
12# RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
13# AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
14# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
15# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
16# NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
17# SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
18# SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
19# THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
20# NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
21# SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
22#
23# BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
24# LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
25# AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
26# OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
27# MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
28#
29# THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
30# WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
31# LAWS PRINCIPLES. ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
32# RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
33# THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
34#
35#*****************************************************************************
36#*
37#* Filename:
38#* ---------
39#* auto_adjust_mem.pm
40#*
41#* Project:
42#* --------
43#*
44#*
45#* Description:
46#* ------------
47#* This module collects the subroutines for common utility.
48#*
49#*
50#* Author:
51#* -------
52#* Qmei Yang (mtk03726)
53#*
54#****************************************************************************/
55#****************************************************************************
56# Included Modules
57#****************************************************************************
58use strict;
59#****************************************************************************
60# Constants
61#****************************************************************************
62my $AAPMC_VERNO = " m0.02";
63 # m0.02, Support load view size insufficient error code
64 # m0.01, Support folder on Linux
65 # v0.03, Add AAPMC::Copyer to support theone architecture
66 # v0.02, 20120425 by mei, add one error code
67 # v0.01, initial version
68
69return 1;
70
71#****************************************************************************
72# Package : ERR
73#****************************************************************************
74package ERR;
75use constant NO_MODIFY => 0;
76use constant MODIFY_SUCCESS => 1;
77use constant CANNOT_ADJUST => 2;
78use constant AAPMCLOG_SUCCESS => 3;
79use constant AUTOCONFIG_SELF_MODIFY_SUCCESS => 4;
80use constant NO_NEED_TO_UPDATE => 5;
81
82use constant ERR_MODIFYFAIL => 101;
83use constant ERR_UNEXPECTED => 102; #default exit
84use constant ERR_MODIFYDUPLICATED => 103;
85use constant ERR_LOADVIEWSIZE_INSUFFICIENT => 104;
86
87sub QueryErrMeaning
88{
89 my ($ERR) = @_;
90 return "[$ERR]NO_MODIFY, PASS" if($ERR == NO_MODIFY);
91 return "[$ERR]MODIFY_SUCCESS, PASS" if($ERR == MODIFY_SUCCESS);
92 return "[$ERR]CANNOT_ADJUST, FAIL" if($ERR == CANNOT_ADJUST);
93 return "[$ERR]AAPMCLOG_SUCCESS, PASS" if($ERR == AAPMCLOG_SUCCESS);
94 return "[$ERR]AUTOCONFIG_SELF_MODIFY_SUCCESS, PASS" if($ERR == AUTOCONFIG_SELF_MODIFY_SUCCESS);
95 return "[$ERR]NO_NEED_TO_UPDATE, PASS" if($ERR == NO_NEED_TO_UPDATE);
96 return "[$ERR]MODIFYFAIL, FAIL" if($ERR == ERR_MODIFYFAIL);
97 return "[$ERR]UNEXPECTED, FAIL" if($ERR == ERR_UNEXPECTED);
98 return "[$ERR]MODIFY DUPLICATED, FAIL" if($ERR == ERR_MODIFYDUPLICATED);
99 return "[$ERR]LOADVIEWSIZE_INSUFFICIENT, FAIL" if($ERR == ERR_LOADVIEWSIZE_INSUFFICIENT);
100}
101
102#****************************************************************************
103# Package : AUTO_ADJUST
104#****************************************************************************
105package AUTO_ADJUST;
106#****************************************************************************
107# subroutine: AUTO_ADJUST::ChangeDefineValue
108# aim to change #define value
109# input: $strFilePath: file path
110# %ChangeListHash={$strName, $strValue}
111# After changing, it should look like: #define $strName $strValue
112# output: $bModified: ERR::NO_MODIFY= no change, ERR::MODIFY_SUCCESS=modified
113# SampleCode:
114# my %ChangeList;
115# $ChangeList{'name1'} = "value1";
116# $ChangeList{'name2'} = "value2";
117# &AUTO_ADJUST::ChangeDefineValue($CUSTOM_FEATURE_CONFIG_H, \%ChangeList);
118# or &AUTO_ADJUST::ChangeDefineValue($CUSTOM_FEATURE_CONFIG_H, {'name1'=>'value1',});
119#****************************************************************************
120sub ChangeDefineValue
121{
122 my ($strFilePath, $ChangeListHash_href) = @_;
123 open (FILE_HANDLE, $strFilePath) or &autoadjust_die("Cannot open $strFilePath\n", __FILE__, __LINE__);
124 my $reading = "";
125 my $bModified = ERR::ERR_MODIFYFAIL;
126
127 while (<FILE_HANDLE>)
128 {
129 my $strLine = $_;
130 foreach my $strName (keys %$ChangeListHash_href)
131 {
132 my $strValue = $ChangeListHash_href->{$strName};
133 if($strLine =~ /^#define\s+($strName)\s+(\S+)/ or
134 $strLine =~ /\/\/\s*#define\s+($strName)\s+/ or
135 $strLine =~ /\/\/\s*#define\s+($strName)$/)
136 {
137 next if(hex($strValue) == hex($2));
138 $strLine = "#define $strName $strValue\n";
139 $bModified = ERR::MODIFY_SUCCESS;
140 }
141 }
142 $reading .= $strLine;
143 }
144 close FILE_HANDLE;
145
146 open (FILE_HANDLE, ">$strFilePath") or &autoadjust_die("Cannot open $strFilePath\n", __FILE__, __LINE__);
147 print FILE_HANDLE $reading;
148 close FILE_HANDLE;
149 #print $bModified == 0 ? "No change\n" : "Modified\n";
150 return $bModified;
151}
152
153
154#****************************************************************************
155# subroutine: AUTO_ADJUST::ModifyByChangeRecord
156# input: $strFolderPath: folder path before mcu\
157# Reference of @ChangeRecord = [$strFilePath, %ChangeListHash, %P4Info]
158# $bNeedBackup: 0=no need backup, 1=need backup
159# output: $err: ERR::NO_MODIFY= no change, ERR::MODIFY_SUCCESS=modified
160# Note: Backup the file before modifying. If no change, delete backup file.
161# Backup file name=File.beforeAutoAdj.timestamp.[rand=3 digits]
162# eg. ABC.h.beforeAutoAdj.1322408801.282
163#****************************************************************************
164sub ModifyByChangeRecord
165{
166 my ($strFolderPath, $ChangeRecord_ref, $bNeedBackup) = @_;
167 my $strFileName = $ChangeRecord_ref->[0];
168 my $TheOneBackupPath = AAPMC_COPYER::TheOneBackup($strFolderPath, $strFileName);
169 my $BackupPath = AAPMC_COPYER::AAPMCBackup($strFolderPath, $strFileName) if ($bNeedBackup ==1);
170 my $filepath = $strFolderPath . $strFileName;
171 my $err = &ChangeDefineValue($filepath, $ChangeRecord_ref->[1]);
172 unlink $BackupPath if($bNeedBackup ==1 and $err == ERR::NO_MODIFY);
173 unlink $TheOneBackupPath if($err == ERR::NO_MODIFY);
174 return $err;
175}
176
177#****************************************************************************
178# subroutine: AUTO_ADJUST::CreateP4InfoTemplate
179# input: $strOwner_ID: P4 ID
180# $strProject: Project Name
181# $strPurpose: CR Purpose
182# $strAdjustmentResult: Change List
183# output: Reference of %P4Info
184#****************************************************************************
185sub CreateP4InfoTemplate
186{
187 my ($strOwner_ID, $strProject, $strPurpose, $strAdjustmentResult) = @_;
188 my %P4Info = ( "OWNER_ID" => $strOwner_ID,
189 "CR_DESCRIPTION" => "[$strProject]$strPurpose\n$strAdjustmentResult" );
190 return \%P4Info;
191}
192#****************************************************************************
193# subroutine: AUTO_ADJUST::GetP4Info
194# input: Reference of %P4Info
195# output: $strOwner_ID: P4 Owner ID
196# $strCR_Description: CR description for 1 modification
197#****************************************************************************
198sub GetP4Info
199{
200 my ($P4Info_ref) = @_;
201 my ($strOwner_ID, $strCR_Description);
202 if($P4Info_ref)
203 {
204 $strOwner_ID = $P4Info_ref->{OWNER_ID};
205 $strCR_Description = $P4Info_ref->{CR_DESCRIPTION};
206 }
207 return ($strOwner_ID, $strCR_Description);
208}
209
210#****************************************************************************
211# subroutine: error_handler
212# input: $error_msg: error message
213#****************************************************************************
214sub error_handler
215{
216 my ($error_msg, $file, $line_no, $strTitle) = @_;
217
218 my $final_error_msg = "$strTitle ERROR: $error_msg at $file line $line_no : $!\n";
219 print $final_error_msg;
220 die $final_error_msg;
221}
222sub autoadjust_die
223{
224 my ($error_msg, $file, $line_no) = @_;
225 &error_handler($error_msg, $file, $line_no, 'AUTO ADJUST MEM');
226}
227
228
229#****************************************************************************
230# Package : AAPMCLogParser
231#****************************************************************************
232package AAPMCLogParser;
233use Storable qw/lock_retrieve lock_nstore/;
234
235my $g_FileArchieve = undef;
236my $g_DBInfo_ref = undef; # %DBInfoTable
237
238#****************************************************************************
239# subroutine: AAPMCLogParser::Open
240# input: $strLogPath: AAPMC.log's path
241# output: $err: ERR::ERR_UNEXPECTED= Load failed
242# ERR::AAPMCLOG_SUCCESS=Load successfully
243#****************************************************************************
244sub Open
245{
246 my ($strLogPath) = @_;
247 my $err = ERR::ERR_UNEXPECTED;
248 if(!-e $strLogPath)
249 {
250 my %Empty = ('AAPMCLog' => ());
251 lock_nstore \%Empty, $strLogPath;
252 }
253 my $g_FileArchieve = lock_retrieve $strLogPath;
254 if($g_FileArchieve)
255 {
256 $g_DBInfo_ref = $g_FileArchieve->{'AAPMCLog'};
257 $err = ERR::AAPMCLOG_SUCCESS;
258 }
259 return $err;
260}
261#****************************************************************************
262# subroutine: AAPMCLogParser::Close
263# input: $strLogPath: AAPMC.log's path
264# output: $err: ERR::ERR_UNEXPECTED= Load failed
265# ERR::AAPMCLOG_SUCCESS=Load successfully
266#****************************************************************************
267sub Close
268{
269 my ($strLogPath) = @_;
270 my $err = ERR::ERR_UNEXPECTED;
271 if(-e $strLogPath)
272 {
273 my %Temp = ('AAPMCLog' => $g_DBInfo_ref);
274 lock_nstore \%Temp, $strLogPath;
275 $err = ERR::AAPMCLOG_SUCCESS;
276 }
277 return $err;
278}
279#****************************************************************************
280# subroutine: AAPMCLogParser::AddOneChangeRecord
281# input: $ChangeFilePath: The file is going to be modified. (store its path by mcu\... without folder information)
282# $ChangeList_ref: Reference of %ChangeListHash={$strName, $strValue}
283# $P4Info_ref: Reference of %P4Info = {$OwnerID, $CR_Description} => You can create by &AUTO_ADJUST::CreateP4InfoTemplate
284# output: $err: ERR::ERR_MODIFYDUPLICATED : Can't add the same key ChangeList more than twice
285# ERR::AAPMCLOG_SUCCESS : Add successfully
286#****************************************************************************
287sub AddOneChangeRecord
288{
289 my ($ChangeFilePath, $ChangeList_ref, $P4Info_ref) = @_;
290 my $err = ERR::ERR_UNEXPECTED;
291 #Trim the string before mcu\ in which mcu should be the last mcu
292 if($ChangeFilePath =~ /\S+(mcu\S+)/)
293 {
294 $ChangeFilePath = $1;
295 }
296 if(0 == &isModified($ChangeFilePath, $ChangeList_ref))
297 {
298 #Key=TimeStamp, Value=0: $ChangeFilePath
299 # 1: %ChangeList
300 # 2: %P4Info
301 $g_DBInfo_ref->{&GetTimeStamp()} = [ $ChangeFilePath, $ChangeList_ref, \%$P4Info_ref ];
302 $err = ERR::AAPMCLOG_SUCCESS;
303 }
304 else
305 {
306 $err = ERR::ERR_MODIFYDUPLICATED;
307 }
308 return $err;
309}
310#****************************************************************************
311# subroutine: AAPMCLogParser::isModified : Called by AddOneChangeRecord()
312# input: $strChangeFilePath: The file is going to be modified. (store its path by mcu\... without folder information)
313# $ChangeList_ref: Reference of %ChangeListHash={$strName, $strValue}
314# output: $isModified: 0=never modified before, 1= it has been modified
315# comment: in the same file, if ChangeList's option existed, then return duplicated modification.
316#****************************************************************************
317sub isModified
318{
319 my ($strChangeFilePath, $ChangeList_ref) = @_;
320 my $isModified = 0; # 0=no, 1=yes;
321 if($g_DBInfo_ref)
322 {
323 while (my ($timestamp, $aChange) = each (%$g_DBInfo_ref))
324 {
325 if(($aChange->[0] eq $strChangeFilePath))
326 {
327 foreach my $option (keys %$ChangeList_ref)
328 {
329 if(exists $aChange->[1]{$option})
330 {
331 $isModified = 1;
332 last;
333 }
334 }
335 }
336 }
337 }
338 #print $isModified==0 ? "never modified\n" : "has been modified\n";
339 return $isModified;
340}
341#****************************************************************************
342# subroutine: AAPMCLogParser::GetAllRecordsIndex
343# input: x
344# output: $err: ERR::ERR_UNEXPECTED: no records in DBInfo.
345# ERR::AAPMCLOG_SUCCESS
346# Reference of @ChangeRecords: an array of all timestamps
347#****************************************************************************
348sub GetAllRecordsIndex
349{
350 my @ChangeRecords;
351 my $err = ERR::ERR_UNEXPECTED;
352 if($g_DBInfo_ref)
353 {
354 map {push (@ChangeRecords, $_);} keys %$g_DBInfo_ref;
355 $err = ERR::AAPMCLOG_SUCCESS;
356 }
357 return ($err, \@ChangeRecords);
358}
359
360
361#****************************************************************************
362# subroutine: AAPMCLogParser::GetByIndex
363# input: $Index: timestamp
364# $Type: AAPMCLogParser::FILEPATH
365# AAPMCLogParser::CHANGELIST
366# AAPMCLogParser::P4Info
367# AAPMCLogParser::RECORD
368# output: 1. $err=ERR::ERR_UNEXPECTED=no data in DBInfo
369# ERR::AAPMCLOG_SUCCESS
370# 2. if $Type=AAPMCLogParser::FILEPATH, return strChangeFilePath
371# $Type=AAPMCLogParser::CHANGELIST, return Reference of %ChangeList
372# $Type=AAPMCLogParser::P4INFO, return Reference of %P4Info,
373# you may use AUTO_ADJUST::GetP4Info to get P4Info
374# $Type=AAPMCLogParser::RECORD, return Reference of %ChangeRecord
375# else, return undef with ERR::ERR_UNEXPECTED
376#****************************************************************************
377use constant FILEPATH => 0;
378use constant CHANGELIST => 1;
379use constant P4INFO => 2;
380use constant RECORD => 3;
381sub GetByIndex
382{
383 my ($Index, $Type) = @_;
384 if($g_DBInfo_ref)
385 {
386 if($Type < RECORD)
387 {
388 return ($g_DBInfo_ref->{$Index}[$Type]);
389 }
390 elsif($Type == RECORD)
391 {
392 return ($g_DBInfo_ref->{$Index}); # @ChangeRecord
393 }
394 }
395 return undef;
396}
397
398#****************************************************************************
399# subroutine: AAPMCLogParser::GetTimeStamp
400# input: x
401# output: string of timestamp with rand(1000)
402# eg. 1322408801.282
403#****************************************************************************
404sub GetTimeStamp
405{
406 return time().".".int(rand(1000));
407}
408
409#****************************************************************************
410# Package : AAPMC_COPYER
411#****************************************************************************
412package AAPMC_COPYER;
413
414use constant THEONE => "theone";
415use constant CONFIG => "conf";
416
417use File::Copy;
418#****************************************************************************
419# subroutine: AAPMC_COPYER::AAPMCBackup
420# input:
421# output:
422#****************************************************************************
423sub AAPMCBackup
424{
425 my ($strBuildFolder, $strFileName) = @_;
426 #Backup for AutoAdj
427 $strBuildFolder .= '/' if($strBuildFolder !~ /\/$/);
428 my $OrgFilePath = $strBuildFolder.$strFileName;
429 my $BackupPath = $OrgFilePath.".beforeAutoAdj".&AAPMCLogParser::GetTimeStamp();
430 copy($OrgFilePath, $BackupPath);
431 return $BackupPath;
432}
433sub TheOneBackup
434{
435 my ($strBuildFolder, $strFileName) = @_;
436 #Backup for the one
437 $strBuildFolder .= '/' if($strBuildFolder !~ /\/$/);
438 my $OrgFilePath = $strBuildFolder.$strFileName;
439 my $BackupPath = $OrgFilePath.".".THEONE;
440 if(!-e $BackupPath)
441 {
442 copy($OrgFilePath, $BackupPath);
443 }
444 return $BackupPath;
445}
446
447#****************************************************************************
448# subroutine: AAPMC_COPYER::IsNeededToCopy
449# purpose: before m sysgen, build flow needs to know if it needs to copy the files
450# from custom\system\[BB]\
451# input: 1. $strBuild_FolderPath : build folder for custom\system
452# 2. $strBB_FolderPath : custom\system\[BB]
453# 3. $strFileName : filename without .conf or .theone for querying if it is needed to copy
454# output: undef = no need to copy
455# 1 = needed to copy
456#****************************************************************************
457sub IsNeededToCopy
458{
459 my ($strBuild_FolderPath, $strBB_FolderPath, $strFileName) = @_;
460 my $bNeededToCopy = undef;
461 $strBuild_FolderPath .= '/' if($strBuild_FolderPath !~ /\/$/);
462 $strBB_FolderPath .= '/' if($strBB_FolderPath !~ /\/$/);
463
464 my $strBuildFile = $strBuild_FolderPath . $strFileName;
465 my $strBBFile = $strBB_FolderPath . $strFileName ."." .CONFIG;
466 my $strTheOneFile = $strBuild_FolderPath . $strFileName . "." . THEONE;
467 if(-e $strBuildFile)
468 {
469 if(-e $strTheOneFile)
470 {
471 $bNeededToCopy = &CompareFile($strBBFile, $strTheOneFile);
472 }
473 else
474 {
475 $bNeededToCopy = 1;
476 }
477 }
478 else
479 {
480 $bNeededToCopy = 1;
481 }
482 return $bNeededToCopy;
483}
484#****************************************************************************
485# subroutine: AAPMC_COPYER::CompareFile
486# input: 1. strSrcPath
487# 2. strDestPath
488# output: undef = no different
489# 1 = have some difference
490#****************************************************************************
491sub CompareFile
492{
493 my ($strSrcPath, $strDestPath) = @_;
494 my $bDeff = undef;
495 my $strSrcContent = &GetFileContent($strSrcPath);
496 my $strDestContent = &GetFileContent($strDestPath);
497 $bDeff = 1 if($strSrcContent ne $strDestContent);
498 return $bDeff;
499
500}
501#****************************************************************************
502# subroutine: AAPMC_COPYER::GetFileContent
503# input: strFilePath
504# output: strFileContent after chomp
505#****************************************************************************
506sub GetFileContent
507{
508 my ($strFilePath) = @_;
509 my $content;
510 open FILE, "<$strFilePath" or &AUTO_ADJUST::error_handler("$strFilePath: open file error!", __FILE__, __LINE__, 'AAPMC_COPYER::GetFileContent');
511 {
512 local $/;
513 $content = <FILE>;
514 }
515 close FILE;
516 chomp($content);
517 return $content;
518}