From d33b0ffc50248ee1c099875830838b41e2d4ad12 Mon Sep 17 00:00:00 2001
From: Massimiliano Culpo <massimiliano.culpo@gmail.com>
Date: Tue, 15 Oct 2019 20:20:49 +0200
Subject: [PATCH] lmod: module files are written in a root folder named by
 target family (#13121)

fixes #13005

This commit fixes an issue with the name of the root directory for
module file hierarchies. Since #3206 the root folder was named after
the microarchitecture used for the spec, which is too specific and
not backward compatible for lmod hierarchies. Here we compute the
root folder name using the target family instead of the target name
itself and we add target information in the 'whatis' portion of the
module file.
---
 lib/spack/spack/modules/lmod.py              |  6 +++++-
 lib/spack/spack/test/modules/lmod.py         | 16 ++++++++++++++++
 share/spack/templates/modules/modulefile.lua |  1 +
 3 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/lib/spack/spack/modules/lmod.py b/lib/spack/spack/modules/lmod.py
index a381e08ca9..e45b97773b 100644
--- a/lib/spack/spack/modules/lmod.py
+++ b/lib/spack/spack/modules/lmod.py
@@ -198,7 +198,11 @@ class LmodFileLayout(BaseFileLayout):
     @property
     def arch_dirname(self):
         """Returns the root folder for THIS architecture"""
-        arch_folder = str(self.spec.architecture)
+        arch_folder = '-'.join([
+            str(self.spec.platform),
+            str(self.spec.os),
+            str(self.spec.target.family)
+        ])
         return os.path.join(
             self.dirname(),  # root for lmod module files
             arch_folder,  # architecture relative path
diff --git a/lib/spack/spack/test/modules/lmod.py b/lib/spack/spack/test/modules/lmod.py
index 7ee173d660..e480d22601 100644
--- a/lib/spack/spack/test/modules/lmod.py
+++ b/lib/spack/spack/test/modules/lmod.py
@@ -262,3 +262,19 @@ def no_op_set(*args, **kwargs):
         # Assert we have core compilers now
         writer, _ = factory(mpileaks_spec_string)
         assert writer.conf.core_compilers
+
+    @pytest.mark.parametrize('spec_str', [
+        'mpileaks target=haswell',
+        'mpileaks target=core2',
+        'mpileaks target=x86_64',
+    ])
+    @pytest.mark.regression('13005')
+    def test_only_generic_microarchitectures_in_root(
+            self, spec_str, factory, module_configuration
+    ):
+        module_configuration('complex_hierarchy')
+        writer, spec = factory(spec_str)
+
+        assert str(spec.target.family) in writer.layout.arch_dirname
+        if spec.target.family != spec.target:
+            assert str(spec.target) not in writer.layout.arch_dirname
diff --git a/share/spack/templates/modules/modulefile.lua b/share/spack/templates/modules/modulefile.lua
index a8eae9bef1..2149c8bfaf 100644
--- a/share/spack/templates/modules/modulefile.lua
+++ b/share/spack/templates/modules/modulefile.lua
@@ -8,6 +8,7 @@
 {% if short_description %}
 whatis([[Name : {{ spec.name }}]])
 whatis([[Version : {{ spec.version }}]])
+whatis([[Target : {{ spec.target }}]])
 whatis([[Short description : {{ short_description }}]])
 {% endif %}
 {% if configure_options %}
-- 
GitLab