From 82be8965f2802d3d556f465786e24aaa160f25f0 Mon Sep 17 00:00:00 2001
From: Massimiliano Culpo <massimiliano.culpo@gmail.com>
Date: Fri, 21 Feb 2020 23:50:54 +0100
Subject: [PATCH] Emit a sensible error message if compiler's target is overly
 specific (#14888)

* Emit a sensible error message if compiler's target is overly specific

fixes #14798
fixes #13733

Compiler specifications require a generic architecture family as
their target. This commit improves the error message that is
displayed to users if they edit compilers.yaml and use an overly
specific name.
---
 lib/spack/spack/compilers/__init__.py |  8 ++++++++
 lib/spack/spack/test/compilers.py     | 25 +++++++++++++++++++++++++
 2 files changed, 33 insertions(+)

diff --git a/lib/spack/spack/compilers/__init__.py b/lib/spack/spack/compilers/__init__.py
index a092c930d7..771459f7f3 100644
--- a/lib/spack/spack/compilers/__init__.py
+++ b/lib/spack/spack/compilers/__init__.py
@@ -413,6 +413,14 @@ def get_compilers(config, cspec=None, arch_spec=None):
             assert arch_spec is None
 
         if arch_spec and target and (target != family and target != 'any'):
+            # If the family of the target is the family we are seeking,
+            # there's an error in the underlying configuration
+            if llnl.util.cpu.targets[target].family == family:
+                msg = ('the "target" field in compilers.yaml accepts only '
+                       'target families [replace "{0}" with "{1}"'
+                       ' in "{2}" specification]')
+                msg = msg.format(str(target), family, items.get('spec', '??'))
+                raise ValueError(msg)
             continue
 
         compilers.append(_compiler_from_config_entry(items))
diff --git a/lib/spack/spack/test/compilers.py b/lib/spack/spack/test/compilers.py
index 9395ddba80..51eedd748f 100644
--- a/lib/spack/spack/test/compilers.py
+++ b/lib/spack/spack/test/compilers.py
@@ -485,3 +485,28 @@ def test_fj_version_detection(version_str, expected_version):
 def test_detecting_mixed_toolchains(compiler_spec, expected_result, config):
     compiler = spack.compilers.compilers_for_spec(compiler_spec).pop()
     assert spack.compilers.is_mixed_toolchain(compiler) is expected_result
+
+
+@pytest.mark.regression('14798,13733')
+def test_raising_if_compiler_target_is_over_specific(config):
+    # Compiler entry with an overly specific target
+    compilers = [{'compiler': {
+        'spec': 'gcc@9.0.1',
+        'paths': {
+            'cc': '/usr/bin/gcc-9',
+            'cxx': '/usr/bin/g++-9',
+            'f77': '/usr/bin/gfortran-9',
+            'fc': '/usr/bin/gfortran-9'
+        },
+        'flags': {},
+        'operating_system': 'ubuntu18.04',
+        'target': 'haswell',
+        'modules': [],
+        'environment': {},
+        'extra_rpaths': []
+    }}]
+    arch_spec = spack.spec.ArchSpec(('linux', 'ubuntu18.04', 'haswell'))
+    with spack.config.override('compilers', compilers):
+        cfg = spack.compilers.get_compiler_config()
+        with pytest.raises(ValueError):
+            spack.compilers.get_compilers(cfg, 'gcc@9.0.1', arch_spec)
-- 
GitLab