Skip to content

Conversation

@JDevlieghere
Copy link
Member

Fix cross-compilation of test inferiors on Darwin, targeting remote Linux. This requires specifying the target triple and using LLD for linking.

Fixes #150806

@llvmbot
Copy link
Member

llvmbot commented Jul 30, 2025

@llvm/pr-subscribers-lldb

Author: Jonas Devlieghere (JDevlieghere)

Changes

Fix cross-compilation of test inferiors on Darwin, targeting remote Linux. This requires specifying the target triple and using LLD for linking.

Fixes #150806


Full diff: https://github.com/llvm/llvm-project/pull/151403.diff

5 Files Affected:

  • (modified) lldb/packages/Python/lldbsuite/test/builders/builder.py (+6-1)
  • (modified) lldb/packages/Python/lldbsuite/test/configuration.py (+4)
  • (modified) lldb/packages/Python/lldbsuite/test/dotest.py (+5)
  • (modified) lldb/packages/Python/lldbsuite/test/dotest_args.py (+8)
  • (modified) lldb/packages/Python/lldbsuite/test/make/Makefile.rules (+10)
diff --git a/lldb/packages/Python/lldbsuite/test/builders/builder.py b/lldb/packages/Python/lldbsuite/test/builders/builder.py
index ada6f9ff4a54f..59b4abdcf7a58 100644
--- a/lldb/packages/Python/lldbsuite/test/builders/builder.py
+++ b/lldb/packages/Python/lldbsuite/test/builders/builder.py
@@ -26,7 +26,7 @@ def getCompiler(self):
 
     def getTriple(self, arch):
         """Returns the triple for the given architecture or None."""
-        return None
+        return configuration.triple
 
     def getExtraMakeArgs(self):
         """
@@ -37,6 +37,11 @@ def getExtraMakeArgs(self):
 
     def getArchCFlags(self, architecture):
         """Returns the ARCH_CFLAGS for the make system."""
+        triple = self.getTriple(architecture)
+        print("getArchCFlags ")
+        print(triple)
+        if triple:
+            return ["ARCH_CFLAGS=-target {}".format(triple)]
         return []
 
     def getMake(self, test_subdir, test_name):
diff --git a/lldb/packages/Python/lldbsuite/test/configuration.py b/lldb/packages/Python/lldbsuite/test/configuration.py
index b2d91fd211477..5e3810992d172 100644
--- a/lldb/packages/Python/lldbsuite/test/configuration.py
+++ b/lldb/packages/Python/lldbsuite/test/configuration.py
@@ -45,6 +45,9 @@
 sdkroot = None
 make_path = None
 
+# Allow specifying a triple for cross compilation.
+triple = None
+
 # The overriden dwarf verison.
 # Don't use this to test the current compiler's
 # DWARF version, as this won't be set if the
@@ -141,6 +144,7 @@
 # Typical values include Debug, Release, RelWithDebInfo and MinSizeRel
 cmake_build_type = None
 
+
 def shouldSkipBecauseOfCategories(test_categories):
     if use_categories:
         if (
diff --git a/lldb/packages/Python/lldbsuite/test/dotest.py b/lldb/packages/Python/lldbsuite/test/dotest.py
index 24236e779e51d..31b48dc50bfaa 100644
--- a/lldb/packages/Python/lldbsuite/test/dotest.py
+++ b/lldb/packages/Python/lldbsuite/test/dotest.py
@@ -321,8 +321,13 @@ def parseOptionsAndInitTestdirs():
             logging.error("No SDK found with the name %s; aborting...", args.apple_sdk)
             sys.exit(-1)
 
+    if args.triple:
+        configuration.triple = args.triple
+
     if args.arch:
         configuration.arch = args.arch
+    elif args.triple:
+        configuration.arch = args.triple.split("-")[0]
     else:
         configuration.arch = platform_machine
 
diff --git a/lldb/packages/Python/lldbsuite/test/dotest_args.py b/lldb/packages/Python/lldbsuite/test/dotest_args.py
index e9c21388bc213..fce9e41cb5385 100644
--- a/lldb/packages/Python/lldbsuite/test/dotest_args.py
+++ b/lldb/packages/Python/lldbsuite/test/dotest_args.py
@@ -58,6 +58,14 @@ def create_parser():
             """Specify the path to sysroot. This overrides apple_sdk sysroot."""
         ),
     )
+    group.add_argument(
+        "--triple",
+        metavar="triple",
+        dest="triple",
+        help=textwrap.dedent(
+            """Specify the target triple. Used for cross compilation."""
+        ),
+    )
     if sys.platform == "darwin":
         group.add_argument(
             "--apple-sdk",
diff --git a/lldb/packages/Python/lldbsuite/test/make/Makefile.rules b/lldb/packages/Python/lldbsuite/test/make/Makefile.rules
index 8521ca508a479..4a26bc2da8a47 100644
--- a/lldb/packages/Python/lldbsuite/test/make/Makefile.rules
+++ b/lldb/packages/Python/lldbsuite/test/make/Makefile.rules
@@ -355,6 +355,16 @@ ifeq "$(OS)" "Windows_NT"
 	endif
 endif
 
+#----------------------------------------------------------------------
+# Darwin cross compilation
+#----------------------------------------------------------------------
+ifeq "$(HOST_OS)" "Darwin"
+        ifneq "$(HOST_OS)" "$(OS)"
+                LDFLAGS += -fuse-ld=lld
+	endif
+endif
+
+
 #----------------------------------------------------------------------
 # C++ standard library options
 #----------------------------------------------------------------------

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debugging leftovers?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only works if you have lld built or installed on your machine. I wonder if there's something we can do to warn you about this before all the tests fail to link?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we could try to cross compile a simple binary upfront and complain when it fails, but I'd say it's reasonable to just say that you need lld if you want to cross compile. I believe the same thing is true on Windows.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the failure mode is that you try to run tests (on a mac, targeting a non-mac), we will try to link the test binaries with lld, which will fail immediately. Someone who doesn't know would (I think) say "Oh, I guess I need lld to build binaries for this remote target" when every test fails the same way.

Is this going to work if I run the lldb testsuite on a macOS system but running the tests on iOS or other Darwin type OS. Looking through existing OS comparisons in Makefile.rules, it seems like it can be Darwin/Android/Windows_NT/FreeBSD/NetBSD (plus I'm sure Linux). So probably it calls an iOS device Darwin too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah good point, I was trying to be generic but we shouldn't use lld when targeting a remote Darwin device.

@JDevlieghere JDevlieghere force-pushed the darwin-cross-compilation branch from a242ac3 to 436d886 Compare July 30, 2025 22:55
Copy link
Collaborator

@jasonmolenda jasonmolenda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the failure mode is that you try to run tests (on a mac, targeting a non-mac), we will try to link the test binaries with lld, which will fail immediately. Someone who doesn't know would (I think) say "Oh, I guess I need lld to build binaries for this remote target" when every test fails the same way.

Is this going to work if I run the lldb testsuite on a macOS system but running the tests on iOS or other Darwin type OS. Looking through existing OS comparisons in Makefile.rules, it seems like it can be Darwin/Android/Windows_NT/FreeBSD/NetBSD (plus I'm sure Linux). So probably it calls an iOS device Darwin too.

@JDevlieghere JDevlieghere force-pushed the darwin-cross-compilation branch from 436d886 to d2ddc86 Compare July 31, 2025 03:25
Fix cross-compilation of test inferiors on Darwin, targeting remote
Linux. This requires specifying the target triple and using LLD for
linking.

Fixes llvm#150806
@JDevlieghere JDevlieghere force-pushed the darwin-cross-compilation branch from d2ddc86 to 279bb81 Compare July 31, 2025 03:42
"""Returns the ARCH_CFLAGS for the make system."""
triple = self.getTriple(architecture)
if triple:
return ["ARCH_CFLAGS=-target {}".format(triple)]
Copy link
Member

@dzhidzhoev dzhidzhoev Jul 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work if ARCH_CFLAGS is overridden in the Makefile?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Make, command-line variables have higher precedence than variables set in the Makefile itself, so this does not overwrite the value when it's passed by the builder.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair, thanks.

# Use LLD when cross compiling on Darwin.
#----------------------------------------------------------------------
ifeq "$(HOST_OS)" "Darwin"
ifneq (,$(filter $(OS), FreeBSD Linux NetBSD Windows_NT))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$(OS) is checked against "Android" in other places, I'd add that too.

@JDevlieghere JDevlieghere merged commit c392708 into llvm:main Jul 31, 2025
9 checks passed
@JDevlieghere JDevlieghere deleted the darwin-cross-compilation branch July 31, 2025 20:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support running the test suite from Darwin host on a non-Darwin target

5 participants