Create workspace and project file for codelite
[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 DefineException
20
21 def isBertosDir(directory):
22    return os.path.exists(directory + "/VERSION")
23
24 def bertosVersion(directory):
25    return open(directory + "/VERSION").readline().strip()
26
27 def createBertosProject(projectInfo):
28     directory = projectInfo.info("PROJECT_PATH")
29     sourcesDir = projectInfo.info("SOURCES_PATH")
30     if not os.path.isdir(directory):
31         os.mkdir(directory)
32     f = open(directory + "/project.bertos", "w")
33     f.write(repr(projectInfo))
34     f.close()
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):
42         os.remove(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)
49     os.mkdir(prjdir)
50     ## Destination configurations files
51     cfgdir = prjdir + "/cfg"
52     shutil.rmtree(cfgdir, True)
53     os.mkdir(cfgdir)
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"]:
59                 value += "U"
60             if "long" in infos["informations"].keys() and infos["informations"]["long"]:
61                 value += "L"
62             string = sub(string, parameter, value)
63         f = open(cfgdir + "/" + os.path.basename(key), "w")
64         f.write(string)
65         f.close()
66     ## Destinatio mk file
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)
74
75 def mkGenerator(projectInfo, makefile):
76     """
77     Generates the mk file for the current project.
78     """
79     mkData = {}
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]
87     for key in mkData:
88         while makefile.find(key) != -1:
89             makefile = makefile.replace(key, mkData[key])
90     return makefile
91
92 def makefileGenerator(projectInfo, makefile):
93     """
94     Generate the Makefile for the current project.
95     """
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")))
99     return makefile
100
101 def csrcGenerator(projectInfo):
102     modules = projectInfo.info("MODULES")
103     files = []
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) + " \\"
114     return csrc
115
116 def clFiles(fileDict, directory):
117     filelist = []
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>")
124     return filelist
125
126 def findSources(path):
127     fileDict = {}
128     for root, dirs, files in os.walk(path):
129         if root.find("svn") == -1:
130             fileDict[root] = {"dirs": [], "files": []}
131             for dir in dirs:
132                 if dir.find("svn") == -1:
133                     fileDict[root]["dirs"].append(dir)
134             for file in files:
135                 if file.endswith(const.EXTENSION_FILTER):
136                     fileDict[root]["files"].append(file)
137     return fileDict
138
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)
144     return template
145
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)
151     return template
152     
153 def getSystemPath():
154     path = os.environ["PATH"]
155     if os.name == "nt":
156         path = path.split(";")
157     else:
158         path = path.split(":")
159     return path
160
161 def findToolchains(pathList):
162     toolchains = []
163     for element in pathList:
164         for toolchain in glob.glob(element+ "/" + const.GCC_NAME):
165             toolchains.append(toolchain)
166     return list(set(toolchains))
167
168 def getToolchainInfo(output):
169     info = {}
170     expr = re.compile("Target: .*")
171     target = expr.findall(output)
172     if len(target) == 1:
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)
180     if len(build) == 1:
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)
190     if len(thread) == 1:
191         info["thread"] = thread[0].split("Thread model: ")[1]
192     return info
193
194 def loadSourceTree(project):
195     fileList = [f for f in os.walk(project.info("SOURCES_PATH"))]
196     project.setInfo("FILE_LIST", fileList)
197
198 def findDefinitions(ftype, project):
199     L = project.info("FILE_LIST")
200     definitions = []
201     for element in L:
202         for filename in element[2]:
203             if fnmatch.fnmatch(filename, ftype):
204                 definitions.append((filename, element[0]))
205     return definitions
206
207 def loadCpuInfos(project):
208     cpuInfos = []
209     for definition in findDefinitions(const.CPU_DEFINITION, project):
210         cpuInfos.append(getInfos(definition))
211     return cpuInfos
212
213 def getInfos(definition):
214     D = {}
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]
222     del D["include"]
223     return D
224
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]
228     return commentList
229
230 def loadModuleDefinition(first_comment):
231     toBeParsed = False
232     moduleDefinition = {}
233     for num, line in enumerate(first_comment):
234         index = line.find("$WIZ$")
235         if index != -1:
236             toBeParsed = True
237             try:
238                 exec line[index + len("$WIZ$ "):] in {}, moduleDefinition
239             except:
240                 raise ParseError(num, line[index:])
241         elif line.find("\\brief") != -1:
242             moduleDefinition["module_description"] = line[line.find("\\brief") + len("\\brief "):]
243     moduleDict = {}
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"]
250         else:
251             moduleDict[moduleDefinition["module_name"]]["depends"] = ()
252         if "module_configuration" in moduleDefinition.keys():
253             moduleDict[moduleDefinition["module_name"]]["configuration"] = moduleDefinition["module_configuration"]
254         else:
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
260
261 def loadDefineLists(commentList):
262     defineList = {}
263     for comment in commentList:
264         for num, line in enumerate(comment):
265             index = line.find("$WIZ$")
266             if index != -1:
267                 try:
268                     exec line[index + len("$WIZ$ "):] in {}, defineList
269                 except:
270                     raise ParseError(num, line[index:])
271     for key, value in defineList.items():
272         if type(value) == str:
273             defineList[key] = (value,)
274     return defineList
275
276 def getDescriptionInformations(comment): 
277     """ 
278     Take the doxygen comment and strip the wizard informations, returning the tuple 
279     (comment, wizard_information) 
280     """
281     brief = ""
282     description = ""
283     information = {}
284     for num, line in enumerate(comment):
285         index = line.find("$WIZ$")
286         if index != -1:
287             if len(brief) == 0:
288                 brief += line[:index].strip()
289             else:
290                 description += " " + line[:index]
291             try:
292                 exec line[index + len("$WIZ$ "):] in {}, information
293             except:
294                 raise ParseError(num, line[index:])
295         else:
296             if len(brief) == 0:
297                 brief += line.strip()
298             else:
299                 description += " " + line
300                 description = description.strip()
301     return brief.strip(), description.strip(), information
302
303 def getDefinitionBlocks(text):
304     """
305     Take a text and return a list of tuple (description, name-value).
306     """
307     block = []
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))
316     return block
317
318 def loadModuleData(project):
319     moduleInfoDict = {}
320     listInfoDict = {}
321     configurationInfoDict = {}
322     for filename, path in findDefinitions("*.h", project):
323         commentList = getCommentList(open(path + "/" + filename, "r").read())
324         if len(commentList) > 0:
325             moduleInfo = {}
326             configurationInfo = {}
327             try:
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"]
335                     try:
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)
341             if toBeParsed:
342                 try:
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)
357     
358 def formatParamNameValue(text):
359     """
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.
362     """
363     block = re.findall("\s*([^\s]+)\s*(.+?)\s*$", text, re.MULTILINE)
364     return block[0]
365
366 def loadConfigurationInfos(path):
367     """
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
378     """
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
399
400 def sub(string, parameter, value):
401     """
402     Substitute the given value at the given parameter define in the given string
403     """
404     return re.sub(r"(?P<define>#define\s+" + parameter + r"\s+)([^\s]+)", r"\g<define>" + value, string)
405
406 def isInt(informations):
407     """
408     Return True if the value is a simple int.
409     """
410     if ("long" not in informatios.keys() or not informations["long"]) and ("unsigned" not in informations.keys() or informations["unsigned"]):
411         return True
412     else:
413         return False
414
415 def isLong(informations):
416     """
417     Return True if the value is a long.
418     """
419     if "long" in informations.keys() and informations["long"] and "unsigned" not in informations.keys():
420         return True
421     else:
422         return False
423
424 def isUnsigned(informations):
425     """
426     Return True if the value is an unsigned.
427     """
428     if "unsigned" in informations.keys() and informations["unsigned"] and "long" not in informations.keys():
429         return True
430     else:
431         return False
432
433 def isUnsignedLong(informations):
434     """
435     Return True if the value is an unsigned long.
436     """
437     if "unsigned" in informations.keys() and "long" in informations.keys() and informations["unsigned"] and informations["long"]:
438         return True
439     else:
440         return False
441
442 class ParseError(Exception):
443     def __init__(self, line_number, line):
444         Exception.__init__(self)
445         self.line_number = line_number
446         self.line = line