Example #1
0
    def createNewMakefile(self):
        '''
        Merge existing Makefile data and user fields from existing 'c_cpp_properties.json.'
        '''
        cP = wks.CProperties()
        cPropertiesData = cP.getCPropertiesData()

        with open(utils.makefilePath, 'r') as makefile:
            data = makefile.readlines()

        # sources
        cSources = cP.getCPropertiesKeyData(cPropertiesData,
                                            self.cPStr.user_cSources)
        data = self.searchAndAppend(data, self.mkfStr.cSources, cSources)

        asmSources = cP.getCPropertiesKeyData(cPropertiesData,
                                              self.cPStr.user_asmSources)
        data = self.searchAndAppend(data, self.mkfStr.asmSources, asmSources)

        ldSources = cP.getCPropertiesKeyData(cPropertiesData,
                                             self.cPStr.user_ldSources)
        data = self.searchAndAppend(data,
                                    self.mkfStr.ldSources,
                                    ldSources,
                                    preappend='-l:')

        # includes
        cIncludes = cP.getCPropertiesKeyData(cPropertiesData,
                                             self.cPStr.user_cIncludes)
        data = self.searchAndAppend(data,
                                    self.mkfStr.cIncludes,
                                    cIncludes,
                                    preappend='-I')

        asmIncludes = cP.getCPropertiesKeyData(cPropertiesData,
                                               self.cPStr.user_asmIncludes)
        data = self.searchAndAppend(data,
                                    self.mkfStr.asmIncludes,
                                    asmIncludes,
                                    preappend='-I')

        ldIncludes = cP.getCPropertiesKeyData(cPropertiesData,
                                              self.cPStr.user_ldIncludes)
        data = self.searchAndAppend(data,
                                    self.mkfStr.ldIncludes,
                                    ldIncludes,
                                    preappend='-L')

        # defines
        cDefines = cP.getCPropertiesKeyData(cPropertiesData,
                                            self.cPStr.user_cDefines)
        data = self.searchAndAppend(data,
                                    self.mkfStr.cDefines,
                                    cDefines,
                                    preappend='-D')

        asmDefines = cP.getCPropertiesKeyData(cPropertiesData,
                                              self.cPStr.user_asmDefines)
        data = self.searchAndAppend(data,
                                    self.mkfStr.asmDefines,
                                    asmDefines,
                                    preappend='-D')

        # compiler flags
        cFlags = cP.getCPropertiesKeyData(cPropertiesData,
                                          self.cPStr.user_cFlags)
        data = self.searchAndAppend(data, self.mkfStr.cFlags, cFlags)

        asmFlags = cP.getCPropertiesKeyData(cPropertiesData,
                                            self.cPStr.user_asmFlags)
        data = self.searchAndAppend(data, self.mkfStr.asmFlags, asmFlags)

        ldFlags = cP.getCPropertiesKeyData(cPropertiesData,
                                           self.cPStr.user_ldFlags)
        data = self.searchAndAppend(data, self.mkfStr.ldFlags, ldFlags)

        data = self.replaceMakefileHeader(data)

        try:
            with open(utils.makefilePath, 'w') as makefile:
                for line in data:
                    makefile.write(line)
            print("New Makefile data succesfully written.")

        except Exception as err:
            errorMsg = "Exception error writing new data to Makefile:\n"
            errorMsg += str(err)
            utils.printAndQuit(errorMsg)
Example #2
0
    def searchAndAppend(self, data, searchString, appendData, preappend=None):
        '''
        Search for string in 'data' list and append 'appendData' according to Makefile syntax.
        if 'preappend' is defined, each item of 'appendData' is preappended with this string.
        '''
        NOT_FOUND = -1

        if preappend is not None:
            appendData = utils.preappendString(appendData, preappend)

        for lineIndex, line in enumerate(data):
            line = line.rstrip('\n')  # strip string of '\n'

            if line.find(searchString) != NOT_FOUND:  # search for start string
                if line[0] == '#':  # this is a comment
                    continue

                if line.find("\\") == NOT_FOUND:
                    # one-liner, no '\' sign at the end of the line
                    if isinstance(appendData, list):  # if this is list
                        if appendData:  # and it is not empty
                            if len(
                                    appendData
                            ) == 1:  # this list has only one item, add it without '\'
                                if line[-1] != ' ':  # avoid double spaces
                                    line += " "
                                data[lineIndex] = line + appendData[0] + "\n"

                            else:
                                # this is list with multiple items, '\' will be needed
                                line += " \\\n"
                                data[lineIndex] = line

                                for itemIndex, item in enumerate(appendData):
                                    stringToInsert = item
                                    if item != appendData[
                                            -1]:  # for last item do not append "\"
                                        stringToInsert += "\\"
                                    stringToInsert += "\n"  # new line must always be added
                                    data.insert(lineIndex + itemIndex + 1,
                                                stringToInsert)

                            return data

                    else:  # appendData is string (not list)
                        if appendData != '':
                            if data[lineIndex][
                                    -1] != ' ':  # avoid double spaces
                                data[lineIndex] += " "
                            data[lineIndex] += appendData + "\n"

                    return data
                else:  # already a multi-liner, append at the beginning, but in new line
                    if isinstance(appendData, list):
                        for itemIndex, item in enumerate(appendData):
                            stringToInsert = item + " \\\n"
                            data.insert(lineIndex + itemIndex + 1,
                                        stringToInsert)
                    else:  # appendData is string (not list)
                        data[lineIndex] += item + " \\\n"

                    return data

        errorMsg = "String item " + str(searchString) + " not found!"
        utils.printAndQuit(errorMsg)
Example #3
0
    def verifyToolsPaths(self, toolsPaths, request=False):
        '''
        This function checks if paths in 'toolsPaths.json' are a valid paths.
        If any path is not valid/missing, user is asked for update via updatePath().
        If 'request' is set to True, user is asked to update path even if it is a valid path.

        Returns updated valid tools paths.
        '''
        for pathName in self.bStr.toolsPaths:
            try:
                mustBeUpdated = False
                if pathName in toolsPaths:
                    # 'toolsPaths.json' keys are not lists. Always a plain path (string)
                    if not utils.pathExists(toolsPaths[pathName]):
                        mustBeUpdated = True
                        # path not valid, check if command
                        if utils.commandExists(toolsPaths[pathName]):
                            mustBeUpdated = False

                    if mustBeUpdated:
                        if toolsPaths[pathName] != '':
                            # avoid reporting invalid file path, if there is an empty string
                            msg = "\n\nInvalid path detected in '" + pathName + "' key."
                            print(msg)
                    else:
                        if request:
                            msg = "\n\nValid path(s) for " + pathName + " detected: '" + toolsPaths[
                                pathName] + "'."
                            msg += "\n\tUpdate? [y/n]: "
                            if utils.getYesNoAnswer(msg):
                                mustBeUpdated = True

                else:  # this key is missing in toolsPaths.json!
                    mustBeUpdated = True

                if mustBeUpdated:
                    if pathName in self.bStr.derivedPaths:
                        continue

                    elif pathName == self.bStr.openOcdConfig:
                        # get openOcdConfig - special handler
                        toolsPaths[pathName] = utils.getOpenOcdConfig(
                            toolsPaths[self.bStr.openOcdPath])

                    elif pathName in self.pathsDescriptionsData:
                        name = self.pathsDescriptionsData[pathName]['name']
                        defaultPath = self.pathsDescriptionsData[pathName][
                            'defaultPath']
                        toolsPaths[pathName] = self.updatePath(
                            name, defaultPath)

                    else:
                        toolsPaths[pathName] = self.updatePath(pathName, None)

            except Exception as err:
                toolsPaths[pathName] = self.updatePath(pathName, None)

        for pathName in self.bStr.derivedPaths:
            if pathName == self.bStr.pythonExec:
                toolsPaths[self.bStr.pythonExec] = utils.getPython3Executable()

            elif pathName == self.bStr.gccInludePath:
                toolsPaths[self.bStr.gccInludePath] = utils.getGccIncludePath(
                    toolsPaths[self.bStr.gccExePath])

            else:
                errorMsg = "ideScripts design error: pathName '" + pathName + "' is in 'self.bStr.derivedPaths' list, "
                errorMsg += "but no 'get()' handler is specified."
                utils.printAndQuit(errorMsg)

        return toolsPaths