1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3 4# Copyright (c) 2020-2021 Huawei Device Co., Ltd. 5# 6# HDF is dual licensed: you can use it either under the terms of 7# the GPL, or the BSD license, at your option. 8# See the LICENSE file in the root of this repository for complete details. 9 10 11import os 12import re 13 14from hdf_tool_settings import HdfToolSettings 15import hdf_utils 16 17 18class HdfLiteMkFile(object): 19 def __init__(self, root): 20 self.file_path = hdf_utils.get_hdf_lite_mk_path(root) 21 self.orig_exist = False 22 if os.path.exists(self.file_path): 23 self.lines = hdf_utils.read_file_lines(self.file_path) 24 self.orig_exist = True 25 else: 26 self.lines = [] 27 self.end_line_pattern = r'LITEOS_BASELIB\s*[+]=\s*--no-whole-archive' 28 self.vendor_pattern = r'include.*hdf_vendor[.]mk' 29 self.target_format = r'include .*vendor/%s/.*/hdf_vendor[.]mk' 30 self.end_line_index = -1 31 self.target_line_index = -1 32 self.current_vendor_pattern = r'^\s*include.*/device/(.*)/lite.mk' 33 34 def _save(self): 35 if self.orig_exist: 36 hdf_utils.write_file(self.file_path, ''.join(self.lines)) 37 38 def _find_all_vendors(self, current_vendor): 39 vendor_lines = [] 40 for index, line in enumerate(self.lines): 41 if self.target_line_index == -1: 42 if re.search(self.target_format % current_vendor, line): 43 self.target_line_index = index 44 continue 45 if self.end_line_index == -1: 46 if re.search(self.end_line_pattern, line): 47 self.end_line_index = index 48 continue 49 if re.search(self.vendor_pattern, line): 50 vendor_lines.append(index) 51 return vendor_lines 52 53 def _comment_other_vendors(self, other_vendors): 54 for index in other_vendors: 55 if not hdf_utils.is_commented_line(self.lines[index], '#'): 56 self.lines[index] = '# %s' % self.lines[index] 57 58 def set_vendor(self, current_vendor): 59 other_vendors = self._find_all_vendors(current_vendor) 60 self._comment_other_vendors(other_vendors) 61 drivers_path = HdfToolSettings().get_drivers_path() 62 new_line = \ 63 'include $(LITEOSTOPDIR)/../../vendor/%s/%s/hdf_vendor.mk\n' % \ 64 (current_vendor, drivers_path) 65 if self.target_line_index != -1: 66 self.lines[self.target_line_index] = new_line 67 elif self.end_line_index != -1: 68 self.lines.insert(self.end_line_index, new_line) 69 else: 70 self.lines.append(new_line) 71 self._save() 72 73 def get_current_vendor(self): 74 for line in self.lines: 75 match_obj = re.search(self.current_vendor_pattern, line) 76 if match_obj: 77 vendor_drivers_path = match_obj.group(1) 78 parts = vendor_drivers_path.split('/') 79 valid_parts = [part for part in parts if part] 80 if valid_parts: 81 return valid_parts[0] 82 return '' 83