Ejemplo n.º 1
0
        private void LogCompilationResult(EmitResult result, bool throwErrorException)
        {
            string errorMessage = "";

            foreach (var diagnostic in result.Diagnostics)
            {
                if (diagnostic.Severity == DiagnosticSeverity.Error)
                {
                    EventOutputError?.Invoke("{0}" + Environment.NewLine, diagnostic.ToString());
                }
                else // catch everything else as warning
                {
                    EventOutputWarning?.Invoke("{0}" + Environment.NewLine, diagnostic.ToString());
                }

                errorMessage += diagnostic + Environment.NewLine;
            }

            if (!result.Success && throwErrorException)
            {
                throw new Error(errorMessage);
            }
        }
Ejemplo n.º 2
0
 public void LogErrorLine(string message, params object[] args)
 {
     EventOutputError?.Invoke(message + Environment.NewLine, args);
 }
Ejemplo n.º 3
0
        private IAssemblyInfo Build(IBuilderContext builderContext, string libraryFile, params string[] sources)
        {
            var assemblyInfo = LoadAssemblyInfo(builderContext, sources);

            HashSet <string> references = new HashSet <string>();

            Dictionary <string, string> providerOptions = new Dictionary <string, string>();

            providerOptions.Add("CompilerVersion", "v4.0");
            CodeDomProvider provider = new Microsoft.CSharp.CSharpCodeProvider(providerOptions);

            CompilerParameters cp = new CompilerParameters();

            if (UseDefaultReferences)
            {
                foreach (string defaultReference in DefaultReferences)
                {
                    references.Add(GetAssemblyDllPath(defaultReference));
                }
            }

            foreach (string assemblyFile in _references)
            {
                references.Add(assemblyFile);
            }

            foreach (Assembly assembly in _assemblies)
            {
                if (!assembly.IsDynamic)
                {
                    references.Add(assembly.Location);
                }
            }

            cp.ReferencedAssemblies.AddRange(references.ToArray());

            // Generate an library
            cp.GenerateExecutable = false;

            // Set the level at which the compiler
            // should start displaying warnings.
            cp.WarningLevel = 4;

            // Set whether to treat all warnings as errors.
            cp.TreatWarningsAsErrors = false;

            // Set compiler argument to optimize output.
            // TODO : figure out why it does not work when uncommenting the following line
            // cp.CompilerOptions = "/optimize";

            // Specify the assembly file name to generate
            if (libraryFile == null)
            {
                cp.GenerateInMemory        = true;
                cp.IncludeDebugInformation = false;
            }
            else
            {
                cp.GenerateInMemory        = false;
                cp.IncludeDebugInformation = true;
                cp.OutputAssembly          = libraryFile;
            }

            // Notes:
            // Avoid getting spoiled by environment variables.
            // C# will give compilation errors if a LIB variable contains non-existing directories.
            Environment.SetEnvironmentVariable("LIB", null);

            // Invoke compilation of the source file.
            CompilerResults cr = provider.CompileAssemblyFromFile(cp, assemblyInfo.SourceFiles.ToArray());

            if (cr.Errors.HasErrors || cr.Errors.HasWarnings)
            {
                string errorMessage = "";
                foreach (CompilerError ce in cr.Errors)
                {
                    if (ce.IsWarning)
                    {
                        EventOutputWarning?.Invoke(ce + Environment.NewLine);
                    }
                    else
                    {
                        EventOutputError?.Invoke(ce + Environment.NewLine);
                    }

                    errorMessage += ce + Environment.NewLine;
                }

                if (cr.Errors.HasErrors)
                {
                    if (builderContext.CompileErrorBehavior == BuilderCompileErrorBehavior.ThrowException)
                    {
                        throw new Error(errorMessage);
                    }
                    return(assemblyInfo);
                }
            }

            assemblyInfo.Assembly = cr.CompiledAssembly;
            assemblyInfo.Id       = assemblyInfo.Assembly.Location;
            return(assemblyInfo);
        }
Ejemplo n.º 4
0
        private IAssemblyInfo Build(IBuilderContext builderContext, string libraryFile, params string[] sources)
        {
            var assemblyInfo = LoadAssemblyInfo(builderContext, sources);

            HashSet <string> references = new HashSet <string>();

            Dictionary <string, string> providerOptions = new Dictionary <string, string>();

            providerOptions.Add("CompilerVersion", "v4.0");
            CodeDomProvider provider = new Microsoft.CSharp.CSharpCodeProvider(providerOptions);

            CompilerParameters cp = new CompilerParameters();

            if (UseDefaultReferences)
            {
                foreach (string defaultReference in DefaultReferences)
                {
                    references.Add(GetAssemblyDllPath(defaultReference));
                }
            }

            foreach (string assemblyFile in _references)
            {
                references.Add(assemblyFile);
            }

            foreach (Assembly assembly in _assemblies)
            {
                if (!assembly.IsDynamic)
                {
                    references.Add(assembly.Location);
                }
            }

            cp.ReferencedAssemblies.AddRange(references.ToArray());

            // Generate an library
            cp.GenerateExecutable = false;

            // Set the level at which the compiler
            // should start displaying warnings.
            cp.WarningLevel = 4;

            // Set whether to treat all warnings as errors.
            cp.TreatWarningsAsErrors = false;

            // Set compiler argument to optimize output.
            // TODO : figure out why it does not work when uncommenting the following line
            // cp.CompilerOptions = "/optimize";

            // If any defines are specified, pass them to the CSC.
            if (_defines.Any())
            {
                cp.CompilerOptions = "-DEFINE:" + string.Join(",", _defines);
            }

            // Specify the assembly file name to generate
            if (libraryFile == null)
            {
                cp.GenerateInMemory        = true;
                cp.IncludeDebugInformation = false;
            }
            else
            {
                cp.GenerateInMemory        = false;
                cp.IncludeDebugInformation = true;
                cp.OutputAssembly          = libraryFile;
            }

            // Notes:
            // Avoid getting spoiled by environment variables.
            // C# will give compilation errors if a LIB variable contains non-existing directories.
            Environment.SetEnvironmentVariable("LIB", null);

            // Configure Temp file collection to avoid deleting its temp file. We will delete them ourselves after the compilation
            // For some reasons, this seems to add just enough delays to avoid the following first chance exception(probably caused by some handles in csc.exe)
            // System.IO.IOException: 'The process cannot access the file 'C:\Users\xxx\AppData\Local\Temp\sa205152\sa205152.out' because it is being used by another process.'
            // That exception wasn't causing real problems but was really annoying when debugging!
            // Executed several times sharpmake and this first chance exception no longer occurs when KeepFiles is true.
            cp.TempFiles.KeepFiles = true;

            // Invoke compilation of the source file.
            CompilerResults cr = provider.CompileAssemblyFromFile(cp, assemblyInfo.SourceFiles.ToArray());

            // Manually delete the files in the temp files collection.
            cp.TempFiles.Delete();

            if (cr.Errors.HasErrors || cr.Errors.HasWarnings)
            {
                string errorMessage = "";
                foreach (CompilerError ce in cr.Errors)
                {
                    if (ce.IsWarning)
                    {
                        EventOutputWarning?.Invoke("{0}" + Environment.NewLine, ce.ToString());
                    }
                    else
                    {
                        EventOutputError?.Invoke("{0}" + Environment.NewLine, ce.ToString());
                    }

                    errorMessage += ce + Environment.NewLine;
                }

                if (cr.Errors.HasErrors)
                {
                    if (builderContext == null || builderContext.CompileErrorBehavior == BuilderCompileErrorBehavior.ThrowException)
                    {
                        throw new Error(errorMessage);
                    }
                    return(assemblyInfo);
                }
            }

            assemblyInfo.Assembly = cr.CompiledAssembly;
            assemblyInfo.Id       = assemblyInfo.Assembly.Location;
            return(assemblyInfo);
        }