static async Task <int> MainAsync(ProgramOptions options) { var engine = new VCProjectEngineObject(); if (options.PrintPlatforms) { PrintPlatforms(engine); return(0); } var dir = Path.GetDirectoryName(options.ProjectFilePath); var baseJsonPath = Path.Combine(dir, "compile_commands.base.json"); var baseObj = File.Exists(baseJsonPath) ? JsonSerializer.Deserialize <FileCompilationInfo>(File.ReadAllBytes(baseJsonPath)) : new FileCompilationInfo(); if (baseObj.arguments != null && baseObj.command != null) { Console.Error.WriteLine("You must specify either `arguments` or `command` in compile_commands.base.json. "); return(2); } baseObj.directory = dir; var output = new List <FileCompilationInfo>(); var project = engine.LoadProject(options.ProjectFilePath) as VCProject; if (project == null) { Console.Error.WriteLine("Invalid project file."); return(3); } //var buildTarget = "Release|Win32"; //var buildTarget = "Release|Windows Mobile 5.0 Pocket PC SDK (ARMV4I)"; var buildTarget = options.BuildTarget; foreach (VCFile file in project.Files) { var fileConfigurations = file.FileConfigurations as IVCCollection; VCFileConfiguration fileConfig = fileConfigurations.Item(buildTarget); if (fileConfig == null) { Console.Error.WriteLine($"warning: Build Target [{buildTarget}] Not Found for `{file.Name}`"); continue; } var platform = buildTarget.Split('|')[1]; var m = new Regex(@"\((\w+)\)").Match(platform); var arch = platform == "Win64" ? "x86_64" : m.Success ? m.Groups[1].Value.ToLower() : "i386"; var entry = ProcessVCFile(options, baseObj.Clone(), file, fileConfig, arch, platform == "Win64"); if (entry != null) { output.Add(entry); } } if (options.OnlyConvertToUtf8) { return(0); } using (var stream = File.OpenWrite(Path.Combine(dir, CompileCommandsJson))) { stream.SetLength(0); await JsonSerializer.SerializeAsync(stream, output, new JsonSerializerOptions() { WriteIndented = true, IgnoreNullValues = true, }); } return(0); }
static void Main(string[] args) { try { // Create a project engine VCProjectEngine projEngine = new VCProjectEngineObject(); // Load a project, change the name, add a configuration VCProject project = (VCProject)projEngine.LoadProject(@"..\..\..\MyProject\MyProject.vcproj"); if (project != null) { //Change the project's name project.Name = "Voila"; //Add a new configuration project.AddConfiguration("Whichever Name"); // Get the debug configuration and change the type to application VCConfiguration configuration = (VCConfiguration)(((IVCCollection)project.Configurations).Item("Debug")); if (configuration != null) { configuration.ConfigurationType = ConfigurationTypes.typeApplication; } else { Console.WriteLine(@"I Couldn't find the configuration"); } // Get the linker tool from the configration. VCLinkerTool linkerTool = (VCLinkerTool)(((IVCCollection)configuration.Tools).Item("VCLinkerTool")); if (linkerTool != null) { // Change the ShowProgress property to "Display All Progress Messages (/VERBOSE)" linkerTool.ShowProgress = linkProgressOption.linkProgressAll; } else { Console.WriteLine(@"I Couldn't find the linkerTool"); } // Add a cpp file called New.cpp if (project.CanAddFile("New.cpp")) { project.AddFile("New.cpp"); } else { Console.WriteLine(@"I Couldn't add the file"); } // Access the files collection IVCCollection filesCollection = (IVCCollection)project.Files; if (filesCollection != null) { // Access a cpp files called bar.cpp that is already in the project. VCFile file = (VCFile)(filesCollection.Item("Existing.cpp")); if (file != null) { // Access the release configuration of this file. VCFileConfiguration fileConfiguration = (VCFileConfiguration)(((IVCCollection)file.FileConfigurations).Item("Release|Win32")); // Get the compiler tool associated with this file. VCCLCompilerTool compilerTool = (VCCLCompilerTool)fileConfiguration.Tool; // Change the optimization property to Full Optimization (/Ox) compilerTool.Optimization = optimizeOption.optimizeFull; } else { Console.WriteLine(@"I Couldn't find the file"); } // Save the project, then remove it. project.ProjectFile = "MyNewProject.vcproj"; project.Save(); } else { Console.WriteLine(@"I Couldn't find the file collection"); } } else { Console.WriteLine(@"I Couldn't find the project"); } } catch (Exception e) { Console.WriteLine("Operation failed for the following reason: {0}", e.Message); } }
/****************************************************************/ #endregion /* Members */ #region LifeCycle /****************************************************************/ public VcProjInfo(string vcProjFile, string outFile, string[] projDependencies) { Console.WriteLine("Going to read project file '{0}'", vcProjFile); Console.WriteLine("aiming to write result to file '{0}'", outFile); if (projDependencies.GetLength(0) <= 0) { Console.WriteLine("With no dependencies."); } else { Console.WriteLine("With dependencies:"); foreach (String s in projDependencies) { Console.WriteLine("\t" + s); } //projDependencies.ToString()); } Console.WriteLine(""); // to visually separate output. //Init project name and .mak file name m_ProjectName = Path.GetFileNameWithoutExtension(vcProjFile); m_MakeFileName = outFile; m_VcProjDependencies = projDependencies; VCProjectEngine vcprojEngine = new VCProjectEngineObject(); try { //Init VCProject vcProj object m_VcProj = (VCProject)vcprojEngine.LoadProject(vcProjFile); } catch (Exception ex) { Console.WriteLine("Some errors occurred during project loading: {0}\n\n\n", ex.Message); Console.WriteLine("This tool knows to convert Visual Studio 2005 projects only\n"); Console.WriteLine("For converting Visual Studio 2008 projects, compile this tool on VS2008 \n"); Console.WriteLine("add Microsoft.VisualStudio.VCProjectEngine9.0.0 reference\n"); } //Init vcproj configurations list m_ConfigCollection = (IVCCollection)m_VcProj.Configurations; //Init targets dictionary (Key is configuration name, Value is VcTargetType object m_TargetDictionary = new Dictionary <string, VcTargetType>(); foreach (VCConfiguration config in m_ConfigCollection) { m_TargetDictionary.Add(config.ConfigurationName, new VcTargetType(config)); } //Init number of vcProj configurations m_NumOfConfigs = m_ConfigCollection.Count; //Init platform name of vcProj IVCCollection platformCollection = (IVCCollection)m_VcProj.Platforms; VCPlatform platform = (VCPlatform)platformCollection.Item(1); m_PlatfromName = platform.Name; //Init Filters collection m_FileFilterCollection = (IVCCollection)m_VcProj.Filters; Console.WriteLine("Creating file " + m_MakeFileName + "..."); //Open StreamWriter for writing into makFileName m_MakFWriter = new StreamWriter(m_MakeFileName); m_SRCGroups = "SRCS="; }