@@ -57,6 +57,7 @@ public function buildPHP(int $build_target = BUILD_TARGET_NONE): void
5757 $ enableFpm = ($ build_target & BUILD_TARGET_FPM ) === BUILD_TARGET_FPM ;
5858 $ enableMicro = ($ build_target & BUILD_TARGET_MICRO ) === BUILD_TARGET_MICRO ;
5959 $ enableEmbed = ($ build_target & BUILD_TARGET_EMBED ) === BUILD_TARGET_EMBED ;
60+ $ enableCgi = ($ build_target & BUILD_TARGET_CGI ) === BUILD_TARGET_CGI ;
6061
6162 SourcePatcher::patchBeforeBuildconf ($ this );
6263
@@ -109,6 +110,7 @@ public function buildPHP(int $build_target = BUILD_TARGET_NONE): void
109110 ($ enableCli ? '--enable-cli=yes ' : '--enable-cli=no ' ) .
110111 ($ enableMicro ? ('--enable-micro=yes ' . $ micro_logo . $ micro_w32 ) : '--enable-micro=no ' ) .
111112 ($ enableEmbed ? '--enable-embed=yes ' : '--enable-embed=no ' ) .
113+ ($ enableCgi ? '--enable-cgi=yes ' : '--enable-cgi=no ' ) .
112114 $ config_file_scan_dir .
113115 $ opcache_jit_arg .
114116 "{$ this ->makeStaticExtensionArgs ()} " .
@@ -127,6 +129,10 @@ public function buildPHP(int $build_target = BUILD_TARGET_NONE): void
127129 if ($ enableFpm ) {
128130 logger ()->warning ('Windows does not support fpm SAPI, I will skip it. ' );
129131 }
132+ if ($ enableCgi ) {
133+ logger ()->info ('building cgi ' );
134+ $ this ->buildCgi ();
135+ }
130136 if ($ enableMicro ) {
131137 logger ()->info ('building micro ' );
132138 $ this ->buildMicro ();
@@ -159,6 +165,24 @@ public function buildCli(): void
159165 $ this ->deployBinary (BUILD_TARGET_CLI );
160166 }
161167
168+ public function buildCgi (): void
169+ {
170+ SourcePatcher::patchWindowsCGITarget ();
171+
172+ $ extra_libs = getenv ('SPC_EXTRA_LIBS ' ) ?: '' ;
173+
174+ // add nmake wrapper
175+ FileSystem::writeFile (SOURCE_PATH . '\php-src\nmake_cgi_wrapper.bat ' , "nmake /nologo LIBS_CGI= \"ws2_32.lib shell32.lib {$ extra_libs }\" EXTRA_LD_FLAGS_PROGRAM= %* " );
176+
177+ cmd ()->cd (SOURCE_PATH . '\php-src ' )->exec ("{$ this ->sdk_prefix } nmake_cgi_wrapper.bat --task-args php-cgi.exe " );
178+
179+ // deploy cgi binary
180+ logger ()->info ('Deploying cgi file ' );
181+ FileSystem::createDir (BUILD_ROOT_PATH . '\bin ' );
182+
183+ cmd ()->exec ('copy ' . escapeshellarg (SOURCE_PATH . "\\php-src \\x64 \\Release " . ($ this ->zts ? '_TS ' : '' ) . "\\php-cgi.exe " ) . ' ' . escapeshellarg (BUILD_ROOT_PATH . '\bin \\php-cgi.exe ' ));
184+ }
185+
162186 public function buildEmbed (): void
163187 {
164188 // TODO: add embed support for windows
@@ -298,6 +322,17 @@ public function sanityCheck(mixed $build_target): void
298322 }
299323 }
300324 }
325+
326+ // sanity check for php-cgi
327+ if (($ build_target & BUILD_TARGET_CGI ) === BUILD_TARGET_CGI ) {
328+ logger ()->info ('running cgi sanity check ' );
329+ FileSystem::writeFile (SOURCE_PATH . '\\php-cgi-test.php ' , '<?php echo "<h1>Hello, World!</h1>"; ?> ' );
330+ [$ ret , $ output ] = cmd ()->execWithResult (BUILD_BIN_PATH . '\\php-cgi.exe -n -f ' . SOURCE_PATH . '\\php-cgi-test.php ' );
331+ $ raw_output = implode ("\n" , $ output );
332+ if ($ ret !== 0 || !str_contains ($ raw_output , 'Hello, World! ' )) {
333+ throw new ValidationException ("cgi failed sanity check. code: {$ ret }, output: {$ raw_output }" , validation_module: 'php-cgi sanity check ' );
334+ }
335+ }
301336 }
302337
303338 /**
0 commit comments