Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / msvc / scripts / monowrap.cs
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
37 using System;
38 using System.Collections.Generic;
39 using System.IO;
40 using System.Text;
41 using System.Diagnostics;
42
43 namespace csc
44 {
45         class Program
46         {
47                 static int Main(string[] args)
48                 {
49                         string cmd = Environment.GetCommandLineArgs () [0];
50                         string tool = Path.GetFileName(cmd);
51                         string profile = Path.GetDirectoryName(cmd);
52                         int p = profile.LastIndexOf('\\');
53                         if (p == -1) {
54                                 Console.Error.WriteLine("Could not find the profile name from this {0}", profile);
55                                 return 1;
56                         }
57                         profile = profile.Substring(p+1);
58
59                         var root_mono = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(cmd), "..\\..\\.."));
60                         if (!File.Exists(Path.Combine(root_mono, "mono\\mini\\mini.c"))) {
61                                 Console.WriteLine("My root is incorrect {0} based on {1}", root_mono, cmd);
62                                 Console.WriteLine("Must be in mono/msvc/scripts/PROFILE/COMMAND.exe");
63                                 return 1;
64                         }
65
66                         p = profile.LastIndexOf ('-');
67                         if (p == -1){
68                                 Console.Error.WriteLine("The directory holding this executable should be MPATHS-COMPILER, instead it is: {0}", profile);
69                                 return 1;
70                         }
71
72                         var root_mcs = Path.GetFullPath (Path.Combine (root_mono, "..\\mcs"));
73                         var mono_cmd = root_mono + "\\msvc\\Win32_Debug_eglib\\bin\\mono.exe";
74
75                         string compiler = null;
76                         switch (profile.Substring (p+1)){
77                         case "basic":
78                                 compiler = root_mcs + "\\class\\lib\\basic\\mcs.exe";
79                                 break;
80                         case "net_1_1_bootstrap":
81                                 compiler = root_mcs + "\\class\\lib\\net_1_1_bootstrap\\mcs.exe";
82                                 break;
83                                 
84                         case "net_1_1":
85                                 compiler = root_mcs + "\\class\\lib\\net_1_1\\mcs.exe";
86                                 break;
87                                 
88                         case "net_2_0_bootstrap":
89                                 compiler = root_mcs + "\\class\\lib\\net_2_0_bootstrap\\gmcs.exe";
90                                 break;
91                                 
92                         case "gmcs":
93                         case "mcs":
94                                 compiler = root_mcs + "\\mcs\\gmcs.exe";
95                                 break;
96                                 
97                         case "moonlight_bootstrap":
98                                 compiler = root_mcs + "\\class\\lib\\moonlight_bootstrap\\smcs.exe";
99                                 break;
100                                 
101                         case "moonlight_raw":
102                                 compiler = root_mcs + "\\class\\lib\\moonlight_raw\\smcs.exe";
103                                 break;
104
105                         default:
106                                 Console.WriteLine ("Unknown compiler configuration: {0}", profile.Substring (p+1));
107                                 return 1;
108
109                         }
110                         var paths = profile.Substring (0, p).Split (new char [] { '-' });
111                         StringBuilder sb = new StringBuilder ();
112                         foreach (string dir in paths){
113                                 if (sb.Length != 0)
114                                         sb.Append (";");
115                                 sb.Append (root_mcs + "\\class\\lib\\" + dir);
116                         }
117                         Environment.SetEnvironmentVariable ("MONO_PATH", sb.ToString ());
118
119                         Console.WriteLine ("Compiler: {0}", compiler);
120                         Console.WriteLine ("MONO_PATH: {0}", sb.ToString ());
121                         var pi = new ProcessStartInfo() {
122                                 FileName = mono_cmd,
123                                 WindowStyle = ProcessWindowStyle.Hidden,
124                                 Arguments = compiler + " " + String.Join (" ", args),
125                                 UseShellExecute = false
126                         };
127
128             try {
129                 var proc = Process.Start (pi);
130
131                 proc.WaitForExit ();
132                 return proc.ExitCode;
133             } catch (System.ComponentModel.Win32Exception){
134                 Console.Error.WriteLine ("Chances are, it did not find {0}", mono_cmd);
135                 throw;
136
137             }
138                 }
139         }
140 }