[tools][eclipse] Fix eclipse target update error.

This commit is contained in:
armink 2019-04-29 18:17:47 +08:00
parent 8afa24c811
commit 6674e77986
2 changed files with 117 additions and 61 deletions

View File

@ -830,6 +830,10 @@ def GenTargetProject(program = None):
from makefile import TargetMakefile from makefile import TargetMakefile
TargetMakefile(Env) TargetMakefile(Env)
if GetOption('target') == 'eclipse':
from eclipse import TargetEclipse
TargetEclipse(Env)
def EndBuilding(target, program = None): def EndBuilding(target, program = None):
import rtconfig import rtconfig

View File

@ -1,3 +1,14 @@
#
# Copyright (c) 2006-2019, RT-Thread Development Team
#
# SPDX-License-Identifier: Apache-2.0
#
# Change Logs:
# Date Author Notes
# 2019-03-21 Bernard the first version
# 2019-04-15 armink fix project update error
#
import os import os
import sys import sys
import glob import glob
@ -25,6 +36,8 @@ def OSPath(path):
else: else:
return [item.replace('\\', '/') for item in path] return [item.replace('\\', '/') for item in path]
# collect the build source code path and parent path
def CollectPaths(paths): def CollectPaths(paths):
all_paths = [] all_paths = []
@ -87,6 +100,8 @@ def ExcludeFiles(infiles, files):
return exl_files return exl_files
# caluclate the exclude path for project
def ExcludePaths(filepath, paths): def ExcludePaths(filepath, paths):
ret = [] ret = []
@ -106,12 +121,18 @@ def ExcludePaths(filepath, paths):
return ret return ret
def HandleToolOption(tools, env):
project = ProjectInfo(env) def ConverToEclipsePathFormat(path):
if path.startswith('.'):
path = path[1:]
return '"${workspace_loc:/${ProjName}/' + path + '}"'
def HandleToolOption(tools, env, project):
BSP_ROOT = os.path.abspath(env['BSP_ROOT']) BSP_ROOT = os.path.abspath(env['BSP_ROOT'])
CPPDEFINES = project['CPPDEFINES'] CPPDEFINES = project['CPPDEFINES']
paths = ['${ProjDirPath}/' + _make_path_relative(BSP_ROOT, os.path.normpath(i)).replace('\\', '/') for i in project['CPPPATH']] paths = [ConverToEclipsePathFormat(RelativeProjectPath(env, os.path.normpath(i)).replace('\\', '/')) for i in project['CPPPATH']]
for tool in tools: for tool in tools:
if tool.get('id').find('c.compile') != 1: if tool.get('id').find('c.compile') != 1:
@ -157,9 +178,8 @@ def HandleToolOption(tools, env):
items = env['LINKFLAGS'].split(' ') items = env['LINKFLAGS'].split(' ')
if '-T' in items: if '-T' in items:
linker_script = items[items.index('-T') + 1] linker_script = items[items.index('-T') + 1]
linker_script = '${ProjDirPath}/' + linker_script linker_script = ConverToEclipsePathFormat(linker_script)
# print('c.linker.scriptfile')
listOptionValue = option.find('listOptionValue') listOptionValue = option.find('listOptionValue')
if listOptionValue != None: if listOptionValue != None:
listOptionValue.set('value', linker_script) listOptionValue.set('value', linker_script)
@ -174,7 +194,7 @@ def HandleToolOption(tools, env):
return return
def HandleRTTRoot(env): def UpdateProjectStructure(env):
bsp_root = env['BSP_ROOT'] bsp_root = env['BSP_ROOT']
rtt_root = env['RTT_ROOT'] rtt_root = env['RTT_ROOT']
@ -185,62 +205,54 @@ def HandleRTTRoot(env):
# always use '/' path separator # always use '/' path separator
rtt_root = rtt_root.replace('\\', '/') rtt_root = rtt_root.replace('\\', '/')
project = etree.parse('.project') # TODO create the virtual folder
root = project.getroot()
linkedResources = root.find('linkedResources') # project = etree.parse('.project')
if linkedResources == None: # root = project.getroot()
# add linkedResources #
linkedResources = SubElement(root, 'linkedResources') # linkedResources = root.find('linkedResources')
# print('add linkedResources') # if linkedResources == None:
else: # # add linkedResources
links = linkedResources.findall('link') # linkedResources = SubElement(root, 'linkedResources')
# search exist 'rt-thread' virtual folder # # print('add linkedResources')
for link in links: # else:
if link.find('name').text == 'rt-thread': # links = linkedResources.findall('link')
# handle location # # search exist 'rt-thread' virtual folder
to_SubElement = False # for link in links:
location = link.find('location') # if link.find('name').text == 'rt-thread':
location.text = rtt_root # # handle location
# to_SubElement = False
if to_SubElement: # location = link.find('location')
# print('to subelement for virtual folder') # location.text = rtt_root
link = SubElement(linkedResources, 'link') #
name = SubElement(link, 'name') # if to_SubElement:
name.text = 'rt-thread' # # print('to subelement for virtual folder')
type = SubElement(link, 'type') # link = SubElement(linkedResources, 'link')
type.text = '2' # name = SubElement(link, 'name')
location = SubElement(link, 'location') # name.text = 'rt-thread'
location.text = rtt_root # type = SubElement(link, 'type')
# type.text = '2'
out = open('.project', 'w') # location = SubElement(link, 'location')
out.write('<?xml version="1.0" encoding="UTF-8"?>\n') # location.text = rtt_root
xml_indent(root) #
out.write(etree.tostring(root, encoding='utf-8')) # out = open('.project', 'w')
out.close() # 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 TargetEclipse(env): def GenExcluding(env, project):
global source_pattern rtt_root = os.path.abspath(env['RTT_ROOT'])
coll_dirs = CollectPaths(project['DIRS'])
all_paths = [OSPath(path) for path in coll_dirs]
print('Update eclipse setting...') exclude_paths = ExcludePaths(rtt_root, all_paths)
if not os.path.exists('.cproject'):
print('no eclipse CDT project found!')
return
HandleRTTRoot(env)
project = ProjectInfo(env)
all_paths = [OSPath(path) for path in CollectPaths(project['DIRS'])]
# print(all_paths)
bsp_root = os.path.abspath(env['BSP_ROOT'])
exclude_paths = ExcludePaths(bsp_root, all_paths)
paths = exclude_paths paths = exclude_paths
exclude_paths = [] exclude_paths = []
# remove the folder which not has source code by source_pattern
for path in paths: for path in paths:
# add bsp and libcpu folder and not collect source files (too more files) # add bsp and libcpu folder and not collect source files (too more files)
if path.endswith('rt-thread\\bsp') or path.endswith('rt-thread\\libcpu'): if path.endswith('rt-thread\\bsp') or path.endswith('rt-thread\\libcpu'):
@ -251,15 +263,37 @@ def TargetEclipse(env):
if len(set): if len(set):
exclude_paths += [path] exclude_paths += [path]
exclude_paths = [_make_path_relative(bsp_root, path).replace('\\', '/') for path in exclude_paths] exclude_paths = [RelativeProjectPath(env, path).replace('\\', '/') for path in exclude_paths]
env['ExPaths'] = exclude_paths env['ExPaths'] = exclude_paths
all_files = CollectFiles(all_paths, source_pattern) all_files = CollectFiles(all_paths, source_pattern)
src_files = project['FILES'] src_files = project['FILES']
exclude_files = ExcludeFiles(all_files, src_files) exclude_files = ExcludeFiles(all_files, src_files)
exclude_files = [_make_path_relative(bsp_root, file).replace('\\', '/') for file in exclude_files] exclude_files = [RelativeProjectPath(env, file).replace('\\', '/') for file in exclude_files]
env['ExFiles'] = exclude_files env['ExFiles'] = exclude_files
return exclude_paths + exclude_files
def RelativeProjectPath(env, path):
project_root = os.path.abspath(env['BSP_ROOT'])
rtt_root = os.path.abspath(env['RTT_ROOT'])
if path.startswith(project_root):
return _make_path_relative(project_root, path)
if path.startswith(rtt_root):
return 'rt-thread/' + _make_path_relative(rtt_root, path)
# TODO add others folder
print('ERROR: the ' + path + 'not support')
return path
def UpdateCproject(env, project, excluding):
excluding = sorted(excluding)
cproject = etree.parse('.cproject') cproject = etree.parse('.cproject')
@ -267,24 +301,21 @@ def TargetEclipse(env):
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) HandleToolOption(tools, env, project)
sourceEntries = cconfiguration.find('storageModule/configuration/sourceEntries') sourceEntries = cconfiguration.find('storageModule/configuration/sourceEntries')
entry = sourceEntries.find('entry') entry = sourceEntries.find('entry')
if entry != None: if entry != None:
sourceEntries.remove(entry) sourceEntries.remove(entry)
excluding = exclude_paths + exclude_files
excluding = sorted(excluding)
value = '' value = ''
for item in excluding: for item in excluding:
if value == '': if value == '':
value = item value = item
else: else:
value += '|' + item value += '|' + item
excluding = value
SubElement(sourceEntries, 'entry', {'excluding': excluding, 'flags': 'VALUE_WORKSPACE_PATH|RESOLVED', 'kind':'sourcePath', 'name':""}) SubElement(sourceEntries, 'entry', {'excluding': value, 'flags': 'VALUE_WORKSPACE_PATH|RESOLVED', 'kind':'sourcePath', 'name':""})
# write back to .cproject # write back to .cproject
out = open('.cproject', 'w') out = open('.cproject', 'w')
@ -294,6 +325,27 @@ def TargetEclipse(env):
out.write(etree.tostring(root, encoding='utf-8')) out.write(etree.tostring(root, encoding='utf-8'))
out.close() out.close()
def TargetEclipse(env):
global source_pattern
print('Update eclipse setting...')
if not os.path.exists('.cproject'):
print('no eclipse CDT project found!')
return
project = ProjectInfo(env)
# update the project file structure info on '.project' file
UpdateProjectStructure(env)
# generate the exclude paths and files
excluding = GenExcluding(env, project)
# update the project configuration on '.cproject' file
UpdateCproject(env, project, excluding)
print('done!') print('done!')
return return