Skip to content

Commit 8d30334

Browse files
authored
Merge pull request #883 from crazywhalecc/sapi/cgi
Add cgi support for macOS and Linux
2 parents 2a2f412 + fe945ab commit 8d30334

File tree

12 files changed

+69
-4
lines changed

12 files changed

+69
-4
lines changed

README-zh.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ bin/spc --version
207207
- `--build-micro`:构建 phpmicro 自执行二进制
208208
- `--build-fpm`:构建 fpm
209209
- `--build-embed`:构建 embed(libphp)
210+
- `--build-cgi`: 构建 cgi(不推荐)
210211
- `--build-all`:构建所有
211212

212213
如果出现了任何错误,可以使用 `--debug` 参数来展示完整的输出日志,以供排查错误:

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ Now we support `cli`, `micro`, `fpm` and `embed` SAPI. You can use one or more o
218218
- `--build-micro`: build static phpmicro self-extracted executable
219219
- `--build-fpm`: build static fpm binary
220220
- `--build-embed`: build embed (libphp)
221+
- `--build-cgi`: build cgi binary (not recommended)
221222
- `--build-all`: build all
222223

223224
If anything goes wrong, use `--debug` option to display full terminal output:

config/env.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ SPC_MICRO_PATCHES=cli_checks,disable_huge_page
103103
; buildconf command
104104
SPC_CMD_PREFIX_PHP_BUILDCONF="./buildconf --force"
105105
; configure command
106-
SPC_CMD_PREFIX_PHP_CONFIGURE="./configure --prefix= --with-valgrind=no --disable-shared --enable-static --disable-all --disable-cgi --disable-phpdbg --with-pic"
106+
SPC_CMD_PREFIX_PHP_CONFIGURE="./configure --prefix= --with-valgrind=no --disable-shared --enable-static --disable-all --disable-phpdbg --with-pic"
107107
; make command
108108
SPC_CMD_PREFIX_PHP_MAKE="make -j${SPC_CONCURRENCY}"
109109

@@ -137,7 +137,7 @@ SPC_MICRO_PATCHES=cli_checks,macos_iconv
137137
; buildconf command
138138
SPC_CMD_PREFIX_PHP_BUILDCONF="./buildconf --force"
139139
; configure command
140-
SPC_CMD_PREFIX_PHP_CONFIGURE="./configure --prefix= --with-valgrind=no --enable-shared=no --enable-static=yes --disable-all --disable-cgi --disable-phpdbg"
140+
SPC_CMD_PREFIX_PHP_CONFIGURE="./configure --prefix= --with-valgrind=no --enable-shared=no --enable-static=yes --disable-all --disable-phpdbg"
141141
; make command
142142
SPC_CMD_PREFIX_PHP_MAKE="make -j${SPC_CONCURRENCY}"
143143

docs/en/guide/manual-build.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ You need to specify a compilation target, choose from the following parameters:
289289

290290
- `--build-cli`: Build a cli sapi (command line interface, which can execute PHP code on the command line)
291291
- `--build-fpm`: Build a fpm sapi (php-fpm, used in conjunction with other traditional fpm architecture software such as nginx)
292+
- `--build-cgi`: Build a cgi sapi (cgi, rarely used)
292293
- `--build-micro`: Build a micro sapi (used to build a standalone executable binary containing PHP code)
293294
- `--build-embed`: Build an embed sapi (used to embed into other C language programs)
294295
- `--build-frankenphp`: Build a [FrankenPHP](https://github.com/php/frankenphp) executable

docs/zh/guide/manual-build.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ bin/spc doctor --auto-fix
249249

250250
- `--build-cli`: 构建一个 cli sapi(命令行界面,可在命令行执行 PHP 代码)
251251
- `--build-fpm`: 构建一个 fpm sapi(php-fpm,用于和其他传统的 fpm 架构的软件如 nginx 配合使用)
252+
- `--build-cgi`: 构建一个 cgi sapi(cgi,可用于传统的 cgi 架构的软件如 apache 配合使用)
252253
- `--build-micro`: 构建一个 micro sapi(用于构建一个包含 PHP 代码的独立可执行二进制)
253254
- `--build-embed`: 构建一个 embed sapi(用于嵌入到其他 C 语言程序中)
254255
- `--build-frankenphp`: 构建一个 [frankenphp](https://github.com/php/frankenphp) 二进制

src/SPC/builder/BuilderBase.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,9 @@ public function getBuildTypeName(int $type): string
392392
if (($type & BUILD_TARGET_FRANKENPHP) === BUILD_TARGET_FRANKENPHP) {
393393
$ls[] = 'frankenphp';
394394
}
395+
if (($type & BUILD_TARGET_CGI) === BUILD_TARGET_CGI) {
396+
$ls[] = 'cgi';
397+
}
395398
return implode(', ', $ls);
396399
}
397400

src/SPC/builder/linux/LinuxBuilder.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public function buildPHP(int $build_target = BUILD_TARGET_NONE): void
8484
$enableMicro = ($build_target & BUILD_TARGET_MICRO) === BUILD_TARGET_MICRO;
8585
$enableEmbed = ($build_target & BUILD_TARGET_EMBED) === BUILD_TARGET_EMBED;
8686
$enableFrankenphp = ($build_target & BUILD_TARGET_FRANKENPHP) === BUILD_TARGET_FRANKENPHP;
87+
$enableCgi = ($build_target & BUILD_TARGET_CGI) === BUILD_TARGET_CGI;
8788

8889
// prepare build php envs
8990
// $musl_flag = SPCTarget::getLibc() === 'musl' ? ' -D__MUSL__' : ' -U__MUSL__';
@@ -110,6 +111,7 @@ public function buildPHP(int $build_target = BUILD_TARGET_NONE): void
110111
($enableFpm ? '--enable-fpm ' . ($this->getLib('libacl') !== null ? '--with-fpm-acl ' : '') : '--disable-fpm ') .
111112
($enableEmbed ? "--enable-embed={$embed_type} " : '--disable-embed ') .
112113
($enableMicro ? '--enable-micro=all-static ' : '--disable-micro ') .
114+
($enableCgi ? '--enable-cgi ' : '--disable-cgi ') .
113115
$config_file_path .
114116
$config_file_scan_dir .
115117
$json_74 .
@@ -131,6 +133,10 @@ public function buildPHP(int $build_target = BUILD_TARGET_NONE): void
131133
logger()->info('building fpm');
132134
$this->buildFpm();
133135
}
136+
if ($enableCgi) {
137+
logger()->info('building cgi');
138+
$this->buildCgi();
139+
}
134140
if ($enableMicro) {
135141
logger()->info('building micro');
136142
$this->buildMicro();
@@ -182,6 +188,25 @@ protected function buildCli(): void
182188
$this->deployBinary(BUILD_TARGET_CLI);
183189
}
184190

191+
protected function buildCgi(): void
192+
{
193+
$vars = SystemUtil::makeEnvVarString($this->getMakeExtraVars());
194+
$SPC_CMD_PREFIX_PHP_MAKE = getenv('SPC_CMD_PREFIX_PHP_MAKE') ?: 'make';
195+
shell()->cd(SOURCE_PATH . '/php-src')
196+
->exec('sed -i "s|//lib|/lib|g" Makefile')
197+
->exec("{$SPC_CMD_PREFIX_PHP_MAKE} {$vars} cgi");
198+
199+
if (!$this->getOption('no-strip', false)) {
200+
shell()->cd(SOURCE_PATH . '/php-src/sapi/cgi')->exec('strip --strip-unneeded php-cgi');
201+
}
202+
if ($this->getOption('with-upx-pack')) {
203+
shell()->cd(SOURCE_PATH . '/php-src/sapi/cgi')
204+
->exec(getenv('UPX_EXEC') . ' --best php-cgi');
205+
}
206+
207+
$this->deployBinary(BUILD_TARGET_CGI);
208+
}
209+
185210
/**
186211
* Build phpmicro sapi
187212
*/

src/SPC/builder/macos/MacOSBuilder.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ public function buildPHP(int $build_target = BUILD_TARGET_NONE): void
100100
$enableMicro = ($build_target & BUILD_TARGET_MICRO) === BUILD_TARGET_MICRO;
101101
$enableEmbed = ($build_target & BUILD_TARGET_EMBED) === BUILD_TARGET_EMBED;
102102
$enableFrankenphp = ($build_target & BUILD_TARGET_FRANKENPHP) === BUILD_TARGET_FRANKENPHP;
103+
$enableCgi = ($build_target & BUILD_TARGET_CGI) === BUILD_TARGET_CGI;
103104

104105
// prepare build php envs
105106
$envs_build_php = SystemUtil::makeEnvVarString([
@@ -124,6 +125,7 @@ public function buildPHP(int $build_target = BUILD_TARGET_NONE): void
124125
($enableFpm ? '--enable-fpm ' : '--disable-fpm ') .
125126
($enableEmbed ? "--enable-embed={$embed_type} " : '--disable-embed ') .
126127
($enableMicro ? '--enable-micro ' : '--disable-micro ') .
128+
($enableCgi ? '--enable-cgi ' : '--disable-cgi ') .
127129
$config_file_path .
128130
$config_file_scan_dir .
129131
$json_74 .
@@ -145,6 +147,10 @@ public function buildPHP(int $build_target = BUILD_TARGET_NONE): void
145147
logger()->info('building fpm');
146148
$this->buildFpm();
147149
}
150+
if ($enableCgi) {
151+
logger()->info('building cgi');
152+
$this->buildCgi();
153+
}
148154
if ($enableMicro) {
149155
logger()->info('building micro');
150156
$this->buildMicro();
@@ -189,6 +195,19 @@ protected function buildCli(): void
189195
$this->deployBinary(BUILD_TARGET_CLI);
190196
}
191197

198+
protected function buildCgi(): void
199+
{
200+
$vars = SystemUtil::makeEnvVarString($this->getMakeExtraVars());
201+
202+
$shell = shell()->cd(SOURCE_PATH . '/php-src');
203+
$SPC_CMD_PREFIX_PHP_MAKE = getenv('SPC_CMD_PREFIX_PHP_MAKE') ?: 'make';
204+
$shell->exec("{$SPC_CMD_PREFIX_PHP_MAKE} {$vars} cgi");
205+
if (!$this->getOption('no-strip', false)) {
206+
$shell->exec('dsymutil -f sapi/cgi/php-cgi')->exec('strip -S sapi/cgi/php-cgi');
207+
}
208+
$this->deployBinary(BUILD_TARGET_CGI);
209+
}
210+
192211
/**
193212
* Build phpmicro sapi
194213
*/

src/SPC/builder/unix/UnixBuilderBase.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,16 @@ protected function sanityCheck(int $build_target): void
120120
}
121121
}
122122

123+
// sanity check for php-cgi
124+
if (($build_target & BUILD_TARGET_CGI) === BUILD_TARGET_CGI) {
125+
logger()->info('running cgi sanity check');
126+
[$ret, $output] = shell()->execWithResult("echo '<?php echo \"<h1>Hello, World!</h1>\";' | " . BUILD_BIN_PATH . '/php-cgi -n');
127+
$raw_output = implode('', $output);
128+
if ($ret !== 0 || !str_contains($raw_output, 'Hello, World!') || !str_contains($raw_output, 'text/html')) {
129+
throw new ValidationException("cgi failed sanity check. code: {$ret}, output: {$raw_output}", validation_module: 'php-cgi sanity check');
130+
}
131+
}
132+
123133
// sanity check for embed
124134
if (($build_target & BUILD_TARGET_EMBED) === BUILD_TARGET_EMBED) {
125135
logger()->info('running embed sanity check');
@@ -207,6 +217,7 @@ protected function deployBinary(int $type): bool
207217
BUILD_TARGET_CLI => SOURCE_PATH . '/php-src/sapi/cli/php',
208218
BUILD_TARGET_MICRO => SOURCE_PATH . '/php-src/sapi/micro/micro.sfx',
209219
BUILD_TARGET_FPM => SOURCE_PATH . '/php-src/sapi/fpm/php-fpm',
220+
BUILD_TARGET_CGI => SOURCE_PATH . '/php-src/sapi/cgi/php-cgi',
210221
default => throw new SPCInternalException("Deployment does not accept type {$type}"),
211222
};
212223
logger()->info('Deploying ' . $this->getBuildTypeName($type) . ' file');

src/SPC/command/BuildPHPCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public function configure(): void
3333
$this->addOption('build-fpm', null, null, 'Build fpm SAPI (not available on Windows)');
3434
$this->addOption('build-embed', null, null, 'Build embed SAPI (not available on Windows)');
3535
$this->addOption('build-frankenphp', null, null, 'Build FrankenPHP SAPI (not available on Windows)');
36+
$this->addOption('build-cgi', null, null, 'Build cgi SAPI (not available on Windows)');
3637
$this->addOption('build-all', null, null, 'Build all SAPI');
3738
$this->addOption('no-strip', null, null, 'build without strip, keep symbols to debug');
3839
$this->addOption('disable-opcache-jit', null, null, 'disable opcache jit');
@@ -274,6 +275,7 @@ private function parseRules(array $shared_extensions = []): int
274275
$rule |= ($this->getOption('build-fpm') ? BUILD_TARGET_FPM : BUILD_TARGET_NONE);
275276
$rule |= $this->getOption('build-embed') || !empty($shared_extensions) ? BUILD_TARGET_EMBED : BUILD_TARGET_NONE;
276277
$rule |= ($this->getOption('build-frankenphp') ? (BUILD_TARGET_FRANKENPHP | BUILD_TARGET_EMBED) : BUILD_TARGET_NONE);
278+
$rule |= ($this->getOption('build-cgi') ? BUILD_TARGET_CGI : BUILD_TARGET_NONE);
277279
$rule |= ($this->getOption('build-all') ? BUILD_TARGET_ALL : BUILD_TARGET_NONE);
278280
return $rule;
279281
}

0 commit comments

Comments
 (0)