Skip to content

Commit 3fb53ad

Browse files
committed
[Tools] Update Keil MDK project generation.
Add C++ files support; Fix the two same group issue if to add a library into an exist group.
1 parent 2e28861 commit 3fb53ad

File tree

1 file changed

+68
-153
lines changed

1 file changed

+68
-153
lines changed

tools/keil.py

Lines changed: 68 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,12 @@
3434
fs_encoding = sys.getfilesystemencoding()
3535

3636
def _get_filetype(fn):
37-
if fn.rfind('.c') != -1 or fn.rfind('.C') != -1 or fn.rfind('.cpp') != -1:
37+
if fn.rfind('.c') != -1 or fn.rfind('.C') != -1:
3838
return 1
3939

40+
if fn.rfind('.cpp') != -1 or fn.rfined('.cxx') != -1:
41+
return 8
42+
4043
# assemble file type
4144
if fn.rfind('.s') != -1 or fn.rfind('.S') != -1:
4245
return 2
@@ -88,131 +91,7 @@ def MDK4AddGroupForFN(ProjectFiles, parent, name, filename, project_path):
8891

8992
file_path.text = path.decode(fs_encoding)
9093

91-
def MDK4AddGroup(ProjectFiles, parent, name, files, project_path):
92-
# don't add an empty group
93-
if len(files) == 0:
94-
return
95-
96-
group = SubElement(parent, 'Group')
97-
group_name = SubElement(group, 'GroupName')
98-
group_name.text = name
99-
100-
for f in files:
101-
fn = f.rfile()
102-
name = fn.name
103-
path = os.path.dirname(fn.abspath)
104-
105-
basename = os.path.basename(path)
106-
path = _make_path_relative(project_path, path)
107-
path = os.path.join(path, name)
108-
109-
files = SubElement(group, 'Files')
110-
file = SubElement(files, 'File')
111-
file_name = SubElement(file, 'FileName')
112-
name = os.path.basename(path)
113-
114-
if name.find('.cpp') != -1:
115-
obj_name = name.replace('.cpp', '.o')
116-
elif name.find('.c') != -1:
117-
obj_name = name.replace('.c', '.o')
118-
elif name.find('.s') != -1:
119-
obj_name = name.replace('.s', '.o')
120-
elif name.find('.S') != -1:
121-
obj_name = name.replace('.s', '.o')
122-
123-
if ProjectFiles.count(obj_name):
124-
name = basename + '_' + name
125-
ProjectFiles.append(obj_name)
126-
file_name.text = name.decode(fs_encoding)
127-
file_type = SubElement(file, 'FileType')
128-
file_type.text = '%d' % _get_filetype(name)
129-
file_path = SubElement(file, 'FilePath')
130-
131-
file_path.text = path.decode(fs_encoding)
132-
133-
def MDK4Project(target, script):
134-
project_path = os.path.dirname(os.path.abspath(target))
135-
136-
project_uvopt = os.path.abspath(target).replace('uvproj', 'uvopt')
137-
if os.path.isfile(project_uvopt):
138-
os.unlink(project_uvopt)
139-
140-
tree = etree.parse('template.uvproj')
141-
root = tree.getroot()
142-
143-
out = file(target, 'wb')
144-
out.write('<?xml version="1.0" encoding="UTF-8" standalone="no" ?>\n')
145-
146-
CPPPATH = []
147-
CPPDEFINES = []
148-
LINKFLAGS = ''
149-
CCFLAGS = ''
150-
ProjectFiles = []
151-
152-
# add group
153-
groups = tree.find('Targets/Target/Groups')
154-
if groups is None:
155-
groups = SubElement(tree.find('Targets/Target'), 'Groups')
156-
groups.clear() # clean old groups
157-
for group in script:
158-
group_xml = MDK4AddGroup(ProjectFiles, groups, group['name'], group['src'], project_path)
159-
160-
# get each include path
161-
if group.has_key('CPPPATH') and group['CPPPATH']:
162-
if CPPPATH:
163-
CPPPATH += group['CPPPATH']
164-
else:
165-
CPPPATH += group['CPPPATH']
166-
167-
# get each group's definitions
168-
if group.has_key('CPPDEFINES') and group['CPPDEFINES']:
169-
if CPPDEFINES:
170-
CPPDEFINES += group['CPPDEFINES']
171-
else:
172-
CPPDEFINES += group['CPPDEFINES']
173-
174-
# get each group's link flags
175-
if group.has_key('LINKFLAGS') and group['LINKFLAGS']:
176-
if LINKFLAGS:
177-
LINKFLAGS += ' ' + group['LINKFLAGS']
178-
else:
179-
LINKFLAGS += group['LINKFLAGS']
180-
181-
if group.has_key('LIBS') and group['LIBS']:
182-
for item in group['LIBS']:
183-
lib_path = ''
184-
for path_item in group['LIBPATH']:
185-
full_path = os.path.join(path_item, item + '.lib')
186-
if os.path.isfile(full_path): # has this library
187-
lib_path = full_path
188-
189-
if lib_path != '':
190-
MDK4AddGroupForFN(ProjectFiles, groups, group['name'], lib_path, project_path)
191-
192-
# write include path, definitions and link flags
193-
IncludePath = tree.find('Targets/Target/TargetOption/TargetArmAds/Cads/VariousControls/IncludePath')
194-
IncludePath.text = ';'.join([_make_path_relative(project_path, os.path.normpath(i)) for i in CPPPATH])
195-
196-
Define = tree.find('Targets/Target/TargetOption/TargetArmAds/Cads/VariousControls/Define')
197-
Define.text = ', '.join(set(CPPDEFINES))
198-
199-
Misc = tree.find('Targets/Target/TargetOption/TargetArmAds/LDads/Misc')
200-
Misc.text = LINKFLAGS
201-
202-
xml_indent(root)
203-
out.write(etree.tostring(root, encoding='utf-8'))
204-
out.close()
205-
206-
# copy uvopt file
207-
if os.path.exists('template.uvopt'):
208-
import shutil
209-
shutil.copy2('template.uvopt', 'project.uvopt')
210-
211-
def MDK5AddGroupForFN(ProjectFiles, parent, name, filename, project_path):
212-
group = SubElement(parent, 'Group')
213-
group_name = SubElement(group, 'GroupName')
214-
group_name.text = name
215-
94+
def MDK4AddLibToGroup(ProjectFiles, group, name, filename, project_path):
21695
name = os.path.basename(filename)
21796
path = os.path.dirname (filename)
21897

@@ -232,6 +111,8 @@ def MDK5AddGroupForFN(ProjectFiles, parent, name, filename, project_path):
232111
obj_name = name.replace('.s', '.o')
233112
elif name.find('.S') != -1:
234113
obj_name = name.replace('.s', '.o')
114+
else:
115+
obj_name = name
235116

236117
if ProjectFiles.count(obj_name):
237118
name = basename + '_' + name
@@ -243,7 +124,7 @@ def MDK5AddGroupForFN(ProjectFiles, parent, name, filename, project_path):
243124

244125
file_path.text = path.decode(fs_encoding)
245126

246-
def MDK5AddGroup(ProjectFiles, parent, name, files, project_path):
127+
def MDK4AddGroup(ProjectFiles, parent, name, files, project_path):
247128
# don't add an empty group
248129
if len(files) == 0:
249130
return
@@ -274,8 +155,6 @@ def MDK5AddGroup(ProjectFiles, parent, name, files, project_path):
274155
obj_name = name.replace('.s', '.o')
275156
elif name.find('.S') != -1:
276157
obj_name = name.replace('.s', '.o')
277-
else:
278-
obj_name = name
279158

280159
if ProjectFiles.count(obj_name):
281160
name = basename + '_' + name
@@ -287,16 +166,13 @@ def MDK5AddGroup(ProjectFiles, parent, name, files, project_path):
287166

288167
file_path.text = path.decode(fs_encoding)
289168

290-
def MDK5Project(target, script):
291-
project_path = os.path.dirname(os.path.abspath(target))
169+
return group
292170

293-
project_uvopt = os.path.abspath(target).replace('uvprojx', 'uvoptx')
294-
if os.path.isfile(project_uvopt):
295-
os.unlink(project_uvopt)
171+
# The common part of making MDK4/5 project
172+
def MDK45Project(tree, target, script):
173+
project_path = os.path.dirname(os.path.abspath(target))
296174

297-
tree = etree.parse('template.uvprojx')
298175
root = tree.getroot()
299-
300176
out = file(target, 'wb')
301177
out.write('<?xml version="1.0" encoding="UTF-8" standalone="no" ?>\n')
302178

@@ -312,7 +188,31 @@ def MDK5Project(target, script):
312188
groups = SubElement(tree.find('Targets/Target'), 'Groups')
313189
groups.clear() # clean old groups
314190
for group in script:
315-
group_xml = MDK4AddGroup(ProjectFiles, groups, group['name'], group['src'], project_path)
191+
group_tree = MDK4AddGroup(ProjectFiles, groups, group['name'], group['src'], project_path)
192+
193+
# for local CPPPATH/CPPDEFINES
194+
if (group_tree != None) and (group.has_key('LOCAL_CPPPATH') or group.has_key('LOCAL_CCFLAGS')):
195+
GroupOption = SubElement(group_tree, 'GroupOption')
196+
GroupArmAds = SubElement(GroupOption, 'GroupArmAds')
197+
Cads = SubElement(GroupArmAds, 'Cads')
198+
VariousControls = SubElement(Cads, 'VariousControls')
199+
MiscControls = SubElement(VariousControls, 'MiscControls')
200+
if group.has_key('LOCAL_CCFLAGS'):
201+
MiscControls.text = group['LOCAL_CCFLAGS']
202+
else:
203+
MiscControls.text = ' '
204+
Define = SubElement(VariousControls, 'Define')
205+
if group.has_key('LOCAL_CPPDEFINES'):
206+
Define.text = ', '.join(set(group['LOCAL_CPPDEFINES']))
207+
else:
208+
Define.text = ' '
209+
Undefine = SubElement(VariousControls, 'Undefine')
210+
Undefine.text = ' '
211+
IncludePath = SubElement(VariousControls, 'IncludePath')
212+
if group.has_key('LOCAL_CPPPATH'):
213+
IncludePath.text = ';'.join([_make_path_relative(project_path, os.path.normpath(i)) for i in group['LOCAL_CPPPATH']])
214+
else:
215+
IncludePath.text = ' '
316216

317217
# get each include path
318218
if group.has_key('CPPPATH') and group['CPPPATH']:
@@ -344,27 +244,17 @@ def MDK5Project(target, script):
344244
lib_path = full_path
345245

346246
if lib_path != '':
347-
MDK4AddGroupForFN(ProjectFiles, groups, group['name'], lib_path, project_path)
348-
349-
# remove repeat path
350-
paths = set()
351-
for path in CPPPATH:
352-
inc = _make_path_relative(project_path, os.path.normpath(path))
353-
paths.add(inc) #.replace('\\', '/')
354-
355-
paths = [i for i in paths]
356-
paths.sort()
357-
CPPPATH = string.join(paths, ';')
358-
359-
definitions = [i for i in set(CPPDEFINES)]
360-
CPPDEFINES = string.join(definitions, ', ')
247+
if (group_tree != None):
248+
MDK4AddLibToGroup(ProjectFiles, group_tree, group['name'], lib_path, project_path)
249+
else:
250+
MDK4AddGroupForFN(ProjectFiles, groups, group['name'], lib_path, project_path)
361251

362252
# write include path, definitions and link flags
363253
IncludePath = tree.find('Targets/Target/TargetOption/TargetArmAds/Cads/VariousControls/IncludePath')
364-
IncludePath.text = CPPPATH
254+
IncludePath.text = ';'.join([_make_path_relative(project_path, os.path.normpath(i)) for i in CPPPATH])
365255

366256
Define = tree.find('Targets/Target/TargetOption/TargetArmAds/Cads/VariousControls/Define')
367-
Define.text = CPPDEFINES
257+
Define.text = ', '.join(set(CPPDEFINES))
368258

369259
Misc = tree.find('Targets/Target/TargetOption/TargetArmAds/LDads/Misc')
370260
Misc.text = LINKFLAGS
@@ -373,6 +263,31 @@ def MDK5Project(target, script):
373263
out.write(etree.tostring(root, encoding='utf-8'))
374264
out.close()
375265

266+
def MDK4Project(target, script):
267+
template_tree = etree.parse('template.uvproj')
268+
269+
MDK45Project(template_tree, target, script)
270+
271+
# remove project.uvopt file
272+
project_uvopt = os.path.abspath(target).replace('uvproj', 'uvopt')
273+
if os.path.isfile(project_uvopt):
274+
os.unlink(project_uvopt)
275+
276+
# copy uvopt file
277+
if os.path.exists('template.uvopt'):
278+
import shutil
279+
shutil.copy2('template.uvopt', 'project.uvopt')
280+
281+
def MDK5Project(target, script):
282+
283+
template_tree = etree.parse('template.uvprojx')
284+
285+
MDK45Project(template_tree, target, script)
286+
287+
# remove project.uvopt file
288+
project_uvopt = os.path.abspath(target).replace('uvprojx', 'uvoptx')
289+
if os.path.isfile(project_uvopt):
290+
os.unlink(project_uvopt)
376291
# copy uvopt file
377292
if os.path.exists('template.uvoptx'):
378293
import shutil

0 commit comments

Comments
 (0)