@@ -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
@@ -102,13 +103,13 @@ public function buildPHP(int $build_target = BUILD_TARGET_NONE): void
102103 ->exec (
103104 "{$ this ->sdk_prefix } configure.bat --task-args \"" .
104105 '--disable-all ' .
105- '--disable-cgi ' .
106106 '--with-php-build= ' . BUILD_ROOT_PATH . ' ' .
107107 '--with-extra-includes= ' . BUILD_INCLUDE_PATH . ' ' .
108108 '--with-extra-libs= ' . BUILD_LIB_PATH . ' ' .
109- ($ enableCli ? '--enable-cli=yes ' : '--enable-cli=no ' ) .
110- ($ enableMicro ? ('--enable-micro=yes ' . $ micro_logo . $ micro_w32 ) : '--enable-micro=no ' ) .
111- ($ enableEmbed ? '--enable-embed=yes ' : '--enable-embed=no ' ) .
109+ ($ enableCli ? '--enable-cli ' : '--disable-cli ' ) .
110+ ($ enableMicro ? ('--enable-micro ' . $ micro_logo . $ micro_w32 ) : '--disable-micro ' ) .
111+ ($ enableEmbed ? '--enable-embed ' : '--disable-embed ' ) .
112+ ($ enableCgi ? '--enable-cgi ' : '--disable-cgi ' ) .
112113 $ config_file_scan_dir .
113114 $ opcache_jit_arg .
114115 "{$ this ->makeStaticExtensionArgs ()} " .
@@ -127,6 +128,10 @@ public function buildPHP(int $build_target = BUILD_TARGET_NONE): void
127128 if ($ enableFpm ) {
128129 logger ()->warning ('Windows does not support fpm SAPI, I will skip it. ' );
129130 }
131+ if ($ enableCgi ) {
132+ logger ()->info ('building cgi ' );
133+ $ this ->buildCgi ();
134+ }
130135 if ($ enableMicro ) {
131136 logger ()->info ('building micro ' );
132137 $ this ->buildMicro ();
@@ -159,6 +164,20 @@ public function buildCli(): void
159164 $ this ->deployBinary (BUILD_TARGET_CLI );
160165 }
161166
167+ public function buildCgi (): void
168+ {
169+ SourcePatcher::patchWindowsCGITarget ();
170+
171+ $ extra_libs = getenv ('SPC_EXTRA_LIBS ' ) ?: '' ;
172+
173+ // add nmake wrapper
174+ FileSystem::writeFile (SOURCE_PATH . '\php-src\nmake_cgi_wrapper.bat ' , "nmake /nologo LIBS_CGI= \"ws2_32.lib kernel32.lib advapi32.lib {$ extra_libs }\" EXTRA_LD_FLAGS_PROGRAM= %* " );
175+
176+ cmd ()->cd (SOURCE_PATH . '\php-src ' )->exec ("{$ this ->sdk_prefix } nmake_cgi_wrapper.bat --task-args php-cgi.exe " );
177+
178+ $ this ->deployBinary (BUILD_TARGET_CGI );
179+ }
180+
162181 public function buildEmbed (): void
163182 {
164183 // TODO: add embed support for windows
@@ -265,7 +284,7 @@ public function sanityCheck(mixed $build_target): void
265284 // sanity check for php-cli
266285 if (($ build_target & BUILD_TARGET_CLI ) === BUILD_TARGET_CLI ) {
267286 logger ()->info ('running cli sanity check ' );
268- [$ ret , $ output ] = cmd ()->execWithResult (BUILD_ROOT_PATH . '\bin \php.exe -n -r "echo \"hello\";" ' );
287+ [$ ret , $ output ] = cmd ()->execWithResult (BUILD_BIN_PATH . '\php.exe -n -r "echo \"hello\";" ' );
269288 if ($ ret !== 0 || trim (implode ('' , $ output )) !== 'hello ' ) {
270289 throw new ValidationException ('cli failed sanity check ' , validation_module: 'php-cli function check ' );
271290 }
@@ -284,7 +303,7 @@ public function sanityCheck(mixed $build_target): void
284303 if (file_exists ($ test_file )) {
285304 @unlink ($ test_file );
286305 }
287- file_put_contents ($ test_file , file_get_contents (BUILD_ROOT_PATH . '\bin \micro.sfx ' ) . $ task ['content ' ]);
306+ file_put_contents ($ test_file , file_get_contents (BUILD_BIN_PATH . '\micro.sfx ' ) . $ task ['content ' ]);
288307 chmod ($ test_file , 0755 );
289308 [$ ret , $ out ] = cmd ()->execWithResult ($ test_file );
290309 foreach ($ task ['conditions ' ] as $ condition => $ closure ) {
@@ -298,6 +317,17 @@ public function sanityCheck(mixed $build_target): void
298317 }
299318 }
300319 }
320+
321+ // sanity check for php-cgi
322+ if (($ build_target & BUILD_TARGET_CGI ) === BUILD_TARGET_CGI ) {
323+ logger ()->info ('running cgi sanity check ' );
324+ FileSystem::writeFile (SOURCE_PATH . '\php-cgi-test.php ' , '<?php echo "<h1>Hello, World!</h1>"; ?> ' );
325+ [$ ret , $ output ] = cmd ()->execWithResult (BUILD_BIN_PATH . '\php-cgi.exe -n -f ' . SOURCE_PATH . '\php-cgi-test.php ' );
326+ $ raw_output = implode ("\n" , $ output );
327+ if ($ ret !== 0 || !str_contains ($ raw_output , 'Hello, World! ' )) {
328+ throw new ValidationException ("cgi failed sanity check. code: {$ ret }, output: {$ raw_output }" , validation_module: 'php-cgi sanity check ' );
329+ }
330+ }
301331 }
302332
303333 /**
@@ -311,20 +341,21 @@ public function deployBinary(int $type): bool
311341 $ src = match ($ type ) {
312342 BUILD_TARGET_CLI => SOURCE_PATH . "\\php-src \\x64 \\Release {$ ts }\\php.exe " ,
313343 BUILD_TARGET_MICRO => SOURCE_PATH . "\\php-src \\x64 \\Release {$ ts }\\micro.sfx " ,
344+ BUILD_TARGET_CGI => SOURCE_PATH . "\\php-src \\x64 \\Release {$ ts }\\php-cgi.exe " ,
314345 default => throw new SPCInternalException ("Deployment does not accept type {$ type }" ),
315346 };
316347
317348 // with-upx-pack for cli and micro
318349 if ($ this ->getOption ('with-upx-pack ' , false )) {
319- if ($ type === BUILD_TARGET_CLI || ($ type === BUILD_TARGET_MICRO && version_compare ($ this ->getMicroVersion (), '0.2.0 ' ) >= 0 )) {
350+ if ($ type === BUILD_TARGET_CLI || $ type === BUILD_TARGET_CGI || ($ type === BUILD_TARGET_MICRO && version_compare ($ this ->getMicroVersion (), '0.2.0 ' ) >= 0 )) {
320351 cmd ()->exec (getenv ('UPX_EXEC ' ) . ' --best ' . escapeshellarg ($ src ));
321352 }
322353 }
323354
324355 logger ()->info ('Deploying ' . $ this ->getBuildTypeName ($ type ) . ' file ' );
325- FileSystem::createDir (BUILD_ROOT_PATH . ' \bin ' );
356+ FileSystem::createDir (BUILD_BIN_PATH );
326357
327- cmd ()->exec ('copy ' . escapeshellarg ($ src ) . ' ' . escapeshellarg (BUILD_ROOT_PATH . '\bin \\' ));
358+ cmd ()->exec ('copy ' . escapeshellarg ($ src ) . ' ' . escapeshellarg (BUILD_BIN_PATH . '\\' ));
328359 return true ;
329360 }
330361
0 commit comments