⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cppnetbindingcompilermanager.cs

📁 SharpDevelop2.0.0 c#开发免费工具
💻 CS
📖 第 1 页 / 共 2 页
字号:
										includedFile = true;
										sb.Append("\"");
										sb.Append(Path.GetFullPath(finfo.Name));
										sb.Append("\"\n");
									}
								}
							}
							break;
					}
				}
			}
			if (!includedFile) {
				return null;
			}
			string responseFileName = Path.GetTempFileName();
			StreamWriter writer = new StreamWriter(responseFileName, false);
//			string standardIncludes = Environment.GetEnvironmentVariable("INCLUDE");
//			if (standardIncludes != null && standardIncludes.Length > 0) {
//				writer.WriteLine("/I\"" + standardIncludes + "\"");
//			}
			
			writer.Write(sb.ToString());
			writer.Close();
			
			_inputFiles.Append("Creating temporary file ");
			_inputFiles.Append(responseFileName);
			_inputFiles.Append(" with following content:<BR>");
			_inputFiles.Append("<PRE>");
			_inputFiles.Append(sb.ToString());
			_inputFiles.Append("</PRE>");
			
			return responseFileName;
		}
		
		Hashtable lastCompiledFiles = new Hashtable();

		private bool ShouldCompileFile(CPPProject p, string fileName)
		{
			CPPCompilerParameters compilerparameters = (CPPCompilerParameters)p.ActiveConfiguration;
			string directory  = Path.GetDirectoryName(compilerparameters.OutputFile);
			string objectFile = Path.Combine(directory, Path.ChangeExtension(Path.GetFileName(fileName), ".obj"));
			if (!File.Exists(objectFile)) {
				return true;
			}
			
			string[] additinalIncludeDirs = compilerparameters.AdditionalCompilerOptions.Split(';');
			ArrayList dirs = new ArrayList(additinalIncludeDirs.Length+1);
			dirs.Add(Path.GetDirectoryName(fileName));
			foreach (string dir in additinalIncludeDirs)
			{
				dirs.Add(dir);
			}
			
			DateTime lastWriteTime = new IncludeParser(fileName, dirs, true).Parse().GetLastWriteTime();
//			DateTime lastWriteTime = File.GetLastWriteTime(fileName);
			
			bool shouldCompile;
			if (lastCompiledFiles[fileName] == null) {
				shouldCompile = true;
			} else {
				shouldCompile = lastWriteTime != (DateTime)lastCompiledFiles[fileName];
			}
			
			lastCompiledFiles[fileName] = lastWriteTime;
			return shouldCompile;
		}
		
		private ICompilerResult InternalCompileProject(CPPProject p, bool preCompiledHeader, bool force)
		{
			CPPCompilerParameters compilerparameters = (CPPCompilerParameters)p.ActiveConfiguration;
			
			string responseFileName = WriteCompilerParameters(p, preCompiledHeader, force);
			if (responseFileName == null) {
				return null;
			}
			string output = String.Empty;
			string error  = String.Empty; 
			
			string compilerName = GetCompilerName();
			string clstr = compilerName + " \"@" + responseFileName + "\"";
			
			TempFileCollection tf = new TempFileCollection();
			
			string currDir = Directory.GetCurrentDirectory();
			string intDir = compilerparameters.IntermediateDirectory;
			if (intDir == null || intDir.Length == 0)  {
				intDir = compilerparameters.OutputDirectory;
			}
			_inputFiles.Append("Executing command: <C>" + clstr + "</C><hr>");

			ICompilerResult result;
			try {
				Executor.ExecWaitWithCapture(clstr, tf, ref output, ref error);
				result = ParseOutput(tf, output, error);
			}
			catch (System.Runtime.InteropServices.ExternalException e) {
				ShowErrorBox(e);
				result = CreateErrorCompilerResult(tf, e);
			}
			finally {
				File.Delete(responseFileName);
				File.Delete(output);
				File.Delete(error);
			}
			
			return result;
		}
		#endregion

		#region LINKER
		private string GetLinkerName()
		{
			return @"link.exe";
		}
		
		private string WriteLinkerOptions(CPPProject p)
		{
			CPPCompilerParameters compilerparameters = (CPPCompilerParameters)p.ActiveConfiguration;
			
			StringBuilder sb = new StringBuilder();
			
			string exe = compilerparameters.OutputFile;
			string dir = Path.GetDirectoryName(Path.GetFullPath(exe));
			sb.Append("/OUT:\"");sb.Append(exe);sb.Append("\"\n");
			foreach (ProjectFile finfo in p.ProjectFiles) {
				if (finfo.Subtype != Subtype.Directory) {
					switch (finfo.BuildAction) {
						case BuildAction.Compile:
							if (CanCompile(finfo.Name))
							{
								sb.Append('"');
								sb.Append(Path.Combine(dir,
							    	                   Path.ChangeExtension(Path.GetFileName(finfo.Name), 
							        	               ".obj")));
								sb.Append("\"\n");
							}
							break;
						case BuildAction.EmbedAsResource:
							sb.Append("/ASSEMBLYRESOURCE:\"");sb.Append(Path.GetFullPath(finfo.Name));sb.Append("\"\n");
							break;
					}
				}
			}
			switch (compilerparameters.ConfigurationType) {
				case ConfigurationType.Dll:
					sb.Append("/DLL\n");
					break;
			}
			
			sb.Append(compilerparameters.GetLinkerOptionsForCompiler());
			
			// write to response file
			string responseFileName = Path.GetTempFileName();
			StreamWriter writer = new StreamWriter(responseFileName);
//			string standardLibs = Environment.GetEnvironmentVariable("LIB");
//			if (standardLibs != null && standardLibs.Length > 0) {
//				foreach (string lib in standardLibs.Split(';')) {
//					if (lib.Length > 0) {
//						writer.WriteLine("/LIBPATH:\"" + lib + "\"");
//					}
//				}
//			}
			writer.Write(sb.ToString());
			writer.Close();
			
			_inputFiles.Append("Creating temporary file <C>" + responseFileName + "</C> with following content:<BR>");
			_inputFiles.Append("<PRE>");
			_inputFiles.Append(sb.ToString());
			_inputFiles.Append("</PRE>");
			return responseFileName;
		}
		
		private ICompilerResult LinkProject(CPPProject p)
		{
			CPPCompilerParameters compilerparameters = (CPPCompilerParameters)p.ActiveConfiguration;
			
			string responseFileName = WriteLinkerOptions(p);
			
			string output = String.Empty;
			string error  = String.Empty; 
			
			string compilerName = GetLinkerName();
			string clstr = compilerName + " \"@" + responseFileName + "\"";
			
			TempFileCollection tf = new TempFileCollection();
			
			string currDir = Directory.GetCurrentDirectory();
			string intDir = compilerparameters.IntermediateDirectory;
			if (intDir == null || intDir.Length == 0)  {
				intDir = compilerparameters.OutputDirectory;
			}
			
			_inputFiles.Append("Executing command : <C>");
			_inputFiles.Append(clstr);
			_inputFiles.Append("</C><hr>");
			Executor.ExecWaitWithCapture(clstr, tf, ref output, ref error);
			
			ICompilerResult result = ParseOutput(tf, output, error);
			
//			File.Delete(responseFileName);
			File.Delete(output);
			File.Delete(error);
			
			return result;
		}
		#endregion
		
		private ICompilerResult CreateErrorCompilerResult(TempFileCollection tf, System.Runtime.InteropServices.ExternalException e)
		{
			CompilerError error = new CompilerError();
			error.Line        = 0;
			error.FileName    = "";
			error.IsWarning   = false;
			error.ErrorNumber = "";
			error.ErrorText   = e.Message;
			CompilerResults cr = new CompilerResults(tf);
			cr.Errors.Add(error);
			return new DefaultCompilerResult(cr, "");
		}
		
		private void InternalParseOutputFile(StringBuilder compilerOutput, CompilerResults cr, string file)
		{
			StreamReader sr = new StreamReader(File.OpenRead(file), Encoding.Default);
			
			// skip fist whitespace line
			sr.ReadLine();
		
			
			while (true) {
				string curLine = sr.ReadLine();
				_buildProcess.Append(curLine);
				_buildProcess.Append("\n");
				compilerOutput.Append(curLine);
				compilerOutput.Append('\n');
				if (curLine == null) {
					break;
				}
				curLine = curLine.Trim();
				if (curLine.Length == 0) {
					continue;
				}
				
				CompilerError error = new CompilerError();
				
				// try to match standard errors
				Match match = normalError.Match(curLine);
				if (match.Success) {
					error.Line = Int32.Parse(match.Result("${line}"));
					try {
						error.FileName = Path.GetFullPath(match.Result("${file}"));
					} catch (Exception) {
						error.FileName = "";
					}
					error.IsWarning   = match.Result("${error}").EndsWith("warning"); 
					error.ErrorNumber = match.Result("${number}");
					error.ErrorText   = match.Result("${message}");
				} else {
					match = generalError.Match(curLine); // try to match general csc errors
					if (match.Success) {
						error.IsWarning   = match.Result("${error}").EndsWith("warning");
						error.ErrorNumber = match.Result("${number}");
						error.ErrorText   = match.Result("${message}");
					} else { // give up and skip the line
						continue;
					}
				}
						
				cr.Errors.Add(error);
			}
			sr.Close();
		}
		
		private ICompilerResult ParseOutput(TempFileCollection tf, string outputFile, string errorFile)
		{
			StringBuilder compilerOutput = new StringBuilder();
			CompilerResults cr = new CompilerResults(tf);
			InternalParseOutputFile(compilerOutput, cr, outputFile);			
			InternalParseOutputFile(compilerOutput, cr, errorFile);			
			return new DefaultCompilerResult(cr, compilerOutput.ToString());
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -