made the linking step prefer ld.lld if avaliable else fall back to ld

This commit is contained in:
IgorCielniak
2025-12-29 13:35:26 +00:00
parent d4b8ce6012
commit acbd6f57da
2 changed files with 43 additions and 22 deletions

48
main.py
View File

@@ -2606,18 +2606,48 @@ def run_nasm(asm_path: Path, obj_path: Path, debug: bool = False) -> None:
subprocess.run(cmd, check=True) subprocess.run(cmd, check=True)
def run_linker(obj_path: Path, exe_path: Path, debug: bool = False, libs: Optional[List[str]] = None) -> None: def run_linker(obj_path: Path, exe_path: Path, debug: bool = False, libs=None):
cmd = ["ld", "-o", str(exe_path), str(obj_path)] libs = libs or []
if libs:
cmd.extend(["-dynamic-linker", "/lib64/ld-linux-x86-64.so.2"]) lld = shutil.which("ld.lld")
for lib in libs: ld = shutil.which("ld")
# If the user passed a full .so name, use -l:libname.so, else -l<name>
if lib.endswith('.so') or '.so.' in lib: if lld:
cmd.append(f"-l:{lib}") linker = lld
use_lld = True
elif ld:
linker = ld
use_lld = False
else: else:
cmd.append(f"-l{lib}") raise RuntimeError("No linker found")
cmd = [linker]
if use_lld:
cmd.extend(["-m", "elf_x86_64"])
else:
raise RuntimeError(
"GNU ld does not support x86_64 on this platform. Install lld."
)
cmd.extend([
"-o", str(exe_path),
str(obj_path),
])
if not libs:
cmd.extend(["-nostdlib", "-static"])
if libs:
cmd.extend([
"-dynamic-linker", "/lib64/ld-linux-x86-64.so.2",
])
for lib in libs:
cmd.append(f"-l:{lib}" if ".so" in lib else f"-l{lib}")
if debug: if debug:
cmd.append("-g") cmd.append("-g")
subprocess.run(cmd, check=True) subprocess.run(cmd, check=True)

9
p.sl
View File

@@ -1,9 +0,0 @@
import stdlib/stdlib.sl
import stdlib/io.sl
: main
12345 0 p! # store 12345 at offset 0
0 p@ puti cr # read and print value at offset 0
67890 8 p! # store 67890 at offset 8
8 p@ puti cr # read and print value at offset 8
;