Пример #1
0
        //FIXME: the fallback is broken since multiple frameworks can have the same corlib
        public TargetFrameworkMoniker GetTargetFrameworkForAssembly(TargetRuntime tr, string file)
        {
            if (!File.Exists(file))
            {
                return(TargetFrameworkMoniker.UNKNOWN);
            }
            var universe = CreateClosedUniverse();

            try {
                IKVM.Reflection.Assembly assembly = universe.LoadFile(file);
                var att = assembly.CustomAttributes.FirstOrDefault(a =>
                                                                   a.AttributeType.FullName == "System.Runtime.Versioning.TargetFrameworkAttribute"
                                                                   );
                if (att != null)
                {
                    if (att.ConstructorArguments.Count == 1)
                    {
                        var v = att.ConstructorArguments[0].Value as string;
                        TargetFrameworkMoniker m;
                        if (v != null && TargetFrameworkMoniker.TryParse(v, out m))
                        {
                            return(m);
                        }
                    }
                    LoggingService.LogError("Invalid TargetFrameworkAttribute in assembly {0}", file);
                }

                foreach (var r in assembly.GetReferencedAssemblies())
                {
                    if (r.Name == "mscorlib")
                    {
                        TargetFramework compatibleFramework = null;
                        // If there are several frameworks that can run the file, pick one that is installed
                        foreach (TargetFramework tf in GetKnownFrameworks())
                        {
                            if (tf.GetCorlibVersion() == r.Version.ToString())
                            {
                                compatibleFramework = tf;
                                if (tr.IsInstalled(tf))
                                {
                                    return(tf.Id);
                                }
                            }
                        }
                        if (compatibleFramework != null)
                        {
                            return(compatibleFramework.Id);
                        }
                        break;
                    }
                }
            } catch (Exception ex) {
                LoggingService.LogError("Error determining target framework for assembly {0}: {1}", file, ex);
                return(TargetFrameworkMoniker.UNKNOWN);
            } finally {
                universe.Dispose();
            }
            LoggingService.LogError("Failed to determine target framework for assembly {0}", file);
            return(TargetFrameworkMoniker.UNKNOWN);
        }
		public static RemoteProjectBuilder GetProjectBuilder (TargetRuntime runtime, string toolsVersion, string file)
		{
			lock (builders) {
				var toolsFx = Runtime.SystemAssemblyService.GetTargetFramework (new TargetFrameworkMoniker (toolsVersion));
				string binDir = runtime.GetMSBuildBinPath (toolsFx);
				
				if (!runtime.IsInstalled (toolsFx))
					throw new InvalidOperationException (string.Format (
						"Runtime '{0}' does not have the MSBuild '{1}' framework installed",
						runtime.Id, toolsVersion));
				
				string builderKey = runtime.Id + " " + toolsVersion;
				RemoteBuildEngine builder;
				if (builders.TryGetValue (builderKey, out builder)) {
					builder.ReferenceCount++;
					return new RemoteProjectBuilder (file, binDir, builder);
				}

				//always start the remote process explicitly, even if it's using the current runtime and fx
				//else it won't pick up the assembly redirects from the builder exe
				var exe = GetExeLocation (runtime, toolsVersion);
				MonoDevelop.Core.Execution.RemotingService.RegisterRemotingChannel ();
				var pinfo = new ProcessStartInfo (exe) {
					UseShellExecute = false,
					CreateNoWindow = true,
					RedirectStandardError = true,
					RedirectStandardInput = true,
				};
				runtime.GetToolsExecutionEnvironment (toolsFx).MergeTo (pinfo);
				
				Process p = null;
				try {
					p = runtime.ExecuteAssembly (pinfo);
					p.StandardInput.WriteLine (Process.GetCurrentProcess ().Id.ToString ());
					string sref = p.StandardError.ReadLine ();
					byte[] data = Convert.FromBase64String (sref);
					MemoryStream ms = new MemoryStream (data);
					BinaryFormatter bf = new BinaryFormatter ();
					builder = new RemoteBuildEngine (p, (IBuildEngine) bf.Deserialize (ms));
				} catch {
					if (p != null) {
						try {
							p.Kill ();
						} catch { }
					}
					throw;
				}
				
				builders [builderKey] = builder;
				builder.ReferenceCount = 1;
				return new RemoteProjectBuilder (file, binDir, builder);
			}
		}
		//FIXME: the fallback is broken since multiple frameworks can have the same corlib
		public TargetFrameworkMoniker GetTargetFrameworkForAssembly (TargetRuntime tr, string file)
		{
			if (!File.Exists (file))
				return TargetFrameworkMoniker.UNKNOWN;
			var universe = CreateClosedUniverse ();
			try {
				IKVM.Reflection.Assembly assembly = universe.LoadFile (file);
				var att = assembly.CustomAttributes.FirstOrDefault (a =>
					a.AttributeType.FullName == "System.Runtime.Versioning.TargetFrameworkAttribute"
				);
				if (att != null) {
					if (att.ConstructorArguments.Count == 1) {
						var v = att.ConstructorArguments[0].Value as string;
						TargetFrameworkMoniker m;
						if (v != null && TargetFrameworkMoniker.TryParse (v, out m)) {
							return m;
						}
					}
					LoggingService.LogError ("Invalid TargetFrameworkAttribute in assembly {0}", file);
				}
				if (tr != null) {
					foreach (var r in assembly.GetReferencedAssemblies ()) {
						if (r.Name == "mscorlib") {
							TargetFramework compatibleFramework = null;
							// If there are several frameworks that can run the file, pick one that is installed
							foreach (TargetFramework tf in GetKnownFrameworks ()) {
								if (tf.GetCorlibVersion () == r.Version.ToString ()) {
									compatibleFramework = tf;
									if (tr.IsInstalled (tf))
										return tf.Id;
								}
							}
							if (compatibleFramework != null)
								return compatibleFramework.Id;
							break;
						}
					}
				}
			} catch (Exception ex) {
				LoggingService.LogError ("Error determining target framework for assembly {0}: {1}", file, ex);
				return TargetFrameworkMoniker.UNKNOWN;
			} finally {
				universe.Dispose ();
			}
			LoggingService.LogError ("Failed to determine target framework for assembly {0}", file);
			return TargetFrameworkMoniker.UNKNOWN;
		}
Пример #4
0
		public static RemoteProjectBuilder GetProjectBuilder (TargetRuntime runtime, string toolsVersion, string file)
		{
			lock (builders) {
				var toolsFx = Runtime.SystemAssemblyService.GetTargetFramework (toolsVersion);
				string binDir = runtime.GetMSBuildBinPath (toolsFx);
				
				if (!runtime.IsInstalled (toolsFx))
					throw new InvalidOperationException (string.Format (
						"Runtime '{0}' cannot be used to build MSBuild '{1}' format projects",
						runtime.Id, toolsVersion));
				
				string builderKey = runtime.Id + " " + toolsVersion;
				RemoteBuildEngine builder;
				if (builders.TryGetValue (builderKey, out builder)) {
					builder.ReferenceCount++;
					return new RemoteProjectBuilder (file, binDir, builder);
				}
				
				if (runtime.IsRunning && toolsVersion == REFERENCED_MSBUILD_TOOLS) {
					if (currentBuildEngine == null)
						currentBuildEngine = new RemoteBuildEngine (null, new BuildEngine ());
					return new RemoteProjectBuilder (file, binDir, currentBuildEngine);
				}
				else {
					string exe = GetExeLocation (toolsVersion);
					MonoDevelop.Core.Execution.RemotingService.RegisterRemotingChannel ();
					ProcessStartInfo pinfo = new ProcessStartInfo (exe);
					runtime.GetToolsExecutionEnvironment (toolsFx).MergeTo (pinfo);
					pinfo.UseShellExecute = false;
					pinfo.RedirectStandardError = true;
					pinfo.RedirectStandardInput = true;
					
					Process p = null;
					try {
						p = runtime.ExecuteAssembly (pinfo);
						p.StandardInput.WriteLine (Process.GetCurrentProcess ().Id.ToString ());
						string sref = p.StandardError.ReadLine ();
						byte[] data = Convert.FromBase64String (sref);
						MemoryStream ms = new MemoryStream (data);
						BinaryFormatter bf = new BinaryFormatter ();
						builder = new RemoteBuildEngine (p, (IBuildEngine) bf.Deserialize (ms));
					} catch {
						if (p != null) {
							try {
								p.Kill ();
							} catch { }
						}
						throw;
					}
				}
				builders [builderKey] = builder;
				builder.ReferenceCount = 1;
				return new RemoteProjectBuilder (file, binDir, builder);
			}
		}
Пример #5
0
        //FIXME: the fallback is broken since multiple frameworks can have the same corlib
        public TargetFrameworkMoniker GetTargetFrameworkForAssembly(TargetRuntime tr, string file)
        {
            if (!File.Exists(file))
            {
                return(TargetFrameworkMoniker.UNKNOWN);
            }

            try {
                using (var reader = new PEReader(File.OpenRead(file))) {
                    var mr = reader.GetMetadataReader();

                    foreach (var customAttributeHandle in mr.GetAssemblyDefinition().GetCustomAttributes())
                    {
                        var customAttribute = mr.GetCustomAttribute(customAttributeHandle);

                        var ctorHandle = customAttribute.Constructor;
                        if (ctorHandle.Kind != HandleKind.MemberReference)
                        {
                            continue;
                        }

                        var ctor     = mr.GetMemberReference((MemberReferenceHandle)ctorHandle);
                        var attrType = mr.GetTypeReference((TypeReferenceHandle)ctor.Parent);

                        var ns = mr.GetString(attrType.Namespace);
                        if (ns != "System.Runtime.Versioning")
                        {
                            continue;
                        }

                        var typeName = mr.GetString(attrType.Name);
                        if (typeName != "TargetFrameworkAttribute")
                        {
                            continue;
                        }

                        var provider       = new StringParameterValueTypeProvider(mr, customAttribute.Value);
                        var signature      = ctor.DecodeMethodSignature(provider, null);
                        var parameterTypes = signature.ParameterTypes;
                        if (parameterTypes.Length != 1)
                        {
                            continue;
                        }

                        var value = parameterTypes [0];
                        if (value != null && TargetFrameworkMoniker.TryParse(value, out var m))
                        {
                            return(m);
                        }
                        LoggingService.LogError("Invalid TargetFrameworkAttribute in assembly {0} - {1}", file, value);
                    }

                    if (tr != null)
                    {
                        foreach (var assemblyReferenceHandle in mr.AssemblyReferences)
                        {
                            var assemblyReference = mr.GetAssemblyReference(assemblyReferenceHandle);

                            var name = mr.GetString(assemblyReference.Name);
                            if (name != "mscorlib")
                            {
                                continue;
                            }

                            TargetFramework compatibleFramework = null;
                            // If there are several frameworks that can run the file, pick one that is installed
                            foreach (TargetFramework tf in GetKnownFrameworks())
                            {
                                if (tf.GetCorlibVersion() == assemblyReference.Version.ToString())
                                {
                                    compatibleFramework = tf;
                                    if (tr.IsInstalled(tf))
                                    {
                                        return(tf.Id);
                                    }
                                }
                            }
                            if (compatibleFramework != null)
                            {
                                return(compatibleFramework.Id);
                            }
                            break;
                        }
                    }
                }
            } catch (Exception ex) {
                LoggingService.LogError("Error determining target framework for assembly {0}: {1}", file, ex);
            }
            return(TargetFrameworkMoniker.UNKNOWN);
        }