Add autoenabled configuration parameters
[bertos.git] / wizard / bertos_utils.py
index 1376942ace266bca2a332e7672f06fa386306a42..47fa4b6b8572315b8c11e3bb53888bd7029bb82a 100644 (file)
@@ -13,8 +13,11 @@ import os
 import fnmatch
 import glob
 import re
+import shutil
 
 import const
+import codelite_project
+import DefineException
 
 def isBertosDir(directory):
    return os.path.exists(directory + "/VERSION")
@@ -22,11 +25,142 @@ def isBertosDir(directory):
 def bertosVersion(directory):
    return open(directory + "/VERSION").readline().strip()
 
-def createBertosProject(directory):
+def createBertosProject(projectInfo):
+    directory = projectInfo.info("PROJECT_PATH")
+    sourcesDir = projectInfo.info("SOURCES_PATH")
     if not os.path.isdir(directory):
         os.mkdir(directory)
-    open(directory + "/project.bertos", "w")
+    f = open(directory + "/project.bertos", "w")
+    f.write(repr(projectInfo))
+    f.close()
+    ## Destination source dir
+    srcdir = directory + "/bertos"
+    shutil.rmtree(srcdir, True)
+    shutil.copytree(sourcesDir + "/bertos", srcdir)
+    ## Destination makefile
+    makefile = directory + "/Makefile"
+    if os.path.exists(makefile):
+        os.remove(makefile)
+    makefile = open("mktemplates/Makefile").read()
+    makefile = makefileGenerator(projectInfo, makefile)
+    open(directory + "/Makefile", "w").write(makefile)
+    ## Destination project dir
+    prjdir = directory + "/" + os.path.basename(directory)
+    shutil.rmtree(prjdir, True)
+    os.mkdir(prjdir)
+    ## Destination configurations files
+    cfgdir = prjdir + "/cfg"
+    shutil.rmtree(cfgdir, True)
+    os.mkdir(cfgdir)
+    for key, value in projectInfo.info("CONFIGURATIONS").items():
+        string = open(sourcesDir + "/" + key, "r").read()
+        for parameter, infos in value.items():
+            value = infos["value"]
+            if "type" in infos["informations"] and infos["informations"]["type"] == "autoenabled":
+                value = "1"
+            if "unsigned" in infos["informations"].keys() and infos["informations"]["unsigned"]:
+                value += "U"
+            if "long" in infos["informations"].keys() and infos["informations"]["long"]:
+                value += "L"
+            string = sub(string, parameter, value)
+        f = open(cfgdir + "/" + os.path.basename(key), "w")
+        f.write(string)
+        f.close()
+    ## Destinatio mk file
+    makefile = open("mktemplates/template.mk", "r").read()
+    makefile = mkGenerator(projectInfo, makefile)
+    open(prjdir + "/" + os.path.basename(prjdir) + ".mk", "w").write(makefile)
+    ## Destination main.c file
+    main = open("srctemplates/main.c", "r").read()
+    open(prjdir + "/main.c", "w").write(main)
+    if "codelite" in projectInfo.info("OUTPUT"):
+        workspace = codeliteWorkspaceGenerator(projectInfo)
+        open(directory + "/" + os.path.basename(prjdir) + ".workspace", "w").write(workspace)
+        project = codeliteProjectGenerator(projectInfo)
+        open(directory + "/" + os.path.basename(prjdir) + ".project", "w").write(project)
 
+def mkGenerator(projectInfo, makefile):
+    """
+    Generates the mk file for the current project.
+    """
+    mkData = {}
+    mkData["$pname"] = os.path.basename(projectInfo.info("PROJECT_PATH"))
+    mkData["$cpuname"] = projectInfo.info("CPU_INFOS")["CORE_CPU"]
+    mkData["$cflags"] = " ".join(projectInfo.info("CPU_INFOS")["C_FLAGS"])
+    mkData["$ldflags"] = " ".join(projectInfo.info("CPU_INFOS")["LD_FLAGS"])
+    mkData["$csrc"], mkData["$pcsrc"], mkData["$constants"] = csrcGenerator(projectInfo)
+    mkData["$prefix"] = projectInfo.info("TOOLCHAIN")["path"].split("gcc")[0]
+    mkData["$suffix"] = projectInfo.info("TOOLCHAIN")["path"].split("gcc")[1]
+    mkData["$cross"] = projectInfo.info("TOOLCHAIN")["path"].split("gcc")[0]
+    mkData["$main"] = projectInfo.info("PROJECT_PATH") + "/" + os.path.basename(projectInfo.info("PROJECT_PATH")) + "/main.c"
+    for key in mkData:
+        while makefile.find(key) != -1:
+            makefile = makefile.replace(key, mkData[key])
+    return makefile
+
+def makefileGenerator(projectInfo, makefile):
+    """
+    Generate the Makefile for the current project.
+    """
+    # TODO: write a general function that works for both the mk file and the Makefile
+    while makefile.find("project_name") != -1:
+        makefile = makefile.replace("project_name", os.path.basename(projectInfo.info("PROJECT_PATH")))
+    return makefile
+
+def csrcGenerator(projectInfo):
+    modules = projectInfo.info("MODULES")
+    if "harvard" in projectInfo.info("CPU_INFOS")["CPU_TAGS"]:
+        harvard = True
+    else:
+        harvard = False
+    csrc = []
+    pcsrc = []
+    constants = {}
+    for module, information in modules.items():
+        if information["enabled"]:
+            if "constants" in information:
+                constants.update(information["constants"])
+            for filename, path in findDefinitions(module + ".c", projectInfo):
+                path = path.replace(projectInfo.info("SOURCES_PATH"), projectInfo.info("PROJECT_PATH"))
+                if not harvard or "harvard" not in information or information["harvard"] == "both":
+                    csrc.append(path + "/" + filename)
+                if harvard and "harvard" in information:
+                    pcsrc.append(path + "/" + filename)
+            for filename, path in findDefinitions(module + "_" + projectInfo.info("CPU_INFOS")["TOOLCHAIN"] + ".c", projectInfo):
+                path = path.replace(projectInfo.info("SOURCES_PATH"), projectInfo.info("PROJECT_PATH"))
+                if not harvard or "harvard" not in information or information["harvard"] == "both":
+                    csrc.append(path + "/" + filename)
+                if harvard and "harvard" in information:
+                    pcsrc.append(path + "/" + filename)
+            for tag in projectInfo.info("CPU_INFOS")["CPU_TAGS"]:
+                for filename, path in findDefinitions(module + "_" + tag + ".c", projectInfo):
+                    path = path.replace(projectInfo.info("SOURCES_PATH"), projectInfo.info("PROJECT_PATH"))
+                    if not harvard or "harvard" not in information or information["harvard"] == "both":
+                        csrc.append(path + "/" + filename)
+                    if harvard and "harvard" in information:
+                        pcsrc.append(path + "/" + filename)
+    csrc = " \\\n\t".join(csrc) + " \\"
+    pcsrc = " \\\n\t".join(pcsrc) + " \\"
+    constants = "\n".join([os.path.basename(projectInfo.info("PROJECT_PATH")) + "_" + key + " = " + str(value) for key, value in constants.items()])
+    return csrc, pcsrc, constants
+
+def codeliteProjectGenerator(projectInfo):
+    template = open("cltemplates/bertos.project").read()
+    filelist = "\n".join(codelite_project.clFiles(codelite_project.findSources(projectInfo.info("PROJECT_PATH")), projectInfo.info("PROJECT_PATH")))
+    while template.find("$filelist") != -1:
+        template = template.replace("$filelist", filelist)
+    projectName = os.path.basename(projectInfo.info("PROJECT_PATH"))
+    while template.find("$project") != -1:
+        template = template.replace("$project", projectName)
+    return template
+
+def codeliteWorkspaceGenerator(projectInfo):
+    template = open("cltemplates/bertos.workspace").read()
+    projectName = os.path.basename(projectInfo.info("PROJECT_PATH"))
+    while template.find("$project") != -1:
+        template = template.replace("$project", projectName)
+    return template
+    
 def getSystemPath():
     path = os.environ["PATH"]
     if os.name == "nt":
@@ -39,8 +173,7 @@ def findToolchains(pathList):
     toolchains = []
     for element in pathList:
         for toolchain in glob.glob(element+ "/" + const.GCC_NAME):
-            if not os.path.islink(toolchain):
-                toolchains.append(toolchain)
+            toolchains.append(toolchain)
     return list(set(toolchains))
 
 def getToolchainInfo(output):
@@ -69,16 +202,22 @@ def getToolchainInfo(output):
         info["thread"] = thread[0].split("Thread model: ")[1]
     return info
 
-def findDefinitions(ftype, path):
-    L = os.walk(path)
+def loadSourceTree(project):
+    fileList = [f for f in os.walk(project.info("SOURCES_PATH"))]
+    project.setInfo("FILE_LIST", fileList)
+
+def findDefinitions(ftype, project):
+    L = project.info("FILE_LIST")
+    definitions = []
     for element in L:
         for filename in element[2]:
             if fnmatch.fnmatch(filename, ftype):
-                yield (filename, element[0])
+                definitions.append((filename, element[0]))
+    return definitions
 
-def loadCpuInfos(path):
+def loadCpuInfos(project):
     cpuInfos = []
-    for definition in findDefinitions(const.CPU_DEFINITION, path):
+    for definition in findDefinitions(const.CPU_DEFINITION, project):
         cpuInfos.append(getInfos(definition))
     return cpuInfos
 
@@ -94,6 +233,95 @@ def getInfos(definition):
     del D["include"]
     return D
 
+def getCommentList(string):
+    commentList = re.findall(r"/\*{2}\s*([^*]*\*(?:[^/*][^*]*\*+)*)/", string)
+    commentList = [re.findall(r"^\s*\* *(.*?)$", comment, re.MULTILINE) for comment in commentList]
+    return commentList
+
+def loadModuleDefinition(first_comment):
+    toBeParsed = False
+    moduleDefinition = {}
+    for num, line in enumerate(first_comment):
+        index = line.find("$WIZ$")
+        if index != -1:
+            toBeParsed = True
+            try:
+                exec line[index + len("$WIZ$ "):] in {}, moduleDefinition
+            except:
+                raise ParseError(num, line[index:])
+        elif line.find("\\brief") != -1:
+            moduleDefinition["module_description"] = line[line.find("\\brief") + len("\\brief "):]
+    moduleDict = {}
+    if "module_name" in moduleDefinition.keys():
+        moduleName = moduleDefinition[const.MODULE_DEFINITION["module_name"]]
+        del moduleDefinition[const.MODULE_DEFINITION["module_name"]]
+        moduleDict[moduleName] = {}
+        if const.MODULE_DEFINITION["module_depends"] in moduleDefinition.keys():
+            if type(moduleDefinition[const.MODULE_DEFINITION["module_depends"]]) == str:
+                moduleDefinition[const.MODULE_DEFINITION["module_depends"]] = (moduleDefinition[const.MODULE_DEFINITION["module_depends"]],)
+            moduleDict[moduleName]["depends"] = moduleDefinition[const.MODULE_DEFINITION["module_depends"]]
+            del moduleDefinition[const.MODULE_DEFINITION["module_depends"]]
+        else:
+            moduleDict[moduleName]["depends"] = ()
+        if const.MODULE_DEFINITION["module_configuration"] in moduleDefinition.keys():
+            moduleDict[moduleName]["configuration"] = moduleDefinition[const.MODULE_DEFINITION["module_configuration"]]
+            del moduleDefinition[const.MODULE_DEFINITION["module_configuration"]]
+        else:
+            moduleDict[moduleName]["configuration"] = ""
+        if "module_description" in moduleDefinition.keys():
+            moduleDict[moduleName]["description"] = moduleDefinition["module_description"]
+            del moduleDefinition["module_description"]
+        if const.MODULE_DEFINITION["module_harvard"] in moduleDefinition.keys():
+            harvard = moduleDefinition[const.MODULE_DEFINITION["module_harvard"]]
+            if harvard == "both" or harvard == "pgm_memory":
+                moduleDict[moduleName]["harvard"] = harvard
+            del moduleDefinition[const.MODULE_DEFINITION["module_harvard"]]
+        moduleDict[moduleName]["constants"] = moduleDefinition
+        moduleDict[moduleName]["enabled"] = False
+    return toBeParsed, moduleDict
+
+def loadDefineLists(commentList):
+    defineList = {}
+    for comment in commentList:
+        for num, line in enumerate(comment):
+            index = line.find("$WIZ$")
+            if index != -1:
+                try:
+                    exec line[index + len("$WIZ$ "):] in {}, defineList
+                except:
+                    raise ParseError(num, line[index:])
+    for key, value in defineList.items():
+        if type(value) == str:
+            defineList[key] = (value,)
+    return defineList
+
+def getDescriptionInformations(comment): 
+    """ 
+    Take the doxygen comment and strip the wizard informations, returning the tuple 
+    (comment, wizard_information) 
+    """
+    brief = ""
+    description = ""
+    information = {}
+    for num, line in enumerate(comment):
+        index = line.find("$WIZ$")
+        if index != -1:
+            if len(brief) == 0:
+                brief += line[:index].strip()
+            else:
+                description += " " + line[:index]
+            try:
+                exec line[index + len("$WIZ$ "):] in {}, information
+            except:
+                raise ParseError(num, line[index:])
+        else:
+            if len(brief) == 0:
+                brief += line.strip()
+            else:
+                description += " " + line
+                description = description.strip()
+    return brief.strip(), description.strip(), information
+
 def getDefinitionBlocks(text):
     """
     Take a text and return a list of tuple (description, name-value).
@@ -101,11 +329,54 @@ def getDefinitionBlocks(text):
     block = []
     block_tmp = re.findall(r"/\*{2}\s*([^*]*\*(?:[^/*][^*]*\*+)*)/\s*#define\s+((?:[^/]*?/?)+)\s*?(?:/{2,3}[^<].*?)?$", text, re.MULTILINE)
     for comment, define in block_tmp:
-        block.append((" ".join(re.findall(r"^\s*\*?\s*(.*?)\s*?(?:/{2}.*?)?$", comment, re.MULTILINE)).strip(), define))
-    block += re.findall(r"/{3}\s*([^<].*?)\s*#define\s+((?:[^/]*?/?)+)\s*?(?:/{2,3}[^<].*?)?$", text, re.MULTILINE)
-    block += [(comment, define) for define, comment in re.findall(r"#define\s*(.*?)\s*/{3}<\s*(.+?)\s*?(?:/{2,3}[^<].*?)?$", text, re.MULTILINE)]
+        # Only the first element is needed
+        block.append(([re.findall(r"^\s*\* *(.*?)$", line, re.MULTILINE)[0] for line in comment.splitlines()], define))
+    for comment, define in re.findall(r"/{3}\s*([^<].*?)\s*#define\s+((?:[^/]*?/?)+)\s*?(?:/{2,3}[^<].*?)?$", text, re.MULTILINE):
+        block.append(([comment], define))
+    for define, comment in re.findall(r"#define\s*(.*?)\s*/{3}<\s*(.+?)\s*?(?:/{2,3}[^<].*?)?$", text, re.MULTILINE):
+        block.append(([comment], define))
     return block
 
+def loadModuleData(project):
+    moduleInfoDict = {}
+    listInfoDict = {}
+    configurationInfoDict = {}
+    for filename, path in findDefinitions("*.h", project):
+        commentList = getCommentList(open(path + "/" + filename, "r").read())
+        if len(commentList) > 0:
+            moduleInfo = {}
+            configurationInfo = {}
+            try:
+                toBeParsed, moduleDict = loadModuleDefinition(commentList[0])
+            except ParseError, err:
+                raise DefineException.ModuleDefineException(path, err.line_number, err.line)
+            for module, information in moduleDict.items():
+                information["category"] = os.path.basename(path)
+                if "configuration" in information.keys() and len(information["configuration"]):
+                    configuration = moduleDict[module]["configuration"]
+                    try:
+                        configurationInfo[configuration] = loadConfigurationInfos(project.info("SOURCES_PATH") + "/" + configuration)
+                    except ParseError, err:
+                        raise DefineException.ConfigurationDefineException(project.info("SOURCES_PATH") + "/" + configuration, err.line_number, err.line)
+            moduleInfoDict.update(moduleDict)
+            configurationInfoDict.update(configurationInfo)
+            if toBeParsed:
+                try:
+                    listDict = loadDefineLists(commentList[1:])
+                    listInfoDict.update(listDict)
+                except ParseError, err:
+                    raise DefineException.EnumDefineException(path, err.line_number, err.line)
+    for filename, path in findDefinitions("*_" + project.info("CPU_INFOS")["TOOLCHAIN"] + ".h", project):
+        commentList = getCommentList(open(path + "/" + filename, "r").read())
+        listInfoDict.update(loadDefineLists(commentList))
+    for tag in project.info("CPU_INFOS")["CPU_TAGS"]:
+        for filename, path in findDefinitions("*_" + tag + ".h", project):
+            commentList = getCommentList(open(path + "/" + filename, "r").read())
+            listInfoDict.update(loadDefineLists(commentList))
+    project.setInfo("MODULES", moduleInfoDict)
+    project.setInfo("LISTS", listInfoDict)
+    project.setInfo("CONFIGURATIONS", configurationInfoDict)
+    
 def formatParamNameValue(text):
     """
     Take the given string and return a tuple with the name of the parameter in the first position
@@ -114,19 +385,6 @@ def formatParamNameValue(text):
     block = re.findall("\s*([^\s]+)\s*(.+?)\s*$", text, re.MULTILINE)
     return block[0]
 
-def getDescriptionInformations(text): 
-    """ 
-    Take the doxygen comment and strip the wizard informations, returning the tuple 
-    (comment, wizard_informations) 
-    """ 
-    index = text.find("$WIZARD") 
-    if index != -1: 
-        exec(text[index + 1:]) 
-        informations = WIZARD 
-        return text[:index].strip(), informations
-    else:
-        return text.strip(), {}
-
 def loadConfigurationInfos(path):
     """
     Return the module configurations found in the given file as a dict with the
@@ -143,44 +401,68 @@ def loadConfigurationInfos(path):
     configurationInfos = {}
     for comment, define in getDefinitionBlocks(open(path, "r").read()):
         name, value = formatParamNameValue(define)
-        description, informations = getDescriptionInformations(comment)
+        brief, description, informations = getDescriptionInformations(comment)
         configurationInfos[name] = {}
         configurationInfos[name]["value"] = value
         configurationInfos[name]["informations"] = informations
+        if ("type" in configurationInfos[name]["informations"].keys() and
+                configurationInfos[name]["informations"]["type"] == "int" and
+                configurationInfos[name]["value"].find("L") != -1):
+            configurationInfos[name]["informations"]["long"] = True
+            configurationInfos[name]["value"] = configurationInfos[name]["value"].replace("L", "")
+        if ("type" in configurationInfos[name]["informations"].keys() and
+                configurationInfos[name]["informations"]["type"] == "int" and
+                configurationInfos[name]["value"].find("U") != -1):
+            configurationInfos[name]["informations"]["unsigned"] = True
+            configurationInfos[name]["value"] = configurationInfos[name]["value"].replace("U", "")
         configurationInfos[name]["description"] = description
+        configurationInfos[name]["brief"] = brief
     return configurationInfos
 
-def loadModuleInfos(path):
+def sub(string, parameter, value):
     """
-    Return the module infos found in the given file as a dict with the module
-    name as key and a dict containig the fields above as value or an empty dict
-    if the given file is not a BeRTOS module:
-        "depends": a list of modules needed by this module
-        "configuration": the cfg_*.h with the module configurations
+    Substitute the given value at the given parameter define in the given string
     """
-    moduleInfos = {}
-    string = open(path, "r").read()
-    commentList = re.findall(r"/\*{2}\s*([^*]*\*(?:[^/*][^*]*\*+)*)/", string)
-    commentList = [" ".join(re.findall(r"^\s*\*?\s*(.*?)\s*?(?:/{2}.*?)?$", comment, re.MULTILINE)).strip() for comment in commentList]
-    for comment in commentList:
-        index = comment.find("$WIZARD_MODULE")
-        if index != -1:
-            exec(comment[index + 1:])
-            moduleInfos[WIZARD_MODULE["name"]] = {"depends": WIZARD_MODULE["depends"], "configuration": WIZARD_MODULE["configuration"]}
-            return moduleInfos
-    return {}
+    return re.sub(r"(?P<define>#define\s+" + parameter + r"\s+)([^\s]+)", r"\g<define>" + value, string)
 
-def loadDefineLists(path):
+def isInt(informations):
     """
-    Return a dict with the name of the list as key and a list of string as value
+    Return True if the value is a simple int.
     """
-    string = open(path, "r").read()
-    commentList = re.findall(r"/\*{2}\s*([^*]*\*(?:[^/*][^*]*\*+)*)/", string)
-    commentList = [" ".join(re.findall(r"^\s*\*?\s*(.*?)\s*?(?:/{2}.*?)?$", comment, re.MULTILINE)).strip() for comment in commentList]
-    listDict = {}
-    for comment in commentList:
-        index = comment.find("$WIZARD_LIST")
-        if index != -1:
-            exec(comment[index + 1:])
-            listDict.update(WIZARD_LIST)
-    return listDict
\ No newline at end of file
+    if ("long" not in informatios.keys() or not informations["long"]) and ("unsigned" not in informations.keys() or informations["unsigned"]):
+        return True
+    else:
+        return False
+
+def isLong(informations):
+    """
+    Return True if the value is a long.
+    """
+    if "long" in informations.keys() and informations["long"] and "unsigned" not in informations.keys():
+        return True
+    else:
+        return False
+
+def isUnsigned(informations):
+    """
+    Return True if the value is an unsigned.
+    """
+    if "unsigned" in informations.keys() and informations["unsigned"] and "long" not in informations.keys():
+        return True
+    else:
+        return False
+
+def isUnsignedLong(informations):
+    """
+    Return True if the value is an unsigned long.
+    """
+    if "unsigned" in informations.keys() and "long" in informations.keys() and informations["unsigned"] and informations["long"]:
+        return True
+    else:
+        return False
+
+class ParseError(Exception):
+    def __init__(self, line_number, line):
+        Exception.__init__(self)
+        self.line_number = line_number
+        self.line = line