2 // settings.cs: All compiler settings
4 // Author: Miguel de Icaza (miguel@ximian.com)
5 // Ravi Pratap (ravi@ximian.com)
6 // Marek Safar (marek.safar@gmail.com)
9 // Dual licensed under the terms of the MIT X11 or GNU GPL
11 // Copyright 2001 Ximian, Inc (http://www.ximian.com)
12 // Copyright 2004-2008 Novell, Inc
13 // Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
16 using System.Collections.Generic;
19 using System.Globalization;
22 namespace Mono.CSharp {
24 public enum LanguageVersion
33 Default = LanguageVersion.V_5,
36 public enum RuntimeVersion
45 Library, Exe, Module, WinExe
58 public class CompilerSettings
61 public Platform Platform;
62 public string TargetExt;
63 public bool VerifyClsCompliance;
65 public LanguageVersion Version;
66 public bool EnhancedWarnings;
67 public bool LoadDefaultReferences;
68 public string SdkVersion;
70 public string StrongNameKeyFile;
71 public string StrongNameKeyContainer;
72 public bool StrongNameDelaySign;
76 public bool WarningsAreErrors;
77 public int WarningLevel;
80 // Assemblies references to be loaded
82 public List<string> AssemblyReferences;
85 // External aliases for assemblies
87 public List<Tuple<string, string>> AssemblyReferencesAliases;
90 // Modules to be embedded
92 public List<string> Modules;
95 // Lookup paths for referenced assemblies
97 public List<string> ReferencesLookupPaths;
102 public Encoding Encoding;
105 // If set, enable XML documentation generation
107 public string DocumentationFile;
109 public string MainClass;
114 public string OutputFile;
117 // The default compiler checked state
122 // If true, the compiler is operating in statement mode,
123 // this currently turns local variable declaration into
124 // static variables of a class
126 public bool StatementMode; // TODO: SUPER UGLY
129 // Whether to allow Unsafe code
133 public string Win32ResourceFile;
134 public string Win32IconFile;
137 // A list of resource files for embedding
139 public List<AssemblyResource> Resources;
141 public bool GenerateDebugInfo;
143 #region Compiler debug flags only
144 public bool ParseOnly, TokenizeOnly, Timestamps;
145 public int DebugFlags;
146 public int VerboseParserFlag;
147 public int FatalCounter;
148 public bool Stacktrace;
151 public bool ShowFullPaths;
154 // Whether we are being linked against the standard libraries.
155 // This is only used to tell whether `System.Object' should
156 // have a base class or not.
160 public RuntimeVersion StdLibRuntimeVersion;
162 public bool WriteMetadataOnly;
164 readonly List<string> conditional_symbols;
166 readonly List<SourceFile> source_files;
168 List<int> warnings_as_error;
169 List<int> warnings_only;
170 HashSet<int> warning_ignore_table;
172 public CompilerSettings ()
177 Platform = Platform.AnyCPU;
178 Version = LanguageVersion.Default;
179 VerifyClsCompliance = true;
180 Encoding = Encoding.UTF8;
181 LoadDefaultReferences = true;
182 StdLibRuntimeVersion = RuntimeVersion.v4;
185 // Default to 1 or mdb files would be platform speficic
188 AssemblyReferences = new List<string> ();
189 AssemblyReferencesAliases = new List<Tuple<string, string>> ();
190 Modules = new List<string> ();
191 ReferencesLookupPaths = new List<string> ();
193 conditional_symbols = new List<string> ();
195 // Add default mcs define
197 conditional_symbols.Add ("__MonoCS__");
199 source_files = new List<SourceFile> ();
204 public SourceFile FirstSourceFile {
206 return source_files.Count > 0 ? source_files [0] : null;
210 public bool HasKeyFileOrContainer {
212 return StrongNameKeyFile != null || StrongNameKeyContainer != null;
216 public bool NeedsEntryPoint {
218 return Target == Target.Exe || Target == Target.WinExe;
222 public List<SourceFile> SourceFiles {
230 public void AddConditionalSymbol (string symbol)
232 if (!conditional_symbols.Contains (symbol))
233 conditional_symbols.Add (symbol);
236 public void AddWarningAsError (int id)
238 if (warnings_as_error == null)
239 warnings_as_error = new List<int> ();
241 warnings_as_error.Add (id);
244 public void AddWarningOnly (int id)
246 if (warnings_only == null)
247 warnings_only = new List<int> ();
249 warnings_only.Add (id);
252 public bool IsConditionalSymbolDefined (string symbol)
254 return conditional_symbols.Contains (symbol);
257 public bool IsWarningAsError (int code)
259 bool is_error = WarningsAreErrors;
261 // Check specific list
262 if (warnings_as_error != null)
263 is_error |= warnings_as_error.Contains (code);
265 // Ignore excluded warnings
266 if (warnings_only != null && warnings_only.Contains (code))
272 public bool IsWarningEnabled (int code, int level)
274 if (WarningLevel < level)
277 return !IsWarningDisabledGlobally (code);
280 public bool IsWarningDisabledGlobally (int code)
282 return warning_ignore_table != null && warning_ignore_table.Contains (code);
285 public void SetIgnoreWarning (int code)
287 if (warning_ignore_table == null)
288 warning_ignore_table = new HashSet<int> ();
290 warning_ignore_table.Add (code);
294 public class CommandLineParser
304 static readonly char[] argument_value_separator = new char[] { ';', ',' };
305 static readonly char[] numeric_value_separator = new char[] { ';', ',', ' ' };
307 readonly TextWriter output;
308 readonly Report report;
311 Dictionary<string, int> source_file_index;
313 public event Func<string[], int, int> UnknownOptionHandler;
315 CompilerSettings parser_settings;
317 public CommandLineParser (TextWriter errorOutput)
318 : this (errorOutput, Console.Out)
322 public CommandLineParser (TextWriter errorOutput, TextWriter messagesOutput)
324 var rp = new StreamReportPrinter (errorOutput);
326 parser_settings = new CompilerSettings ();
327 report = new Report (new CompilerContext (parser_settings, rp), rp);
328 this.output = messagesOutput;
331 public bool HasBeenStopped {
333 return stop_argument;
340 "The Mono C# compiler is Copyright 2001-2011, Novell, Inc.\n\n" +
341 "The compiler source code is released under the terms of the \n" +
342 "MIT X11 or GNU GPL licenses\n\n" +
344 "For more information on Mono, visit the project Web site\n" +
345 " http://www.mono-project.com\n\n" +
347 "The compiler was written by Miguel de Icaza, Ravi Pratap, Martin Baulig, Marek Safar, Raja R Harinath, Atushi Enomoto");
350 public CompilerSettings ParseArguments (string[] args)
352 CompilerSettings settings = new CompilerSettings ();
353 List<string> response_file_list = null;
354 bool parsing_options = true;
355 stop_argument = false;
356 source_file_index = new Dictionary<string, int> ();
358 for (int i = 0; i < args.Length; i++) {
359 string arg = args[i];
365 string response_file = arg.Substring (1);
367 if (response_file_list == null)
368 response_file_list = new List<string> ();
370 if (response_file_list.Contains (response_file)) {
371 report.Error (1515, "Response file `{0}' specified multiple times", response_file);
375 response_file_list.Add (response_file);
377 extra_args = LoadArgs (response_file);
378 if (extra_args == null) {
379 report.Error (2011, "Unable to open response file: " + response_file);
383 args = AddArgs (args, extra_args);
387 if (parsing_options) {
389 parsing_options = false;
393 bool dash_opt = arg[0] == '-';
394 bool slash_opt = arg[0] == '/';
396 switch (ParseOptionUnix (arg, ref args, ref i, settings)) {
397 case ParseResult.Error:
398 case ParseResult.Success:
400 case ParseResult.Stop:
401 stop_argument = true;
403 case ParseResult.UnknownOption:
404 if (UnknownOptionHandler != null) {
405 var ret = UnknownOptionHandler (args, i);
415 if (dash_opt || slash_opt) {
417 string csc_opt = dash_opt ? "/" + arg.Substring (1) : arg;
418 switch (ParseOption (csc_opt, ref args, settings)) {
419 case ParseResult.Error:
420 case ParseResult.Success:
422 case ParseResult.UnknownOption:
423 // Need to skip `/home/test.cs' however /test.cs is considered as error
424 if ((slash_opt && arg.Length > 3 && arg.IndexOf ('/', 2) > 0))
427 if (UnknownOptionHandler != null) {
428 var ret = UnknownOptionHandler (args, i);
435 Error_WrongOption (arg);
438 case ParseResult.Stop:
439 stop_argument = true;
445 ProcessSourceFiles (arg, false, settings.SourceFiles);
448 if (report.Errors > 0)
454 void ProcessSourceFiles (string spec, bool recurse, List<SourceFile> sourceFiles)
456 string path, pattern;
458 SplitPathAndPattern (spec, out path, out pattern);
459 if (pattern.IndexOf ('*') == -1) {
460 AddSourceFile (spec, sourceFiles);
464 string[] files = null;
466 files = Directory.GetFiles (path, pattern);
467 } catch (System.IO.DirectoryNotFoundException) {
468 report.Error (2001, "Source file `" + spec + "' could not be found");
470 } catch (System.IO.IOException) {
471 report.Error (2001, "Source file `" + spec + "' could not be found");
474 foreach (string f in files) {
475 AddSourceFile (f, sourceFiles);
481 string[] dirs = null;
484 dirs = Directory.GetDirectories (path);
488 foreach (string d in dirs) {
490 // Don't include path in this string, as each
491 // directory entry already does
492 ProcessSourceFiles (d + "/" + pattern, true, sourceFiles);
496 static string[] AddArgs (string[] args, string[] extra_args)
499 new_args = new string[extra_args.Length + args.Length];
501 // if args contains '--' we have to take that into account
502 // split args into first half and second half based on '--'
503 // and add the extra_args before --
504 int split_position = Array.IndexOf (args, "--");
505 if (split_position != -1) {
506 Array.Copy (args, new_args, split_position);
507 extra_args.CopyTo (new_args, split_position);
508 Array.Copy (args, split_position, new_args, split_position + extra_args.Length, args.Length - split_position);
510 args.CopyTo (new_args, 0);
511 extra_args.CopyTo (new_args, args.Length);
517 void AddAssemblyReference (string alias, string assembly, CompilerSettings settings)
519 if (assembly.Length == 0) {
520 report.Error (1680, "Invalid reference alias `{0}='. Missing filename", alias);
524 if (!IsExternAliasValid (alias)) {
525 report.Error (1679, "Invalid extern alias for -reference. Alias `{0}' is not a valid identifier", alias);
529 settings.AssemblyReferencesAliases.Add (Tuple.Create (alias, assembly));
532 void AddResource (AssemblyResource res, CompilerSettings settings)
534 if (settings.Resources == null) {
535 settings.Resources = new List<AssemblyResource> ();
536 settings.Resources.Add (res);
540 if (settings.Resources.Contains (res)) {
541 report.Error (1508, "The resource identifier `{0}' has already been used in this assembly", res.Name);
545 settings.Resources.Add (res);
548 void AddSourceFile (string fileName, List<SourceFile> sourceFiles)
550 string path = Path.GetFullPath (fileName);
553 if (source_file_index.TryGetValue (path, out index)) {
554 string other_name = sourceFiles[index - 1].Name;
555 if (fileName.Equals (other_name))
556 report.Warning (2002, 1, "Source file `{0}' specified multiple times", other_name);
558 report.Warning (2002, 1, "Source filenames `{0}' and `{1}' both refer to the same file: {2}", fileName, other_name, path);
563 var unit = new SourceFile (fileName, path, sourceFiles.Count + 1);
564 sourceFiles.Add (unit);
565 source_file_index.Add (path, unit.Index);
568 public bool ProcessWarningsList (string text, Action<int> action)
571 foreach (string wid in text.Split (numeric_value_separator)) {
573 if (!int.TryParse (wid, NumberStyles.AllowLeadingWhite, CultureInfo.InvariantCulture, out id)) {
574 report.Error (1904, "`{0}' is not a valid warning number", wid);
579 if (report.CheckWarningCode (id, Location.Null))
586 void Error_RequiresArgument (string option)
588 report.Error (2006, "Missing argument for `{0}' option", option);
591 void Error_RequiresFileName (string option)
593 report.Error (2005, "Missing file specification for `{0}' option", option);
596 void Error_WrongOption (string option)
598 report.Error (2007, "Unrecognized command-line option: `{0}'", option);
601 static bool IsExternAliasValid (string identifier)
603 return Tokenizer.IsValidIdentifier (identifier);
606 static string[] LoadArgs (string file)
609 var args = new List<string> ();
612 f = new StreamReader (file);
617 StringBuilder sb = new StringBuilder ();
619 while ((line = f.ReadLine ()) != null) {
622 for (int i = 0; i < t; i++) {
625 if (c == '"' || c == '\'') {
628 for (i++; i < t; i++) {
635 } else if (c == ' ') {
637 args.Add (sb.ToString ());
644 args.Add (sb.ToString ());
649 return args.ToArray ();
655 "Other flags in the compiler\n" +
656 " --fatal[=COUNT] Makes error after COUNT fatal\n" +
657 " --lint Enhanced warnings\n" +
658 " --metadata-only Produced assembly will contain metadata only\n" +
659 " --parse Only parses the source file\n" +
660 " --runtime:VERSION Sets mscorlib.dll metadata version: v1, v2, v4\n" +
661 " --stacktrace Shows stack trace at error location\n" +
662 " --timestamp Displays time stamps of various compiler events\n" +
663 " -v Verbose parsing (for debugging the parser)\n" +
664 " --mcs-debug X Sets MCS debugging level to X\n");
668 // This parses the -arg and /arg options to the compiler, even if the strings
669 // in the following text use "/arg" on the strings.
671 ParseResult ParseOption (string option, ref string[] args, CompilerSettings settings)
673 int idx = option.IndexOf (':');
680 arg = option.Substring (0, idx);
682 value = option.Substring (idx + 1);
685 switch (arg.ToLowerInvariant ()) {
687 return ParseResult.Success;
693 settings.Target = Target.Exe;
697 settings.Target = Target.WinExe;
701 settings.Target = Target.Library;
702 settings.TargetExt = ".dll";
706 settings.Target = Target.Module;
707 settings.TargetExt = ".netmodule";
711 report.Error (2019, "Invalid target type for -target. Valid options are `exe', `winexe', `library' or `module'");
712 return ParseResult.Error;
714 return ParseResult.Success;
717 if (value.Length == 0) {
718 Error_RequiresFileName (option);
719 return ParseResult.Error;
721 settings.OutputFile = value;
722 return ParseResult.Success;
728 settings.Optimize = true;
729 return ParseResult.Success;
733 settings.Optimize = false;
734 return ParseResult.Success;
736 // TODO: Not supported by csc 3.5+
738 case "/incremental+":
739 case "/incremental-":
741 return ParseResult.Success;
745 if (value.Length == 0) {
746 Error_RequiresArgument (option);
747 return ParseResult.Error;
750 foreach (string d in value.Split (argument_value_separator)) {
751 string conditional = d.Trim ();
752 if (!Tokenizer.IsValidIdentifier (conditional)) {
753 report.Warning (2029, 1, "Invalid conditional define symbol `{0}'", conditional);
757 settings.AddConditionalSymbol (conditional);
759 return ParseResult.Success;
764 // We should collect data, runtime, etc and store in the file specified
766 output.WriteLine ("To file bug reports, please visit: http://www.mono-project.com/Bugs");
767 return ParseResult.Success;
772 if (value.Length == 0) {
773 Error_RequiresArgument (option);
774 return ParseResult.Error;
776 packages = String.Join (" ", value.Split (new Char[] { ';', ',', '\n', '\r' }));
777 string pkgout = Driver.GetPackageFlags (packages, report);
780 return ParseResult.Error;
782 string[] xargs = pkgout.Trim (new Char[] { ' ', '\n', '\r', '\t' }).Split (new Char[] { ' ', '\t' });
783 args = AddArgs (args, xargs);
784 return ParseResult.Success;
788 case "/linkresource":
791 AssemblyResource res = null;
792 string[] s = value.Split (argument_value_separator, StringSplitOptions.RemoveEmptyEntries);
795 if (s[0].Length == 0)
797 res = new AssemblyResource (s[0], Path.GetFileName (s[0]));
800 res = new AssemblyResource (s[0], s[1]);
803 if (s[2] != "public" && s[2] != "private") {
804 report.Error (1906, "Invalid resource visibility option `{0}'. Use either `public' or `private' instead", s[2]);
805 return ParseResult.Error;
807 res = new AssemblyResource (s[0], s[1], s[2] == "private");
810 report.Error (-2005, "Wrong number of arguments for option `{0}'", option);
811 return ParseResult.Error;
815 res.IsEmbeded = arg[1] == 'r' || arg[1] == 'R';
816 AddResource (res, settings);
819 return ParseResult.Success;
822 if (value.Length == 0) {
823 Error_RequiresFileName (option);
824 return ParseResult.Error;
826 ProcessSourceFiles (value, true, settings.SourceFiles);
827 return ParseResult.Success;
831 if (value.Length == 0) {
832 Error_RequiresFileName (option);
833 return ParseResult.Error;
836 string[] refs = value.Split (argument_value_separator);
837 foreach (string r in refs) {
842 int index = val.IndexOf ('=');
844 string alias = r.Substring (0, index);
845 string assembly = r.Substring (index + 1);
846 AddAssemblyReference (alias, assembly, settings);
847 if (refs.Length != 1) {
848 report.Error (2034, "Cannot specify multiple aliases using single /reference option");
849 return ParseResult.Error;
852 settings.AssemblyReferences.Add (val);
855 return ParseResult.Success;
858 if (value.Length == 0) {
859 Error_RequiresFileName (option);
860 return ParseResult.Error;
863 string[] refs = value.Split (argument_value_separator);
864 foreach (string r in refs) {
865 settings.Modules.Add (r);
867 return ParseResult.Success;
870 if (value.Length == 0) {
871 Error_RequiresFileName (option);
872 return ParseResult.Error;
875 if (settings.Win32IconFile != null)
876 report.Error (1565, "Cannot specify the `win32res' and the `win32ico' compiler option at the same time");
878 settings.Win32ResourceFile = value;
879 return ParseResult.Success;
882 if (value.Length == 0) {
883 Error_RequiresFileName (option);
884 return ParseResult.Error;
887 if (settings.Win32ResourceFile != null)
888 report.Error (1565, "Cannot specify the `win32res' and the `win32ico' compiler option at the same time");
890 settings.Win32IconFile = value;
891 return ParseResult.Success;
894 if (value.Length == 0) {
895 Error_RequiresFileName (option);
896 return ParseResult.Error;
899 settings.DocumentationFile = value;
900 return ParseResult.Success;
905 if (value.Length == 0) {
906 return ParseResult.Error;
909 libdirs = value.Split (argument_value_separator);
910 foreach (string dir in libdirs)
911 settings.ReferencesLookupPaths.Add (dir);
912 return ParseResult.Success;
916 settings.GenerateDebugInfo = false;
917 return ParseResult.Success;
920 if (value.Equals ("full", StringComparison.OrdinalIgnoreCase) || value.Equals ("pdbonly", StringComparison.OrdinalIgnoreCase) || idx < 0) {
921 settings.GenerateDebugInfo = true;
922 return ParseResult.Success;
925 if (value.Length > 0) {
926 report.Error (1902, "Invalid debug option `{0}'. Valid options are `full' or `pdbonly'", value);
928 Error_RequiresArgument (option);
931 return ParseResult.Error;
934 settings.GenerateDebugInfo = true;
935 return ParseResult.Success;
939 settings.Checked = true;
940 return ParseResult.Success;
943 settings.Checked = false;
944 return ParseResult.Success;
948 settings.VerifyClsCompliance = true;
949 return ParseResult.Success;
952 settings.VerifyClsCompliance = false;
953 return ParseResult.Success;
957 settings.Unsafe = true;
958 return ParseResult.Success;
961 settings.Unsafe = false;
962 return ParseResult.Success;
965 case "/warnaserror+":
966 if (value.Length == 0) {
967 settings.WarningsAreErrors = true;
968 parser_settings.WarningsAreErrors = true;
970 if (!ProcessWarningsList (value, v => settings.AddWarningAsError (v)))
971 return ParseResult.Error;
973 return ParseResult.Success;
975 case "/warnaserror-":
976 if (value.Length == 0) {
977 settings.WarningsAreErrors = false;
979 if (!ProcessWarningsList (value, v => settings.AddWarningOnly (v)))
980 return ParseResult.Error;
982 return ParseResult.Success;
986 if (value.Length == 0) {
987 Error_RequiresArgument (option);
988 return ParseResult.Error;
991 SetWarningLevel (value, settings);
992 return ParseResult.Success;
995 if (value.Length == 0) {
996 Error_RequiresArgument (option);
997 return ParseResult.Error;
1000 if (!ProcessWarningsList (value, v => settings.SetIgnoreWarning (v)))
1001 return ParseResult.Error;
1003 return ParseResult.Success;
1006 settings.LoadDefaultReferences = false;
1007 return ParseResult.Success;
1010 if (value.Length == 0) {
1011 Error_RequiresArgument (option);
1012 return ParseResult.Error;
1015 switch (value.ToLowerInvariant ()) {
1017 settings.Platform = Platform.Arm;
1020 settings.Platform = Platform.AnyCPU;
1023 settings.Platform = Platform.X86;
1026 settings.Platform = Platform.X64;
1029 settings.Platform = Platform.IA64;
1031 case "anycpu32bitpreferred":
1032 settings.Platform = Platform.AnyCPU32Preferred;
1035 report.Error (1672, "Invalid -platform option `{0}'. Valid options are `anycpu', `anycpu32bitpreferred', `arm', `x86', `x64' or `itanium'",
1037 return ParseResult.Error;
1040 return ParseResult.Success;
1043 if (value.Length == 0) {
1044 Error_RequiresArgument (option);
1045 return ParseResult.Error;
1048 settings.SdkVersion = value;
1049 return ParseResult.Success;
1051 // We just ignore this.
1052 case "/errorreport":
1054 if (value.Length == 0) {
1055 Error_RequiresArgument (option);
1056 return ParseResult.Error;
1059 return ParseResult.Success;
1061 case "/helpinternal":
1063 return ParseResult.Stop;
1068 return ParseResult.Stop;
1072 if (value.Length == 0) {
1073 Error_RequiresArgument (option);
1074 return ParseResult.Error;
1076 settings.MainClass = value;
1077 return ParseResult.Success;
1081 settings.StdLib = false;
1082 return ParseResult.Success;
1085 settings.StdLib = true;
1086 return ParseResult.Success;
1089 settings.ShowFullPaths = true;
1090 return ParseResult.Success;
1093 if (value.Length == 0) {
1094 Error_RequiresFileName (option);
1095 return ParseResult.Error;
1098 settings.StrongNameKeyFile = value;
1099 return ParseResult.Success;
1101 case "/keycontainer":
1102 if (value.Length == 0) {
1103 Error_RequiresArgument (option);
1104 return ParseResult.Error;
1107 settings.StrongNameKeyContainer = value;
1108 return ParseResult.Success;
1112 settings.StrongNameDelaySign = true;
1113 return ParseResult.Success;
1116 settings.StrongNameDelaySign = false;
1117 return ParseResult.Success;
1119 case "/langversion":
1120 if (value.Length == 0) {
1121 Error_RequiresArgument (option);
1122 return ParseResult.Error;
1125 switch (value.ToLowerInvariant ()) {
1127 settings.Version = LanguageVersion.ISO_1;
1128 return ParseResult.Success;
1130 settings.Version = LanguageVersion.Default;
1131 return ParseResult.Success;
1133 settings.Version = LanguageVersion.ISO_2;
1134 return ParseResult.Success;
1136 settings.Version = LanguageVersion.V_3;
1137 return ParseResult.Success;
1139 settings.Version = LanguageVersion.V_4;
1140 return ParseResult.Success;
1142 settings.Version = LanguageVersion.V_5;
1143 return ParseResult.Success;
1145 settings.Version = LanguageVersion.Future;
1146 return ParseResult.Success;
1149 report.Error (1617, "Invalid -langversion option `{0}'. It must be `ISO-1', `ISO-2', `3', `4', `5', `Default' or `Future'", value);
1150 return ParseResult.Error;
1153 if (value.Length == 0) {
1154 Error_RequiresArgument (option);
1155 return ParseResult.Error;
1160 settings.Encoding = Encoding.UTF8;
1163 settings.Encoding = Encoding.Default;
1167 settings.Encoding = Encoding.GetEncoding (int.Parse (value));
1169 report.Error (2016, "Code page `{0}' is invalid or not installed", value);
1171 return ParseResult.Error;
1173 return ParseResult.Success;
1176 return ParseResult.UnknownOption;
1181 // Currently handles the Unix-like command line options, but will be
1182 // deprecated in favor of the CSCParseOption, which will also handle the
1183 // options that start with a dash in the future.
1185 ParseResult ParseOptionUnix (string arg, ref string[] args, ref int i, CompilerSettings settings)
1189 settings.VerboseParserFlag++;
1190 return ParseResult.Success;
1194 return ParseResult.Stop;
1197 settings.ParseOnly = true;
1198 return ParseResult.Success;
1200 case "--main": case "-m":
1201 report.Warning (-29, 1, "Compatibility: Use -main:CLASS instead of --main CLASS or -m CLASS");
1202 if ((i + 1) >= args.Length){
1203 Error_RequiresArgument (arg);
1204 return ParseResult.Error;
1206 settings.MainClass = args[++i];
1207 return ParseResult.Success;
1210 report.Warning (-29, 1, "Compatibility: Use -unsafe instead of --unsafe");
1211 settings.Unsafe = true;
1212 return ParseResult.Success;
1214 case "/?": case "/h": case "/help":
1217 return ParseResult.Stop;
1220 report.Warning (-29, 1, "Compatibility: Use -d:SYMBOL instead of --define SYMBOL");
1221 if ((i + 1) >= args.Length){
1222 Error_RequiresArgument (arg);
1223 return ParseResult.Error;
1226 settings.AddConditionalSymbol (args [++i]);
1227 return ParseResult.Success;
1230 settings.TokenizeOnly = true;
1231 return ParseResult.Success;
1235 report.Warning (-29, 1, "Compatibility: Use -out:FILE instead of --output FILE or -o FILE");
1236 if ((i + 1) >= args.Length){
1237 Error_RequiresArgument (arg);
1238 return ParseResult.Error;
1240 settings.OutputFile = args[++i];
1241 return ParseResult.Success;
1244 report.Warning (-29, 1, "Compatibility: Use -checked instead of --checked");
1245 settings.Checked = true;
1246 return ParseResult.Success;
1248 case "--stacktrace":
1249 settings.Stacktrace = true;
1250 return ParseResult.Success;
1252 case "--linkresource":
1254 report.Warning (-29, 1, "Compatibility: Use -linkres:VALUE instead of --linkres VALUE");
1255 if ((i + 1) >= args.Length){
1256 Error_RequiresArgument (arg);
1257 return ParseResult.Error;
1260 AddResource (new AssemblyResource (args[++i], args[i]), settings);
1261 return ParseResult.Success;
1265 report.Warning (-29, 1, "Compatibility: Use -res:VALUE instead of --res VALUE");
1266 if ((i + 1) >= args.Length){
1267 Error_RequiresArgument (arg);
1268 return ParseResult.Error;
1271 AddResource (new AssemblyResource (args[++i], args[i], true), settings);
1272 return ParseResult.Success;
1275 report.Warning (-29, 1, "Compatibility: Use -target:KIND instead of --target KIND");
1276 if ((i + 1) >= args.Length){
1277 Error_RequiresArgument (arg);
1278 return ParseResult.Error;
1281 string type = args [++i];
1284 settings.Target = Target.Library;
1285 settings.TargetExt = ".dll";
1289 settings.Target = Target.Exe;
1293 settings.Target = Target.WinExe;
1297 settings.Target = Target.Module;
1298 settings.TargetExt = ".dll";
1301 report.Error (2019, "Invalid target type for -target. Valid options are `exe', `winexe', `library' or `module'");
1304 return ParseResult.Success;
1307 report.Warning (-29, 1, "Compatibility: Use -r:LIBRARY instead of -r library");
1308 if ((i + 1) >= args.Length){
1309 Error_RequiresArgument (arg);
1310 return ParseResult.Error;
1313 string val = args [++i];
1314 int idx = val.IndexOf ('=');
1316 string alias = val.Substring (0, idx);
1317 string assembly = val.Substring (idx + 1);
1318 AddAssemblyReference (alias, assembly, settings);
1319 return ParseResult.Success;
1322 settings.AssemblyReferences.Add (val);
1323 return ParseResult.Success;
1326 report.Warning (-29, 1, "Compatibility: Use -lib:ARG instead of --L arg");
1327 if ((i + 1) >= args.Length){
1328 Error_RequiresArgument (arg);
1329 return ParseResult.Error;
1331 settings.ReferencesLookupPaths.Add (args [++i]);
1332 return ParseResult.Success;
1335 settings.EnhancedWarnings = true;
1336 return ParseResult.Success;
1339 report.Warning (-29, 1, "Compatibility: Use -nostdlib instead of --nostdlib");
1340 settings.StdLib = false;
1341 return ParseResult.Success;
1344 report.Warning (-29, 1, "Compatibility: Use -nowarn instead of --nowarn");
1345 if ((i + 1) >= args.Length){
1346 Error_RequiresArgument (arg);
1347 return ParseResult.Error;
1352 warn = int.Parse (args [++i]);
1355 Environment.Exit (1);
1357 settings.SetIgnoreWarning (warn);
1358 return ParseResult.Success;
1361 report.Warning (-29, 1, "Compatibility: Use -warn:LEVEL instead of --wlevel LEVEL");
1362 if ((i + 1) >= args.Length){
1363 Error_RequiresArgument (arg);
1364 return ParseResult.Error;
1367 SetWarningLevel (args [++i], settings);
1368 return ParseResult.Success;
1371 if ((i + 1) >= args.Length){
1372 Error_RequiresArgument (arg);
1373 return ParseResult.Error;
1377 settings.DebugFlags = int.Parse (args [++i]);
1379 Error_RequiresArgument (arg);
1380 return ParseResult.Error;
1383 return ParseResult.Success;
1387 return ParseResult.Stop;
1390 report.Warning (-29, 1, "Compatibility: Use -recurse:PATTERN option instead --recurse PATTERN");
1391 if ((i + 1) >= args.Length){
1392 Error_RequiresArgument (arg);
1393 return ParseResult.Error;
1395 ProcessSourceFiles (args [++i], true, settings.SourceFiles);
1396 return ParseResult.Success;
1399 settings.Timestamps = true;
1400 return ParseResult.Success;
1402 case "--debug": case "-g":
1403 report.Warning (-29, 1, "Compatibility: Use -debug option instead of -g or --debug");
1404 settings.GenerateDebugInfo = true;
1405 return ParseResult.Success;
1408 report.Warning (-29, 1, "Compatibility: Use -noconfig option instead of --noconfig");
1409 settings.LoadDefaultReferences = false;
1410 return ParseResult.Success;
1412 case "--metadata-only":
1413 settings.WriteMetadataOnly = true;
1414 return ParseResult.Success;
1417 if (arg.StartsWith ("--fatal", StringComparison.Ordinal)){
1419 if (arg.StartsWith ("--fatal=", StringComparison.Ordinal))
1420 int.TryParse (arg.Substring (8), out fatal);
1422 settings.FatalCounter = fatal;
1423 return ParseResult.Success;
1425 if (arg.StartsWith ("--runtime:", StringComparison.Ordinal)) {
1426 string version = arg.Substring (10);
1431 settings.StdLibRuntimeVersion = RuntimeVersion.v1;
1435 settings.StdLibRuntimeVersion = RuntimeVersion.v2;
1439 settings.StdLibRuntimeVersion = RuntimeVersion.v4;
1442 return ParseResult.Success;
1445 return ParseResult.UnknownOption;
1449 void SetWarningLevel (string s, CompilerSettings settings)
1454 level = int.Parse (s);
1457 if (level < 0 || level > 4) {
1458 report.Error (1900, "Warning level must be in the range 0-4");
1461 settings.WarningLevel = level;
1465 // Given a path specification, splits the path from the file/pattern
1467 static void SplitPathAndPattern (string spec, out string path, out string pattern)
1469 int p = spec.LastIndexOf ('/');
1472 // Windows does not like /file.cs, switch that to:
1477 pattern = spec.Substring (1);
1479 path = spec.Substring (0, p);
1480 pattern = spec.Substring (p + 1);
1485 p = spec.LastIndexOf ('\\');
1487 path = spec.Substring (0, p);
1488 pattern = spec.Substring (p + 1);
1499 "Mono C# compiler, Copyright 2001-2011 Novell, Inc., Copyright 2011-2012 Xamarin, Inc\n" +
1500 "mcs [options] source-files\n" +
1501 " --about About the Mono C# compiler\n" +
1502 " -addmodule:M1[,Mn] Adds the module to the generated assembly\n" +
1503 " -checked[+|-] Sets default aritmetic overflow context\n" +
1504 " -clscheck[+|-] Disables CLS Compliance verifications\n" +
1505 " -codepage:ID Sets code page to the one in ID (number, utf8, reset)\n" +
1506 " -define:S1[;S2] Defines one or more conditional symbols (short: -d)\n" +
1507 " -debug[+|-], -g Generate debugging information\n" +
1508 " -delaysign[+|-] Only insert the public key into the assembly (no signing)\n" +
1509 " -doc:FILE Process documentation comments to XML file\n" +
1510 " -fullpaths Any issued error or warning uses absolute file path\n" +
1511 " -help Lists all compiler options (short: -?)\n" +
1512 " -keycontainer:NAME The key pair container used to sign the output assembly\n" +
1513 " -keyfile:FILE The key file used to strongname the ouput assembly\n" +
1514 " -langversion:TEXT Specifies language version: ISO-1, ISO-2, 3, 4, 5, Default or Future\n" +
1515 " -lib:PATH1[,PATHn] Specifies the location of referenced assemblies\n" +
1516 " -main:CLASS Specifies the class with the Main method (short: -m)\n" +
1517 " -noconfig Disables implicitly referenced assemblies\n" +
1518 " -nostdlib[+|-] Does not reference mscorlib.dll library\n" +
1519 " -nowarn:W1[,Wn] Suppress one or more compiler warnings\n" +
1520 " -optimize[+|-] Enables advanced compiler optimizations (short: -o)\n" +
1521 " -out:FILE Specifies output assembly name\n" +
1522 " -pkg:P1[,Pn] References packages P1..Pn\n" +
1523 " -platform:ARCH Specifies the target platform of the output assembly\n" +
1524 " ARCH can be one of: anycpu, anycpu32bitpreferred, arm,\n" +
1525 " x86, x64 or itanium. The default is anycpu.\n" +
1526 " -recurse:SPEC Recursively compiles files according to SPEC pattern\n" +
1527 " -reference:A1[,An] Imports metadata from the specified assembly (short: -r)\n" +
1528 " -reference:ALIAS=A Imports metadata using specified extern alias (short: -r)\n" +
1529 " -sdk:VERSION Specifies SDK version of referenced assemblies\n" +
1530 " VERSION can be one of: 2, 4, 4.5 (default) or a custom value\n" +
1531 " -target:KIND Specifies the format of the output assembly (short: -t)\n" +
1532 " KIND can be one of: exe, winexe, library, module\n" +
1533 " -unsafe[+|-] Allows to compile code which uses unsafe keyword\n" +
1534 " -warnaserror[+|-] Treats all warnings as errors\n" +
1535 " -warnaserror[+|-]:W1[,Wn] Treats one or more compiler warnings as errors\n" +
1536 " -warn:0-4 Sets warning level, the default is 4 (short -w:)\n" +
1537 " -helpinternal Shows internal and advanced compiler options\n" +
1540 " -linkresource:FILE[,ID] Links FILE as a resource (short: -linkres)\n" +
1541 " -resource:FILE[,ID] Embed FILE as a resource (short: -res)\n" +
1542 " -win32res:FILE Specifies Win32 resource file (.res)\n" +
1543 " -win32icon:FILE Use this icon for the output\n" +
1544 " @file Read response file for more options\n\n" +
1545 "Options can be of the form -option or /option");
1550 string version = System.Reflection.MethodBase.GetCurrentMethod ().DeclaringType.Assembly.GetName ().Version.ToString ();
1551 output.WriteLine ("Mono C# compiler version {0}", version);
1555 public class RootContext
1558 // Contains the parsed tree
1560 static ModuleContainer root;
1562 static public ModuleContainer ToplevelTypes {
1563 get { return root; }
1564 set { root = value; }