xref: /llvm-project/lldb/test/API/macosx/delay-init-dependency/TestDelayInitDependency.py (revision 45927d730bcd2aa3380834ca8db96e32a8b2f2b1)
119342081SJason Molenda"""Test binaries with delay-init dependencies."""
219342081SJason Molenda
319342081SJason Molendaimport subprocess
419342081SJason Molendaimport lldb
519342081SJason Molendafrom lldbsuite.test.decorators import *
619342081SJason Molendafrom lldbsuite.test.lldbtest import *
719342081SJason Molendafrom lldbsuite.test import lldbutil
819342081SJason Molenda
919342081SJason Molenda
1019342081SJason Molendaclass TestDelayInitDependencies(TestBase):
1119342081SJason Molenda    NO_DEBUG_INFO_TESTCASE = True
1219342081SJason Molenda
1319342081SJason Molenda    @skipUnlessDarwin
14*45927d73SJason Molenda    @skipIf(macos_version=["<", "15.0"])
1519342081SJason Molenda    def test_delay_init_dependency(self):
1619342081SJason Molenda        TestBase.setUp(self)
1719342081SJason Molenda        out = subprocess.run(
1819342081SJason Molenda            ["xcrun", "ld", "-delay_library"],
1919342081SJason Molenda            universal_newlines=True,
2019342081SJason Molenda            stdout=subprocess.PIPE,
2119342081SJason Molenda            stderr=subprocess.PIPE,
2219342081SJason Molenda        )
2319342081SJason Molenda        if "delay_library missing" not in out.stderr:
2419342081SJason Molenda            self.skipTest(
2519342081SJason Molenda                "Skipped because the linker doesn't know about -delay_library"
2619342081SJason Molenda            )
2719342081SJason Molenda        self.build()
2819342081SJason Molenda        main_source = "main.c"
2919342081SJason Molenda        exe = self.getBuildArtifact("a.out")
3019342081SJason Molenda        lib = self.getBuildArtifact("libfoo.dylib")
3119342081SJason Molenda
3219342081SJason Molenda        target = self.dbg.CreateTarget(exe)
3319342081SJason Molenda        self.assertTrue(target, VALID_TARGET)
3419342081SJason Molenda
3519342081SJason Molenda        # libfoo.dylib should not be in the target pre-execution
3619342081SJason Molenda        for m in target.modules:
3719342081SJason Molenda            self.assertNotEqual(m.GetFileSpec().GetFilename(), "libfoo.dylib")
3819342081SJason Molenda
3919342081SJason Molenda        # This run without arguments will not load libfoo.dylib
4019342081SJason Molenda        li = lldb.SBLaunchInfo([])
4119342081SJason Molenda        li.SetWorkingDirectory(self.getBuildDir())
4219342081SJason Molenda        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(
4319342081SJason Molenda            self, "// break here", lldb.SBFileSpec("main.c"), li
4419342081SJason Molenda        )
4519342081SJason Molenda        for m in target.modules:
4619342081SJason Molenda            self.assertNotEqual(m.GetFileSpec().GetFilename(), "libfoo.dylib")
4719342081SJason Molenda
4819342081SJason Molenda        process.Kill()
4919342081SJason Molenda        self.dbg.DeleteTarget(target)
5019342081SJason Molenda
5119342081SJason Molenda        # This run with one argument will load libfoo.dylib
5219342081SJason Molenda        li = lldb.SBLaunchInfo([])
5319342081SJason Molenda        li.SetWorkingDirectory(self.getBuildDir())
5419342081SJason Molenda        li.SetArguments(["one-argument"], True)
5519342081SJason Molenda        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(
5619342081SJason Molenda            self, "// break here", lldb.SBFileSpec("main.c"), li
5719342081SJason Molenda        )
5819342081SJason Molenda
5919342081SJason Molenda        found_libfoo = False
6019342081SJason Molenda        for m in target.modules:
6119342081SJason Molenda            if m.GetFileSpec().GetFilename() == "libfoo.dylib":
6219342081SJason Molenda                found_libfoo = True
6319342081SJason Molenda        self.assertTrue(found_libfoo)
64