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 = { ';', ',' };
305 static readonly char[] numeric_value_separator = { ';', ',', ' ' };
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 if (!ParseArguments (settings, args))
359 public bool ParseArguments (CompilerSettings settings, string[] args)
361 if (settings == null)
362 throw new ArgumentNullException ("settings");
364 List<string> response_file_list = null;
365 bool parsing_options = true;
366 stop_argument = false;
367 source_file_index = new Dictionary<string, int> ();
369 for (int i = 0; i < args.Length; i++) {
370 string arg = args[i];
376 string response_file = arg.Substring (1);
378 if (response_file_list == null)
379 response_file_list = new List<string> ();
381 if (response_file_list.Contains (response_file)) {
382 report.Error (1515, "Response file `{0}' specified multiple times", response_file);
386 response_file_list.Add (response_file);
388 extra_args = LoadArgs (response_file);
389 if (extra_args == null) {
390 report.Error (2011, "Unable to open response file: " + response_file);
394 args = AddArgs (args, extra_args);
398 if (parsing_options) {
400 parsing_options = false;
404 bool dash_opt = arg[0] == '-';
405 bool slash_opt = arg[0] == '/';
407 switch (ParseOptionUnix (arg, ref args, ref i, settings)) {
408 case ParseResult.Error:
409 case ParseResult.Success:
411 case ParseResult.Stop:
412 stop_argument = true;
414 case ParseResult.UnknownOption:
415 if (UnknownOptionHandler != null) {
416 var ret = UnknownOptionHandler (args, i);
426 if (dash_opt || slash_opt) {
428 string csc_opt = dash_opt ? "/" + arg.Substring (1) : arg;
429 switch (ParseOption (csc_opt, ref args, settings)) {
430 case ParseResult.Error:
431 case ParseResult.Success:
433 case ParseResult.UnknownOption:
434 // Need to skip `/home/test.cs' however /test.cs is considered as error
435 if ((slash_opt && arg.Length > 3 && arg.IndexOf ('/', 2) > 0))
438 if (UnknownOptionHandler != null) {
439 var ret = UnknownOptionHandler (args, i);
446 Error_WrongOption (arg);
449 case ParseResult.Stop:
450 stop_argument = true;
456 ProcessSourceFiles (arg, false, settings.SourceFiles);
459 return report.Errors == 0;
462 void ProcessSourceFiles (string spec, bool recurse, List<SourceFile> sourceFiles)
464 string path, pattern;
466 SplitPathAndPattern (spec, out path, out pattern);
467 if (pattern.IndexOf ('*') == -1) {
468 AddSourceFile (spec, sourceFiles);
474 files = Directory.GetFiles (path, pattern);
475 } catch (System.IO.DirectoryNotFoundException) {
476 report.Error (2001, "Source file `" + spec + "' could not be found");
478 } catch (System.IO.IOException) {
479 report.Error (2001, "Source file `" + spec + "' could not be found");
482 foreach (string f in files) {
483 AddSourceFile (f, sourceFiles);
489 string[] dirs = null;
492 dirs = Directory.GetDirectories (path);
496 foreach (string d in dirs) {
498 // Don't include path in this string, as each
499 // directory entry already does
500 ProcessSourceFiles (d + "/" + pattern, true, sourceFiles);
504 static string[] AddArgs (string[] args, string[] extra_args)
507 new_args = new string[extra_args.Length + args.Length];
509 // if args contains '--' we have to take that into account
510 // split args into first half and second half based on '--'
511 // and add the extra_args before --
512 int split_position = Array.IndexOf (args, "--");
513 if (split_position != -1) {
514 Array.Copy (args, new_args, split_position);
515 extra_args.CopyTo (new_args, split_position);
516 Array.Copy (args, split_position, new_args, split_position + extra_args.Length, args.Length - split_position);
518 args.CopyTo (new_args, 0);
519 extra_args.CopyTo (new_args, args.Length);
525 void AddAssemblyReference (string alias, string assembly, CompilerSettings settings)
527 if (assembly.Length == 0) {
528 report.Error (1680, "Invalid reference alias `{0}='. Missing filename", alias);
532 if (!IsExternAliasValid (alias)) {
533 report.Error (1679, "Invalid extern alias for -reference. Alias `{0}' is not a valid identifier", alias);
537 settings.AssemblyReferencesAliases.Add (Tuple.Create (alias, assembly));
540 void AddResource (AssemblyResource res, CompilerSettings settings)
542 if (settings.Resources == null) {
543 settings.Resources = new List<AssemblyResource> ();
544 settings.Resources.Add (res);
548 if (settings.Resources.Contains (res)) {
549 report.Error (1508, "The resource identifier `{0}' has already been used in this assembly", res.Name);
553 settings.Resources.Add (res);
556 void AddSourceFile (string fileName, List<SourceFile> sourceFiles)
558 string path = Path.GetFullPath (fileName);
561 if (source_file_index.TryGetValue (path, out index)) {
562 string other_name = sourceFiles[index - 1].Name;
563 if (fileName.Equals (other_name))
564 report.Warning (2002, 1, "Source file `{0}' specified multiple times", other_name);
566 report.Warning (2002, 1, "Source filenames `{0}' and `{1}' both refer to the same file: {2}", fileName, other_name, path);
571 var unit = new SourceFile (fileName, path, sourceFiles.Count + 1);
572 sourceFiles.Add (unit);
573 source_file_index.Add (path, unit.Index);
576 public bool ProcessWarningsList (string text, Action<int> action)
579 foreach (string wid in text.Split (numeric_value_separator, StringSplitOptions.RemoveEmptyEntries)) {
581 if (!int.TryParse (wid, NumberStyles.AllowLeadingWhite, CultureInfo.InvariantCulture, out id)) {
582 report.Error (1904, "`{0}' is not a valid warning number", wid);
587 if (report.CheckWarningCode (id, Location.Null))
594 void Error_RequiresArgument (string option)
596 report.Error (2006, "Missing argument for `{0}' option", option);
599 void Error_RequiresFileName (string option)
601 report.Error (2005, "Missing file specification for `{0}' option", option);
604 void Error_WrongOption (string option)
606 report.Error (2007, "Unrecognized command-line option: `{0}'", option);
609 static bool IsExternAliasValid (string identifier)
611 return Tokenizer.IsValidIdentifier (identifier);
614 static string[] LoadArgs (string file)
617 var args = new List<string> ();
620 f = new StreamReader (file);
625 StringBuilder sb = new StringBuilder ();
627 while ((line = f.ReadLine ()) != null) {
630 for (int i = 0; i < t; i++) {
633 if (c == '"' || c == '\'') {
636 for (i++; i < t; i++) {
643 } else if (c == ' ') {
645 args.Add (sb.ToString ());
652 args.Add (sb.ToString ());
657 return args.ToArray ();
663 "Other flags in the compiler\n" +
664 " --fatal[=COUNT] Makes error after COUNT fatal\n" +
665 " --lint Enhanced warnings\n" +
666 " --metadata-only Produced assembly will contain metadata only\n" +
667 " --parse Only parses the source file\n" +
668 " --runtime:VERSION Sets mscorlib.dll metadata version: v1, v2, v4\n" +
669 " --stacktrace Shows stack trace at error location\n" +
670 " --timestamp Displays time stamps of various compiler events\n" +
671 " -v Verbose parsing (for debugging the parser)\n" +
672 " --mcs-debug X Sets MCS debugging level to X\n");
676 // This parses the -arg and /arg options to the compiler, even if the strings
677 // in the following text use "/arg" on the strings.
679 ParseResult ParseOption (string option, ref string[] args, CompilerSettings settings)
681 int idx = option.IndexOf (':');
688 arg = option.Substring (0, idx);
690 value = option.Substring (idx + 1);
693 switch (arg.ToLowerInvariant ()) {
695 return ParseResult.Success;
701 settings.Target = Target.Exe;
705 settings.Target = Target.WinExe;
709 settings.Target = Target.Library;
710 settings.TargetExt = ".dll";
714 settings.Target = Target.Module;
715 settings.TargetExt = ".netmodule";
719 report.Error (2019, "Invalid target type for -target. Valid options are `exe', `winexe', `library' or `module'");
720 return ParseResult.Error;
722 return ParseResult.Success;
725 if (value.Length == 0) {
726 Error_RequiresFileName (option);
727 return ParseResult.Error;
729 settings.OutputFile = value;
730 return ParseResult.Success;
736 settings.Optimize = true;
737 return ParseResult.Success;
741 settings.Optimize = false;
742 return ParseResult.Success;
744 // TODO: Not supported by csc 3.5+
746 case "/incremental+":
747 case "/incremental-":
749 return ParseResult.Success;
753 if (value.Length == 0) {
754 Error_RequiresArgument (option);
755 return ParseResult.Error;
758 foreach (string d in value.Split (argument_value_separator)) {
759 string conditional = d.Trim ();
760 if (!Tokenizer.IsValidIdentifier (conditional)) {
761 report.Warning (2029, 1, "Invalid conditional define symbol `{0}'", conditional);
765 settings.AddConditionalSymbol (conditional);
767 return ParseResult.Success;
772 // We should collect data, runtime, etc and store in the file specified
774 output.WriteLine ("To file bug reports, please visit: http://www.mono-project.com/Bugs");
775 return ParseResult.Success;
780 if (value.Length == 0) {
781 Error_RequiresArgument (option);
782 return ParseResult.Error;
784 packages = String.Join (" ", value.Split (new Char[] { ';', ',', '\n', '\r' }));
785 string pkgout = Driver.GetPackageFlags (packages, report);
788 return ParseResult.Error;
790 string[] xargs = pkgout.Trim (new Char[] { ' ', '\n', '\r', '\t' }).Split (new Char[] { ' ', '\t' });
791 args = AddArgs (args, xargs);
792 return ParseResult.Success;
796 case "/linkresource":
799 AssemblyResource res = null;
800 string[] s = value.Split (argument_value_separator, StringSplitOptions.RemoveEmptyEntries);
803 if (s[0].Length == 0)
805 res = new AssemblyResource (s[0], Path.GetFileName (s[0]));
808 res = new AssemblyResource (s[0], s[1]);
811 if (s[2] != "public" && s[2] != "private") {
812 report.Error (1906, "Invalid resource visibility option `{0}'. Use either `public' or `private' instead", s[2]);
813 return ParseResult.Error;
815 res = new AssemblyResource (s[0], s[1], s[2] == "private");
818 report.Error (-2005, "Wrong number of arguments for option `{0}'", option);
819 return ParseResult.Error;
823 res.IsEmbeded = arg[1] == 'r' || arg[1] == 'R';
824 AddResource (res, settings);
827 return ParseResult.Success;
830 if (value.Length == 0) {
831 Error_RequiresFileName (option);
832 return ParseResult.Error;
834 ProcessSourceFiles (value, true, settings.SourceFiles);
835 return ParseResult.Success;
839 if (value.Length == 0) {
840 Error_RequiresFileName (option);
841 return ParseResult.Error;
844 string[] refs = value.Split (argument_value_separator);
845 foreach (string r in refs) {
850 int index = val.IndexOf ('=');
852 string alias = r.Substring (0, index);
853 string assembly = r.Substring (index + 1);
854 AddAssemblyReference (alias, assembly, settings);
855 if (refs.Length != 1) {
856 report.Error (2034, "Cannot specify multiple aliases using single /reference option");
857 return ParseResult.Error;
860 settings.AssemblyReferences.Add (val);
863 return ParseResult.Success;
866 if (value.Length == 0) {
867 Error_RequiresFileName (option);
868 return ParseResult.Error;
871 string[] refs = value.Split (argument_value_separator);
872 foreach (string r in refs) {
873 settings.Modules.Add (r);
875 return ParseResult.Success;
878 if (value.Length == 0) {
879 Error_RequiresFileName (option);
880 return ParseResult.Error;
883 if (settings.Win32IconFile != null)
884 report.Error (1565, "Cannot specify the `win32res' and the `win32ico' compiler option at the same time");
886 settings.Win32ResourceFile = value;
887 return ParseResult.Success;
890 if (value.Length == 0) {
891 Error_RequiresFileName (option);
892 return ParseResult.Error;
895 if (settings.Win32ResourceFile != null)
896 report.Error (1565, "Cannot specify the `win32res' and the `win32ico' compiler option at the same time");
898 settings.Win32IconFile = value;
899 return ParseResult.Success;
902 if (value.Length == 0) {
903 Error_RequiresFileName (option);
904 return ParseResult.Error;
907 settings.DocumentationFile = value;
908 return ParseResult.Success;
913 if (value.Length == 0) {
914 return ParseResult.Error;
917 libdirs = value.Split (argument_value_separator);
918 foreach (string dir in libdirs)
919 settings.ReferencesLookupPaths.Add (dir);
920 return ParseResult.Success;
924 settings.GenerateDebugInfo = false;
925 return ParseResult.Success;
928 if (value.Equals ("full", StringComparison.OrdinalIgnoreCase) || value.Equals ("pdbonly", StringComparison.OrdinalIgnoreCase) || idx < 0) {
929 settings.GenerateDebugInfo = true;
930 return ParseResult.Success;
933 if (value.Length > 0) {
934 report.Error (1902, "Invalid debug option `{0}'. Valid options are `full' or `pdbonly'", value);
936 Error_RequiresArgument (option);
939 return ParseResult.Error;
942 settings.GenerateDebugInfo = true;
943 return ParseResult.Success;
947 settings.Checked = true;
948 return ParseResult.Success;
951 settings.Checked = false;
952 return ParseResult.Success;
956 settings.VerifyClsCompliance = true;
957 return ParseResult.Success;
960 settings.VerifyClsCompliance = false;
961 return ParseResult.Success;
965 settings.Unsafe = true;
966 return ParseResult.Success;
969 settings.Unsafe = false;
970 return ParseResult.Success;
973 case "/warnaserror+":
974 if (value.Length == 0) {
975 settings.WarningsAreErrors = true;
976 parser_settings.WarningsAreErrors = true;
978 if (!ProcessWarningsList (value, settings.AddWarningAsError))
979 return ParseResult.Error;
981 return ParseResult.Success;
983 case "/warnaserror-":
984 if (value.Length == 0) {
985 settings.WarningsAreErrors = false;
987 if (!ProcessWarningsList (value, settings.AddWarningOnly))
988 return ParseResult.Error;
990 return ParseResult.Success;
994 if (value.Length == 0) {
995 Error_RequiresArgument (option);
996 return ParseResult.Error;
999 SetWarningLevel (value, settings);
1000 return ParseResult.Success;
1003 if (value.Length == 0) {
1004 Error_RequiresArgument (option);
1005 return ParseResult.Error;
1008 if (!ProcessWarningsList (value, settings.SetIgnoreWarning))
1009 return ParseResult.Error;
1011 return ParseResult.Success;
1014 settings.LoadDefaultReferences = false;
1015 return ParseResult.Success;
1018 if (value.Length == 0) {
1019 Error_RequiresArgument (option);
1020 return ParseResult.Error;
1023 switch (value.ToLowerInvariant ()) {
1025 settings.Platform = Platform.Arm;
1028 settings.Platform = Platform.AnyCPU;
1031 settings.Platform = Platform.X86;
1034 settings.Platform = Platform.X64;
1037 settings.Platform = Platform.IA64;
1039 case "anycpu32bitpreferred":
1040 settings.Platform = Platform.AnyCPU32Preferred;
1043 report.Error (1672, "Invalid -platform option `{0}'. Valid options are `anycpu', `anycpu32bitpreferred', `arm', `x86', `x64' or `itanium'",
1045 return ParseResult.Error;
1048 return ParseResult.Success;
1051 if (value.Length == 0) {
1052 Error_RequiresArgument (option);
1053 return ParseResult.Error;
1056 settings.SdkVersion = value;
1057 return ParseResult.Success;
1059 // We just ignore this.
1060 case "/errorreport":
1062 if (value.Length == 0) {
1063 Error_RequiresArgument (option);
1064 return ParseResult.Error;
1067 return ParseResult.Success;
1069 case "/helpinternal":
1071 return ParseResult.Stop;
1076 return ParseResult.Stop;
1080 if (value.Length == 0) {
1081 Error_RequiresArgument (option);
1082 return ParseResult.Error;
1084 settings.MainClass = value;
1085 return ParseResult.Success;
1089 settings.StdLib = false;
1090 return ParseResult.Success;
1093 settings.StdLib = true;
1094 return ParseResult.Success;
1097 settings.ShowFullPaths = true;
1098 return ParseResult.Success;
1101 if (value.Length == 0) {
1102 Error_RequiresFileName (option);
1103 return ParseResult.Error;
1106 settings.StrongNameKeyFile = value;
1107 return ParseResult.Success;
1109 case "/keycontainer":
1110 if (value.Length == 0) {
1111 Error_RequiresArgument (option);
1112 return ParseResult.Error;
1115 settings.StrongNameKeyContainer = value;
1116 return ParseResult.Success;
1120 settings.StrongNameDelaySign = true;
1121 return ParseResult.Success;
1124 settings.StrongNameDelaySign = false;
1125 return ParseResult.Success;
1127 case "/langversion":
1128 if (value.Length == 0) {
1129 Error_RequiresArgument (option);
1130 return ParseResult.Error;
1133 switch (value.ToLowerInvariant ()) {
1135 settings.Version = LanguageVersion.ISO_1;
1136 return ParseResult.Success;
1138 settings.Version = LanguageVersion.Default;
1139 return ParseResult.Success;
1141 settings.Version = LanguageVersion.ISO_2;
1142 return ParseResult.Success;
1144 settings.Version = LanguageVersion.V_3;
1145 return ParseResult.Success;
1147 settings.Version = LanguageVersion.V_4;
1148 return ParseResult.Success;
1150 settings.Version = LanguageVersion.V_5;
1151 return ParseResult.Success;
1153 settings.Version = LanguageVersion.Future;
1154 return ParseResult.Success;
1157 report.Error (1617, "Invalid -langversion option `{0}'. It must be `ISO-1', `ISO-2', `3', `4', `5', `Default' or `Future'", value);
1158 return ParseResult.Error;
1161 if (value.Length == 0) {
1162 Error_RequiresArgument (option);
1163 return ParseResult.Error;
1168 settings.Encoding = Encoding.UTF8;
1171 settings.Encoding = Encoding.Default;
1175 settings.Encoding = Encoding.GetEncoding (int.Parse (value));
1177 report.Error (2016, "Code page `{0}' is invalid or not installed", value);
1179 return ParseResult.Error;
1181 return ParseResult.Success;
1184 return ParseResult.UnknownOption;
1189 // Currently handles the Unix-like command line options, but will be
1190 // deprecated in favor of the CSCParseOption, which will also handle the
1191 // options that start with a dash in the future.
1193 ParseResult ParseOptionUnix (string arg, ref string[] args, ref int i, CompilerSettings settings)
1197 settings.VerboseParserFlag++;
1198 return ParseResult.Success;
1202 return ParseResult.Stop;
1205 settings.ParseOnly = true;
1206 return ParseResult.Success;
1208 case "--main": case "-m":
1209 report.Warning (-29, 1, "Compatibility: Use -main:CLASS instead of --main CLASS or -m CLASS");
1210 if ((i + 1) >= args.Length){
1211 Error_RequiresArgument (arg);
1212 return ParseResult.Error;
1214 settings.MainClass = args[++i];
1215 return ParseResult.Success;
1218 report.Warning (-29, 1, "Compatibility: Use -unsafe instead of --unsafe");
1219 settings.Unsafe = true;
1220 return ParseResult.Success;
1222 case "/?": case "/h": case "/help":
1225 return ParseResult.Stop;
1228 report.Warning (-29, 1, "Compatibility: Use -d:SYMBOL instead of --define SYMBOL");
1229 if ((i + 1) >= args.Length){
1230 Error_RequiresArgument (arg);
1231 return ParseResult.Error;
1234 settings.AddConditionalSymbol (args [++i]);
1235 return ParseResult.Success;
1238 settings.TokenizeOnly = true;
1239 return ParseResult.Success;
1243 report.Warning (-29, 1, "Compatibility: Use -out:FILE instead of --output FILE or -o FILE");
1244 if ((i + 1) >= args.Length){
1245 Error_RequiresArgument (arg);
1246 return ParseResult.Error;
1248 settings.OutputFile = args[++i];
1249 return ParseResult.Success;
1252 report.Warning (-29, 1, "Compatibility: Use -checked instead of --checked");
1253 settings.Checked = true;
1254 return ParseResult.Success;
1256 case "--stacktrace":
1257 settings.Stacktrace = true;
1258 return ParseResult.Success;
1260 case "--linkresource":
1262 report.Warning (-29, 1, "Compatibility: Use -linkres:VALUE instead of --linkres VALUE");
1263 if ((i + 1) >= args.Length){
1264 Error_RequiresArgument (arg);
1265 return ParseResult.Error;
1268 AddResource (new AssemblyResource (args[++i], args[i]), settings);
1269 return ParseResult.Success;
1273 report.Warning (-29, 1, "Compatibility: Use -res:VALUE instead of --res VALUE");
1274 if ((i + 1) >= args.Length){
1275 Error_RequiresArgument (arg);
1276 return ParseResult.Error;
1279 AddResource (new AssemblyResource (args[++i], args[i], true), settings);
1280 return ParseResult.Success;
1283 report.Warning (-29, 1, "Compatibility: Use -target:KIND instead of --target KIND");
1284 if ((i + 1) >= args.Length){
1285 Error_RequiresArgument (arg);
1286 return ParseResult.Error;
1289 string type = args [++i];
1292 settings.Target = Target.Library;
1293 settings.TargetExt = ".dll";
1297 settings.Target = Target.Exe;
1301 settings.Target = Target.WinExe;
1305 settings.Target = Target.Module;
1306 settings.TargetExt = ".dll";
1309 report.Error (2019, "Invalid target type for -target. Valid options are `exe', `winexe', `library' or `module'");
1312 return ParseResult.Success;
1315 report.Warning (-29, 1, "Compatibility: Use -r:LIBRARY instead of -r library");
1316 if ((i + 1) >= args.Length){
1317 Error_RequiresArgument (arg);
1318 return ParseResult.Error;
1321 string val = args [++i];
1322 int idx = val.IndexOf ('=');
1324 string alias = val.Substring (0, idx);
1325 string assembly = val.Substring (idx + 1);
1326 AddAssemblyReference (alias, assembly, settings);
1327 return ParseResult.Success;
1330 settings.AssemblyReferences.Add (val);
1331 return ParseResult.Success;
1334 report.Warning (-29, 1, "Compatibility: Use -lib:ARG instead of --L arg");
1335 if ((i + 1) >= args.Length){
1336 Error_RequiresArgument (arg);
1337 return ParseResult.Error;
1339 settings.ReferencesLookupPaths.Add (args [++i]);
1340 return ParseResult.Success;
1343 settings.EnhancedWarnings = true;
1344 return ParseResult.Success;
1347 report.Warning (-29, 1, "Compatibility: Use -nostdlib instead of --nostdlib");
1348 settings.StdLib = false;
1349 return ParseResult.Success;
1352 report.Warning (-29, 1, "Compatibility: Use -nowarn instead of --nowarn");
1353 if ((i + 1) >= args.Length){
1354 Error_RequiresArgument (arg);
1355 return ParseResult.Error;
1360 warn = int.Parse (args [++i]);
1363 Environment.Exit (1);
1365 settings.SetIgnoreWarning (warn);
1366 return ParseResult.Success;
1369 report.Warning (-29, 1, "Compatibility: Use -warn:LEVEL instead of --wlevel LEVEL");
1370 if ((i + 1) >= args.Length){
1371 Error_RequiresArgument (arg);
1372 return ParseResult.Error;
1375 SetWarningLevel (args [++i], settings);
1376 return ParseResult.Success;
1379 if ((i + 1) >= args.Length){
1380 Error_RequiresArgument (arg);
1381 return ParseResult.Error;
1385 settings.DebugFlags = int.Parse (args [++i]);
1387 Error_RequiresArgument (arg);
1388 return ParseResult.Error;
1391 return ParseResult.Success;
1395 return ParseResult.Stop;
1398 report.Warning (-29, 1, "Compatibility: Use -recurse:PATTERN option instead --recurse PATTERN");
1399 if ((i + 1) >= args.Length){
1400 Error_RequiresArgument (arg);
1401 return ParseResult.Error;
1403 ProcessSourceFiles (args [++i], true, settings.SourceFiles);
1404 return ParseResult.Success;
1407 settings.Timestamps = true;
1408 return ParseResult.Success;
1410 case "--debug": case "-g":
1411 report.Warning (-29, 1, "Compatibility: Use -debug option instead of -g or --debug");
1412 settings.GenerateDebugInfo = true;
1413 return ParseResult.Success;
1416 report.Warning (-29, 1, "Compatibility: Use -noconfig option instead of --noconfig");
1417 settings.LoadDefaultReferences = false;
1418 return ParseResult.Success;
1420 case "--metadata-only":
1421 settings.WriteMetadataOnly = true;
1422 return ParseResult.Success;
1425 if (arg.StartsWith ("--fatal", StringComparison.Ordinal)){
1427 if (arg.StartsWith ("--fatal=", StringComparison.Ordinal))
1428 int.TryParse (arg.Substring (8), out fatal);
1430 settings.FatalCounter = fatal;
1431 return ParseResult.Success;
1433 if (arg.StartsWith ("--runtime:", StringComparison.Ordinal)) {
1434 string version = arg.Substring (10);
1439 settings.StdLibRuntimeVersion = RuntimeVersion.v1;
1443 settings.StdLibRuntimeVersion = RuntimeVersion.v2;
1447 settings.StdLibRuntimeVersion = RuntimeVersion.v4;
1450 return ParseResult.Success;
1453 return ParseResult.UnknownOption;
1457 void SetWarningLevel (string s, CompilerSettings settings)
1462 level = int.Parse (s);
1465 if (level < 0 || level > 4) {
1466 report.Error (1900, "Warning level must be in the range 0-4");
1469 settings.WarningLevel = level;
1473 // Given a path specification, splits the path from the file/pattern
1475 static void SplitPathAndPattern (string spec, out string path, out string pattern)
1477 int p = spec.LastIndexOf ('/');
1480 // Windows does not like /file.cs, switch that to:
1485 pattern = spec.Substring (1);
1487 path = spec.Substring (0, p);
1488 pattern = spec.Substring (p + 1);
1493 p = spec.LastIndexOf ('\\');
1495 path = spec.Substring (0, p);
1496 pattern = spec.Substring (p + 1);
1507 "Mono C# compiler, Copyright 2001-2011 Novell, Inc., Copyright 2011-2012 Xamarin, Inc\n" +
1508 "mcs [options] source-files\n" +
1509 " --about About the Mono C# compiler\n" +
1510 " -addmodule:M1[,Mn] Adds the module to the generated assembly\n" +
1511 " -checked[+|-] Sets default aritmetic overflow context\n" +
1512 " -clscheck[+|-] Disables CLS Compliance verifications\n" +
1513 " -codepage:ID Sets code page to the one in ID (number, utf8, reset)\n" +
1514 " -define:S1[;S2] Defines one or more conditional symbols (short: -d)\n" +
1515 " -debug[+|-], -g Generate debugging information\n" +
1516 " -delaysign[+|-] Only insert the public key into the assembly (no signing)\n" +
1517 " -doc:FILE Process documentation comments to XML file\n" +
1518 " -fullpaths Any issued error or warning uses absolute file path\n" +
1519 " -help Lists all compiler options (short: -?)\n" +
1520 " -keycontainer:NAME The key pair container used to sign the output assembly\n" +
1521 " -keyfile:FILE The key file used to strongname the ouput assembly\n" +
1522 " -langversion:TEXT Specifies language version: ISO-1, ISO-2, 3, 4, 5, Default or Future\n" +
1523 " -lib:PATH1[,PATHn] Specifies the location of referenced assemblies\n" +
1524 " -main:CLASS Specifies the class with the Main method (short: -m)\n" +
1525 " -noconfig Disables implicitly referenced assemblies\n" +
1526 " -nostdlib[+|-] Does not reference mscorlib.dll library\n" +
1527 " -nowarn:W1[,Wn] Suppress one or more compiler warnings\n" +
1528 " -optimize[+|-] Enables advanced compiler optimizations (short: -o)\n" +
1529 " -out:FILE Specifies output assembly name\n" +
1530 " -pkg:P1[,Pn] References packages P1..Pn\n" +
1531 " -platform:ARCH Specifies the target platform of the output assembly\n" +
1532 " ARCH can be one of: anycpu, anycpu32bitpreferred, arm,\n" +
1533 " x86, x64 or itanium. The default is anycpu.\n" +
1534 " -recurse:SPEC Recursively compiles files according to SPEC pattern\n" +
1535 " -reference:A1[,An] Imports metadata from the specified assembly (short: -r)\n" +
1536 " -reference:ALIAS=A Imports metadata using specified extern alias (short: -r)\n" +
1537 " -sdk:VERSION Specifies SDK version of referenced assemblies\n" +
1538 " VERSION can be one of: 2, 4, 4.5 (default) or a custom value\n" +
1539 " -target:KIND Specifies the format of the output assembly (short: -t)\n" +
1540 " KIND can be one of: exe, winexe, library, module\n" +
1541 " -unsafe[+|-] Allows to compile code which uses unsafe keyword\n" +
1542 " -warnaserror[+|-] Treats all warnings as errors\n" +
1543 " -warnaserror[+|-]:W1[,Wn] Treats one or more compiler warnings as errors\n" +
1544 " -warn:0-4 Sets warning level, the default is 4 (short -w:)\n" +
1545 " -helpinternal Shows internal and advanced compiler options\n" +
1548 " -linkresource:FILE[,ID] Links FILE as a resource (short: -linkres)\n" +
1549 " -resource:FILE[,ID] Embed FILE as a resource (short: -res)\n" +
1550 " -win32res:FILE Specifies Win32 resource file (.res)\n" +
1551 " -win32icon:FILE Use this icon for the output\n" +
1552 " @file Read response file for more options\n\n" +
1553 "Options can be of the form -option or /option");
1558 string version = System.Reflection.MethodBase.GetCurrentMethod ().DeclaringType.Assembly.GetName ().Version.ToString ();
1559 output.WriteLine ("Mono C# compiler version {0}", version);
1563 public class RootContext
1566 // Contains the parsed tree
1568 static ModuleContainer root;
1570 static public ModuleContainer ToplevelTypes {
1571 get { return root; }
1572 set { root = value; }