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