Files
unkernel/gl/wasm/gen-gl-missing.py

56 lines
2.1 KiB
Python

#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>
# gen-gl-missing.py LIST HEADER: print C stubs for the GL entry points named
# in LIST (one per line, '#' comments) with the prototypes HEADER declares
# (epoxy/gl.h run through the preprocessor). Each stub says once on stderr
# that it was called and returns 0: these are the functions virglrenderer
# links against that emscripten's WebGL2 bindings do not provide (desktop GL,
# GLES 3.1+, extensions WebGL lacks); most sit behind feature checks that
# fail on WebGL2, and a call that gets through shows up in the log.
import re
import sys
names = []
for line in open(sys.argv[1]):
line = line.split('#', 1)[0].strip()
if line:
names.append(line)
text = open(sys.argv[2]).read()
protos = {}
for stmt in text.split(';'):
stmt = ' '.join(stmt.split())
m = re.match(r'^(.*?)\b(gl\w+)\s*\((.*)\)$', stmt)
if not m or '{' in stmt or 'typedef' in m.group(1):
continue
ret = re.sub(r'__attribute__\s*\(\((?:[^()]|\([^()]*\))*\)\)', '', m.group(1))
ret = ' '.join(w for w in ret.split() if w not in ('extern', 'GLAPI', 'APIENTRY', 'GLAPIENTRY'))
protos.setdefault(m.group(2), (ret, m.group(3)))
missing = [n for n in names if n not in protos]
if missing:
sys.exit('gen-gl-missing.py: no prototype for ' + ', '.join(missing))
print('// Generated by gl/wasm/gen-gl-missing.py from gl/wasm/gl-missing.txt; do not edit.')
print('#include <epoxy/gl.h>')
print('#include <stdio.h>')
print()
print("static void __attribute__((unused)) kw_gl_missing(const char *name, int *said)")
print('{')
print('\tif (!*said) {')
print('\t\t*said = 1;')
print('\t\tfprintf(stderr, "kw_gl_missing: %s called: not in WebGL2, ignored\\n", name);')
print('\t}')
print('}')
for n in names:
ret, args = protos[n]
print()
print('%s %s(%s)' % (ret, n, args if args.strip() else 'void'))
print('{')
print('\tstatic int said;')
print('\tkw_gl_missing("%s", &said);' % n)
if ret != 'void':
print('\treturn (%s)0;' % ret)
print('}')