Order the configuration property
[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(project_info):
29     directory = project_info.info("PROJECT_PATH")
30     sources_dir = project_info.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(project_info))
35     f.close()
36     ## Destination source dir
37     srcdir = directory + "/bertos"
38     shutil.rmtree(srcdir, True)
39     shutil.copytree(sources_dir + "/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(project_info, 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 project_info.info("CONFIGURATIONS").items():
56         string = open(sources_dir + "/" + 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(project_info, 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 project_info.info("OUTPUT"):
77         workspace = codeliteWorkspaceGenerator(project_info)
78         open(directory + "/" + os.path.basename(prjdir) + ".workspace", "w").write(workspace)
79         project = codeliteProjectGenerator(project_info)
80         open(directory + "/" + os.path.basename(prjdir) + ".project", "w").write(project)
81
82 def mkGenerator(project_info, makefile):
83     """
84     Generates the mk file for the current project.
85     """
86     mk_data = {}
87     mk_data["$pname"] = os.path.basename(project_info.info("PROJECT_PATH"))
88     mk_data["$cpuname"] = project_info.info("CPU_INFOS")["CORE_CPU"]
89     mk_data["$cflags"] = " ".join(project_info.info("CPU_INFOS")["C_FLAGS"])
90     mk_data["$ldflags"] = " ".join(project_info.info("CPU_INFOS")["LD_FLAGS"])
91     mk_data["$csrc"], mk_data["$pcsrc"], mk_data["$constants"] = csrcGenerator(project_info)
92     mk_data["$prefix"] = project_info.info("TOOLCHAIN")["path"].split("gcc")[0]
93     mk_data["$suffix"] = project_info.info("TOOLCHAIN")["path"].split("gcc")[1]
94     mk_data["$cross"] = project_info.info("TOOLCHAIN")["path"].split("gcc")[0]
95     mk_data["$main"] = os.path.basename(project_info.info("PROJECT_PATH")) + "/main.c"
96     for key in mk_data:
97         while makefile.find(key) != -1:
98             makefile = makefile.replace(key, mk_data[key])
99     return makefile
100
101 def makefileGenerator(project_info, 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(project_info.info("PROJECT_PATH")))
108     return makefile
109
110 def csrcGenerator(project_info):
111     modules = project_info.info("MODULES")
112     files = project_info.info("FILES")
113     if "harvard" in project_info.info("CPU_INFOS")["CPU_TAGS"]:
114         harvard = True
115     else:
116         harvard = False
117     ## file to be included in CSRC variable
118     csrc = []
119     ## file to be included in PCSRC variable
120     pcsrc = []
121     ## constants to be included at the beginning of the makefile
122     constants = {}
123     module_files = set([])
124     dependency_files = set([])
125     ## assembly sources
126     asm_files = set([])
127     for module, information in modules.items():
128         if information["enabled"]:
129             if "constants" in information:
130                 constants.update(information["constants"])
131             cfiles, sfiles = findModuleFiles(module, project_info)
132             module_files |= set(cfiles)
133             asm_files |= set(sfiles)
134             for file_dependency in information["depends"]:
135                 if file_dependency in files:
136                     dependencyCFiles, dependencySFiles = findModuleFiles(file_dependency, project_info)
137                     dependency_files |= set(dependencyCFiles)
138                     asm_files |= set(dependencySFiles)
139             for file in module_files:
140                 if not harvard or "harvard" not in information or information["harvard"] == "both":
141                     csrc.append(file)
142                 if harvard and "harvard" in information:
143                     pcsrc.append(file)
144             for file in dependency_files:
145                 csrc.append(file)
146     csrc = " \\\n\t".join(csrc) + " \\"
147     pcsrc = " \\\n\t".join(pcsrc) + " \\"
148     constants = "\n".join([os.path.basename(project_info.info("PROJECT_PATH")) + "_" + key + " = " + str(value) for key, value in constants.items()])
149     return csrc, pcsrc, constants
150     
151 def findModuleFiles(module, project_info):
152     ## Find the files related to the selected module
153     cfiles = []
154     sfiles = []
155     ## .c files related to the module and the cpu architecture
156     for filename, path in findDefinitions(module + ".c", project_info) + \
157             findDefinitions(module + "_" + project_info.info("CPU_INFOS")["TOOLCHAIN"] + ".c", project_info):
158         path = path.replace(project_info.info("SOURCES_PATH") + "/", "")
159         cfiles.append(path + "/" + filename)
160     ## .s files related to the module and the cpu architecture
161     for filename, path in findDefinitions(module + ".s", project_info) + \
162             findDefinitions(module + "_" + project_info.info("CPU_INFOS")["TOOLCHAIN"] + ".s", project_info) + \
163             findDefinitions(module + ".S", project_info) + \
164             findDefinitions(module + "_" + project_info.info("CPU_INFOS")["TOOLCHAIN"] + ".S", project_info):
165         path = path.replace(project_info.info("SOURCES_PATH") + "/", "")
166         sfiles.append(path + "/" + filename)
167     ## .c and .s files related to the module and the cpu tags
168     for tag in project_info.info("CPU_INFOS")["CPU_TAGS"]:
169         for filename, path in findDefinitions(module + "_" + tag + ".c", project_info):
170             path = path.replace(project_info.info("SOURCES_PATH") + "/", "")
171             cfiles.append(path + "/" + filename)
172         for filename, path in findDefinitions(module + "_" + tag + ".s", project_info) + \
173                 findDefinitions(module + "_" + tag + ".S", project_info):
174             path = path.replace(project_info.info("SOURCES_PATH") + "/", "")
175             sfiles.append(path + "/" + filename)
176     return cfiles, sfiles
177
178 def codeliteProjectGenerator(project_info):
179     template = open("cltemplates/bertos.project").read()
180     filelist = "\n".join(codelite_project.clFiles(codelite_project.findSources(project_info.info("PROJECT_PATH")), project_info.info("PROJECT_PATH")))
181     while template.find("$filelist") != -1:
182         template = template.replace("$filelist", filelist)
183     project_name = os.path.basename(project_info.info("PROJECT_PATH"))
184     while template.find("$project") != -1:
185         template = template.replace("$project", project_name)
186     return template
187
188 def codeliteWorkspaceGenerator(project_info):
189     template = open("cltemplates/bertos.workspace").read()
190     project_name = os.path.basename(project_info.info("PROJECT_PATH"))
191     while template.find("$project") != -1:
192         template = template.replace("$project", project_name)
193     return template
194     
195 def getSystemPath():
196     path = os.environ["PATH"]
197     if os.name == "nt":
198         path = path.split(";")
199     else:
200         path = path.split(":")
201     return path
202
203 def findToolchains(path_list):
204     toolchains = []
205     for element in path_list:
206         for toolchain in glob.glob(element+ "/" + const.GCC_NAME):
207             toolchains.append(toolchain)
208     return list(set(toolchains))
209
210 def getToolchainInfo(output):
211     info = {}
212     expr = re.compile("Target: .*")
213     target = expr.findall(output)
214     if len(target) == 1:
215         info["target"] = target[0].split("Target: ")[1]
216     expr = re.compile("gcc version [0-9,.]*")
217     version = expr.findall(output)
218     if len(version) == 1:
219         info["version"] = version[0].split("gcc version ")[1]
220     expr = re.compile("gcc version [0-9,.]* \(.*\)")
221     build = expr.findall(output)
222     if len(build) == 1:
223         build = build[0].split("gcc version ")[1]
224         build = build[build.find("(") + 1 : build.find(")")]
225         info["build"] = build
226     expr = re.compile("Configured with: .*")
227     configured = expr.findall(output)
228     if len(configured) == 1:
229         info["configured"] = configured[0].split("Configured with: ")[1]
230     expr = re.compile("Thread model: .*")
231     thread = expr.findall(output)
232     if len(thread) == 1:
233         info["thread"] = thread[0].split("Thread model: ")[1]
234     return info
235
236 def loadSourceTree(project):
237     fileList = [f for f in os.walk(project.info("SOURCES_PATH"))]
238     project.setInfo("FILE_LIST", fileList)
239
240 def findDefinitions(ftype, project):
241     L = project.info("FILE_LIST")
242     definitions = []
243     for element in L:
244         for filename in element[2]:
245             if fnmatch.fnmatch(filename, ftype):
246                 definitions.append((filename, element[0]))
247     return definitions
248
249 def loadCpuInfos(project):
250     cpuInfos = []
251     for definition in findDefinitions(const.CPU_DEFINITION, project):
252         cpuInfos.append(getInfos(definition))
253     return cpuInfos
254
255 def getInfos(definition):
256     D = {}
257     D.update(const.CPU_DEF)
258     def include(filename, dict = D, directory=definition[1]):
259         execfile(directory + "/" + filename, {}, D)
260     D["include"] = include
261     include(definition[0], D)
262     D["CPU_NAME"] = definition[0].split(".")[0]
263     D["DEFINITION_PATH"] = definition[1] + "/" + definition[0]
264     del D["include"]
265     return D
266
267 def getCommentList(string):
268     comment_list = re.findall(r"/\*{2}\s*([^*]*\*(?:[^/*][^*]*\*+)*)/", string)
269     comment_list = [re.findall(r"^\s*\* *(.*?)$", comment, re.MULTILINE) for comment in comment_list]
270     return comment_list
271
272 def loadModuleDefinition(first_comment):
273     to_be_parsed = False
274     module_definition = {}
275     for num, line in enumerate(first_comment):
276         index = line.find("$WIZ$")
277         if index != -1:
278             to_be_parsed = True
279             try:
280                 exec line[index + len("$WIZ$ "):] in {}, module_definition
281             except:
282                 raise ParseError(num, line[index:])
283         elif line.find("\\brief") != -1:
284             module_definition["module_description"] = line[line.find("\\brief") + len("\\brief "):]
285     module_dict = {}
286     if "module_name" in module_definition.keys():
287         module_name = module_definition[const.MODULE_DEFINITION["module_name"]]
288         del module_definition[const.MODULE_DEFINITION["module_name"]]
289         module_dict[module_name] = {}
290         if const.MODULE_DEFINITION["module_depends"] in module_definition.keys():
291             if type(module_definition[const.MODULE_DEFINITION["module_depends"]]) == str:
292                 module_definition[const.MODULE_DEFINITION["module_depends"]] = (module_definition[const.MODULE_DEFINITION["module_depends"]],)
293             module_dict[module_name]["depends"] = module_definition[const.MODULE_DEFINITION["module_depends"]]
294             del module_definition[const.MODULE_DEFINITION["module_depends"]]
295         else:
296             module_dict[module_name]["depends"] = ()
297         if const.MODULE_DEFINITION["module_configuration"] in module_definition.keys():
298             module_dict[module_name]["configuration"] = module_definition[const.MODULE_DEFINITION["module_configuration"]]
299             del module_definition[const.MODULE_DEFINITION["module_configuration"]]
300         else:
301             module_dict[module_name]["configuration"] = ""
302         if "module_description" in module_definition.keys():
303             module_dict[module_name]["description"] = module_definition["module_description"]
304             del module_definition["module_description"]
305         if const.MODULE_DEFINITION["module_harvard"] in module_definition.keys():
306             harvard = module_definition[const.MODULE_DEFINITION["module_harvard"]]
307             if harvard == "both" or harvard == "pgm_memory":
308                 module_dict[module_name]["harvard"] = harvard
309             del module_definition[const.MODULE_DEFINITION["module_harvard"]]
310         module_dict[module_name]["constants"] = module_definition
311         module_dict[module_name]["enabled"] = False
312     return to_be_parsed, module_dict
313
314 def loadDefineLists(comment_list):
315     define_list = {}
316     for comment in comment_list:
317         for num, line in enumerate(comment):
318             index = line.find("$WIZ$")
319             if index != -1:
320                 try:
321                     exec line[index + len("$WIZ$ "):] in {}, define_list
322                 except:
323                     raise ParseError(num, line[index:])
324     for key, value in define_list.items():
325         if type(value) == str:
326             define_list[key] = (value,)
327     return define_list
328
329 def getDescriptionInformations(comment): 
330     """ 
331     Take the doxygen comment and strip the wizard informations, returning the tuple 
332     (comment, wizard_information) 
333     """
334     brief = ""
335     description = ""
336     information = {}
337     for num, line in enumerate(comment):
338         index = line.find("$WIZ$")
339         if index != -1:
340             if len(brief) == 0:
341                 brief += line[:index].strip()
342             else:
343                 description += " " + line[:index]
344             try:
345                 exec line[index + len("$WIZ$ "):] in {}, information
346             except:
347                 raise ParseError(num, line[index:])
348         else:
349             if len(brief) == 0:
350                 brief += line.strip()
351             else:
352                 description += " " + line
353                 description = description.strip()
354     return brief.strip(), description.strip(), information
355
356 def getDefinitionBlocks(text):
357     """
358     Take a text and return a list of tuple (description, name-value).
359     """
360     block = []
361     block_tmp = re.finditer(r"/\*{2}\s*([^*]*\*(?:[^/*][^*]*\*+)*)/\s*#define\s+((?:[^/]*?/?)+)\s*?(?:/{2,3}[^<].*?)?$", text, re.MULTILINE)
362     for match in block_tmp:
363         # Only the first element is needed
364         comment = match.group(1)
365         define = match.group(2)
366         start = match.start()
367         block.append(([re.findall(r"^\s*\* *(.*?)$", line, re.MULTILINE)[0] for line in comment.splitlines()], define, start))
368     for match in re.finditer(r"/{3}\s*([^<].*?)\s*#define\s+((?:[^/]*?/?)+)\s*?(?:/{2,3}[^<].*?)?$", text, re.MULTILINE):
369         comment = match.group(1)
370         define = match.group(2)
371         start = match.start()
372         block.append(([comment], define, start))
373     for match in re.finditer(r"#define\s*(.*?)\s*/{3}<\s*(.+?)\s*?(?:/{2,3}[^<].*?)?$", text, re.MULTILINE):
374         comment = match.group(2)
375         define = match.group(1)
376         start = match.start()
377         block.append(([comment], define, start))
378     return block
379
380 def loadModuleData(project):
381     module_info_dict = {}
382     list_info_dict = {}
383     configuration_info_dict = {}
384     file_dict = {}
385     for filename, path in findDefinitions("*.h", project) + findDefinitions("*.c", project) + findDefinitions("*.s", project) + findDefinitions("*.S", project):
386         comment_list = getCommentList(open(path + "/" + filename, "r").read())
387         if len(comment_list) > 0:
388             module_info = {}
389             configuration_info = {}
390             try:
391                 to_be_parsed, module_dict = loadModuleDefinition(comment_list[0])
392             except ParseError, err:
393                 raise DefineException.ModuleDefineException(path, err.line_number, err.line)
394             for module, information in module_dict.items():
395                 information["category"] = os.path.basename(path)
396                 if "configuration" in information.keys() and len(information["configuration"]):
397                     configuration = module_dict[module]["configuration"]
398                     try:
399                         configuration_info[configuration] = loadConfigurationInfos(project.info("SOURCES_PATH") + "/" + configuration)
400                     except ParseError, err:
401                         raise DefineException.ConfigurationDefineException(project.info("SOURCES_PATH") + "/" + configuration, err.line_number, err.line)
402             module_info_dict.update(module_dict)
403             configuration_info_dict.update(configuration_info)
404             if to_be_parsed:
405                 try:
406                     list_dict = loadDefineLists(comment_list[1:])
407                     list_info_dict.update(list_dict)
408                 except ParseError, err:
409                     raise DefineException.EnumDefineException(path, err.line_number, err.line)
410     for filename, path in findDefinitions("*_" + project.info("CPU_INFOS")["TOOLCHAIN"] + ".h", project):
411         comment_list = getCommentList(open(path + "/" + filename, "r").read())
412         list_info_dict.update(loadDefineLists(comment_list))
413     for tag in project.info("CPU_INFOS")["CPU_TAGS"]:
414         for filename, path in findDefinitions("*_" + tag + ".h", project):
415             comment_list = getCommentList(open(path + "/" + filename, "r").read())
416             list_info_dict.update(loadDefineLists(comment_list))
417     project.setInfo("MODULES", module_info_dict)
418     project.setInfo("LISTS", list_info_dict)
419     project.setInfo("CONFIGURATIONS", configuration_info_dict)
420     project.setInfo("FILES", file_dict)
421     
422 def formatParamNameValue(text):
423     """
424     Take the given string and return a tuple with the name of the parameter in the first position
425     and the value in the second.
426     """
427     block = re.findall("\s*([^\s]+)\s*(.+?)\s*$", text, re.MULTILINE)
428     return block[0]
429
430 def loadConfigurationInfos(path):
431     """
432     Return the module configurations found in the given file as a dict with the
433     parameter name as key and a dict containig the fields above as value:
434         "value": the value of the parameter
435         "description": the description of the parameter
436         "informations": a dict containig optional informations:
437             "type": "int" | "boolean" | "enum"
438             "min": the minimum value for integer parameters
439             "max": the maximum value for integer parameters
440             "long": boolean indicating if the num is a long
441             "unsigned": boolean indicating if the num is an unsigned
442             "value_list": the name of the enum for enum parameters
443     """
444     configuration_infos = {}
445     configuration_infos["paramlist"] = []
446     for comment, define, start in getDefinitionBlocks(open(path, "r").read()):
447         name, value = formatParamNameValue(define)
448         brief, description, informations = getDescriptionInformations(comment)
449         configuration_infos["paramlist"].append((start, name))
450         configuration_infos[name] = {}
451         configuration_infos[name]["value"] = value
452         configuration_infos[name]["informations"] = informations
453         if not "type" in configuration_infos[name]["informations"]:
454             configuration_infos[name]["informations"]["type"] = findParameterType(configuration_infos[name])
455         if ("type" in configuration_infos[name]["informations"].keys() and
456                 configuration_infos[name]["informations"]["type"] == "int" and
457                 configuration_infos[name]["value"].find("L") != -1):
458             configuration_infos[name]["informations"]["long"] = True
459             configuration_infos[name]["value"] = configuration_infos[name]["value"].replace("L", "")
460         if ("type" in configuration_infos[name]["informations"].keys() and
461                 configuration_infos[name]["informations"]["type"] == "int" and
462                 configuration_infos[name]["value"].find("U") != -1):
463             configuration_infos[name]["informations"]["unsigned"] = True
464             configuration_infos[name]["value"] = configuration_infos[name]["value"].replace("U", "")
465         configuration_infos[name]["description"] = description
466         configuration_infos[name]["brief"] = brief
467     return configuration_infos
468
469 def findParameterType(parameter):
470     if "value_list" in parameter["informations"]:
471         return "enum"
472     if "min" in parameter["informations"] or "max" in parameter["informations"] or re.match(r"^\d+U?L?$", parameter["value"]) != None:
473         return "int"
474
475 def sub(string, parameter, value):
476     """
477     Substitute the given value at the given parameter define in the given string
478     """
479     return re.sub(r"(?P<define>#define\s+" + parameter + r"\s+)([^\s]+)", r"\g<define>" + value, string)
480
481 def isInt(informations):
482     """
483     Return True if the value is a simple int.
484     """
485     if ("long" not in informatios.keys() or not informations["long"]) and ("unsigned" not in informations.keys() or informations["unsigned"]):
486         return True
487     else:
488         return False
489
490 def isLong(informations):
491     """
492     Return True if the value is a long.
493     """
494     if "long" in informations.keys() and informations["long"] and "unsigned" not in informations.keys():
495         return True
496     else:
497         return False
498
499 def isUnsigned(informations):
500     """
501     Return True if the value is an unsigned.
502     """
503     if "unsigned" in informations.keys() and informations["unsigned"] and "long" not in informations.keys():
504         return True
505     else:
506         return False
507
508 def isUnsignedLong(informations):
509     """
510     Return True if the value is an unsigned long.
511     """
512     if "unsigned" in informations.keys() and "long" in informations.keys() and informations["unsigned"] and informations["long"]:
513         return True
514     else:
515         return False
516
517 class ParseError(Exception):
518     def __init__(self, line_number, line):
519         Exception.__init__(self)
520         self.line_number = line_number
521         self.line = line