~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to src/core/MonoDevelop.Projects.Formats.MSBuild/MonoDevelop.Projects.Formats.MSBuild/ProjectBuilder.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
{
39
39
        public class ProjectBuilder: MarshalByRefObject, IProjectBuilder
40
40
        {
41
 
                Project project;
42
41
                Engine engine;
43
42
                string file;
44
43
                ILogWriter currentLogWriter;
45
44
                MDConsoleLogger consoleLogger;
46
 
                string currentConfiguration, currentPlatform;
47
 
 
48
 
                AutoResetEvent wordDoneEvent = new AutoResetEvent (false);
49
 
                ThreadStart workDelegate;
50
 
                object workLock = new object ();
51
 
                Thread workThread;
52
 
                Exception workError;
53
 
 
54
 
                public ProjectBuilder (string file, string binDir)
 
45
                BuildEngine buildEngine;
 
46
 
 
47
                public ProjectBuilder (BuildEngine buildEngine, Engine engine, string file)
55
48
                {
56
 
                        this.file = file;
57
 
                        RunSTA (delegate
58
 
                        {
59
 
                                engine = new Engine (binDir);
60
 
                                engine.GlobalProperties.SetProperty ("BuildingInsideVisualStudio", "true");
61
 
                                
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");
66
 
 
67
 
                                consoleLogger = new MDConsoleLogger (LoggerVerbosity.Normal, LogWriteLine, null, null);
68
 
                                engine.RegisterLogger (consoleLogger);
69
 
                        });
70
 
                        
 
49
                        this.file = file;
 
50
                        this.engine = engine;
 
51
                        this.buildEngine = buildEngine;
 
52
                        consoleLogger = new MDConsoleLogger (LoggerVerbosity.Normal, LogWriteLine, null, null);
71
53
                        Refresh ();
72
54
                }
 
55
 
 
56
                public void Dispose ()
 
57
                {
 
58
                        buildEngine.UnloadProject (file);
 
59
                }
73
60
                
74
61
                public void Refresh ()
75
62
                {
76
 
                        RunSTA (delegate
77
 
                        {
78
 
                                //just unload the project. it will be reloaded when we next build
79
 
                                project = null;
80
 
                                var loadedProj = engine.GetLoadedProject (file);
81
 
                                if (loadedProj != null)
82
 
                                        engine.UnloadProject (loadedProj);
83
 
                        });
 
63
                        buildEngine.UnloadProject (file);
84
64
                }
85
65
                
86
66
                void LogWriteLine (string txt)
89
69
                                currentLogWriter.WriteLine (txt);
90
70
                }
91
71
                
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)
94
74
                {
95
75
                        MSBuildResult[] result = null;
96
 
                        RunSTA (delegate
 
76
                        BuildEngine.RunSTA (delegate
97
77
                        {
98
 
                                try {
99
 
                                        SetupProject (configuration, platform);
 
78
                                try {
 
79
                                        var project = SetupProject (configurations);
100
80
                                        currentLogWriter = logWriter;
101
81
 
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);
104
86
 
105
87
                                        consoleLogger.Verbosity = GetVerbosity (verbosity);
106
 
                                        
 
88
 
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);
135
117
                        }
136
118
                }
137
119
 
138
 
                public string[] GetAssemblyReferences (string configuration, string platform)
 
120
                public string[] GetAssemblyReferences (ProjectConfigurationInfo[] configurations)
139
121
                {
140
122
                        string[] refsArray = null;
141
123
 
142
 
                        RunSTA (delegate
 
124
                        BuildEngine.RunSTA (delegate
143
125
                        {
144
 
                                SetupProject (configuration, platform);
 
126
                                var project = SetupProject (configurations);
145
127
                                
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;
156
138
                }
157
139
                
158
 
                void SetupProject (string configuration, string platform)
 
140
                Project SetupProject (ProjectConfigurationInfo[] configurations)
159
141
                {
160
 
                        if (project != null && configuration == currentConfiguration && platform == currentPlatform)
161
 
                                return;
162
 
                        currentConfiguration = configuration;
163
 
                        currentPlatform = platform;
164
 
                        
 
142
                        Project project = null;
 
143
 
 
144
                        foreach (var pc in configurations) {
 
145
                                var p = engine.GetLoadedProject (pc.ProjectFile);
 
146
                                if (p == null) {
 
147
                                        p = new Project (engine);
 
148
                                        p.Load (pc.ProjectFile);
 
149
                                }
 
150
                                p.GlobalProperties.SetProperty ("Configuration", pc.Configuration);
 
151
                                if (!string.IsNullOrEmpty (pc.Platform))
 
152
                                        p.GlobalProperties.SetProperty ("Platform", pc.Platform);
 
153
                                else
 
154
                                        p.GlobalProperties.RemoveProperty ("Platform");
 
155
                                if (pc.ProjectFile == file)
 
156
                                        project = p;
 
157
                        }
 
158
 
165
159
                        Environment.CurrentDirectory = Path.GetDirectoryName (file);
166
 
                        engine.GlobalProperties.SetProperty ("Configuration", configuration);
167
 
                        if (!string.IsNullOrEmpty (platform))
168
 
                                engine.GlobalProperties.SetProperty ("Platform", platform);
169
 
                        else
170
 
                                engine.GlobalProperties.RemoveProperty ("Platform");
171
 
                        
172
 
                        project = new Project (engine);
173
 
                        project.Load (file);
 
160
                        return project;
174
161
                }
175
162
                
176
163
                public override object InitializeLifetimeService ()
178
165
                        return null;
179
166
                }
180
167
 
181
 
                void RunSTA (ThreadStart ts)
182
 
                {
183
 
                        lock (workLock) {
184
 
                                lock (threadLock) {
185
 
                                        workDelegate = ts;
186
 
                                        workError = null;
187
 
                                        if (workThread == null) {
188
 
                                                workThread = new Thread (STARunner);
189
 
                                                workThread.SetApartmentState (ApartmentState.STA);
190
 
                                                workThread.IsBackground = true;
191
 
                                                workThread.Start ();
192
 
                                        }
193
 
                                        else
194
 
                                                // Awaken the existing thread
195
 
                                                Monitor.Pulse (threadLock);
196
 
                                }
197
 
                                wordDoneEvent.WaitOne ();
198
 
                        }
199
 
                        if (workError != null)
200
 
                                throw new Exception ("MSBuild operation failed", workError);
201
 
                }
202
 
 
203
 
                object threadLock = new object ();
204
 
 
205
 
                void STARunner ()
206
 
                {
207
 
                        lock (threadLock) {
208
 
                                do {
209
 
                                        try {
210
 
                                                workDelegate ();
211
 
                                        }
212
 
                                        catch (Exception ex) {
213
 
                                                workError = ex;
214
 
                                        }
215
 
                                        wordDoneEvent.Set ();
216
 
                                }
217
 
                                while (Monitor.Wait (threadLock, 60000));
218
 
 
219
 
                                workThread = null;
220
 
                        }
221
 
                }
222
 
                
223
168
                //from MSBuildProjectService
224
169
                static string UnescapeString (string str)
225
170
                {