From ecef72c47185eb4ee3e8eed0c5448354c16ac30c Mon Sep 17 00:00:00 2001
From: Greg Becker <becker33@llnl.gov>
Date: Wed, 20 Mar 2024 09:39:26 -0700
Subject: Target.optimization_flags converts non-numeric versions to numeric
 (#43179)

---
 lib/spack/spack/target.py                |  1 +
 lib/spack/spack/version/version_types.py | 28 +++++++++++++++++++++++-----
 2 files changed, 24 insertions(+), 5 deletions(-)

diff --git a/lib/spack/spack/target.py b/lib/spack/spack/target.py
index cd28d8e5fa..5f1f4f9e5a 100644
--- a/lib/spack/spack/target.py
+++ b/lib/spack/spack/target.py
@@ -155,4 +155,5 @@ class Target:
                 # log this and just return compiler.version instead
                 tty.debug(str(e))
 
+        compiler_version = compiler_version.dotted.force_numeric
         return self.microarchitecture.optimization_flags(compiler.name, str(compiler_version))
diff --git a/lib/spack/spack/version/version_types.py b/lib/spack/spack/version/version_types.py
index e34dc85996..3e403256ea 100644
--- a/lib/spack/spack/version/version_types.py
+++ b/lib/spack/spack/version/version_types.py
@@ -193,12 +193,15 @@ class StandardVersion(ConcreteVersion):
         message = "{cls.__name__} indices must be integers"
         raise TypeError(message.format(cls=cls))
 
+    def _stringify(self):
+        string = ""
+        for index in range(len(self.version)):
+            string += str(self.version[index])
+            string += str(self.separators[index])
+        return string
+
     def __str__(self):
-        return (
-            self.string
-            if isinstance(self.string, str)
-            else ".".join((str(c) for c in self.version))
-        )
+        return self.string or self._stringify()
 
     def __repr__(self) -> str:
         # Print indirect repr through Version(...)
@@ -257,6 +260,21 @@ class StandardVersion(ConcreteVersion):
             isinstance(p, VersionStrComponent) and isinstance(p.data, int) for p in self.version
         )
 
+    @property
+    def force_numeric(self):
+        """Replaces all non-numeric components of the version with 0
+
+        This can be used to pass Spack versions to libraries that have stricter version schema.
+        """
+        numeric = tuple(0 if isinstance(v, VersionStrComponent) else v for v in self.version)
+        # null separators except the final one have to be converted to avoid concatenating ints
+        # default to '.' as most common delimiter for versions
+        separators = tuple(
+            "." if s == "" and i != len(self.separators) - 1 else s
+            for i, s in enumerate(self.separators)
+        )
+        return type(self)(None, numeric, separators)
+
     @property
     def dotted(self):
         """The dotted representation of the version.
-- 
cgit v1.2.3-70-g09d2