xref: /llvm-project/lldb/test/API/macosx/delay-init-dependency/TestDelayInitDependency.py (revision 45927d730bcd2aa3380834ca8db96e32a8b2f2b1)
1"""Test binaries with delay-init dependencies."""
2
3import subprocess
4import lldb
5from lldbsuite.test.decorators import *
6from lldbsuite.test.lldbtest import *
7from lldbsuite.test import lldbutil
8
9
10class TestDelayInitDependencies(TestBase):
11    NO_DEBUG_INFO_TESTCASE = True
12
13    @skipUnlessDarwin
14    @skipIf(macos_version=["<", "15.0"])
15    def test_delay_init_dependency(self):
16        TestBase.setUp(self)
17        out = subprocess.run(
18            ["xcrun", "ld", "-delay_library"],
19            universal_newlines=True,
20            stdout=subprocess.PIPE,
21            stderr=subprocess.PIPE,
22        )
23        if "delay_library missing" not in out.stderr:
24            self.skipTest(
25                "Skipped because the linker doesn't know about -delay_library"
26            )
27        self.build()
28        main_source = "main.c"
29        exe = self.getBuildArtifact("a.out")
30        lib = self.getBuildArtifact("libfoo.dylib")
31
32        target = self.dbg.CreateTarget(exe)
33        self.assertTrue(target, VALID_TARGET)
34
35        # libfoo.dylib should not be in the target pre-execution
36        for m in target.modules:
37            self.assertNotEqual(m.GetFileSpec().GetFilename(), "libfoo.dylib")
38
39        # This run without arguments will not load libfoo.dylib
40        li = lldb.SBLaunchInfo([])
41        li.SetWorkingDirectory(self.getBuildDir())
42        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(
43            self, "// break here", lldb.SBFileSpec("main.c"), li
44        )
45        for m in target.modules:
46            self.assertNotEqual(m.GetFileSpec().GetFilename(), "libfoo.dylib")
47
48        process.Kill()
49        self.dbg.DeleteTarget(target)
50
51        # This run with one argument will load libfoo.dylib
52        li = lldb.SBLaunchInfo([])
53        li.SetWorkingDirectory(self.getBuildDir())
54        li.SetArguments(["one-argument"], True)
55        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(
56            self, "// break here", lldb.SBFileSpec("main.c"), li
57        )
58
59        found_libfoo = False
60        for m in target.modules:
61            if m.GetFileSpec().GetFilename() == "libfoo.dylib":
62                found_libfoo = True
63        self.assertTrue(found_libfoo)
64