This repository has been archived by the owner on Aug 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathCVXOps.cs
225 lines (193 loc) · 6.86 KB
/
CVXOps.cs
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
* ClangVSx - Compiler Bridge for CLang in MS Visual Studio
* Harry Denholm, ishani.org 2011-2012
*
* https://github.com/ishani/ClangVSx
* http://www.ishani.org/web/articles/code/clangvsx/
*
* Released under LLVM Release License. See LICENSE.TXT for details.
*/
using System;
using System.Windows.Forms;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.VCProjectEngine;
namespace ClangVSx
{
internal class ClangOps
{
#region Delegates
public delegate void BuildEventDelegate(bool success);
#endregion
private readonly DTE2 _applicationObject;
private readonly OutputWindowPane _outputPane;
private readonly Window _vsOutputWindow;
/// <summary>
///
/// </summary>
public ClangOps(DTE2 appObj)
{
_applicationObject = appObj;
const string owpName = "Clang C/C++";
// go find the output window
_vsOutputWindow = _applicationObject.Windows.Item(Constants.vsWindowKindOutput);
_vsOutputWindow.Visible = true;
var theOutputWindow = (OutputWindow)_vsOutputWindow.Object;
// add or acquire the output pane
try
{
_outputPane = theOutputWindow.OutputWindowPanes.Item(owpName);
}
catch
{
_outputPane = theOutputWindow.OutputWindowPanes.Add(owpName);
}
}
/// <summary>
/// simple wrapper for writing text to the output pane
/// </summary>
public void WriteToOutputPane(string text)
{
_outputPane.OutputString(text);
}
/// <summary>
/// compile a single VCFile, do nothing with the OBJ
/// </summary>
public bool CompileSingleFile(VCFile vcFile, VCProject vcProject, VCConfiguration vcCfg,
String additionalCmds = "")
{
CVXBuildSystem buildSystem;
try
{
buildSystem = new CVXBuildSystem(_vsOutputWindow, _outputPane);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "ClangVSx Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
String prevEnv = Environment.CurrentDirectory;
try
{
Environment.CurrentDirectory = vcProject.ProjectDirectory;
WriteToOutputPane("Project Directory : " + Environment.CurrentDirectory + "\n");
return buildSystem.CompileSingleFile(vcFile, vcProject, vcCfg, additionalCmds);
}
catch (Exception ex)
{
WriteToOutputPane("Exception During File Compile : \n" + ex.Message + "\n");
}
finally
{
Environment.CurrentDirectory = prevEnv;
}
return false;
}
/// <summary>
/// Compile the project that is set as the 'startup project' in the solution
/// </summary>
public void BuildActiveProject(object buildConfig)
{
// cast the input object; this is designed to be run with ParameterizedThreadStart, so we have to accept 'object'...
var config = (buildConfig as ProjectBuildConfig);
if (config == null)
throw new InvalidCastException(
"BuildActiveProject called with invalid argument - ProjectBuildConfig required");
// mark the build as ready-to-go
config.BuildBegun(true);
CVXBuildSystem buildSystem;
try
{
buildSystem = new CVXBuildSystem(_vsOutputWindow, _outputPane);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "ClangVSx Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
config.BuildFinished(false);
return;
}
String prevEnv = Environment.CurrentDirectory;
try
{
// loop through the startup projects
foreach (object startUpProj in (Array)_applicationObject.Solution.SolutionBuild.StartupProjects)
{
if (config.BuildShouldCancel.ShouldCancelBuild())
{
WriteToOutputPane("Stopping build... \n");
break;
}
// is this project a VC++ one? the guid is hardcoded because it doesn't seem to be included
// anywhere else in the constants, EnvDTE, etc..!
Project p = _applicationObject.Solution.Item(startUpProj);
if (p.Kind.ToUpper().Equals("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}"))
{
WriteToOutputPane("Building Project : " + p.Name + "\n");
var vcProject = p.Object as VCProject;
if (vcProject == null)
{
WriteToOutputPane("Error : Could not cast project to VCProject.\n");
config.BuildFinished(false);
return;
}
Configuration cfg = p.ConfigurationManager.ActiveConfiguration;
VCConfiguration vcCfg = null;
try
{
var cfgArray = (IVCCollection)vcProject.Configurations;
foreach (VCConfiguration vcr in cfgArray)
{
// we .ToLower() here as there are some weird occurances where names don't match as VS is holding
// onto a capitalised version that isn't visible in any of the VS IDEs :Z
if (vcr.ConfigurationName.ToLower() == cfg.ConfigurationName.ToLower() &&
vcr.Platform.Name.ToLower() == cfg.PlatformName.ToLower())
{
vcCfg = vcr;
}
}
}
catch (Exception)
{
WriteToOutputPane("Error - failed to determine VC configuration\n");
}
if (vcCfg == null)
{
WriteToOutputPane("Error : Could not find '" + cfg.ConfigurationName + "' configuration!\n");
}
else
{
WriteToOutputPane("Configuration : " + vcCfg.Name + "\n");
}
Environment.CurrentDirectory = vcProject.ProjectDirectory;
WriteToOutputPane("Project Directory : " + Environment.CurrentDirectory + "\n");
bool result = buildSystem.BuildProject(vcProject, vcCfg, config.JustLink, config.BuildShouldCancel);
config.BuildFinished(result);
return;
}
else
{
WriteToOutputPane("Ignoring non-C++ Project : " + p.Name + "\n");
}
}
}
catch (Exception ex)
{
WriteToOutputPane("Exception During Build : \n" + ex.Message + "\n");
}
finally
{
config.BuildFinished(false);
Environment.CurrentDirectory = prevEnv;
}
}
#region Nested type: ProjectBuildConfig
public class ProjectBuildConfig
{
public BuildEventDelegate BuildBegun;
public BuildEventDelegate BuildFinished;
public IBuildCancellation BuildShouldCancel;
public bool JustLink;
}
#endregion
}
}