Files
neoLegacy-mac/tests/test_orbis_link_integration.py

61 lines
2.7 KiB
Python

import os
import subprocess
import tempfile
import unittest
from pathlib import Path
from patch_orbis_relocations import patch_directory, patch_elf
class OrbisLinkIntegrationTests(unittest.TestCase):
def test_real_archive_relocations_link_with_llvm(self) -> None:
source_root = Path(os.environ.get("IGGY_SOURCE_DIR", Path.home() / "neoLegacy"))
archive = source_root / "Minecraft.Client/Orbis/Iggy/lib/libiggy_orbis.a"
llvm_root = Path(os.environ.get(
"LLVM_DIR", Path.home() / ".local/llvm22/LLVM-22.1.8-macOS-ARM64"
))
llvm_ar = llvm_root / "bin/llvm-ar"
ld_lld = llvm_root / "bin/ld.lld"
if not archive.is_file():
self.skipTest(f"missing owned Iggy archive: {archive}")
self.assertTrue(llvm_ar.is_file(), f"missing LLVM archiver: {llvm_ar}")
self.assertTrue(ld_lld.is_file(), f"missing LLVM linker: {ld_lld}")
with tempfile.TemporaryDirectory() as temporary_directory:
temporary = Path(temporary_directory)
original_output = temporary / "original.so"
original_link = subprocess.run(
[str(ld_lld), "-shared", "--whole-archive", str(archive),
"--no-whole-archive", "--unresolved-symbols=ignore-all",
"-o", str(original_output)],
capture_output=True, text=True, check=False,
)
self.assertNotEqual(original_link.returncode, 0)
self.assertIn("unknown relocation (40)", original_link.stderr)
extracted = temporary / "extracted"
patched = temporary / "patched"
extracted.mkdir()
subprocess.run([str(llvm_ar), "x", str(archive)], cwd=extracted, check=True)
file_count, relocation_count = patch_directory(extracted, patched)
self.assertEqual(file_count, 76)
self.assertEqual(relocation_count, 2463)
self.assertTrue(all(patch_elf(path.read_bytes())[1] == 0
for path in patched.iterdir()))
linked_output = temporary / "patched.so"
patched_objects = tuple(str(path) for path in sorted(patched.iterdir()))
patched_link = subprocess.run(
[str(ld_lld), "-shared", "--whole-archive", *patched_objects,
"--no-whole-archive", "--unresolved-symbols=ignore-all",
"-o", str(linked_output)],
capture_output=True, text=True, check=False,
)
self.assertEqual(patched_link.returncode, 0, patched_link.stderr)
self.assertTrue(linked_output.is_file())
self.assertGreater(linked_output.stat().st_size, 0)
if __name__ == "__main__":
unittest.main()