[Fix] (CI): separate CPU builds from GPU stages and repair CTest metadata

This commit is contained in:
2026-10-05 10:00:48 -04:00
parent 4eee6ae251
commit 039b342b97
7 changed files with 719 additions and 387 deletions
+63
View File
@@ -0,0 +1,63 @@
#!/usr/bin/env python3
"""Relocate packaged CTest metadata and select the executing runner's drivers."""
import argparse
import os
from pathlib import Path
import re
import subprocess
def normalize(text, old_root, new_root, cmake_root, hardware=False, icd=None):
if old_root:
text = text.replace(old_root, new_root)
# Artifact builds and test hosts can have different CMake installations.
text = re.sub(r'"[^"\n]*/bin/cmake"', '"cmake"', text)
text = re.sub(r'/[^"\[\]\s;()]*/Modules/(GoogleTest(?:AddTests)?\.cmake|GoogleTest/LaunchTest\.cmake)',
lambda m: cmake_root + '/Modules/' + m[1], text)
lines = []
for line in text.splitlines(keepends=True):
if re.search(r'ENVIRONMENT|LABELS', line):
# GoogleTest discovery receives list properties, not one value with
# escaped separators. Otherwise BACKEND becomes "DirectVulkan;...".
line = line.replace('\\;', ';')
if icd:
line = re.sub(r'VK_ICD_FILENAMES=[^;"\]\s]+', 'VK_ICD_FILENAMES=' + icd, line)
if hardware:
for value in ('LIBGL_ALWAYS_SOFTWARE=1', 'MESA_GL_VERSION_OVERRIDE=3.3',
'MESA_GLSL_VERSION_OVERRIDE=330'):
line = line.replace(value + ';', '').replace(';' + value, '').replace(value, '')
lines.append(line)
return ''.join(lines)
def prepare(build, workspace, cmake_root, hardware, icd):
marker = build / '.ci-source-root'
old_root = marker.read_text().strip() if marker.exists() else str(workspace)
changed = 0
for path in list(build.rglob('*.cmake')) + list(build.rglob('runtime-mode.json')):
original = path.read_text()
revised = normalize(original, old_root, str(workspace), cmake_root, hardware, icd)
if original != revised:
path.write_text(revised)
changed += 1
# Drop discovery caches so the repaired properties are used on this host.
for path in build.rglob('*_tests*.json'):
path.unlink()
marker.write_text(str(workspace) + '\n')
print(f'Prepared {build}: {changed} metadata files; hardware={hardware}; ICD={icd}')
def main():
parser = argparse.ArgumentParser()
parser.add_argument('build', nargs='+', type=Path)
args = parser.parse_args()
info = subprocess.check_output(['cmake', '--system-information'], text=True)
cmake_root = re.search(r'^CMAKE_ROOT "([^"]+)"', info, re.M)[1]
workspace = Path(os.environ.get('GITHUB_WORKSPACE', os.getcwd())).resolve()
for build in args.build:
prepare(build, workspace, cmake_root, os.environ.get('MOBILEGL_CI_NATIVE_GPU') == '1',
os.environ.get('VK_ICD_FILENAMES'))
if __name__ == '__main__':
main()
+2 -2
View File
@@ -1,5 +1,5 @@
#!/usr/bin/env python3
"""Choose one execution platform for the entire Test artifact dependency graph."""
"""Choose a GPU runner after the consuming stage has its build artifacts."""
import json
import os
import sys
@@ -48,7 +48,7 @@ def main():
labels, pool, reason = choose(runners)
except (ValueError, KeyError, OSError, urllib.error.URLError) as error:
# An API/auth error is not evidence that the machine is offline. Do not
# silently send the whole matrix to GitHub when the status cannot be read.
# silently send GPU tests to GitHub when the status cannot be read.
print(f"::error::Cannot determine runner availability: {error}", file=sys.stderr)
return 1
with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as output:
+14
View File
@@ -0,0 +1,14 @@
#!/usr/bin/env bash
set -euo pipefail
# CPU runners may be GitHub-hosted or self-hosted; the artifact ABI stays Ubuntu.
test -f /.dockerenv
. /etc/os-release
test "$ID" = ubuntu && test "$VERSION_ID" = 24.04
apt-get update
apt-get install -y curl gnupg tar xz-utils zstd build-essential gh jq unzip zip wget pkg-config cmake
curl --fail --silent --show-error https://apt.llvm.org/llvm-snapshot.gpg.key \
| gpg --dearmor -o /usr/share/keyrings/llvm-archive-keyring.gpg
echo 'deb [signed-by=/usr/share/keyrings/llvm-archive-keyring.gpg] https://apt.llvm.org/noble/ llvm-toolchain-noble-20 main' \
> /etc/apt/sources.list.d/llvm.list
apt-get update
apt-get install -y libc++1-20 libc++abi1-20 libunwind-20
+84
View File
@@ -0,0 +1,84 @@
#!/usr/bin/env python3
import importlib.util
import json
import os
from pathlib import Path
import re
import shutil
import subprocess
import tempfile
import unittest
spec = importlib.util.spec_from_file_location('runtime', Path(__file__).with_name('prepare_test_runtime.py'))
runtime = importlib.util.module_from_spec(spec)
spec.loader.exec_module(runtime)
class MetadataTest(unittest.TestCase):
def test_environment_and_labels_are_lists(self):
text = r'[[ENVIRONMENT]] [[MOBILEGL_BACKEND_TYPE=DirectVulkan\;MOBILEGL_TRANSPORT=inproc]] [[LABELS]] [[gpu\;split]]'
fixed = runtime.normalize(text, '', '/new', '/cmake')
self.assertNotIn(r'\;', fixed)
self.assertIn('DirectVulkan;MOBILEGL_TRANSPORT=inproc', fixed)
def test_other_escaped_values_are_preserved(self):
text = r'COMMAND "argument\;with-semicolon"'
self.assertEqual(runtime.normalize(text, '', '/new', '/cmake'), text)
def test_relocation_and_cmake_modules(self):
text = '"/old/build/test" "/old/tool/bin/cmake" "/old/tool/share/cmake/Modules/GoogleTest.cmake"'
fixed = runtime.normalize(text, '/old', '/new', '/usr/share/cmake')
self.assertIn('"/new/build/test"', fixed)
self.assertIn('"cmake"', fixed)
self.assertIn('/usr/share/cmake/Modules/GoogleTest.cmake', fixed)
def test_hardware_replaces_icd_and_removes_software_overrides(self):
text = 'ENVIRONMENT "VK_ICD_FILENAMES=/lvp.json;LIBGL_ALWAYS_SOFTWARE=1;MESA_GL_VERSION_OVERRIDE=3.3;MESA_GLSL_VERSION_OVERRIDE=330;EGL_PLATFORM=surfaceless"'
fixed = runtime.normalize(text, '', '/new', '/cmake', True, '/dzn.json')
self.assertIn('VK_ICD_FILENAMES=/dzn.json', fixed)
self.assertIn('EGL_PLATFORM=surfaceless', fixed)
self.assertNotIn('SOFTWARE', fixed)
self.assertNotIn('VERSION_OVERRIDE', fixed)
self.assertIn('LIBGL_ALWAYS_SOFTWARE=1', runtime.normalize(text, '', '/new', '/cmake'))
def test_preparation_is_idempotent_and_drops_discovery_cache(self):
with tempfile.TemporaryDirectory() as folder:
root = Path(folder)
(root / '.ci-source-root').write_text('/old\n')
(root / 'tests.cmake').write_text('ENVIRONMENT "BACKEND=x\\;TRANSPORT=y"\n"/old/build"')
(root / 'x_tests.json').write_text('{}')
runtime.prepare(root, Path('/new'), '/cmake', False, None)
first = (root / 'tests.cmake').read_text()
runtime.prepare(root, Path('/new'), '/cmake', False, None)
self.assertEqual(first, (root / 'tests.cmake').read_text())
self.assertFalse((root / 'x_tests.json').exists())
@unittest.skipUnless(os.name == 'posix' and shutil.which('cmake'), 'requires POSIX CMake')
def test_real_ctest_discovery_separates_environment(self):
with tempfile.TemporaryDirectory() as folder:
root = Path(folder)
probe = root / 'probe.py'
probe.write_text('#!/usr/bin/env python3\nimport sys, os\n'
'if "--gtest_list_tests" in sys.argv:\n print("Probe.\\n Environment")\n'
'else:\n assert os.environ.get("MOBILEGL_BACKEND_TYPE") == "DirectVulkan", repr(dict(os.environ))\n assert os.environ.get("MOBILEGL_TRANSPORT") == "inproc"\n')
probe.chmod(0o755)
(root / 'CMakeLists.txt').write_text(
'cmake_minimum_required(VERSION 3.22)\nproject(EnvProbe NONE)\nenable_testing()\ninclude(GoogleTest)\n'
f'add_executable(probe IMPORTED)\nset_target_properties(probe PROPERTIES IMPORTED_LOCATION "{probe}")\n'
'gtest_discover_tests(probe DISCOVERY_MODE PRE_TEST PROPERTIES '
'ENVIRONMENT "MOBILEGL_BACKEND_TYPE=DirectVulkan\\;MOBILEGL_TRANSPORT=inproc" LABELS "gpu\\;split")\n')
build = root / 'build'
subprocess.run(['cmake', '-S', str(root), '-B', str(build)], check=True, capture_output=True)
info = subprocess.check_output(['cmake', '--system-information'], text=True)
cmake_root = re.search(r'^CMAKE_ROOT "([^"]+)"', info, re.M)[1]
runtime.prepare(build, root, cmake_root, False, None)
inventory = json.loads(subprocess.check_output(['ctest', '--test-dir', str(build), '--show-only=json-v1'], text=True))
props = {p['name']: p['value'] for p in inventory['tests'][0]['properties']}
self.assertEqual(props['ENVIRONMENT'], ['MOBILEGL_BACKEND_TYPE=DirectVulkan', 'MOBILEGL_TRANSPORT=inproc'])
self.assertEqual(set(props['LABELS']), {'gpu', 'split'})
result = subprocess.run(['ctest', '--test-dir', str(build), '--output-on-failure'], capture_output=True, text=True)
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
if __name__ == '__main__':
unittest.main()