开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
main
分支 (3)
标签 (5)
main
3.6
master
v3.4.2
v3.4.1
v3.4.0
v3.4.0-beta1
v3.4.0-alpha1
main
分支 (3)
标签 (5)
main
3.6
master
v3.4.2
v3.4.1
v3.4.0
v3.4.0-beta1
v3.4.0-alpha1
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
main
分支 (3)
标签 (5)
main
3.6
master
v3.4.2
v3.4.1
v3.4.0
v3.4.0-beta1
v3.4.0-alpha1
Program.cs 19.15 KB
一键复制 编辑 原始数据 按行查看 历史
slozier 提交于 2025年01月26日 09:48 +08:00 . Rename projects (#1882)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Resources;
using IKVM.Reflection;
using IKVM.Reflection.Emit;
using IronPython.Hosting;
using IronPython.Runtime;
using IronPython.Runtime.Operations;
using Microsoft.Scripting.Hosting;
using Microsoft.Scripting.Runtime;
using Type = IKVM.Reflection.Type;
namespace IronPython.Compiler {
public class Program {
/// <summary>
/// Generates the stub .exe file for starting the app
/// </summary>
/// <param name="config"></param>
private static void GenerateExe(Config config) {
var u = new Universe();
var aName = new AssemblyName(Path.GetFileNameWithoutExtension(new FileInfo(config.Output).Name));
var ab = u.DefineDynamicAssembly(aName, AssemblyBuilderAccess.Save, config.OutputPath);
var mb = ab.DefineDynamicModule(config.Output, aName.Name + (aName.Name.EndsWith(".exe", StringComparison.Ordinal) ? string.Empty : ".exe"));
var tb = mb.DefineType("PythonMain", IKVM.Reflection.TypeAttributes.Public);
if (!string.IsNullOrEmpty(config.Win32Icon)) {
ab.__DefineIconResource(File.ReadAllBytes(config.Win32Icon));
}
var attributes = new List<Tuple<string, Type>> {
Tuple.Create(config.FileVersion, u.Import(typeof(System.Reflection.AssemblyFileVersionAttribute))),
Tuple.Create(config.ProductName, u.Import(typeof(System.Reflection.AssemblyProductAttribute))),
Tuple.Create(config.ProductVersion, u.Import(typeof(System.Reflection.AssemblyInformationalVersionAttribute))),
Tuple.Create(config.Copyright, u.Import(typeof(System.Reflection.AssemblyCopyrightAttribute)))
};
foreach (var attr in attributes) {
if (!string.IsNullOrWhiteSpace(config.FileVersion)) {
CustomAttributeBuilder builder = new CustomAttributeBuilder(attr.Item2.GetConstructor(new[] { u.Import(typeof(string)) }), new object[] { attr.Item1 });
ab.SetCustomAttribute(builder);
}
}
ab.DefineVersionInfoResource();
MethodBuilder assemblyResolveMethod = null;
ILGenerator gen = null;
if (config.Standalone) {
ConsoleOps.Info("Generating stand alone executable");
config.Embed = true;
var embedAssemblies = new HashSet<string> {
// DLR
"Microsoft.Dynamic",
"Microsoft.Scripting",
// System.Memory
"System.Buffers",
"System.Memory",
"System.Numerics.Vectors",
"System.Runtime.CompilerServices.Unsafe",
};
foreach (var a in System.AppDomain.CurrentDomain.GetAssemblies()) {
var n = new AssemblyName(a.FullName);
if (!a.IsDynamic && a.EntryPoint == null && (n.Name.StartsWith("IronPython", StringComparison.Ordinal) || embedAssemblies.Contains(n.Name))) {
ConsoleOps.Info($"\tEmbedded {n.Name}{n.Version}");
var f = new FileStream(a.Location, FileMode.Open, FileAccess.Read);
mb.DefineManifestResource("Dll." + n.Name, f, IKVM.Reflection.ResourceAttributes.Public);
}
}
foreach (var dll in config.DLLs) {
var name = Path.GetFileNameWithoutExtension(dll);
ConsoleOps.Info($"\tEmbedded {name}");
var f = new FileStream(dll, FileMode.Open, FileAccess.Read);
mb.DefineManifestResource("Dll." + name, f, IKVM.Reflection.ResourceAttributes.Public);
}
// we currently do no error checking on what is passed in to the assemblyresolve event handler
assemblyResolveMethod = tb.DefineMethod("AssemblyResolve", MethodAttributes.Public | MethodAttributes.Static, u.Import(typeof(System.Reflection.Assembly)), new IKVM.Reflection.Type[] { u.Import(typeof(System.Object)), u.Import(typeof(System.ResolveEventArgs)) });
gen = assemblyResolveMethod.GetILGenerator();
var s = gen.DeclareLocal(u.Import(typeof(System.IO.Stream))); // resource stream
gen.Emit(OpCodes.Ldnull);
gen.Emit(OpCodes.Stloc, s);
var d = gen.DeclareLocal(u.Import(typeof(byte[]))); // data buffer;
gen.EmitCall(OpCodes.Call, u.Import(typeof(System.Reflection.Assembly)).GetMethod("GetEntryAssembly"), Type.EmptyTypes);
gen.Emit(OpCodes.Ldstr, "Dll.");
gen.Emit(OpCodes.Ldarg_1); // The event args
gen.EmitCall(OpCodes.Callvirt, u.Import(typeof(System.ResolveEventArgs)).GetMethod("get_Name"), Type.EmptyTypes);
gen.Emit(OpCodes.Newobj, u.Import(typeof(System.Reflection.AssemblyName)).GetConstructor(new IKVM.Reflection.Type[] { u.Import(typeof(string)) }));
gen.EmitCall(OpCodes.Call, u.Import(typeof(System.Reflection.AssemblyName)).GetMethod("get_Name"), Type.EmptyTypes);
gen.EmitCall(OpCodes.Call, u.Import(typeof(string)).GetMethod("Concat", new IKVM.Reflection.Type[] { u.Import(typeof(string)), u.Import(typeof(string)) }), Type.EmptyTypes);
gen.EmitCall(OpCodes.Callvirt, u.Import(typeof(System.Reflection.Assembly)).GetMethod("GetManifestResourceStream", new IKVM.Reflection.Type[] { u.Import(typeof(string)) }), Type.EmptyTypes);
gen.Emit(OpCodes.Stloc, s);
gen.Emit(OpCodes.Ldloc, s);
gen.EmitCall(OpCodes.Callvirt, u.Import(typeof(System.IO.Stream)).GetMethod("get_Length"), Type.EmptyTypes);
gen.Emit(OpCodes.Newarr, u.Import(typeof(System.Byte)));
gen.Emit(OpCodes.Stloc, d);
gen.Emit(OpCodes.Ldloc, s);
gen.Emit(OpCodes.Ldloc, d);
gen.Emit(OpCodes.Ldc_I4_0);
gen.Emit(OpCodes.Ldloc, s);
gen.EmitCall(OpCodes.Callvirt, u.Import(typeof(System.IO.Stream)).GetMethod("get_Length"), Type.EmptyTypes);
gen.Emit(OpCodes.Conv_I4);
gen.EmitCall(OpCodes.Callvirt, u.Import(typeof(System.IO.Stream)).GetMethod("Read", new IKVM.Reflection.Type[] { u.Import(typeof(byte[])), u.Import(typeof(int)), u.Import(typeof(int)) }), Type.EmptyTypes);
gen.Emit(OpCodes.Pop);
gen.Emit(OpCodes.Ldloc, d);
gen.EmitCall(OpCodes.Call, u.Import(typeof(System.Reflection.Assembly)).GetMethod("Load", new IKVM.Reflection.Type[] { u.Import(typeof(byte[])) }), Type.EmptyTypes);
gen.Emit(OpCodes.Ret);
// generate a static constructor to assign the AssemblyResolve handler (otherwise it tries to use IronPython before it adds the handler)
// the other way of handling this would be to move the call to InitializeModule into a separate method.
var staticConstructor = tb.DefineConstructor(MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, Type.EmptyTypes);
gen = staticConstructor.GetILGenerator();
gen.EmitCall(OpCodes.Call, u.Import(typeof(System.AppDomain)).GetMethod("get_CurrentDomain"), Type.EmptyTypes);
gen.Emit(OpCodes.Ldnull);
gen.Emit(OpCodes.Ldftn, assemblyResolveMethod);
gen.Emit(OpCodes.Newobj, u.Import(typeof(System.ResolveEventHandler)).GetConstructor(new IKVM.Reflection.Type[] { u.Import(typeof(object)), u.Import(typeof(System.IntPtr)) }));
gen.EmitCall(OpCodes.Callvirt, u.Import(typeof(System.AppDomain)).GetMethod("add_AssemblyResolve"), Type.EmptyTypes);
gen.Emit(OpCodes.Ret);
}
var mainMethod = tb.DefineMethod("Main", MethodAttributes.Public | MethodAttributes.Static, u.Import(typeof(int)), Type.EmptyTypes);
if (config.Target == PEFileKinds.WindowApplication && config.UseMta) {
mainMethod.SetCustomAttribute(u.Import(typeof(System.MTAThreadAttribute)).GetConstructor(Type.EmptyTypes), Array.Empty<byte>());
} else if (config.Target == PEFileKinds.WindowApplication || config.Target == PEFileKinds.ConsoleApplication && config.UseSta) {
mainMethod.SetCustomAttribute(u.Import(typeof(System.STAThreadAttribute)).GetConstructor(Type.EmptyTypes), Array.Empty<byte>());
}
gen = mainMethod.GetILGenerator();
// variables for saving original working directory and return code of script
var strVar = gen.DeclareLocal(u.Import(typeof(string)));
var intVar = gen.DeclareLocal(u.Import(typeof(int)));
LocalBuilder dictVar = null;
if (config.PythonOptions.Count > 0) {
var True = u.Import(typeof(ScriptingRuntimeHelpers)).GetField("True");
var False = u.Import(typeof(ScriptingRuntimeHelpers)).GetField("False");
dictVar = gen.DeclareLocal(u.Import(typeof(Dictionary<string, object>)));
gen.Emit(OpCodes.Newobj, u.Import(typeof(Dictionary<string, object>)).GetConstructor(Type.EmptyTypes));
gen.Emit(OpCodes.Stloc, dictVar);
foreach (var option in config.PythonOptions) {
gen.Emit(OpCodes.Ldloc, dictVar);
gen.Emit(OpCodes.Ldstr, option.Key);
if (option.Value is int val) {
if (val >= -128 && val <= 127)
gen.Emit(OpCodes.Ldc_I4_S, val); // this is more optimized
else
gen.Emit(OpCodes.Ldc_I4, val);
gen.Emit(OpCodes.Box, u.Import(typeof(System.Int32)));
} else if (option.Value.Equals(ScriptingRuntimeHelpers.True)) {
gen.Emit(OpCodes.Ldsfld, True);
} else if (option.Value.Equals(ScriptingRuntimeHelpers.False)) {
gen.Emit(OpCodes.Ldsfld, False);
}
gen.EmitCall(OpCodes.Callvirt, u.Import(typeof(Dictionary<string, object>)).GetMethod("Add", new IKVM.Reflection.Type[] { u.Import(typeof(string)), u.Import(typeof(object)) }), Type.EmptyTypes);
}
}
Label tryStart = gen.BeginExceptionBlock();
// get the ScriptCode assembly...
if (config.Embed) {
// put the generated DLL into the resources for the stub exe
var mem = new MemoryStream();
var rw = new ResourceWriter(mem);
rw.AddResource("IPDll." + Path.GetFileNameWithoutExtension(config.Output) + ".dll", File.ReadAllBytes(Path.Combine(config.OutputPath, config.Output) + ".dll"));
rw.Generate();
mem.Position = 0;
mb.DefineManifestResource("IPDll.resources", mem, ResourceAttributes.Public);
File.Delete(Path.Combine(config.OutputPath, config.Output) + ".dll");
// generate code to load the resource
gen.Emit(OpCodes.Ldstr, "IPDll");
gen.EmitCall(OpCodes.Call, u.Import(typeof(System.Reflection.Assembly)).GetMethod("GetEntryAssembly"), Type.EmptyTypes);
gen.Emit(OpCodes.Newobj, u.Import(typeof(System.Resources.ResourceManager)).GetConstructor(new IKVM.Reflection.Type[] { u.Import(typeof(string)), u.Import(typeof(System.Reflection.Assembly)) }));
gen.Emit(OpCodes.Ldstr, "IPDll." + Path.GetFileNameWithoutExtension(config.Output) + ".dll");
gen.EmitCall(OpCodes.Call, u.Import(typeof(System.Resources.ResourceManager)).GetMethod("GetObject", new IKVM.Reflection.Type[] { u.Import(typeof(string)) }), Type.EmptyTypes);
gen.EmitCall(OpCodes.Call, u.Import(typeof(System.Reflection.Assembly)).GetMethod("Load", new IKVM.Reflection.Type[] { u.Import(typeof(byte[])) }), Type.EmptyTypes);
} else {
// save current working directory
gen.EmitCall(OpCodes.Call, u.Import(typeof(System.Environment)).GetMethod("get_CurrentDirectory"), Type.EmptyTypes);
gen.Emit(OpCodes.Stloc, strVar);
gen.EmitCall(OpCodes.Call, u.Import(typeof(System.Reflection.Assembly)).GetMethod("GetEntryAssembly"), Type.EmptyTypes);
gen.EmitCall(OpCodes.Callvirt, u.Import(typeof(System.Reflection.Assembly)).GetMethod("get_Location"), Type.EmptyTypes);
gen.Emit(OpCodes.Newobj, u.Import(typeof(System.IO.FileInfo)).GetConstructor(new IKVM.Reflection.Type[] { u.Import(typeof(string)) }));
gen.EmitCall(OpCodes.Call, u.Import(typeof(System.IO.FileInfo)).GetMethod("get_Directory"), Type.EmptyTypes);
gen.EmitCall(OpCodes.Call, u.Import(typeof(System.IO.DirectoryInfo)).GetMethod("get_FullName"), Type.EmptyTypes);
gen.EmitCall(OpCodes.Call, u.Import(typeof(System.Environment)).GetMethod("set_CurrentDirectory"), Type.EmptyTypes);
gen.Emit(OpCodes.Ldstr, config.Output + ".dll");
gen.EmitCall(OpCodes.Call, u.Import(typeof(System.IO.Path)).GetMethod("GetFullPath", new IKVM.Reflection.Type[] { u.Import(typeof(string)) }), Type.EmptyTypes);
// result of GetFullPath stays on the stack during the restore of the
// original working directory
// restore original working directory
gen.Emit(OpCodes.Ldloc, strVar);
gen.EmitCall(OpCodes.Call, u.Import(typeof(System.Environment)).GetMethod("set_CurrentDirectory"), Type.EmptyTypes);
// for the LoadFile() call, the full path of the assembly is still is on the stack
// as the result from the call to GetFullPath()
gen.EmitCall(OpCodes.Call, u.Import(typeof(System.Reflection.Assembly)).GetMethod("LoadFile", new IKVM.Reflection.Type[] { u.Import(typeof(string)) }), Type.EmptyTypes);
}
// emit module name
gen.Emit(OpCodes.Ldstr, "__main__"); // main module name
gen.Emit(OpCodes.Ldnull); // no references
gen.Emit(OpCodes.Ldc_I4_0); // don't ignore environment variables for engine startup
if (config.PythonOptions.Count > 0) {
gen.Emit(OpCodes.Ldloc, dictVar);
} else {
gen.Emit(OpCodes.Ldnull);
}
// call InitializeModuleEx
// (this will also run the script)
// and put the return code on the stack
gen.EmitCall(OpCodes.Call, u.Import(typeof(PythonOps)).GetMethod(nameof(PythonOps.InitializeModuleEx),
new IKVM.Reflection.Type[] { u.Import(typeof(System.Reflection.Assembly)), u.Import(typeof(string)), u.Import(typeof(string[])), u.Import(typeof(bool)), u.Import(typeof(Dictionary<string, object>)) }),
Type.EmptyTypes);
gen.Emit(OpCodes.Stloc, intVar);
gen.BeginCatchBlock(u.Import(typeof(Exception)));
if (config.Target == PEFileKinds.ConsoleApplication) {
gen.EmitCall(OpCodes.Callvirt, u.Import(typeof(System.Exception)).GetMethod("get_Message", Type.EmptyTypes), Type.EmptyTypes);
gen.Emit(OpCodes.Stloc, strVar);
gen.Emit(OpCodes.Ldstr, config.ErrorMessageFormat);
gen.Emit(OpCodes.Ldloc, strVar);
gen.EmitCall(OpCodes.Call, u.Import(typeof(System.Console)).GetMethod("WriteLine", new IKVM.Reflection.Type[] { u.Import(typeof(string)), u.Import(typeof(string)) }), Type.EmptyTypes);
} else {
gen.EmitCall(OpCodes.Callvirt, u.Import(typeof(System.Exception)).GetMethod("get_Message", Type.EmptyTypes), Type.EmptyTypes);
gen.Emit(OpCodes.Stloc, strVar);
gen.Emit(OpCodes.Ldstr, config.ErrorMessageFormat);
gen.Emit(OpCodes.Ldloc, strVar);
gen.EmitCall(OpCodes.Call, u.Import(typeof(string)).GetMethod("Format", new IKVM.Reflection.Type[] { u.Import(typeof(string)), u.Import(typeof(string)) }), Type.EmptyTypes);
gen.Emit(OpCodes.Ldstr, "Error");
gen.Emit(OpCodes.Ldc_I4, (int)System.Windows.Forms.MessageBoxButtons.OK);
gen.Emit(OpCodes.Ldc_I4, (int)System.Windows.Forms.MessageBoxIcon.Error);
gen.EmitCall(OpCodes.Call, u.Import(typeof(System.Windows.Forms.MessageBox)).GetMethod("Show", new IKVM.Reflection.Type[] { u.Import(typeof(string)), u.Import(typeof(string)), u.Import(typeof(System.Windows.Forms.MessageBoxButtons)), u.Import(typeof(System.Windows.Forms.MessageBoxIcon)) }), Type.EmptyTypes);
gen.Emit(OpCodes.Pop);
}
gen.Emit(OpCodes.Ldc_I4, -1); // return code is -1 to show failure
gen.Emit(OpCodes.Stloc, intVar);
gen.EndExceptionBlock();
gen.Emit(OpCodes.Ldloc, intVar);
gen.Emit(OpCodes.Ret);
tb.CreateType();
ab.SetEntryPoint(mainMethod, config.Target);
string fileName = aName.Name.EndsWith(".exe", StringComparison.Ordinal) ? aName.Name : aName.Name + ".exe";
ab.Save(fileName, config.Platform, config.Machine);
}
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;
}
}
}
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/MicroDistanceStudio/ironpython3.git
git@gitee.com:MicroDistanceStudio/ironpython3.git
MicroDistanceStudio
ironpython3
ironpython3
main
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /