Browse Source

Merge pull request #2792 from armink/fix_eclipse

Fix eclipse
Bernard Xiong 6 years ago
parent
commit
559f94f879
2 changed files with 64 additions and 57 deletions
  1. 11 5
      tools/building.py
  2. 53 52
      tools/eclipse.py

+ 11 - 5
tools/building.py

@@ -167,15 +167,20 @@ def PrepareBuilding(env, root_directory, has_libcpu=False, remove_components = [
                       default = False,
                       default = False,
                       help = 'make distribution for RT-Thread Studio IDE')
                       help = 'make distribution for RT-Thread Studio IDE')
     AddOption('--project-path',
     AddOption('--project-path',
-                      dest = 'make-project-path',
+                      dest = 'project-path',
                       type = 'string',
                       type = 'string',
                       default = False,
                       default = False,
                       help = 'set dist-ide project output path')
                       help = 'set dist-ide project output path')
     AddOption('--project-name',
     AddOption('--project-name',
-                      dest = 'make-project-name',
+                      dest = 'project-name',
                       type = 'string',
                       type = 'string',
                       default = False,
                       default = False,
                       help = 'set project name')
                       help = 'set project name')
+    AddOption('--reset-project-config',
+                      dest = 'reset-project-config',
+                      action = 'store_true',
+                      default = False,
+                      help = 'reset the project configurations to default')
     AddOption('--cscope',
     AddOption('--cscope',
                       dest = 'cscope',
                       dest = 'cscope',
                       action = 'store_true',
                       action = 'store_true',
@@ -847,7 +852,8 @@ def GenTargetProject(program = None):
 
 
     if GetOption('target') == 'eclipse':
     if GetOption('target') == 'eclipse':
         from eclipse import TargetEclipse
         from eclipse import TargetEclipse
-        TargetEclipse(Env)
+        TargetEclipse(Env, GetOption('reset-project-config'), GetOption('project-name'))
+
 
 
 def EndBuilding(target, program = None):
 def EndBuilding(target, program = None):
     import rtconfig
     import rtconfig
@@ -882,8 +888,8 @@ def EndBuilding(target, program = None):
         need_exit = True
         need_exit = True
     if GetOption('make-dist-ide') and program != None:
     if GetOption('make-dist-ide') and program != None:
         from mkdist import MkDist
         from mkdist import MkDist
-        project_path = GetOption('make-project-path')
-        project_name = GetOption('make-project-name')
+        project_path = GetOption('project-path')
+        project_name = GetOption('project-name')
 
 
         if not isinstance(project_path, str) or len(project_path) == 0 :
         if not isinstance(project_path, str) or len(project_path) == 0 :
             print("\nwarning : --project-path=your_project_path parameter is required.")
             print("\nwarning : --project-path=your_project_path parameter is required.")

+ 53 - 52
tools/eclipse.py

@@ -102,15 +102,15 @@ def ExcludeFiles(infiles, files):
 
 
 
 
 # caluclate the exclude path for project
 # caluclate the exclude path for project
-def ExcludePaths(filepath, paths):
+def ExcludePaths(rootpath, paths):
     ret = []
     ret = []
 
 
-    files = os.listdir(filepath)
+    files = os.listdir(rootpath)
     for file in files:
     for file in files:
         if file.startswith('.'):
         if file.startswith('.'):
             continue
             continue
 
 
-        fullname = os.path.join(filepath, file)
+        fullname = os.path.join(rootpath, file)
 
 
         if os.path.isdir(fullname):
         if os.path.isdir(fullname):
             # print(fullname)
             # print(fullname)
@@ -128,7 +128,7 @@ def ConverToEclipsePathFormat(path):
     return '"${workspace_loc:/${ProjName}/' + path + '}"'
     return '"${workspace_loc:/${ProjName}/' + path + '}"'
 
 
 
 
-def HandleToolOption(tools, env, project):
+def HandleToolOption(tools, env, project, reset):
     BSP_ROOT = os.path.abspath(env['BSP_ROOT'])
     BSP_ROOT = os.path.abspath(env['BSP_ROOT'])
 
 
     CPPDEFINES = project['CPPDEFINES']
     CPPDEFINES = project['CPPDEFINES']
@@ -143,7 +143,11 @@ def HandleToolOption(tools, env, project):
                     include_paths = option.findall('listOptionValue')
                     include_paths = option.findall('listOptionValue')
                     project_paths = []
                     project_paths = []
                     for item in include_paths:
                     for item in include_paths:
-                        project_paths += [item.get('value')]
+                        if reset is True:
+                            # clean all old configuration
+                            option.remove(item)
+                        else:
+                            project_paths += [item.get('value')]
 
 
                     if len(project_paths) > 0:
                     if len(project_paths) > 0:
                         cproject_paths = set(paths) - set(project_paths)
                         cproject_paths = set(paths) - set(project_paths)
@@ -159,7 +163,11 @@ def HandleToolOption(tools, env, project):
                     defs = option.findall('listOptionValue')
                     defs = option.findall('listOptionValue')
                     project_defs = []
                     project_defs = []
                     for item in defs:
                     for item in defs:
-                        project_defs += [item.get('value')]
+                        if reset is True:
+                            # clean all old configuration
+                            option.remove(item)
+                        else:
+                            project_defs += [item.get('value')]
                     if len(project_defs) > 0:
                     if len(project_defs) > 0:
                         cproject_defs = set(CPPDEFINES) - set(project_defs)
                         cproject_defs = set(CPPDEFINES) - set(project_defs)
                     else:
                     else:
@@ -194,61 +202,54 @@ def HandleToolOption(tools, env, project):
 
 
     return
     return
 
 
-def UpdateProjectStructure(env):
+
+def UpdateProjectStructure(env, prj_name):
     bsp_root = env['BSP_ROOT']
     bsp_root = env['BSP_ROOT']
     rtt_root = env['RTT_ROOT']
     rtt_root = env['RTT_ROOT']
 
 
-    if not rtt_root.startswith(bsp_root):
-        to_SubElement = True
-        # print('handle virtual root')
+    project = etree.parse('.project')
+    root = project.getroot()
 
 
-        # always use '/' path separator
-        rtt_root = rtt_root.replace('\\', '/')
+    if rtt_root.startswith(bsp_root):
+        linkedResources = root.find('linkedResources')
+        if linkedResources == None:
+            linkedResources = SubElement(root, 'linkedResources')
 
 
-        # TODO create the virtual folder
+        links = linkedResources.findall('link')
+        # delete all RT-Thread folder links
+        for link in links:
+            if link.find('name').text.startswith('rt-thread'):
+                linkedResources.remove(link)
 
 
-#         project = etree.parse('.project')
-#         root = project.getroot()
-#
-#         linkedResources = root.find('linkedResources')
-#         if linkedResources == None:
-#             # add linkedResources
-#             linkedResources = SubElement(root, 'linkedResources')
-#             # print('add linkedResources')
-#         else:
-#             links = linkedResources.findall('link')
-#             # search exist 'rt-thread' virtual folder
-#             for link in links:
-#                 if link.find('name').text == 'rt-thread':
-#                     # handle location
-#                     to_SubElement = False
-#                     location = link.find('location')
-#                     location.text = rtt_root
-#
-#         if to_SubElement:
-#             # print('to subelement for virtual folder')
-#             link = SubElement(linkedResources, 'link')
-#             name = SubElement(link, 'name')
-#             name.text = 'rt-thread'
-#             type = SubElement(link, 'type')
-#             type.text = '2'
-#             location = SubElement(link, 'location')
-#             location.text = rtt_root
-#
-#         out = open('.project', 'w')
-#         out.write('<?xml version="1.0" encoding="UTF-8"?>\n')
-#         xml_indent(root)
-#         out.write(etree.tostring(root, encoding='utf-8'))
-#         out.close()
+    if prj_name:
+        name = root.find('name')
+        if name == None:
+            name = SubElement(root, 'name')
+        name.text = prj_name
+
+    out = open('.project', 'w')
+    out.write('<?xml version="1.0" encoding="UTF-8"?>\n')
+    xml_indent(root)
+    out.write(etree.tostring(root, encoding = 'utf-8'))
+    out.close()
 
 
     return
     return
 
 
 def GenExcluding(env, project):
 def GenExcluding(env, project):
     rtt_root = os.path.abspath(env['RTT_ROOT'])
     rtt_root = os.path.abspath(env['RTT_ROOT'])
+    bsp_root = os.path.abspath(env['BSP_ROOT'])
     coll_dirs = CollectPaths(project['DIRS'])
     coll_dirs = CollectPaths(project['DIRS'])
     all_paths = [OSPath(path) for path in coll_dirs]
     all_paths = [OSPath(path) for path in coll_dirs]
 
 
-    exclude_paths = ExcludePaths(rtt_root, all_paths)
+    if bsp_root.startswith(rtt_root):
+        # bsp folder is in the RT-Thread root folder, such as the RT-Thread source code on GitHub
+        exclude_paths = ExcludePaths(rtt_root, all_paths)
+    elif rtt_root.startswith(bsp_root):
+        # RT-Thread root folder is in the bsp folder, such as project folder which generate by 'scons --dist' cmd
+        exclude_paths = ExcludePaths(bsp_root, all_paths)
+    else:
+        exclude_paths = ExcludePaths(rtt_root, all_paths)
+        exclude_paths += ExcludePaths(bsp_root, all_paths)
 
 
     paths = exclude_paths
     paths = exclude_paths
     exclude_paths = []
     exclude_paths = []
@@ -292,7 +293,7 @@ def RelativeProjectPath(env, path):
     return path
     return path
 
 
 
 
-def UpdateCproject(env, project, excluding):
+def UpdateCproject(env, project, excluding, reset):
     excluding = sorted(excluding)
     excluding = sorted(excluding)
 
 
     cproject = etree.parse('.cproject')
     cproject = etree.parse('.cproject')
@@ -301,7 +302,7 @@ def UpdateCproject(env, project, excluding):
     cconfigurations = root.findall('storageModule/cconfiguration')
     cconfigurations = root.findall('storageModule/cconfiguration')
     for cconfiguration in cconfigurations:
     for cconfiguration in cconfigurations:
         tools = cconfiguration.findall('storageModule/configuration/folderInfo/toolChain/tool')
         tools = cconfiguration.findall('storageModule/configuration/folderInfo/toolChain/tool')
-        HandleToolOption(tools, env, project)
+        HandleToolOption(tools, env, project, reset)
 
 
         sourceEntries = cconfiguration.find('storageModule/configuration/sourceEntries')
         sourceEntries = cconfiguration.find('storageModule/configuration/sourceEntries')
         entry = sourceEntries.find('entry')
         entry = sourceEntries.find('entry')
@@ -326,7 +327,7 @@ def UpdateCproject(env, project, excluding):
     out.close()
     out.close()
 
 
 
 
-def TargetEclipse(env):
+def TargetEclipse(env, reset = False, prj_name = None):
     global source_pattern
     global source_pattern
 
 
     print('Update eclipse setting...')
     print('Update eclipse setting...')
@@ -338,13 +339,13 @@ def TargetEclipse(env):
     project = ProjectInfo(env)
     project = ProjectInfo(env)
 
 
     # update the project file structure info on '.project' file
     # update the project file structure info on '.project' file
-    UpdateProjectStructure(env)
+    UpdateProjectStructure(env, prj_name)
 
 
     # generate the exclude paths and files
     # generate the exclude paths and files
     excluding = GenExcluding(env, project)
     excluding = GenExcluding(env, project)
 
 
     # update the project configuration on '.cproject' file
     # update the project configuration on '.cproject' file
-    UpdateCproject(env, project, excluding)
+    UpdateCproject(env, project, excluding, reset)
 
 
     print('done!')
     print('done!')