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); }
public bool Validate() { if (Files.Count == 1 && string.IsNullOrWhiteSpace(MainName)) { MainName = Files[0]; } if (Files.Count == 0 && !string.IsNullOrWhiteSpace(MainName)) { Files.Add(MainName); } if (Files == null || Files.Count == 0 || string.IsNullOrEmpty(MainName)) { ConsoleOps.Error("No files or main defined"); return(false); } if (Target != PEFileKinds.Dll && string.IsNullOrEmpty(MainName)) { ConsoleOps.Error("EXEs require /main:<filename> to be specified"); return(false); } if (DLLs.Count > 0 && !Standalone) { ConsoleOps.Error("DLLs can only be used in standalone mode"); return(false); } if (string.IsNullOrWhiteSpace(Output) && !string.IsNullOrWhiteSpace(MainName)) { Output = Path.GetFileNameWithoutExtension(MainName); OutputPath = Path.GetDirectoryName(MainName); } else if (string.IsNullOrWhiteSpace(Output) && Files != null && Files.Count > 0) { Output = Path.GetFileNameWithoutExtension(Files[0]); OutputPath = Path.GetDirectoryName(Files[0]); } if (!string.IsNullOrWhiteSpace(Win32Icon) && Target == PEFileKinds.Dll) { ConsoleOps.Error("DLLs may not have a win32icon"); return(false); } else if (!string.IsNullOrWhiteSpace(Win32Icon) && !File.Exists(Win32Icon)) { ConsoleOps.Error($"win32icon '{Win32Icon}' does not exist"); return(false); } return(true); }
public static int Main(string[] args) { var files = new List <string>(); var config = new Config(); config.ParseArgs(args); if (!config.Validate()) { ConsoleOps.Usage(true); } // we don't use the engine, but we create it so we can have a default context. ScriptEngine engine = Python.CreateEngine(config.PythonOptions); ConsoleOps.Info($"IronPython Compiler for {engine.Setup.DisplayName} ({engine.LanguageVersion})"); ConsoleOps.Info($"{config}"); ConsoleOps.Info("compiling..."); var compileOptions = new Dictionary <string, object>() { { "mainModule", config.MainName }, { "assemblyFileVersion", config.FileVersion }, { "copyright", config.Copyright }, { "productName", config.ProductName }, { "productVersion", config.ProductVersion }, }; try { ClrModule.CompileModules(DefaultContext.DefaultCLS, Path.Combine(config.OutputPath, Path.ChangeExtension(config.Output, ".dll")), compileOptions, config.Files.ToArray()); var outputfilename = Path.Combine(config.OutputPath, Path.ChangeExtension(config.Output, ".dll")); if (config.Target != PEFileKinds.Dll) { outputfilename = Path.Combine(config.OutputPath, Path.ChangeExtension(config.Output, ".exe")); GenerateExe(config); } ConsoleOps.Info($"Saved to {outputfilename}"); } catch (Exception e) { Console.WriteLine(); ConsoleOps.Error(true, e.Message); } return(0); }
public bool Validate() { if (Files.Count == 1 && string.IsNullOrEmpty(MainName)) { MainName = Files[0]; } if (Files == null || Files.Count == 0 || string.IsNullOrEmpty(MainName)) { ConsoleOps.Error("No files or main defined"); return(false); } if (Target != PEFileKinds.Dll && string.IsNullOrEmpty(MainName)) { ConsoleOps.Error("EXEs require /main:<filename> to be specified"); return(false); } if (string.IsNullOrEmpty(Output) && !string.IsNullOrEmpty(MainName)) { Output = Path.GetFileNameWithoutExtension(MainName); } else if (string.IsNullOrEmpty(Output) && Files != null && Files.Count > 0) { Output = Path.GetFileNameWithoutExtension(Files[0]); } if (!string.IsNullOrEmpty(Win32Icon) && Target == PEFileKinds.Dll) { ConsoleOps.Error("DLLs may not have a win32icon"); return(false); } else if (!string.IsNullOrEmpty(Win32Icon) && !File.Exists(Win32Icon)) { ConsoleOps.Error("win32icon '{0}' does not exist", Win32Icon); return(false); } return(true); }
private Assembly LoadMscorlib(IEnumerable <string> references) { if (references != null) { foreach (string r in references) { try { if (AssemblyName.GetAssemblyName(r).Name == "mscorlib") { return(LoadFile(r)); } } catch { } } } foreach (string mscorlib in FindAssemblyPath("mscorlib.dll")) { return(LoadFile(mscorlib)); } ConsoleOps.Error(true, "unable to find mscorlib.dll"); return(null); }
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); } } } } }