4 # Copyright 2008 Develer S.r.l. (http://www.develer.com/)
9 # Author: Lorenzo Berni <duplo@develer.com>
19 import codelite_project
20 import DefineException
22 def isBertosDir(directory):
23 return os.path.exists(directory + "/VERSION")
25 def bertosVersion(directory):
26 return open(directory + "/VERSION").readline().strip()
28 def createBertosProject(projectInfo):
29 directory = projectInfo.info("PROJECT_PATH")
30 sourcesDir = projectInfo.info("SOURCES_PATH")
31 if not os.path.isdir(directory):
33 f = open(directory + "/project.bertos", "w")
34 f.write(repr(projectInfo))
36 ## Destination source dir
37 srcdir = directory + "/bertos"
38 shutil.rmtree(srcdir, True)
39 shutil.copytree(sourcesDir + "/bertos", srcdir)
40 ## Destination makefile
41 makefile = directory + "/Makefile"
42 if os.path.exists(makefile):
44 makefile = open("mktemplates/Makefile").read()
45 makefile = makefileGenerator(projectInfo, makefile)
46 open(directory + "/Makefile", "w").write(makefile)
47 ## Destination project dir
48 prjdir = directory + "/" + os.path.basename(directory)
49 shutil.rmtree(prjdir, True)
51 ## Destination configurations files
52 cfgdir = prjdir + "/cfg"
53 shutil.rmtree(cfgdir, True)
55 for key, value in projectInfo.info("CONFIGURATIONS").items():
56 string = open(sourcesDir + "/" + key, "r").read()
57 for parameter, infos in value.items():
58 value = infos["value"]
59 if "unsigned" in infos["informations"].keys() and infos["informations"]["unsigned"]:
61 if "long" in infos["informations"].keys() and infos["informations"]["long"]:
63 string = sub(string, parameter, value)
64 f = open(cfgdir + "/" + os.path.basename(key), "w")
68 makefile = open("mktemplates/template.mk", "r").read()
69 makefile = mkGenerator(projectInfo, makefile)
70 open(prjdir + "/" + os.path.basename(prjdir) + ".mk", "w").write(makefile)
71 ## Destination main.c file
72 main = open("srctemplates/main.c", "r").read()
73 open(prjdir + "/main.c", "w").write(main)
74 if "codelite" in projectInfo.info("OUTPUT"):
75 workspace = codeliteWorkspaceGenerator(projectInfo)
76 open(directory + "/" + os.path.basename(prjdir) + ".workspace", "w").write(workspace)
77 project = codeliteProjectGenerator(projectInfo)
78 open(directory + "/" + os.path.basename(prjdir) + ".project", "w").write(project)
80 def mkGenerator(projectInfo, makefile):
82 Generates the mk file for the current project.
85 mkData["$pname"] = os.path.basename(projectInfo.info("PROJECT_PATH"))
86 mkData["$cpuname"] = projectInfo.info("CPU_INFOS")["CORE_CPU"]
87 mkData["$cflags"] = " ".join(projectInfo.info("CPU_INFOS")["C_FLAGS"])
88 mkData["$ldflags"] = " ".join(projectInfo.info("CPU_INFOS")["LD_FLAGS"])
89 mkData["$csrc"], mkData["$pcsrc"] = csrcGenerator(projectInfo)
90 mkData["$prefix"] = projectInfo.info("TOOLCHAIN")["path"].split("gcc")[0]
91 mkData["$suffix"] = projectInfo.info("TOOLCHAIN")["path"].split("gcc")[1]
92 mkData["$cross"] = projectInfo.info("TOOLCHAIN")["path"].split("gcc")[0]
93 mkData["$main"] = projectInfo.info("PROJECT_PATH") + "/" + os.path.basename(projectInfo.info("PROJECT_PATH")) + "/main.c"
95 while makefile.find(key) != -1:
96 makefile = makefile.replace(key, mkData[key])
99 def makefileGenerator(projectInfo, makefile):
101 Generate the Makefile for the current project.
103 # TODO: write a general function that works for both the mk file and the Makefile
104 while makefile.find("project_name") != -1:
105 makefile = makefile.replace("project_name", os.path.basename(projectInfo.info("PROJECT_PATH")))
108 def csrcGenerator(projectInfo):
109 modules = projectInfo.info("MODULES")
110 if "harvard" in projectInfo.info("CPU_INFOS")["CPU_TAGS"]:
111 pcsrc_need = projectInfo.info("CPU_INFOS")["PC_SRC"]
116 for module, information in modules.items():
117 if information["enabled"]:
118 for filename, path in findDefinitions(module + ".c", projectInfo):
119 path = path.replace(projectInfo.info("SOURCES_PATH"), projectInfo.info("PROJECT_PATH"))
120 csrc.append(path + "/" + filename)
121 if module in pcsrc_need:
122 pcsrc.append(path + "/" + filename)
123 for filename, path in findDefinitions(module + "_" + projectInfo.info("CPU_INFOS")["TOOLCHAIN"] + ".c", projectInfo):
124 path = path.replace(projectInfo.info("SOURCES_PATH"), projectInfo.info("PROJECT_PATH"))
125 csrc.append(path + "/" + filename)
126 if module in pcsrc_need:
127 pcsrc.append(path + "/" + filename)
128 for tag in projectInfo.info("CPU_INFOS")["CPU_TAGS"]:
129 for filename, path in findDefinitions(module + "_" + tag + ".c", projectInfo):
130 path = path.replace(projectInfo.info("SOURCES_PATH"), projectInfo.info("PROJECT_PATH"))
131 csrc.append(path + "/" + filename)
132 if module in pcsrc_need:
133 pcsrc.append(path + "/" + filename)
134 csrc = " \\\n\t".join(csrc) + " \\"
135 pcsrc = " \\\n\t".join(pcsrc) + " \\"
138 def codeliteProjectGenerator(projectInfo):
139 template = open("cltemplates/bertos.project").read()
140 filelist = "\n".join(codelite_project.clFiles(codelite_project.findSources(projectInfo.info("PROJECT_PATH")), projectInfo.info("PROJECT_PATH")))
141 while template.find("$filelist") != -1:
142 template = template.replace("$filelist", filelist)
143 projectName = os.path.basename(projectInfo.info("PROJECT_PATH"))
144 while template.find("$project") != -1:
145 template = template.replace("$project", projectName)
148 def codeliteWorkspaceGenerator(projectInfo):
149 template = open("cltemplates/bertos.workspace").read()
150 projectName = os.path.basename(projectInfo.info("PROJECT_PATH"))
151 while template.find("$project") != -1:
152 template = template.replace("$project", projectName)
156 path = os.environ["PATH"]
158 path = path.split(";")
160 path = path.split(":")
163 def findToolchains(pathList):
165 for element in pathList:
166 for toolchain in glob.glob(element+ "/" + const.GCC_NAME):
167 toolchains.append(toolchain)
168 return list(set(toolchains))
170 def getToolchainInfo(output):
172 expr = re.compile("Target: .*")
173 target = expr.findall(output)
175 info["target"] = target[0].split("Target: ")[1]
176 expr = re.compile("gcc version [0-9,.]*")
177 version = expr.findall(output)
178 if len(version) == 1:
179 info["version"] = version[0].split("gcc version ")[1]
180 expr = re.compile("gcc version [0-9,.]* \(.*\)")
181 build = expr.findall(output)
183 build = build[0].split("gcc version ")[1]
184 build = build[build.find("(") + 1 : build.find(")")]
185 info["build"] = build
186 expr = re.compile("Configured with: .*")
187 configured = expr.findall(output)
188 if len(configured) == 1:
189 info["configured"] = configured[0].split("Configured with: ")[1]
190 expr = re.compile("Thread model: .*")
191 thread = expr.findall(output)
193 info["thread"] = thread[0].split("Thread model: ")[1]
196 def loadSourceTree(project):
197 fileList = [f for f in os.walk(project.info("SOURCES_PATH"))]
198 project.setInfo("FILE_LIST", fileList)
200 def findDefinitions(ftype, project):
201 L = project.info("FILE_LIST")
204 for filename in element[2]:
205 if fnmatch.fnmatch(filename, ftype):
206 definitions.append((filename, element[0]))
209 def loadCpuInfos(project):
211 for definition in findDefinitions(const.CPU_DEFINITION, project):
212 cpuInfos.append(getInfos(definition))
215 def getInfos(definition):
217 D.update(const.CPU_DEF)
218 def include(filename, dict = D, directory=definition[1]):
219 execfile(directory + "/" + filename, {}, D)
220 D["include"] = include
221 include(definition[0], D)
222 D["CPU_NAME"] = definition[0].split(".")[0]
223 D["DEFINITION_PATH"] = definition[1] + "/" + definition[0]
227 def getCommentList(string):
228 commentList = re.findall(r"/\*{2}\s*([^*]*\*(?:[^/*][^*]*\*+)*)/", string)
229 commentList = [re.findall(r"^\s*\* *(.*?)$", comment, re.MULTILINE) for comment in commentList]
232 def loadModuleDefinition(first_comment):
234 moduleDefinition = {}
235 for num, line in enumerate(first_comment):
236 index = line.find("$WIZ$")
240 exec line[index + len("$WIZ$ "):] in {}, moduleDefinition
242 raise ParseError(num, line[index:])
243 elif line.find("\\brief") != -1:
244 moduleDefinition["module_description"] = line[line.find("\\brief") + len("\\brief "):]
246 if "module_name" in moduleDefinition.keys():
247 moduleName = moduleDefinition[const.MODULE_DEFINITION["module_name"]]
248 del moduleDefinition[const.MODULE_DEFINITION["module_name"]]
249 moduleDict[moduleName] = {}
250 if const.MODULE_DEFINITION["module_depends"] in moduleDefinition.keys():
251 if type(moduleDefinition[const.MODULE_DEFINITION["module_depends"]]) == str:
252 moduleDefinition[const.MODULE_DEFINITION["module_depends"]] = (moduleDefinition[const.MODULE_DEFINITION["module_depends"]],)
253 moduleDict[moduleName]["depends"] = moduleDefinition[const.MODULE_DEFINITION["module_depends"]]
254 del moduleDefinition[const.MODULE_DEFINITION["module_depends"]]
256 moduleDict[moduleName]["depends"] = ()
257 if const.MODULE_DEFINITION["module_configuration"] in moduleDefinition.keys():
258 moduleDict[moduleName]["configuration"] = moduleDefinition[const.MODULE_DEFINITION["module_configuration"]]
259 del moduleDefinition[const.MODULE_DEFINITION["module_configuration"]]
261 moduleDict[moduleName]["configuration"] = ""
262 if "module_description" in moduleDefinition.keys():
263 moduleDict[moduleName]["description"] = moduleDefinition["module_description"]
264 del moduleDefinition["module_description"]
265 moduleDict[moduleName]["consts"] = moduleDefinition
266 moduleDict[moduleName]["enabled"] = False
267 return toBeParsed, moduleDict
269 def loadDefineLists(commentList):
271 for comment in commentList:
272 for num, line in enumerate(comment):
273 index = line.find("$WIZ$")
276 exec line[index + len("$WIZ$ "):] in {}, defineList
278 raise ParseError(num, line[index:])
279 for key, value in defineList.items():
280 if type(value) == str:
281 defineList[key] = (value,)
284 def getDescriptionInformations(comment):
286 Take the doxygen comment and strip the wizard informations, returning the tuple
287 (comment, wizard_information)
292 for num, line in enumerate(comment):
293 index = line.find("$WIZ$")
296 brief += line[:index].strip()
298 description += " " + line[:index]
300 exec line[index + len("$WIZ$ "):] in {}, information
302 raise ParseError(num, line[index:])
305 brief += line.strip()
307 description += " " + line
308 description = description.strip()
309 return brief.strip(), description.strip(), information
311 def getDefinitionBlocks(text):
313 Take a text and return a list of tuple (description, name-value).
316 block_tmp = re.findall(r"/\*{2}\s*([^*]*\*(?:[^/*][^*]*\*+)*)/\s*#define\s+((?:[^/]*?/?)+)\s*?(?:/{2,3}[^<].*?)?$", text, re.MULTILINE)
317 for comment, define in block_tmp:
318 # Only the first element is needed
319 block.append(([re.findall(r"^\s*\* *(.*?)$", line, re.MULTILINE)[0] for line in comment.splitlines()], define))
320 for comment, define in re.findall(r"/{3}\s*([^<].*?)\s*#define\s+((?:[^/]*?/?)+)\s*?(?:/{2,3}[^<].*?)?$", text, re.MULTILINE):
321 block.append(([comment], define))
322 for define, comment in re.findall(r"#define\s*(.*?)\s*/{3}<\s*(.+?)\s*?(?:/{2,3}[^<].*?)?$", text, re.MULTILINE):
323 block.append(([comment], define))
326 def loadModuleData(project):
329 configurationInfoDict = {}
330 for filename, path in findDefinitions("*.h", project):
331 commentList = getCommentList(open(path + "/" + filename, "r").read())
332 if len(commentList) > 0:
334 configurationInfo = {}
336 toBeParsed, moduleDict = loadModuleDefinition(commentList[0])
337 except ParseError, err:
338 raise DefineException.ModuleDefineException(path, err.line_number, err.line)
339 for module, information in moduleDict.items():
340 information["category"] = os.path.basename(path)
341 if "configuration" in information.keys() and len(information["configuration"]):
342 configuration = moduleDict[module]["configuration"]
344 configurationInfo[configuration] = loadConfigurationInfos(project.info("SOURCES_PATH") + "/" + configuration)
345 except ParseError, err:
346 raise DefineException.ConfigurationDefineException(project.info("SOURCES_PATH") + "/" + configuration, err.line_number, err.line)
347 moduleInfoDict.update(moduleDict)
348 configurationInfoDict.update(configurationInfo)
351 listDict = loadDefineLists(commentList[1:])
352 listInfoDict.update(listDict)
353 except ParseError, err:
354 raise DefineException.EnumDefineException(path, err.line_number, err.line)
355 for filename, path in findDefinitions("*_" + project.info("CPU_INFOS")["TOOLCHAIN"] + ".h", project):
356 commentList = getCommentList(open(path + "/" + filename, "r").read())
357 listInfoDict.update(loadDefineLists(commentList))
358 for tag in project.info("CPU_INFOS")["CPU_TAGS"]:
359 for filename, path in findDefinitions("*_" + tag + ".h", project):
360 commentList = getCommentList(open(path + "/" + filename, "r").read())
361 listInfoDict.update(loadDefineLists(commentList))
362 project.setInfo("MODULES", moduleInfoDict)
363 project.setInfo("LISTS", listInfoDict)
364 project.setInfo("CONFIGURATIONS", configurationInfoDict)
366 def formatParamNameValue(text):
368 Take the given string and return a tuple with the name of the parameter in the first position
369 and the value in the second.
371 block = re.findall("\s*([^\s]+)\s*(.+?)\s*$", text, re.MULTILINE)
374 def loadConfigurationInfos(path):
376 Return the module configurations found in the given file as a dict with the
377 parameter name as key and a dict containig the fields above as value:
378 "value": the value of the parameter
379 "description": the description of the parameter
380 "informations": a dict containig optional informations:
381 "type": "int" | "boolean" | "enum"
382 "min": the minimum value for integer parameters
383 "max": the maximum value for integer parameters
384 "long": boolean indicating if the num is a long
385 "value_list": the name of the enum for enum parameters
387 configurationInfos = {}
388 for comment, define in getDefinitionBlocks(open(path, "r").read()):
389 name, value = formatParamNameValue(define)
390 brief, description, informations = getDescriptionInformations(comment)
391 configurationInfos[name] = {}
392 configurationInfos[name]["value"] = value
393 configurationInfos[name]["informations"] = informations
394 if ("type" in configurationInfos[name]["informations"].keys() and
395 configurationInfos[name]["informations"]["type"] == "int" and
396 configurationInfos[name]["value"].find("L") != -1):
397 configurationInfos[name]["informations"]["long"] = True
398 configurationInfos[name]["value"] = configurationInfos[name]["value"].replace("L", "")
399 if ("type" in configurationInfos[name]["informations"].keys() and
400 configurationInfos[name]["informations"]["type"] == "int" and
401 configurationInfos[name]["value"].find("U") != -1):
402 configurationInfos[name]["informations"]["unsigned"] = True
403 configurationInfos[name]["value"] = configurationInfos[name]["value"].replace("U", "")
404 configurationInfos[name]["description"] = description
405 configurationInfos[name]["brief"] = brief
406 return configurationInfos
408 def sub(string, parameter, value):
410 Substitute the given value at the given parameter define in the given string
412 return re.sub(r"(?P<define>#define\s+" + parameter + r"\s+)([^\s]+)", r"\g<define>" + value, string)
414 def isInt(informations):
416 Return True if the value is a simple int.
418 if ("long" not in informatios.keys() or not informations["long"]) and ("unsigned" not in informations.keys() or informations["unsigned"]):
423 def isLong(informations):
425 Return True if the value is a long.
427 if "long" in informations.keys() and informations["long"] and "unsigned" not in informations.keys():
432 def isUnsigned(informations):
434 Return True if the value is an unsigned.
436 if "unsigned" in informations.keys() and informations["unsigned"] and "long" not in informations.keys():
441 def isUnsignedLong(informations):
443 Return True if the value is an unsigned long.
445 if "unsigned" in informations.keys() and "long" in informations.keys() and informations["unsigned"] and informations["long"]:
450 class ParseError(Exception):
451 def __init__(self, line_number, line):
452 Exception.__init__(self)
453 self.line_number = line_number