From 18025b2bd58e052d624af9b66d2b8df77182f68a Mon Sep 17 00:00:00 2001 From: Ryan Fairfax Date: Fri, 27 Sep 2024 09:47:18 -0700 Subject: [PATCH] fix(build): Support glibc qualifiers for Rust targets If you're targetting an older Linux distro the default glibc version being linked against may be too high to produce runnable images. cargo zigbuild supports specifying a specific glibc version to use for a link target by appending "." to the target triple. For example, "x86_64-unknown-linux-gnu.2.17" will target v2.17. For the most part this just works but we need to strip this suffix when looking for output binaries. This fix adds that logic. --- project/RustPlugin.scala | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/project/RustPlugin.scala b/project/RustPlugin.scala index c2f1604844..2ffc5ab631 100644 --- a/project/RustPlugin.scala +++ b/project/RustPlugin.scala @@ -119,16 +119,18 @@ object RustPlugin extends AutoPlugin { // For each architecture find artifacts for (archTarget <- rustArchitectures.value) { + val normalizedArch = normalizeRustTarget(archTarget) + // Special case - host - val archFolder = if (archTarget == "host") { + val archFolder = if (normalizedArch == "host") { targetFolder / releaseDir } else { // General case - targetFolder / archTarget / releaseDir + targetFolder / normalizedArch / releaseDir } // get os arch / kernel, build path - val resourceArchTarget = mapRustTargetToJVMTarget(archTarget) + val resourceArchTarget = mapRustTargetToJVMTarget(normalizedArch) // Find library files in folder // We place every produced library in a resource path like @@ -207,6 +209,23 @@ object RustPlugin extends AutoPlugin { } } + // Normalize a target string by stripping excess info, like GLIBC version + private def normalizeRustTarget(target: String): String = { + // Handle strings like x86_64-unknown-linux-gnu.2.17, + // which contain a GLIBC version suffix + // + // We want to drop the suffix to get x86_64-unknown-linux-gnu + val RustPattern = "([^-]+)-([^-]+)-([^.]+).*".r + + target match { + // Valid inputs + case "host" => target + case RustPattern(arch, vendor, kernel) => s"$arch-$vendor-$kernel" + // Not matched + case x => sys.error(s"Unsupported target $x") + } + } + // Get normalized host kernel name private def getHostKernel: String = { if (SystemUtils.IS_OS_LINUX) {