4 # Copyright 2008 Develer S.r.l. (http://www.develer.com/)
9 # Author: Lorenzo Berni <duplo@develer.com>
19 import DefineException
21 def isBertosDir(directory):
22 return os.path.exists(directory + "/VERSION")
24 def bertosVersion(directory):
25 return open(directory + "/VERSION").readline().strip()
27 def createBertosProject(projectInfo):
28 directory = projectInfo.info("PROJECT_PATH")
29 sourcesDir = projectInfo.info("SOURCES_PATH")
30 if not os.path.isdir(directory):
32 f = open(directory + "/project.bertos", "w")
33 f.write(repr(projectInfo))
35 ## Destination source dir
36 srcdir = directory + "/bertos"
37 shutil.rmtree(srcdir, True)
38 shutil.copytree(sourcesDir + "/bertos", srcdir)
39 ## Destination makefile
40 makefile = directory + "/Makefile"
41 if os.path.exists(makefile):
43 makefile = open("mktemplates/Makefile").read()
44 makefile = makefileGenerator(projectInfo, makefile)
45 open(directory + "/Makefile", "w").write(makefile)
46 ## Destination project dir
47 prjdir = directory + "/" + os.path.basename(directory)
48 shutil.rmtree(prjdir, True)
50 ## Destination configurations files
51 cfgdir = prjdir + "/cfg"
52 shutil.rmtree(cfgdir, True)
54 for key, value in projectInfo.info("CONFIGURATIONS").items():
55 string = open(sourcesDir + "/" + key, "r").read()
56 for parameter, infos in value.items():
57 value = infos["value"]
58 if "unsigned" in infos["informations"].keys() and infos["informations"]["unsigned"]:
60 if "long" in infos["informations"].keys() and infos["informations"]["long"]:
62 string = sub(string, parameter, value)
63 f = open(cfgdir + "/" + os.path.basename(key), "w")
67 makefile = open("mktemplates/template.mk", "r").read()
68 makefile = mkGenerator(projectInfo, makefile)
69 open(prjdir + "/" + os.path.basename(prjdir) + ".mk", "w").write(makefile)
70 workspace = codeliteWorkspaceGenerator(projectInfo)
71 open(directory + "/" + os.path.basename(prjdir) + ".workspace", "w").write(workspace)
72 project = codeliteProjectGenerator(projectInfo)
73 open(directory + "/" + os.path.basename(prjdir) + ".project", "w").write(project)
75 def mkGenerator(projectInfo, makefile):
77 Generates the mk file for the current project.
80 mkData["$pname"] = os.path.basename(projectInfo.info("PROJECT_PATH"))
81 mkData["$cpuname"] = projectInfo.info("CPU_INFOS")["CPU_NAME"]
82 mkData["$cflags"] = " ".join(projectInfo.info("CPU_INFOS")["C_FLAGS"])
83 mkData["$ldflags"] = " ".join(projectInfo.info("CPU_INFOS")["LD_FLAGS"])
84 mkData["$csrc"] = csrcGenerator(projectInfo)
85 mkData["$prefix"] = os.path.basename(projectInfo.info("TOOLCHAIN")["path"]).split("gcc")[0]
86 mkData["$suffix"] = os.path.basename(projectInfo.info("TOOLCHAIN")["path"]).split("gcc")[1]
88 while makefile.find(key) != -1:
89 makefile = makefile.replace(key, mkData[key])
92 def makefileGenerator(projectInfo, makefile):
94 Generate the Makefile for the current project.
96 # TODO: write a general function that works for both the mk file and the Makefile
97 while makefile.find("project_name") != -1:
98 makefile = makefile.replace("project_name", os.path.basename(projectInfo.info("PROJECT_PATH")))
101 def csrcGenerator(projectInfo):
102 modules = projectInfo.info("MODULES")
104 for module, information in modules.items():
105 if information["enabled"]:
106 for filename, path in findDefinitions(module + ".c", projectInfo):
107 files.append(path + "/" + filename)
108 for filename, path in findDefinitions(module + "_" + projectInfo.info("CPU_INFOS")["TOOLCHAIN"] + ".c", projectInfo):
109 files.append(path + "/" + filename)
110 for tag in projectInfo.info("CPU_INFOS")["CPU_TAGS"]:
111 for filename, path in findDefinitions(module + "_" + tag + ".c", projectInfo):
112 files.append(path + "/" + filename)
113 csrc = " \\\n\t".join(files) + " \\"
116 def clFiles(fileDict, directory):
118 filelist.append("<VirtualDirectory Name=\"%s\">" %os.path.basename(directory))
119 for f in fileDict[directory]["files"]:
120 filelist.append("<File Name=\"%s\"/>" %os.path.join(directory, f))
121 for d in fileDict[directory]["dirs"]:
122 filelist += clFiles(fileDict, os.path.join(directory, d))
123 filelist.append("</VirtualDirectory>")
126 def findSources(path):
128 for root, dirs, files in os.walk(path):
129 if root.find("svn") == -1:
130 fileDict[root] = {"dirs": [], "files": []}
132 if dir.find("svn") == -1:
133 fileDict[root]["dirs"].append(dir)
135 if file.endswith(const.EXTENSION_FILTER):
136 fileDict[root]["files"].append(file)
139 def codeliteProjectGenerator(projectInfo):
140 template = open("cltemplates/bertos.project").read()
141 filelist = "\n".join(clFiles(findSources(projectInfo.info("PROJECT_PATH")), projectInfo.info("PROJECT_PATH")))
142 while template.find("$filelist") != -1:
143 template = template.replace("$filelist", filelist)
146 def codeliteWorkspaceGenerator(projectInfo):
147 template = open("cltemplates/bertos.workspace").read()
148 projectName = os.path.basename(projectInfo.info("PROJECT_PATH"))
149 while template.find("$project") != -1:
150 template = template.replace("$project", projectName)
154 path = os.environ["PATH"]
156 path = path.split(";")
158 path = path.split(":")
161 def findToolchains(pathList):
163 for element in pathList:
164 for toolchain in glob.glob(element+ "/" + const.GCC_NAME):
165 toolchains.append(toolchain)
166 return list(set(toolchains))
168 def getToolchainInfo(output):
170 expr = re.compile("Target: .*")
171 target = expr.findall(output)
173 info["target"] = target[0].split("Target: ")[1]
174 expr = re.compile("gcc version [0-9,.]*")
175 version = expr.findall(output)
176 if len(version) == 1:
177 info["version"] = version[0].split("gcc version ")[1]
178 expr = re.compile("gcc version [0-9,.]* \(.*\)")
179 build = expr.findall(output)
181 build = build[0].split("gcc version ")[1]
182 build = build[build.find("(") + 1 : build.find(")")]
183 info["build"] = build
184 expr = re.compile("Configured with: .*")
185 configured = expr.findall(output)
186 if len(configured) == 1:
187 info["configured"] = configured[0].split("Configured with: ")[1]
188 expr = re.compile("Thread model: .*")
189 thread = expr.findall(output)
191 info["thread"] = thread[0].split("Thread model: ")[1]
194 def loadSourceTree(project):
195 fileList = [f for f in os.walk(project.info("SOURCES_PATH"))]
196 project.setInfo("FILE_LIST", fileList)
198 def findDefinitions(ftype, project):
199 L = project.info("FILE_LIST")
202 for filename in element[2]:
203 if fnmatch.fnmatch(filename, ftype):
204 definitions.append((filename, element[0]))
207 def loadCpuInfos(project):
209 for definition in findDefinitions(const.CPU_DEFINITION, project):
210 cpuInfos.append(getInfos(definition))
213 def getInfos(definition):
215 D.update(const.CPU_DEF)
216 def include(filename, dict = D, directory=definition[1]):
217 execfile(directory + "/" + filename, {}, D)
218 D["include"] = include
219 include(definition[0], D)
220 D["CPU_NAME"] = definition[0].split(".")[0]
221 D["DEFINITION_PATH"] = definition[1] + "/" + definition[0]
225 def getCommentList(string):
226 commentList = re.findall(r"/\*{2}\s*([^*]*\*(?:[^/*][^*]*\*+)*)/", string)
227 commentList = [re.findall(r"^\s*\* *(.*?)$", comment, re.MULTILINE) for comment in commentList]
230 def loadModuleDefinition(first_comment):
232 moduleDefinition = {}
233 for num, line in enumerate(first_comment):
234 index = line.find("$WIZ$")
238 exec line[index + len("$WIZ$ "):] in {}, moduleDefinition
240 raise ParseError(num, line[index:])
241 elif line.find("\\brief") != -1:
242 moduleDefinition["module_description"] = line[line.find("\\brief") + len("\\brief "):]
244 if "module_name" in moduleDefinition.keys():
245 moduleDict[moduleDefinition["module_name"]] = {}
246 if "module_depends" in moduleDefinition.keys():
247 if type(moduleDefinition["module_depends"]) == str:
248 moduleDefinition["module_depends"] = (moduleDefinition["module_depends"],)
249 moduleDict[moduleDefinition["module_name"]]["depends"] = moduleDefinition["module_depends"]
251 moduleDict[moduleDefinition["module_name"]]["depends"] = ()
252 if "module_configuration" in moduleDefinition.keys():
253 moduleDict[moduleDefinition["module_name"]]["configuration"] = moduleDefinition["module_configuration"]
255 moduleDict[moduleDefinition["module_name"]]["configuration"] = ""
256 if "module_description" in moduleDefinition.keys():
257 moduleDict[moduleDefinition["module_name"]]["description"] = moduleDefinition["module_description"]
258 moduleDict[moduleDefinition["module_name"]]["enabled"] = False
259 return toBeParsed, moduleDict
261 def loadDefineLists(commentList):
263 for comment in commentList:
264 for num, line in enumerate(comment):
265 index = line.find("$WIZ$")
268 exec line[index + len("$WIZ$ "):] in {}, defineList
270 raise ParseError(num, line[index:])
271 for key, value in defineList.items():
272 if type(value) == str:
273 defineList[key] = (value,)
276 def getDescriptionInformations(comment):
278 Take the doxygen comment and strip the wizard informations, returning the tuple
279 (comment, wizard_information)
284 for num, line in enumerate(comment):
285 index = line.find("$WIZ$")
288 brief += line[:index].strip()
290 description += " " + line[:index]
292 exec line[index + len("$WIZ$ "):] in {}, information
294 raise ParseError(num, line[index:])
297 brief += line.strip()
299 description += " " + line
300 description = description.strip()
301 return brief.strip(), description.strip(), information
303 def getDefinitionBlocks(text):
305 Take a text and return a list of tuple (description, name-value).
308 block_tmp = re.findall(r"/\*{2}\s*([^*]*\*(?:[^/*][^*]*\*+)*)/\s*#define\s+((?:[^/]*?/?)+)\s*?(?:/{2,3}[^<].*?)?$", text, re.MULTILINE)
309 for comment, define in block_tmp:
310 # Only the first element is needed
311 block.append(([re.findall(r"^\s*\* *(.*?)$", line, re.MULTILINE)[0] for line in comment.splitlines()], define))
312 for comment, define in re.findall(r"/{3}\s*([^<].*?)\s*#define\s+((?:[^/]*?/?)+)\s*?(?:/{2,3}[^<].*?)?$", text, re.MULTILINE):
313 block.append(([comment], define))
314 for define, comment in re.findall(r"#define\s*(.*?)\s*/{3}<\s*(.+?)\s*?(?:/{2,3}[^<].*?)?$", text, re.MULTILINE):
315 block.append(([comment], define))
318 def loadModuleData(project):
321 configurationInfoDict = {}
322 for filename, path in findDefinitions("*.h", project):
323 commentList = getCommentList(open(path + "/" + filename, "r").read())
324 if len(commentList) > 0:
326 configurationInfo = {}
328 toBeParsed, moduleDict = loadModuleDefinition(commentList[0])
329 except ParseError, err:
330 raise DefineException.ModuleDefineException(path, err.line_number, err.line)
331 for module, information in moduleDict.items():
332 information["category"] = os.path.basename(path)
333 if "configuration" in information.keys() and len(information["configuration"]):
334 configuration = moduleDict[module]["configuration"]
336 configurationInfo[configuration] = loadConfigurationInfos(project.info("SOURCES_PATH") + "/" + configuration)
337 except ParseError, err:
338 raise DefineException.ConfigurationDefineException(project.info("SOURCES_PATH") + "/" + configuration, err.line_number, err.line)
339 moduleInfoDict.update(moduleDict)
340 configurationInfoDict.update(configurationInfo)
343 listDict = loadDefineLists(commentList[1:])
344 listInfoDict.update(listDict)
345 except ParseError, err:
346 raise DefineException.EnumDefineException(path, err.line_number, err.line)
347 for filename, path in findDefinitions("*_" + project.info("CPU_INFOS")["TOOLCHAIN"] + ".h", project):
348 commentList = getCommentList(open(path + "/" + filename, "r").read())
349 listInfoDict.update(loadDefineLists(commentList))
350 for tag in project.info("CPU_INFOS")["CPU_TAGS"]:
351 for filename, path in findDefinitions("*_" + tag + ".h", project):
352 commentList = getCommentList(open(path + "/" + filename, "r").read())
353 listInfoDict.update(loadDefineLists(commentList))
354 project.setInfo("MODULES", moduleInfoDict)
355 project.setInfo("LISTS", listInfoDict)
356 project.setInfo("CONFIGURATIONS", configurationInfoDict)
358 def formatParamNameValue(text):
360 Take the given string and return a tuple with the name of the parameter in the first position
361 and the value in the second.
363 block = re.findall("\s*([^\s]+)\s*(.+?)\s*$", text, re.MULTILINE)
366 def loadConfigurationInfos(path):
368 Return the module configurations found in the given file as a dict with the
369 parameter name as key and a dict containig the fields above as value:
370 "value": the value of the parameter
371 "description": the description of the parameter
372 "informations": a dict containig optional informations:
373 "type": "int" | "boolean" | "enum"
374 "min": the minimum value for integer parameters
375 "max": the maximum value for integer parameters
376 "long": boolean indicating if the num is a long
377 "value_list": the name of the enum for enum parameters
379 configurationInfos = {}
380 for comment, define in getDefinitionBlocks(open(path, "r").read()):
381 name, value = formatParamNameValue(define)
382 brief, description, informations = getDescriptionInformations(comment)
383 configurationInfos[name] = {}
384 configurationInfos[name]["value"] = value
385 configurationInfos[name]["informations"] = informations
386 if ("type" in configurationInfos[name]["informations"].keys() and
387 configurationInfos[name]["informations"]["type"] == "int" and
388 configurationInfos[name]["value"].find("L") != -1):
389 configurationInfos[name]["informations"]["long"] = True
390 configurationInfos[name]["value"] = configurationInfos[name]["value"].replace("L", "")
391 if ("type" in configurationInfos[name]["informations"].keys() and
392 configurationInfos[name]["informations"]["type"] == "int" and
393 configurationInfos[name]["value"].find("U") != -1):
394 configurationInfos[name]["informations"]["unsigned"] = True
395 configurationInfos[name]["value"] = configurationInfos[name]["value"].replace("U", "")
396 configurationInfos[name]["description"] = description
397 configurationInfos[name]["brief"] = brief
398 return configurationInfos
400 def sub(string, parameter, value):
402 Substitute the given value at the given parameter define in the given string
404 return re.sub(r"(?P<define>#define\s+" + parameter + r"\s+)([^\s]+)", r"\g<define>" + value, string)
406 def isInt(informations):
408 Return True if the value is a simple int.
410 if ("long" not in informatios.keys() or not informations["long"]) and ("unsigned" not in informations.keys() or informations["unsigned"]):
415 def isLong(informations):
417 Return True if the value is a long.
419 if "long" in informations.keys() and informations["long"] and "unsigned" not in informations.keys():
424 def isUnsigned(informations):
426 Return True if the value is an unsigned.
428 if "unsigned" in informations.keys() and informations["unsigned"] and "long" not in informations.keys():
433 def isUnsignedLong(informations):
435 Return True if the value is an unsigned long.
437 if "unsigned" in informations.keys() and "long" in informations.keys() and informations["unsigned"] and informations["long"]:
442 class ParseError(Exception):
443 def __init__(self, line_number, line):
444 Exception.__init__(self)
445 self.line_number = line_number