修正了一些错误
This commit is contained in:
@@ -10,6 +10,7 @@ import w32.win32base
|
||||
import subprocess
|
||||
import viperlib
|
||||
import ast
|
||||
import lib.core.VLogger as VLogger
|
||||
import lib.core.Handles.HandlesTranslator as HandlesTranslator
|
||||
import lib.core.Handles.HandlesStruct as HandlesStruct
|
||||
import lib.core.Handles.HandlesType as HandlesType
|
||||
@@ -68,6 +69,7 @@ def TranslateFileGetTrans(mb: memhub.MemBuddy | t.CPtr, file_path: str,
|
||||
bytes_read: LONG = f.read_all(src_buf, SRC_BUF_SIZE)
|
||||
f.close()
|
||||
if bytes_read <= 0:
|
||||
stdlib.free(src_buf)
|
||||
return None
|
||||
if bytes_read < SRC_BUF_SIZE:
|
||||
src_buf[bytes_read] = 0
|
||||
@@ -77,6 +79,8 @@ def TranslateFileGetTrans(mb: memhub.MemBuddy | t.CPtr, file_path: str,
|
||||
ast._lexer_init(lx, src_buf, mb)
|
||||
tokens: ast.Token | t.CPtr = ast.tokenize(lx)
|
||||
tree: ast.AST | t.CPtr = ast.parse_tokens(mb, tokens)
|
||||
# parse 完成后 AST 树已独立,释放源代码缓冲区(避免 1MB/文件泄漏)
|
||||
stdlib.free(src_buf)
|
||||
if tree is None:
|
||||
return None
|
||||
tr: HandlesTranslator.Translator | t.CPtr = mb.alloc(HandlesTranslator.Translator.__sizeof__())
|
||||
@@ -268,12 +272,27 @@ def compile_ll_to_obj(ir_path: str, output_dir: str, module_name: str, cc_cmd: s
|
||||
|
||||
result: subprocess.CompletedProcess | t.CPtr = subprocess.run(cmd, True, True)
|
||||
if result is None:
|
||||
stdio.printf("[FATAL][LLC] subprocess.run 返回 None: %s\n", module_name)
|
||||
fb: t.CChar | t.CPtr = VLogger.fmt_buf()
|
||||
if fb is not None:
|
||||
viperlib.snprintf(fb, 1024, "subprocess.run 返回 None: %s", module_name)
|
||||
VLogger.error(fb, "LLC")
|
||||
return 1
|
||||
if result.returncode != 0:
|
||||
stdio.printf("[FATAL][LLC] 编译失败 (module=%s, cmd=%s)\n", module_name, cmd)
|
||||
# 先直接输出 llc 的具体错误信息(VLogger.error 会 sys.exit,必须先输出)
|
||||
# 注意: subprocess 在 Windows 下将 stderr 合并到 stdout(si.hStdError = stdout_write)
|
||||
# 因此 result.stderr 总是 None,错误信息在 result.stdout 中
|
||||
# stdout 通常已含换行符,不再额外加 \n
|
||||
if result.stdout is not None:
|
||||
stdio.printf("[LLC] 输出:\n%s\n", result.stdout)
|
||||
stdio.printf("%s", result.stdout)
|
||||
stdio.fflush(0)
|
||||
if result.stderr is not None:
|
||||
stdio.printf("%s", result.stderr)
|
||||
stdio.fflush(0)
|
||||
# 最后输出编译失败摘要(VLogger.error 会 sys.exit)
|
||||
fb = VLogger.fmt_buf()
|
||||
if fb is not None:
|
||||
viperlib.snprintf(fb, 1024, "编译失败 (module=%s, cmd=%s)", module_name, cmd)
|
||||
VLogger.error(fb, "LLC")
|
||||
return 1
|
||||
return 0
|
||||
|
||||
@@ -370,9 +389,15 @@ def link_obj_to_exe(output_dir: str, module_name: str, linker_cmd: str, linker_f
|
||||
if includes_binary_dir is not None:
|
||||
extra_len = collect_obj_files(includes_binary_dir, extra_objs, EXTRA_BUF_SIZE)
|
||||
if extra_len > 0:
|
||||
stdio.printf("[link] 附加 %d 字节的 includes.binary .obj 文件\n", extra_len)
|
||||
fb: t.CChar | t.CPtr = VLogger.fmt_buf()
|
||||
if fb is not None:
|
||||
viperlib.snprintf(fb, 1024, "附加 %d 字节的 includes.binary .obj 文件", extra_len)
|
||||
VLogger.info(fb, "link")
|
||||
else:
|
||||
stdio.printf("[link] 警告: includes.binary 无 .obj 文件: %s\n", includes_binary_dir)
|
||||
fb = VLogger.fmt_buf()
|
||||
if fb is not None:
|
||||
viperlib.snprintf(fb, 1024, "includes.binary 无 .obj 文件: %s", includes_binary_dir)
|
||||
VLogger.warning(fb, "link")
|
||||
|
||||
# 构造命令: clang++ {obj_path} extra_objs -o {output_dir}/{linker_output} linker_flags
|
||||
# 注意: .obj 文件必须在 -l 库标志之前,否则链接器无法解析符号依赖
|
||||
@@ -391,16 +416,25 @@ def link_obj_to_exe(output_dir: str, module_name: str, linker_cmd: str, linker_f
|
||||
|
||||
result: subprocess.CompletedProcess | t.CPtr = subprocess.run(cmd, True, True)
|
||||
if result is None:
|
||||
stdio.printf("[link] subprocess.run 返回 None\n")
|
||||
VLogger.error("subprocess.run 返回 None", "link")
|
||||
return 1
|
||||
if result.returncode != 0:
|
||||
stdio.printf("[link] 链接失败,返回码: %d\n", result.returncode)
|
||||
stdio.printf("[link] 命令: %s\n", cmd)
|
||||
# 显示链接器错误输出(subprocess 将 stderr 合并到 stdout)
|
||||
fb: t.CChar | t.CPtr = VLogger.fmt_buf()
|
||||
if fb is not None:
|
||||
viperlib.snprintf(fb, 1024, "链接失败,返回码: %d", result.returncode)
|
||||
VLogger.error(fb, "link")
|
||||
fb = VLogger.fmt_buf()
|
||||
if fb is not None:
|
||||
viperlib.snprintf(fb, 1024, "命令: %s", cmd)
|
||||
VLogger.error(fb, "link")
|
||||
# 显示链接器错误输出
|
||||
# 注意: subprocess 在 Windows 下将 stderr 合并到 stdout,result.stderr 总是 None
|
||||
if result.stdout is not None:
|
||||
stdio.printf("[link] 链接器输出:\n%s\n", result.stdout)
|
||||
VLogger.error(result.stdout, "link")
|
||||
else:
|
||||
stdio.printf("[link] 无输出捕获\n")
|
||||
VLogger.error("无输出捕获", "link")
|
||||
if result.stderr is not None:
|
||||
VLogger.error(result.stderr, "link")
|
||||
return 1
|
||||
return 0
|
||||
|
||||
@@ -428,7 +462,10 @@ def compile_module_to_obj(ir_buf: bytes, ir_len: t.CSizeT,
|
||||
# Step 1: 写 .ll 文件
|
||||
ret: int = write_ir_to_file(ir_buf, ir_len, temp_dir, module_name)
|
||||
if ret != 0:
|
||||
stdio.printf("[compile] 写 .ll 失败: %s\n", module_name)
|
||||
fb: t.CChar | t.CPtr = VLogger.fmt_buf()
|
||||
if fb is not None:
|
||||
viperlib.snprintf(fb, 1024, "写 .ll 失败: %s", module_name)
|
||||
VLogger.error(fb, "compile")
|
||||
return 1
|
||||
|
||||
# Step 2: 构造切片 .ll 路径并编译 → .obj
|
||||
@@ -438,7 +475,10 @@ def compile_module_to_obj(ir_buf: bytes, ir_len: t.CSizeT,
|
||||
|
||||
ret = compile_ll_to_obj(ir_path, output_dir, module_name, cc_cmd, cc_flags)
|
||||
if ret != 0:
|
||||
stdio.printf("[compile] llc 编译失败: %s\n", module_name)
|
||||
fb = VLogger.fmt_buf()
|
||||
if fb is not None:
|
||||
viperlib.snprintf(fb, 1024, "llc 编译失败: %s", module_name)
|
||||
VLogger.error(fb, "compile")
|
||||
return 1
|
||||
return 0
|
||||
|
||||
@@ -486,13 +526,22 @@ def link_objs_to_exe(obj_paths: str, obj_paths_len: t.CSizeT,
|
||||
|
||||
result: subprocess.CompletedProcess | t.CPtr = subprocess.run(cmd, True, True)
|
||||
if result is None:
|
||||
stdio.printf("[link] subprocess.run 返回 None\n")
|
||||
VLogger.error("subprocess.run 返回 None", "link")
|
||||
return 1
|
||||
if result.returncode != 0:
|
||||
stdio.printf("[link] 链接失败,返回码: %d\n", result.returncode)
|
||||
stdio.printf("[link] 命令: %s\n", cmd)
|
||||
# 先直接输出 linker 的具体错误信息(VLogger.error 会 sys.exit,必须先输出)
|
||||
# stdout/stderr 通常已含换行符,不再额外加 \n
|
||||
if result.stdout is not None:
|
||||
stdio.printf("[link] 链接器输出:\n%s\n", result.stdout)
|
||||
stdio.printf("%s", result.stdout)
|
||||
stdio.fflush(0)
|
||||
if result.stderr is not None:
|
||||
stdio.printf("%s", result.stderr)
|
||||
stdio.fflush(0)
|
||||
# 最后输出链接失败摘要(VLogger.error 会 sys.exit)
|
||||
fb: t.CChar | t.CPtr = VLogger.fmt_buf()
|
||||
if fb is not None:
|
||||
viperlib.snprintf(fb, 1024, "链接失败,返回码: %d, 命令: %s", result.returncode, cmd)
|
||||
VLogger.error(fb, "link")
|
||||
return 1
|
||||
return 0
|
||||
|
||||
|
||||
Reference in New Issue
Block a user