Skip to content

Instantly share code, notes, and snippets.

@roberthartung
Last active September 16, 2015 12:31
Show Gist options
  • Save roberthartung/78aa9b5be1eb165f8db5 to your computer and use it in GitHub Desktop.
Save roberthartung/78aa9b5be1eb165f8db5 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import sys
import os
import xml.etree.ElementTree as ET
import time
# no arguments
if len(sys.argv) == 1:
sys.exit("Usage: " + sys.argv[0] + " Project_Directory")
project_dir = os.path.abspath(sys.argv[1])
file_project = os.path.join(project_dir, ".project")
file_cproject = os.path.join(project_dir, ".cproject")
# check if required files/dirs exist
if not os.path.isdir(project_dir):
sys.exit("ERROR: Given directory does not exist.")
if not os.path.isfile(file_project):
sys.exit("ERROR: Unable to find .project file in directory.")
if not os.path.isfile(file_cproject):
sys.exit("ERROR: Unable to find .cproject file in directory.")
# parse proejct to get the workspace name
project_tree = ET.parse(file_project)
project_name = project_tree.find("name").text
# parse .cproject
cproject_tree = ET.parse(file_cproject)
cproject = cproject_tree.getroot()
# get interesting elements
nodes = cproject_tree.findall('storageModule[@moduleId="org.eclipse.cdt.core.settings"]/cconfiguration/storageModule[@moduleId="cdtBuildSystem"]/configuration')
# interesting files
include_paths=["/Inc", "/Drivers/CMSIS/Include", "/Drivers/CMSIS/Device/ST/STM32F2xx/Include", "/Drivers/STM32F2xx_HAL_Driver/Inc", "/User"]
source_paths=["/User"]
defines=["USE_HAL_DRIVER", "STM32F205xx"]
CROSS_COMPILER_PREFIX = "ilg.gnuarmeclipse.managedbuild.cross."
def add_include_paths(tool, class_includepaths, class_defs):
option = tool.find('option[@superClass="'+class_includepaths+'"]')
if option == None:
option = ET.SubElement(tool, "option", {"id" : class_includepaths + "." + str(int(time.time())),"name" : "Include paths (-I)", "superClass": class_includepaths, "useByScannerDiscovery" : "false", "valueType" : "includePath"})
for path in include_paths:
value = '"${workspace_loc:/'+project_name + path+'}"'
if option.find("listOptionValue[@value='"+value+"']") == None:
ET.SubElement(option, "listOptionValue", {"builtIn" : "false", "value": value})
# Find definitions
defs = tool.find('option[@superClass="'+class_defs+'"]')
if defs == None:
defs = ET.SubElement(tool, "option", {"id" : class_defs + "." + str(int(time.time())),"name" : "Defined symbols (-D)", "superClass": class_defs, "useByScannerDiscovery" : "false", "valueType" : "definedSymbols"})
for define in defines:
value = define
if defs.find("listOptionValue[@value='"+value+"']") == None:
ET.SubElement(defs, "listOptionValue", {"builtIn" : "false", "value": value})
return None
if len(nodes) == 1:
node = nodes[0]
folderInfo = node.findall('folderInfo')[0]
toolChain = folderInfo.findall('toolChain')[0]
for tool in toolChain.iter('tool'):
superClass = tool.attrib['superClass']
if superClass == "ilg.gnuarmeclipse.managedbuild.cross.tool.assembler":
add_include_paths(tool, "ilg.gnuarmeclipse.managedbuild.cross.option.assembler.include.paths", "ilg.gnuarmeclipse.managedbuild.cross.option.assembler.defs")
elif superClass == "ilg.gnuarmeclipse.managedbuild.cross.tool.c.compiler":
add_include_paths(tool, "ilg.gnuarmeclipse.managedbuild.cross.option.c.compiler.include.paths", "ilg.gnuarmeclipse.managedbuild.cross.option.c.compiler.defs")
elif superClass == "ilg.gnuarmeclipse.managedbuild.cross.tool.cpp.compiler":
add_include_paths(tool, "ilg.gnuarmeclipse.managedbuild.cross.option.cpp.compiler.include.paths", "ilg.gnuarmeclipse.managedbuild.cross.option.cpp.compiler.defs")
with open(os.path.join(project_dir, ".cproject"), "w+") as fh:
fh.write("""<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?fileVersion 4.0.0?>""")
list = ET.tostringlist(cproject, "UTF-8")
del list[0]
fh.write("".join(list))
fh.close()
print "Done."
else:
print "Error: Folder not found"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment