From 21f4a6aeca3e5ae70a0f46fb5185a0a2c6fbb1ef Mon Sep 17 00:00:00 2001 From: TermiNexus Date: Wed, 14 Oct 2026 13:47:36 +0800 Subject: [PATCH] =?UTF-8?q?fix(build):=20kernel.elf=20=E9=93=BE=E6=8E=A5?= =?UTF-8?q?=E5=91=BD=E4=BB=A4=E6=94=B9=E5=86=99=E5=81=9A=E7=A8=B3=E5=81=A5?= =?UTF-8?q?=E5=8C=96=EF=BC=8C=E9=81=BF=E5=85=8D=E8=AF=AF=E8=A6=86=E7=9B=96?= =?UTF-8?q?=20kernel.bin?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 原改写用字面 replace("--oformat binary"/"kernel.bin"),若 .bat 里的写法是 "--oformat=binary" 或输出名大小写/路径不同就会匹配不中——那种情况下执行的 仍是原始命令,会把 kernel.bin 覆盖成非预期产物。现改为正则同时兜住 "--oformat binary"/"--oformat=binary"、"-o "/"--output=", 并加保护:改写后若不包含 kernel.elf 则直接跳过,绝不执行未改写的命令。 --- build.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/build.py b/build.py index e441b7b..c713b1e 100644 --- a/build.py +++ b/build.py @@ -1,5 +1,6 @@ import shutil import os +import re import sys import subprocess import threading @@ -39,11 +40,19 @@ if os.path.exists(p2c_bat): if "ld.lld" in low and (".bin" in low or "output" in low): link_line = line.rstrip("\r\n") if link_line is not None: - elf_line = link_line.replace("--oformat binary", "").replace("--strip-all", "") - elf_line = elf_line.replace("kernel.bin", "kernel.elf") - print("[build] linking kernel.elf for analysis ...") - if os.system(elf_line) != 0: - print("[build] 警告: kernel.elf 链接失败(不影响 kernel.bin)") + # 稳健改写:--oformat 可能是 "--oformat binary" 或 "--oformat=binary";输出可能是 + # "-o " 或 "--output="。任一没匹配上就放弃(绝不去执行原样命令,否则会把 + # kernel.bin 覆盖成非预期产物)。 + elf_line = re.sub(r"--oformat[ =]binary", "", link_line) + elf_line = re.sub(r"--strip-all", "", elf_line) + elf_line = re.sub(r"(-o\s+)\S+", r"\1" + kernel_elf, elf_line) + elf_line = re.sub(r"(--output=)\S+", r"\1" + kernel_elf, elf_line) + if elf_line == link_line or kernel_elf not in elf_line: + print("[build] 警告: 未能改写链接命令,跳过 kernel.elf(避免误覆盖 kernel.bin)") + else: + print("[build] linking kernel.elf for analysis ...") + if os.system(elf_line) != 0: + print("[build] 警告: kernel.elf 链接失败(不影响 kernel.bin)") else: print("[build] 警告: 未从 phase2command 取到链接命令,跳过 kernel.elf")