diff --git a/tools/ide_exporter.py b/tools/ide_exporter.py index 543164acda..c0886d7f55 100755 --- a/tools/ide_exporter.py +++ b/tools/ide_exporter.py @@ -37,7 +37,6 @@ # ############################################################################ -from __future__ import print_function import os import subprocess import re @@ -73,80 +72,110 @@ optional arguments: -d, --dump Dump project structure tree """ -IAR = 'iar' -UVISION_GCC = 'uvision_gcc' -UVISION_ARMCC = 'uvision_armcc' +IAR = "iar" +UVISION_GCC = "uvision_gcc" +UVISION_ARMCC = "uvision_armcc" -COMPILE_PREFIX_LIST = ('CC: ', 'AS: ', 'CXX:') -LIB_PREFIX_LIST = ('AR: ') -LINK_PREFIX_LIST = ('LD: ') -MAKE_ENTER_DIR = 'Entering directory' +COMPILE_PREFIX_LIST = ("CC: ", "AS: ", "CXX:") +LIB_PREFIX_LIST = "AR: " +LINK_PREFIX_LIST = "LD: " +MAKE_ENTER_DIR = "Entering directory" PREFIX_LEN = 4 -IAR_EXT_REMAP = {r'gnu/(\w+)\.S$' : r'iar/\g<1>.S'} -ARMCC_EXT_REMAP = {r'gnu/(\w+)\.S$' : r'armcc/\g<1>.S', - r'(\w+)\.a$': r'\g<1>.lib'} +IAR_EXT_REMAP = {r"gnu/(\w+)\.S$": r"iar/\g<1>.S"} +ARMCC_EXT_REMAP = {r"gnu/(\w+)\.S$": r"armcc/\g<1>.S", r"(\w+)\.a$": r"\g<1>.lib"} UVISION_GCC_EXT_REMAP = {} # file ext to FileTye in uVision project -UVISION_FILE_TYPE_MAP = {'.c': '1', '.S' : '1', '.cxx' : '8', '.lib' : '4', '.a' : '4'} +UVISION_FILE_TYPE_MAP = {".c": "1", ".S": "1", ".cxx": "8", ".lib": "4", ".a": "4"} # tags convention: tag[0] = root_tags, create if doesn't exist # tag[1] = (sub_tag,) tag without text, create new # tag[2] = (leaf_tag,) with text, create new -IAR_PRJ_SETTINGS = {'group_tags' : ('', ('group',), ('name', )), - 'file_tags' :('', ('file',), ('name', )), - 'rel_base' : '$PROJ_DIR$/', - 'cleared_nodes' : ('group', 'file', ), - 'include_pnodes' : (".//*[name='CCIncludePath2']", ".//*[name='AUserIncludes']"), - 'include_tag' : 'state', - 'output_path' : {'exe' : 'Obj', 'obj' : 'Obj', 'lst' : 'Lst'}, - 'ext_remap' : IAR_EXT_REMAP,} +IAR_PRJ_SETTINGS = { + "group_tags": ("", ("group",), ("name",)), + "file_tags": ("", ("file",), ("name",)), + "rel_base": "$PROJ_DIR$/", + "cleared_nodes": ( + "group", + "file", + ), + "include_pnodes": (".//*[name='CCIncludePath2']", ".//*[name='AUserIncludes']"), + "include_tag": "state", + "output_path": {"exe": "Obj", "obj": "Obj", "lst": "Lst"}, + "ext_remap": IAR_EXT_REMAP, +} -IAR_WSP_SETTINGS = {'group_tags': ('',), - 'file_tags':('', ('project', ), ('path',)), - 'rel_base':'$WS_DIR$/', - 'cleared_nodes': ('project', )} +IAR_WSP_SETTINGS = { + "group_tags": ("",), + "file_tags": ("", ("project",), ("path",)), + "rel_base": "$WS_DIR$/", + "cleared_nodes": ("project",), +} -UVISION_ARMCC_PRJ_SETTINGS = {'root_group':'', - 'group_tags': ('.//Targets/Target/Groups', ('Group', ), ('GroupName', )), - 'file_tags':('Files', ('File', ), ('FileName', 'FileType', 'FilePath', )), - 'rel_base':'', - 'cleared_nodes': ('.//Group', ), - 'include_pnodes' : ".//VariousControls/IncludePath", - 'output_path' : {'exe' : 'Obj', 'obj' : 'Obj', 'lst' : 'Lst'}, - 'ext_remap' : ARMCC_EXT_REMAP, - 'uv_file_type' : UVISION_FILE_TYPE_MAP} +UVISION_ARMCC_PRJ_SETTINGS = { + "root_group": "", + "group_tags": (".//Targets/Target/Groups", ("Group",), ("GroupName",)), + "file_tags": ( + "Files", + ("File",), + ( + "FileName", + "FileType", + "FilePath", + ), + ), + "rel_base": "", + "cleared_nodes": (".//Group",), + "include_pnodes": ".//VariousControls/IncludePath", + "output_path": {"exe": "Obj", "obj": "Obj", "lst": "Lst"}, + "ext_remap": ARMCC_EXT_REMAP, + "uv_file_type": UVISION_FILE_TYPE_MAP, +} -UVISION_GCC_PRJ_SETTINGS = {'root_group':'', - 'group_tags': ('.//Targets/Target/Groups', ('Group', ), ('GroupName', )), - 'file_tags':('Files', ('File', ), ('FileName', 'FileType', 'FilePath', )), - 'rel_base':'', - 'cleared_nodes': ('.//Group', ), - 'include_pnodes' : ".//VariousControls/IncludePath", - 'saved_tags' : ('.//FileOption', ), - 'output_path' : {'exe' : 'Obj', 'obj' : 'Obj', 'lst' : 'Lst'}, - 'ext_remap' : UVISION_GCC_EXT_REMAP, - 'uv_file_type' : UVISION_FILE_TYPE_MAP, - 'c_misc' : ('.//Carm', '-fno-builtin -Wall -Wstrict-prototypes -Wshadow -Wundef -g'), - 'cxx_misc' : ('.//Carm', '-fno-builtin -fno-exceptions -fcheck-new -fno-rtti -Wall -Wshadow -Wundef -g'), - 'ld_misc' : ('.//LDarm', '--entry=__start -lgcc'), - 'cxx_def' : ('.//Carm', ''),} +UVISION_GCC_PRJ_SETTINGS = { + "root_group": "", + "group_tags": (".//Targets/Target/Groups", ("Group",), ("GroupName",)), + "file_tags": ( + "Files", + ("File",), + ( + "FileName", + "FileType", + "FilePath", + ), + ), + "rel_base": "", + "cleared_nodes": (".//Group",), + "include_pnodes": ".//VariousControls/IncludePath", + "saved_tags": (".//FileOption",), + "output_path": {"exe": "Obj", "obj": "Obj", "lst": "Lst"}, + "ext_remap": UVISION_GCC_EXT_REMAP, + "uv_file_type": UVISION_FILE_TYPE_MAP, + "c_misc": (".//Carm", "-fno-builtin -Wall -Wstrict-prototypes -Wshadow -Wundef -g"), + "cxx_misc": ( + ".//Carm", + "-fno-builtin -fno-exceptions -fcheck-new -fno-rtti -Wall -Wshadow -Wundef -g", + ), + "ld_misc": (".//LDarm", "--entry=__start -lgcc"), + "cxx_def": (".//Carm", ""), +} -UVISION_WSP_SETTINGS = {'group_tags': ('',), - 'file_tags':('', ('project', ), ('PathAndName', )), - 'rel_base':'', - 'cleared_nodes': ('project', )} +UVISION_WSP_SETTINGS = { + "group_tags": ("",), + "file_tags": ("", ("project",), ("PathAndName",)), + "rel_base": "", + "cleared_nodes": ("project",), +} -LIB_EXTS = ('.a', '.lib') -ASM_EXTS = ('.s', '.S') +LIB_EXTS = (".a", ".lib") +ASM_EXTS = (".s", ".S") def get_common_dir(dir_list): - """ Get common parent directory of a given directory list - """ + """Get common parent directory of a given directory list""" com_dir = dir_list[0] found = False while found == False: @@ -160,7 +189,7 @@ def get_common_dir(dir_list): if found: return com_dir else: - return "/" #return root + return "/" # return root class SourceInfo(object): @@ -171,7 +200,8 @@ class SourceInfo(object): include: List of including dir in compiled command flags: other compiled flags """ - def __init__(self, src, include=None, flags=''): + + def __init__(self, src, include=None, flags=""): self.include = [] if include is not None: self.include = include @@ -181,23 +211,22 @@ class SourceInfo(object): @staticmethod def get_common_src_dir(sinfo_list): - """ Get Common directory from list of source code - """ + """Get Common directory from list of source code""" source_list = [info.src for info in sinfo_list] com_dir = get_common_dir(source_list) return com_dir @staticmethod def get_including_set(sinfo_list): - """ Get including set from list of source code - """ + """Get including set from list of source code""" include_set = set() for sinfo in sinfo_list: for inc in sinfo.include: - if inc != '': + if inc != "": include_set.add(inc) return include_set + class IdeProject(object): """Base IDE project class. @@ -222,56 +251,55 @@ class IdeProject(object): self.root = None self.ewp_ET = None self.settings = {} - self.rel_base = '' + self.rel_base = "" if settings is not None: self.settings = settings self.use_gcc = use_gcc - self.rel_base = self.settings.get('rel_base', '') - self.saved_nodes = {} #some inside nodes need to save before clear all sources + self.rel_base = self.settings.get("rel_base", "") + self.saved_nodes = {} # some inside nodes need to save before clear all sources try: - #Read template project xml structure - parser = ET.XMLParser(remove_blank_text=True) # use parser to make pretty print works + # Read template project xml structure + parser = ET.XMLParser( + remove_blank_text=True + ) # use parser to make pretty print works self.ewp_ET = ET.parse(proj, parser) self.root = self.ewp_ET.getroot() # Save some template nodes before clear - for tag in self.settings.get('saved_tags', []): + for tag in self.settings.get("saved_tags", []): n = self.root.find(tag) self.saved_nodes[tag] = deepcopy(n) - self.clear_src_nodes() # Clear all source node in template file + self.clear_src_nodes() # Clear all source node in template file except Exception as e: print("ERR: {0}".format(str(e))) raise Exception("Can't init IdeProject object") def get_relpath(self, dest): - """ Get relative path from its base directory - """ + """Get relative path from its base directory""" return self.rel_base + os.path.relpath(dest, self.proj_dir) def get_output_dir(self): - """ - """ - out_paths = self.settings.get('output_path', {}) - return out_paths.get('exe', '') + """""" + out_paths = self.settings.get("output_path", {}) + return out_paths.get("exe", "") def get_obj_dir(self): - """ - """ - out_paths = self.settings.get('output_path', {}) - return out_paths.get('obj', '') + """""" + out_paths = self.settings.get("output_path", {}) + return out_paths.get("obj", "") def get_lst_dir(self): - """ - """ - out_paths = self.settings.get('output_path', {}) - return out_paths.get('lst', '') + """""" + out_paths = self.settings.get("output_path", {}) + return out_paths.get("lst", "") def write(self, ofile): - """ Write etree to file - """ - self.ewp_ET.write(ofile, pretty_print=True, xml_declaration=True, encoding='UTF-8') + """Write etree to file""" + self.ewp_ET.write( + ofile, pretty_print=True, xml_declaration=True, encoding="UTF-8" + ) def remove_nodes(self, element, remove_list): """Delete nodes in list from the xlm tree @@ -288,7 +316,7 @@ class IdeProject(object): try: for node in remove_list: p = element.find(node) - while p is not None: + while p is not None: c = p.getparent() c.remove(p) p = element.find(node) @@ -296,28 +324,27 @@ class IdeProject(object): print(str(e)) def clear_src_nodes(self): - """ Remove all predefined node in settings from its etree - """ - self.remove_nodes(self.root, self.settings.get('cleared_nodes', [])) + """Remove all predefined node in settings from its etree""" + self.remove_nodes(self.root, self.settings.get("cleared_nodes", [])) def make_nodes(self, parent_node, tags, *args): - """ Create node(s) by using tag convention - Return most inner parent nodes + """Create node(s) by using tag convention + Return most inner parent nodes """ if parent_node is None: parent_node = self.root head = None root = None - #print "Create tags: ", tags + # print "Create tags: ", tags if len(tags) == 3: # Check root, create if not exist root_tag = tags[0] - if root_tag != '': + if root_tag != "": root = parent_node.find(root_tag) if root is None: - root = ET.SubElement(parent_node, root_tag.split('/')[-1]) + root = ET.SubElement(parent_node, root_tag.split("/")[-1]) else: root = parent_node @@ -339,11 +366,11 @@ class IdeProject(object): if head is not None: root.append(head) else: - raise Exception('Wrong tag convention') + raise Exception("Wrong tag convention") return p_node def make_group(self, parent_node, *args): - """ Create group of source/lib tags + """Create group of source/lib tags Tags info are get from settings Args: parent_node : @@ -351,18 +378,18 @@ class IdeProject(object): return: Return group node """ - tags = self.settings.get('group_tags', []) + tags = self.settings.get("group_tags", []) return self.make_nodes(parent_node, tags, *args) def make_file(self, parent_node, *args): - """ Create group of source/lib tags - Tags info are get from settings + """Create group of source/lib tags + Tags info are get from settings """ - tags = self.settings.get('file_tags', []) + tags = self.settings.get("file_tags", []) return self.make_nodes(parent_node, tags, *args) def make_src_nodes(self, source, group=None, parent_node=None): - """ Create xlm nodes for list of source file + """Create xlm nodes for list of source file Args: sources: list of SourceInfo @@ -374,7 +401,7 @@ class IdeProject(object): pass def make_include(self, sources, parent_node=None): - """ Create including nodes from source info for project + """Create including nodes from source info for project Args: sources: list of SourceInfo @@ -385,7 +412,7 @@ class IdeProject(object): pass def make_output_dir(self, target): - """ Update output directory setting for project + """Update output directory setting for project Args: target: project output target name @@ -393,23 +420,28 @@ class IdeProject(object): """ pass - def add_misc(self, mtype, misc=''): + + def add_misc(self, mtype, misc=""): pass + def add_define(self, dtype, symbols): pass + def set_link_libs(self, lib_dir, libs): pass + def set_mcu(self, mcu): pass + def set_core(self, core): pass @staticmethod def factory(objtype, xml_file, out_dir=None): - """ Factory to create obj by derived type - """ + """Factory to create obj by derived type""" return objtype(xml_file, out_dir=out_dir) + class IARWorkspace(IdeProject): """IAR workspace class. @@ -418,20 +450,23 @@ class IARWorkspace(IdeProject): Attributes: """ + def __init__(self, proj, out_dir=None): super(IARWorkspace, self).__init__(proj, IAR_WSP_SETTINGS, out_dir) + class IARProject(IdeProject): """IAR project class. Add some specific logics to create source, include and output setting """ + def __init__(self, proj, settings=IAR_PRJ_SETTINGS, out_dir=None): super(IARProject, self).__init__(proj, settings, out_dir) def make_include(self, sources, parent_node=None): - """ Create including nodes from source info for project + """Create including nodes from source info for project IAR sample including nodes