forked from ircmaxell/php-compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompile.php
More file actions
executable file
·278 lines (270 loc) · 12.6 KB
/
compile.php
File metadata and controls
executable file
·278 lines (270 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?php
declare(strict_types=1);
/**
* This file is part of PHP-Compiler, a PHP CFG Compiler for PHP code
*
* @copyright 2015 Anthony Ferrara. All rights reserved
* @license MIT See LICENSE at the root of the project for more info
*/
use PHPCompiler\Runtime;
use PHPCompiler\Web\DeployRoot;
use PHPCompiler\Web\LiteralIncludeDiscovery;
use PHPCompiler\Web\SourceBundler;
use PHPCompiler\Web\Superglobals;
/**
* True when bin/compile.php is building a user/test fixture binary (not bootstrap/self-host spine).
*/
function phpc_compile_is_user_script_aot(string $normalized): bool
{
if ('' === $normalized || '-' === $normalized || 'Command line code' === $normalized) {
return true;
}
if (str_contains($normalized, 'bootstrap-aot/')) {
return false;
}
if (str_contains($normalized, 'selfhost/')) {
return false;
}
if (str_contains($normalized, 'compile_driver.php')) {
return false;
}
if (str_contains($normalized, 'compiler_unit_probe')) {
return false;
}
return true;
}
function run(string $filename, string $code, array $options): void
{
$normalized = '-' !== $filename ? str_replace('\\', '/', $filename) : '';
if (\class_exists(\PHPCompiler\JIT\Progress::class, false)) {
\PHPCompiler\JIT\Progress::notePhase('bin_compile_run_begin');
\PHPCompiler\JIT\Progress::noteEntry($filename);
}
// When executing as a compiled native driver, default to self-host mode. Relying on getenv()
// alone is fragile in early bootstrap contexts, and the native driver is only used for the
// self-host ladder.
if (\function_exists('php_compiler_cli_should_skip_entry_driver')) {
$selfhostAot = getenv('PHP_COMPILER_SELFHOST_AOT');
if (false === $selfhostAot || '' === $selfhostAot) {
putenv('PHP_COMPILER_SELFHOST_AOT=1');
}
// Compiled argv drivers must enable the M3 compile-driver lowering allowlist; otherwise
// key Runtime entrypoints can be stubbed and compilation returns null (#3004).
$m3CompileDriver = getenv('PHP_COMPILER_M3_COMPILE_DRIVER');
if (false === $m3CompileDriver || '' === $m3CompileDriver) {
putenv('PHP_COMPILER_M3_COMPILE_DRIVER=1');
}
$m3CompileDriverMain = getenv('PHP_COMPILER_M3_COMPILE_DRIVER_MAIN');
if (false === $m3CompileDriverMain || '' === $m3CompileDriverMain) {
putenv('PHP_COMPILER_M3_COMPILE_DRIVER_MAIN=1');
}
$m4BinCompile = getenv('PHP_COMPILER_M4_BIN_COMPILE_DRIVER');
if (false === $m4BinCompile || '' === $m4BinCompile) {
putenv('PHP_COMPILER_M4_BIN_COMPILE_DRIVER=1');
}
// Gen-2+ argv driver recompiling bin/compile.php must register bootstrap-aot sidecars (#3004).
$m5DriverHost = getenv('PHP_COMPILER_M5_DRIVER_HOST');
if (false === $m5DriverHost || '' === $m5DriverHost) {
putenv('PHP_COMPILER_M5_DRIVER_HOST=1');
}
}
if ('' !== $normalized && str_contains($normalized, 'bootstrap-aot/')) {
// Bootstrap AOT fixtures require real JIT lowering; ignore inherited self-host stub env (#1086).
//
// But: if we're already running under a compiled self-host driver, forcing stub-off
// can crash before we have a chance to fall back (e.g. inventory argv driver compiling
// bootstrap-aot fixtures as part of self-host compile-smoke, #2967).
//
// Use a runtime-native marker instead of getenv(): self-host AOT execution stubs and
// env access can be unreliable precisely in the scenarios we're trying to debug.
if (!\function_exists('php_compiler_cli_should_skip_entry_driver')) {
putenv('PHP_COMPILER_SELFHOST_AOT=0');
}
}
if ('-' !== $filename && str_contains($normalized, 'test/selfhost/')) {
$selfhostAot = getenv('PHP_COMPILER_SELFHOST_AOT');
if (false === $selfhostAot || '' === $selfhostAot) {
putenv('PHP_COMPILER_SELFHOST_AOT=1');
}
}
if ('' !== $normalized && str_contains($normalized, 'selfhost/') && str_contains($normalized, 'compile_driver.php') && !isset($options['-l'])) {
putenv('PHP_COMPILER_SELFHOST_AOT=1');
putenv('PHP_COMPILER_M3_COMPILE_DRIVER=1');
putenv('PHP_COMPILER_M3_COMPILE_DRIVER_MAIN=1');
if (str_contains($normalized, 'compiler_helloworld_smoke/compile_driver.php')
|| str_contains($normalized, 'bootstrap_loop_smoke/compile_driver.php')) {
putenv('PHP_COMPILER_M3_EMIT_LOG_PREFIX=helloworld_compile_smoke');
} elseif (str_contains($normalized, 'runtime_compile_smoke/compile_driver.php')) {
putenv('PHP_COMPILER_M3_EMIT_LOG_PREFIX=runtime_compile_smoke_m3_emit');
putenv('PHP_COMPILER_M3_EMIT_HELPER_SPINE=1');
} elseif (str_contains($normalized, 'jit_unit_probe/compile_driver.php')) {
putenv('PHP_COMPILER_M3_EMIT_LOG_PREFIX=jit_unit_probe_m3_emit');
} elseif (str_contains($normalized, 'compile_driver.php')) {
putenv('PHP_COMPILER_M3_EMIT_LOG_PREFIX=compile_smoke_m3_emit');
}
$inventoryEmit = getenv('BOOTSTRAP_M3_USE_INVENTORY_EMIT_DRIVER') ?: getenv('PHP_COMPILER_M3_INVENTORY_EMIT_DRIVER');
if ('1' === $inventoryEmit || 'true' === strtolower((string) $inventoryEmit)) {
putenv('PHP_COMPILER_M3_INVENTORY_EMIT_DRIVER=1');
// Runtime: do not force emit-helper TU/minimal mode here.
// The inventory driver is expected to compile arbitrary sources; forcing emit-helper
// mode can route Runtime::parseAndCompile through minimal sentinel stubs and recurse (#2967).
}
}
if ('' !== $normalized && str_contains($normalized, 'jit_result_stub.php')) {
putenv('PHP_COMPILER_SELFHOST_AOT=1');
}
if ('' !== $normalized && str_contains($normalized, 'bootstrap-vendor-prelink/generated/')) {
$vendorPrelink = getenv('PHP_COMPILER_VENDOR_PRELINK');
if ('1' === $vendorPrelink || 'true' === strtolower((string) $vendorPrelink)) {
// Real-lower parse/compile for vendor bundles; avoid M3 emit-TU sidecar host-compile (#3028, #3036).
putenv('PHP_COMPILER_M3_EMIT_HELPER_SPINE=1');
putenv('PHP_COMPILER_KEEP_OBJECT_FILE=1');
}
}
$includes = $options['--include'] ?? [];
if (!is_array($includes)) {
$includes = [] === $includes || '' === $includes ? [] : [$includes];
}
/** @var list<string> $includes */
if ([] === $includes && '-' !== $filename && is_file($filename)) {
$runtime = new Runtime(Runtime::MODE_AOT);
$includes = LiteralIncludeDiscovery::discoverDirectAbsolutePaths($runtime, $filename);
}
if ([] !== $includes) {
$projectRoot = DeployRoot::findProjectRootForPath($filename);
[$code, $filename] = SourceBundler::bundleForAot($filename, $includes, $projectRoot);
}
$runtime = new Runtime(Runtime::MODE_AOT);
$queryString = $options['-q'] ?? null;
if (!is_string($queryString) || '' === $queryString) {
$fromEnv = getenv('QUERY_STRING');
if (is_string($fromEnv) && '' !== $fromEnv) {
$queryString = $fromEnv;
}
}
$postBody = $options['-p'] ?? null;
if (!is_string($postBody) || '' === $postBody) {
$bodyEnv = getenv('REQUEST_BODY');
if (is_string($bodyEnv) && '' !== $bodyEnv) {
$postBody = $bodyEnv;
}
}
$scriptFilename = null;
if ('-' !== $filename && 'Command line code' !== $filename) {
$resolved = realpath($filename);
if (false !== $resolved) {
$scriptFilename = $resolved;
}
}
Superglobals::populateFromEnvironment(
$runtime->vmContext,
is_string($queryString) ? $queryString : null,
is_string($postBody) ? $postBody : null,
$scriptFilename
);
$prevUserScriptAot = getenv('PHP_COMPILER_AOT_USER_SCRIPT');
$setUserScriptAot = phpc_compile_is_user_script_aot($normalized);
if ($setUserScriptAot && \function_exists('putenv')) {
putenv('PHP_COMPILER_AOT_USER_SCRIPT=1');
$_ENV['PHP_COMPILER_AOT_USER_SCRIPT'] = '1';
$_SERVER['PHP_COMPILER_AOT_USER_SCRIPT'] = '1';
}
try {
$block = $runtime->parseAndCompile($code, $filename);
} finally {
if ($setUserScriptAot && \function_exists('putenv')) {
if (false === $prevUserScriptAot || null === $prevUserScriptAot) {
putenv('PHP_COMPILER_AOT_USER_SCRIPT=');
unset($_ENV['PHP_COMPILER_AOT_USER_SCRIPT'], $_SERVER['PHP_COMPILER_AOT_USER_SCRIPT']);
} else {
putenv('PHP_COMPILER_AOT_USER_SCRIPT='.$prevUserScriptAot);
$_ENV['PHP_COMPILER_AOT_USER_SCRIPT'] = $prevUserScriptAot;
$_SERVER['PHP_COMPILER_AOT_USER_SCRIPT'] = $prevUserScriptAot;
}
}
}
if (null === $block) {
if (! isset($options['-l'])) {
$diag = \PHPCompiler\Runtime::getLastParseFailure();
if (null === $diag || '' === $diag) {
$diag = $runtime->formatParseAndCompileNullDetail(null);
}
$suffix = null !== $diag && '' !== $diag ? ' — '.$diag : '';
fwrite(STDERR, 'compile.php: parseAndCompile returned null for '.$filename.$suffix."\n");
exit(2);
}
fwrite(STDERR, "phpc lint: failed to compile {$filename}\n");
exit(1);
}
if (! isset($options['-l'])) {
if (! isset($options['-o']) || $options['-o'] === true) {
$options['-o'] = str_replace('.php', '', $filename);
}
if (isset($options['-y'])) {
$debugFile = true === $options['-y'] ? $options['-o'] : $options['-y'];
$runtime->setDebug($debugFile);
}
if (isset($options['--debug-symbols'])) {
$runtime->setAotDebugSymbols(true);
}
// Literal path for self-host AOT/JIT include folding (#54, #1492).
require_once 'lib/AOT/LinkerProcessPolyfill.php';
if (!\function_exists('phpc_run_command')) {
/**
* @param array<string, string>|null $env
*
* @return array{code:int,stdout:string,stderr:string}|null
*/
function phpc_run_command(string $command, ?array $env = null): ?array
{
return \PHPCompiler\AOT\LinkerProcessPolyfill::run($command, $env);
}
}
$prevSelfHostAot = getenv('PHP_COMPILER_SELFHOST_AOT');
$setSelfHostAotForCompile = \function_exists('putenv') && (false === $prevSelfHostAot || '' === (string) $prevSelfHostAot);
if ($setSelfHostAotForCompile) {
// Keep LLVM 9 stable during AOT compilation; some lowering paths are still sensitive (#2600).
putenv('PHP_COMPILER_SELFHOST_AOT=1');
$_ENV['PHP_COMPILER_SELFHOST_AOT'] = '1';
$_SERVER['PHP_COMPILER_SELFHOST_AOT'] = '1';
}
if ($setUserScriptAot && \function_exists('putenv')) {
putenv('PHP_COMPILER_AOT_USER_SCRIPT=1');
$_ENV['PHP_COMPILER_AOT_USER_SCRIPT'] = '1';
$_SERVER['PHP_COMPILER_AOT_USER_SCRIPT'] = '1';
}
try {
$runtime->standalone($block, $options['-o'], $code, $filename);
} catch (\LogicException $e) {
fwrite(STDERR, $e->getMessage()."\n");
exit(2);
} finally {
if ($setUserScriptAot && \function_exists('putenv')) {
if (false === $prevUserScriptAot || null === $prevUserScriptAot) {
putenv('PHP_COMPILER_AOT_USER_SCRIPT=');
unset($_ENV['PHP_COMPILER_AOT_USER_SCRIPT'], $_SERVER['PHP_COMPILER_AOT_USER_SCRIPT']);
} else {
putenv('PHP_COMPILER_AOT_USER_SCRIPT='.$prevUserScriptAot);
$_ENV['PHP_COMPILER_AOT_USER_SCRIPT'] = $prevUserScriptAot;
$_SERVER['PHP_COMPILER_AOT_USER_SCRIPT'] = $prevUserScriptAot;
}
}
if ($setSelfHostAotForCompile) {
putenv('PHP_COMPILER_SELFHOST_AOT=');
unset($_ENV['PHP_COMPILER_SELFHOST_AOT'], $_SERVER['PHP_COMPILER_SELFHOST_AOT']);
}
}
}
}
if (
!(defined('PHP_COMPILER_LIB_SPINE_SMOKE') && PHP_COMPILER_LIB_SPINE_SMOKE)
&& !(\function_exists('php_compiler_cli_should_skip_entry_driver') && php_compiler_cli_should_skip_entry_driver())
) {
// Use literal require paths so self-host AOT/JIT can fold includes (#54, #1492).
require_once __DIR__.'/../src/cli.php';
php_compiler_cli_note_invocation_cwd();
chdir(__DIR__.'/..');
require_once 'src/cli.php';
require_once 'src/cli_driver.php';
php_compiler_cli_dispatch();
}