Esempio n. 1
0
        private static bool WriteFilters(VSProject project, VSFullConfiguration[] fullConfigurations)
        {
            Directory.CreateDirectory(project.folderUrl);

            using (FileStream fileStream = new FileStream(project.filtersUrl, FileMode.Create))
                using (StreamWriter streamWriter = new StreamWriter(fileStream))
                    using (XmlTextWriter writer = new XmlTextWriter(streamWriter))
                    {
                        writer.Formatting  = Formatting.Indented;
                        writer.Indentation = 2;

                        writer.WriteStartDocument();
                        writer.WriteStartElement("Project", "http://schemas.microsoft.com/developer/msbuild/2003");
                        writer.WriteAttributeString("ToolsVersion", "4.0");


                        HashSet <string> files = new HashSet <string>();
                        // Add base files to filter
                        foreach (string file in Directory.GetFiles(project.module.folderUrl))
                        {
                            files.Add(file);
                        }

                        // Add all config files
                        foreach (var fullConfig in fullConfigurations)
                        {
                            ModuleRecipe recipe = project.module.GetRecipe(fullConfig.AsRecipeTarget());
                            foreach (string file in recipe.GetAllTrackedFiles(project.module.folderUrl))
                            {
                                files.Add(file);
                            }
                        }

                        Dictionary <string, Guid> filters = cachedGuids;

                        // Create all required filters
                        writer.WriteStartElement("ItemGroup");
                        foreach (string file in files)
                        {
                            string relativeFile = file.Substring(project.module.folderUrl.Length);
                            string filter       = GetFilterFromFile(relativeFile);

                            if (filter.Length != 0)
                            {
                                if (!filters.ContainsKey(filter))
                                {
                                    // Add all sub filters
                                    string currentFilter = "";

                                    foreach (var subFilter in filter.Split('\\'))
                                    {
                                        if (currentFilter.Length == 0)
                                        {
                                            currentFilter = subFilter;
                                        }
                                        else
                                        {
                                            currentFilter += '\\' + subFilter;
                                        }

                                        if (!filters.ContainsKey(currentFilter))
                                        {
                                            writer.WriteStartElement("Filter");
                                            writer.WriteAttributeString("Include", currentFilter);

                                            Guid guid = Guid.NewGuid();
                                            filters[currentFilter] = guid;

                                            writer.WriteElementString("UniqueIdentifier", "{" + guid.ToString().ToUpper() + "}");
                                            writer.WriteEndElement();
                                        }
                                    }
                                }
                            }
                        }
                        writer.WriteEndElement();


                        // Add ClCompiler ClInclude and None references
                        writer.WriteStartElement("ItemGroup");
                        foreach (string file in files)
                        {
                            string relativeFile = file.Substring(project.module.folderUrl.Length);
                            string filter       = GetFilterFromFile(relativeFile);

                            if (file.EndsWith(".cpp") || file.EndsWith(".c"))
                            {
                                writer.WriteStartElement("ClCompile");
                            }
                            else if (file.EndsWith(".hpp") || file.EndsWith(".h"))
                            {
                                writer.WriteStartElement("ClInclude");
                            }
                            else
                            {
                                writer.WriteStartElement("None");
                            }

                            writer.WriteAttributeString("Include", file);

                            if (filters.ContainsKey(filter))
                            {
                                writer.WriteElementString("Filter", filter);
                            }

                            writer.WriteEndElement();
                        }
                        writer.WriteEndElement();


                        writer.WriteEndElement();
                        writer.WriteEndDocument();

                        writer.Flush();
                        writer.Close();
                    }

            return(true);
        }
Esempio n. 2
0
        private static bool WriteProject(VSProject project, VSFullConfiguration[] fullConfigurations)
        {
            Directory.CreateDirectory(project.folderUrl);

            using (FileStream fileStream = new FileStream(project.projectUrl, FileMode.Create))
                using (StreamWriter streamWriter = new StreamWriter(fileStream))
                    using (XmlTextWriter writer = new XmlTextWriter(streamWriter))
                    {
                        writer.Formatting  = Formatting.Indented;
                        writer.Indentation = 2;

                        writer.WriteStartDocument();
                        writer.WriteStartElement("Project", "http://schemas.microsoft.com/developer/msbuild/2003");
                        writer.WriteAttributeString("DefaultTargets", "Build");
                        writer.WriteAttributeString("ToolsVersion", project.toolsVersion);

                        // START - Configurations Defs
                        writer.WriteStartElement("ItemGroup");
                        writer.WriteAttributeString("Label", "ProjectConfigurations");

                        foreach (var fullConfig in fullConfigurations)
                        {
                            writer.WriteStartElement("ProjectConfiguration");
                            writer.WriteAttributeString("Include", fullConfig.configName + "|" + fullConfig.platform.platform);

                            writer.WriteElementString("Configuration", fullConfig.configName);
                            writer.WriteElementString("Platform", fullConfig.platform.platform);
                            writer.WriteEndElement();
                        }
                        writer.WriteEndElement();
                        // END - Configurations Defs


                        // Globals
                        writer.WriteStartElement("PropertyGroup");
                        writer.WriteAttributeString("Label", "Globals");
                        writer.WriteElementString("ProjectGuid", '{' + project.module.guid.ToString().ToUpper() + '}');
                        writer.WriteElementString("RootNamespace", project.module.internalName);
                        writer.WriteElementString("WindowsTargetPlatformVersion", "8.1");
                        writer.WriteEndElement();
                        // END Globals


                        // Props
                        writer.WriteStartElement("Import");
                        writer.WriteAttributeString("Project", "$(VCTargetsPath)\\Microsoft.Cpp.Default.props");
                        writer.WriteEndElement();


                        // START - Configurations Settings
                        foreach (var fullConfig in fullConfigurations)
                        {
                            ModuleRecipe recipe = project.module.GetRecipe(fullConfig.AsRecipeTarget());

                            // Configuration
                            writer.WriteStartElement("PropertyGroup");
                            writer.WriteAttributeString("Label", "Configuration");
                            writer.WriteAttributeString("Condition", "'$(Configuration)|$(Platform)'=='" + fullConfig.configName + "|" + fullConfig.platform.platform + "'");
                            writer.WriteElementString("UseDebugLibraries", recipe.debugLibrary.ToString());
                            writer.WriteElementString("WholeProgramOptimization", recipe.optimizeOutput.ToString());
                            writer.WriteElementString("PlatformToolset", "v140");
                            writer.WriteElementString("CharacterSet", "MultiByte");
                            if (recipe.output == ModuleRecipe.OutputType.Application)
                            {
                                writer.WriteElementString("ConfigurationType", "Application");
                            }
                            else if (recipe.output == ModuleRecipe.OutputType.Dynamic)
                            {
                                writer.WriteElementString("ConfigurationType", "DynamicLibrary");
                            }
                            else if (recipe.output == ModuleRecipe.OutputType.Static)
                            {
                                writer.WriteElementString("ConfigurationType", "StaticLibrary");
                            }

                            writer.WriteEndElement();             // PropertyGroup
                        }
                        // END - Configurations Settings


                        writer.WriteStartElement("Import");
                        writer.WriteAttributeString("Project", "$(VCTargetsPath)\\Microsoft.Cpp.props");
                        writer.WriteEndElement();


                        // START - Property sheets
                        foreach (var fullConfig in fullConfigurations)
                        {
                            ModuleRecipe recipe = project.module.GetRecipe(fullConfig.AsRecipeTarget());

                            // Property sheet
                            writer.WriteStartElement("ImportGroup");
                            writer.WriteAttributeString("Label", "PropertySheets");
                            writer.WriteAttributeString("Condition", "'$(Configuration)|$(Platform)'=='" + fullConfig.configName + "|" + fullConfig.platform.platform + "'");

                            writer.WriteStartElement("Import");
                            writer.WriteAttributeString("Label", "LocalAppDataPlatform");
                            writer.WriteAttributeString("Project", "$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props");
                            writer.WriteAttributeString("Condition", "exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')");
                            writer.WriteEndElement();             // Import

                            writer.WriteEndElement();             // ImportGroup
                        }
                        // END - Property sheets


                        // START - Folder Settings
                        foreach (var fullConfig in fullConfigurations)
                        {
                            ModuleRecipe recipe = project.module.GetRecipe(fullConfig.AsRecipeTarget());

                            // Item defs
                            writer.WriteStartElement("PropertyGroup");
                            writer.WriteAttributeString("Condition", "'$(Configuration)|$(Platform)'=='" + fullConfig.configName + "|" + fullConfig.platform.platform + "'");

                            writer.WriteElementString("OutDir", Path.Combine(project.module.collection.outputDirectory, "$(Platform)", project.module.displayName) + "\\");
                            writer.WriteElementString("IntDir", Path.Combine(project.module.collection.metaDirectory, "VS\\$(Platform)\\$(Configuration)", project.module.displayName) + "\\");
                            writer.WriteElementString("TargetName", project.module.displayName + " " + fullConfig.target.name + " " + fullConfig.configuration.name);

                            writer.WriteEndElement();             // PropertyGroup
                        }
                        // END - Folder Settings


                        // START - Compilation Settings
                        foreach (var fullConfig in fullConfigurations)
                        {
                            ModuleRecipe recipe = project.module.GetRecipe(fullConfig.AsRecipeTarget());

                            // Item defs
                            writer.WriteStartElement("ItemDefinitionGroup");
                            writer.WriteAttributeString("Condition", "'$(Configuration)|$(Platform)'=='" + fullConfig.configName + "|" + fullConfig.platform.platform + "'");
                            writer.WriteStartElement("ClCompile");

                            writer.WriteElementString("WarningLevel", "Level" + project.warningLevel.ToString());
                            writer.WriteElementString("TreatWarningAsError", "true");
                            writer.WriteElementString("Optimization", recipe.optimizeOutput ? "MaxSpeed" : "Disabled");
                            writer.WriteElementString("FunctionLevelLinking", recipe.optimizeOutput.ToString());
                            writer.WriteElementString("IntrinsicFunctions", recipe.optimizeOutput.ToString());
                            writer.WriteElementString("DebugInformationFormat", !recipe.optimizeOutput ? "ProgramDatabase" : "EditAndContinue");
                            writer.WriteElementString("SDLCheck", "true");

                            // Precompiled header
                            if (recipe.precompiledHeader.Length != 0)
                            {
                                writer.WriteElementString("PrecompiledHeader", "Create");
                                writer.WriteElementString("PrecompiledHeaderFile", recipe.precompiledHeader.Replace('/', '\\'));
                                writer.WriteElementString("PrecompiledHeaderOutputFile", Path.Combine(project.module.collection.metaDirectory, "VS\\$(Platform)\\$(Configuration)\\PCH", project.module.displayName + ".pch"));
                            }

                            // Definitions
                            string defs = "";
                            foreach (string def in recipe.GetFullDefinitions())
                            {
                                if (def.Length != 0)
                                {
                                    defs += def + ";";
                                }
                            }
                            writer.WriteElementString("PreprocessorDefinitions", defs + "%(PreprocessorDefinitions)");

                            // Includes
                            string includes = "";
                            foreach (string dir in recipe.GetFullCodeDirectories())
                            {
                                if (dir.Length != 0)
                                {
                                    includes += dir + ';';
                                }
                            }
                            writer.WriteElementString("AdditionalIncludeDirectories", includes);

                            writer.WriteEndElement();             // ClCompile


                            writer.WriteStartElement("Link");
                            writer.WriteElementString("EnableCOMDATFolding", recipe.optimizeOutput.ToString());
                            writer.WriteElementString("OptimizeReferences", recipe.optimizeOutput.ToString());
                            writer.WriteEndElement();             // Link

                            // Add any DLL dependencies
                            writer.WriteStartElement("PostBuildEvent");
                            writer.WriteElementString("Message", "Copying required dynamic modules to %(OutDir)");
                            writer.WriteEndElement();

                            foreach (ModuleRecipe dependency in recipe.GetFullModuleDependencies())
                            {
                                if (dependency.output == ModuleRecipe.OutputType.Dynamic)
                                {
                                    VSProject dudProject           = new VSProject(dependency.module);
                                    string    dependencyOutputFile = dudProject.module.displayName + " " + fullConfig.target.name + " " + fullConfig.configuration.name + ".dll";
                                    string    dependencyOutputDir  = Path.Combine(dudProject.module.collection.outputDirectory, "$(Platform)", dudProject.module.displayName) + "\\";
                                    string    dependencyOutputUrl  = Path.Combine(dependencyOutputDir, dependencyOutputFile);

                                    writer.WriteStartElement("PostBuildEvent");
                                    writer.WriteElementString("Command", "copy \"" + dependencyOutputUrl + "\" \"$(OutDir)\"");
                                    writer.WriteEndElement();
                                }
                            }

                            writer.WriteEndElement();             // ItemDefinitionGroup
                        }
                        // END - Compilation Settings


                        // ProjectReferences
                        foreach (var fullConfig in fullConfigurations)
                        {
                            ModuleRecipe recipe = project.module.GetRecipe(fullConfig.AsRecipeTarget());

                            writer.WriteStartElement("ItemGroup");
                            writer.WriteAttributeString("Condition", "'$(Configuration)|$(Platform)'=='" + fullConfig.configName + "|" + fullConfig.platform.platform + "'");
                            foreach (ModuleRecipe dependency in recipe.GetFullModuleDependencies())
                            {
                                VSProject dudProject = new VSProject(dependency.module);

                                writer.WriteStartElement("ProjectReference");
                                writer.WriteAttributeString("Include", dudProject.projectUrl);
                                writer.WriteElementString("Project", "{" + dependency.module.guid.ToString() + "}");
                                writer.WriteEndElement();
                            }
                            writer.WriteEndElement();
                        }

                        // Add ClCompiler ClInclude and None references
                        foreach (var fullConfig in fullConfigurations)
                        {
                            ModuleRecipe recipe = project.module.GetRecipe(fullConfig.AsRecipeTarget());
                            string[]     files  = recipe.GetAllTrackedFiles(project.module.folderUrl);

                            writer.WriteStartElement("ItemGroup");
                            writer.WriteAttributeString("Condition", "'$(Configuration)|$(Platform)'=='" + fullConfig.configName + "|" + fullConfig.platform.platform + "'");
                            foreach (string file in files)
                            {
                                if (file.EndsWith(".cpp") || file.EndsWith(".c"))
                                {
                                    writer.WriteStartElement("ClCompile");
                                }
                                else if (file.EndsWith(".hpp") || file.EndsWith(".h"))
                                {
                                    writer.WriteStartElement("ClInclude");
                                }
                                else
                                {
                                    writer.WriteStartElement("None");
                                }

                                writer.WriteAttributeString("Include", file);
                                writer.WriteEndElement();
                            }
                            writer.WriteEndElement();
                        }

                        // Add files from base folder
                        writer.WriteStartElement("ItemGroup");
                        foreach (string file in Directory.GetFiles(project.module.folderUrl))
                        {
                            writer.WriteStartElement("None");
                            writer.WriteAttributeString("Include", file);
                            writer.WriteEndElement();
                        }
                        writer.WriteEndElement();


                        writer.WriteStartElement("Import");
                        writer.WriteAttributeString("Project", "$(VCTargetsPath)\\Microsoft.Cpp.targets");
                        writer.WriteEndElement();

                        writer.WriteStartElement("ImportGroup");
                        writer.WriteAttributeString("Label", "ExtensionTargets");
                        writer.WriteEndElement();

                        writer.WriteEndElement();
                        writer.WriteEndDocument();

                        writer.Flush();
                        writer.Close();
                    }
            return(true);
        }