public static string GetCompilerPath(IDictionary <string, string> provOptions, string compilerExecutable) { // Get the location of the runtime, the usual answer string compPath = Executor.GetRuntimeInstallDirectory(); // if provOptions is provided check to see if it alters what version we should bind to. // provOptions can be null if someone does new VB/CSCodeProvider(), in which case // they get the default behavior. if (provOptions != null) { string directoryPath; bool directoryPathPresent = provOptions.TryGetValue(DirectoryPath, out directoryPath); string versionVal; bool versionValPresent = provOptions.TryGetValue(NameTag, out versionVal); if (directoryPathPresent && versionValPresent) { throw new InvalidOperationException(SR.GetString(SR.Cannot_Specify_Both_Compiler_Path_And_Version, DirectoryPath, NameTag)); } // If they have an explicit path, use it. Otherwise, look it up from the registry. if (directoryPathPresent) { return(directoryPath); } // If they have specified a version number in providerOptions, use it. if (versionValPresent) { switch (versionVal) { case RedistVersionInfo.InPlaceVersion: // Use the RuntimeInstallDirectory, already obtained break; case RedistVersionInfo.RedistVersion: // lock to the Orcas version, if it's not available throw (we'll throw at compile time) compPath = GetCompilerPathFromRegistry(versionVal); break; case RedistVersionInfo.RedistVersion20: //look up 2.0 compiler path from registry compPath = GetCompilerPathFromRegistry(versionVal); break; default: compPath = null; break; } } } if (compPath == null) { throw new InvalidOperationException(SR.GetString(SR.CompilerNotFound, compilerExecutable)); } return(compPath); }
public static string GetCompilerPath(IDictionary <string, string> provOptions, string compilerExecutable) { string runtimeInstallDirectory = Executor.GetRuntimeInstallDirectory(); if (provOptions != null) { string str2; string str3; bool flag = provOptions.TryGetValue("CompilerDirectoryPath", out str2); bool flag2 = provOptions.TryGetValue("CompilerVersion", out str3); if (flag && flag2) { throw new InvalidOperationException(SR.GetString("Cannot_Specify_Both_Compiler_Path_And_Version", new object[] { "CompilerDirectoryPath", "CompilerVersion" })); } if (flag) { return(str2); } if (flag2) { string str4 = str3; if (str4 == null) { goto Label_00A9; } if (str4 != "v4.0") { if (!(str4 == "v3.5")) { if (str4 == "v2.0") { runtimeInstallDirectory = GetCompilerPathFromRegistry(str3); goto Label_00AB; } goto Label_00A9; } runtimeInstallDirectory = GetCompilerPathFromRegistry(str3); } } } goto Label_00AB; Label_00A9: runtimeInstallDirectory = null; Label_00AB: if (runtimeInstallDirectory == null) { throw new InvalidOperationException(SR.GetString("CompilerNotFound", new object[] { compilerExecutable })); } return(runtimeInstallDirectory); }
protected virtual CompilerResults FromFileBatch(CompilerParameters options, string[] fileNames) { if (options == null) { throw new ArgumentNullException("options"); } if (fileNames == null) { throw new ArgumentNullException("fileNames"); } new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand(); string outputFile = null; int nativeReturnValue = 0; CompilerResults results = new CompilerResults(options.TempFiles); new SecurityPermission(SecurityPermissionFlag.ControlEvidence).Assert(); try { results.Evidence = options.Evidence; } finally { CodeAccessPermission.RevertAssert(); } bool flag = false; if ((options.OutputAssembly == null) || (options.OutputAssembly.Length == 0)) { string fileExtension = options.GenerateExecutable ? "exe" : "dll"; options.OutputAssembly = results.TempFiles.AddExtension(fileExtension, !options.GenerateInMemory); new FileStream(options.OutputAssembly, FileMode.Create, FileAccess.ReadWrite).Close(); flag = true; } results.TempFiles.AddExtension("pdb"); string cmdArgs = this.CmdArgsFromParameters(options) + " " + JoinStringArray(fileNames, " "); string responseFileCmdArgs = this.GetResponseFileCmdArgs(options, cmdArgs); string trueArgs = null; if (responseFileCmdArgs != null) { trueArgs = cmdArgs; cmdArgs = responseFileCmdArgs; } this.Compile(options, Executor.GetRuntimeInstallDirectory(), this.CompilerName, cmdArgs, ref outputFile, ref nativeReturnValue, trueArgs); results.NativeCompilerReturnValue = nativeReturnValue; if ((nativeReturnValue != 0) || (options.WarningLevel > 0)) { FileStream stream = new FileStream(outputFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); try { if (stream.Length > 0L) { string str6; StreamReader reader = new StreamReader(stream, Encoding.UTF8); do { str6 = reader.ReadLine(); if (str6 != null) { results.Output.Add(str6); this.ProcessCompilerOutputLine(results, str6); } }while (str6 != null); } } finally { stream.Close(); } if ((nativeReturnValue != 0) && flag) { File.Delete(options.OutputAssembly); } } if (!results.Errors.HasErrors && options.GenerateInMemory) { FileStream stream2 = new FileStream(options.OutputAssembly, FileMode.Open, FileAccess.Read, FileShare.Read); try { int length = (int)stream2.Length; byte[] buffer = new byte[length]; stream2.Read(buffer, 0, length); new SecurityPermission(SecurityPermissionFlag.ControlEvidence).Assert(); try { results.CompiledAssembly = Assembly.Load(buffer, null, options.Evidence); } finally { CodeAccessPermission.RevertAssert(); } return(results); } finally { stream2.Close(); } } results.PathToAssembly = options.OutputAssembly; return(results); }
/// <include file='doc\CodeCompiler.uex' path='docs/doc[@for="CodeCompiler.FromFileBatch"]/*' /> /// <devdoc> /// <para> /// Compiles the specified files using the specified options, and returns the /// results from the compilation. /// </para> /// </devdoc> protected virtual CompilerResults FromFileBatch(CompilerParameters options, string[] fileNames) { new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand(); string outputFile = null; int retValue = 0; CompilerResults results = new CompilerResults(options.TempFiles); SecurityPermission perm1 = new SecurityPermission(SecurityPermissionFlag.ControlEvidence); perm1.Assert(); try { results.Evidence = options.Evidence; } finally { SecurityPermission.RevertAssert(); } bool createdEmptyAssembly = false; if (options.OutputAssembly == null || options.OutputAssembly.Length == 0) { string extension = (options.GenerateExecutable) ? "exe" : "dll"; options.OutputAssembly = results.TempFiles.AddExtension(extension, !options.GenerateInMemory); // Create an empty assembly. This is so that the file will have permissions that // we can later access with our current credential. If we don't do this, the compiler // could end up creating an assembly that we cannot open (bug ASURT 83492) new FileStream(options.OutputAssembly, FileMode.Create, FileAccess.ReadWrite).Close(); createdEmptyAssembly = true; } results.TempFiles.AddExtension("pdb"); string args = CmdArgsFromParameters(options) + " " + JoinStringArray(fileNames, " "); // Use a response file if the compiler supports it string responseFileArgs = GetResponseFileCmdArgs(options, args); string trueArgs = null; if (responseFileArgs != null) { trueArgs = args; args = responseFileArgs; } Compile(options, Executor.GetRuntimeInstallDirectory(), CompilerName, args, ref outputFile, ref retValue, trueArgs); results.NativeCompilerReturnValue = retValue; // only look for errors/warnings if the compile failed or the caller set the warning level if (retValue != 0 || options.WarningLevel > 0) { FileStream outputStream = new FileStream(outputFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); try { if (outputStream.Length > 0) { // The output of the compiler is in UTF8 (bug 54925) StreamReader sr = new StreamReader(outputStream, Encoding.UTF8); string line; do { line = sr.ReadLine(); if (line != null) { results.Output.Add(line); ProcessCompilerOutputLine(results, line); } } while (line != null); } } finally { outputStream.Close(); } // Delete the empty assembly if we created one if (retValue != 0 && createdEmptyAssembly) { File.Delete(options.OutputAssembly); } } if (!results.Errors.HasErrors && options.GenerateInMemory) { FileStream fs = new FileStream(options.OutputAssembly, FileMode.Open, FileAccess.Read, FileShare.Read); try { int fileLen = (int)fs.Length; byte[] b = new byte[fileLen]; fs.Read(b, 0, fileLen); SecurityPermission perm = new SecurityPermission(SecurityPermissionFlag.ControlEvidence); perm.Assert(); try { results.CompiledAssembly = Assembly.Load(b, null, options.Evidence); } finally { SecurityPermission.RevertAssert(); } } finally { fs.Close(); } } else { results.PathToAssembly = options.OutputAssembly; } return(results); }