monowrap.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //
  2. // This is a wrapper used to invoke one of the Mono C#
  3. // compilers with the correct MONO_PATH depending on
  4. // where this command is located.
  5. //
  6. // This allows us to use MSBuild CscToolPath property to
  7. // point to this directory to use our compiler to drive
  8. // the build and set the MONO_PATH as it is expected to
  9. // be for bootstrap
  10. //
  11. // The MONO_PATH and the compiler are chosen based on the
  12. // directory that hosts the command.
  13. //
  14. // The directory specifies a profile, and the format is:
  15. // PROFILES-COMPILER
  16. //
  17. // Will request that the COMPILER compiler is used setting
  18. // MONO_PATH to PROFILES. The PROFILES string can contain
  19. // multiple directories, separated by dashes.
  20. //
  21. // COMPILER is one of:
  22. // basic -> class/lib/basic/mcs.exe
  23. // net_1_1_bootstrap -> class/lib/net_1_1_bootstrap/mcs.exe
  24. // net_1_1 -> class/lib/net_1_1/mcs.exe
  25. // net_2_0_bootstrap -> class/lib/net_2_0_bootstrap/gmcs.exe
  26. // gmcs -> mcs/gmcs.exe
  27. // moonlight_bootstrap -> class/lib/moonlight_bootstrap/smcs.exe
  28. // moonlight_raw -> class/lib/moonlight_raw/smcs.exe
  29. //
  30. // So for example:
  31. // moonlight_bootstrap-net_2_0-moonlight_bootstrap
  32. //
  33. // Will set MONO_PATH to "%MCS_ROOT%\class\lib\moonlight_bootstrap;%MCS_ROOT\class\lib\net_2_0"
  34. // and run the compiler in %MCS_ROOT%\class\lib\moonlight_bootstrap
  35. //
  36. using System;
  37. using System.Collections.Generic;
  38. using System.IO;
  39. using System.Text;
  40. using System.Diagnostics;
  41. namespace csc
  42. {
  43. class Program
  44. {
  45. static int Main(string[] args)
  46. {
  47. string cmd = Environment.GetCommandLineArgs () [0];
  48. string tool = Path.GetFileName(cmd);
  49. string profile = Path.GetDirectoryName(cmd);
  50. int p = profile.LastIndexOf('\\');
  51. if (p == -1) {
  52. Console.Error.WriteLine("Could not find the profile name from this {0}", profile);
  53. return 1;
  54. }
  55. profile = profile.Substring(p+1);
  56. var root_mono = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(cmd), "..\\..\\.."));
  57. if (!File.Exists(Path.Combine(root_mono, "mono\\mini\\mini.c"))) {
  58. Console.WriteLine("My root is incorrect {0} based on {1}", root_mono, cmd);
  59. Console.WriteLine("Must be in mono/msvc/scripts/PROFILE/COMMAND.exe");
  60. return 1;
  61. }
  62. p = profile.LastIndexOf ('-');
  63. if (p == -1){
  64. Console.Error.WriteLine("The directory holding this executable should be MPATHS-COMPILER, instead it is: {0}", profile);
  65. return 1;
  66. }
  67. var root_mcs = Path.GetFullPath (Path.Combine (root_mono, "..\\mcs"));
  68. var mono_cmd = root_mono + "\\msvc\\Win32_Debug_eglib\\bin\\mono.exe";
  69. string compiler = null;
  70. switch (profile.Substring (p+1)){
  71. case "basic":
  72. compiler = root_mcs + "\\class\\lib\\basic\\mcs.exe";
  73. break;
  74. case "net_1_1_bootstrap":
  75. compiler = root_mcs + "\\class\\lib\\net_1_1_bootstrap\\mcs.exe";
  76. break;
  77. case "net_1_1":
  78. compiler = root_mcs + "\\class\\lib\\net_1_1\\mcs.exe";
  79. break;
  80. case "net_2_0_bootstrap":
  81. compiler = root_mcs + "\\class\\lib\\net_2_0_bootstrap\\gmcs.exe";
  82. break;
  83. case "gmcs":
  84. case "mcs":
  85. compiler = root_mcs + "\\mcs\\gmcs.exe";
  86. break;
  87. case "moonlight_bootstrap":
  88. compiler = root_mcs + "\\class\\lib\\moonlight_bootstrap\\smcs.exe";
  89. break;
  90. case "moonlight_raw":
  91. compiler = root_mcs + "\\class\\lib\\moonlight_raw\\smcs.exe";
  92. break;
  93. default:
  94. Console.WriteLine ("Unknown compiler configuration: {0}", profile.Substring (p+1));
  95. return 1;
  96. }
  97. var paths = profile.Substring (0, p).Split (new char [] { '-' });
  98. StringBuilder sb = new StringBuilder ();
  99. foreach (string dir in paths){
  100. if (sb.Length != 0)
  101. sb.Append (";");
  102. sb.Append (root_mcs + "\\class\\lib\\" + dir);
  103. }
  104. Environment.SetEnvironmentVariable ("MONO_PATH", sb.ToString ());
  105. Console.WriteLine ("Compiler: {0}", compiler);
  106. Console.WriteLine ("MONO_PATH: {0}", sb.ToString ());
  107. var pi = new ProcessStartInfo() {
  108. FileName = mono_cmd,
  109. WindowStyle = ProcessWindowStyle.Hidden,
  110. Arguments = compiler + " " + String.Join (" ", args),
  111. UseShellExecute = false
  112. };
  113. try {
  114. var proc = Process.Start (pi);
  115. proc.WaitForExit ();
  116. return proc.ExitCode;
  117. } catch (System.ComponentModel.Win32Exception){
  118. Console.Error.WriteLine ("Chances are, it did not find {0}", mono_cmd);
  119. throw;
  120. }
  121. }
  122. }
  123. }