public void Merge(ConfigurationsPerBff other) { Debug.Assert(other.BffFilePath == BffFilePath); ProjectsWereFiltered = ProjectsWereFiltered && other.ProjectsWereFiltered; var merged = new HashSet <Solution.Configuration>(Configurations); foreach (Solution.Configuration conf in other) { merged.Add(conf); } ResolvedProjects.AddRange(other.ResolvedProjects); Configurations = merged.ToArray(); }
private static bool GenerateMasterBffFile(Builder builder, ConfigurationsPerBff configurationsPerBff) { string masterBffFilePath = Util.GetCapitalizedPath(configurationsPerBff.BffFilePathWithExtension); string masterBffDirectory = Path.GetDirectoryName(masterBffFilePath); string masterBffFileName = Path.GetFileName(masterBffFilePath); // Global configuration file is in the same directory as the master bff but filename suffix added to its filename. string globalConfigFullPath = GetGlobalBffConfigFileName(masterBffFilePath); string globalConfigFileName = Path.GetFileName(globalConfigFullPath); var solutionProjects = configurationsPerBff.ResolvedProjects; if (solutionProjects.Count == 0 && configurationsPerBff.ProjectsWereFiltered) { // We are running in filter mode for submit assistant and all projects were filtered out. // We need to skip generation and delete any existing master bff file. Util.TryDeleteFile(masterBffFilePath); return(false); } // Start writing Bff var fileGenerator = new FileGenerator(); var masterBffInfo = new MasterBffInfo(); var bffPreBuildSection = new Dictionary <string, string>(); var bffCustomPreBuildSection = new Dictionary <string, string>(); var bffMasterSection = new Dictionary <string, string>(); var masterBffCopySections = new List <string>(); var masterBffCustomSections = new UniqueList <string>(); // section that is not ordered bool mustGenerateFastbuild = false; var platformBffCache = new Dictionary <Platform, IPlatformBff>(); var verificationPostBuildCopies = new Dictionary <string, string>(); foreach (Solution.Configuration solutionConfiguration in configurationsPerBff) { foreach (var solutionProject in solutionProjects) { var project = solutionProject.Project; // Export projects do not have any bff if (project.GetType().IsDefined(typeof(Export), false)) { continue; } // When the project has a source file filter, only keep it if the file list is not empty if (project.SourceFilesFilters != null && (project.SourceFilesFiltersCount == 0 || project.SkipProjectWhenFiltersActive)) { continue; } Solution.Configuration.IncludedProjectInfo includedProject = solutionConfiguration.GetProject(solutionProject.Project.GetType()); bool perfectMatch = includedProject != null && solutionProject.Configurations.Contains(includedProject.Configuration); if (!perfectMatch) { continue; } var conf = includedProject.Configuration; if (!conf.IsFastBuildEnabledProjectConfig()) { continue; } mustGenerateFastbuild = true; IPlatformBff platformBff = platformBffCache.GetValueOrAdd(conf.Platform, PlatformRegistry.Query <IPlatformBff>(conf.Platform)); platformBff.AddCompilerSettings(masterBffInfo.CompilerSettings, conf); if (FastBuildSettings.WriteAllConfigsSection && includedProject.ToBuild == Solution.Configuration.IncludedProjectInfo.Build.Yes) { masterBffInfo.AllConfigsSections.Add(Bff.GetShortProjectName(project, conf)); } using (fileGenerator.Declare("conf", conf)) using (fileGenerator.Declare("target", conf.Target)) using (fileGenerator.Declare("project", conf.Project)) { var preBuildEvents = new Dictionary <string, Project.Configuration.BuildStepBase>(); if (conf.Output == Project.Configuration.OutputType.Exe || conf.ExecuteTargetCopy) { var copies = ProjectOptionsGenerator.ConvertPostBuildCopiesToRelative(conf, masterBffDirectory); foreach (var copy in copies) { var sourceFile = copy.Key; var sourceFileName = Path.GetFileName(sourceFile); var destinationFolder = copy.Value; var destinationFile = Path.Combine(destinationFolder, sourceFileName); // use the global root for alias computation, as the project has not idea in which master bff it has been included var destinationRelativeToGlobal = Util.GetConvertedRelativePath(masterBffDirectory, destinationFolder, conf.Project.RootPath, true, conf.Project.RootPath); { string key = sourceFileName + destinationRelativeToGlobal; string currentSourceFullPath = Util.PathGetAbsolute(masterBffDirectory, sourceFile); string previous; if (verificationPostBuildCopies.TryGetValue(key, out previous)) { if (previous != currentSourceFullPath) { builder.LogErrorLine("A post-build copy to the destination '{0}' already exist but from different sources: '{1}' and '{2}'!", Util.PathGetAbsolute(masterBffDirectory, destinationFolder), previous, currentSourceFullPath); } } else { verificationPostBuildCopies.Add(key, currentSourceFullPath); } } string fastBuildCopyAlias = UtilityMethods.GetFastBuildCopyAlias(sourceFileName, destinationRelativeToGlobal); { using (fileGenerator.Declare("fastBuildCopyAlias", fastBuildCopyAlias)) using (fileGenerator.Declare("fastBuildCopySource", sourceFile)) using (fileGenerator.Declare("fastBuildCopyDest", destinationFile)) { if (!bffMasterSection.ContainsKey(fastBuildCopyAlias)) { bffMasterSection.Add(fastBuildCopyAlias, fileGenerator.Resolver.Resolve(Bff.Template.ConfigurationFile.CopyFileSection)); } } } } foreach (var eventPair in conf.EventPreBuildExecute) { preBuildEvents.Add(eventPair.Key, eventPair.Value); } foreach (var buildEvent in conf.ResolvedEventPreBuildExe) { string eventKey = ProjectOptionsGenerator.MakeBuildStepName(conf, buildEvent, Vcxproj.BuildStep.PreBuild); preBuildEvents.Add(eventKey, buildEvent); } WriteEvents(fileGenerator, preBuildEvents, bffPreBuildSection, masterBffDirectory); } var customPreBuildEvents = new Dictionary <string, Project.Configuration.BuildStepBase>(); foreach (var eventPair in conf.EventCustomPrebuildExecute) { customPreBuildEvents.Add(eventPair.Key, eventPair.Value); } foreach (var buildEvent in conf.ResolvedEventCustomPreBuildExe) { string eventKey = ProjectOptionsGenerator.MakeBuildStepName(conf, buildEvent, Vcxproj.BuildStep.PreBuildCustomAction); customPreBuildEvents.Add(eventKey, buildEvent); } WriteEvents(fileGenerator, customPreBuildEvents, bffCustomPreBuildSection, masterBffDirectory); if (includedProject.ToBuild == Solution.Configuration.IncludedProjectInfo.Build.Yes) { MergeBffIncludeTreeRecursive(conf, ref masterBffInfo.BffIncludeToDependencyIncludes); } } } } if (!mustGenerateFastbuild) { throw new Error("Sharpmake-FastBuild : Trying to generate a MasterBff with none of its projects having a FastBuild configuration, or having a platform supporting it, or all of them having conf.DoNotGenerateFastBuild = true"); } masterBffCopySections.AddRange(bffMasterSection.Values); masterBffCopySections.AddRange(bffPreBuildSection.Values); masterBffCustomSections.AddRange(bffCustomPreBuildSection.Values); var result = new StringBuilder(); foreach (var projectBffFullPath in GetMasterIncludeList(masterBffInfo.BffIncludeToDependencyIncludes)) { string projectFullPath = Path.GetDirectoryName(projectBffFullPath); var projectPathRelativeFromMasterBff = Util.PathGetRelative(masterBffDirectory, projectFullPath, true); string bffKeyRelative = Path.Combine(Bff.CurrentBffPathKey, Path.GetFileName(projectBffFullPath)); string include = string.Join( Environment.NewLine, "{", $" {Bff.CurrentBffPathVariable} = \"{projectPathRelativeFromMasterBff}\"", $" #include \"{bffKeyRelative}\"", "}" ); result.AppendLine(include); } string fastBuildMasterBffDependencies = result.Length == 0 ? FileGeneratorUtilities.RemoveLineTag : result.ToString(); GenerateMasterBffGlobalSettingsFile(builder, globalConfigFullPath, masterBffInfo); using (fileGenerator.Declare("fastBuildProjectName", masterBffFileName)) using (fileGenerator.Declare("fastBuildGlobalConfigurationInclude", $"#include \"{globalConfigFileName}\"")) { fileGenerator.Write(Bff.Template.ConfigurationFile.HeaderFile); foreach (Platform platform in platformBffCache.Keys) // kind of cheating to use that cache instead of the masterBffInfo.CompilerSettings, but it works :) { using (fileGenerator.Declare("fastBuildDefine", Bff.GetPlatformSpecificDefine(platform))) fileGenerator.Write(Bff.Template.ConfigurationFile.Define); } fileGenerator.Write(Bff.Template.ConfigurationFile.GlobalConfigurationInclude); } WriteMasterCopySection(fileGenerator, masterBffCopySections); WriteMasterCustomSection(fileGenerator, masterBffCustomSections); using (fileGenerator.Declare("fastBuildProjectName", masterBffFileName)) using (fileGenerator.Declare("fastBuildOrderedBffDependencies", fastBuildMasterBffDependencies)) { fileGenerator.Write(Bff.Template.ConfigurationFile.Includes); } if (masterBffInfo.AllConfigsSections.Count != 0) { using (fileGenerator.Declare("fastBuildConfigs", UtilityMethods.FBuildFormatList(masterBffInfo.AllConfigsSections, 4))) { fileGenerator.Write(Bff.Template.ConfigurationFile.AllConfigsSection); } } // remove all line that contain RemoveLineTag fileGenerator.RemoveTaggedLines(); MemoryStream bffCleanMemoryStream = fileGenerator.ToMemoryStream(); // Write master .bff file FileInfo bffFileInfo = new FileInfo(masterBffFilePath); bool updated = builder.Context.WriteGeneratedFile(null, bffFileInfo, bffCleanMemoryStream); foreach (var confsPerSolution in configurationsPerBff) { confsPerSolution.Solution.PostGenerationCallback?.Invoke(masterBffDirectory, Path.GetFileNameWithoutExtension(masterBffFileName), FastBuildSettings.FastBuildConfigFileExtension); } return(updated); }
private void GenerateIncludeBffFileForSolution(Builder builder, string bffFilePath, ConfigurationsPerBff confsPerBff, IList <string> generatedFiles, IList <string> skippedFiles) { var bffFileInfo = new FileInfo(bffFilePath); var fileGenerator = new FileGenerator(); using (fileGenerator.Declare("solutionFileName", Path.GetFileNameWithoutExtension(bffFileInfo.Name))) using (fileGenerator.Declare("masterBffFilePath", Util.PathGetRelative(bffFileInfo.DirectoryName, confsPerBff.BffFilePathWithExtension))) fileGenerator.Write(Bff.Template.ConfigurationFile.IncludeMasterBff); using (var bffFileStream = fileGenerator.ToMemoryStream()) { if (builder.Context.WriteGeneratedFile(null, bffFileInfo, bffFileStream)) { generatedFiles.Add(bffFilePath); } else { skippedFiles.Add(bffFilePath); } } }
public void Generate( Builder builder, Solution solution, List <Solution.Configuration> solutionConfigurations, string solutionFile, List <string> generatedFiles, List <string> skipFiles ) { if (!FastBuildSettings.FastBuildSupportEnabled) { return; } // // In every case, we need a BFF with the name of the generated solution to start a // build from Visual Studio with the generated projects. If the name of the BFF to // generate for that solution happens to be the same as the solution's name, we // generate everything in that file. Otherwise, we generate the content of the BFF in // the appropriate file and we generate an *additional* BFF with the same name as the // solution which simply includes the "real" bff. // // The reason we have to do it like that is to enable building a specific project in // Visual Studio. A project can be included in several solutions, and can be built // differently depending on the solution. This means we can't generate a specific // master BFF name in the project's make command because which BFF is needed depends on // the solution, not the project. So instead, when VS builds a project, we use // $(SolutionName).bff as the BFF make command. // // So, if you want a shared master BFF instead of doing it per-solution or change the // name, that's great, but we *need* $(SolutionName).bff for things to work in Visual // Studio, even if all it does is include the real BFF. // IEnumerable <ConfigurationsPerBff> confsPerBffs = ConfigurationsPerBff.Create(solution, solutionConfigurations).ToArray(); foreach (ConfigurationsPerBff confsPerBff in confsPerBffs) { if (confsPerBff.Configurations.Any(conf => conf.SolutionFilePath != conf.MasterBffFilePath)) { string bffIncludeFilePath = solutionFile + FastBuildSettings.FastBuildConfigFileExtension; if (!Util.PathIsSame(bffIncludeFilePath, confsPerBff.BffFilePathWithExtension)) { GenerateIncludeBffFileForSolution(builder, bffIncludeFilePath, confsPerBff, generatedFiles, skipFiles); } // First collect all solutions and sort them by master BFF, then once we have all of // them, the post-generation event handler will actually generate the BFF. lock (s_confsPerSolutions) { if (!s_postGenerationHandlerInitialized) { builder.EventPostGeneration += (projects, solutions) => { GenerateMasterBffFiles(builder, s_confsPerSolutions.Values); }; s_postGenerationHandlerInitialized = true; } ConfigurationsPerBff other; if (s_confsPerSolutions.TryGetValue(confsPerBff.BffFilePath, out other)) { other.Merge(confsPerBff); } else { s_confsPerSolutions.Add(confsPerBff.BffFilePath, confsPerBff); } } } else { GenerateMasterBffFiles(builder, new[] { confsPerBff }); } } }