Include .s files
[bertos.git] / wizard / bertos_utils.py
1 #!/usr/bin/env python
2 # encoding: utf-8
3 #
4 # Copyright 2008 Develer S.r.l. (http://www.develer.com/)
5 # All rights reserved.
6 #
7 # $Id:$
8 #
9 # Author: Lorenzo Berni <duplo@develer.com>
10 #
11
12 import os
13 import fnmatch
14 import glob
15 import re
16 import shutil
17
18 import const
19 import codelite_project
20 import DefineException
21
22 def isBertosDir(directory):
23    return os.path.exists(directory + "/VERSION")
24
25 def bertosVersion(directory):
26    return open(directory + "/VERSION").readline().strip()
27
28 def createBertosProject(projectInfo):
29     directory = projectInfo.info("PROJECT_PATH")
30     sourcesDir = projectInfo.info("SOURCES_PATH")
31     if not os.path.isdir(directory):
32         os.mkdir(directory)
33     f = open(directory + "/project.bertos", "w")
34     f.write(repr(projectInfo))
35     f.close()
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):
43         os.remove(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)
50     os.mkdir(prjdir)
51     ## Destination configurations files
52     cfgdir = prjdir + "/cfg"
53     shutil.rmtree(cfgdir, True)
54     os.mkdir(cfgdir)
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":
60                 value = "1"
61             if "unsigned" in infos["informations"].keys() and infos["informations"]["unsigned"]:
62                 value += "U"
63             if "long" in infos["informations"].keys() and infos["informations"]["long"]:
64                 value += "L"
65             string = sub(string, parameter, value)
66         f = open(cfgdir + "/" + os.path.basename(key), "w")
67         f.write(string)
68         f.close()
69     ## Destinatio mk file
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)
81
82 def mkGenerator(projectInfo, makefile):
83     """
84     Generates the mk file for the current project.
85     """
86     mkData = {}
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"
96     for key in mkData:
97         while makefile.find(key) != -1:
98             makefile = makefile.replace(key, mkData[key])
99     return makefile
100
101 def makefileGenerator(projectInfo, makefile):
102     """
103     Generate the Makefile for the current project.
104     """
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")))
108     return makefile
109
110 def csrcGenerator(projectInfo):
111     modules = projectInfo.info("MODULES")
112     files = projectInfo.info("FILES")
113     if "harvard" in projectInfo.info("CPU_INFOS")["CPU_TAGS"]:
114         harvard = True
115     else:
116         harvard = False
117     csrc = []
118     pcsrc = []
119     constants = {}
120     moduleFiles = set([])
121     dependencyFiles = set([])
122     asmFiles = 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":
137                     csrc.append(file)
138                 if harvard and "harvard" in information:
139                     pcsrc.append(file)
140             for file in dependencyFiles:
141                 csrc.append(file)
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()])
145     print asmFiles
146     return csrc, pcsrc, constants
147     
148 def findModuleFiles(module, projectInfo):
149     cfiles = []
150     sfiles = []
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
182
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)
191     return template
192
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)
198     return template
199     
200 def getSystemPath():
201     path = os.environ["PATH"]
202     if os.name == "nt":
203         path = path.split(";")
204     else:
205         path = path.split(":")
206     return path
207
208 def findToolchains(pathList):
209     toolchains = []
210     for element in pathList:
211         for toolchain in glob.glob(element+ "/" + const.GCC_NAME):
212             toolchains.append(toolchain)
213     return list(set(toolchains))
214
215 def getToolchainInfo(output):
216     info = {}
217     expr = re.compile("Target: .*")
218     target = expr.findall(output)
219     if len(target) == 1:
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)
227     if len(build) == 1:
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)
237     if len(thread) == 1:
238         info["thread"] = thread[0].split("Thread model: ")[1]
239     return info
240
241 def loadSourceTree(project):
242     fileList = [f for f in os.walk(project.info("SOURCES_PATH"))]
243     project.setInfo("FILE_LIST", fileList)
244
245 def findDefinitions(ftype, project):
246     L = project.info("FILE_LIST")
247     definitions = []
248     for element in L:
249         for filename in element[2]:
250             if fnmatch.fnmatch(filename, ftype):
251                 definitions.append((filename, element[0]))
252     return definitions
253
254 def loadCpuInfos(project):
255     cpuInfos = []
256     for definition in findDefinitions(const.CPU_DEFINITION, project):
257         cpuInfos.append(getInfos(definition))
258     return cpuInfos
259
260 def getInfos(definition):
261     D = {}
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]
269     del D["include"]
270     return D
271
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]
275     return commentList
276
277 def loadModuleDefinition(first_comment):
278     toBeParsed = False
279     moduleDefinition = {}
280     for num, line in enumerate(first_comment):
281         index = line.find("$WIZ$")
282         if index != -1:
283             toBeParsed = True
284             try:
285                 exec line[index + len("$WIZ$ "):] in {}, moduleDefinition
286             except:
287                 raise ParseError(num, line[index:])
288         elif line.find("\\brief") != -1:
289             moduleDefinition["module_description"] = line[line.find("\\brief") + len("\\brief "):]
290     moduleDict = {}
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"]]
300         else:
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"]]
305         else:
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
318
319 def loadDefineLists(commentList):
320     defineList = {}
321     for comment in commentList:
322         for num, line in enumerate(comment):
323             index = line.find("$WIZ$")
324             if index != -1:
325                 try:
326                     exec line[index + len("$WIZ$ "):] in {}, defineList
327                 except:
328                     raise ParseError(num, line[index:])
329     for key, value in defineList.items():
330         if type(value) == str:
331             defineList[key] = (value,)
332     return defineList
333
334 def getDescriptionInformations(comment): 
335     """ 
336     Take the doxygen comment and strip the wizard informations, returning the tuple 
337     (comment, wizard_information) 
338     """
339     brief = ""
340     description = ""
341     information = {}
342     for num, line in enumerate(comment):
343         index = line.find("$WIZ$")
344         if index != -1:
345             if len(brief) == 0:
346                 brief += line[:index].strip()
347             else:
348                 description += " " + line[:index]
349             try:
350                 exec line[index + len("$WIZ$ "):] in {}, information
351             except:
352                 raise ParseError(num, line[index:])
353         else:
354             if len(brief) == 0:
355                 brief += line.strip()
356             else:
357                 description += " " + line
358                 description = description.strip()
359     return brief.strip(), description.strip(), information
360
361 def getDefinitionBlocks(text):
362     """
363     Take a text and return a list of tuple (description, name-value).
364     """
365     block = []
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))
374     return block
375
376 def loadModuleData(project):
377     moduleInfoDict = {}
378     listInfoDict = {}
379     configurationInfoDict = {}
380     fileDict = {}
381     for filename, path in findDefinitions("*.h", project):
382         commentList = getCommentList(open(path + "/" + filename, "r").read())
383         if len(commentList) > 0:
384             moduleInfo = {}
385             configurationInfo = {}
386             try:
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"]
394                     try:
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)
400             if toBeParsed:
401                 try:
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)
417     
418 def formatParamNameValue(text):
419     """
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.
422     """
423     block = re.findall("\s*([^\s]+)\s*(.+?)\s*$", text, re.MULTILINE)
424     return block[0]
425
426 def loadConfigurationInfos(path):
427     """
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
438     """
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
459
460 def sub(string, parameter, value):
461     """
462     Substitute the given value at the given parameter define in the given string
463     """
464     return re.sub(r"(?P<define>#define\s+" + parameter + r"\s+)([^\s]+)", r"\g<define>" + value, string)
465
466 def isInt(informations):
467     """
468     Return True if the value is a simple int.
469     """
470     if ("long" not in informatios.keys() or not informations["long"]) and ("unsigned" not in informations.keys() or informations["unsigned"]):
471         return True
472     else:
473         return False
474
475 def isLong(informations):
476     """
477     Return True if the value is a long.
478     """
479     if "long" in informations.keys() and informations["long"] and "unsigned" not in informations.keys():
480         return True
481     else:
482         return False
483
484 def isUnsigned(informations):
485     """
486     Return True if the value is an unsigned.
487     """
488     if "unsigned" in informations.keys() and informations["unsigned"] and "long" not in informations.keys():
489         return True
490     else:
491         return False
492
493 def isUnsignedLong(informations):
494     """
495     Return True if the value is an unsigned long.
496     """
497     if "unsigned" in informations.keys() and "long" in informations.keys() and informations["unsigned"] and informations["long"]:
498         return True
499     else:
500         return False
501
502 class ParseError(Exception):
503     def __init__(self, line_number, line):
504         Exception.__init__(self)
505         self.line_number = line_number
506         self.line = line