"""Host test of the 166 recorder. Requires Clang/lld or MSVC (Windows). Example: py -3 tests/ump_logger/run_test.py --compiler "C:/path/to/cl.exe" All build products are written to a temporary directory. """ import argparse import os from pathlib import Path import shutil import subprocess import tempfile parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--compiler', default=shutil.which('clang') or shutil.which('cl') or shutil.which('gcc') or ('C:/msys64/mingw64/bin/gcc.exe' if Path('C:/msys64/mingw64/bin/gcc.exe').is_file() else None)) # Поддерживает имеющийся GCC для проверки нового include-пути без обязательной установки другого host-компилятора. args = parser.parse_args() if not args.compiler: parser.error('Provide --compiler pointing to clang.exe, cl.exe or gcc.exe') # Явно перечисляет поддерживаемые инструменты проверки, не меняя компилятор самой прошивки. root = Path(__file__).resolve().parents[2] with tempfile.TemporaryDirectory(prefix='ump-recorder-') as directory: output = Path(directory) library = output / 'recorder_test.dll' includes = [root / 'tests/ump_logger/stubs', root / 'Source/Internal/Include', root.parent / 'newProject/templates/c/firmware-info/include'] # Адаптер CAN теперь использует общий статический интерфейс паспорта, как production-сборка 166. source = root / 'tests/ump_logger/recorder_test.c' if Path(args.compiler).stem.lower() == 'cl': command = [args.compiler, '/nologo', '/LD', '/GS-', '/wd4068', '/wd4312', *['/I' + str(p) for p in includes], str(source), '/Fo' + str(output / 'recorder.obj'), '/link', '/NOENTRY', '/NODEFAULTLIB', '/OUT:' + str(library)] elif Path(args.compiler).stem.lower() in ('gcc', 'cc'): # MinGW использует обычную DLL с CRT и не принимает специальные параметры компоновщика Clang/lld. command = [args.compiler, '-shared', '-O3', '-Wno-unknown-pragmas', '-Wno-int-to-pointer-cast', *['-I' + str(p) for p in includes], str(source), '-o', str(library)] # Компилирует тот же существующий recorder_test.c и production-логгер без копирования его алгоритма. os.environ['PATH'] = str(Path(args.compiler).resolve().parent) + os.pathsep + os.environ.get('PATH', '') # Даёт дочернему Python доступ к runtime DLL выбранного MinGW только в этом тестовом процессе. else: command = [args.compiler, '-shared', '-nostdlib', '-fuse-ld=lld', '-Wl,/noentry,/nodefaultlib', '-Wno-unknown-pragmas', '-Wno-int-to-pointer-cast', *['-I' + str(p) for p in includes], str(source), '-o', str(library)] subprocess.run(command, cwd=output, check=True) subprocess.run([os.sys.executable, '-c', 'import ctypes,sys; r=ctypes.CDLL(sys.argv[1]).run_tests(); ' 'print("PASS" if r==0 else f"FAIL: C line {r}"); sys.exit(bool(r))', str(library)], check=True)