4 # Copyright 2008 Develer S.r.l. (http://www.develer.com/)
9 # Author: Lorenzo Berni <duplo@develer.com>
20 def isBertosDir(directory):
21 return os.path.exists(directory + "/VERSION")
23 def bertosVersion(directory):
24 return open(directory + "/VERSION").readline().strip()
26 def createBertosProject(projectInfos):
27 directory = projectInfos.info("PROJECT_PATH")
28 sourcesDir = projectInfos.info("SOURCES_PATH")
29 if not os.path.isdir(directory):
31 f = open(directory + "/project.bertos", "w")
32 f.write(repr(projectInfos))
34 ## Destination source dir
35 srcdir = directory + "/bertos"
36 shutil.rmtree(srcdir, True)
37 shutil.copytree(sourcesDir + "/bertos", srcdir)
38 ## Destination makefile
39 makefile = directory + "/Makefile"
40 if os.path.exists(makefile):
42 makefile = open("mktemplates/Makefile").read()
43 makefile = makefileGenerator(projectInfos, makefile)
44 open(directory + "/Makefile", "w").write(makefile)
45 ## Destination project dir
46 prjdir = directory + "/" + os.path.basename(directory)
47 shutil.rmtree(prjdir, True)
49 ## Destination configurations files
50 cfgdir = prjdir + "/cfg"
51 shutil.rmtree(cfgdir, True)
53 for key, value in projectInfos.info("CONFIGURATIONS").items():
54 string = open(sourcesDir + "/" + key, "r").read()
55 for parameter, infos in value.items():
56 value = infos["value"]
57 if "long" in infos["informations"].keys() and infos["informations"]["long"]:
59 string = sub(string, parameter, value)
60 f = open(cfgdir + "/" + os.path.basename(key), "w")
64 makefile = open("mktemplates/template.mk", "r").read()
65 makefile = mkGenerator(projectInfos, makefile)
66 open(prjdir + "/" + "project.mk", "w").write(makefile)
68 def mkGenerator(projectInfos, makefile):
70 Generates the mk file for the current project.
73 mkData["pname"] = os.path.basename(projectInfos.info("PROJECT_PATH"))
74 mkData["cpuname"] = projectInfos.info("CPU_INFOS")["CPU_NAME"]
75 mkData["cflags"] = " ".join(projectInfos.info("CPU_INFOS")["C_FLAGS"])
76 mkData["ldflags"] = " ".join(projectInfos.info("CPU_INFOS")["LD_FLAGS"])
78 while makefile.find(key) != -1:
79 makefile = makefile.replace(key, mkData[key])
82 def makefileGenerator(projectInfos, makefile):
84 Generate the Makefile for the current project.
86 # TODO: write a general function that works for both the mk file and the Makefile
87 while makefile.find("project_name") != -1:
88 makefile = makefile.replace("project_name", os.path.basename(projectInfos.info("PROJECT_PATH")))
92 path = os.environ["PATH"]
94 path = path.split(";")
96 path = path.split(":")
99 def findToolchains(pathList):
101 for element in pathList:
102 for toolchain in glob.glob(element+ "/" + const.GCC_NAME):
103 if not os.path.islink(toolchain):
104 toolchains.append(toolchain)
105 return list(set(toolchains))
107 def getToolchainInfo(output):
109 expr = re.compile("Target: .*")
110 target = expr.findall(output)
112 info["target"] = target[0].split("Target: ")[1]
113 expr = re.compile("gcc version [0-9,.]*")
114 version = expr.findall(output)
115 if len(version) == 1:
116 info["version"] = version[0].split("gcc version ")[1]
117 expr = re.compile("gcc version [0-9,.]* \(.*\)")
118 build = expr.findall(output)
120 build = build[0].split("gcc version ")[1]
121 build = build[build.find("(") + 1 : build.find(")")]
122 info["build"] = build
123 expr = re.compile("Configured with: .*")
124 configured = expr.findall(output)
125 if len(configured) == 1:
126 info["configured"] = configured[0].split("Configured with: ")[1]
127 expr = re.compile("Thread model: .*")
128 thread = expr.findall(output)
130 info["thread"] = thread[0].split("Thread model: ")[1]
133 def findDefinitions(ftype, path):
136 for filename in element[2]:
137 if fnmatch.fnmatch(filename, ftype):
138 yield (filename, element[0])
140 def loadCpuInfos(path):
142 for definition in findDefinitions(const.CPU_DEFINITION, path):
143 cpuInfos.append(getInfos(definition))
146 def getInfos(definition):
148 D.update(const.CPU_DEF)
149 def include(filename, dict = D, directory=definition[1]):
150 execfile(directory + "/" + filename, {}, D)
151 D["include"] = include
152 include(definition[0], D)
153 D["CPU_NAME"] = definition[0].split(".")[0]
154 D["DEFINITION_PATH"] = definition[1] + "/" + definition[0]
158 def getDefinitionBlocks(text):
160 Take a text and return a list of tuple (description, name-value).
163 block_tmp = re.findall(r"/\*{2}\s*([^*]*\*(?:[^/*][^*]*\*+)*)/\s*#define\s+((?:[^/]*?/?)+)\s*?(?:/{2,3}[^<].*?)?$", text, re.MULTILINE)
164 for comment, define in block_tmp:
165 block.append((" ".join(re.findall(r"^\s*\*?\s*(.*?)\s*?(?:/{2}.*?)?$", comment, re.MULTILINE)).strip(), define))
166 block += re.findall(r"/{3}\s*([^<].*?)\s*#define\s+((?:[^/]*?/?)+)\s*?(?:/{2,3}[^<].*?)?$", text, re.MULTILINE)
167 block += [(comment, define) for define, comment in re.findall(r"#define\s*(.*?)\s*/{3}<\s*(.+?)\s*?(?:/{2,3}[^<].*?)?$", text, re.MULTILINE)]
170 def formatParamNameValue(text):
172 Take the given string and return a tuple with the name of the parameter in the first position
173 and the value in the second.
175 block = re.findall("\s*([^\s]+)\s*(.+?)\s*$", text, re.MULTILINE)
178 def getDescriptionInformations(text):
180 Take the doxygen comment and strip the wizard informations, returning the tuple
181 (comment, wizard_informations)
183 index = text.find("$WIZARD")
185 exec(text[index + 1:])
186 informations = WIZARD
187 return text[:index].strip(), informations
189 return text.strip(), {}
191 def loadConfigurationInfos(path):
193 Return the module configurations found in the given file as a dict with the
194 parameter name as key and a dict containig the fields above as value:
195 "value": the value of the parameter
196 "description": the description of the parameter
197 "informations": a dict containig optional informations:
198 "type": "int" | "boolean" | "enum"
199 "min": the minimum value for integer parameters
200 "max": the maximum value for integer parameters
201 "long": boolean indicating if the num is a long
202 "value_list": the name of the enum for enum parameters
204 configurationInfos = {}
205 for comment, define in getDefinitionBlocks(open(path, "r").read()):
206 name, value = formatParamNameValue(define)
207 description, informations = getDescriptionInformations(comment)
208 configurationInfos[name] = {}
209 configurationInfos[name]["value"] = value
210 configurationInfos[name]["informations"] = informations
211 configurationInfos[name]["description"] = description
212 return configurationInfos
214 def loadModuleInfos(path):
216 Return the module infos found in the given file as a dict with the module
217 name as key and a dict containig the fields above as value or an empty dict
218 if the given file is not a BeRTOS module:
219 "depends": a list of modules needed by this module
220 "configuration": the cfg_*.h with the module configurations
221 "description": a string containing the brief description of doxygen
222 "enabled": contains False but the wizard will change if the user select
226 string = open(path, "r").read()
227 commentList = re.findall(r"/\*{2}\s*([^*]*\*(?:[^/*][^*]*\*+)*)/", string)
228 commentList = [" ".join(re.findall(r"^\s*\*?\s*(.*?)\s*?(?:/{2}.*?)?$", comment, re.MULTILINE)).strip() for comment in commentList]
229 for comment in commentList:
230 index = comment.find("$WIZARD_MODULE")
232 exec(comment[index + 1:])
233 moduleInfos[WIZARD_MODULE["name"]] = {"depends": WIZARD_MODULE["depends"],
234 "configuration": WIZARD_MODULE["configuration"],
240 def loadModuleInfosDict(path):
242 Return the dict containig all the modules
245 for filename, path in findDefinitions("*.h", path):
246 moduleInfosDict.update(loadModuleInfos(path + "/" + filename))
247 return moduleInfosDict
249 def loadDefineLists(path):
251 Return a dict with the name of the list as key and a list of string as value
253 string = open(path, "r").read()
254 commentList = re.findall(r"/\*{2}\s*([^*]*\*(?:[^/*][^*]*\*+)*)/", string)
255 commentList = [" ".join(re.findall(r"^\s*\*?\s*(.*?)\s*?(?:/{2}.*?)?$", comment, re.MULTILINE)).strip() for comment in commentList]
257 for comment in commentList:
258 index = comment.find("$WIZARD_LIST")
260 exec(comment[index + 1:])
261 listDict.update(WIZARD_LIST)
264 def loadDefineListsDict(path):
266 Return the dict containing all the define lists
269 for filename, path in findDefinitions("*.h", path):
270 defineListsDict.update(loadDefineLists(path + "/" + filename))
271 return defineListsDict
273 def sub(string, parameter, value):
275 Substitute the given value at the given parameter define in the given string
277 return re.sub(r"(?P<define>#define\s+" + parameter + r"\s+)([^\s]+)", r"\g<define>" + value, string)