winzik/docker/standalone/registry/functions.php

70 lines
3.9 KiB
PHP

<?php
declare(strict_types=1);
// Helpers of JumPHP's StaticPHP registry (loaded by spc.registry.yml's autoload).
/** The extension's config.w32 for Windows: w32/jumphp.js (shared helpers) + w32/<ext>.config.w32. */
function jumphp_config_w32(string $ext, string $source_dir): void
{
$w32 = __DIR__ . '/w32';
file_put_contents("$source_dir/config.w32", file_get_contents("$w32/jumphp.js") . "\n" . file_get_contents("$w32/$ext.config.w32"));
// php-sdl has no source at its root: an empty one for EXTENSION() (w32/sdl.config.w32)
if ($ext === 'sdl' && !is_file("$source_dir/jumphp_w32.c")) {
file_put_contents("$source_dir/jumphp_w32.c", "/* JumPHP: the root file of php-sdl on Windows (its sources are in src/) */\n");
}
}
/**
* PHP-OpenGL links no OpenGL library: its calls go through pointers asked to SDL (SDL_GL_GetProcAddress) on their
* first call, once the game has its OpenGL context; SDL loads the player's OpenGL (libGL.so.1, opengl32.dll).
* Linux: every gl* call (typed by its prototype); Windows: those opengl32.lib lacks (OpenGL 1.2+, with a PFN...PROC
* type in SDL_opengl_glext.h). Renames the calls (glFoo( -> jumphp_glFoo() and writes jumphp_glproc.h, included
* at the end of opengl.h.
*/
function jumphp_gl_loader(string $source_dir, bool $windows, string $glext_header = ''): void
{
$types = [];
if ($windows) {
preg_match_all('/\bPFN(GL\w+)PROC\b/', (string)file_get_contents($glext_header), $m);
$types = array_flip($m[1]);
}
$rename = static fn (string $fn): string => $windows && !isset($types[strtoupper($fn)]) ? $fn : "jumphp_$fn";
$used = [];
foreach (glob("$source_dir/*.[ch]") ?: [] as $file) {
if (basename($file) === 'jumphp_glproc.h') {
continue;
}
$code = (string)file_get_contents($file);
// the calls, and the functions JumPHP's patch hands to its macros: JUMPHP_GEN(glGenFramebuffers, glGenFramebuffers)
$code = preg_replace_callback('/(?<![\w.>])(gl[A-Z]\w*)(\s*\()/', static fn (array $c): string => $rename($c[1]) . $c[2], $code);
$code = preg_replace_callback('/(JUMPHP_[A-Z_]+\(\s*gl\w+\s*,\s*)(gl[A-Z]\w*)/', static fn (array $c): string => $c[1] . $rename($c[2]), $code);
file_put_contents($file, $code);
preg_match_all('/\bjumphp_(gl[A-Z]\w*)/', $code, $m);
$used += array_flip($m[1]);
}
$h = "/* generated by JumPHP's StaticPHP registry (functions.php: jumphp_gl_loader) */\n#pragma once\n#include <SDL2/SDL.h>\n";
// Windows: GL/gl.h is already in (opengl.h), so SDL_opengl.h skips its glext part: the PFN types, asked for here
$h .= $windows ? "#include <SDL2/SDL_opengl.h>\n#include <SDL2/SDL_opengl_glext.h>\n" : '';
foreach (array_keys($used) as $fn) {
$type = $windows ? 'PFN' . strtoupper($fn) . 'PROC' : "__typeof__(&$fn)";
$h .= "static $type jumphp_p_$fn;\n#define jumphp_$fn (jumphp_p_$fn ? jumphp_p_$fn : (jumphp_p_$fn = ($type)SDL_GL_GetProcAddress(\"$fn\")))\n";
}
file_put_contents("$source_dir/jumphp_glproc.h", $h);
$gl = (string)file_get_contents("$source_dir/opengl.h");
if (!str_contains($gl, 'jumphp_glproc.h')) {
file_put_contents("$source_dir/opengl.h", "#ifndef GL_GLEXT_PROTOTYPES\n#define GL_GLEXT_PROTOTYPES\n#endif\n" . $gl . "\n#include \"jumphp_glproc.h\"\n");
}
}
/**
* An extension's configure tests its library by linking it alone (AC_CHECK_LIB): a static library that needs
* others (SDL2, FreeType, the C++ runtime) fails it. StaticPHP links them all at the end: keep only what the test
* adds when it passes.
*/
function jumphp_no_link_test(string $config_m4, string $lib): void
{
$code = (string)file_get_contents($config_m4);
$code = preg_replace('/AC_CHECK_LIB\(\s*' . preg_quote($lib, '/') . '\s*,\s*\w+\s*,\s*\[\s*(PHP_ADD_LIBRARY[^\n]*\))\s*\]\s*,\s*\[\s*AC_MSG_ERROR\([^\n]*\)\s*\]\s*\)/', '$1', $code, 1);
file_put_contents($config_m4, $code);
}