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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
|
// VBBindingCompilerServices.cs
//
// This file was derived from a file from #Develop, and relicensed
// by Markus Palme to MIT/X11
//
// Authors:
// Markus Palme <MarkusPalme@gmx.de>
// Rolf Bjarne Kvinge <RKvinge@novell.com>
//
// Copyright (C) 2001-2007 Markus Palme <MarkusPalme@gmx.de>
// Copyright (C) 2008 Novell, Inc (http://www.novell.com)
//
// 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 System;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.IO;
using System.Diagnostics;
using System.CodeDom.Compiler;
using MonoDevelop.Core;
using MonoDevelop.Core.Execution;
using MonoDevelop.Projects;
using MonoDevelop.Projects.Text;
namespace MonoDevelop.VBNetBinding {
/// <summary>
/// This class controls the compilation of VB.net files and VB.net projects
/// </summary>
public class VBBindingCompilerServices
{
string GenerateOptions (DotNetProjectConfiguration configuration, VBCompilerParameters compilerparameters, VBProjectParameters projectparameters, string outputFileName)
{
var project = configuration.ParentItem;
StringBuilder sb = new StringBuilder ();
sb.AppendFormat ("\"-out:{0}\"", outputFileName);
sb.AppendLine ();
sb.AppendLine ("-nologo");
sb.AppendLine ("-utf8output");
sb.AppendLine ("-quiet");
sb.AppendFormat ("-debug:{0}", compilerparameters.DebugType);
sb.AppendLine ();
if (compilerparameters.Optimize)
sb.AppendLine ("-optimize+");
if (projectparameters.OptionStrict)
sb.AppendLine ("-optionstrict+");
else
sb.AppendLine ("-optionstrict-");
if (projectparameters.OptionExplicit)
sb.AppendLine ("-optionexplicit+");
else
sb.AppendLine ("-optionexplicit-");
if (projectparameters.BinaryOptionCompare)
sb.AppendLine ("-optioncompare:binary");
else
sb.AppendLine ("-optioncompare:text");
if (projectparameters.OptionInfer)
sb.AppendLine ("-optioninfer+");
else
sb.AppendLine ("-optioninfer-");
string mytype = projectparameters.MyType;
if (!string.IsNullOrEmpty (mytype)) {
sb.AppendFormat ("-define:_MYTYPE=\\\"{0}\\\"", mytype);
sb.AppendLine ();
}
string win32IconPath = projectparameters.ApplicationIcon;
if (!string.IsNullOrEmpty (win32IconPath) && File.Exists (win32IconPath)) {
sb.AppendFormat ("\"-win32icon:{0}\"", win32IconPath);
sb.AppendLine ();
}
if (!string.IsNullOrEmpty (projectparameters.CodePage)) {
TextEncoding enc = TextEncoding.GetEncoding (projectparameters.CodePage);
sb.AppendFormat ("-codepage:{0}", enc.CodePage);
sb.AppendLine ();
}
if (!string.IsNullOrEmpty (project.DefaultNamespace)) {
sb.AppendFormat ("-rootnamespace:{0}", project.DefaultNamespace);
sb.AppendLine ();
}
if (!string.IsNullOrEmpty (compilerparameters.DefineConstants)) {
sb.AppendFormat ("\"-define:{0}\"", compilerparameters.DefineConstants);
sb.AppendLine ();
}
if (compilerparameters.DefineDebug)
sb.AppendLine ("-define:DEBUG=-1");
if (compilerparameters.DefineTrace)
sb.AppendLine ("-define:TRACE=-1");
if (compilerparameters.WarningsDisabled) {
sb.AppendLine ("-nowarn");
} else if (!string.IsNullOrEmpty (compilerparameters.NoWarn)) {
sb.AppendFormat ("-nowarn:{0}", compilerparameters.NoWarn);
sb.AppendLine ();
}
if (!string.IsNullOrEmpty (compilerparameters.WarningsAsErrors)) {
sb.AppendFormat ("-warnaserror+:{0}", compilerparameters.WarningsAsErrors);
sb.AppendLine ();
}
if (configuration.SignAssembly) {
if (File.Exists (configuration.AssemblyKeyFile)) {
sb.AppendFormat ("\"-keyfile:{0}\"", configuration.AssemblyKeyFile);
sb.AppendLine ();
}
}
if (configuration.DelaySign)
sb.AppendLine ("-delaySign");
if (!string.IsNullOrEmpty (compilerparameters.DocumentationFile)) {
sb.AppendFormat ("\"-doc:{0}\"", compilerparameters.DocumentationFile);
sb.AppendLine ();
}
if (!string.IsNullOrEmpty (projectparameters.StartupObject) && projectparameters.StartupObject != "Sub Main") {
sb.Append ("-main:");
sb.Append (projectparameters.StartupObject);
sb.AppendLine ();
}
if (compilerparameters.RemoveIntegerChecks)
sb.AppendLine ("-removeintchecks+");
if (!string.IsNullOrEmpty (compilerparameters.AdditionalParameters)) {
sb.Append (compilerparameters.AdditionalParameters);
sb.AppendLine ();
}
switch (configuration.CompileTarget) {
case CompileTarget.Exe:
sb.AppendLine ("-target:exe");
break;
case CompileTarget.WinExe:
sb.AppendLine ("-target:winexe");
break;
case CompileTarget.Library:
sb.AppendLine ("-target:library");
break;
case CompileTarget.Module:
sb.AppendLine ("-target:module");
break;
default:
throw new NotSupportedException("unknown compile target:" + configuration.CompileTarget);
}
return sb.ToString();
}
public BuildResult Compile (ProjectItemCollection items, DotNetProjectConfiguration configuration, ConfigurationSelector configSelector, IProgressMonitor monitor)
{
VBCompilerParameters compilerparameters = (VBCompilerParameters) configuration.CompilationParameters;
if (compilerparameters == null)
compilerparameters = new VBCompilerParameters ();
VBProjectParameters projectparameters = (VBProjectParameters) configuration.ProjectParameters;
if (projectparameters == null)
projectparameters = new VBProjectParameters ();
string exe = configuration.CompiledOutputName;
string responseFileName = Path.GetTempFileName();
StreamWriter writer = new StreamWriter (responseFileName);
writer.WriteLine (GenerateOptions (configuration, compilerparameters, projectparameters, exe));
// Write references
foreach (ProjectReference lib in items.GetAll<ProjectReference> ()) {
foreach (string fileName in lib.GetReferencedFileNames (configSelector)) {
writer.Write ("\"-r:");
writer.Write (fileName);
writer.WriteLine ("\"");
}
}
// Write source files and embedded resources
foreach (ProjectFile finfo in items.GetAll<ProjectFile> ()) {
if (finfo.Subtype != Subtype.Directory) {
switch (finfo.BuildAction) {
case "Compile":
writer.WriteLine("\"" + finfo.Name + "\"");
break;
case "EmbeddedResource":
string fname = finfo.Name;
if (String.Compare (Path.GetExtension (fname), ".resx", true) == 0)
fname = Path.ChangeExtension (fname, ".resources");
writer.WriteLine("\"-resource:{0},{1}\"", fname, finfo.ResourceId);
break;
default:
continue;
}
}
}
// Write source files and embedded resources
foreach (Import import in items.GetAll<Import> ()) {
writer.WriteLine ("-imports:{0}", import.Include);
}
TempFileCollection tf = new TempFileCollection ();
writer.Close();
string output = "";
string compilerName = configuration.TargetRuntime.GetToolPath (configuration.TargetFramework, "vbc");
if (compilerName == null) {
BuildResult res = new BuildResult ();
res.AddError (string.Format ("Visual Basic .NET compiler not found ({0})", configuration.TargetRuntime.DisplayName));
return res;
}
string workingDir = ".";
if (configuration.ParentItem != null)
workingDir = configuration.ParentItem.BaseDirectory;
int exitCode;
var envVars = configuration.TargetRuntime.GetToolsExecutionEnvironment (configuration.TargetFramework);
monitor.Log.WriteLine (Path.GetFileName (compilerName) + " " + string.Join (" ", File.ReadAllLines (responseFileName)));
exitCode = DoCompilation (compilerName, responseFileName, tf, workingDir, envVars, ref output);
monitor.Log.WriteLine (output);
BuildResult result = ParseOutput (tf, output);
if (result.Errors.Count == 0 && exitCode != 0) {
// Compilation failed, but no errors?
// Show everything the compiler said.
result.AddError (output);
}
FileService.DeleteFile (responseFileName);
if (configuration.CompileTarget != CompileTarget.Library) {
WriteManifestFile (exe);
}
return result;
}
// code duplication: see C# backend : CSharpBindingCompilerManager
void WriteManifestFile(string fileName)
{
string manifestFile = String.Concat(fileName, ".manifest");
if (File.Exists(manifestFile)) {
return;
}
StreamWriter sw = new StreamWriter(manifestFile);
sw.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
sw.WriteLine("");
sw.WriteLine("<assembly xmlns=\"urn:schemas-microsoft-com:asm.v1\" manifestVersion=\"1.0\">");
sw.WriteLine(" <dependency>");
sw.WriteLine(" <dependentAssembly>");
sw.WriteLine(" <assemblyIdentity");
sw.WriteLine(" type=\"win32\"");
sw.WriteLine(" name=\"Microsoft.Windows.Common-Controls\"");
sw.WriteLine(" version=\"6.0.0.0\"");
sw.WriteLine(" processorArchitecture=\"X86\"");
sw.WriteLine(" publicKeyToken=\"6595b64144ccf1df\"");
sw.WriteLine(" language=\"*\"");
sw.WriteLine(" />");
sw.WriteLine(" </dependentAssembly>");
sw.WriteLine(" </dependency>");
sw.WriteLine("</assembly>");
sw.Close();
}
BuildResult ParseOutput(TempFileCollection tf, string output)
{
var result = new BuildResult (output, 1, 0);
using (var sr = new StringReader (output)) {
while (true) {
string curLine = sr.ReadLine();
if (curLine == null) {
break;
}
curLine = curLine.Trim();
if (curLine.Length == 0) {
continue;
}
var error = CreateErrorFromString (curLine);
if (error != null)
result.Append (error);
}
}
return result;
}
static BuildError CreateErrorFromString (string error_string)
{
var err = BuildError.FromMSBuildErrorFormat (error_string);
if (err != null) {
// Workaround for bug #484351. Vbnc incorrectly emits this warning.
if (err.ErrorNumber == "VBNC2009" && err.ErrorText != null && err.ErrorText.IndexOf ("optioninfer", StringComparison.Ordinal) != -1)
return null;
}
return err;
}
private int DoCompilation (string compilerName, string responseFileName, TempFileCollection tf, string working_dir, ExecutionEnvironment envVars, ref string output)
{
StringWriter outwr = new StringWriter ();
try {
ProcessStartInfo pinfo = new ProcessStartInfo (compilerName, "\"@" + responseFileName + "\"");
pinfo.WorkingDirectory = working_dir;
envVars.MergeTo (pinfo);
pinfo.UseShellExecute = false;
pinfo.RedirectStandardOutput = true;
pinfo.RedirectStandardError = true;
ProcessWrapper pw = Runtime.ProcessService.StartProcess (pinfo, outwr, outwr, null);
pw.WaitForOutput ();
output = outwr.ToString ();
return pw.ExitCode;
} finally {
if (outwr != null)
outwr.Dispose ();
}
}
}
}
|