4 # Copyright 2008 Develer S.r.l. (http://www.develer.com/)
9 # Author: Lorenzo Berni <duplo@develer.com>
19 import DefineException
21 def isBertosDir(directory):
22 return os.path.exists(directory + "/VERSION")
24 def bertosVersion(directory):
25 return open(directory + "/VERSION").readline().strip()
27 def createBertosProject(projectInfos):
28 directory = projectInfos.info("PROJECT_PATH")
29 sourcesDir = projectInfos.info("SOURCES_PATH")
30 if not os.path.isdir(directory):
32 f = open(directory + "/project.bertos", "w")
33 f.write(repr(projectInfos))
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):
43 makefile = open("mktemplates/Makefile").read()
44 makefile = makefileGenerator(projectInfos, makefile)
45 open(directory + "/Makefile", "w").write(makefile)
46 ## Destination project dir
47 prjdir = directory + "/" + os.path.basename(directory)
48 shutil.rmtree(prjdir, True)
50 ## Destination configurations files
51 cfgdir = prjdir + "/cfg"
52 shutil.rmtree(cfgdir, True)
54 for key, value in projectInfos.info("CONFIGURATIONS").items():
55 string = open(sourcesDir + "/" + key, "r").read()
56 for parameter, infos in value.items():
57 value = infos["value"]
58 if "long" in infos["informations"].keys() and infos["informations"]["long"]:
60 string = sub(string, parameter, value)
61 f = open(cfgdir + "/" + os.path.basename(key), "w")
65 makefile = open("mktemplates/template.mk", "r").read()
66 makefile = mkGenerator(projectInfos, makefile)
67 open(prjdir + "/" + os.path.basename(prjdir) + ".mk", "w").write(makefile)
69 def mkGenerator(projectInfos, makefile):
71 Generates the mk file for the current project.
74 mkData["pname"] = os.path.basename(projectInfos.info("PROJECT_PATH"))
75 mkData["cpuname"] = projectInfos.info("CPU_INFOS")["CPU_NAME"]
76 mkData["cflags"] = " ".join(projectInfos.info("CPU_INFOS")["C_FLAGS"])
77 mkData["ldflags"] = " ".join(projectInfos.info("CPU_INFOS")["LD_FLAGS"])
79 while makefile.find(key) != -1:
80 makefile = makefile.replace(key, mkData[key])
83 def makefileGenerator(projectInfos, makefile):
85 Generate the Makefile for the current project.
87 # TODO: write a general function that works for both the mk file and the Makefile
88 while makefile.find("project_name") != -1:
89 makefile = makefile.replace("project_name", os.path.basename(projectInfos.info("PROJECT_PATH")))
93 path = os.environ["PATH"]
95 path = path.split(";")
97 path = path.split(":")
100 def findToolchains(pathList):
102 for element in pathList:
103 for toolchain in glob.glob(element+ "/" + const.GCC_NAME):
104 if not os.path.islink(toolchain):
105 toolchains.append(toolchain)
106 return list(set(toolchains))
108 def getToolchainInfo(output):
110 expr = re.compile("Target: .*")
111 target = expr.findall(output)
113 info["target"] = target[0].split("Target: ")[1]
114 expr = re.compile("gcc version [0-9,.]*")
115 version = expr.findall(output)
116 if len(version) == 1:
117 info["version"] = version[0].split("gcc version ")[1]
118 expr = re.compile("gcc version [0-9,.]* \(.*\)")
119 build = expr.findall(output)
121 build = build[0].split("gcc version ")[1]
122 build = build[build.find("(") + 1 : build.find(")")]
123 info["build"] = build
124 expr = re.compile("Configured with: .*")
125 configured = expr.findall(output)
126 if len(configured) == 1:
127 info["configured"] = configured[0].split("Configured with: ")[1]
128 expr = re.compile("Thread model: .*")
129 thread = expr.findall(output)
131 info["thread"] = thread[0].split("Thread model: ")[1]
134 def findDefinitions(ftype, path):
137 for filename in element[2]:
138 if fnmatch.fnmatch(filename, ftype):
139 yield (filename, element[0])
141 def loadCpuInfos(path):
143 for definition in findDefinitions(const.CPU_DEFINITION, path):
144 cpuInfos.append(getInfos(definition))
147 def getInfos(definition):
149 D.update(const.CPU_DEF)
150 def include(filename, dict = D, directory=definition[1]):
151 execfile(directory + "/" + filename, {}, D)
152 D["include"] = include
153 include(definition[0], D)
154 D["CPU_NAME"] = definition[0].split(".")[0]
155 D["DEFINITION_PATH"] = definition[1] + "/" + definition[0]
159 def getDefinitionBlocks(text):
161 Take a text and return a list of tuple (description, name-value).
164 block_tmp = re.findall(r"/\*{2}\s*([^*]*\*(?:[^/*][^*]*\*+)*)/\s*#define\s+((?:[^/]*?/?)+)\s*?(?:/{2,3}[^<].*?)?$", text, re.MULTILINE)
165 for comment, define in block_tmp:
166 block.append((" ".join(re.findall(r"^\s*\*?\s*(.*?)\s*?(?:/{2}.*?)?$", comment, re.MULTILINE)).strip(), define))
167 block += re.findall(r"/{3}\s*([^<].*?)\s*#define\s+((?:[^/]*?/?)+)\s*?(?:/{2,3}[^<].*?)?$", text, re.MULTILINE)
168 block += [(comment, define) for define, comment in re.findall(r"#define\s*(.*?)\s*/{3}<\s*(.+?)\s*?(?:/{2,3}[^<].*?)?$", text, re.MULTILINE)]
171 def formatParamNameValue(text):
173 Take the given string and return a tuple with the name of the parameter in the first position
174 and the value in the second.
176 block = re.findall("\s*([^\s]+)\s*(.+?)\s*$", text, re.MULTILINE)
179 def getDescriptionInformations(text):
181 Take the doxygen comment and strip the wizard informations, returning the tuple
182 (comment, wizard_informations)
184 index = text.find("$WIZARD")
186 exec(text[index + 1:])
187 informations = WIZARD
188 return text[:index].strip(), informations
190 return text.strip(), {}
192 def loadConfigurationInfos(path):
194 Return the module configurations found in the given file as a dict with the
195 parameter name as key and a dict containig the fields above as value:
196 "value": the value of the parameter
197 "description": the description of the parameter
198 "informations": a dict containig optional informations:
199 "type": "int" | "boolean" | "enum"
200 "min": the minimum value for integer parameters
201 "max": the maximum value for integer parameters
202 "long": boolean indicating if the num is a long
203 "value_list": the name of the enum for enum parameters
206 configurationInfos = {}
207 for comment, define in getDefinitionBlocks(open(path, "r").read()):
208 name, value = formatParamNameValue(define)
209 description, informations = getDescriptionInformations(comment)
210 configurationInfos[name] = {}
211 configurationInfos[name]["value"] = value
212 configurationInfos[name]["informations"] = informations
213 configurationInfos[name]["description"] = description
214 return configurationInfos
216 raise DefineException.ConfigurationDefineException(path)
218 def loadModuleInfos(path):
220 Return the module infos found in the given file as a dict with the module
221 name as key and a dict containig the fields above as value or an empty dict
222 if the given file is not a BeRTOS module:
223 "depends": a list of modules needed by this module
224 "configuration": the cfg_*.h with the module configurations
225 "description": a string containing the brief description of doxygen
226 "enabled": contains False but the wizard will change if the user select
231 string = open(path, "r").read()
232 commentList = re.findall(r"/\*{2}\s*([^*]*\*(?:[^/*][^*]*\*+)*)/", string)
233 commentList = [" ".join(re.findall(r"^\s*\*?\s*(.*?)\s*?(?:/{2}.*?)?$", comment, re.MULTILINE)).strip() for comment in commentList]
234 for comment in commentList:
235 index = comment.find("$WIZARD_MODULE")
237 exec(comment[index + 1:])
238 moduleInfos[WIZARD_MODULE["name"]] = {"depends": WIZARD_MODULE["depends"],
239 "configuration": WIZARD_MODULE["configuration"],
245 raise DefineException.ModuleDefineException(path)
247 def loadModuleInfosDict(path):
249 Return the dict containig all the modules
252 for filename, path in findDefinitions("*.h", path):
253 moduleInfosDict.update(loadModuleInfos(path + "/" + filename))
254 return moduleInfosDict
256 def loadDefineLists(path):
258 Return a dict with the name of the list as key and a list of string as value
261 string = open(path, "r").read()
262 commentList = re.findall(r"/\*{2}\s*([^*]*\*(?:[^/*][^*]*\*+)*)/", string)
263 commentList = [" ".join(re.findall(r"^\s*\*?\s*(.*?)\s*?(?:/{2}.*?)?$", comment, re.MULTILINE)).strip() for comment in commentList]
265 for comment in commentList:
266 index = comment.find("$WIZARD_LIST")
268 exec(comment[index + 1:])
269 listDict.update(WIZARD_LIST)
272 raise DefineException.EnumDefineException(path)
274 def loadDefineListsDict(path):
276 Return the dict containing all the define lists
279 for filename, path in findDefinitions("*.h", path):
280 defineListsDict.update(loadDefineLists(path + "/" + filename))
281 return defineListsDict
283 def sub(string, parameter, value):
285 Substitute the given value at the given parameter define in the given string
287 return re.sub(r"(?P<define>#define\s+" + parameter + r"\s+)([^\s]+)", r"\g<define>" + value, string)