39
39
public class ProjectBuilder: MarshalByRefObject, IProjectBuilder
44
43
ILogWriter currentLogWriter;
45
44
MDConsoleLogger consoleLogger;
46
string currentConfiguration, currentPlatform;
48
AutoResetEvent wordDoneEvent = new AutoResetEvent (false);
49
ThreadStart workDelegate;
50
object workLock = new object ();
54
public ProjectBuilder (string file, string binDir)
45
BuildEngine buildEngine;
47
public ProjectBuilder (BuildEngine buildEngine, Engine engine, string file)
59
engine = new Engine (binDir);
60
engine.GlobalProperties.SetProperty ("BuildingInsideVisualStudio", "true");
62
//we don't have host compilers in MD, and this is set to true by some of the MS targets
63
//which causes it to always run the CoreCompile task if BuildingInsideVisualStudio is also
64
//true, because the VS in-process compiler would take care of the deps tracking
65
engine.GlobalProperties.SetProperty ("UseHostCompilerIfAvailable", "false");
67
consoleLogger = new MDConsoleLogger (LoggerVerbosity.Normal, LogWriteLine, null, null);
68
engine.RegisterLogger (consoleLogger);
51
this.buildEngine = buildEngine;
52
consoleLogger = new MDConsoleLogger (LoggerVerbosity.Normal, LogWriteLine, null, null);
56
public void Dispose ()
58
buildEngine.UnloadProject (file);
74
61
public void Refresh ()
78
//just unload the project. it will be reloaded when we next build
80
var loadedProj = engine.GetLoadedProject (file);
81
if (loadedProj != null)
82
engine.UnloadProject (loadedProj);
63
buildEngine.UnloadProject (file);
86
66
void LogWriteLine (string txt)
89
69
currentLogWriter.WriteLine (txt);
92
public MSBuildResult[] RunTarget (string target, string configuration, string platform, ILogWriter logWriter,
72
public MSBuildResult[] RunTarget (string target, ProjectConfigurationInfo[] configurations, ILogWriter logWriter,
93
73
MSBuildVerbosity verbosity)
95
75
MSBuildResult[] result = null;
76
BuildEngine.RunSTA (delegate
99
SetupProject (configuration, platform);
79
var project = SetupProject (configurations);
100
80
currentLogWriter = logWriter;
102
LocalLogger logger = new LocalLogger (Path.GetDirectoryName (file));
103
engine.RegisterLogger (logger);
82
LocalLogger logger = new LocalLogger (Path.GetDirectoryName (file));
83
engine.UnregisterAllLoggers ();
84
engine.RegisterLogger (logger);
85
engine.RegisterLogger (consoleLogger);
105
87
consoleLogger.Verbosity = GetVerbosity (verbosity);
107
89
// We are using this BuildProject overload and the BuildSettings.None argument as a workaround to
108
90
// an xbuild bug which causes references to not be resolved after the project has been built once.
109
91
engine.BuildProject (project, new string[] { target }, new Hashtable (), BuildSettings.None);
138
public string[] GetAssemblyReferences (string configuration, string platform)
120
public string[] GetAssemblyReferences (ProjectConfigurationInfo[] configurations)
140
122
string[] refsArray = null;
124
BuildEngine.RunSTA (delegate
144
SetupProject (configuration, platform);
126
var project = SetupProject (configurations);
146
128
// We are using this BuildProject overload and the BuildSettings.None argument as a workaround to
147
129
// an xbuild bug which causes references to not be resolved after the project has been built once.
155
137
return refsArray;
158
void SetupProject (string configuration, string platform)
140
Project SetupProject (ProjectConfigurationInfo[] configurations)
160
if (project != null && configuration == currentConfiguration && platform == currentPlatform)
162
currentConfiguration = configuration;
163
currentPlatform = platform;
142
Project project = null;
144
foreach (var pc in configurations) {
145
var p = engine.GetLoadedProject (pc.ProjectFile);
147
p = new Project (engine);
148
p.Load (pc.ProjectFile);
150
p.GlobalProperties.SetProperty ("Configuration", pc.Configuration);
151
if (!string.IsNullOrEmpty (pc.Platform))
152
p.GlobalProperties.SetProperty ("Platform", pc.Platform);
154
p.GlobalProperties.RemoveProperty ("Platform");
155
if (pc.ProjectFile == file)
165
159
Environment.CurrentDirectory = Path.GetDirectoryName (file);
166
engine.GlobalProperties.SetProperty ("Configuration", configuration);
167
if (!string.IsNullOrEmpty (platform))
168
engine.GlobalProperties.SetProperty ("Platform", platform);
170
engine.GlobalProperties.RemoveProperty ("Platform");
172
project = new Project (engine);
176
163
public override object InitializeLifetimeService ()
181
void RunSTA (ThreadStart ts)
187
if (workThread == null) {
188
workThread = new Thread (STARunner);
189
workThread.SetApartmentState (ApartmentState.STA);
190
workThread.IsBackground = true;
194
// Awaken the existing thread
195
Monitor.Pulse (threadLock);
197
wordDoneEvent.WaitOne ();
199
if (workError != null)
200
throw new Exception ("MSBuild operation failed", workError);
203
object threadLock = new object ();
212
catch (Exception ex) {
215
wordDoneEvent.Set ();
217
while (Monitor.Wait (threadLock, 60000));
223
168
//from MSBuildProjectService
224
169
static string UnescapeString (string str)