def Run(log: Log, toolConfig: ToolConfig, formatPackageList: List[Package],
            customPackageFileFilter: Optional[CustomPackageFileFilter],
            packageRecipeResultManager: PackageRecipeResultManager,
            clangFormatConfiguration: ClangFormatConfiguration,
            repairEnabled: bool) -> None:
        """ RunClangFormat on a package at a time """

        clangExeInfo = PerformClangUtil.LookupRecipeResults(
            packageRecipeResultManager,
            clangFormatConfiguration.RecipePackageName,
            MagicValues.ClangFormatCommand)

        if log.Verbosity >= 1:
            PerformClangUtil.ShowVersion(log, clangExeInfo)

        log.LogPrint("Running clang-format")

        # Filter the package list so it only contains things we can process
        finalPackageList = [
            package for package in formatPackageList
            if PerformClangUtil.CanProcessPackage(package)
        ]

        sortedPackages = list(finalPackageList)
        sortedPackages.sort(key=lambda s: s.Name.lower())
        for package in sortedPackages:
            filteredFiles = None if customPackageFileFilter is None else customPackageFileFilter.TryLocateFilePatternInPackage(
                log, package, clangFormatConfiguration.FileExtensions)
            if customPackageFileFilter is None or filteredFiles is not None:
                _RunClangFormat(log, toolConfig, clangFormatConfiguration,
                                clangExeInfo, package, filteredFiles,
                                repairEnabled)
示例#2
0
    def Run(log: Log, toolConfig: ToolConfig, formatPackageList: List[Package],
            customPackageFileFilter: Optional[CustomPackageFileFilter],
            packageRecipeResultManager: PackageRecipeResultManager,
            clangFormatConfiguration: ClangFormatConfiguration,
            cmakeConfig: GeneratorCMakeConfig, repairEnabled: bool,
            buildThreads: int, useLegacyTidyMethod: bool) -> None:
        """ RunClangFormat on a package at a time """

        # Lookup the recommended build threads using the standard build algorithm
        numBuildThreads = PlatformBuildUtil.GetRecommendedBuildThreads(
            buildThreads)

        clangExeInfo = PerformClangUtil.LookupRecipeResults(
            packageRecipeResultManager,
            clangFormatConfiguration.RecipePackageName,
            MagicValues.ClangFormatCommand)
        ninjaExeInfo = PerformClangUtil.LookupRecipeResults(
            packageRecipeResultManager,
            clangFormatConfiguration.NinjaRecipePackageName,
            MagicValues.NinjaCommand)

        log.LogPrint("ClangFormat version: {0}".format(clangExeInfo.Version))
        log.LogPrint("Ninja version: {0}".format(ninjaExeInfo.Version))

        log.LogPrint("Running clang-format")

        # Filter the package list so it only contains things we can process
        finalPackageList = [
            package for package in formatPackageList
            if PerformClangUtil.CanProcessPackage(package)
        ]

        sortedPackages = list(finalPackageList)
        sortedPackages.sort(key=lambda s: s.Name.lower())

        #test = set()
        #for entry in sortedPackages:
        #    if entry.Name in test:
        #        raise Exception("duplicated package")
        #    else:
        #       test.add(entry.Name)

        if useLegacyTidyMethod:
            count = PerformClangFormatHelper.Process(
                log, toolConfig, customPackageFileFilter,
                clangFormatConfiguration, clangExeInfo, sortedPackages,
                repairEnabled, numBuildThreads)
        else:
            count = PerformClangFormatHelper2.Process(
                log, toolConfig, customPackageFileFilter,
                clangFormatConfiguration, cmakeConfig, clangExeInfo,
                ninjaExeInfo, sortedPackages, repairEnabled, numBuildThreads)

        if count == 0:
            if customPackageFileFilter is None:
                log.DoPrintWarning("No files processed")
            else:
                log.DoPrintWarning(
                    "No files processed, could not find a package that matches {0}"
                    .format(customPackageFileFilter))
    def Run(log: Log, toolConfig: ToolConfig, formatPackageList: List[Package],
            customPackageFileFilter: Optional[CustomPackageFileFilter],
            packageRecipeResultManager: PackageRecipeResultManager,
            clangFormatConfiguration: ClangFormatConfiguration, repairEnabled: bool, buildThreads: int) -> None:
        """ RunClangFormat on a package at a time """

        # Lookup the recommended build threads using the standard build algorithm
        buildThreads = PlatformBuildUtil.GetRecommendedBuildThreads(buildThreads)

        clangExeInfo = PerformClangUtil.LookupRecipeResults(packageRecipeResultManager, clangFormatConfiguration.RecipePackageName,
                                                            MagicValues.ClangFormatCommand)

        if log.Verbosity >= 1:
            PerformClangUtil.ShowVersion(log, clangExeInfo)

        log.LogPrint("Running clang-format")

        # Filter the package list so it only contains things we can process
        finalPackageList = [package for package in formatPackageList if PerformClangUtil.CanProcessPackage(package)]

        sortedPackages = list(finalPackageList)
        sortedPackages.sort(key=lambda s: s.Name.lower())

        count = PerformClangFormatHelper.Process(log, toolConfig, customPackageFileFilter, clangFormatConfiguration, clangExeInfo, sortedPackages,
                                                 repairEnabled, buildThreads)
        if count == 0:
            if customPackageFileFilter is None:
                log.DoPrintWarning("No files processed")
            else:
                log.DoPrintWarning("No files processed, could not find a package that matches {0}".format(customPackageFileFilter))
示例#4
0
    def __init__(self, log: Log, package: Package,
                 clangFormatConfiguration: ClangFormatConfiguration,
                 filteredFiles: Optional[List[str]]) -> None:
        allFiles = []  # type: List[ResolvedPath]

        if package.ResolvedBuildAllIncludeFiles is not None and package.AllowCheck and not package.IsVirtual:
            if package.AbsolutePath is None or package.ResolvedBuildSourceFiles is None:
                raise Exception("Invalid package")

            if filteredFiles is None:
                for fileName in package.ResolvedBuildAllIncludeFiles:
                    fullPath = IOUtil.Join(package.AbsolutePath, fileName)
                    # Only process files with the expected extension
                    if PerformClangUtil.IsValidExtension(
                            fileName, clangFormatConfiguration.FileExtensions):
                        allFiles.append(ResolvedPath(fileName, fullPath))

                for fileName in package.ResolvedBuildSourceFiles:
                    fullPath = IOUtil.Join(package.AbsolutePath, fileName)
                    if PerformClangUtil.IsValidExtension(
                            fileName, clangFormatConfiguration.FileExtensions):
                        allFiles.append(ResolvedPath(fileName, fullPath))

                if package.ResolvedContentFiles is not None:
                    for resolvedPath in package.ResolvedContentFiles:
                        if PerformClangUtil.IsValidExtension(
                                resolvedPath.ResolvedPath,
                                clangFormatConfiguration.FileExtensions):
                            allFiles.append(
                                self.__GetResolvedPath(package, resolvedPath))

                if package.ResolvedContentBuilderSyncInputFiles is not None:
                    for resolvedPath in package.ResolvedContentBuilderSyncInputFiles:
                        if PerformClangUtil.IsValidExtension(
                                resolvedPath.ResolvedPath,
                                clangFormatConfiguration.FileExtensions):
                            allFiles.append(
                                self.__GetResolvedPath(package, resolvedPath))

                if package.ResolvedContentBuilderBuildInputFiles is not None:
                    for resolvedPath in package.ResolvedContentBuilderBuildInputFiles:
                        if PerformClangUtil.IsValidExtension(
                                resolvedPath.ResolvedPath,
                                clangFormatConfiguration.FileExtensions):
                            allFiles.append(
                                self.__GetResolvedPath(package, resolvedPath))
            else:
                for fileEntry in filteredFiles:
                    allFiles.append(
                        self.__GetResolvedPathFromAbsPath(package, fileEntry))
        self.AllFiles = allFiles
示例#5
0
def Scan(log: Log, scanPackageList: List[Package],
         customPackageFileFilter: Optional[CustomPackageFileFilter],
         repairEnabled: bool, thirdpartyExceptionDir: Optional[str],
         checkType: CheckType, disableWrite: bool) -> None:
    """
    Run through all source files that are part of the packages and check for common errors
    :param scanPackageList: the packages that will be scanned.
    """

    log.LogPrint("Running source scan")

    extensionList = __g_includeExtensionList + __g_sourceExtensionList

    # Filter the package list so it only contains things we can process
    finalPackageList = [
        package for package in scanPackageList
        if PerformClangUtil.CanProcessPackage(package)
    ]

    totalErrorCount = 0
    for package in finalPackageList:
        filteredFiles = None if customPackageFileFilter is None else customPackageFileFilter.TryLocateFilePatternInPackage(
            log, package, extensionList)
        if customPackageFileFilter is None or filteredFiles is not None:
            totalErrorCount += __ScanFiles(log, package, filteredFiles,
                                           repairEnabled,
                                           thirdpartyExceptionDir, checkType,
                                           disableWrite)

    if totalErrorCount > 0 and not repairEnabled:
        log.DoPrint(
            "BEWARE: If you have made a backup of your files you can try to auto correct the errors with '--Repair' but do so at your own peril"
        )
示例#6
0
    def TryLocateFilePatternInPackage(
            self, log: Log, package: Package,
            validExtensionList: List[str]) -> Optional[List[str]]:
        allPackageFileList = self.__GetAllPackageFilesList(package, None)

        filenameDict = self.__CreateFilenameLookupDict(allPackageFileList)
        # pattern match the filenames
        matchedFilenames = fnmatch.filter(filenameDict.keys(),
                                          self.PatternFileName)

        matchedFiles = self.__MatchPaths(matchedFilenames, filenameDict,
                                         self.PatternDirectory)

        # strip all files with unsupported extensions
        matchedFiles = [
            filepath for filepath in matchedFiles
            if PerformClangUtil.IsValidExtension(filepath, validExtensionList)
        ]
        matchedFiles.sort()
        return matchedFiles if len(matchedFiles) > 0 else None
def _RunClangFormat(log: Log, toolConfig: ToolConfig,
                    clangFormatConfiguration: ClangFormatConfiguration,
                    clangExeInfo: ClangExeInfo, package: Package,
                    filteredFiles: Optional[List[str]],
                    repairEnabled: bool) -> None:
    if not package.ResolvedBuildAllIncludeFiles or not package.AllowCheck or package.IsVirtual:
        return

    if package.AbsolutePath is None or package.ResolvedBuildSourceFiles is None:
        raise Exception("Invalid package")

    allFiles = []  # type: List[str]

    if filteredFiles is None:
        for fileName in package.ResolvedBuildAllIncludeFiles:
            fullPath = IOUtil.Join(package.AbsolutePath, fileName)
            # Only process files with the expected extension
            if PerformClangUtil.IsValidExtension(
                    fileName, clangFormatConfiguration.FileExtensions):
                allFiles.append(fileName)

        for fileName in package.ResolvedBuildSourceFiles:
            fullPath = IOUtil.Join(package.AbsolutePath, fileName)
            if PerformClangUtil.IsValidExtension(
                    fileName, clangFormatConfiguration.FileExtensions):
                allFiles.append(fileName)

        if package.ResolvedContentFiles is not None:
            for resolvedPath in package.ResolvedContentFiles:
                if PerformClangUtil.IsValidExtension(
                        resolvedPath.ResolvedPath,
                        clangFormatConfiguration.FileExtensions):
                    allFiles.append(resolvedPath.ResolvedPath)

        if package.ResolvedContentBuilderSyncInputFiles is not None:
            for resolvedPath in package.ResolvedContentBuilderSyncInputFiles:
                if PerformClangUtil.IsValidExtension(
                        resolvedPath.ResolvedPath,
                        clangFormatConfiguration.FileExtensions):
                    allFiles.append(resolvedPath.ResolvedPath)

        if package.ResolvedContentBuilderBuildInputFiles is not None:
            for resolvedPath in package.ResolvedContentBuilderBuildInputFiles:
                if PerformClangUtil.IsValidExtension(
                        resolvedPath.ResolvedPath,
                        clangFormatConfiguration.FileExtensions):
                    allFiles.append(resolvedPath.ResolvedPath)
    else:
        allFiles += filteredFiles

    cmd = clangExeInfo.ClangCommand
    buildCommand = [cmd, '-style=file']
    if repairEnabled:
        buildCommand.append('-i')
    if len(clangFormatConfiguration.AdditionalUserArguments) > 0:
        log.LogPrint("Adding user supplied arguments before '--' {0}".format(
            clangFormatConfiguration.AdditionalUserArguments))
        buildCommand += clangFormatConfiguration.AdditionalUserArguments

    buildCommand += allFiles

    currentWorkingDirectory = package.AbsolutePath
    FileFinder.FindClosestFileInRoot(log, toolConfig, currentWorkingDirectory,
                                     clangFormatConfiguration.CustomFormatFile)

    try:
        # if verbose enabled we log the clang-format version
        if log.Verbosity >= 4:
            log.LogPrint("Running command '{0}' in cwd: {1}".format(
                buildCommand, currentWorkingDirectory))

        result = subprocess.call(buildCommand, cwd=currentWorkingDirectory)
        if result != 0:
            log.LogPrintWarning(
                "The command '{0}' failed with '{1}'. It was run with CWD: '{2}'"
                .format(" ".join(buildCommand), result,
                        currentWorkingDirectory))
            sys.exit(result)
    except FileNotFoundError:
        log.DoPrintWarning(
            "The command '{0}' failed with 'file not found'. It was run with CWD: '{1}'"
            .format(" ".join(buildCommand), currentWorkingDirectory))
        raise
    def Run(log: Log, toolConfig: ToolConfig, platformId: str,
            topLevelPackage: Package, tidyPackageList: List[Package],
            userBuildVariantsDict: Dict[str, str], pythonScriptRoot: str,
            generatorContext: GeneratorContext, sdkConfigTemplatePath: str,
            packageRecipeResultManager: PackageRecipeResultManager,
            performClangTidyConfig: PerformClangTidyConfig,
            customPackageFileFilter: Optional[CustomPackageFileFilter],
            clangFormatFilename: Optional[str], buildThreads: int) -> None:
        """
        RunClangTidy on a package at a time
        :param topLevelPackage: the top level system package
        :param tidyPackageList: the packages to 'tidy'
        :param userBuildVariantsDict: the variant configuration supplied by the user
        :param performClangTidyConfig:
        :param customPackageFileFilter:
        :param clangFormatFilename:
        :param repairEnabled:
        """

        # Lookup the recommended build threads using the standard build algorithm
        buildThreads = PlatformBuildUtil.GetRecommendedBuildThreads(
            buildThreads)

        clangExeInfo = PerformClangUtil.LookupRecipeResults(
            packageRecipeResultManager,
            performClangTidyConfig.ClangTidyConfiguration.RecipePackageName,
            MagicValues.ClangTidyCommand)

        BuildVariantUtil.ValidateUserVariantSettings(log, topLevelPackage,
                                                     userBuildVariantsDict)
        BuildVariantUtil.LogVariantSettings(log, userBuildVariantsDict)
        resolvedVariantSettingsDict = BuildVariantUtil.CreateCompleteStaticVariantSettings(
            topLevelPackage.ResolvedAllVariantDict, userBuildVariantsDict)

        if log.Verbosity >= 1:
            PerformClangUtil.ShowVersion(log, clangExeInfo)
        log.LogPrint("Running clang-tidy")

        # Filter the package list so it only contains things we can process
        finalPackageList = [
            package for package in tidyPackageList
            if PerformClangUtil.CanProcessPackage(package)
        ]

        generatorConfig = GeneratorConfig(sdkConfigTemplatePath, toolConfig)
        generatorReportDict = generatorContext.Generator.GenerateReport(
            log, generatorConfig, finalPackageList)

        # Validate report dict
        for package in finalPackageList:
            if package not in generatorReportDict:
                raise Exception(
                    "Generator report is missing information for package '{0}'"
                    .format(package.Name))

        localVariantInfo = LocalVariantInfo(resolvedVariantSettingsDict,
                                            generatorReportDict,
                                            pythonScriptRoot)

        count = PerformClangTidyHelper.ProcessAllPackages(
            log, toolConfig, platformId, pythonScriptRoot,
            performClangTidyConfig, clangExeInfo, finalPackageList,
            customPackageFileFilter, localVariantInfo, buildThreads)
        if count == 0:
            if customPackageFileFilter is None:
                log.DoPrintWarning("No files processed")
            else:
                log.DoPrintWarning(
                    "No files processed, could not find a package that matches {0}"
                    .format(customPackageFileFilter))
def _RunClangTidy(
        log: Log,
        toolConfig: ToolConfig,
        platformId: str,
        performClangTidyConfig: PerformClangTidyConfig,
        clangExeInfo: ClangExeInfo,
        package: Package,
        filteredFiles: Optional[List[str]],
        clangFormatFilename: Optional[str],
        localVariantInfo: LocalVariantInfo,
        virtualVariantEnvironmentCache: VirtualVariantEnvironmentCache,
        logOutput: bool = False) -> None:
    if package.ResolvedBuildAllIncludeFiles is None:
        raise Exception("invalid package")
    log.LogPrint("- {0}".format(package.Name))

    clangTidyConfiguration = performClangTidyConfig.ClangTidyConfiguration

    if package.AbsolutePath is None or package.ResolvedBuildSourceFiles is None:
        raise Exception("Invalid package")

    allFiles = []  # type: List[str]

    if filteredFiles is None:
        for fileName in package.ResolvedBuildAllIncludeFiles:
            fullPath = IOUtil.Join(package.AbsolutePath, fileName)
            # Only process files with the expected extension
            if PerformClangUtil.IsValidExtension(
                    fileName, clangTidyConfiguration.FileExtensions):
                allFiles.append(fileName)

        for fileName in package.ResolvedBuildSourceFiles:
            fullPath = IOUtil.Join(package.AbsolutePath, fileName)
            if PerformClangUtil.IsValidExtension(
                    fileName, clangTidyConfiguration.FileExtensions):
                allFiles.append(fileName)
    else:
        for fileName in filteredFiles:
            if PerformClangUtil.IsValidExtension(
                    fileName, clangTidyConfiguration.FileExtensions):
                allFiles.append(fileName)

    # ensure we process the files in the same order every time
    allFiles.sort()

    platformCompilerFlags = []  # type: List[str]
    platformDefineCommands = []  # type: List[str]
    platformStrictChecks = []  # type: List[str]
    if platformId in clangTidyConfiguration.PlatformDict:
        clangPlatformConfig = clangTidyConfiguration.PlatformDict[platformId]
        platformCompilerFlags = clangPlatformConfig.Compiler.Flags
        for platformDefine in clangPlatformConfig.Defines.All:
            platformDefineCommands.append('-D')
            platformDefineCommands.append(platformDefine)
        # We default to release for now
        for platformDefine in clangPlatformConfig.Defines.Release:
            platformDefineCommands.append('-D')
            platformDefineCommands.append(platformDefine)
        for strictCheck in clangPlatformConfig.StrictChecks:
            platformStrictChecks.append(strictCheck)

    includePaths = __BuildClangTidyPackageIncludePaths(
        log, localVariantInfo, virtualVariantEnvironmentCache, package)
    packageDefineCommands = __BuildClangTidyPackageDefines(
        log, localVariantInfo, package)

    cmd = clangExeInfo.ClangCommand
    buildCommand = [cmd]
    if performClangTidyConfig.Repair:
        buildCommand.append('-fix')

    #buildCommand.append('-fix-errors')

    #buildCommand.append('-header-filter=.*')

    usingCheckCommand = False
    if len(performClangTidyConfig.OverrideChecks) > 0:
        newOverrideChecks = ",".join(performClangTidyConfig.OverrideChecks)
        log.LogPrintVerbose(
            2, "Overriding checks checks '{0}'".format(newOverrideChecks))
        if performClangTidyConfig.StrictChecks:
            log.DoPrintWarning(
                "Ignoreing strict checks because 'override' is enabled")
        buildCommand.append("--checks")
        buildCommand.append(newOverrideChecks)
        usingCheckCommand = True
    elif performClangTidyConfig.StrictChecks and len(platformStrictChecks) > 0:
        newStrictChecks = ",".join(platformStrictChecks)
        log.LogPrintVerbose(
            2, "Adding strict checks '{0}'".format(newStrictChecks))
        buildCommand.append("--checks")
        buildCommand.append(newStrictChecks)
        usingCheckCommand = True

    if len(performClangTidyConfig.AdditionalUserArguments) > 0:
        log.LogPrintVerbose(
            2, "Adding user supplied arguments before '--' {0}".format(
                performClangTidyConfig.AdditionalUserArguments))
        if usingCheckCommand and '--checks' in performClangTidyConfig.AdditionalUserArguments:
            log.DoPrintWarning(
                "another command is adding '--checks' so it could conflict with the user supplied argument"
            )
        buildCommand += performClangTidyConfig.AdditionalUserArguments

    buildCommand += allFiles

    buildCommand.append('--')

    if len(platformCompilerFlags) > 0:
        buildCommand += __LookupEnvironmentVariables(
            platformCompilerFlags, virtualVariantEnvironmentCache)
    if len(platformDefineCommands) > 0:
        buildCommand += platformDefineCommands

    if len(includePaths) > 0:
        buildCommand += includePaths
    if len(packageDefineCommands) > 0:
        buildCommand += packageDefineCommands

    if len(performClangTidyConfig.PostfixArguments) > 0:
        log.LogPrintVerbose(
            2, "Adding user supplied arguments after '--' {0}".format(
                performClangTidyConfig.PostfixArguments))
        buildCommand += performClangTidyConfig.PostfixArguments

    currentWorkingDirectory = package.AbsolutePath

    if clangFormatFilename is not None:
        FileFinder.FindClosestFileInRoot(log, toolConfig,
                                         currentWorkingDirectory,
                                         clangFormatFilename)
    FileFinder.FindClosestFileInRoot(log, toolConfig, currentWorkingDirectory,
                                     clangTidyConfiguration.CustomTidyFile)

    try:
        if log.Verbosity >= 4:
            log.LogPrint("Running command '{0}' in cwd: {1}".format(
                buildCommand, currentWorkingDirectory))
        result = __RunNow(log, buildCommand, currentWorkingDirectory,
                          logOutput)
        if result != 0:
            log.LogPrintWarning(
                "The command '{0}' failed with '{1}'. It was run with CWD: '{2}'"
                .format(" ".join(buildCommand), result,
                        currentWorkingDirectory))
            raise ExitException(result)
    except FileNotFoundError:
        log.DoPrintWarning(
            "The command '{0}' failed with 'file not found'. It was run with CWD: '{1}'"
            .format(" ".join(buildCommand), currentWorkingDirectory))
        raise