rt-thread-official/tools/eclipse.py

432 lines
15 KiB
Python
Raw Normal View History

#
# 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
#
2019-03-21 23:04:37 +08:00
import os
import sys
import glob
from utils import *
from utils import _make_path_relative
from utils import xml_indent
import xml.etree.ElementTree as etree
from xml.etree.ElementTree import SubElement
source_pattern = ['*.c', '*.cpp', '*.cxx', '*.s', '*.S', '*.asm']
def OSPath(path):
import platform
if type(path) == type('str'):
if platform.system() == 'Windows':
return path.replace('/', '\\')
else:
return path.replace('\\', '/')
else:
if platform.system() == 'Windows':
return [item.replace('/', '\\') for item in path]
else:
return [item.replace('\\', '/') for item in path]
# collect the build source code path and parent path
2019-03-21 23:04:37 +08:00
def CollectPaths(paths):
all_paths = []
def ParentPaths(path):
ret = os.path.dirname(path)
if ret == path or ret == '':
return []
return [ret] + ParentPaths(ret)
for path in paths:
# path = os.path.abspath(path)
path = path.replace('\\', '/')
all_paths = all_paths + [path] + ParentPaths(path)
all_paths = list(set(all_paths))
return sorted(all_paths)
'''
Collect all of files under paths
'''
def CollectFiles(paths, pattern):
files = []
for path in paths:
if type(pattern) == type(''):
files = files + glob.glob(path + '/' + pattern)
else:
for item in pattern:
# print('--> %s' % (path + '/' + item))
files = files + glob.glob(path + '/' + item)
return sorted(files)
def CollectAllFilesinPath(path, pattern):
files = []
for item in pattern:
files += glob.glob(path + '/' + item)
list = os.listdir(path)
if len(list):
for item in list:
if item.startswith('.'):
continue
if item == 'bsp':
continue
if os.path.isdir(os.path.join(path, item)):
files = files + CollectAllFilesinPath(os.path.join(path, item), pattern)
return files
'''
Exclude files from infiles
'''
def ExcludeFiles(infiles, files):
in_files = set([OSPath(file) for file in infiles])
exl_files = set([OSPath(file) for file in files])
exl_files = in_files - exl_files
return exl_files
# caluclate the exclude path for project
def ExcludePaths(rootpath, paths):
2019-03-21 23:04:37 +08:00
ret = []
files = os.listdir(rootpath)
2019-03-21 23:04:37 +08:00
for file in files:
if file.startswith('.'):
continue
fullname = os.path.join(rootpath, file)
2019-03-21 23:04:37 +08:00
if os.path.isdir(fullname):
# print(fullname)
if not fullname in paths:
ret = ret + [fullname]
else:
ret = ret + ExcludePaths(fullname, paths)
return ret
def ConverToEclipsePathFormat(path):
if path.startswith('.'):
path = path[1:]
return '"${workspace_loc:/${ProjName}/' + path + '}"'
def HandleToolOption(tools, env, project, reset):
2019-03-21 23:04:37 +08:00
BSP_ROOT = os.path.abspath(env['BSP_ROOT'])
CPPDEFINES = project['CPPDEFINES']
paths = [ConverToEclipsePathFormat(RelativeProjectPath(env, os.path.normpath(i)).replace('\\', '/')) for i in project['CPPPATH']]
2019-03-21 23:04:37 +08:00
for tool in tools:
if tool.get('id').find('c.compile') != 1:
options = tool.findall('option')
include_paths_option = None
include_files_option = None
defs_option = None
# find all compile options
2019-03-21 23:04:37 +08:00
for option in options:
2019-08-05 18:00:20 +08:00
if option.get('id').find('c.compiler.include.paths') != -1 or option.get('id').find('c.compiler.option.includepaths') != -1:
include_paths_option = option
elif option.get('id').find('c.compiler.include.files') != -1 or option.get('id').find('c.compiler.option.includefiles') != -1 :
include_files_option = option
elif option.get('id').find('c.compiler.defs') != -1 or option.get('id').find('c.compiler.option.definedsymbols') != -1:
defs_option = option
# change the inclue path
if include_paths_option is not None :
option = include_paths_option
# find all of paths in this project
include_paths = option.findall('listOptionValue')
project_paths = []
for item in include_paths:
if reset is True:
# clean all old configuration
option.remove(item)
2019-03-21 23:04:37 +08:00
else:
project_paths += [item.get('value')]
if len(project_paths) > 0:
cproject_paths = set(paths) - set(project_paths)
else:
cproject_paths = paths
# print('c.compiler.include.paths')
cproject_paths = sorted(cproject_paths)
for item in cproject_paths:
SubElement(option, 'listOptionValue', {'builtIn': 'false', 'value': item})
# change the inclue files (default) or definitions
if include_files_option is not None:
option = include_files_option
file_header = '''
#ifndef RTCONFIG_PREINC_H__
#define RTCONFIG_PREINC_H__
/* Automatically generated file; DO NOT EDIT. */
/* RT-Thread Configuration */
'''
file_tail = '\n#endif /*RTCONFIG_PREINC_H__*/\n'
rtt_pre_inc_item = '"${workspace_loc:/${ProjName}/rtconfig_preinc.h}"'
# save the CPPDEFINES in to rtconfig_preinc.h
with open('rtconfig_preinc.h', mode = 'w+') as f:
f.write(file_header)
for cppdef in CPPDEFINES:
f.write("#define " + cppdef + '\n')
f.write(file_tail)
# change the c.compiler.include.files
files = option.findall('listOptionValue')
find_ok = False
for item in files:
if item.get('value') == rtt_pre_inc_item:
find_ok = True
break
if find_ok is False:
SubElement(option, 'listOptionValue', {'builtIn': 'false', 'value': rtt_pre_inc_item})
elif defs_option is not None :
option = defs_option
defs = option.findall('listOptionValue')
project_defs = []
for item in defs:
if reset is True:
# clean all old configuration
option.remove(item)
2019-03-21 23:04:37 +08:00
else:
project_defs += [item.get('value')]
if len(project_defs) > 0:
cproject_defs = set(CPPDEFINES) - set(project_defs)
else:
cproject_defs = CPPDEFINES
2019-03-21 23:04:37 +08:00
# print('c.compiler.defs')
cproject_defs = sorted(cproject_defs)
for item in cproject_defs:
SubElement(option, 'listOptionValue', {'builtIn': 'false', 'value': item})
2019-03-21 23:04:37 +08:00
if tool.get('id').find('c.linker') != -1:
options = tool.findall('option')
for option in options:
# update linker script config
2019-03-21 23:04:37 +08:00
if option.get('id').find('c.linker.scriptfile') != -1:
linker_script = 'link.lds'
items = env['LINKFLAGS'].split(' ')
if '-T' in items:
linker_script = items[items.index('-T') + 1]
linker_script = ConverToEclipsePathFormat(linker_script)
2019-03-21 23:04:37 +08:00
listOptionValue = option.find('listOptionValue')
if listOptionValue != None:
listOptionValue.set('value', linker_script)
else:
SubElement(option, 'listOptionValue', {'builtIn': 'false', 'value': linker_script})
2019-08-05 18:00:20 +08:00
# scriptfile in stm32cubeIDE
if option.get('id').find('c.linker.option.script') != -1:
items = env['LINKFLAGS'].split(' ')
if '-T' in items:
linker_script = ConverToEclipsePathFormat(items[items.index('-T') + 1]).strip('"')
option.set('value',linker_script)
# update nostartfiles config
2019-03-21 23:04:37 +08:00
if option.get('id').find('c.linker.nostart') != -1:
if env['LINKFLAGS'].find('-nostartfiles') != -1:
option.set('value', 'true')
else:
option.set('value', 'false')
# update libs
if option.get('id').find('c.linker.libs') != -1 and env.has_key('LIBS'):
# remove old libs
for item in option.findall('listOptionValue'):
option.remove(item)
# add new libs
for lib in env['LIBS']:
SubElement(option, 'listOptionValue', {'builtIn': 'false', 'value': lib})
# update lib paths
if option.get('id').find('c.linker.paths') != -1 and env.has_key('LIBPATH'):
# remove old lib paths
for item in option.findall('listOptionValue'):
option.remove(item)
# add new old lib paths
for path in env['LIBPATH']:
SubElement(option, 'listOptionValue', {'builtIn': 'false', 'value': path})
2019-03-21 23:04:37 +08:00
return
def UpdateProjectStructure(env, prj_name):
2019-03-21 23:04:37 +08:00
bsp_root = env['BSP_ROOT']
rtt_root = env['RTT_ROOT']
project = etree.parse('.project')
root = project.getroot()
2019-03-21 23:04:37 +08:00
if rtt_root.startswith(bsp_root):
linkedResources = root.find('linkedResources')
if linkedResources == None:
linkedResources = SubElement(root, 'linkedResources')
2019-03-21 23:04:37 +08:00
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)
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()
2019-03-21 23:04:37 +08:00
return
def GenExcluding(env, project):
rtt_root = os.path.abspath(env['RTT_ROOT'])
bsp_root = os.path.abspath(env['BSP_ROOT'])
coll_dirs = CollectPaths(project['DIRS'])
all_paths = [OSPath(path) for path in coll_dirs]
2019-03-21 23:04:37 +08:00
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
check_path = []
exclude_paths = []
# analyze the primary folder which relative to BSP_ROOT and in all_paths
for path in all_paths :
if path.startswith(bsp_root) :
folders = RelativeProjectPath(env, path).split('\\')
if folders[0] != '.' and '\\' + folders[0] not in check_path:
check_path += ['\\' + folders[0]]
# exclue the folder which has managed by scons
for path in check_path:
exclude_paths += ExcludePaths(bsp_root + path, all_paths)
else:
exclude_paths = ExcludePaths(rtt_root, all_paths)
exclude_paths += ExcludePaths(bsp_root, all_paths)
2019-03-21 23:04:37 +08:00
paths = exclude_paths
exclude_paths = []
# remove the folder which not has source code by source_pattern
2019-03-21 23:04:37 +08:00
for path in paths:
# 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'):
exclude_paths += [path]
continue
set = CollectAllFilesinPath(path, source_pattern)
if len(set):
exclude_paths += [path]
exclude_paths = [RelativeProjectPath(env, path).replace('\\', '/') for path in exclude_paths]
2019-03-21 23:04:37 +08:00
all_files = CollectFiles(all_paths, source_pattern)
src_files = project['FILES']
exclude_files = ExcludeFiles(all_files, src_files)
exclude_files = [RelativeProjectPath(env, file).replace('\\', '/') for file in exclude_files]
env['ExPaths'] = exclude_paths
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, reset):
excluding = sorted(excluding)
2019-03-21 23:04:37 +08:00
cproject = etree.parse('.cproject')
root = cproject.getroot()
cconfigurations = root.findall('storageModule/cconfiguration')
for cconfiguration in cconfigurations:
tools = cconfiguration.findall('storageModule/configuration/folderInfo/toolChain/tool')
HandleToolOption(tools, env, project, reset)
2019-03-21 23:04:37 +08:00
sourceEntries = cconfiguration.find('storageModule/configuration/sourceEntries')
entry = sourceEntries.find('entry')
if entry != None:
sourceEntries.remove(entry)
value = ''
for item in excluding:
if value == '':
value = item
else:
value += '|' + item
SubElement(sourceEntries, 'entry', {'excluding': value, 'flags': 'VALUE_WORKSPACE_PATH|RESOLVED', 'kind':'sourcePath', 'name':""})
2019-03-21 23:04:37 +08:00
# write back to .cproject
out = open('.cproject', 'w')
out.write('<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n')
out.write('<?fileVersion 4.0.0?>')
xml_indent(root)
out.write(etree.tostring(root, encoding='utf-8'))
out.close()
def TargetEclipse(env, reset = False, prj_name = None):
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, prj_name)
# generate the exclude paths and files
excluding = GenExcluding(env, project)
# update the project configuration on '.cproject' file
UpdateCproject(env, project, excluding, reset)
2019-03-21 23:04:37 +08:00
print('done!')
return