Source code for batools.prefabflags

# Released under the MIT License. See LICENSE for details.
#
"""Guard: public prefab binaries must carry no private build flags.

Before prefab binaries and plus libraries are published to efrocache
(which public builds download), we scan each for the build-flags marker
the plus layer compiles in and refuse to publish any built with private
capabilities: a developer build, or an infrastructure build (our own
server-side builds). Either would hand public users capabilities only
our own builds should have.
"""

from __future__ import annotations  # Docs-generation hack.

import os
import subprocess

from efro.error import CleanError

# Build-flags marker the plus layer compiles in (python_methods_plus.cc);
# prefab binaries and libs carry it via plus.
_MARKER = b'BA_PLUS_BUILD_FLAGS:'

# The flags a public prefab must have.
_PUBLIC_FLAGS = b'dev=0,infra=0'

# Prefab files that contain plus. Linked binaries and static libs must
# show the marker; MSVC .lib files are built with whole-program
# optimization (objects hold intermediate code), so the marker may not
# appear there as plain bytes -- checked when present, not required.
_REQUIRED = {
    'ballisticakit',
    'ballisticakit_headless',
    'BallisticaKit.exe',
    'BallisticaKitHeadless.exe',
    'libballisticaplus.a',
}
_OPTIONAL = {
    'BallisticaKitGenericPlus.lib',
    'BallisticaKitHeadlessPlus.lib',
}


[docs] def check_prefab_build_flags() -> None: """Build the efrocache targets and verify every prefab's flags. Raises :class:`~efro.error.CleanError` listing each offending file. """ subprocess.run( ['make', f'-j{os.cpu_count() or 4}', 'efrocache-build'], check=True ) paths = ( subprocess.run( ['make', 'efrocache-list'], check=True, capture_output=True ) .stdout.decode() .split() ) problems: list[str] = [] checked = 0 for path in paths: name = os.path.basename(path) required = name in _REQUIRED if not required and name not in _OPTIONAL: continue checked += 1 problem = _check_file(path, required=required) if problem is not None: problems.append(problem) if problems: raise CleanError( 'Refusing to publish prefabs:\n ' + '\n '.join(problems) ) print(f'Prefab build-flags check passed ({checked} file(s)).')
def _check_file(path: str, *, required: bool) -> str | None: """Return a problem description for one file, or None if it's fine.""" with open(path, 'rb') as infile: data = infile.read() flagsets: set[bytes] = set() start = 0 while (idx := data.find(_MARKER, start)) != -1: start = idx + len(_MARKER) flagsets.add(data[start : start + len(_PUBLIC_FLAGS)]) if not flagsets: return ( f'{path}: no plus build-flags marker found.' if required else None ) bad = sorted(f.decode(errors='replace') for f in flagsets - {_PUBLIC_FLAGS}) if bad: return ( f'{path}: plus build flags {bad};' f' public prefabs must be {_PUBLIC_FLAGS.decode()}.' ) return None # Docs-generation hack; import some stuff that we likely only forward-declared # in our actual source code so that docs tools can find it. from typing import (Coroutine, Any, Literal, Callable, Generator, Awaitable, Sequence, Self, TypeIs) import asyncio from concurrent.futures import Future from pathlib import Path from enum import Enum