Guest User

Untitled

a guest
Oct 21st, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.37 KB | None | 0 0
  1.         private CompilerResults CompileFromFileBatch (CompilerParameters options, string[] fileNames)
  2.         {
  3.             if (null == options)
  4.                 throw new ArgumentNullException("options");
  5.             if (null == fileNames)
  6.                 throw new ArgumentNullException("fileNames");
  7.  
  8.             CompilerResults results=new CompilerResults(options.TempFiles);
  9.             Process mcs=new Process();
  10.  
  11. #if !NET_2_0
  12.             string mcs_output;
  13.             string mcs_stdout;
  14.             string[] mcsOutput;
  15. #endif
  16.            
  17.             // FIXME: these lines had better be platform independent.
  18.             if (Path.DirectorySeparatorChar == '\\') {
  19.                 mcs.StartInfo.FileName = windowsMonoPath;
  20.                 mcs.StartInfo.Arguments = "\"" + windowsMcsPath + "\" " +
  21. #if NET_2_0
  22.                     BuildArgs (options, fileNames, ProviderOptions);
  23. #else
  24.                     BuildArgs (options, fileNames);
  25. #endif
  26.             } else {
  27. #if NET_2_0
  28.                 // FIXME: This is a temporary hack to make code genaration work in 2.0+
  29. #if NET_4_0
  30.                 mcs.StartInfo.FileName="dmcs";
  31. #else
  32.                 mcs.StartInfo.FileName="gmcs";
  33. #endif
  34.                 mcs.StartInfo.Arguments=BuildArgs(options, fileNames, ProviderOptions);
  35. #else
  36.                 mcs.StartInfo.FileName="mcs";
  37.                 mcs.StartInfo.Arguments=BuildArgs(options, fileNames);
  38. #endif
  39.             }
  40.  
  41. #if NET_2_0
  42.             mcsOutput = new StringCollection ();
  43.             mcsOutMutex = new Mutex ();
  44. #endif
  45.  
  46.             string monoPath = Environment.GetEnvironmentVariable ("MONO_PATH");
  47.             if (monoPath == null)
  48.                 monoPath = String.Empty;
  49.            
  50.             string privateBinPath = AppDomain.CurrentDomain.SetupInformation.PrivateBinPath;
  51.             if (privateBinPath != null && privateBinPath.Length > 0)
  52.                 monoPath = String.Format ("{0}:{1}", privateBinPath, monoPath);
  53.  
  54.             if (monoPath.Length > 0) {
  55.                 StringDictionary dict = mcs.StartInfo.EnvironmentVariables;
  56.                 if (dict.ContainsKey ("MONO_PATH"))
  57.                     dict ["MONO_PATH"] = monoPath;
  58.                 else
  59.                     dict.Add ("MONO_PATH", monoPath);
  60.             }
  61.            
  62.             mcs.StartInfo.CreateNoWindow=true;
  63.             mcs.StartInfo.UseShellExecute=false;
  64.             mcs.StartInfo.RedirectStandardOutput=true;
  65.             mcs.StartInfo.RedirectStandardError=true;
  66. #if NET_2_0
  67.             mcs.ErrorDataReceived += new DataReceivedEventHandler (McsStderrDataReceived);
  68. #endif
  69.            
  70.             try {
  71.                 mcs.Start();
  72.             } catch (Exception e) {
  73.                 Win32Exception exc = e as Win32Exception;
  74.                 if (exc != null) {
  75.                     throw new SystemException (String.Format ("Error running {0}: {1}", mcs.StartInfo.FileName,
  76.                                     Win32Exception.W32ErrorMessage (exc.NativeErrorCode)));
  77.                 }
  78.                 throw;
  79.             }
  80.  
  81.             try {
  82. #if NET_2_0
  83.                 mcs.BeginOutputReadLine ();
  84.                 mcs.BeginErrorReadLine ();
  85. #else
  86.                 // If there are a few kB in stdout, we might lock
  87.                 mcs_output=mcs.StandardError.ReadToEnd();
  88.                 mcs_stdout=mcs.StandardOutput.ReadToEnd ();
  89. #endif
  90.                 mcs.WaitForExit();
  91.                
  92.                 results.NativeCompilerReturnValue = mcs.ExitCode;
  93.             } finally {
  94. #if NET_2_0
  95.                 mcs.CancelErrorRead ();
  96.                 mcs.CancelOutputRead ();
  97. #endif
  98.  
  99.                 mcs.Close();
  100.             }
  101.  
  102. #if NET_2_0
  103.             StringCollection sc = mcsOutput;
  104. #else
  105.             mcsOutput = mcs_output.Split (System.Environment.NewLine.ToCharArray ());
  106.             StringCollection sc = new StringCollection ();
  107. #endif
  108.                
  109.             bool loadIt=true;
  110.             foreach (string error_line in mcsOutput) {
  111. #if !NET_2_0
  112.                 sc.Add (error_line);
  113. #endif
  114.                 CompilerError error = CreateErrorFromString (error_line);
  115.                 if (error != null) {
  116.                     results.Errors.Add (error);
  117.                     if (!error.IsWarning)
  118.                         loadIt = false;
  119.                 }
  120.             }
  121.            
  122.             if (sc.Count > 0) {
  123.                 sc.Insert (0, mcs.StartInfo.FileName + " " + mcs.StartInfo.Arguments + Environment.NewLine);
  124.                 results.Output = sc;
  125.             }
  126.  
  127.             if (loadIt) {
  128.                 if (!File.Exists (options.OutputAssembly)) {
  129.                     StringBuilder sb = new StringBuilder ();
  130.                     foreach (string s in sc)
  131.                         sb.Append (s + Environment.NewLine);
  132.                    
  133.                     throw new Exception ("Compiler failed to produce the assembly. Output: '" + sb.ToString () + "'");
  134.                 }
  135.                
  136.                 if (options.GenerateInMemory) {
  137.                     using (FileStream fs = File.OpenRead(options.OutputAssembly)) {
  138.                         byte[] buffer = new byte[fs.Length];
  139.                         fs.Read(buffer, 0, buffer.Length);
  140.                         results.CompiledAssembly = Assembly.Load(buffer, null, options.Evidence);
  141.                         fs.Close();
  142.                     }
  143.                 } else {
  144.                     // Avoid setting CompiledAssembly right now since the output might be a netmodule
  145.                     results.PathToAssembly = options.OutputAssembly;
  146.                 }
  147.             } else {
  148.                 results.CompiledAssembly = null;
  149.             }
  150.            
  151.             return results;
  152.         }
Add Comment
Please, Sign In to add comment