summaryrefslogtreecommitdiff
path: root/mcs/class/Microsoft.Build/Microsoft.Build.Execution/BuildManager.cs
blob: 0c179afa48efedf470ffea5b6c884576b8a7d6c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// BuildManager.cs
//
// Author:
//   Rolf Bjarne Kvinge (rolf@xamarin.com)
//   Atsushi Enomoto (atsushi@xamarin.com)
//
// Copyright (C) 2011,2013 Xamarin Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using Microsoft.Build.Evaluation;
using System;
using System.Collections.Generic;
using System.Threading;
using Microsoft.Build.Internal;
using System.Linq;

namespace Microsoft.Build.Execution
{
	public class BuildManager
	{
		static BuildManager default_manager = new BuildManager ();

		public static BuildManager DefaultBuildManager {
			get { return default_manager; }
		}
		
		public BuildManager ()
		{
		}

		public BuildManager (string hostName)
		{
			throw new NotImplementedException ();
		}
		
		public void Dispose ()
		{
			WaitHandle.WaitAll (submissions.Select (s => s.WaitHandle).ToArray ());
			BuildNodeManager.Stop ();
		}

		~BuildManager ()
		{
			// maybe processes created by out-of-process nodes should be signaled.
			BuildNodeManager.Stop ();
		}

		readonly List<BuildSubmission> submissions = new List<BuildSubmission> ();
		
		BuildParameters ongoing_build_parameters;
		
		internal BuildParameters OngoingBuildParameters {
			get { return ongoing_build_parameters; }
		}

		public void BeginBuild (BuildParameters parameters)
		{
			if (ongoing_build_parameters != null)
				throw new InvalidOperationException ("There is already ongoing build");
			ongoing_build_parameters = parameters.Clone ();
		}

		public BuildResult Build (BuildParameters parameters, BuildRequestData requestData)
		{
			BeginBuild (parameters);
			var ret = BuildRequest (requestData);
			EndBuild ();
			return ret;
		}

		public BuildResult BuildRequest (BuildRequestData requestData)
		{
			var sub = PendBuildRequest (requestData);
			sub.Execute ();
			return sub.BuildResult;
		}
		
		public void CancelAllSubmissions ()
		{
			foreach (var sub in submissions) {
				try {
					if (!sub.IsCompleted)
						sub.Cancel ();
				} catch (InvalidOperationException) {
					// some submissions could be already done during this iteration. Ignore that.
				}
			}
			submissions.Clear ();
		}

		public void EndBuild ()
		{
			if (ongoing_build_parameters == null)
				throw new InvalidOperationException ("Build has not started");
			if (submissions.Count > 0)
				WaitHandle.WaitAll (submissions.Select (s => s.WaitHandle).ToArray ());
			BuildNodeManager.Stop ();
			ongoing_build_parameters = null;
		}
		
		Dictionary<Project,ProjectInstance> instances = new Dictionary<Project, ProjectInstance> ();

		public ProjectInstance GetProjectInstanceForBuild (Project project)
		{
			if (project == null)
				throw new ArgumentNullException ("project");
			if (project.FullPath == null)
				throw new ArgumentNullException ("project", "FullPath parameter in the project cannot be null.");
			if (project.FullPath == string.Empty)
				throw new ArgumentException ("FullPath parameter in the project cannot be empty.", "project");
			// other than that, any invalid path character is accepted...
			
			return GetProjectInstanceForBuildInternal (project);
		}
			
		internal ProjectInstance GetProjectInstanceForBuildInternal (Project project)
		{
			if (!instances.ContainsKey (project))
				instances [project] = project.CreateProjectInstance ();
			return instances [project];
		}

		public BuildSubmission PendBuildRequest (BuildRequestData requestData)
		{
			if (ongoing_build_parameters == null)
				throw new InvalidOperationException ("This method cannot be called before calling BeginBuild method.");
			var sub = new BuildSubmission (this, requestData);
			submissions.Add (sub);
			return sub;
		}

		public void ResetCaches ()
		{
			if (OngoingBuildParameters != null)
				throw new InvalidOperationException ("Cannot reset caches while builds are in progress.");
			
			BuildNodeManager.ResetCaches ();
		}
		
		BuildNodeManager build_node_manager;
		
		internal BuildNodeManager BuildNodeManager {
			get {
				if (build_node_manager == null)
						build_node_manager = new BuildNodeManager (this);
				return build_node_manager;
			}
		}
	}
}