public static bool operator ==(CompilationDatabase a, CompilationDatabase b)
        {
            if (System.Object.ReferenceEquals(a, b))
            {
                return(true);
            }

            if (((object)a == null) || ((object)b == null))
            {
                return(false);
            }

            if (a.CompileCommandCount != b.CompileCommandCount)
            {
                return(false);
            }

            foreach (CompileCommand aCommand in a._compileCommands)
            {
                CompileCommand bCommand = b._compileCommands.Find(x => x == aCommand);
                if (bCommand == null || aCommand.File != bCommand.File)
                {
                    return(false);
                }
            }

            return(true);
        }
Beispiel #2
0
        public static CompileCommand DeserializeFromJson(string serialized)
        {
            CompileCommand command = null;

            if (serialized.Length > 0)
            {
                command = JsonConvert.DeserializeObject <CompileCommand>(serialized);
            }
            return(command);
        }
        private CompileCommand CreateCompileCommand(ProjectItem item, string commandFlags, string vcStandard, string cStandard, bool isMakefileProject)
        {
            Logging.Logging.LogInfo("Starting to create Command Object from item \"" + Logging.Obfuscation.NameObfuscator.GetObfuscatedName(item.Name) + "\"");
            try
            {
                DTE dte = item.DTE;

                if (dte == null)
                {
                    Logging.Logging.LogError("Failed to retrieve DTE object. Abort creating command object.");
                }

                IVCFileWrapper vcFile = VCFileWrapperFactory.create(item.Object);

                IVCCLCompilerToolWrapper compilerTool = null;
                if (!isMakefileProject)
                {
                    List <IVCFileConfigurationWrapper> vcFileConfigurations = vcFile.GetFileConfigurations();
                    foreach (IVCFileConfigurationWrapper vcFileConfiguration in vcFileConfigurations)
                    {
                        if (vcFileConfiguration != null && vcFileConfiguration.isValid())
                        {
                            compilerTool = vcFileConfiguration.GetCLCompilerTool();
                            if (compilerTool != null && compilerTool.isValid())
                            {
                                break;
                            }
                        }
                    }
                }

                if (CheckIsSourceFile(item))
                {
                    string additionalOptions = "";
                    if (compilerTool != null && compilerTool.isValid())
                    {
                        additionalOptions = compilerTool.GetAdditionalOptions();

                        if (additionalOptions == "$(NOINHERIT)")
                        {
                            additionalOptions = "";
                        }
                    }

                    string languageStandardOption;
                    if (additionalOptions.Contains("-std="))
                    {
                        // if a language standard was defined in the additional options we do not need to set the language standard again.
                        languageStandardOption = "";
                    }
                    else
                    {
                        if (compilerTool != null && compilerTool.isValid() && compilerTool.GetCompilesAsC())
                        {
                            languageStandardOption = "-std=" + cStandard;
                        }
                        else if (compilerTool != null && compilerTool.isValid() && compilerTool.GetCompilesAsCPlusPlus())
                        {
                            languageStandardOption = "-std=" + vcStandard;
                        }
                        else
                        {
                            // we cannot derive the language from the compiler tool, so we need to check the file extension
                            if (ProjectUtility.HasProperty(item.Properties, "Extension") &&
                                _cFileExtensions.Contains(item.Properties.Item("Extension").Value.ToString().Substring(1))                                 // extension property starts with "."
                                )
                            {
                                languageStandardOption = "-std=" + cStandard;
                            }
                            else
                            {
                                languageStandardOption = "-std=" + vcStandard;
                            }
                        }
                    }

                    CompileCommand command = new CompileCommand();

                    string relativeFilePath = item.Properties.Item("RelativePath").Value.ToString();
                    command.File = _pathResolver.GetAsAbsoluteCanonicalPath(relativeFilePath, vcFile.GetProject());
                    command.File = command.File.Replace('\\', '/');

                    command.Directory = _pathResolver.GetCompilationDatabaseFilePath();

                    command.Command = "clang-tool " + commandFlags;

                    command.Command += languageStandardOption + " ";

                    command.Command += additionalOptions + " ";

                    command.Command += "\"" + command.File + "\"";

                    return(command);
                }
            }
            catch (Exception e)
            {
                Logging.Logging.LogError("Exception: " + e.Message);
            }

            Logging.Logging.LogError("Failed to create command object.");

            return(null);
        }
Beispiel #4
0
        public void CreateCompileCommands(Project project, string solutionConfigurationName, string solutionPlatformName, string cStandard, string additionalClangOptions, bool nonSystemIncludesUseAngleBrackets, Action <CompileCommand> lambda)
        {
            Logging.Logging.LogInfo("Creating command objects for project \"" + Logging.Obfuscation.NameObfuscator.GetObfuscatedName(project.Name) + "\".");

            DTE  dte         = project.DTE;
            Guid projectGuid = Utility.ProjectUtility.ReloadProject(project);

            IVCProjectWrapper vcProject = VCProjectEngineWrapper.VCProjectWrapperFactory.create(project.Object);

            if (vcProject != null && vcProject.isValid())
            {
                Logging.Logging.LogInfo("Project \"" + Logging.Obfuscation.NameObfuscator.GetObfuscatedName(project.Name) + "\" has been converted to VCProject " + vcProject.GetWrappedVersion() + ".");
            }
            else
            {
                Logging.Logging.LogWarning("Project \"" + Logging.Obfuscation.NameObfuscator.GetObfuscatedName(project.Name) + "\" could not be converted to VCProject, skipping.");
                return;
            }

            string projectConfigurationName = "";
            string projectPlatformName      = "";

            foreach (SolutionConfiguration2 solutionConfiguration in SolutionUtility.GetSolutionBuild2(dte).SolutionConfigurations)
            {
                if (solutionConfiguration.Name == solutionConfigurationName && solutionConfiguration.PlatformName == solutionPlatformName)
                {
                    foreach (SolutionContext solutionContext in solutionConfiguration.SolutionContexts)
                    {
                        if (vcProject.GetProjectFile().EndsWith(solutionContext.ProjectName))
                        {
                            projectConfigurationName = solutionContext.ConfigurationName;
                            projectPlatformName      = solutionContext.PlatformName;
                        }
                    }
                }
            }

            if (projectConfigurationName.Length == 0 || projectPlatformName.Length == 0)
            {
                Logging.Logging.LogWarning("No project configuration found for solution configuration, trying to use solution configuration on project.");
                projectConfigurationName = solutionConfigurationName;
                projectPlatformName      = solutionPlatformName;
            }

            IVCConfigurationWrapper vcProjectConfiguration = vcProject.getConfiguration(projectConfigurationName, projectPlatformName);

            if (vcProjectConfiguration != null && vcProjectConfiguration.isValid())
            {
                SetCompatibilityVersionFlag(vcProject, vcProjectConfiguration);

                string commandFlags = "";
                {
                    // gather include paths and preprocessor definitions of the project
                    List <string> projectIncludeDirectories = ProjectUtility.GetProjectIncludeDirectories(vcProject, vcProjectConfiguration, _pathResolver);
                    List <string> systemIncludeDirectories  = ProjectUtility.GetSystemIncludeDirectories(vcProject, vcProjectConfiguration, _pathResolver);
                    List <string> preprocessorDefinitions   = ProjectUtility.GetPreprocessorDefinitions(vcProject, vcProjectConfiguration);
                    List <string> forcedIncludeFiles        = ProjectUtility.GetForcedIncludeFiles(vcProject, vcProjectConfiguration, _pathResolver);

                    foreach (string flag in _compatibilityFlags)
                    {
                        commandFlags += flag + " ";
                    }

                    commandFlags += _compatibilityVersionFlag + " ";

                    if (!string.IsNullOrWhiteSpace(additionalClangOptions))
                    {
                        commandFlags += additionalClangOptions;
                    }

                    if (nonSystemIncludesUseAngleBrackets)
                    {
                        systemIncludeDirectories.InsertRange(0, projectIncludeDirectories);
                        projectIncludeDirectories.Clear();
                    }

                    foreach (string dir in projectIncludeDirectories)
                    {
                        commandFlags += " -I \"" + dir + "\" ";
                    }

                    foreach (string dir in systemIncludeDirectories)
                    {
                        commandFlags += " -isystem \"" + dir + "\" ";
                    }

                    foreach (string prepDef in preprocessorDefinitions)
                    {
                        commandFlags += " -D " + prepDef + " ";
                    }

                    foreach (string file in forcedIncludeFiles)
                    {
                        commandFlags += " -include \"" + file + "\" ";
                    }
                }

                string cppStandard = Utility.ProjectUtility.GetCppStandardForProject(vcProject);
                Logging.Logging.LogInfo("Found C++ standard " + cppStandard + ".");

                bool isMakefileProject = vcProjectConfiguration.isMakefileConfiguration();

                // create command objects for all applicable project items
                {
                    foreach (ProjectItem item in Utility.ProjectUtility.GetProjectItems(project))
                    {
                        CompileCommand command = CreateCompileCommand(item, commandFlags, cppStandard, cStandard, isMakefileProject);
                        if (command == null)
                        {
                            continue;
                        }
                        lambda(command);
                    }
                }

                if (projectGuid != Guid.Empty)
                {
                    Utility.ProjectUtility.UnloadProject(projectGuid, dte);
                }
            }
            else
            {
                Logging.Logging.LogError("No project configuration found. Skipping this project");
            }
        }
 public void AddCompileCommand(CompileCommand commandObject)
 {
     _compileCommands.Add(commandObject);
 }