[aot] Set null uw_info for tramps without unwind info
[mono.git] / mcs / tools / sgen / sgen.cs
1 // 
2 // sgen.cs
3 //
4 // Authors:
5 //   Lluis Sanchez Gual (lluis@ximian.com)
6 //   Atsushi Enomoto (atsushi@ximian.com)
7 //
8 // Copyright (C) 2003 Ximian, Inc.
9 // Copyright (C) 2006 Novell, Inc.
10 //
11
12 using System;
13 using System.Xml.Serialization;
14 using System.IO;
15 using System.Reflection;
16 using System.Collections;
17 using System.Collections.Specialized;
18 using System.CodeDom.Compiler;
19
20 public class Driver
21 {
22         static int Main (string[] args)
23         {
24                 Driver d = new Driver();
25                 return d.Run (args);
26         }
27                 
28         string assembly;
29         ArrayList references = new ArrayList ();
30         ArrayList types;
31         string compilerOptions;
32         bool proxyTypes;
33         bool debug;
34         bool keep;
35         bool force;
36         string outDir;
37         bool help;
38         bool silent;
39         bool nologo;
40         bool verbose;
41         string unknownArg;
42         
43
44         public int Run (string[] args)
45         {
46                 ParseArgs (args);
47                 
48                 if (!nologo)
49                 {
50                         Console.WriteLine ("Mono Xml Serializer Generator Tool");
51                         Console.WriteLine ("Mono version " + Environment.Version);
52                         Console.WriteLine ();
53                 }
54                 
55                 if (unknownArg != null)
56                 {
57                         Console.WriteLine ("Unknown option: " + unknownArg);
58                         Console.WriteLine ();
59                         return 1;
60                 }
61                 
62                 if (help)
63                 {
64                         Console.WriteLine ("Usage: sgen [options]");
65                         Console.WriteLine ();
66                         return 0;
67                 }
68                 
69                 if (assembly == null) {
70                         Console.WriteLine ("Assembly name not provided");
71                         Console.WriteLine ();
72                         return 1;
73                 }
74                 
75                 Assembly asm = null;
76                 
77                 try {
78                         asm = Assembly.Load (assembly);
79                 }
80                 catch {}
81                 
82                 if (asm == null) {
83                         try {
84                                 asm = Assembly.LoadFrom (assembly);
85                         } catch {
86                                 Console.WriteLine ("Specified assembly cannot be loaded.");
87                                 Console.WriteLine ();
88                                 return 1;
89                         }
90                 }
91                 ArrayList userTypes = new ArrayList ();
92                 ArrayList maps = new ArrayList ();
93                 XmlReflectionImporter imp = new XmlReflectionImporter ();
94                 
95                 if (verbose)
96                         Console.WriteLine ("Generating serializer for the following types:");
97
98                 if (types == null) {
99                         foreach (Type t in asm.GetTypes ()) {
100                                 try {
101                                         maps.Add (imp.ImportTypeMapping (t));
102                                         userTypes.Add (t);
103                                         if (verbose)
104                                                 Console.WriteLine( " - " + t );
105                                 } catch (InvalidOperationException ex) {
106                                         if (verbose)
107                                                 Console.WriteLine (" - Warning: " + ex.Message);
108                                 } catch (NotImplementedException ex) {
109                                         if (verbose) {
110                                                 Console.WriteLine (" - Warning: ignoring '" + t + "'");
111                                                 Console.WriteLine ("   " + ex.Message);
112                                         }
113                                 }
114                         }
115                 } else {
116                         foreach (string type in types) {
117                                 try {
118                                         Type t = asm.GetType (type);
119                                         maps.Add (imp.ImportTypeMapping (t));
120                                         userTypes.Add (t);
121                                         if (verbose)
122                                                 Console.WriteLine (" - " + t);
123                                 } catch (InvalidOperationException ex) {
124                                         if (verbose)
125                                                 Console.WriteLine (" - Warning: " + ex.Message);
126                                 } catch (NotImplementedException ex) {
127                                         if (verbose) {
128                                                 Console.WriteLine (" - Warning: ignoring '" + type + "'");
129                                                 Console.WriteLine ("   " + ex.Message);
130                                         }
131                                 }
132                         }
133                 }
134
135                 if (verbose)
136                         Console.WriteLine ();
137                         
138                 CompilerParameters parameters = new CompilerParameters ();
139                 parameters.GenerateInMemory = false;
140                 parameters.IncludeDebugInformation = debug;
141                 parameters.ReferencedAssemblies.AddRange ((string[])references.ToArray(typeof(string)));
142                 parameters.TempFiles = new TempFileCollection (Environment.CurrentDirectory, keep);
143                 parameters.CompilerOptions = compilerOptions;
144                 
145                 string file = Path.GetFileNameWithoutExtension (asm.Location) + ".XmlSerializers.dll";
146                 if (outDir == null) outDir = Path.GetDirectoryName (asm.Location);
147                 parameters.OutputAssembly = Path.Combine (outDir, file);
148                 
149                 if (File.Exists (parameters.OutputAssembly) && !force) {
150                         Console.WriteLine ("Cannot generate assembly '" + parameters.OutputAssembly + "' because it already exist. Use /force option to overwrite the existing assembly");
151                         Console.WriteLine ();
152                         return 1;
153                 }
154                 
155                 XmlSerializer.GenerateSerializer (
156                                 (Type[]) userTypes.ToArray (typeof(Type)), 
157                                 (XmlTypeMapping[]) maps.ToArray (typeof(XmlTypeMapping)), 
158                                 parameters);
159                                 
160                 if (!silent) {
161                         Console.WriteLine ("Generated assembly: " + file);
162                         Console.WriteLine ();
163                 }
164                 
165                 return 0;
166         }
167
168         void ParseArgs (string[] args)
169         {
170                 foreach (string arg in args)
171                 {
172                         int index = arg.Length > 2 && arg [0] == '-' && arg [1] == '-' ? 2 : -1;
173                         index = index >= 0 ? index : arg.Length > 0 && arg [0] == '/' ? 1 : -1;
174                         if (index < 0)
175                         {
176                                 assembly = arg;
177                                 continue;
178                         }
179                         
180                         int i = arg.IndexOf (':', index);
181                         if (i == -1) i = arg.Length;
182                         string op = arg.Substring (index, i - index).ToLowerInvariant ();
183                         string param = (i < arg.Length - index) ? arg.Substring (i + 1) : "";
184                         if (op == "assembly" || op == "a") {
185                                 assembly = param;
186                         }
187                         else if (op == "type" || op == "t") {
188                                 if (types == null) types = new ArrayList ();
189                                 types.Add (param);
190                         }
191                         else if (op == "reference" || op == "r") {
192                                 references.Add (param);
193                         }
194                         else if (op == "compiler" || op == "c") {
195                                 compilerOptions = param;
196                         }
197                         else if (op == "proxytypes" || op == "p") {
198                                 proxyTypes = true;
199                         }
200                         else if (op == "debug" || op == "d") {
201                                 debug = true;
202                         }
203                         else if (op == "keep" || op == "k") {
204                                 keep = true;
205                         }
206                         else if (op == "force" || op == "f") {
207                                 force = true;
208                         }
209                         else if (op == "out" || op == "o") {
210                                 outDir = param;
211                         }
212                         else if (op == "?" || op == "help") {
213                                 help = true;
214                         }
215                         else if (op == "nologo" || op == "n") {
216                                 nologo = true;
217                         }
218                         else if (op == "silent" || op == "s") {
219                                 silent = true;
220                         }
221                         else if (op == "verbose" || op == "v") {
222                                 verbose = true;
223                         }
224                         else if (arg.StartsWith ("/") && (arg.EndsWith (".dll") || arg.EndsWith (".exe")) && arg.IndexOfAny (Path.InvalidPathChars) == -1)
225                         {
226                                 assembly = arg;
227                                 continue;
228                         }
229                         else {
230                                 unknownArg = arg;
231                                 return;
232                         }
233                 }
234         }
235 }
236