Skip to content

Commit 40676b6

Browse files
committed
fix[tools][eclipse]: avoid unnecessary project file rewrites during eclipse target generation
1 parent c6cfed3 commit 40676b6

File tree

1 file changed

+32
-16
lines changed

1 file changed

+32
-16
lines changed

tools/targets/eclipse.py

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,26 @@
2929
source_pattern = ['*.c', '*.cpp', '*.cxx', '*.cc', '*.s', '*.S', '*.asm','*.cmd']
3030

3131

32+
def _write_text_if_changed(path, content, encoding='utf-8'):
33+
old_content = None
34+
if os.path.exists(path):
35+
with open(path, 'r', encoding=encoding, errors='ignore') as f:
36+
old_content = f.read()
37+
38+
if old_content == content:
39+
return False
40+
41+
with open(path, 'w', encoding=encoding, newline='\n') as f:
42+
f.write(content)
43+
44+
return True
45+
46+
47+
def _serialize_xml(root, xml_decl):
48+
xml_indent(root)
49+
return xml_decl + etree.tostring(root, encoding='utf-8').decode('utf-8')
50+
51+
3252
def OSPath(path):
3353
import platform
3454

@@ -257,11 +277,11 @@ def HandleToolOption(tools, env, project, reset):
257277
file_tail = '\n#endif /*RTCONFIG_PREINC_H__*/\n'
258278
rtt_pre_inc_item = '"${workspace_loc:/${ProjName}/rtconfig_preinc.h}"'
259279
# save the CPPDEFINES in to rtconfig_preinc.h
260-
with open('rtconfig_preinc.h', mode = 'w+') as f:
261-
f.write(file_header)
262-
for cppdef in CPPDEFINES:
263-
f.write("#define " + cppdef.replace('=', ' ') + '\n')
264-
f.write(file_tail)
280+
preinc_content = file_header
281+
for cppdef in CPPDEFINES:
282+
preinc_content += "#define " + cppdef.replace('=', ' ') + '\n'
283+
preinc_content += file_tail
284+
_write_text_if_changed('rtconfig_preinc.h', preinc_content)
265285
# change the c.compiler.include.files
266286
files = option.findall('listOptionValue')
267287
find_ok = False
@@ -380,11 +400,8 @@ def UpdateProjectStructure(env, prj_name):
380400
name = SubElement(root, 'name')
381401
name.text = prj_name
382402

383-
out = open('.project', 'w')
384-
out.write('<?xml version="1.0" encoding="UTF-8"?>\n')
385-
xml_indent(root)
386-
out.write(etree.tostring(root, encoding='utf-8').decode('utf-8'))
387-
out.close()
403+
project_content = _serialize_xml(root, '<?xml version="1.0" encoding="UTF-8"?>\n')
404+
_write_text_if_changed('.project', project_content)
388405

389406
return
390407

@@ -518,12 +535,11 @@ def UpdateCproject(env, project, excluding, reset, prj_name):
518535
SubElement(configuration, 'resource', {'resourceType': "PROJECT", 'workspacePath': prj_name})
519536

520537
# write back to .cproject
521-
out = open('.cproject', 'w')
522-
out.write('<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n')
523-
out.write('<?fileVersion 4.0.0?>')
524-
xml_indent(root)
525-
out.write(etree.tostring(root, encoding='utf-8').decode('utf-8'))
526-
out.close()
538+
cproject_content = _serialize_xml(
539+
root,
540+
'<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n<?fileVersion 4.0.0?>'
541+
)
542+
_write_text_if_changed('.cproject', cproject_content)
527543

528544

529545
def TargetEclipse(env, reset=False, prj_name=None):

0 commit comments

Comments
 (0)