5 // Jochen Wezel (jwezel@compumaster.de)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 // (c) 2003 Jochen Wezel (http://www.compumaster.de)
9 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.CodeDom.Compiler;
35 using System.ComponentModel;
38 using System.Reflection;
39 using System.Collections;
40 using System.Collections.Specialized;
41 using System.Diagnostics;
42 using System.Text.RegularExpressions;
44 namespace Microsoft.VisualBasic
46 partial class VBCodeGenerator
48 protected override CompilerResults FromFileBatch(CompilerParameters options, string[] fileNames)
51 throw new ArgumentNullException(nameof(options));
53 if (fileNames == null)
54 throw new ArgumentNullException(nameof(fileNames));
56 CompilerResults results = new CompilerResults (options.TempFiles);
57 Process vbnc = new Process ();
59 string vbnc_output = "";
60 string[] vbnc_output_lines;
61 // FIXME: these lines had better be platform independent.
62 if (Path.DirectorySeparatorChar == '\\') {
63 vbnc.StartInfo.FileName = MonoToolsLocator.Mono;
64 vbnc.StartInfo.Arguments = MonoToolsLocator.VBCompiler + ' ' + BuildArgs (options, fileNames);
66 vbnc.StartInfo.FileName = MonoToolsLocator.VBCompiler;
67 vbnc.StartInfo.Arguments = BuildArgs (options, fileNames);
69 //Console.WriteLine (vbnc.StartInfo.Arguments);
70 vbnc.StartInfo.CreateNoWindow = true;
71 vbnc.StartInfo.UseShellExecute = false;
72 vbnc.StartInfo.RedirectStandardOutput = true;
75 } catch (Exception e) {
76 Win32Exception exc = e as Win32Exception;
78 throw new SystemException (String.Format ("Error running {0}: {1}", vbnc.StartInfo.FileName,
79 Win32Exception.GetErrorMessage (exc.NativeErrorCode)));
85 vbnc_output = vbnc.StandardOutput.ReadToEnd ();
88 results.NativeCompilerReturnValue = vbnc.ExitCode;
93 if (results.NativeCompilerReturnValue == 1) {
95 vbnc_output_lines = vbnc_output.Split (Environment.NewLine.ToCharArray ());
96 foreach (string error_line in vbnc_output_lines) {
97 CompilerError error = CreateErrorFromString (error_line);
99 results.Errors.Add (error);
104 if ((loadIt == false && !results.Errors.HasErrors) // Failed, but no errors? Probably couldn't parse the compiler output correctly.
105 || (results.NativeCompilerReturnValue != 0 && results.NativeCompilerReturnValue != 1)) // Neither success (0), nor failure (1), so it crashed.
107 // Show the entire output as one big error message.
109 CompilerError error = new CompilerError (string.Empty, 0, 0, "VBNC_CRASH", vbnc_output);
110 results.Errors.Add (error);
114 if (options.GenerateInMemory) {
115 using (FileStream fs = File.OpenRead (options.OutputAssembly)) {
116 byte[] buffer = new byte[fs.Length];
117 fs.Read (buffer, 0, buffer.Length);
118 results.CompiledAssembly = Assembly.Load (buffer, null);
122 results.CompiledAssembly = Assembly.LoadFrom (options.OutputAssembly);
123 results.PathToAssembly = options.OutputAssembly;
126 results.CompiledAssembly = null;
132 static string BuildArgs (CompilerParameters options, string[] fileNames)
134 StringBuilder args = new StringBuilder ();
135 args.Append ("/quiet ");
136 if (options.GenerateExecutable)
137 args.Append ("/target:exe ");
139 args.Append ("/target:library ");
141 /* Disabled. It causes problems now. -- Gonzalo
142 if (options.IncludeDebugInformation)
143 args.AppendFormat("/debug ");
146 if (options.TreatWarningsAsErrors)
147 args.Append ("/warnaserror ");
149 /* Disabled. vbnc does not support warninglevels.
150 if (options.WarningLevel != -1)
151 args.AppendFormat ("/wlevel:{0} ", options.WarningLevel);
154 if (options.OutputAssembly == null || options.OutputAssembly.Length == 0) {
155 string ext = (options.GenerateExecutable ? "exe" : "dll");
156 options.OutputAssembly = GetTempFileNameWithExtension (options.TempFiles, ext, !options.GenerateInMemory);
159 args.AppendFormat ("/out:\"{0}\" ", options.OutputAssembly);
161 bool Reference2MSVBFound;
162 Reference2MSVBFound = false;
163 if (null != options.ReferencedAssemblies) {
164 foreach (string import in options.ReferencedAssemblies) {
165 if (string.Compare (import, "Microsoft.VisualBasic", true, System.Globalization.CultureInfo.InvariantCulture) == 0)
166 Reference2MSVBFound = true;
167 args.AppendFormat ("/r:\"{0}\" ", import);
171 // add standard import to Microsoft.VisualBasic if missing
172 if (!Reference2MSVBFound)
173 args.Append ("/r:\"Microsoft.VisualBasic.dll\" ");
175 if (options.CompilerOptions != null) {
176 args.Append (options.CompilerOptions);
179 /* Disabled, vbnc does not support this.
180 args.Append (" -- "); // makes vbnc not try to process filenames as options
182 foreach (string source in fileNames)
183 args.AppendFormat (" \"{0}\" ", source);
185 return args.ToString ();
188 static CompilerError CreateErrorFromString (string error_string)
190 CompilerError error = new CompilerError ();
191 Regex reg = new Regex (@"^(\s*(?<file>.*)?\((?<line>\d*)(,(?<column>\d*))?\)\s+)?:\s*" +
192 @"(?<level>Error|Warning)?\s*(?<number>.*):\s(?<message>.*)",
193 RegexOptions.Compiled | RegexOptions.ExplicitCapture);
195 Match match = reg.Match (error_string);
196 if (!match.Success) {
200 if (String.Empty != match.Result ("${file}"))
201 error.FileName = match.Result ("${file}").Trim ();
203 if (String.Empty != match.Result ("${line}"))
204 error.Line = Int32.Parse (match.Result ("${line}"));
206 if (String.Empty != match.Result ("${column}"))
207 error.Column = Int32.Parse (match.Result ("${column}"));
209 if (match.Result ("${level}").Trim () == "Warning")
210 error.IsWarning = true;
212 error.ErrorNumber = match.Result ("${number}");
213 error.ErrorText = match.Result ("${message}");
218 private static string GetTempFileNameWithExtension (TempFileCollection temp_files, string extension, bool keepFile)
220 return temp_files.AddExtension (extension, keepFile);