private Assembly _universe_AssemblyResolve(object sender, IKVM.Reflection.ResolveEventArgs args) { AssemblyName name = new AssemblyName(args.Name); AssemblyName previousMatch = null; int previousMatchLevel = 0; foreach (Assembly asm in _universe.GetAssemblies()) { if (Match(asm.GetName(), name, ref previousMatch, ref previousMatchLevel)) { return(asm); } } foreach (string file in FindAssemblyPath(name.Name + ".dll")) { if (Match(AssemblyName.GetAssemblyName(file), name, ref previousMatch, ref previousMatchLevel)) { return(LoadFile(file)); } } if (args.RequestingAssembly != null) { string path = Path.Combine(Path.GetDirectoryName(args.RequestingAssembly.Location), name.Name + ".dll"); if (File.Exists(path) && Match(AssemblyName.GetAssemblyName(path), name, ref previousMatch, ref previousMatchLevel)) { return(LoadFile(path)); } } string hintpath; if (_hintpaths.TryGetValue(name.FullName, out hintpath)) { string path = Path.Combine(hintpath, name.Name + ".dll"); if (File.Exists(path) && Match(AssemblyName.GetAssemblyName(path), name, ref previousMatch, ref previousMatchLevel)) { return(LoadFile(path)); } } if (previousMatch != null) { if (previousMatchLevel == 2) { ConsoleOps.Warning("assuming assembly reference '{0}' matches '{1}', you may need to supply runtime policy", previousMatch.FullName, name.FullName); return(LoadFile(new Uri(previousMatch.CodeBase).LocalPath)); } else if (args.RequestingAssembly != null) { ConsoleOps.Error(true, "Assembly '{0}' uses '{1}' which has a higher version than referenced assembly '{2}'", args.RequestingAssembly.FullName, name.FullName, previousMatch.FullName); } else { ConsoleOps.Error(true, "Assembly '{0}' was requested which is a higher version than referenced assembly '{1}'", name.FullName, previousMatch.FullName); } } else { ConsoleOps.Error(true, "unable to find assembly '{0}' {1}", args.Name, args.RequestingAssembly != null ? string.Format(" (a dependency of '{0}')", args.RequestingAssembly.FullName) : string.Empty); } return(null); }
private Assembly LoadFile(string path) { string ex = null; try { using (RawModule module = _universe.OpenRawModule(path)) { if (_mscorlibVersion != null) { // to avoid problems (i.e. weird exceptions), we don't allow assemblies to load that reference a newer version of mscorlib foreach (AssemblyName asmref in module.GetReferencedAssemblies()) { if (asmref.Name == "mscorlib" && asmref.Version > _mscorlibVersion) { ConsoleOps.Error(true, "unable to load assembly '{0}' as it depends on a higher version of mscorlib than the one currently loaded", path); } } } Assembly asm = _universe.LoadAssembly(module); if (asm.Location != module.Location && CanonicalizePath(asm.Location) != CanonicalizePath(module.Location)) { ConsoleOps.Warning("assembly '{0}' is ignored as previously loaded assembly '{1}' has the same identity '{2}'", path, asm.Location, asm.FullName); } return(asm); } } catch (IOException x) { ex = x.Message; } catch (UnauthorizedAccessException x) { ex = x.Message; } catch (IKVM.Reflection.BadImageFormatException x) { ex = x.Message; } ConsoleOps.Error(true, "unable to load assembly '{0}'" + Environment.NewLine + " ({1})", path, ex); return(null); }
private IEnumerable <string> FindAssemblyPath(string file) { if (Path.IsPathRooted(file)) { if (File.Exists(file)) { yield return(file); } } else { foreach (string dir in _libpaths) { string path = Path.Combine(dir, file); if (File.Exists(path)) { yield return(path); } // for legacy compat, we try again after appending .dll path = Path.Combine(dir, file + ".dll"); if (File.Exists(path)) { ConsoleOps.Warning("Found assembly '{0}' using legacy search rule, please append '.dll' to the reference", file); yield return(path); } } } }
public AssemblyResolver(Universe universe, bool nostdlib, IEnumerable <string> references, IEnumerable <string> libpaths) { _universe = universe; _libpaths.Add(Environment.CurrentDirectory); _libpaths.AddRange(libpaths); if (!nostdlib) { _libpaths.Add(System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory()); } // items passed in via /lib:<path> foreach (string path in libpaths) { foreach (string dir in path.Split(new char[] { Path.PathSeparator }, StringSplitOptions.RemoveEmptyEntries)) { if (Directory.Exists(dir)) { _libpaths.Add(dir); } else { ConsoleOps.Warning("Directory specified ('{0}') by /lib: does not exist", dir); } } } string envLib = Environment.GetEnvironmentVariable("LIB"); if (!string.IsNullOrEmpty(envLib)) { foreach (string dir in envLib.Split(new char[] { Path.PathSeparator }, StringSplitOptions.RemoveEmptyEntries)) { if (Directory.Exists(dir)) { _libpaths.Add(dir); } else { ConsoleOps.Warning("Directory specified ('{0}') in LIB does not exist", dir); } } } if (nostdlib) { _mscorlibVersion = LoadMscorlib(references).GetName().Version; } else { _mscorlibVersion = universe.Load("mscorlib").GetName().Version; } _universe.AssemblyResolve += _universe_AssemblyResolve; }
public void ParseArgs(IEnumerable <string> args, List <string> respFiles = null) { foreach (var a in args) { var arg = a.Trim(); if (arg.StartsWith("#")) { continue; } if (arg.StartsWith("/main:")) { MainName = Main = arg.Substring(6).Trim('"'); // only override the target kind if its currently a DLL if (Target == PEFileKinds.Dll) { Target = PEFileKinds.ConsoleApplication; } } else if (arg.StartsWith("/out:")) { Output = arg.Substring(5).Trim('"'); } else if (arg.StartsWith("/target:")) { string tgt = arg.Substring(8).Trim('"'); switch (tgt) { case "exe": Target = PEFileKinds.ConsoleApplication; break; case "winexe": Target = PEFileKinds.WindowApplication; break; default: Target = PEFileKinds.Dll; break; } } else if (arg.StartsWith("/platform:")) { string plat = arg.Substring(10).Trim('"'); switch (plat) { case "x86": Platform = IKVM.Reflection.PortableExecutableKinds.ILOnly | IKVM.Reflection.PortableExecutableKinds.Required32Bit; Machine = IKVM.Reflection.ImageFileMachine.I386; break; case "x64": Platform = IKVM.Reflection.PortableExecutableKinds.ILOnly | IKVM.Reflection.PortableExecutableKinds.PE32Plus; Machine = IKVM.Reflection.ImageFileMachine.AMD64; break; default: Platform = IKVM.Reflection.PortableExecutableKinds.ILOnly; Machine = IKVM.Reflection.ImageFileMachine.I386; break; } } else if (arg.StartsWith("/win32icon:")) { Win32Icon = arg.Substring(11).Trim('"'); } else if (arg.StartsWith("/version:")) { Version = arg.Substring(9).Trim('"'); } else if (arg.StartsWith("/errfmt:")) { ErrorMessageFormat = arg.Substring(8); } else if (arg.StartsWith("/embed")) { Embed = true; } else if (arg.StartsWith("/standalone")) { Standalone = true; } else if (arg.StartsWith("/mta")) { UseMta = true; } else if (arg.StartsWith("/file_info")) { string[] items = arg.Substring(10).Split(':'); if (items.Length == 2) { switch (items[0].Trim()) { case "version": FileInfoVersion = new Version(items[1].Trim()); break; case "product": FileInfoProduct = items[1].Trim(); break; case "product_version": FileInfoProductVersion = items[1].Trim(); break; case "company": FileInfoCompany = items[1].Trim(); break; case "copyright": FileInfoCopyright = items[1].Trim(); break; case "trademark": FileInfoTrademark = items[1].Trim(); break; default: ConsoleOps.Warning("Unknown File Information option {0}", arg); break; } } } else if (Array.IndexOf(new string[] { "/?", "-?", "/h", "-h" }, args) >= 0) { ConsoleOps.Usage(true); } else { if (arg.StartsWith("@")) { var respFile = Path.GetFullPath(arg.Substring(1)); if (respFiles == null) { respFiles = new List <string>(); } if (!respFiles.Contains(respFile)) { respFiles.Add(respFile); ParseArgs(File.ReadAllLines(respFile), respFiles); } else { ConsoleOps.Warning("Already parsed response file '{0}'", arg.Substring(1)); } } else { Files.Add(arg); } } } }
public void ParseArgs(IEnumerable <string> args, List <string> respFiles = null) { var helpStrings = new string[] { "/?", "-?", "/h", "-h" }; foreach (var a in args) { var arg = a.Trim(); if (arg.StartsWith("#", StringComparison.Ordinal)) { continue; } if (arg.StartsWith("/main:", StringComparison.Ordinal)) { MainName = Main = arg.Substring(6).Trim('"'); // only override the target kind if its currently a DLL if (Target == PEFileKinds.Dll) { Target = PEFileKinds.ConsoleApplication; } } else if (arg.StartsWith("/out:", StringComparison.Ordinal)) { Output = arg.Substring(5).Trim('"'); } else if (arg.StartsWith("/target:", StringComparison.Ordinal)) { string tgt = arg.Substring(8).Trim('"'); switch (tgt) { case "exe": Target = PEFileKinds.ConsoleApplication; break; case "winexe": Target = PEFileKinds.WindowApplication; break; default: Target = PEFileKinds.Dll; break; } } else if (arg.StartsWith("/platform:", StringComparison.Ordinal)) { string plat = arg.Substring(10).Trim('"'); switch (plat) { case "x86": Platform = IKVM.Reflection.PortableExecutableKinds.ILOnly | IKVM.Reflection.PortableExecutableKinds.Required32Bit; Machine = IKVM.Reflection.ImageFileMachine.I386; break; case "x64": Platform = IKVM.Reflection.PortableExecutableKinds.ILOnly | IKVM.Reflection.PortableExecutableKinds.PE32Plus; Machine = IKVM.Reflection.ImageFileMachine.AMD64; break; default: Platform = IKVM.Reflection.PortableExecutableKinds.ILOnly; Machine = IKVM.Reflection.ImageFileMachine.AMD64; break; } } else if (arg.StartsWith("/win32icon:", StringComparison.Ordinal)) { Win32Icon = arg.Substring(11).Trim('"'); } else if (arg.StartsWith("/fileversion:", StringComparison.Ordinal)) { FileVersion = arg.Substring(13).Trim('"'); } else if (arg.StartsWith("/productversion:", StringComparison.Ordinal)) { ProductVersion = arg.Substring(16).Trim('"'); } else if (arg.StartsWith("/productname:", StringComparison.Ordinal)) { ProductName = arg.Substring(13).Trim('"'); } else if (arg.StartsWith("/copyright:", StringComparison.Ordinal)) { Copyright = arg.Substring(11).Trim('"'); } else if (arg.StartsWith("/errfmt:", StringComparison.Ordinal)) { ErrorMessageFormat = arg.Substring(8); } else if (arg.Equals("/embed", StringComparison.Ordinal)) { Embed = true; } else if (arg.Equals("/standalone", StringComparison.Ordinal)) { Standalone = true; } else if (arg.Equals("/mta", StringComparison.Ordinal)) { UseMta = true; } else if (arg.Equals("/sta", StringComparison.Ordinal)) { UseSta = true; } else if (arg.StartsWith("/recurse:", StringComparison.Ordinal)) { string pattern = arg.Substring(9); if (string.IsNullOrWhiteSpace(pattern)) { ConsoleOps.Error(true, "Missing pattern for /recurse option"); } foreach (var f in Directory.EnumerateFiles(Environment.CurrentDirectory, pattern)) { Files.Add(Path.GetFullPath(f)); } } else if (Array.IndexOf(helpStrings, arg) >= 0) { ConsoleOps.Usage(true); } else if (arg.StartsWith("/py:", StringComparison.Ordinal)) { // if you add a parameter that takes a different type then // ScriptingRuntimeHelpers.True/False or int // you need ot also modify Program.cs for standalone generation. string[] pyargs = arg.Substring(4).Trim('"').Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); switch (pyargs[0]) { case "-X:Frames": PythonOptions["Frames"] = ScriptingRuntimeHelpers.True; break; case "-X:FullFrames": PythonOptions["Frames"] = PythonOptions["FullFrames"] = ScriptingRuntimeHelpers.True; break; case "-X:Tracing": PythonOptions["Tracing"] = ScriptingRuntimeHelpers.True; break; case "-X:GCStress": int gcStress; if (!int.TryParse(pyargs[1], out gcStress) || (gcStress <0 || gcStress> GC.MaxGeneration)) { ConsoleOps.Error(true, $"The argument for the {pyargs[1]} option must be between 0 and {GC.MaxGeneration}."); } PythonOptions["GCStress"] = gcStress; break; case "-X:MaxRecursion": // we need about 6 frames for starting up, so 10 is a nice round number. int limit; if (!int.TryParse(pyargs[1], out limit) || limit < 10) { ConsoleOps.Error(true, $"The argument for the {pyargs[1]} option must be an integer >= 10."); } PythonOptions["RecursionLimit"] = limit; break; case "-X:EnableProfiler": PythonOptions["EnableProfiler"] = ScriptingRuntimeHelpers.True; break; case "-X:LightweightScopes": PythonOptions["LightweightScopes"] = ScriptingRuntimeHelpers.True; break; case "-X:Debug": PythonOptions["Debug"] = ScriptingRuntimeHelpers.True; break; } } else { if (arg.StartsWith("@", StringComparison.Ordinal)) { var respFile = Path.GetFullPath(arg.Substring(1)); if (respFiles == null) { respFiles = new List <string>(); } if (!respFiles.Contains(respFile)) { respFiles.Add(respFile); ParseArgs(File.ReadAllLines(respFile), respFiles); } else { ConsoleOps.Warning($"Already parsed response file '{arg.Substring(1)}'"); } } else { if (arg.ToLower().EndsWith(".dll", StringComparison.Ordinal)) { DLLs.Add(arg); } else { Files.Add(arg); } } } } }