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 "type" in infos["informations"] and infos["informations"]["type"] == "autoenabled":
61 if "unsigned" in infos["informations"].keys() and infos["informations"]["unsigned"]:
63 if "long" in infos["informations"].keys() and infos["informations"]["long"]:
65 string = sub(string, parameter, value)
66 f = open(cfgdir + "/" + os.path.basename(key), "w")
70 makefile = open("mktemplates/template.mk", "r").read()
71 makefile = mkGenerator(projectInfo, makefile)
72 open(prjdir + "/" + os.path.basename(prjdir) + ".mk", "w").write(makefile)
73 ## Destination main.c file
74 main = open("srctemplates/main.c", "r").read()
75 open(prjdir + "/main.c", "w").write(main)
76 if "codelite" in projectInfo.info("OUTPUT"):
77 workspace = codeliteWorkspaceGenerator(projectInfo)
78 open(directory + "/" + os.path.basename(prjdir) + ".workspace", "w").write(workspace)
79 project = codeliteProjectGenerator(projectInfo)
80 open(directory + "/" + os.path.basename(prjdir) + ".project", "w").write(project)
82 def mkGenerator(projectInfo, makefile):
84 Generates the mk file for the current project.
87 mkData["$pname"] = os.path.basename(projectInfo.info("PROJECT_PATH"))
88 mkData["$cpuname"] = projectInfo.info("CPU_INFOS")["CORE_CPU"]
89 mkData["$cflags"] = " ".join(projectInfo.info("CPU_INFOS")["C_FLAGS"])
90 mkData["$ldflags"] = " ".join(projectInfo.info("CPU_INFOS")["LD_FLAGS"])
91 mkData["$csrc"], mkData["$pcsrc"], mkData["$constants"] = csrcGenerator(projectInfo)
92 mkData["$prefix"] = projectInfo.info("TOOLCHAIN")["path"].split("gcc")[0]
93 mkData["$suffix"] = projectInfo.info("TOOLCHAIN")["path"].split("gcc")[1]
94 mkData["$cross"] = projectInfo.info("TOOLCHAIN")["path"].split("gcc")[0]
95 mkData["$main"] = os.path.basename(projectInfo.info("PROJECT_PATH")) + "/main.c"
97 while makefile.find(key) != -1:
98 makefile = makefile.replace(key, mkData[key])
101 def makefileGenerator(projectInfo, makefile):
103 Generate the Makefile for the current project.
105 # TODO: write a general function that works for both the mk file and the Makefile
106 while makefile.find("project_name") != -1:
107 makefile = makefile.replace("project_name", os.path.basename(projectInfo.info("PROJECT_PATH")))
110 def csrcGenerator(projectInfo):
111 modules = projectInfo.info("MODULES")
112 files = projectInfo.info("FILES")
113 if "harvard" in projectInfo.info("CPU_INFOS")["CPU_TAGS"]:
120 moduleFiles = set([])
121 dependencyFiles = set([])
123 for module, information in modules.items():
124 if information["enabled"]:
125 if "constants" in information:
126 constants.update(information["constants"])
127 cfiles, sfiles = findModuleFiles(module, projectInfo)
128 moduleFiles |= set(cfiles)
129 asmFiles |= set(sfiles)
130 for fileDependency in information["depends"]:
131 if fileDependency in files:
132 dependencyCFiles, dependencySFiles = findModuleFiles(fileDependency, projectInfo)
133 dependencyFiles |= set(dependencyCFiles)
134 asmFiles |= set(dependencySFiles)
135 for file in moduleFiles:
136 if not harvard or "harvard" not in information or information["harvard"] == "both":
138 if harvard and "harvard" in information:
140 for file in dependencyFiles:
142 csrc = " \\\n\t".join(csrc) + " \\"
143 pcsrc = " \\\n\t".join(pcsrc) + " \\"
144 constants = "\n".join([os.path.basename(projectInfo.info("PROJECT_PATH")) + "_" + key + " = " + str(value) for key, value in constants.items()])
146 return csrc, pcsrc, constants
148 def findModuleFiles(module, projectInfo):
151 for filename, path in findDefinitions(module + ".c", projectInfo):
152 path = path.replace(projectInfo.info("SOURCES_PATH") + "/", "")
153 cfiles.append(path + "/" + filename)
154 for filename, path in findDefinitions(module + "_" + projectInfo.info("CPU_INFOS")["TOOLCHAIN"] + ".c", projectInfo):
155 path = path.replace(projectInfo.info("SOURCES_PATH") + "/", "")
156 cfiles.append(path + "/" + filename)
157 for tag in projectInfo.info("CPU_INFOS")["CPU_TAGS"]:
158 for filename, path in findDefinitions(module + "_" + tag + ".c", projectInfo):
159 path = path.replace(projectInfo.info("SOURCES_PATH") + "/", "")
160 cfiles.append(path + "/" + filename)
161 for filename, path in findDefinitions(module + ".s", projectInfo):
162 path = path.replace(projectInfo.info("SOURCES_PATH") + "/", "")
163 sfiles.append(path + "/" + filename)
164 for filename, path in findDefinitions(module + "_" + projectInfo.info("CPU_INFOS")["TOOLCHAIN"] + ".s", projectInfo):
165 path = path.replace(projectInfo.info("SOURCES_PATH") + "/", "")
166 sfiles.append(path + "/" + filename)
167 for tag in projectInfo.info("CPU_INFOS")["CPU_TAGS"]:
168 for filename, path in findDefinitions(module + "_" + tag + ".s", projectInfo):
169 path = path.replace(projectInfo.info("SOURCES_PATH") + "/", "")
170 sfiles.append(path + "/" + filename)
171 for filename, path in findDefinitions(module + ".S", projectInfo):
172 path = path.replace(projectInfo.info("SOURCES_PATH") + "/", "")
173 sfiles.append(path + "/" + filename)
174 for filename, path in findDefinitions(module + "_" + projectInfo.info("CPU_INFOS")["TOOLCHAIN"] + ".S", projectInfo):
175 path = path.replace(projectInfo.info("SOURCES_PATH") + "/", "")
176 sfiles.append(path + "/" + filename)
177 for tag in projectInfo.info("CPU_INFOS")["CPU_TAGS"]:
178 for filename, path in findDefinitions(module + "_" + tag + ".S", projectInfo):
179 path = path.replace(projectInfo.info("SOURCES_PATH") + "/", "")
180 sfiles.append(path + "/" + filename)
181 return cfiles, sfiles
183 def codeliteProjectGenerator(projectInfo):
184 template = open("cltemplates/bertos.project").read()
185 filelist = "\n".join(codelite_project.clFiles(codelite_project.findSources(projectInfo.info("PROJECT_PATH")), projectInfo.info("PROJECT_PATH")))
186 while template.find("$filelist") != -1:
187 template = template.replace("$filelist", filelist)
188 projectName = os.path.basename(projectInfo.info("PROJECT_PATH"))
189 while template.find("$project") != -1:
190 template = template.replace("$project", projectName)
193 def codeliteWorkspaceGenerator(projectInfo):
194 template = open("cltemplates/bertos.workspace").read()
195 projectName = os.path.basename(projectInfo.info("PROJECT_PATH"))
196 while template.find("$project") != -1:
197 template = template.replace("$project", projectName)
201 path = os.environ["PATH"]
203 path = path.split(";")
205 path = path.split(":")
208 def findToolchains(pathList):
210 for element in pathList:
211 for toolchain in glob.glob(element+ "/" + const.GCC_NAME):
212 toolchains.append(toolchain)
213 return list(set(toolchains))
215 def getToolchainInfo(output):
217 expr = re.compile("Target: .*")
218 target = expr.findall(output)
220 info["target"] = target[0].split("Target: ")[1]
221 expr = re.compile("gcc version [0-9,.]*")
222 version = expr.findall(output)
223 if len(version) == 1:
224 info["version"] = version[0].split("gcc version ")[1]
225 expr = re.compile("gcc version [0-9,.]* \(.*\)")
226 build = expr.findall(output)
228 build = build[0].split("gcc version ")[1]
229 build = build[build.find("(") + 1 : build.find(")")]
230 info["build"] = build
231 expr = re.compile("Configured with: .*")
232 configured = expr.findall(output)
233 if len(configured) == 1:
234 info["configured"] = configured[0].split("Configured with: ")[1]
235 expr = re.compile("Thread model: .*")
236 thread = expr.findall(output)
238 info["thread"] = thread[0].split("Thread model: ")[1]
241 def loadSourceTree(project):
242 fileList = [f for f in os.walk(project.info("SOURCES_PATH"))]
243 project.setInfo("FILE_LIST", fileList)
245 def findDefinitions(ftype, project):
246 L = project.info("FILE_LIST")
249 for filename in element[2]:
250 if fnmatch.fnmatch(filename, ftype):
251 definitions.append((filename, element[0]))
254 def loadCpuInfos(project):
256 for definition in findDefinitions(const.CPU_DEFINITION, project):
257 cpuInfos.append(getInfos(definition))
260 def getInfos(definition):
262 D.update(const.CPU_DEF)
263 def include(filename, dict = D, directory=definition[1]):
264 execfile(directory + "/" + filename, {}, D)
265 D["include"] = include
266 include(definition[0], D)
267 D["CPU_NAME"] = definition[0].split(".")[0]
268 D["DEFINITION_PATH"] = definition[1] + "/" + definition[0]
272 def getCommentList(string):
273 commentList = re.findall(r"/\*{2}\s*([^*]*\*(?:[^/*][^*]*\*+)*)/", string)
274 commentList = [re.findall(r"^\s*\* *(.*?)$", comment, re.MULTILINE) for comment in commentList]
277 def loadModuleDefinition(first_comment):
279 moduleDefinition = {}
280 for num, line in enumerate(first_comment):
281 index = line.find("$WIZ$")
285 exec line[index + len("$WIZ$ "):] in {}, moduleDefinition
287 raise ParseError(num, line[index:])
288 elif line.find("\\brief") != -1:
289 moduleDefinition["module_description"] = line[line.find("\\brief") + len("\\brief "):]
291 if "module_name" in moduleDefinition.keys():
292 moduleName = moduleDefinition[const.MODULE_DEFINITION["module_name"]]
293 del moduleDefinition[const.MODULE_DEFINITION["module_name"]]
294 moduleDict[moduleName] = {}
295 if const.MODULE_DEFINITION["module_depends"] in moduleDefinition.keys():
296 if type(moduleDefinition[const.MODULE_DEFINITION["module_depends"]]) == str:
297 moduleDefinition[const.MODULE_DEFINITION["module_depends"]] = (moduleDefinition[const.MODULE_DEFINITION["module_depends"]],)
298 moduleDict[moduleName]["depends"] = moduleDefinition[const.MODULE_DEFINITION["module_depends"]]
299 del moduleDefinition[const.MODULE_DEFINITION["module_depends"]]
301 moduleDict[moduleName]["depends"] = ()
302 if const.MODULE_DEFINITION["module_configuration"] in moduleDefinition.keys():
303 moduleDict[moduleName]["configuration"] = moduleDefinition[const.MODULE_DEFINITION["module_configuration"]]
304 del moduleDefinition[const.MODULE_DEFINITION["module_configuration"]]
306 moduleDict[moduleName]["configuration"] = ""
307 if "module_description" in moduleDefinition.keys():
308 moduleDict[moduleName]["description"] = moduleDefinition["module_description"]
309 del moduleDefinition["module_description"]
310 if const.MODULE_DEFINITION["module_harvard"] in moduleDefinition.keys():
311 harvard = moduleDefinition[const.MODULE_DEFINITION["module_harvard"]]
312 if harvard == "both" or harvard == "pgm_memory":
313 moduleDict[moduleName]["harvard"] = harvard
314 del moduleDefinition[const.MODULE_DEFINITION["module_harvard"]]
315 moduleDict[moduleName]["constants"] = moduleDefinition
316 moduleDict[moduleName]["enabled"] = False
317 return toBeParsed, moduleDict
319 def loadDefineLists(commentList):
321 for comment in commentList:
322 for num, line in enumerate(comment):
323 index = line.find("$WIZ$")
326 exec line[index + len("$WIZ$ "):] in {}, defineList
328 raise ParseError(num, line[index:])
329 for key, value in defineList.items():
330 if type(value) == str:
331 defineList[key] = (value,)
334 def getDescriptionInformations(comment):
336 Take the doxygen comment and strip the wizard informations, returning the tuple
337 (comment, wizard_information)
342 for num, line in enumerate(comment):
343 index = line.find("$WIZ$")
346 brief += line[:index].strip()
348 description += " " + line[:index]
350 exec line[index + len("$WIZ$ "):] in {}, information
352 raise ParseError(num, line[index:])
355 brief += line.strip()
357 description += " " + line
358 description = description.strip()
359 return brief.strip(), description.strip(), information
361 def getDefinitionBlocks(text):
363 Take a text and return a list of tuple (description, name-value).
366 block_tmp = re.findall(r"/\*{2}\s*([^*]*\*(?:[^/*][^*]*\*+)*)/\s*#define\s+((?:[^/]*?/?)+)\s*?(?:/{2,3}[^<].*?)?$", text, re.MULTILINE)
367 for comment, define in block_tmp:
368 # Only the first element is needed
369 block.append(([re.findall(r"^\s*\* *(.*?)$", line, re.MULTILINE)[0] for line in comment.splitlines()], define))
370 for comment, define in re.findall(r"/{3}\s*([^<].*?)\s*#define\s+((?:[^/]*?/?)+)\s*?(?:/{2,3}[^<].*?)?$", text, re.MULTILINE):
371 block.append(([comment], define))
372 for define, comment in re.findall(r"#define\s*(.*?)\s*/{3}<\s*(.+?)\s*?(?:/{2,3}[^<].*?)?$", text, re.MULTILINE):
373 block.append(([comment], define))
376 def loadModuleData(project):
379 configurationInfoDict = {}
381 for filename, path in findDefinitions("*.h", project):
382 commentList = getCommentList(open(path + "/" + filename, "r").read())
383 if len(commentList) > 0:
385 configurationInfo = {}
387 toBeParsed, moduleDict = loadModuleDefinition(commentList[0])
388 except ParseError, err:
389 raise DefineException.ModuleDefineException(path, err.line_number, err.line)
390 for module, information in moduleDict.items():
391 information["category"] = os.path.basename(path)
392 if "configuration" in information.keys() and len(information["configuration"]):
393 configuration = moduleDict[module]["configuration"]
395 configurationInfo[configuration] = loadConfigurationInfos(project.info("SOURCES_PATH") + "/" + configuration)
396 except ParseError, err:
397 raise DefineException.ConfigurationDefineException(project.info("SOURCES_PATH") + "/" + configuration, err.line_number, err.line)
398 moduleInfoDict.update(moduleDict)
399 configurationInfoDict.update(configurationInfo)
402 listDict = loadDefineLists(commentList[1:])
403 listInfoDict.update(listDict)
404 except ParseError, err:
405 raise DefineException.EnumDefineException(path, err.line_number, err.line)
406 for filename, path in findDefinitions("*_" + project.info("CPU_INFOS")["TOOLCHAIN"] + ".h", project):
407 commentList = getCommentList(open(path + "/" + filename, "r").read())
408 listInfoDict.update(loadDefineLists(commentList))
409 for tag in project.info("CPU_INFOS")["CPU_TAGS"]:
410 for filename, path in findDefinitions("*_" + tag + ".h", project):
411 commentList = getCommentList(open(path + "/" + filename, "r").read())
412 listInfoDict.update(loadDefineLists(commentList))
413 project.setInfo("MODULES", moduleInfoDict)
414 project.setInfo("LISTS", listInfoDict)
415 project.setInfo("CONFIGURATIONS", configurationInfoDict)
416 project.setInfo("FILES", fileDict)
418 def formatParamNameValue(text):
420 Take the given string and return a tuple with the name of the parameter in the first position
421 and the value in the second.
423 block = re.findall("\s*([^\s]+)\s*(.+?)\s*$", text, re.MULTILINE)
426 def loadConfigurationInfos(path):
428 Return the module configurations found in the given file as a dict with the
429 parameter name as key and a dict containig the fields above as value:
430 "value": the value of the parameter
431 "description": the description of the parameter
432 "informations": a dict containig optional informations:
433 "type": "int" | "boolean" | "enum"
434 "min": the minimum value for integer parameters
435 "max": the maximum value for integer parameters
436 "long": boolean indicating if the num is a long
437 "value_list": the name of the enum for enum parameters
439 configurationInfos = {}
440 for comment, define in getDefinitionBlocks(open(path, "r").read()):
441 name, value = formatParamNameValue(define)
442 brief, description, informations = getDescriptionInformations(comment)
443 configurationInfos[name] = {}
444 configurationInfos[name]["value"] = value
445 configurationInfos[name]["informations"] = informations
446 if ("type" in configurationInfos[name]["informations"].keys() and
447 configurationInfos[name]["informations"]["type"] == "int" and
448 configurationInfos[name]["value"].find("L") != -1):
449 configurationInfos[name]["informations"]["long"] = True
450 configurationInfos[name]["value"] = configurationInfos[name]["value"].replace("L", "")
451 if ("type" in configurationInfos[name]["informations"].keys() and
452 configurationInfos[name]["informations"]["type"] == "int" and
453 configurationInfos[name]["value"].find("U") != -1):
454 configurationInfos[name]["informations"]["unsigned"] = True
455 configurationInfos[name]["value"] = configurationInfos[name]["value"].replace("U", "")
456 configurationInfos[name]["description"] = description
457 configurationInfos[name]["brief"] = brief
458 return configurationInfos
460 def sub(string, parameter, value):
462 Substitute the given value at the given parameter define in the given string
464 return re.sub(r"(?P<define>#define\s+" + parameter + r"\s+)([^\s]+)", r"\g<define>" + value, string)
466 def isInt(informations):
468 Return True if the value is a simple int.
470 if ("long" not in informatios.keys() or not informations["long"]) and ("unsigned" not in informations.keys() or informations["unsigned"]):
475 def isLong(informations):
477 Return True if the value is a long.
479 if "long" in informations.keys() and informations["long"] and "unsigned" not in informations.keys():
484 def isUnsigned(informations):
486 Return True if the value is an unsigned.
488 if "unsigned" in informations.keys() and informations["unsigned"] and "long" not in informations.keys():
493 def isUnsignedLong(informations):
495 Return True if the value is an unsigned long.
497 if "unsigned" in informations.keys() and "long" in informations.keys() and informations["unsigned"] and informations["long"]:
502 class ParseError(Exception):
503 def __init__(self, line_number, line):
504 Exception.__init__(self)
505 self.line_number = line_number