From 12fe95b1943eb832a54ba09274fa02c60d04f6b0 Mon Sep 17 00:00:00 2001
From: Ross Burton <ross@burtonini.com>
Date: Wed, 20 Jun 2018 13:45:44 +0100
Subject: [PATCH 1/3] Validate cpu_family (#3753)

* environment: validate cpu_family in cross file

* run_unittests: add unittest to ensure CPU family list in docs and environment matches

* run_unittests: skip compiler options test if not in a git repository

* environment: validate the detected cpu_family

* docs: add 32-bit PowerPC and 32/64-bit MIPS to CPU Families table

Names gathered by booting Linux in Qemu and running:

$ python3
import platform; platform.machine()

Partial fix for #3751

Upstream-Status: Backport
Signed-off-by: Ross Burton <ross.burton@intel.com>
---
 mesonbuild/environment.py | 24 ++++++++++++++++++++++++
 run_unittests.py          | 18 ++++++++++++++++++
 2 files changed, 42 insertions(+)

diff --git a/mesonbuild/environment.py b/mesonbuild/environment.py
index d02a837..678d009 100644
--- a/mesonbuild/environment.py
+++ b/mesonbuild/environment.py
@@ -72,6 +72,22 @@ from .compilers import (
 
 build_filename = 'meson.build'
 
+known_cpu_families = (
+    'aarch64',
+    'arm',
+    'e2k',
+    'ia64',
+    'mips',
+    'mips64',
+    'parisc',
+    'ppc',
+    'ppc64',
+    'ppc64le',
+    'sparc64',
+    'x86',
+    'x86_64'
+)
+
 # Environment variables that each lang uses.
 cflags_mapping = {'c': 'CFLAGS',
                   'cpp': 'CXXFLAGS',
@@ -210,6 +226,10 @@ def detect_cpu_family(compilers):
                 pass
         return 'x86_64'
     # Add fixes here as bugs are reported.
+
+    if trial not in known_cpu_families:
+        mlog.warning('Unknown CPU family %s, please report this at https://github.com/mesonbuild/meson/issues/new' % trial)
+
     return trial
 
 def detect_cpu(compilers):
@@ -1021,6 +1041,10 @@ class CrossBuildInfo:
                     res = eval(value, {'__builtins__': None}, {'true': True, 'false': False})
                 except Exception:
                     raise EnvironmentException('Malformed value in cross file variable %s.' % entry)
+
+                if entry == 'cpu_family' and res not in known_cpu_families:
+                    mlog.warning('Unknown CPU family %s, please report this at https://github.com/mesonbuild/meson/issues/new' % value)
+
                 if self.ok_type(res):
                     self.config[s][entry] = res
                 elif isinstance(res, list):
diff --git a/run_unittests.py b/run_unittests.py
index 3c215db..7185008 100755
--- a/run_unittests.py
+++ b/run_unittests.py
@@ -2065,6 +2065,24 @@ recommended as it can lead to undefined behaviour on some platforms''')
         self.wipe()
 
 
+    @unittest.skipIf(not os.path.isdir('docs'), 'Doc dir not found, presumably because this is a tarball release.')
+    def test_cpu_families_documented(self):
+        with open("docs/markdown/Reference-tables.md") as f:
+            md = f.read()
+        self.assertIsNotNone(md)
+
+        sections = list(re.finditer(r"^## (.+)$", md, re.MULTILINE))
+        for s1, s2 in zip(sections[::2], sections[1::2]):
+            if s1.group(1) == "CPU families":
+                # Extract the content for this section
+                content = md[s1.end():s2.start()]
+                # Find the list entries
+                arches = [m.group(1) for m in re.finditer(r"^\| (\w+) +\|", content, re.MULTILINE)]
+                # Drop the header
+                arches = set(arches[1:])
+                self.assertEqual(arches, set(mesonbuild.environment.known_cpu_families))
+
+
 class FailureTests(BasePlatformTests):
     '''
     Tests that test failure conditions. Build files here should be dynamically
-- 
2.12.0

