Aggiunte nuove meccaniche di load dei progetti e nuovi files di progetto.
[bertos.git] / wizard / bertos_utils.py
1 #!/usr/bin/env python
2 # encoding: utf-8
3 #
4 # This file is part of BeRTOS.
5 #
6 # Bertos is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19 #
20 # As a special exception, you may use this file as part of a free software
21 # library without restriction.  Specifically, if other files instantiate
22 # templates or use macros or inline functions from this file, or you compile
23 # this file and link it with other files to produce an executable, this
24 # file does not by itself cause the resulting executable to be covered by
25 # the GNU General Public License.  This exception does not however
26 # invalidate any other reasons why the executable file might be covered by
27 # the GNU General Public License.
28 #
29 # Copyright 2008 Develer S.r.l. (http://www.develer.com/)
30 #
31 # $Id$
32 #
33 # Author: Lorenzo Berni <duplo@develer.com>
34 #
35
36 import os
37 import fnmatch
38 import glob
39 import re
40 import shutil
41 # Use custom copytree function
42 import copytree
43 import pickle
44
45 import const
46 import plugins
47 import DefineException
48 import BProject
49
50 def isBertosDir(directory):
51    return os.path.exists(directory + "/VERSION")
52
53 def bertosVersion(directory):
54    return open(directory + "/VERSION").readline().strip()
55
56 def loadBertosProject(project_file):
57     project_data = pickle.loads(open(project_file, "r").read())
58     project_info = BProject.BProject()
59     project_info.setInfo("PROJECT_PATH", os.path.dirname(project_file))
60     project_info.setInfo("SOURCES_PATH", project_data["SOURCES_PATH"])
61     loadSourceTree(project_info)
62     cpu_name = project_data["CPU_NAME"]
63     project_info.setInfo("CPU_NAME", cpu_name)
64     cpu_info = loadCpuInfos(project_info)
65     for cpu in cpu_info:
66         print cpu["CPU_NAME"], cpu_name
67         if cpu["CPU_NAME"] == cpu_name:
68             print "sono uguali"
69             project_info.setInfo("CPU_INFOS", cpu)
70             break
71     loadModuleData(project_info)
72     print project_info
73
74
75 def projectFileGenerator(project_info):
76     directory = project_info.info("PROJECT_PATH")
77     project_data = {}
78     enabled_modules = []
79     for module, information in project_info.info("MODULES").items():
80         if information["enabled"]:
81             enabled_modules.append(module)
82     project_data["ENABLED_MODULES"] = enabled_modules
83     project_data["SOURCES_PATH"] = project_info.info("SOURCES_PATH")
84     project_data["TOOLCHAIN"] = project_info.info("TOOLCHAIN")
85     project_data["CPU_NAME"] = project_info.info("CPU_NAME")
86     print project_info.info("CPU_NAME")
87     project_data["SELECTED_FREQ"] = project_info.info("SELECTED_FREQ")
88     return pickle.dumps(project_data)
89
90 def createBertosProject(project_info):
91     directory = project_info.info("PROJECT_PATH")
92     sources_dir = project_info.info("SOURCES_PATH")
93     if os.path.isdir(directory):
94         shutil.rmtree(directory, True)        
95     os.makedirs(directory)
96     f = open(directory + "/project.bertos", "w")
97     f.write(projectFileGenerator(project_info))
98     f.close()
99     # Destination source dir
100     srcdir = directory + "/bertos"
101     shutil.rmtree(srcdir, True)
102     copytree.copytree(sources_dir + "/bertos", srcdir, ignore_list=const.IGNORE_LIST)
103     # Destination makefile
104     makefile = directory + "/Makefile"
105     if os.path.exists(makefile):
106         os.remove(makefile)
107     makefile = open("mktemplates/Makefile").read()
108     makefile = makefileGenerator(project_info, makefile)
109     open(directory + "/Makefile", "w").write(makefile)
110     # Destination project dir
111     prjdir = directory + "/" + os.path.basename(directory)
112     shutil.rmtree(prjdir, True)
113     os.mkdir(prjdir)
114     # Destination hw files
115     hwdir = prjdir + "/hw"
116     shutil.rmtree(hwdir, True)
117     os.mkdir(hwdir)
118     # Copy all the hw files
119     for module, information in project_info.info("MODULES").items():
120         for hwfile in information["hw"]:
121             string = open(sources_dir + "/" + hwfile, "r").read()
122             open(hwdir + "/" + os.path.basename(hwfile), "w").write(string)
123     # Destination configurations files
124     cfgdir = prjdir + "/cfg"
125     shutil.rmtree(cfgdir, True)
126     os.mkdir(cfgdir)
127     # Set to 1 the autoenabled for enabled modules
128     for module, information in project_info.info("MODULES").items():
129         if information["enabled"] and "configuration" in information and information["configuration"] != "":
130             configurations = project_info.info("CONFIGURATIONS")
131             configuration = configurations[information["configuration"]]
132             for start, parameter in configuration["paramlist"]:
133                 if "type" in configuration[parameter]["informations"] and configuration[parameter]["informations"]["type"] == "autoenabled":
134                     configuration[parameter]["value"] = "1"
135             project_info.setInfo("CONFIGURATIONS", configurations)
136     # Copy all the configuration files
137     for configuration, information in project_info.info("CONFIGURATIONS").items():
138         string = open(sources_dir + "/" + configuration, "r").read()
139         for start, parameter in information["paramlist"]:
140             infos = information[parameter]
141             value = infos["value"]
142             if "unsigned" in infos["informations"] and infos["informations"]["unsigned"]:
143                 value += "U"
144             if "long" in infos["informations"] and infos["informations"]["long"]:
145                 value += "L"
146             string = sub(string, parameter, value)
147         f = open(cfgdir + "/" + os.path.basename(configuration), "w")
148         f.write(string)
149         f.close()
150     # Destinatio mk file
151     makefile = open("mktemplates/template.mk", "r").read()
152     makefile = mkGenerator(project_info, makefile)
153     open(prjdir + "/" + os.path.basename(prjdir) + ".mk", "w").write(makefile)
154     # Destination main.c file
155     main = open("srctemplates/main.c", "r").read()
156     open(prjdir + "/main.c", "w").write(main)
157     # Files for selected plugins
158     relevants_files = {}
159     for plugin in project_info.info("OUTPUT"):
160         module = loadPlugin(plugin)
161         relevants_files[plugin] = module.createProject(project_info)
162     project_info.setInfo("RELEVANT_FILES", relevants_files)
163
164 def loadPlugin(plugin):
165     """
166     Returns the given plugin module.
167     """
168     return getattr(__import__("plugins", {}, {}, [plugin]), plugin)
169     
170 def mkGenerator(project_info, makefile):
171     """
172     Generates the mk file for the current project.
173     """
174     mk_data = {}
175     mk_data["$pname"] = os.path.basename(project_info.info("PROJECT_PATH"))
176     mk_data["$cpuflag"] = project_info.info("CPU_INFOS")["CPU_FLAG_NAME"]
177     mk_data["$cpuname"] = project_info.info("CPU_INFOS")["CORE_CPU"]
178     mk_data["$cpuclockfreq"] = project_info.info("SELECTED_FREQ")
179     mk_data["$cflags"] = " ".join(project_info.info("CPU_INFOS")["C_FLAGS"])
180     mk_data["$ldflags"] = " ".join(project_info.info("CPU_INFOS")["LD_FLAGS"])
181     mk_data["$cppflags"] = " ".join(project_info.info("CPU_INFOS")["CPP_FLAGS"])
182     mk_data["$cppaflags"] = " ".join(project_info.info("CPU_INFOS")["CPPA_FLAGS"])
183     mk_data["$cxxflags"] = " ".join(project_info.info("CPU_INFOS")["CXX_FLAGS"])
184     mk_data["$asflags"] = " ".join(project_info.info("CPU_INFOS")["AS_FLAGS"])
185     mk_data["$arflags"] = " ".join(project_info.info("CPU_INFOS")["AR_FLAGS"])
186     mk_data["$csrc"], mk_data["$pcsrc"], mk_data["$cppasrc"], mk_data["$cxxsrc"], mk_data["$asrc"], mk_data["$constants"] = csrcGenerator(project_info)
187     mk_data["$prefix"] = replaceSeparators(project_info.info("TOOLCHAIN")["path"].split("gcc")[0])
188     mk_data["$suffix"] = replaceSeparators(project_info.info("TOOLCHAIN")["path"].split("gcc")[1])
189     mk_data["$main"] = os.path.basename(project_info.info("PROJECT_PATH")) + "/main.c"
190     for key in mk_data:
191         while makefile.find(key) != -1:
192             makefile = makefile.replace(key, mk_data[key])
193     return makefile
194
195 def makefileGenerator(project_info, makefile):
196     """
197     Generate the Makefile for the current project.
198     """
199     # TODO write a general function that works for both the mk file and the Makefile
200     while makefile.find("project_name") != -1:
201         makefile = makefile.replace("project_name", os.path.basename(project_info.info("PROJECT_PATH")))
202     return makefile
203
204 def csrcGenerator(project_info):
205     modules = project_info.info("MODULES")
206     files = project_info.info("FILES")
207     if "harvard" in project_info.info("CPU_INFOS")["CPU_TAGS"]:
208         harvard = True
209     else:
210         harvard = False
211     # file to be included in CSRC variable
212     csrc = []
213     # file to be included in PCSRC variable
214     pcsrc = []
215     # files to be included in CPPASRC variable
216     cppasrc = []
217     # files to be included in CXXSRC variable
218     cxxsrc = []
219     # files to be included in ASRC variable
220     asrc = []
221     # constants to be included at the beginning of the makefile
222     constants = {}
223     for module, information in modules.items():
224         module_files = set([])
225         dependency_files = set([])
226         # assembly sources
227         asm_files = set([])
228         hwdir = os.path.basename(project_info.info("PROJECT_PATH")) + "/hw" 
229         if information["enabled"]:
230             if "constants" in information:
231                 constants.update(information["constants"])
232             cfiles, sfiles = findModuleFiles(module, project_info)
233             module_files |= set(cfiles)
234             asm_files |= set(sfiles)
235             for file in information["hw"]:
236                 if file.endswith(".c"):
237                     module_files |= set([hwdir + "/" + os.path.basename(file)])
238             for file_dependency in information["depends"]:
239                 if file_dependency in files:
240                     dependencyCFiles, dependencySFiles = findModuleFiles(file_dependency, project_info)
241                     dependency_files |= set(dependencyCFiles)
242                     asm_files |= set(dependencySFiles)
243             for file in module_files:
244                 if not harvard or information.get("harvard", "both") == "both":
245                     csrc.append(file)
246                 if harvard and "harvard" in information:
247                     pcsrc.append(file)
248             for file in dependency_files:
249                 csrc.append(file)
250             for file in project_info.info("CPU_INFOS")["C_SRC"]:
251                 csrc.append(file)
252             for file in project_info.info("CPU_INFOS")["PC_SRC"]:
253                 pcsrc.append(file)
254             for file in asm_files:
255                 cppasrc.append(file)
256     for file in project_info.info("CPU_INFOS")["CPPA_SRC"]:
257         cppasrc.append(file)
258     for file in project_info.info("CPU_INFOS")["CXX_SRC"]:
259         cxxsrc.append(file)
260     for file in project_info.info("CPU_INFOS")["ASRC"]:
261         asrc.append(file)
262     csrc = " \\\n\t".join(csrc) + " \\"
263     pcsrc = " \\\n\t".join(pcsrc) + " \\"
264     cppasrc = " \\\n\t".join(cppasrc) + " \\"
265     cxxsrc = " \\\n\t".join(cxxsrc) + " \\"
266     asrc = " \\\n\t".join(asrc) + " \\"
267     constants = "\n".join([os.path.basename(project_info.info("PROJECT_PATH")) + "_" + key + " = " + unicode(value) for key, value in constants.items()])
268     return csrc, pcsrc, cppasrc, cxxsrc, asrc, constants
269
270 def findModuleFiles(module, project_info):
271     # Find the files related to the selected module
272     cfiles = []
273     sfiles = []
274     # .c files related to the module and the cpu architecture
275     for filename, path in findDefinitions(module + ".c", project_info) + \
276             findDefinitions(module + "_" + project_info.info("CPU_INFOS")["TOOLCHAIN"] + ".c", project_info):
277         path = path.replace(project_info.info("SOURCES_PATH") + os.sep, "")
278         path = replaceSeparators(path)
279         cfiles.append(path + "/" + filename)
280     # .s files related to the module and the cpu architecture
281     for filename, path in findDefinitions(module + ".s", project_info) + \
282             findDefinitions(module + "_" + project_info.info("CPU_INFOS")["TOOLCHAIN"] + ".s", project_info) + \
283             findDefinitions(module + ".S", project_info) + \
284             findDefinitions(module + "_" + project_info.info("CPU_INFOS")["TOOLCHAIN"] + ".S", project_info):
285         path = path.replace(project_info.info("SOURCES_PATH") + os.sep, "")
286         path = replaceSeparators(path)
287         sfiles.append(path + "/" + filename)
288     # .c and .s files related to the module and the cpu tags
289     for tag in project_info.info("CPU_INFOS")["CPU_TAGS"]:
290         for filename, path in findDefinitions(module + "_" + tag + ".c", project_info):
291             path = path.replace(project_info.info("SOURCES_PATH") + os.sep, "")
292             if os.sep != "/":
293                 path = replaceSeparators(path)
294             cfiles.append(path + "/" + filename)
295         for filename, path in findDefinitions(module + "_" + tag + ".s", project_info) + \
296                 findDefinitions(module + "_" + tag + ".S", project_info):
297             path = path.replace(project_info.info("SOURCES_PATH") + os.sep, "")
298             path = replaceSeparators(path)
299             sfiles.append(path + "/" + filename)
300     return cfiles, sfiles
301
302 def replaceSeparators(path):
303     """
304     Replace the separators in the given path with unix standard separator.
305     """
306     if os.sep != "/":
307         while path.find(os.sep) != -1:
308             path = path.replace(os.sep, "/")
309     return path
310
311 def getSystemPath():
312     path = os.environ["PATH"]
313     if os.name == "nt":
314         path = path.split(";")
315     else:
316         path = path.split(":")
317     return path
318
319 def findToolchains(path_list):
320     toolchains = []
321     for element in path_list:
322         for toolchain in glob.glob(element+ "/" + const.GCC_NAME):
323             toolchains.append(toolchain)
324     return list(set(toolchains))
325
326 def getToolchainInfo(output):
327     info = {}
328     expr = re.compile("Target: .*")
329     target = expr.findall(output)
330     if len(target) == 1:
331         info["target"] = target[0].split("Target: ")[1]
332     expr = re.compile("gcc version [0-9,.]*")
333     version = expr.findall(output)
334     if len(version) == 1:
335         info["version"] = version[0].split("gcc version ")[1]
336     expr = re.compile("gcc version [0-9,.]* \(.*\)")
337     build = expr.findall(output)
338     if len(build) == 1:
339         build = build[0].split("gcc version ")[1]
340         build = build[build.find("(") + 1 : build.find(")")]
341         info["build"] = build
342     expr = re.compile("Configured with: .*")
343     configured = expr.findall(output)
344     if len(configured) == 1:
345         info["configured"] = configured[0].split("Configured with: ")[1]
346     expr = re.compile("Thread model: .*")
347     thread = expr.findall(output)
348     if len(thread) == 1:
349         info["thread"] = thread[0].split("Thread model: ")[1]
350     return info
351
352 def loadSourceTree(project):
353     fileList = [f for f in os.walk(project.info("SOURCES_PATH"))]
354     project.setInfo("FILE_LIST", fileList)
355
356 def findDefinitions(ftype, project):
357     L = project.info("FILE_LIST")
358     definitions = []
359     for element in L:
360         for filename in element[2]:
361             if fnmatch.fnmatch(filename, ftype):
362                 definitions.append((filename, element[0]))
363     return definitions
364
365 def loadCpuInfos(project):
366     cpuInfos = []
367     for definition in findDefinitions(const.CPU_DEFINITION, project):
368         cpuInfos.append(getInfos(definition))
369     return cpuInfos
370
371 def getTagSet(cpu_info):
372     tag_set = set([])
373     for cpu in cpu_info:
374         tag_set |= set([cpu["CPU_NAME"]])
375         tag_set |= set(cpu["CPU_TAGS"])
376         tag_set |= set([cpu["CORE_CPU"]])
377         tag_set |= set([cpu["TOOLCHAIN"]])
378     return tag_set
379         
380
381 def getInfos(definition):
382     D = {}
383     D.update(const.CPU_DEF)
384     def include(filename, dict = D, directory=definition[1]):
385         execfile(directory + "/" + filename, {}, D)
386     D["include"] = include
387     include(definition[0], D)
388     D["CPU_NAME"] = definition[0].split(".")[0]
389     D["DEFINITION_PATH"] = definition[1] + "/" + definition[0]
390     del D["include"]
391     return D
392
393 def getCommentList(string):
394     comment_list = re.findall(r"/\*{2}\s*([^*]*\*(?:[^/*][^*]*\*+)*)/", string)
395     comment_list = [re.findall(r"^\s*\* *(.*?)$", comment, re.MULTILINE) for comment in comment_list]
396     return comment_list
397
398 def loadModuleDefinition(first_comment):
399     to_be_parsed = False
400     module_definition = {}
401     for num, line in enumerate(first_comment):
402         index = line.find("$WIZ$")
403         if index != -1:
404             to_be_parsed = True
405             try:
406                 exec line[index + len("$WIZ$ "):] in {}, module_definition
407             except:
408                 raise ParseError(num, line[index:])
409         elif line.find("\\brief") != -1:
410             module_definition["module_description"] = line[line.find("\\brief") + len("\\brief "):]
411     module_dict = {}
412     if "module_name" in module_definition:
413         module_name = module_definition[const.MODULE_DEFINITION["module_name"]]
414         del module_definition[const.MODULE_DEFINITION["module_name"]]
415         module_dict[module_name] = {}
416         if const.MODULE_DEFINITION["module_depends"] in module_definition:
417             depends = module_definition[const.MODULE_DEFINITION["module_depends"]]
418             del module_definition[const.MODULE_DEFINITION["module_depends"]]
419             if type(depends) == str:
420                 depends = (depends,)
421             module_dict[module_name]["depends"] = depends
422         else:
423             module_dict[module_name]["depends"] = ()
424         if const.MODULE_DEFINITION["module_configuration"] in module_definition:
425             module_dict[module_name]["configuration"] = module_definition[const.MODULE_DEFINITION["module_configuration"]]
426             del module_definition[const.MODULE_DEFINITION["module_configuration"]]
427         else:
428             module_dict[module_name]["configuration"] = ""
429         if "module_description" in module_definition:
430             module_dict[module_name]["description"] = module_definition["module_description"]
431             del module_definition["module_description"]
432         if const.MODULE_DEFINITION["module_harvard"] in module_definition:
433             harvard = module_definition[const.MODULE_DEFINITION["module_harvard"]]
434             if harvard == "both" or harvard == "pgm_memory":
435                 module_dict[module_name]["harvard"] = harvard
436             del module_definition[const.MODULE_DEFINITION["module_harvard"]]
437         if const.MODULE_DEFINITION["module_hw"] in module_definition:
438             hw = module_definition[const.MODULE_DEFINITION["module_hw"]]
439             del module_definition[const.MODULE_DEFINITION["module_hw"]]
440             if type(hw) == str:
441                 hw = (hw, )
442             module_dict[module_name]["hw"] = hw
443         else:
444             module_dict[module_name]["hw"] = ()
445         if const.MODULE_DEFINITION["module_supports"] in module_definition:
446             supports = module_definition[const.MODULE_DEFINITION["module_supports"]]
447             del module_definition[const.MODULE_DEFINITION["module_supports"]]
448             module_dict[module_name]["supports"] = supports
449         module_dict[module_name]["constants"] = module_definition
450         module_dict[module_name]["enabled"] = False
451     return to_be_parsed, module_dict
452
453 def isSupported(project, module=None, property_id=None):
454     if not module and property_id:
455         item = project.info("CONFIGURATIONS")[property_id[0]][property_id[1]]["informations"]
456     else:
457         item = project.info("MODULES")[module]
458     tag_dict = project.info("ALL_CPU_TAGS")
459     if "supports" in item:
460         support_string = item["supports"]
461         supported = {}
462         try:
463             exec "supported = " + support_string in tag_dict, supported
464         except:
465             raise SupportedException(support_string)
466         return supported["supported"]
467     else:
468         return True
469
470 def loadDefineLists(comment_list):
471     define_list = {}
472     for comment in comment_list:
473         for num, line in enumerate(comment):
474             index = line.find("$WIZ$")
475             if index != -1:
476                 try:
477                     exec line[index + len("$WIZ$ "):] in {}, define_list
478                 except:
479                     raise ParseError(num, line[index:])
480     for key, value in define_list.items():
481         if type(value) == str:
482             define_list[key] = (value,)
483     return define_list
484
485 def getDescriptionInformations(comment):
486     """
487     Take the doxygen comment and strip the wizard informations, returning the tuple
488     (comment, wizard_information)
489     """
490     brief = ""
491     description = ""
492     information = {}
493     for num, line in enumerate(comment):
494         index = line.find("$WIZ$")
495         if index != -1:
496             if len(brief) == 0:
497                 brief += line[:index].strip()
498             else:
499                 description += " " + line[:index]
500             try:
501                 exec line[index + len("$WIZ$ "):] in {}, information
502             except:
503                 raise ParseError(num, line[index:])
504         else:
505             if len(brief) == 0:
506                 brief += line.strip()
507             else:
508                 description += " " + line
509                 description = description.strip()
510     return brief.strip(), description.strip(), information
511
512 def getDefinitionBlocks(text):
513     """
514     Take a text and return a list of tuple (description, name-value).
515     """
516     block = []
517     block_tmp = re.finditer(r"/\*{2}\s*([^*]*\*(?:[^/*][^*]*\*+)*)/\s*#define\s+((?:[^/]*?/?)+)\s*?(?:/{2,3}[^<].*?)?$", text, re.MULTILINE)
518     for match in block_tmp:
519         # Only the first element is needed
520         comment = match.group(1)
521         define = match.group(2)
522         start = match.start()
523         block.append(([re.findall(r"^\s*\* *(.*?)$", line, re.MULTILINE)[0] for line in comment.splitlines()], define, start))
524     for match in re.finditer(r"/{3}\s*([^<].*?)\s*#define\s+((?:[^/]*?/?)+)\s*?(?:/{2,3}[^<].*?)?$", text, re.MULTILINE):
525         comment = match.group(1)
526         define = match.group(2)
527         start = match.start()
528         block.append(([comment], define, start))
529     for match in re.finditer(r"#define\s*(.*?)\s*/{3}<\s*(.+?)\s*?(?:/{2,3}[^<].*?)?$", text, re.MULTILINE):
530         comment = match.group(2)
531         define = match.group(1)
532         start = match.start()
533         block.append(([comment], define, start))
534     return block
535
536 def loadModuleData(project):
537     module_info_dict = {}
538     list_info_dict = {}
539     configuration_info_dict = {}
540     file_dict = {}
541     for filename, path in findDefinitions("*.h", project) + findDefinitions("*.c", project) + findDefinitions("*.s", project) + findDefinitions("*.S", project):
542         comment_list = getCommentList(open(path + "/" + filename, "r").read())
543         if len(comment_list) > 0:
544             module_info = {}
545             configuration_info = {}
546             try:
547                 to_be_parsed, module_dict = loadModuleDefinition(comment_list[0])
548             except ParseError, err:
549                 raise DefineException.ModuleDefineException(path, err.line_number, err.line)
550             for module, information in module_dict.items():
551                 if "depends" not in information:
552                     information["depends"] = ()
553                 information["depends"] += (filename.split(".")[0],)
554                 information["category"] = os.path.basename(path)
555                 if "configuration" in information and len(information["configuration"]):
556                     configuration = module_dict[module]["configuration"]
557                     try:
558                         configuration_info[configuration] = loadConfigurationInfos(project.info("SOURCES_PATH") + "/" + configuration)
559                     except ParseError, err:
560                         raise DefineException.ConfigurationDefineException(project.info("SOURCES_PATH") + "/" + configuration, err.line_number, err.line)
561             module_info_dict.update(module_dict)
562             configuration_info_dict.update(configuration_info)
563             if to_be_parsed:
564                 try:
565                     list_dict = loadDefineLists(comment_list[1:])
566                     list_info_dict.update(list_dict)
567                 except ParseError, err:
568                     raise DefineException.EnumDefineException(path, err.line_number, err.line)
569     for filename, path in findDefinitions("*_" + project.info("CPU_INFOS")["TOOLCHAIN"] + ".h", project):
570         comment_list = getCommentList(open(path + "/" + filename, "r").read())
571         list_info_dict.update(loadDefineLists(comment_list))
572     for tag in project.info("CPU_INFOS")["CPU_TAGS"]:
573         for filename, path in findDefinitions("*_" + tag + ".h", project):
574             comment_list = getCommentList(open(path + "/" + filename, "r").read())
575             list_info_dict.update(loadDefineLists(comment_list))
576     project.setInfo("MODULES", module_info_dict)
577     project.setInfo("LISTS", list_info_dict)
578     project.setInfo("CONFIGURATIONS", configuration_info_dict)
579     project.setInfo("FILES", file_dict)
580
581 def formatParamNameValue(text):
582     """
583     Take the given string and return a tuple with the name of the parameter in the first position
584     and the value in the second.
585     """
586     block = re.findall("\s*([^\s]+)\s*(.+?)\s*$", text, re.MULTILINE)
587     return block[0]
588
589 def loadConfigurationInfos(path):
590     """
591     Return the module configurations found in the given file as a dict with the
592     parameter name as key and a dict containig the fields above as value:
593         "value": the value of the parameter
594         "description": the description of the parameter
595         "informations": a dict containig optional informations:
596             "type": "int" | "boolean" | "enum"
597             "min": the minimum value for integer parameters
598             "max": the maximum value for integer parameters
599             "long": boolean indicating if the num is a long
600             "unsigned": boolean indicating if the num is an unsigned
601             "value_list": the name of the enum for enum parameters
602     """
603     configuration_infos = {}
604     configuration_infos["paramlist"] = []
605     for comment, define, start in getDefinitionBlocks(open(path, "r").read()):
606         name, value = formatParamNameValue(define)
607         brief, description, informations = getDescriptionInformations(comment)
608         configuration_infos["paramlist"].append((start, name))
609         configuration_infos[name] = {}
610         configuration_infos[name]["value"] = value
611         configuration_infos[name]["informations"] = informations
612         if not "type" in configuration_infos[name]["informations"]:
613             configuration_infos[name]["informations"]["type"] = findParameterType(configuration_infos[name])
614         if ("type" in configuration_infos[name]["informations"] and
615                 configuration_infos[name]["informations"]["type"] == "int" and
616                 configuration_infos[name]["value"].find("L") != -1):
617             configuration_infos[name]["informations"]["long"] = True
618             configuration_infos[name]["value"] = configuration_infos[name]["value"].replace("L", "")
619         if ("type" in configuration_infos[name]["informations"] and
620                 configuration_infos[name]["informations"]["type"] == "int" and
621                 configuration_infos[name]["value"].find("U") != -1):
622             configuration_infos[name]["informations"]["unsigned"] = True
623             configuration_infos[name]["value"] = configuration_infos[name]["value"].replace("U", "")
624         configuration_infos[name]["description"] = description
625         configuration_infos[name]["brief"] = brief
626     return configuration_infos
627
628 def findParameterType(parameter):
629     if "value_list" in parameter["informations"]:
630         return "enum"
631     if "min" in parameter["informations"] or "max" in parameter["informations"] or re.match(r"^\d+U?L?$", parameter["value"]) != None:
632         return "int"
633
634 def sub(string, parameter, value):
635     """
636     Substitute the given value at the given parameter define in the given string
637     """
638     return re.sub(r"(?P<define>#define\s+" + parameter + r"\s+)([^\s]+)", r"\g<define>" + value, string)
639
640 def isInt(informations):
641     """
642     Return True if the value is a simple int.
643     """
644     if ("long" not in informatios or not informations["long"]) and ("unsigned" not in informations or informations["unsigned"]):
645         return True
646     else:
647         return False
648
649 def isLong(informations):
650     """
651     Return True if the value is a long.
652     """
653     if "long" in informations and informations["long"] and "unsigned" not in informations:
654         return True
655     else:
656         return False
657
658 def isUnsigned(informations):
659     """
660     Return True if the value is an unsigned.
661     """
662     if "unsigned" in informations and informations["unsigned"] and "long" not in informations:
663         return True
664     else:
665         return False
666
667 def isUnsignedLong(informations):
668     """
669     Return True if the value is an unsigned long.
670     """
671     if "unsigned" in informations and "long" in informations and informations["unsigned"] and informations["long"]:
672         return True
673     else:
674         return False
675
676 class ParseError(Exception):
677     def __init__(self, line_number, line):
678         Exception.__init__(self)
679         self.line_number = line_number
680         self.line = line
681
682 class SupportedException(Exception):
683     def __init__(self, support_string):
684         Exception.__init__(self)
685         self.support_string = support_string