Graduate the csproj/solution to completed - you can now build with the toplevel net_4...
[mono.git] / msvc / scripts / genproj.cs
1 //
2 // Consumes the order.xml file that contains a list of all the assemblies to build
3 // and produces a solution and the csproj files for it
4 //
5 // Currently this hardcodes a set of assemblies to build, the net-4.x series, but 
6 // it can be extended to handle the command line tools.
7 //
8 // KNOWN ISSUES:
9 //    * This fails to find matches for "System" and "System.xml" when processing the
10 //      RabbitMQ executable, likely, because we do not process executables yet
11 //
12 //    * Has not been tested in a while with the command line tools
13 //
14 using System;
15 using System.IO;
16 using System.Collections.Generic;
17 using System.Text;
18 using System.Globalization;
19 using System.Xml.Linq;
20 using System.Xml.XPath;
21 using System.Linq;
22 using System.Xml;
23
24 public enum Target {
25         Library, Exe, Module, WinExe
26 }
27
28 public enum LanguageVersion {
29         ISO_1 = 1,
30         Default_MCS = 2,
31         ISO_2 = 3,
32         LINQ = 4,
33         Future = 5,
34         Default = LINQ
35 }
36
37 class SlnGenerator {
38         public static readonly string NewLine = "\r\n"; //Environment.NewLine; // "\n"; 
39         public SlnGenerator (string formatVersion = "2012")
40         {
41                 switch (formatVersion) {
42                 case "2008":
43                         this.header = MakeHeader ("10.00", "2008");
44                         break;
45                 default:
46                         this.header = MakeHeader ("12.00", "2012");
47                         break;
48                 }
49         }
50
51         const string project_start = "Project(\"{{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}}\") = \"{0}\", \"{1}\", \"{2}\""; // Note: No need to double up on {} around {2}
52         const string project_end = "EndProject";
53
54         public List<MsbuildGenerator.VsCsproj> libraries = new List<MsbuildGenerator.VsCsproj> ();
55         string header;
56
57         string MakeHeader (string formatVersion, string yearTag)
58         {
59                 return string.Format ("Microsoft Visual Studio Solution File, Format Version {0}" + NewLine + "# Visual Studio {1}", formatVersion, yearTag);
60         }
61
62         public void Add (MsbuildGenerator.VsCsproj vsproj)
63         {
64                 try {
65                         libraries.Add (vsproj);
66                 } catch (Exception ex) {
67                         Console.WriteLine (ex);
68                 }
69         }
70
71         public void Write (string filename)
72         {
73                 var fullPath = Path.GetDirectoryName (filename) + "/";
74                 
75                 using (var sln = new StreamWriter (filename)) {
76                         sln.WriteLine ();
77                         sln.WriteLine (header);
78                         foreach (var proj in libraries) {
79                                 var unixProjFile = proj.csProjFilename.Replace ("\\", "/");
80                                 var fullProjPath = Path.GetFullPath (unixProjFile);
81                                 sln.WriteLine (project_start, proj.library, MsbuildGenerator.GetRelativePath (fullPath, fullProjPath), proj.projectGuid);
82                                 sln.WriteLine (project_end);
83                         }
84                         sln.WriteLine ("Global");
85
86                         sln.WriteLine ("\tGlobalSection(SolutionConfigurationPlatforms) = preSolution");
87                         sln.WriteLine ("\t\tDebug|Any CPU = Debug|Any CPU");
88                         sln.WriteLine ("\t\tRelease|Any CPU = Release|Any CPU");
89                         sln.WriteLine ("\tEndGlobalSection");
90
91                         sln.WriteLine ("\tGlobalSection(ProjectConfigurationPlatforms) = postSolution");
92                         foreach (var proj in libraries) {
93                                 var guid = proj.projectGuid;
94                                 sln.WriteLine ("\t\t{0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU", guid);
95                                 sln.WriteLine ("\t\t{0}.Debug|Any CPU.Build.0 = Debug|Any CPU", guid);
96                                 sln.WriteLine ("\t\t{0}.Release|Any CPU.ActiveCfg = Release|Any CPU", guid);
97                                 sln.WriteLine ("\t\t{0}.Release|Any CPU.Build.0 = Release|Any CPU", guid);
98                         }
99                         sln.WriteLine ("\tEndGlobalSection");
100
101                         sln.WriteLine ("\tGlobalSection(SolutionProperties) = preSolution");
102                         sln.WriteLine ("\t\tHideSolutionNode = FALSE");
103                         sln.WriteLine ("\tEndGlobalSection");
104
105                         sln.WriteLine ("EndGlobal");
106                 }
107         }
108
109         internal bool ContainsProjectIdentifier (string projId)
110         {
111                 return libraries.FindIndex (x => (x.library == projId)) >= 0;
112         }
113
114         public int Count { get { return libraries.Count; } }
115 }
116
117 class MsbuildGenerator {
118         static readonly string NewLine = SlnGenerator.NewLine;
119         static XmlNamespaceManager xmlns;
120
121         public const string profile_2_0 = "_2_0";
122         public const string profile_3_5 = "_3_5";
123         public const string profile_4_0 = "_4_0";
124         public const string profile_4_x = "_4_x";
125
126         static void Usage ()
127         {
128                 Console.WriteLine ("Invalid argument");
129         }
130
131         static string template;
132         static MsbuildGenerator ()
133         {
134                 using (var input = new StreamReader ("csproj.tmpl")) {
135                         template = input.ReadToEnd ();
136                 }
137
138                 xmlns = new XmlNamespaceManager (new NameTable ());
139                 xmlns.AddNamespace ("x", "http://schemas.microsoft.com/developer/msbuild/2003");
140         }
141
142         // The directory as specified in order.xml
143         public string dir;
144         string library;
145         string projectGuid;
146         string fx_version;
147
148         XElement xproject;
149         public string CsprojFilename;
150
151         //
152         // Our base directory, this is relative to our exectution point mono/msvc/scripts
153         string base_dir;
154         string mcs_topdir;
155
156         public string LibraryOutput, AbsoluteLibraryOutput;
157
158         public MsbuildGenerator (XElement xproject)
159         {
160                 this.xproject = xproject;
161                 dir = xproject.Attribute ("dir").Value;
162                 library = xproject.Attribute ("library").Value;
163                 CsprojFilename = "..\\..\\mcs\\" + dir + "\\" + library + ".csproj";
164                 LibraryOutput = xproject.Element ("library_output").Value;
165
166                 projectGuid = LookupOrGenerateGuid ();
167                 fx_version = xproject.Element ("fx_version").Value;
168                 Csproj = new VsCsproj () {
169                         csProjFilename = this.CsprojFilename,
170                         projectGuid = this.projectGuid,
171                         library_output = this.LibraryOutput,
172                         fx_version = double.Parse (fx_version),
173                         library = this.library,
174                         MsbuildGenerator = this
175                 };
176
177                 if (dir == "mcs") {
178                         mcs_topdir = "../";
179                         class_dir = "../class/";
180                         base_dir = "../../mcs/mcs";
181                 } else {
182                         mcs_topdir = "../";
183
184                         foreach (char c in dir) {
185                                 if (c == '/')
186                                         mcs_topdir = "..//" + mcs_topdir;
187                         }
188                         class_dir = mcs_topdir.Substring (3);
189
190                         base_dir = Path.Combine ("..", "..", "mcs", dir);
191                 }
192                 AbsoluteLibraryOutput = Path.GetFullPath (Path.Combine (base_dir, LibraryOutput));
193         }
194
195         string LookupOrGenerateGuid ()
196         {
197                 var projectFile = NativeName (CsprojFilename);
198                 if (File.Exists (projectFile)){
199                         var doc = XDocument.Load (projectFile);
200                         return doc.XPathSelectElement ("x:Project/x:PropertyGroup/x:ProjectGuid", xmlns).Value;
201                 }
202                 return "{" + Guid.NewGuid ().ToString ().ToUpper () + "}";
203         }
204
205         // Currently used
206         bool Unsafe = false;
207         StringBuilder defines = new StringBuilder ();
208         bool Optimize = true;
209         bool want_debugging_support = false;
210         string main = null;
211         Dictionary<string, string> embedded_resources = new Dictionary<string, string> ();
212         List<string> warning_as_error = new List<string> ();
213         List<int> ignore_warning = new List<int> ();
214         bool load_default_config = true;
215         bool StdLib = true;
216         List<string> references = new List<string> ();
217         List<string> libs = new List<string> ();
218         List<string> reference_aliases = new List<string> ();
219         bool showWarnings = true;
220
221         // Currently unused
222 #pragma warning disable 0219, 0414
223         int WarningLevel = 4;
224
225         bool Checked = false;
226         bool WarningsAreErrors;
227         bool VerifyClsCompliance = true;
228         string win32IconFile;
229         string StrongNameKeyFile;
230         bool copyLocal = true;
231         Target Target = Target.Library;
232         string TargetExt = ".exe";
233         string OutputFile;
234         string StrongNameKeyContainer;
235         bool StrongNameDelaySign = false;
236         LanguageVersion Version = LanguageVersion.Default;
237         string CodePage;
238
239         // Class directory, relative to 
240         string class_dir;
241 #pragma warning restore 0219,414
242
243         readonly char [] argument_value_separator = new char [] { ';', ',' };
244
245         //
246         // This parses the -arg and /arg options to the compiler, even if the strings
247         // in the following text use "/arg" on the strings.
248         //
249         bool CSCParseOption (string option, ref string [] args)
250         {
251                 int idx = option.IndexOf (':');
252                 string arg, value;
253
254                 if (idx == -1) {
255                         arg = option;
256                         value = "";
257                 } else {
258                         arg = option.Substring (0, idx);
259
260                         value = option.Substring (idx + 1);
261                 }
262
263                 switch (arg.ToLower (CultureInfo.InvariantCulture)) {
264                 case "/nologo":
265                         return true;
266
267                 case "/t":
268                 case "/target":
269                         switch (value) {
270                         case "exe":
271                                 Target = Target.Exe;
272                                 break;
273
274                         case "winexe":
275                                 Target = Target.WinExe;
276                                 break;
277
278                         case "library":
279                                 Target = Target.Library;
280                                 TargetExt = ".dll";
281                                 break;
282
283                         case "module":
284                                 Target = Target.Module;
285                                 TargetExt = ".netmodule";
286                                 break;
287
288                         default:
289                                 return false;
290                         }
291                         return true;
292
293                 case "/out":
294                         if (value.Length == 0) {
295                                 Usage ();
296                                 Environment.Exit (1);
297                         }
298                         OutputFile = value;
299                         return true;
300
301                 case "/o":
302                 case "/o+":
303                 case "/optimize":
304                 case "/optimize+":
305                         Optimize = true;
306                         return true;
307
308                 case "/o-":
309                 case "/optimize-":
310                         Optimize = false;
311                         return true;
312
313                 case "/incremental":
314                 case "/incremental+":
315                 case "/incremental-":
316                         // nothing.
317                         return true;
318
319                 case "/d":
320                 case "/define": {
321                                 if (value.Length == 0) {
322                                         Usage ();
323                                         Environment.Exit (1);
324                                 }
325
326                                 foreach (string d in value.Split (argument_value_separator)) {
327                                         if (defines.Length != 0)
328                                                 defines.Append (";");
329                                         defines.Append (d);
330                                 }
331
332                                 return true;
333                         }
334
335                 case "/bugreport":
336                         //
337                         // We should collect data, runtime, etc and store in the file specified
338                         //
339                         return true;
340                 case "/linkres":
341                 case "/linkresource":
342                 case "/res":
343                 case "/resource":
344                         bool embeded = arg [1] == 'r' || arg [1] == 'R';
345                         string [] s = value.Split (argument_value_separator);
346                         switch (s.Length) {
347                         case 1:
348                                 if (s [0].Length == 0)
349                                         goto default;
350                                 embedded_resources [s [0]] = Path.GetFileName (s [0]);
351                                 break;
352                         case 2:
353                                 embedded_resources [s [0]] = s [1];
354                                 break;
355                         case 3:
356                                 Console.WriteLine ("Does not support this method yet: {0}", arg);
357                                 Environment.Exit (1);
358                                 break;
359                         default:
360                                 Console.WriteLine ("Wrong number of arguments for option `{0}'", option);
361                                 Environment.Exit (1);
362                                 break;
363                         }
364
365                         return true;
366
367                 case "/recurse":
368                         Console.WriteLine ("/recurse not supported");
369                         Environment.Exit (1);
370                         return true;
371
372                 case "/r":
373                 case "/reference": {
374                                 if (value.Length == 0) {
375                                         Console.WriteLine ("-reference requires an argument");
376                                         Environment.Exit (1);
377                                 }
378
379                                 string [] refs = value.Split (argument_value_separator);
380                                 foreach (string r in refs) {
381                                         string val = r;
382                                         int index = val.IndexOf ('=');
383                                         if (index > -1) {
384                                                 reference_aliases.Add (r);
385                                                 continue;
386                                         }
387
388                                         if (val.Length != 0)
389                                                 references.Add (val);
390                                 }
391                                 return true;
392                         }
393                 case "/main":
394                         main = value;
395                         return true;
396
397                 case "/m":
398                 case "/addmodule":
399                 case "/win32res":
400                 case "/doc": 
401                         if (showWarnings)
402                                 Console.WriteLine ("{0} = not supported", arg);
403                         return true;
404                         
405                 case "/lib": {
406                                 libs.Add (value);
407                                 return true;
408                         }
409                 case "/win32icon": {
410                                 win32IconFile = value;
411                                 return true;
412                         }
413                 case "/debug-":
414                         want_debugging_support = false;
415                         return true;
416
417                 case "/debug":
418                 case "/debug+":
419                         want_debugging_support = true;
420                         return true;
421
422                 case "/checked":
423                 case "/checked+":
424                         Checked = true;
425                         return true;
426
427                 case "/checked-":
428                         Checked = false;
429                         return true;
430
431                 case "/clscheck":
432                 case "/clscheck+":
433                         return true;
434
435                 case "/clscheck-":
436                         VerifyClsCompliance = false;
437                         return true;
438
439                 case "/unsafe":
440                 case "/unsafe+":
441                         Unsafe = true;
442                         return true;
443
444                 case "/unsafe-":
445                         Unsafe = false;
446                         return true;
447
448                 case "/warnaserror":
449                 case "/warnaserror+":
450                         if (value.Length == 0) {
451                                 WarningsAreErrors = true;
452                         } else {
453                                 foreach (string wid in value.Split (argument_value_separator))
454                                         warning_as_error.Add (wid);
455                         }
456                         return true;
457
458                 case "/-runtime":
459                         // Console.WriteLine ("Warning ignoring /runtime:v4");
460                         return true;
461
462                 case "/warnaserror-":
463                         if (value.Length == 0) {
464                                 WarningsAreErrors = false;
465                         } else {
466                                 foreach (string wid in value.Split (argument_value_separator))
467                                         warning_as_error.Remove (wid);
468                         }
469                         return true;
470
471                 case "/warn":
472                         WarningLevel = Int32.Parse (value);
473                         return true;
474
475                 case "/nowarn": {
476                                 string [] warns;
477
478                                 if (value.Length == 0) {
479                                         Console.WriteLine ("/nowarn requires an argument");
480                                         Environment.Exit (1);
481                                 }
482
483                                 warns = value.Split (argument_value_separator);
484                                 foreach (string wc in warns) {
485                                         try {
486                                                 if (wc.Trim ().Length == 0)
487                                                         continue;
488
489                                                 int warn = Int32.Parse (wc);
490                                                 if (warn < 1) {
491                                                         throw new ArgumentOutOfRangeException ("warn");
492                                                 }
493                                                 ignore_warning.Add (warn);
494                                         } catch {
495                                                 Console.WriteLine (String.Format ("`{0}' is not a valid warning number", wc));
496                                                 Environment.Exit (1);
497                                         }
498                                 }
499                                 return true;
500                         }
501
502                 case "/noconfig":
503                         load_default_config = false;
504                         return true;
505
506                 case "/nostdlib":
507                 case "/nostdlib+":
508                         StdLib = false;
509                         return true;
510
511                 case "/nostdlib-":
512                         StdLib = true;
513                         return true;
514
515                 case "/fullpaths":
516                         return true;
517
518                 case "/keyfile":
519                         if (value == String.Empty) {
520                                 Console.WriteLine ("{0} requires an argument", arg);
521                                 Environment.Exit (1);
522                         }
523                         StrongNameKeyFile = value;
524                         return true;
525                 case "/keycontainer":
526                         if (value == String.Empty) {
527                                 Console.WriteLine ("{0} requires an argument", arg);
528                                 Environment.Exit (1);
529                         }
530                         StrongNameKeyContainer = value;
531                         return true;
532                 case "/delaysign+":
533                 case "/delaysign":
534                         StrongNameDelaySign = true;
535                         return true;
536                 case "/delaysign-":
537                         StrongNameDelaySign = false;
538                         return true;
539
540                 case "/langversion":
541                         switch (value.ToLower (CultureInfo.InvariantCulture)) {
542                         case "iso-1":
543                                 Version = LanguageVersion.ISO_1;
544                                 return true;
545
546                         case "default":
547                                 Version = LanguageVersion.Default;
548                                 return true;
549                         case "iso-2":
550                                 Version = LanguageVersion.ISO_2;
551                                 return true;
552                         case "future":
553                                 Version = LanguageVersion.Future;
554                                 return true;
555                         }
556                         Console.WriteLine ("Invalid option `{0}' for /langversion. It must be either `ISO-1', `ISO-2' or `Default'", value);
557                         Environment.Exit (1);
558                         return true;
559
560                 case "/codepage":
561                         CodePage = value;
562                         return true;
563
564                 case "/publicsign":
565                         return true;
566                         
567                 case "/-getresourcestrings":
568                         return true;
569                 }
570
571                 Console.WriteLine ("Failing with : {0}", arg);
572                 return false;
573         }
574
575         static string [] LoadArgs (string file)
576         {
577                 StreamReader f;
578                 var args = new List<string> ();
579                 string line;
580                 try {
581                         f = new StreamReader (file);
582                 } catch {
583                         return null;
584                 }
585
586                 StringBuilder sb = new StringBuilder ();
587
588                 while ((line = f.ReadLine ()) != null) {
589                         int t = line.Length;
590
591                         for (int i = 0; i < t; i++) {
592                                 char c = line [i];
593
594                                 if (c == '"' || c == '\'') {
595                                         char end = c;
596
597                                         for (i++; i < t; i++) {
598                                                 c = line [i];
599
600                                                 if (c == end)
601                                                         break;
602                                                 sb.Append (c);
603                                         }
604                                 } else if (c == ' ') {
605                                         if (sb.Length > 0) {
606                                                 args.Add (sb.ToString ());
607                                                 sb.Length = 0;
608                                         }
609                                 } else
610                                         sb.Append (c);
611                         }
612                         if (sb.Length > 0) {
613                                 args.Add (sb.ToString ());
614                                 sb.Length = 0;
615                         }
616                 }
617
618                 string [] ret_value = new string [args.Count];
619                 args.CopyTo (ret_value, 0);
620
621                 return ret_value;
622         }
623
624         static string Load (string f)
625         {
626                 var native = NativeName (f);
627
628                 if (File.Exists (native)) {
629                         using (var sr = new StreamReader (native)) {
630                                 return sr.ReadToEnd ();
631                         }
632                 } else
633                         return "";
634         }
635
636         public static string NativeName (string path)
637         {
638                 if (System.IO.Path.DirectorySeparatorChar == '/')
639                         return path.Replace ("\\", "/");
640                 else
641                         return path.Replace ("/", "\\");
642         }
643
644         public class VsCsproj {
645                 public string projectGuid;
646                 public string output;
647                 public string library_output;
648                 public string csProjFilename;
649                 public double fx_version;
650                 public List<VsCsproj> projReferences = new List<VsCsproj> ();
651                 public string library;
652                 public MsbuildGenerator MsbuildGenerator;
653         }
654
655         public VsCsproj Csproj;
656
657         public VsCsproj Generate (Dictionary<string,MsbuildGenerator> projects, bool showWarnings = false)
658         {
659                 var generatedProjFile = NativeName (Csproj.csProjFilename);
660                 //Console.WriteLine ("Generating: {0}", generatedProjFile);
661
662                 string boot, flags, output_name, built_sources, response, profile;
663
664                 boot = xproject.Element ("boot").Value;
665                 flags = xproject.Element ("flags").Value;
666                 output_name = xproject.Element ("output").Value;
667                 if (output_name.EndsWith (".exe"))
668                         Target = Target.Exe;
669                 built_sources = xproject.Element ("built_sources").Value;
670                 response = xproject.Element ("response").Value;
671
672                 profile = xproject.Element ("profile").Value;
673                 if (string.IsNullOrEmpty (response)) {
674                         // Address the issue where entries are missing the fx_version
675                         // Should be fixed in the Makefile or elsewhere; this is a workaround
676                         //<fx_version>basic</fx_version>
677                         //<profile>./../build/deps/mcs.exe.sources.response</profile>
678                         //<response></response>
679                         response = profile;
680                         profile = fx_version;
681                         if (response.Contains ("build") || response.Contains ("basic") || response.Contains (profile_2_0)) {
682                                 fx_version = "2.0";
683                                 if (response.Contains (profile_2_0)) profile = "net_2_0";
684                         } if (response.Contains ("build") || response.Contains ("basic") || response.Contains (profile_2_0)) {
685                                 fx_version = "2.0";
686                         } else if (response.Contains (profile_3_5)) {
687                                 fx_version = "3.5";
688                                 profile = "net_3_5";
689                         } else if (response.Contains (profile_4_0)) {
690                                 fx_version = "4.0";
691                                 profile = "net_4_0";
692                         } else if (response.Contains (profile_4_x)) {
693                                 fx_version = "4.5";
694                                 profile = "net_4_x";
695                         }
696                 }
697                 //
698                 // Prebuild code, might be in inputs, check:
699                 //  inputs/LIBRARY-PROFILE.pre
700                 //  inputs/LIBRARY.pre
701                 //
702                 string prebuild = Load (library + ".pre");
703                 string prebuild_windows, prebuild_unix;
704                 
705                 int q = library.IndexOf ("-");
706                 if (q != -1)
707                         prebuild = prebuild + Load (library.Substring (0, q) + ".pre");
708
709                 if (prebuild.IndexOf ("@MONO@") != -1){
710                         prebuild_unix = prebuild.Replace ("@MONO@", "mono").Replace ("@CAT@", "cat");
711                         prebuild_windows = prebuild.Replace ("@MONO@", "").Replace ("@CAT@", "type");
712                 } else {
713                         prebuild_unix = prebuild.Replace ("jay.exe", "jay");
714                         prebuild_windows = prebuild;
715                 }
716                 
717                 const string condition_unix    = "Condition=\" '$(OS)' != 'Windows_NT' \"";
718                 const string condition_windows = "Condition=\" '$(OS)' == 'Windows_NT' \"";
719                 prebuild =
720                         "    <PreBuildEvent " + condition_unix + ">" + NewLine + prebuild_unix + NewLine + "    </PreBuildEvent>" + NewLine +
721                         "    <PreBuildEvent " + condition_windows + ">" + NewLine + prebuild_windows + NewLine + "    </PreBuildEvent>" + NewLine;
722
723                 var all_args = new Queue<string []> ();
724                 all_args.Enqueue (flags.Split ());
725                 while (all_args.Count > 0) {
726                         string [] f = all_args.Dequeue ();
727
728                         for (int i = 0; i < f.Length; i++) {
729                                 if (f [i].Length > 0 && f [i][0] == '-')
730                                         f [i] = "/" + f [i].Substring (1);
731                                 
732                                 if (f [i] [0] == '@') {
733                                         string [] extra_args;
734                                         string response_file = f [i].Substring (1);
735
736                                         var resp_file_full = Path.Combine (base_dir, response_file);
737                                         extra_args = LoadArgs (resp_file_full);
738                                         if (extra_args == null) {
739                                                 Console.WriteLine ("Unable to open response file: " + resp_file_full);
740                                                 Environment.Exit (1);
741                                         }
742
743                                         all_args.Enqueue (extra_args);
744                                         continue;
745                                 }
746
747                                 if (CSCParseOption (f [i], ref f))
748                                         continue;
749                                 Console.WriteLine ("Failure with {0}", f [i]);
750                                 Environment.Exit (1);
751                         }
752                 }
753
754                 string [] source_files;
755                 //Console.WriteLine ("Base: {0} res: {1}", base_dir, response);
756                 using (var reader = new StreamReader (NativeName (base_dir + "\\" + response))) {
757                         source_files = reader.ReadToEnd ().Split ();
758                 }
759
760                 Array.Sort (source_files);
761
762                 StringBuilder sources = new StringBuilder ();
763                 foreach (string s in source_files) {
764                         if (s.Length == 0)
765                                 continue;
766
767                         string src = s.Replace ("/", "\\");
768                         if (src.StartsWith (@"Test\..\"))
769                                 src = src.Substring (8, src.Length - 8);
770
771                         sources.AppendFormat ("    <Compile Include=\"{0}\" />" + NewLine, src);
772                 }
773
774                 source_files = built_sources.Split ();
775                 Array.Sort (source_files);
776
777                 foreach (string s in source_files) {
778                         if (s.Length == 0)
779                                 continue;
780
781                         string src = s.Replace ("/", "\\");
782                         if (src.StartsWith (@"Test\..\"))
783                                 src = src.Substring (8, src.Length - 8);
784
785                         sources.AppendFormat ("    <Compile Include=\"{0}\" />" + NewLine, src);
786                 }
787                 sources.Remove (sources.Length - 1, 1);
788
789                 //if (library == "corlib-build") // otherwise, does not compile on fx_version == 4.0
790                 //{
791                 //    references.Add("System.dll");
792                 //    references.Add("System.Xml.dll");
793                 //}
794
795                 //if (library == "System.Core-build") // otherwise, slow compile. May be a transient need.
796                 //{
797                 //    this.ignore_warning.Add(1685);
798                 //    this.ignore_warning.Add(0436);
799                 //}
800
801                 var refs = new StringBuilder ();
802
803                 bool is_test = response.Contains ("_test_");
804                 if (is_test) {
805                         // F:\src\mono\mcs\class\lib\net_2_0\nunit.framework.dll
806                         // F:\src\mono\mcs\class\SomeProject\SomeProject_test_-net_2_0.csproj
807                         var nunitLibPath = string.Format (@"..\lib\{0}\nunit.framework.dll", profile);
808                         refs.Append (string.Format ("    <Reference Include=\"{0}\" />" + NewLine, nunitLibPath));
809                 }
810
811                 var resources = new StringBuilder ();
812                 if (embedded_resources.Count > 0) {
813                         resources.AppendFormat ("  <ItemGroup>" + NewLine);
814                         foreach (var dk in embedded_resources) {
815                                 resources.AppendFormat ("    <EmbeddedResource Include=\"{0}\">" + NewLine, dk.Key);
816                                 resources.AppendFormat ("      <LogicalName>{0}</LogicalName>" + NewLine, dk.Value);
817                                 resources.AppendFormat ("    </EmbeddedResource>" + NewLine);
818                         }
819                         resources.AppendFormat ("  </ItemGroup>" + NewLine);
820                 }
821         
822
823                 if (references.Count > 0 || reference_aliases.Count > 0) {
824                         // -r:mscorlib.dll -r:System.dll
825                         //<ProjectReference Include="..\corlib\corlib-basic.csproj">
826                         //  <Project>{155aef28-c81f-405d-9072-9d52780e3e70}</Project>
827                         //  <Name>corlib-basic</Name>
828                         //</ProjectReference>
829                         //<ProjectReference Include="..\System\System-basic.csproj">
830                         //  <Project>{2094e859-db2f-481f-9630-f89d31d9ed48}</Project>
831                         //  <Name>System-basic</Name>
832                         //</ProjectReference>
833                         var refdistinct = references.Distinct ();
834                         foreach (string r in refdistinct) {
835                                 
836                                 var match = GetMatchingCsproj (r, projects);
837                                 if (match != null) {
838                                         AddProjectReference (refs, Csproj, match, r, null);
839                                 } else {
840                                         if (showWarnings){
841                                                 Console.WriteLine ("{0}: Could not find a matching project reference for {1}", library, Path.GetFileName (r));
842                                                 Console.WriteLine ("  --> Adding reference with hintpath instead");
843                                         }
844                                         refs.Append ("    <Reference Include=\"" + r + "\">" + NewLine);
845                                         refs.Append ("      <SpecificVersion>False</SpecificVersion>" + NewLine);
846                                         refs.Append ("      <HintPath>" + r + "</HintPath>" + NewLine);
847                                         refs.Append ("      <Private>False</Private>" + NewLine);
848                                         refs.Append ("    </Reference>" + NewLine);
849                                 }
850                         }
851
852                         foreach (string r in reference_aliases) {
853                                 int index = r.IndexOf ('=');
854                                 string alias = r.Substring (0, index);
855                                 string assembly = r.Substring (index + 1);
856                                 var match = GetMatchingCsproj (assembly, projects, explicitPath: true);
857                                 if (match != null) {
858                                         AddProjectReference (refs, Csproj, match, r, alias);
859                                 } else {
860                                         throw new NotSupportedException (string.Format ("From {0}, could not find a matching project reference for {1}", library, r));
861                                         refs.Append ("    <Reference Include=\"" + assembly + "\">" + NewLine);
862                                         refs.Append ("      <SpecificVersion>False</SpecificVersion>" + NewLine);
863                                         refs.Append ("      <HintPath>" + r + "</HintPath>" + NewLine);
864                                         refs.Append ("      <Aliases>" + alias + "</Aliases>" + NewLine);
865                                         refs.Append ("    </Reference>" + NewLine);
866
867                                 }
868                         }
869                 }
870
871                 // Possible inputs:
872                 // ../class/lib/build/tmp/System.Xml.dll  [No longer possible, we should be removing this from order.xml]
873                 //   /class/lib/basic/System.Core.dll
874                 // <library_output>mcs.exe</library_output>
875                 string build_output_dir;
876                 if (LibraryOutput.Contains ("/"))
877                         build_output_dir = Path.GetDirectoryName (LibraryOutput);
878                 else
879                         build_output_dir = "bin\\Debug\\" + library;
880                 
881
882                 string postbuild_unix = string.Empty;
883                 string postbuild_windows = string.Empty;
884
885                 var postbuild =  
886                         "    <PostBuildEvent " + condition_unix + ">" + NewLine + postbuild_unix + NewLine + "    </PostBuildEvent>" + NewLine +
887                         "    <PostBuildEvent " + condition_windows + ">" + NewLine + postbuild_windows + NewLine + "    </PostBuildEvent>";
888                         
889
890                 bool basic_or_build = (library.Contains ("-basic") || library.Contains ("-build"));
891
892                 //
893                 // Replace the template values
894                 //
895
896                 string strongNameSection = "";
897                 if (StrongNameKeyFile != null){
898                         strongNameSection = String.Format (
899                                 "  <PropertyGroup>" + NewLine +
900                                 "    <SignAssembly>true</SignAssembly>" + NewLine +
901                                 "{1}" +
902                                 "  </PropertyGroup>" + NewLine +
903                                 "  <PropertyGroup>" + NewLine +
904                                 "    <AssemblyOriginatorKeyFile>{0}</AssemblyOriginatorKeyFile>" + NewLine +
905                                 "  </PropertyGroup>", StrongNameKeyFile, StrongNameDelaySign ? "    <DelaySign>true</DelaySign>" + NewLine : "");
906                 }
907                 Csproj.output = template.
908                         Replace ("@OUTPUTTYPE@", Target == Target.Library ? "Library" : "Exe").
909                         Replace ("@SIGNATURE@", strongNameSection).
910                         Replace ("@PROJECTGUID@", Csproj.projectGuid).
911                         Replace ("@DEFINES@", defines.ToString ()).
912                         Replace ("@DISABLEDWARNINGS@", string.Join (",", (from i in ignore_warning select i.ToString ()).ToArray ())).
913                         //Replace("@NOSTDLIB@", (basic_or_build || (!StdLib)) ? "<NoStdLib>true</NoStdLib>" : string.Empty).
914                         Replace ("@NOSTDLIB@", "<NoStdLib>" + (!StdLib).ToString () + "</NoStdLib>").
915                         Replace ("@NOCONFIG@", "<NoConfig>" + (!load_default_config).ToString () + "</NoConfig>").
916                         Replace ("@ALLOWUNSAFE@", Unsafe ? "<AllowUnsafeBlocks>true</AllowUnsafeBlocks>" : "").
917                         Replace ("@FX_VERSION", fx_version).
918                         Replace ("@ASSEMBLYNAME@", Path.GetFileNameWithoutExtension (output_name)).
919                         Replace ("@OUTPUTDIR@", build_output_dir).
920                         Replace ("@DEFINECONSTANTS@", defines.ToString ()).
921                         Replace ("@DEBUG@", want_debugging_support ? "true" : "false").
922                         Replace ("@DEBUGTYPE@", want_debugging_support ? "full" : "pdbonly").
923                         Replace ("@REFERENCES@", refs.ToString ()).
924                         Replace ("@PREBUILD@", prebuild).
925                         Replace ("@STARTUPOBJECT@", main == null ? "" : $"<StartupObject>{main}</StartupObject>").
926                         Replace ("@POSTBUILD@", postbuild).
927                         //Replace ("@ADDITIONALLIBPATHS@", String.Format ("<AdditionalLibPaths>{0}</AdditionalLibPaths>", string.Join (",", libs.ToArray ()))).
928                         Replace ("@ADDITIONALLIBPATHS@", String.Empty).
929                         Replace ("@RESOURCES@", resources.ToString ()).
930                         Replace ("@OPTIMIZE@", Optimize ? "true" : "false").
931                         Replace ("@SOURCES@", sources.ToString ());
932
933                 //Console.WriteLine ("Generated {0}", ofile.Replace ("\\", "/"));
934                 using (var o = new StreamWriter (generatedProjFile)) {
935                         o.WriteLine (Csproj.output);
936                 }
937
938                 return Csproj;
939         }
940
941         void AddProjectReference (StringBuilder refs, VsCsproj result, MsbuildGenerator match, string r, string alias)
942         {
943                 refs.AppendFormat ("    <ProjectReference Include=\"{0}\">{1}", GetRelativePath (result.csProjFilename, match.CsprojFilename), NewLine);
944                 refs.Append ("      <Project>" + match.projectGuid + "</Project>" + NewLine);
945                 refs.Append ("      <Name>" + Path.GetFileNameWithoutExtension (match.CsprojFilename.Replace ('\\', Path.DirectorySeparatorChar)) + "</Name>" + NewLine);
946                 if (alias != null)
947                         refs.Append ("      <Aliases>" + alias + "</Aliases>");
948                 refs.Append ("    </ProjectReference>" + NewLine);
949                 if (!result.projReferences.Contains (match.Csproj))
950                         result.projReferences.Add (match.Csproj);
951         }
952
953         public static string GetRelativePath (string from, string to)
954         {
955                 from = from.Replace ("\\", "/");
956                 to = to.Replace ("\\", "/");
957                 var fromUri = new Uri (Path.GetFullPath (from));
958                 var toUri = new Uri (Path.GetFullPath (to));
959
960                 var ret =  fromUri.MakeRelativeUri (toUri).ToString ().Replace ("%5C", "\x5c");
961                 return ret;
962         }
963
964         MsbuildGenerator GetMatchingCsproj (string dllReferenceName, Dictionary<string,MsbuildGenerator> projects, bool explicitPath = false)
965         {
966                 // libDir would be "./../../class/lib/net_4_x for example
967                 // project 
968                 if (!dllReferenceName.EndsWith (".dll"))
969                         dllReferenceName += ".dll";
970
971                 var probe = Path.GetFullPath (Path.Combine (base_dir, dllReferenceName));
972                 foreach (var project in projects){
973                         if (probe == project.Value.AbsoluteLibraryOutput)
974                                 return project.Value;
975                 }
976
977                 // not explicit, search for the library in the lib path order specified
978
979                 foreach (var libDir in libs) {
980                         var abs = Path.GetFullPath (Path.Combine (base_dir, libDir));
981                         foreach (var project in projects){
982                                 probe = Path.Combine (abs, dllReferenceName);
983
984                                 if (probe == project.Value.AbsoluteLibraryOutput)
985                                         return project.Value;
986                         }
987                 }
988                 Console.WriteLine ("Did not find referenced {0} with libs={1}", dllReferenceName, String.Join (", ", libs));
989                 foreach (var p in projects) {
990                 //      Console.WriteLine ("{0}", p.Value.AbsoluteLibraryOutput);
991                 }
992                 return null;
993         }
994
995 }
996
997 public class Driver {
998
999         static IEnumerable<XElement> GetProjects (bool full = false)
1000         {
1001                 XDocument doc = XDocument.Load ("order.xml");
1002                 foreach (XElement project in doc.Root.Elements ()) {
1003                         string dir = project.Attribute ("dir").Value;
1004                         string library = project.Attribute ("library").Value;
1005                         var profile = project.Element ("profile").Value;
1006
1007                         // Skip facades for now, the tool doesn't know how to deal with them yet.
1008                         if (dir.Contains ("Facades"))
1009                                 continue;
1010
1011                         // These are currently broken, skip until they're fixed.
1012                         if (dir.StartsWith ("mcs") || dir.Contains ("apigen"))
1013                                 continue;
1014
1015                         //
1016                         // Do only class libraries for now
1017                         //
1018                         if (!(dir.StartsWith ("class") || dir.StartsWith ("mcs") || dir.StartsWith ("basic")))
1019                                 continue;
1020
1021                         if (full){
1022                                 if (!library.Contains ("tests"))
1023                                         yield return project;
1024                                 continue;
1025                         }
1026                         
1027                         //
1028                         // Do not do 2.1, it is not working yet
1029                         // Do not do basic, as there is no point (requires a system mcs to be installed).
1030                         //
1031                         if (library.Contains ("moonlight") || library.Contains ("-basic") || library.EndsWith ("bootstrap")  || library.Contains ("build"))
1032                                 continue;
1033
1034                         // The next ones are to make debugging easier for now
1035                         if (profile == "basic")
1036                                 continue;
1037                         if (profile != "net_4_x" || library.Contains ("tests"))
1038                                 continue;
1039
1040                         yield return project;
1041                 }
1042         }
1043
1044         static void Main (string [] args)
1045         {
1046                 if (!File.Exists ("genproj.cs")) {
1047                         Console.WriteLine ("This command must be executed from mono/msvc/scripts");
1048                         Environment.Exit (1);
1049                 }
1050
1051                 if (args.Length == 1 && args [0].ToLower ().Contains ("-h")) {
1052                         Console.WriteLine ("Usage:");
1053                         Console.WriteLine ("genproj.exe [visual_studio_release] [output_full_solutions]");
1054                         Console.WriteLine ("If output_full_solutions is false, only the main System*.dll");
1055                         Console.WriteLine (" assemblies (and dependencies) is included in the solution.");
1056                         Console.WriteLine ("Example:");
1057                         Console.WriteLine ("genproj.exe 2012 false");
1058                         Console.WriteLine ("genproj.exe with no arguments is equivalent to 'genproj.exe 2012 true'\n\n");
1059                         Console.WriteLine ("genproj.exe deps");
1060                         Console.WriteLine ("Generates a Makefile dependency file from the projects input");
1061                         Environment.Exit (0);
1062                 }
1063
1064                 var slnVersion = (args.Length > 0) ? args [0] : "2012";
1065                 bool fullSolutions = (args.Length > 1) ? bool.Parse (args [1]) : true;
1066
1067                 // To generate makefile depenedencies
1068                 var makefileDeps =  (args.Length > 0 && args [0] == "deps");
1069
1070                 var sln_gen = new SlnGenerator (slnVersion);
1071                 var four_five_sln_gen = new SlnGenerator (slnVersion);
1072                 var projects = new Dictionary<string,MsbuildGenerator> ();
1073
1074                 var duplicates = new List<string> ();
1075                 foreach (var project in GetProjects (makefileDeps)) {
1076                         var library_output = project.Element ("library_output").Value;
1077                         projects [library_output] = new MsbuildGenerator (project);
1078                 }
1079                 foreach (var project in GetProjects (makefileDeps)){
1080                         var library_output = project.Element ("library_output").Value;
1081                         var gen = projects [library_output];
1082                         try {
1083                                 var csproj = gen.Generate (projects);
1084                                 var csprojFilename = csproj.csProjFilename;
1085                                 if (!sln_gen.ContainsProjectIdentifier (csproj.library)) {
1086                                         sln_gen.Add (csproj);
1087                                 } else {
1088                                         duplicates.Add (csprojFilename);
1089                                 }
1090                                 
1091                         } catch (Exception e) {
1092                                 Console.WriteLine ("Error in {0}\n{1}", project, e);
1093                         }
1094                 }
1095
1096                 Func<MsbuildGenerator.VsCsproj, bool> additionalFilter;
1097                 additionalFilter = fullSolutions ? (Func<MsbuildGenerator.VsCsproj, bool>)null : IsCommonLibrary;
1098
1099                 FillSolution (four_five_sln_gen, MsbuildGenerator.profile_4_x, projects.Values, additionalFilter);
1100
1101                 if (duplicates.Count () > 0) {
1102                         var sb = new StringBuilder ();
1103                         sb.AppendLine ("WARNING: Skipped some project references, apparent duplicates in order.xml:");
1104                         foreach (var item in duplicates) {
1105                                 sb.AppendLine (item);
1106                         }
1107                         Console.WriteLine (sb.ToString ());
1108                 }
1109
1110                 WriteSolution (four_five_sln_gen, Path.Combine ("..", "..", MakeSolutionName (MsbuildGenerator.profile_4_x)));
1111
1112                 if (makefileDeps){
1113                         const string classDirPrefix = "./../../";
1114                         Console.WriteLine ("here {0}", sln_gen.libraries.Count);
1115                         foreach (var p in sln_gen.libraries){
1116                                 string rebasedOutput = RebaseToClassDirectory (MsbuildGenerator.GetRelativePath ("../../mcs/class", p.library_output));
1117                                 
1118                                 Console.Write ("{0}: ", rebasedOutput);
1119                                 foreach (var r in p.projReferences){
1120                                         var lo = r.library_output;
1121                                         if (lo.StartsWith (classDirPrefix))
1122                                                 lo = lo.Substring (classDirPrefix.Length);
1123                                         else
1124                                                 lo = "<<ERROR-dependency is not a class library>>";
1125                                         Console.Write ("{0} ", lo);
1126                                 }
1127                                 Console.Write ("\n\t(cd {0}; make {1})", p.MsbuildGenerator.dir, p.library_output);
1128                                 Console.WriteLine ("\n");
1129                         }
1130                 }
1131                 
1132                 // A few other optional solutions
1133                 // Solutions with 'everything' and the most common libraries used in development may be of interest
1134                 //WriteSolution (sln_gen, "mcs_full.sln");
1135                 //WriteSolution (small_full_sln_gen, "small_full.sln");
1136                 // The following may be useful if lacking visual studio or MonoDevelop, to bootstrap mono compiler self-hosting
1137                 //WriteSolution (basic_sln_gen, "mcs_basic.sln");
1138                 //WriteSolution (build_sln_gen, "mcs_build.sln");
1139         }
1140
1141         // Rebases a path, assuming that execution is taking place in the "class" subdirectory,
1142         // so it strips ../class/ from a path, which is a no-op
1143         static string RebaseToClassDirectory (string path)
1144         {
1145                 const string prefix = "../class/";
1146                 int p = path.IndexOf (prefix);
1147                 if (p == -1)
1148                         return path;
1149                 return path.Substring (0, p) + path.Substring (p+prefix.Length);
1150                 return path;
1151         }
1152         
1153         static string MakeSolutionName (string profileTag)
1154         {
1155                 return "net" + profileTag + ".sln";
1156         }
1157
1158         static void FillSolution (SlnGenerator solution, string profileString, IEnumerable<MsbuildGenerator> projects, Func<MsbuildGenerator.VsCsproj, bool> additionalFilter = null)
1159         {
1160                 foreach (var generator in projects) {
1161                         var vsCsproj = generator.Csproj;
1162                         if (!vsCsproj.library.Contains (profileString))
1163                                 continue;
1164                         if (additionalFilter != null && !additionalFilter (vsCsproj))
1165                                 continue;
1166                         var csprojFilename = vsCsproj.csProjFilename;
1167                         if (!solution.ContainsProjectIdentifier (vsCsproj.library)) {
1168                                 solution.Add (vsCsproj);
1169                                 RecursiveAddProj (solution, vsCsproj);
1170                         }
1171                 }
1172         }
1173
1174         static void RecursiveAddProj (SlnGenerator solution, MsbuildGenerator.VsCsproj vsCsproj, int recursiveDepth = 1)
1175         {
1176                 const int max_recursive = 16;
1177                 if (recursiveDepth > max_recursive) throw new Exception (string.Format ("Reached {0} levels of project dependency", max_recursive));
1178                 foreach (var projRef in vsCsproj.projReferences) {
1179                         if (!solution.ContainsProjectIdentifier (projRef.library)) {
1180                                 solution.Add (projRef);
1181                                 RecursiveAddProj (solution, projRef, recursiveDepth + 1);
1182                         }
1183                 }
1184         }
1185
1186         static void WriteSolution (SlnGenerator sln_gen, string slnfilename)
1187         {
1188                 Console.WriteLine (String.Format ("Writing solution {1}, with {0} projects", sln_gen.Count, slnfilename));
1189                 sln_gen.Write (slnfilename);
1190         }
1191
1192         static bool IsCommonLibrary (MsbuildGenerator.VsCsproj proj)
1193         {
1194                 var library = proj.library;
1195                 //if (library.Contains ("-basic"))
1196                 //      return true;
1197                 //if (library.Contains ("-build"))
1198                 //      return true;
1199                 //if (library.StartsWith ("corlib"))
1200                 //      return true;
1201                 if (library.StartsWith ("System-"))
1202                         return true;
1203                 if (library.StartsWith ("System.Xml"))
1204                         return true;
1205                 if (library.StartsWith ("System.Secu"))
1206                         return true;
1207                 if (library.StartsWith ("System.Configuration"))
1208                         return true;
1209                 if (library.StartsWith ("System.Core"))
1210                         return true;
1211                 //if (library.StartsWith ("Mono."))
1212                 //      return true;
1213
1214                 return false;
1215         }
1216 }