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 type(appendData) is 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 type(appendData) is 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)
Exemple #2
0
    def getCompileTask(self):
        '''
        Create compile current file task (execute gcc compile command).
        '''
        taskData = """
        {
            "label": "will be replaced with templateStrings string",
            "type": "shell",
            "command": "will be replaced with GCC path below",
            "args": ["will be replaced with path from buildData.json"],
            "problemMatcher": {
                "pattern": {
                    "regexp": "^(.*):(\\\\d+):(\\\\d+):\\\\s+(warning|error):\\\\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                }
            },
            "presentation": {
                "focus": true
            }
        }
        """
        jsonTaskData = json.loads(taskData)

        # get compiler C flags, defines, includes, ... from 'buildData.json'
        buildData = build.BuildData().getBuildData()
        jsonTaskData["label"] = tmpStr.taskName_compile

        # defines
        cDefines = buildData[self.bStr.cDefines]
        cDefines = utils.preappendString(cDefines, '-D')

        # includes
        cIncludes = buildData[self.bStr.cIncludes]
        cIncludes = utils.preappendString(cIncludes, '-I')

        # build directory
        buildDir = buildData[self.bStr.buildDirPath]

        # c flags
        cFlags = buildData[self.bStr.cFlags]
        for flagIndex, flag in enumerate(cFlags):
            if flag == "-MF":
                newFlagString = "-MF'" + buildDir + "/${fileBasenameNoExtension}.d'"
                cFlags[flagIndex] = newFlagString
                continue

        # output file
        outputFilePath = "'" + buildDir + "/${fileBasenameNoExtension}.o'"
        outputFile = ["-o"]
        outputFile.append(outputFilePath)

        # compile file string
        fileString = "'${relativeFile}'"
        fileString = [fileString]

        jsonTaskData["command"] = buildData[self.bStr.gccExePath]
        jsonTaskData["args"] = ["-c"]   # only compile switch
        jsonTaskData["args"].extend(cDefines)
        jsonTaskData["args"].extend(cIncludes)
        jsonTaskData["args"].extend(cFlags)
        jsonTaskData["args"].extend(fileString)
        jsonTaskData["args"].extend(outputFile)

        return jsonTaskData