Example #1
0
    static int Main(string[] args)
    {
        if (0 < args.Length && args[0] == "/break")
        {
            string[] newArgs = new string[args.Length - 1];
            Array.Copy(args, 1, newArgs, 0, newArgs.Length);
            args = newArgs;
            System.Diagnostics.Debugger.Break();
        }
        int    rc = 0;
        bool   includeStandardResponseFile = true;
        string fileName  = null;
        int    n         = args == null ? 0 : args.Length;
        bool   testsuite = false;

        for (int i = 0; i < n; i++)
        {
            string arg = args[i];
            if (arg == null || arg.Length < 1)
            {
                continue;
            }
            char ch = arg[0];
            if (ch == '/')
            {
                if (arg == "/noconfig" || arg == "/nostdlib")
                {
                    includeStandardResponseFile = false;
                }
            }
            else if (ch == '-')
            {
                if (arg == "-noconfig" || arg == "-nostdlib")
                {
                    includeStandardResponseFile = false;
                }
            }
            else if (ch != '@')
            {
                fileName = arg;
                if (BetterPath.GetExtension(fileName) == ".suite")
                {
                    testsuite = true;
                }
            }
        }
        if (includeStandardResponseFile)
        {
            int    nFiles             = 2;
            string globalPath         = Path.GetDirectoryName(typeof(object).Assembly.Location);
            string globalResponseFile = Path.Combine(globalPath, "csc.rsp");
            if (!File.Exists(globalResponseFile))
            {
                globalResponseFile = null; nFiles--;
            }
            string localPath         = Directory.GetCurrentDirectory();
            string localResponseFile = Path.Combine(localPath, "csc.rsp");
            if (!File.Exists(localResponseFile))
            {
                localResponseFile = null;
                nFiles--;
            }
            if (nFiles > 0)
            {
                string[] newArgs = new string[n + nFiles];
                int      i       = 0;
                if (globalResponseFile != null)
                {
                    newArgs[i++] = "@" + globalResponseFile;
                }
                if (localResponseFile != null)
                {
                    newArgs[i++] = "@" + localResponseFile;
                }
                for (int j = 0; j < n; j++)
                {
                    newArgs[i + j] = args[j];
                }
                args = newArgs;
            }
        }
        Microsoft.SpecSharp.SpecSharpCompilerOptions options = new Microsoft.SpecSharp.SpecSharpCompilerOptions();
        options.TempFiles          = new TempFileCollection(Directory.GetCurrentDirectory(), true);
        options.GenerateExecutable = true;
        options.MayLockFiles       = true;
        options.CompileAndExecute  = testsuite;
        ErrorNodeList errors = new ErrorNodeList(0);

        Microsoft.SpecSharp.Compiler compiler = new Microsoft.SpecSharp.Compiler();
        string[] fileNames = compiler.ParseCompilerParameters(options, args, errors);
        if (options.DisplayCommandLineHelp)
        {
            System.Resources.ResourceManager rm = new System.Resources.ResourceManager("ssc.Messages", typeof(main).Module.Assembly);
            Console.WriteLine(rm.GetString("UsageTitle", null));
            Console.WriteLine(options.GetOptionHelp());
            return(0);
        }
        if (testsuite)
        {
            // fileNames has expanded wildcards.
            bool suiteSuccess = true;
            foreach (string file in fileNames)
            {
                suiteSuccess &= main.RunSuite(file);
            }

            if (suiteSuccess)
            {
                return(0);
            }
            return(1);
        }
        string fatalErrorString = null;

        n = errors.Count;
        for (int i = 0; i < n; i++)
        {
            ErrorNode e = errors[i];
            if (e == null)
            {
                continue;
            }
            rc++;
            if (fatalErrorString == null)
            {
                fatalErrorString = compiler.GetFatalErrorString();
            }
            Console.Write(fatalErrorString, e.Code.ToString("0000"));
            Console.WriteLine(e.GetMessage());
        }
        if (rc > 0)
        {
            return(1);
        }
        switch (options.TargetPlatform)
        {
        case PlatformType.notSpecified:
            if (options.NoStandardLibrary && (options.StandardLibraryLocation == null || options.StandardLibraryLocation.Length == 0))
            {
                Microsoft.SpecSharp.TargetPlatform.SetToV2(options.TargetPlatformLocation);
            }
            break;

        case PlatformType.v1: Microsoft.SpecSharp.TargetPlatform.SetToV1(options.TargetPlatformLocation); break;

        case PlatformType.v11: Microsoft.SpecSharp.TargetPlatform.SetToV1_1(options.TargetPlatformLocation); break;

        case PlatformType.v2: Microsoft.SpecSharp.TargetPlatform.SetToV2(options.TargetPlatformLocation); break;

        default:
            if (options.TargetPlatformLocation != null) //TODO: assert not null
            {
                Microsoft.SpecSharp.TargetPlatform.SetToPostV1_1(options.TargetPlatformLocation);
            }
            break;
        }
        compiler.UpdateRuntimeAssemblyLocations(options);
        CompilerResults results;

        if (fileNames.Length == 1)
        {
            if (options.EmitManifest)
            {
                results = compiler.CompileAssemblyFromFile(options, fileNames[0], new ErrorNodeList(), true);
            }
            else
            {
                results = compiler.CompileModuleFromFile(options, fileNames[0]);
            }
        }
        else
        {
            if (options.EmitManifest)
            {
                results = compiler.CompileAssemblyFromFileBatch(options, fileNames);
            }
            else
            {
                results = compiler.CompileModuleFromFileBatch(options, fileNames);
            }
        }
        string errorString   = null;
        string warningString = null;

        foreach (CompilerError e in results.Errors)
        {
            if (e.FileName != null && e.FileName.Length > 0)
            {
                Console.Write(main.GetPath(e.FileName, options));
                Console.Write('(');
                Console.Write(e.Line);
                Console.Write(',');
                Console.Write(e.Column);
                Console.Write("): ");
            }
            if (e.IsWarning)
            {
                if (!e.ErrorNumber.StartsWith("CS") || e.ErrorNumber.Length == 6)
                {
                    if (warningString == null)
                    {
                        warningString = compiler.GetWarningString();
                    }
                    Console.Write(warningString, e.ErrorNumber);
                }
            }
            else
            {
                if (!e.ErrorNumber.StartsWith("CS") || e.ErrorNumber.Length == 6)
                {
                    rc++; //REVIEW: include related location errors?
                    if (errorString == null)
                    {
                        errorString = compiler.GetErrorString();
                    }
                    Console.Write(errorString, e.ErrorNumber);
                }
            }
            Console.WriteLine(e.ErrorText);
        }
        if (rc > 0)
        {
            return(1);
        }
        if ((rc = results.NativeCompilerReturnValue) == 0 && options.CompileAndExecute &&
            results.CompiledAssembly != null && results.CompiledAssembly.EntryPoint != null)
        {
            if (results.CompiledAssembly.EntryPoint.GetParameters().Length == 0)
            {
                results.CompiledAssembly.EntryPoint.Invoke(null, null);
            }
            else
            {
                results.CompiledAssembly.EntryPoint.Invoke(null, new object[] { new string[0] });
            }
        }
        if (rc > 0)
        {
            return(1);
        }
        return(0);
    }