/Microsoft.Build/Microsoft.Build/Microsoft/Build/Execution/BuildManager.cs
C# | 1029 lines | 976 code | 53 blank | 0 comment | 184 complexity | c1b9151988daebd786038fddc68b68d0 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-3.0
Large files files are truncated, but you can click here to view the full file
- namespace Microsoft.Build.Execution
- {
- using Microsoft.Build;
- using Microsoft.Build.BackEnd;
- using Microsoft.Build.BackEnd.Logging;
- using Microsoft.Build.Evaluation;
- using Microsoft.Build.Exceptions;
- using Microsoft.Build.Framework;
- using Microsoft.Build.Logging;
- using Microsoft.Build.Shared;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Runtime;
- using System.Threading;
-
- public class BuildManager : INodePacketHandler, IBuildComponentHost
- {
- private bool acquiredProjectRootElementCacheFromProjectInstance;
- private HashSet<NGen<int>> activeNodes;
- private BuildManagerState buildManagerState;
- private BuildParameters buildParameters;
- private Dictionary<int, BuildSubmission> buildSubmissions;
- private BuildComponentFactoryCollection componentFactories;
- private IConfigCache configCache;
- private string hostName;
- private LegacyThreadingData legacyThreadingData;
- private LoggingExceptionDelegate loggingThreadExceptionEventHandler;
- private static int nextBuildId;
- private int nextBuildRequestConfigurationId;
- private int nextBuildSubmissionId;
- private int nextUnnamedProjectId;
- private AutoResetEvent noActiveSubmissionsEvent;
- private NodeConfiguration nodeConfiguration;
- private Dictionary<NGen<int>, HashSet<NGen<int>>> nodeIdToKnownConfigurations;
- private INodeManager nodeManager;
- private AutoResetEvent noNodesActiveEvent;
- private bool overallBuildSuccess;
- private ProjectFinishedEventHandler projectFinishedEventHandler;
- private ProjectStartedEventHandler projectStartedEventHandler;
- private Dictionary<int, BuildEventArgs> projectStartedEvents;
- private IResultsCache resultsCache;
- private IScheduler scheduler;
- private bool shuttingDown;
- private static BuildManager singletonInstance;
- private object syncLock;
- private Exception threadException;
- private Dictionary<ProjectInstance, string> unnamedProjectInstanceToNames;
-
- [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
- public BuildManager() : this("Unnamed")
- {
- }
-
- public BuildManager(string hostName)
- {
- this.syncLock = new object();
- ErrorUtilities.VerifyThrowArgumentNull(hostName, "hostName");
- this.hostName = hostName;
- this.buildManagerState = BuildManagerState.Idle;
- this.buildSubmissions = new Dictionary<int, BuildSubmission>();
- this.noActiveSubmissionsEvent = new AutoResetEvent(true);
- this.activeNodes = new HashSet<NGen<int>>();
- this.noNodesActiveEvent = new AutoResetEvent(true);
- this.nodeIdToKnownConfigurations = new Dictionary<NGen<int>, HashSet<NGen<int>>>();
- this.unnamedProjectInstanceToNames = new Dictionary<ProjectInstance, string>();
- this.nextUnnamedProjectId = 1;
- this.componentFactories = new BuildComponentFactoryCollection(this);
- this.componentFactories.RegisterDefaultFactories();
- this.projectStartedEvents = new Dictionary<int, BuildEventArgs>();
- this.projectStartedEventHandler = new ProjectStartedEventHandler(this.OnProjectStarted);
- this.projectFinishedEventHandler = new ProjectFinishedEventHandler(this.OnProjectFinished);
- this.loggingThreadExceptionEventHandler = new LoggingExceptionDelegate(this.OnThreadException);
- this.legacyThreadingData = new LegacyThreadingData();
- this.nextBuildRequestConfigurationId = 0;
- }
-
- public void BeginBuild(BuildParameters parameters)
- {
- lock (this.syncLock)
- {
- this.RequireState(BuildManagerState.Idle, "BuildInProgress");
- if (Environment.GetEnvironmentVariable("MSBUILDDUMPOPPORTUNISTICINTERNSTATS") == "1")
- {
- OpportunisticIntern.EnableStatisticsGathering();
- }
- this.overallBuildSuccess = true;
- if (parameters != null)
- {
- this.buildParameters = parameters.Clone();
- }
- else
- {
- this.buildParameters = new BuildParameters();
- }
- if (this.buildParameters.ResetCaches || ((this.configCache != null) && this.configCache.IsConfigCacheSizeLargerThanThreshold()))
- {
- this.ResetCaches();
- }
- this.buildParameters.BuildId = GetNextBuildId();
- this.nodeManager = ((IBuildComponentHost) this).GetComponent(BuildComponentType.NodeManager) as INodeManager;
- this.scheduler = ((IBuildComponentHost) this).GetComponent(BuildComponentType.Scheduler) as IScheduler;
- this.configCache = ((IBuildComponentHost) this).GetComponent(BuildComponentType.ConfigCache) as IConfigCache;
- this.resultsCache = ((IBuildComponentHost) this).GetComponent(BuildComponentType.ResultsCache) as IResultsCache;
- this.nodeManager.RegisterPacketHandler(NodePacketType.BuildRequestBlocker, new NodePacketFactoryMethod(BuildRequestBlocker.FactoryForDeserialization), this);
- this.nodeManager.RegisterPacketHandler(NodePacketType.BuildRequestConfiguration, new NodePacketFactoryMethod(BuildRequestConfiguration.FactoryForDeserialization), this);
- this.nodeManager.RegisterPacketHandler(NodePacketType.BuildRequestConfigurationResponse, new NodePacketFactoryMethod(BuildRequestConfigurationResponse.FactoryForDeserialization), this);
- this.nodeManager.RegisterPacketHandler(NodePacketType.BuildResult, new NodePacketFactoryMethod(BuildResult.FactoryForDeserialization), this);
- this.nodeManager.RegisterPacketHandler(NodePacketType.NodeShutdown, new NodePacketFactoryMethod(NodeShutdown.FactoryForDeserialization), this);
- ILoggingService loggingService = this.CreateLoggingService(this.buildParameters.Loggers, this.buildParameters.ForwardingLoggers);
- this.componentFactories.ReplaceFactory(BuildComponentType.LoggingService, loggingService as IBuildComponent);
- this.nodeManager.RegisterPacketHandler(NodePacketType.LogMessage, new NodePacketFactoryMethod(LogMessagePacket.FactoryForDeserialization), loggingService as INodePacketHandler);
- try
- {
- loggingService.LogBuildStarted();
- }
- catch (Exception)
- {
- this.ShutdownLoggingService(loggingService);
- throw;
- }
- if (this.threadException != null)
- {
- this.ShutdownLoggingService(loggingService);
- throw this.threadException;
- }
- this.buildManagerState = BuildManagerState.Building;
- this.noActiveSubmissionsEvent.Set();
- this.noNodesActiveEvent.Set();
- }
- }
-
- public BuildResult Build(BuildParameters parameters, BuildRequestData requestData)
- {
- BuildResult result;
- this.BeginBuild(parameters);
- try
- {
- result = this.BuildRequest(requestData);
- if ((result.Exception == null) && (this.threadException != null))
- {
- result.Exception = this.threadException;
- this.threadException = null;
- }
- }
- finally
- {
- this.EndBuild();
- }
- return result;
- }
-
- public BuildResult BuildRequest(BuildRequestData requestData)
- {
- return this.PendBuildRequest(requestData).Execute();
- }
-
- public void CancelAllSubmissions()
- {
- ThreadPool.QueueUserWorkItem(delegate (object state) {
- lock (this.syncLock)
- {
- if (!this.shuttingDown && (this.buildManagerState == BuildManagerState.Building))
- {
- this.overallBuildSuccess = false;
- foreach (BuildSubmission submission in this.buildSubmissions.Values)
- {
- if (submission.BuildRequest != null)
- {
- BuildResult result = new BuildResult(submission.BuildRequest, new BuildAbortedException());
- this.resultsCache.AddResult(result);
- submission.CompleteResults(result);
- }
- }
- this.ShutdownNodesAsync(true);
- }
- }
- });
- }
-
- private void CheckSubmissionCompletenessAndRemove(BuildSubmission submission)
- {
- lock (this.syncLock)
- {
- if (submission.IsCompleted || (submission.BuildRequest == null))
- {
- this.buildSubmissions.Remove(submission.SubmissionId);
- }
- if (this.buildSubmissions.Count == 0)
- {
- this.noActiveSubmissionsEvent.Set();
- }
- }
- }
-
- private ILoggingService CreateLoggingService(IEnumerable<ILogger> loggers, IEnumerable<ForwardingLoggerRecord> forwardingLoggers)
- {
- LoggerMode mode = ((this.buildParameters.MaxNodeCount == 1) && this.buildParameters.UseSynchronousLogging) ? LoggerMode.Synchronous : LoggerMode.Asynchronous;
- ILoggingService loggingService = LoggingService.CreateLoggingService(mode, 1);
- ((IBuildComponent) loggingService).InitializeComponent(this);
- this.threadException = null;
- loggingService.OnLoggingThreadException += this.loggingThreadExceptionEventHandler;
- loggingService.OnProjectStarted += this.projectStartedEventHandler;
- loggingService.OnProjectFinished += this.projectFinishedEventHandler;
- try
- {
- if (loggers != null)
- {
- foreach (ILogger logger in loggers)
- {
- loggingService.RegisterLogger(logger);
- }
- }
- if (loggingService.Loggers.Count == 0)
- {
- loggingService.RegisterLogger(new NullLogger());
- }
- if (forwardingLoggers == null)
- {
- return loggingService;
- }
- foreach (ForwardingLoggerRecord record in forwardingLoggers)
- {
- loggingService.RegisterDistributedLogger(record.CentralLogger, record.ForwardingLoggerDescription);
- }
- }
- catch (Exception)
- {
- if (loggingService != null)
- {
- this.ShutdownLoggingService(loggingService);
- }
- throw;
- }
- return loggingService;
- }
-
- public void EndBuild()
- {
- lock (this.syncLock)
- {
- this.ErrorIfState(BuildManagerState.WaitingForBuildToComplete, "WaitingForEndOfBuild");
- this.ErrorIfState(BuildManagerState.Idle, "NoBuildInProgress");
- this.VerifyStateInternal(BuildManagerState.Building);
- List<BuildSubmission> list = new List<BuildSubmission>(this.buildSubmissions.Values);
- foreach (BuildSubmission submission in list)
- {
- this.CheckSubmissionCompletenessAndRemove(submission);
- }
- this.buildManagerState = BuildManagerState.WaitingForBuildToComplete;
- }
- ILoggingService loggingService = ((IBuildComponentHost) this).LoggingService;
- try
- {
- this.noActiveSubmissionsEvent.WaitOne();
- this.ShutdownNodesAsync(false);
- this.noNodesActiveEvent.WaitOne();
- ErrorUtilities.VerifyThrow(this.buildSubmissions.Count == 0, "All submissions not yet complete.");
- ErrorUtilities.VerifyThrow(this.activeNodes.Count == 0, "All nodes not yet shut down.");
- if (loggingService != null)
- {
- loggingService.LogBuildFinished(this.overallBuildSuccess);
- }
- }
- finally
- {
- this.ShutdownLoggingService(loggingService);
- if (this.buildParameters.LegacyThreadingSemantics)
- {
- this.legacyThreadingData.MainThreadSubmissionId = -1;
- }
- this.Reset();
- this.buildManagerState = BuildManagerState.Idle;
- if (this.threadException != null)
- {
- throw this.threadException;
- }
- if (Environment.GetEnvironmentVariable("MSBUILDDUMPOPPORTUNISTICINTERNSTATS") == "1")
- {
- OpportunisticIntern.ReportStatistics();
- }
- }
- }
-
- private void ErrorIfState(BuildManagerState disallowedState, string exceptionResouorce)
- {
- if (this.buildManagerState == disallowedState)
- {
- ErrorUtilities.ThrowInvalidOperation(exceptionResouorce, new object[0]);
- }
- }
-
- internal void ExecuteSubmission(BuildSubmission submission, bool allowMainThreadBuild)
- {
- ErrorUtilities.VerifyThrowArgumentNull(submission, "submission");
- ErrorUtilities.VerifyThrow(!submission.IsCompleted, "Submission already complete.");
- ProjectInstance projectInstance = submission.BuildRequestData.ProjectInstance;
- if (projectInstance != null)
- {
- if (this.acquiredProjectRootElementCacheFromProjectInstance)
- {
- ErrorUtilities.VerifyThrowArgument(this.buildParameters.ProjectRootElementCache == projectInstance.ProjectRootElementCache, "OM_BuildSubmissionsMultipleProjectCollections");
- }
- else
- {
- this.buildParameters.ProjectRootElementCache = projectInstance.ProjectRootElementCache;
- this.acquiredProjectRootElementCacheFromProjectInstance = true;
- }
- }
- else if (this.buildParameters.ProjectRootElementCache == null)
- {
- this.buildParameters.ProjectRootElementCache = new ProjectRootElementCache(false);
- }
- this.VerifyStateInternal(BuildManagerState.Building);
- try
- {
- BuildRequestBlocker blocker;
- if (string.IsNullOrEmpty(submission.BuildRequestData.ProjectFullPath))
- {
- string str;
- ErrorUtilities.VerifyThrow(submission.BuildRequestData.ProjectInstance != null, "Unexpected null path for a submission with no ProjectInstance.");
- if (!this.unnamedProjectInstanceToNames.TryGetValue(submission.BuildRequestData.ProjectInstance, out str))
- {
- str = "Unnamed_" + this.nextUnnamedProjectId++;
- this.unnamedProjectInstanceToNames[submission.BuildRequestData.ProjectInstance] = str;
- }
- submission.BuildRequestData.ProjectFullPath = Path.Combine(submission.BuildRequestData.ProjectInstance.GetProperty("MSBuildProjectDirectory").EvaluatedValue, str);
- }
- BuildRequestConfiguration config = new BuildRequestConfiguration(submission.BuildRequestData, this.buildParameters.DefaultToolsVersion);
- BuildRequestConfiguration matchingConfiguration = this.configCache.GetMatchingConfiguration(config);
- if (matchingConfiguration != null)
- {
- lock (this.syncLock)
- {
- ErrorUtilities.VerifyThrowInvalidOperation(!this.scheduler.IsCurrentlyBuildingConfiguration(matchingConfiguration.ConfigurationId), "ProjectAlreadyBuilding");
- foreach (BuildSubmission submission2 in this.buildSubmissions.Values)
- {
- ErrorUtilities.VerifyThrowInvalidOperation((submission2.BuildRequest == null) || (submission2.BuildRequest.ConfigurationId != matchingConfiguration.ConfigurationId), "ProjectAlreadyBuilding");
- }
- }
- }
- BuildRequestConfiguration configuration3 = this.ResolveConfiguration(config, matchingConfiguration, (submission.BuildRequestData.Flags & BuildRequestDataFlags.ReplaceExistingProjectInstance) == BuildRequestDataFlags.ReplaceExistingProjectInstance);
- submission.BuildRequest = new Microsoft.Build.BackEnd.BuildRequest(submission.SubmissionId, 0, configuration3.ConfigurationId, submission.BuildRequestData.TargetNames, submission.BuildRequestData.HostServices, BuildEventContext.Invalid, null);
- if (this.shuttingDown)
- {
- BuildResult result = new BuildResult(submission.BuildRequest, new BuildAbortedException());
- submission.CompleteResults(result);
- submission.CompleteLogging(true);
- this.CheckSubmissionCompletenessAndRemove(submission);
- }
- else
- {
- blocker = new BuildRequestBlocker(-1, new string[0], new Microsoft.Build.BackEnd.BuildRequest[] { submission.BuildRequest });
- ThreadPool.QueueUserWorkItem(delegate (object context) {
- try
- {
- this.IssueRequestToScheduler(submission, allowMainThreadBuild, blocker);
- }
- catch (Exception exception)
- {
- this.HandleExecuteSubmissionException(submission, exception);
- }
- });
- }
- }
- catch (Exception exception)
- {
- this.HandleExecuteSubmissionException(submission, exception);
- throw;
- }
- }
-
- private I ExpectPacketType<I>(INodePacket packet, NodePacketType expectedType) where I: class, INodePacket
- {
- I local = packet as I;
- ErrorUtilities.VerifyThrow(local != null, "Incorrect packet type: {0} should have been {1}", packet.Type, expectedType);
- return local;
- }
-
- private int GetNewConfigurationId()
- {
- if (this.scheduler != null)
- {
- return (this.scheduler.MinimumAssignableConfigurationId + this.nextBuildRequestConfigurationId++);
- }
- return ++this.nextBuildRequestConfigurationId;
- }
-
- private static int GetNextBuildId()
- {
- return Interlocked.Increment(ref nextBuildId);
- }
-
- private int GetNextSubmissionId()
- {
- return this.nextBuildSubmissionId++;
- }
-
- private NodeConfiguration GetNodeConfiguration()
- {
- if (this.nodeConfiguration == null)
- {
- ILoggingService component = ((IBuildComponentHost) this).GetComponent(BuildComponentType.LoggingService) as ILoggingService;
- List<LoggerDescription> list = new List<LoggerDescription>(component.LoggerDescriptions);
- this.nodeConfiguration = new NodeConfiguration(-1, this.buildParameters, list.ToArray(), AppDomain.CurrentDomain.SetupInformation);
- }
- return this.nodeConfiguration;
- }
-
- public ProjectInstance GetProjectInstanceForBuild(Project project)
- {
- this.configCache = ((IBuildComponentHost) this).GetComponent(BuildComponentType.ConfigCache) as IConfigCache;
- BuildRequestConfiguration matchingConfiguration = this.configCache.GetMatchingConfiguration(new ConfigurationMetadata(project));
- if ((matchingConfiguration != null) && matchingConfiguration.IsLoaded)
- {
- matchingConfiguration.RetrieveFromCache();
- return matchingConfiguration.Project;
- }
- ProjectInstance projectInstance = project.CreateProjectInstance();
- matchingConfiguration = new BuildRequestConfiguration(this.GetNewConfigurationId(), new BuildRequestData(projectInstance, new string[0]), null);
- this.configCache.AddConfiguration(matchingConfiguration);
- return projectInstance;
- }
-
- private void HandleConfigurationRequest(int node, BuildRequestConfiguration unresolvedConfiguration)
- {
- BuildRequestConfiguration configuration = this.ResolveConfiguration(unresolvedConfiguration, null, false);
- BuildRequestConfigurationResponse packet = new BuildRequestConfigurationResponse(unresolvedConfiguration.ConfigurationId, configuration.ConfigurationId);
- HashSet<NGen<int>> set = null;
- if (!this.nodeIdToKnownConfigurations.TryGetValue(node, out set))
- {
- set = new HashSet<NGen<int>>();
- this.nodeIdToKnownConfigurations[node] = set;
- }
- set.Add(configuration.ConfigurationId);
- this.nodeManager.SendData(node, packet);
- }
-
- private void HandleExecuteSubmissionException(BuildSubmission submission, Exception ex)
- {
- InvalidProjectFileException invalidProjectFileException = ex as InvalidProjectFileException;
- if ((invalidProjectFileException != null) && !invalidProjectFileException.HasBeenLogged)
- {
- BuildEventContext buildEventContext = new BuildEventContext(submission.SubmissionId, 1, -1, -2, -1, -1);
- ((IBuildComponentHost) this).LoggingService.LogInvalidProjectFileError(buildEventContext, invalidProjectFileException);
- invalidProjectFileException.HasBeenLogged = true;
- }
- if (submission.BuildRequest != null)
- {
- BuildResult result = new BuildResult(submission.BuildRequest, ex);
- submission.CompleteResults(result);
- submission.CompleteLogging(true);
- }
- this.overallBuildSuccess = false;
- this.CheckSubmissionCompletenessAndRemove(submission);
- }
-
- private void HandleNewRequest(int node, BuildRequestBlocker blocker)
- {
- if (blocker.BuildRequests != null)
- {
- foreach (Microsoft.Build.BackEnd.BuildRequest request in blocker.BuildRequests)
- {
- BuildRequestConfiguration config = this.configCache[request.ConfigurationId];
- if (FileUtilities.IsSolutionFilename(config.ProjectFullPath))
- {
- try
- {
- this.LoadSolutionIntoConfiguration(config, request.BuildEventContext);
- }
- catch (InvalidProjectFileException exception)
- {
- this.resultsCache.AddResult(new BuildResult(request, exception));
- if (node == 0)
- {
- throw;
- }
- }
- }
- }
- }
- IEnumerable<ScheduleResponse> responses = this.scheduler.ReportRequestBlocked(node, blocker);
- this.PerformSchedulingActions(responses);
- }
-
- private void HandleNodeShutdown(int node, NodeShutdown shutdownPacket)
- {
- this.shuttingDown = true;
- ErrorUtilities.VerifyThrow(this.activeNodes.Contains(node), "Unexpected shutdown from node {0} which shouldn't exist.", node);
- this.activeNodes.Remove(node);
- if (shutdownPacket.Reason != NodeShutdownReason.Requested)
- {
- if (shutdownPacket.Reason == NodeShutdownReason.ConnectionFailed)
- {
- ILoggingService component = ((IBuildComponentHost) this).GetComponent(BuildComponentType.LoggingService) as ILoggingService;
- foreach (BuildSubmission submission in this.buildSubmissions.Values)
- {
- BuildEventContext buildEventContext = new BuildEventContext(submission.SubmissionId, -2, -1, -2, -1, -1);
- component.LogError(buildEventContext, new BuildEventFileInfo(string.Empty), "ChildExitedPrematurely", new object[] { node });
- }
- }
- else if (((shutdownPacket.Reason == NodeShutdownReason.Error) && (this.buildSubmissions.Values.Count == 0)) && (shutdownPacket.Exception != null))
- {
- (((IBuildComponentHost) this).GetComponent(BuildComponentType.LoggingService) as ILoggingService).LogError(BuildEventContext.Invalid, new BuildEventFileInfo(string.Empty), "ChildExitedPrematurely", new object[] { shutdownPacket.Exception.ToString() });
- this.OnThreadException(shutdownPacket.Exception);
- }
- this.nodeManager.ShutdownNodes(this.buildParameters.EnableNodeReuse);
- foreach (BuildSubmission submission2 in this.buildSubmissions.Values)
- {
- if (submission2.BuildRequest != null)
- {
- this.resultsCache.AddResult(new BuildResult(submission2.BuildRequest, shutdownPacket.Exception ?? new BuildAbortedException()));
- }
- }
- this.scheduler.ReportBuildAborted(node);
- }
- List<BuildSubmission> list = new List<BuildSubmission>(this.buildSubmissions.Values);
- foreach (BuildSubmission submission3 in list)
- {
- if (submission3.BuildRequest != null)
- {
- BuildResult resultsForConfiguration = this.resultsCache.GetResultsForConfiguration(submission3.BuildRequest.ConfigurationId);
- if (resultsForConfiguration == null)
- {
- resultsForConfiguration = new BuildResult(submission3.BuildRequest, new BuildAbortedException());
- }
- submission3.CompleteResults(resultsForConfiguration);
- submission3.CompleteLogging(true);
- this.overallBuildSuccess = this.overallBuildSuccess && (submission3.BuildResult.OverallResult == BuildResultCode.Success);
- this.CheckSubmissionCompletenessAndRemove(submission3);
- }
- }
- if (this.activeNodes.Count == 0)
- {
- this.noNodesActiveEvent.Set();
- }
- }
-
- private void HandleResult(int node, BuildResult result)
- {
- BuildRequestConfiguration configuration = this.configCache[result.ConfigurationId];
- if (result.DefaultTargets != null)
- {
- if (configuration.ProjectDefaultTargets == null)
- {
- configuration.ProjectDefaultTargets = result.DefaultTargets;
- }
- if (configuration.ProjectInitialTargets == null)
- {
- configuration.ProjectInitialTargets = result.InitialTargets;
- }
- }
- IEnumerable<ScheduleResponse> responses = this.scheduler.ReportResult(node, result);
- this.PerformSchedulingActions(responses);
- }
-
- private void IssueRequestToScheduler(BuildSubmission submission, bool allowMainThreadBuild, BuildRequestBlocker blocker)
- {
- bool flag = false;
- try
- {
- lock (this.syncLock)
- {
- if (this.shuttingDown)
- {
- throw new BuildAbortedException();
- }
- if ((allowMainThreadBuild && this.buildParameters.LegacyThreadingSemantics) && (this.legacyThreadingData.MainThreadSubmissionId == -1))
- {
- flag = true;
- this.legacyThreadingData.MainThreadSubmissionId = submission.SubmissionId;
- }
- this.HandleNewRequest(0, blocker);
- }
- }
- catch (Exception exception)
- {
- InvalidProjectFileException invalidProjectFileException = exception as InvalidProjectFileException;
- if (invalidProjectFileException != null)
- {
- if (!invalidProjectFileException.HasBeenLogged)
- {
- BuildEventContext buildEventContext = new BuildEventContext(submission.SubmissionId, 1, -1, -2, -1, -1);
- ((IBuildComponentHost) this).LoggingService.LogInvalidProjectFileError(buildEventContext, invalidProjectFileException);
- invalidProjectFileException.HasBeenLogged = true;
- }
- }
- else if (!(exception is BuildAbortedException) && ExceptionHandling.NotExpectedException(exception))
- {
- throw;
- }
- if (flag)
- {
- this.legacyThreadingData.MainThreadSubmissionId = -1;
- }
- if (invalidProjectFileException == null)
- {
- BuildEventContext context2 = new BuildEventContext(submission.SubmissionId, 1, -1, -2, -1, -1);
- ((IBuildComponentHost) this).LoggingService.LogFatalBuildError(context2, exception, new BuildEventFileInfo(submission.BuildRequestData.ProjectFullPath));
- }
- submission.CompleteLogging(true);
- this.ReportResultsToSubmission(new BuildResult(submission.BuildRequest, exception));
- this.overallBuildSuccess = false;
- }
- }
-
- internal void LoadSolutionIntoConfiguration(BuildRequestConfiguration config, BuildEventContext buildEventContext)
- {
- if (!config.IsLoaded)
- {
- ErrorUtilities.VerifyThrow(FileUtilities.IsSolutionFilename(config.ProjectFullPath), "{0} is not a solution", config.ProjectFullPath);
- ProjectInstance[] instanceArray = ProjectInstance.LoadSolutionForBuild(config.ProjectFullPath, config.Properties, config.ExplicitToolsVersionSpecified ? config.ToolsVersion : null, this.buildParameters, ((IBuildComponentHost) this).LoggingService, buildEventContext);
- config.Project = instanceArray[0];
- for (int i = 1; i < instanceArray.Length; i++)
- {
- BuildRequestConfiguration configuration = new BuildRequestConfiguration(this.GetNewConfigurationId(), instanceArray[i]);
- if (this.configCache.GetMatchingConfiguration(configuration) == null)
- {
- this.configCache.AddConfiguration(configuration);
- }
- }
- }
- }
-
- IBuildComponent IBuildComponentHost.GetComponent(BuildComponentType type)
- {
- return this.componentFactories.GetComponent(type);
- }
-
- void IBuildComponentHost.RegisterFactory(BuildComponentType componentType, BuildComponentFactoryDelegate factory)
- {
- this.componentFactories.ReplaceFactory(componentType, factory);
- }
-
- void INodePacketHandler.PacketReceived(int node, INodePacket packet)
- {
- lock (this.syncLock)
- {
- if (!this.shuttingDown || (packet.Type == NodePacketType.NodeShutdown))
- {
- switch (packet.Type)
- {
- case NodePacketType.BuildResult:
- {
- BuildResult result = this.ExpectPacketType<BuildResult>(packet, NodePacketType.BuildResult);
- this.HandleResult(node, result);
- break;
- }
- case NodePacketType.NodeShutdown:
- {
- NodeShutdown shutdownPacket = this.ExpectPacketType<NodeShutdown>(packet, NodePacketType.NodeShutdown);
- this.HandleNodeShutdown(node, shutdownPacket);
- break;
- }
- case NodePacketType.BuildRequestConfiguration:
- {
- BuildRequestConfiguration unresolvedConfiguration = this.ExpectPacketType<BuildRequestConfiguration>(packet, NodePacketType.BuildRequestConfiguration);
- this.HandleConfigurationRequest(node, unresolvedConfiguration);
- break;
- }
- case NodePacketType.BuildRequestBlocker:
- {
- BuildRequestBlocker blocker = this.ExpectPacketType<BuildRequestBlocker>(packet, NodePacketType.BuildRequestBlocker);
- this.HandleNewRequest(node, blocker);
- break;
- }
- default:
- ErrorUtilities.ThrowInternalError("Unexpected packet received by BuildManager: {0}", new object[] { packet.Type });
- break;
- }
- }
- }
- }
-
- private void OnProjectFinished(object sender, ProjectFinishedEventArgs e)
- {
- if (!this.shuttingDown)
- {
- lock (this.syncLock)
- {
- BuildEventArgs args;
- if (this.projectStartedEvents.TryGetValue(e.BuildEventContext.SubmissionId, out args) && args.BuildEventContext.Equals(e.BuildEventContext))
- {
- BuildSubmission submission;
- this.projectStartedEvents.Remove(e.BuildEventContext.SubmissionId);
- if (this.buildSubmissions.TryGetValue(e.BuildEventContext.SubmissionId, out submission))
- {
- submission.CompleteLogging(false);
- this.CheckSubmissionCompletenessAndRemove(submission);
- }
- }
- }
- }
- }
-
- private void OnProjectStarted(object sender, ProjectStartedEventArgs e)
- {
- if (!this.projectStartedEvents.ContainsKey(e.BuildEventContext.SubmissionId))
- {
- this.projectStartedEvents[e.BuildEventContext.SubmissionId] = e;
- }
- }
-
- private void OnThreadException(Exception e)
- {
- lock (this.syncLock)
- {
- if (this.threadException == null)
- {
- this.threadException = e;
- List<BuildSubmission> list = new List<BuildSubmission>(this.buildSubmissions.Values);
- foreach (BuildSubmission submission in list)
- {
- if (submission.BuildRequest == null)
- {
- continue;
- }
- submission.CompleteLogging(false);
- if ((submission.BuildResult == null) || (submission.BuildResult.Exception == null))
- {
- if (submission.BuildResult == null)
- {
- submission.BuildResult = new BuildResult(submission.BuildRequest, e);
- }
- else
- {
- submission.BuildResult.Exception = this.threadException;
- }
- }
- this.CheckSubmissionCompletenessAndRemove(submission);
- }
- }
- }
- }
-
- public BuildSubmission PendBuildRequest(BuildRequestData requestData)
- {
- lock (this.syncLock)
- {
- ErrorUtilities.VerifyThrowArgumentNull(requestData, "requestData");
- this.ErrorIfState(BuildManagerState.WaitingForBuildToComplete, "WaitingForEndOfBuild");
- this.ErrorIfState(BuildManagerState.Idle, "NoBuildInProgress");
- this.VerifyStateInternal(BuildManagerState.Building);
- BuildSubmission submission = new BuildSubmission(this, this.GetNextSubmissionId(), requestData, this.buildParameters.LegacyThreadingSemantics);
- this.buildSubmissions.Add(submission.SubmissionId, submission);
- this.noActiveSubmissionsEvent.Reset();
- return submission;
- }
- }
-
- private void PerformSchedulingActions(IEnumerable<ScheduleResponse> responses)
- {
- foreach (ScheduleResponse response in responses)
- {
- List<NodeInfo> list;
- int num;
- NodeInfo info;
- switch (response.Action)
- {
- case ScheduleActionType.NoAction:
- {
- continue;
- }
- case ScheduleActionType.Schedule:
- case ScheduleActionType.ScheduleWithConfiguration:
- {
- if (response.Action == ScheduleActionType.ScheduleWithConfiguration)
- {
- HashSet<NGen<int>> set = null;
- if (!this.nodeIdToKnownConfigurations.TryGetValue(response.NodeId, out set) || !set.Contains(response.BuildRequest.ConfigurationId))
- {
- IConfigCache component = this.componentFactories.GetComponent(BuildComponentType.ConfigCache) as IConfigCache;
- this.nodeManager.SendData(response.NodeId, component[response.BuildRequest.ConfigurationId]);
- }
- }
- this.nodeManager.SendData(response.NodeId, response.BuildRequest);
- continue;
- }
- case ScheduleActionType.ReportResults:
- case ScheduleActionType.ResumeExecution:
- case ScheduleActionType.CircularDependency:
- {
- this.nodeManager.SendData(response.NodeId, response.Unblocker);
- continue;
- }
- case ScheduleActionType.CreateNode:
- list = new List<NodeInfo>();
- num = 0;
- goto Label_0107;
-
- case ScheduleActionType.SubmissionComplete:
- {
- if (this.buildParameters.DetailedSummary)
- {
- this.scheduler.WriteDetailedSummary(response.BuildResult.SubmissionId);
- }
- this.ReportResultsToSubmission(response.BuildResult);
- continue;
- }
- default:
- goto Label_01B9;
- }
- Label_00A3:
- info = this.nodeManager.CreateNode(this.GetNodeConfiguration(), response.RequiredNodeType);
- if (info != null)
- {
- this.noNodesActiveEvent.Reset();
- this.activeNodes.Add(info.NodeId);
- list.Add(info);
- ErrorUtilities.VerifyThrow(this.activeNodes.Count != 0, "Still 0 nodes after asking for a new node. Build cannot proceed.");
- }
- num++;
- Label_0107:
- if (num < response.NumberOfNodesToCreate)
- {
- goto Label_00A3;
- }
- IEnumerable<ScheduleResponse> enumerable = this.scheduler.ReportNodesCreated(list);
- this.PerformSchedulingActions(enumerable);
- continue;
- Label_01B9:;
- ErrorUtilities.ThrowInternalError("Scheduling action {0} not handled.", new object[] { response.Action });
- }
- }
-
- private void ReportResultsToSubmission(BuildResult result)
- {
- lock (this.syncLock)
- {
- if (this.buildSubmissions.ContainsKey(result.SubmissionId))
- {
- BuildSubmission submission = this.buildSubmissions[result.SubmissionId];
- submission.CompleteResults(result);
- if (result.Exception is InternalLoggerException)
- {
- submission.CompleteLogging(false);
- }
- this.overallBuildSuccess = this.overallBuildSuccess && (this.buildSubmissions[result.SubmissionId].BuildResult.OverallResult == BuildResultCode.Success);
- this.CheckSubmissionCompletenessAndRemove(submission);
- }
- }
- }
-
- private void RequireState(BuildManagerState requiredState, string exceptionResouorce)
- {
- ErrorUtilities.VerifyThrowInvalidOperation(this.buildManagerState == requiredState, exceptionResouorce);
- }
-
- private void Reset()
- {
- this.nodeManager.UnregisterPacketHandler(NodePacketType.BuildRequestBlocker);
- this.nodeManager.UnregisterPacketHandler(NodePacketType.BuildRequestConfiguration);
- this.nodeManager.UnregisterPacketHandler(NodePacketType.BuildRequestConfigurationResponse);
- this.nodeManager.UnregisterPacketHandler(NodePacketType.BuildResult);
- this.nodeManager.UnregisterPacketHandler(NodePacketType.NodeShutdown);
- this.nodeManager.ClearPerBuildState();
- this.nodeManager = null;
- this.shuttingDown = false;
- this.nodeConfiguration = null;
- this.buildSubmissions.Clear();
- this.scheduler.Reset();
- this.scheduler = null;
- this.acquiredProjectRootElementCacheFromProjectInstance = false;
- this.unnamedProjectInstanceToNames.Clear();
- this.nodeIdToKnownConfigurations.Clear();
- this.nextUnnamedProjectId = 1;
- if (this.configCache != null)
- {
- foreach (BuildRequestConfiguration configuration in this.configCache)
- {
- configuration.ActivelyBuildingTargets.Clear();
- }
- }
- if (Environment.GetEnvironmentVariable("MSBUILDCLEARXMLCACHEONBUILDMANAGER") == "1")
- {
- this.buildParameters.ProjectRootElementCache.Clear();
- }
- }
-
- public void ResetCaches()
- {
- this.ErrorIfState(BuildManagerState.WaitingForBuildToComplete, "WaitingForEndOfBuild");
- this.ErrorIfState(BuildManagerState.Building, "BuildInProgress");
- this.configCache = ((IBuildComponentHost) this).GetComponent(BuildComponentType.ConfigCache) as IConfigCache;
- this.configCache.ClearConfigurations();
- this.resultsCache = ((IBuildComponentHost) this).GetComponent(BuildComponentType.ResultsCache) as IResultsCache;
- this.resultsCache.ClearResults();
- FileUtilities.ClearCacheDirectory();
- }
-
- private BuildRequestConfiguration ResolveConfiguration(BuildRequestConfiguration unresolvedConfiguration, BuildRequestConfiguration matchingConfigurationFromCache, bool replaceProjectInstance)
- {
- BuildRequestConfiguration config = matchingConfigurationFromCache ?? this.configCache.GetMatchingConfiguration(unresolvedConfiguration);
- if (config == null)
- {
- int configurationIdFromPlan = this.scheduler.GetConfigurationIdFromPlan(unresolvedConfiguration.ProjectFullPath);
- if (this.configCache.HasConfiguration(configurationIdFromPlan) || (configurationIdFromPlan == 0))
- {
- configurationIdFromPlan = this.GetNewConfigurationId();
- }
- config = unresolvedConfiguration.ShallowCloneWithNewId(configurationIdFromPlan);
- this.configCache.AddConfiguration(config);
- return config;
- }
- if (replaceProjectInstance && (unresolvedConfiguration.Project != null))
- {
- config.Project = unresolvedConfiguration.Project;
- this.resultsCache.ClearResultsForConfiguration(config.ConfigurationId);
- return config;
- }
- if (((unresolvedConfiguration.Project != null) && (config.Project != null)) && !object.ReferenceEquals(unresolvedConfiguration.Project, config.Project))
- {
- this.resultsCache.ClearResultsForConfiguration(config.ConfigurationId);
- config.Project = unresolvedConfiguration.Project;
- }
- return config;
- }
-
- private void ShutdownLoggingService(ILoggingService loggingService)
- {
- if (loggingService != null)
- {
- loggingService.OnLoggingThreadException -= this.loggingThreadExceptionEventHandler;
- loggingService.OnProjectFinished -= this.projectFinishedEventHandler;
- loggingService.OnProjectStarted -= this.projectStartedEventHandler;
- (loggingService as IBuildComponent).ShutdownComponent();
- }
- }
-
- private void ShutdownNodesAsync(bool abort)
- {
- this.shuttingDown = true;
- this.nodeManager.ShutdownNodes(!abort && this.buildParameters.EnableNodeReuse);
- }
-
- private void VerifyStateInternal(BuildManagerState requiredState)
- {
- if (this.buildManagerState != requiredState)
- {
- ErrorUtilities.ThrowInternalError("Expected state {0}, actual state {1}", new object[] { requiredState, this.buildManagerState });
- }
- }
-
- public static BuildManager DefaultBuildManager
- {
- get
- {
- if (singletonInstance == null)
- {
- singletonInstance = new BuildManager("Default");
- }
- return singletonInstance;
- }
- }
-
- BuildParameters IBuildComponentHost.BuildParameters
- {
- get
- {
- return this.buildParameters;
- }
- }
-
- LegacyThreadingData IBuildComponentHost.LegacyThreadingData
- {
- get
- {
- return this.legacyThreadingData;
- }
- }
-
- ILoggingService IBuildComponentHost.LoggingService
- {
- get
- {
- return (this.componentFactories.GetComponent(BuildComponentType.LoggingService) as ILoggingService);
- }
- }
-
- string IBuildComponentHost.Name
- {
- get
- {
- return this.hostName;
- }
- }
-
- private enum BuildManagerState
- {
- Idle,
- Building,
- WaitingForBuildToComplete
- }
-
- private class NullLogger : ILogger
- {
- public void Initialize(IEventSource eventSource)
- {
- }
-
- public void Shutdown()
- {
- }
-
- public string Parameters
- {
- get
- {
- return string.Empty;
- }
- set
- {
- }
- }
-
- public LoggerVerbosity Verbosity
- {
- get
- {
- return LoggerVerbosity.Normal;
- }
- set
- {
- }
- }
- …
Large files files are truncated, but you can click here to view the full file