Skip to content

Commit 80687df

Browse files
authored
Add macOS strip option -S, fix macOS micro strip option (#778)
* Add macOS strip option `-S`, fix macOS micro strip option * Change docs and help messages
1 parent 056971f commit 80687df

File tree

7 files changed

+31
-45
lines changed

7 files changed

+31
-45
lines changed

docs/en/faq/index.md

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,11 @@ buildroot/bin/php -d "zend_extension=/path/to/php{PHP_VER}-{ts/nts}/xdebug.so" -
3838
```
3939

4040
For macOS platform, almost all binaries under macOS cannot be truly purely statically linked, and almost all binaries will link macOS system libraries: `/usr/lib/libresolv.9.dylib` and `/usr/lib/libSystem.B.dylib`.
41-
So on macOS, you can use statically compiled PHP binaries under certain compilation conditions, and dynamically linked extensions:
41+
So on macOS, you can **directly** use SPC to build statically compiled PHP binaries with dynamically linked extensions:
4242

43-
1. Using the `--no-strip` parameter will not strip information such as debugging symbols from the binary file for use with external Zend extensions such as `Xdebug`.
44-
2. If you want to compile some Zend extensions, use Homebrew, MacPorts, source code compilation, and install a normal version of PHP on your operating system.
45-
3. Use the `phpize && ./configure && make` command to compile the extensions you want to use.
46-
4. Copy the extension file `xxxx.so` to the outside, use the statically compiled PHP binary, for example to use the Xdebug extension: `cd buildroot/bin/ && ./php -d "zend_extension=/path/to/xdebug.so"`.
47-
48-
```bash
49-
# build statically linked php-cli but not stripped
50-
bin/spc build ffi --build-cli --no-strip
51-
```
43+
1. Build shared extension `xxx.so` using: `--build-shared=XXX` option. e.g. `bin/spc build bcmath,zlib --build-shared=xdebug --build-cli`
44+
2. You will get `buildroot/modules/xdebug.so` and `buildroot/bin/php`.
45+
3. The `xdebug.so` file could be used for php that version and thread-safe are the same.
5246

5347
## Can it support Oracle database extension?
5448

docs/en/guide/manual-build.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ You can try to use the following commands:
339339
- `--cxx=XXX`: Specifies the execution command of the C++ language compiler (Linux defaults to `g++`, macOS defaults to `clang++`)
340340
- `--with-clean`: clean up old make files before compiling PHP
341341
- `--enable-zts`: Make compiled PHP thread-safe version (default is NTS version)
342-
- `--no-strip`: Do not run `strip` after compiling the PHP library to trim the binary file to reduce its size (the macOS binary file without trim can use dynamically linked third-party extensions)
342+
- `--no-strip`: Do not run `strip` after compiling the PHP library to trim the binary file to reduce its size
343343
- `--with-libs=XXX,YYY`: Compile the specified dependent library before compiling PHP, and activate some extended optional functions (such as libavif of the gd library, etc.)
344344
- `--with-config-file-path=XXX`: Set the path in which to look for `php.ini` (Check [here](../faq/index.html#what-is-the-path-of-php-ini) for default paths)
345345
- `--with-config-file-scan-dir=XXX`: Set the directory to scan for `.ini` files after reading `php.ini` (Check [here](../faq/index.html#what-is-the-path-of-php-ini) for default paths)

docs/zh/faq/index.md

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,11 @@ buildroot/bin/php -d "zend_extension=/path/to/php{PHP_VER}-{ts/nts}/xdebug.so" -
3535
```
3636

3737
对于 macOS 平台来说,macOS 下的几乎所有二进制文件都无法真正纯静态链接,几乎所有二进制文件都会链接 macOS 的系统库:`/usr/lib/libresolv.9.dylib``/usr/lib/libSystem.B.dylib`
38-
所以在 macOS 系统下,在特定的编译条件下可以使用静态编译的 php 二进制文件,可使用动态链接的扩展
38+
因此,在 macOS 上,您可以直接构建出使用静态编译的 PHP 二进制文件和动态链接的扩展
3939

40-
1. 使用 `--no-strip` 参数,将不会对二进制文件去除调试符号等信息,以供使用 `Xdebug` 等外部 Zend 扩展。
41-
2. 如果要编译某些 Zend 扩展,使用 Homebrew、MacPorts、源码编译的形式,在所在的操作系统安装一个普通版本的 PHP。
42-
3. 使用 `phpize && ./configure && make` 命令编译想要使用的扩展。
43-
4. 将扩展文件 `xxxx.so` 拷贝到外部,使用静态编译的 PHP 二进制,例如使用 Xdebug 扩展:`cd buildroot/bin/ && ./php -d "zend_extension=/path/to/xdebug.so"`
44-
45-
```bash
46-
# 构建静态 php-cli
47-
bin/spc build ffi --build-cli --no-strip
48-
```
40+
1. 使用 `--build-shared=XXX` 选项构建共享扩展 `xxx.so`。例如:`bin/spc build bcmath,zlib --build-shared=xdebug --build-cli`
41+
2. 您将获得 `buildroot/modules/xdebug.so``buildroot/bin/php`
42+
3. `xdebug.so` 文件可用于版本和线程安全相同的 php。
4943

5044
## 可以支持 Oracle 数据库扩展吗
5145

docs/zh/guide/manual-build.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ bin/spc build bcmath,curl,openssl,ftp,posix,pcntl --build-cli
296296
- `--cxx=XXX`: 指定 C++ 语言编译器的执行命令(Linux 默认 `g++`,macOS 默认 `clang++`)
297297
- `--with-clean`: 编译 PHP 前先清理旧的 make 产生的文件
298298
- `--enable-zts`: 让编译的 PHP 为线程安全版本(默认为 NTS 版本)
299-
- `--no-strip`: 编译 PHP 库后不运行 `strip` 裁剪二进制文件缩小体积(不裁剪的 macOS 二进制文件可使用动态链接的第三方扩展)
299+
- `--no-strip`: 编译 PHP 库后不运行 `strip` 裁剪二进制文件缩小体积
300300
- `--with-libs=XXX,YYY`: 编译 PHP 前先编译指定的依赖库,激活部分扩展的可选功能(例如 gd 库的 libavif 等)
301301
- `--with-config-file-path=XXX`: 查找 `php.ini` 的路径(在 [这里](../faq/index.html#php-ini-的路径是什么) 查看默认路径)
302302
- `--with-config-file-scan-dir=XXX`: 读取 `php.ini` 后扫描 `.ini` 文件的目录(在 [这里](../faq/index.html#php-ini-的路径是什么) 查看默认路径)

src/SPC/builder/macos/MacOSBuilder.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ protected function buildCli(): void
207207
$SPC_CMD_PREFIX_PHP_MAKE = getenv('SPC_CMD_PREFIX_PHP_MAKE') ?: 'make';
208208
$shell->exec("{$SPC_CMD_PREFIX_PHP_MAKE} {$vars} cli");
209209
if (!$this->getOption('no-strip', false)) {
210-
$shell->exec('dsymutil -f sapi/cli/php')->exec('strip sapi/cli/php');
210+
$shell->exec('dsymutil -f sapi/cli/php')->exec('strip -S sapi/cli/php');
211211
}
212212
$this->deployBinary(BUILD_TARGET_CLI);
213213
}
@@ -234,12 +234,15 @@ protected function buildMicro(): void
234234

235235
// patch fake cli for micro
236236
$vars['EXTRA_CFLAGS'] .= $enable_fake_cli;
237-
if ($this->getOption('no-strip', false)) {
238-
$vars['STRIP'] = 'dsymutil -f ';
239-
}
240237
$vars = SystemUtil::makeEnvVarString($vars);
241238

242-
shell()->cd(SOURCE_PATH . '/php-src')->exec(getenv('SPC_CMD_PREFIX_PHP_MAKE') . " {$vars} micro");
239+
$shell = shell()->cd(SOURCE_PATH . '/php-src');
240+
// build
241+
$shell->exec(getenv('SPC_CMD_PREFIX_PHP_MAKE') . " {$vars} micro");
242+
// strip
243+
if (!$this->getOption('no-strip', false)) {
244+
$shell->exec('dsymutil -f sapi/micro/micro.sfx')->exec('strip -S sapi/micro/micro.sfx');
245+
}
243246

244247
$this->deployBinary(BUILD_TARGET_MICRO);
245248

@@ -261,7 +264,7 @@ protected function buildFpm(): void
261264
$shell = shell()->cd(SOURCE_PATH . '/php-src');
262265
$shell->exec(getenv('SPC_CMD_PREFIX_PHP_MAKE') . " {$vars} fpm");
263266
if (!$this->getOption('no-strip', false)) {
264-
$shell->exec('dsymutil -f sapi/fpm/php-fpm')->exec('strip sapi/fpm/php-fpm');
267+
$shell->exec('dsymutil -f sapi/fpm/php-fpm')->exec('strip -S sapi/fpm/php-fpm');
265268
}
266269
$this->deployBinary(BUILD_TARGET_FPM);
267270
}

src/SPC/command/BuildPHPCommand.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function configure(): void
3434
$this->addOption('build-embed', null, InputOption::VALUE_OPTIONAL, 'Build embed SAPI (not available on Windows)');
3535
$this->addOption('build-frankenphp', null, null, 'Build FrankenPHP SAPI (not available on Windows)');
3636
$this->addOption('build-all', null, null, 'Build all SAPI');
37-
$this->addOption('no-strip', null, null, 'build without strip, in order to debug and load external extensions');
37+
$this->addOption('no-strip', null, null, 'build without strip, keep symbols to debug');
3838
$this->addOption('disable-opcache-jit', null, null, 'disable opcache jit');
3939
$this->addOption('with-config-file-path', null, InputOption::VALUE_REQUIRED, 'Set the path in which to look for php.ini', $isWindows ? null : '/usr/local/etc/php');
4040
$this->addOption('with-config-file-scan-dir', null, InputOption::VALUE_REQUIRED, 'Set the directory to scan for .ini files after reading php.ini', $isWindows ? null : '/usr/local/etc/php/conf.d');
@@ -62,11 +62,6 @@ public function handle(): int
6262
$rule = $this->parseRules($shared_extensions);
6363

6464
// check dynamic extension build env
65-
// macOS must use --no-strip option
66-
if (!empty($shared_extensions) && PHP_OS_FAMILY === 'Darwin' && !$this->getOption('no-strip')) {
67-
$this->output->writeln('MacOS does not support dynamic extension loading with stripped binary, please use --no-strip option!');
68-
return static::FAILURE;
69-
}
7065
// linux must build with glibc
7166
if (!empty($shared_extensions) && PHP_OS_FAMILY === 'Linux' && getenv('SPC_LIBC') !== 'glibc') {
7267
$this->output->writeln('Linux does not support dynamic extension loading with musl-libc full-static build, please build with glibc!');

src/globals/test-extensions.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313

1414
// test php version (8.1 ~ 8.4 available, multiple for matrix)
1515
$test_php_version = [
16-
// '8.1',
17-
// '8.2',
18-
// '8.3',
16+
'8.1',
17+
'8.2',
18+
'8.3',
1919
'8.4',
2020
];
2121

@@ -24,27 +24,27 @@
2424
'macos-13',
2525
// 'macos-14',
2626
'macos-15',
27-
'ubuntu-latest',
28-
'ubuntu-22.04',
29-
'ubuntu-24.04',
30-
'ubuntu-22.04-arm',
31-
'ubuntu-24.04-arm',
27+
// 'ubuntu-latest',
28+
// 'ubuntu-22.04',
29+
// 'ubuntu-24.04',
30+
// 'ubuntu-22.04-arm',
31+
// 'ubuntu-24.04-arm',
3232
// 'windows-latest',
3333
];
3434

3535
// whether enable thread safe
36-
$zts = true;
36+
$zts = false;
3737

3838
$no_strip = false;
3939

4040
// compress with upx
4141
$upx = false;
4242

4343
// whether to test frankenphp build, only available for macos and linux
44-
$frankenphp = true;
44+
$frankenphp = false;
4545

4646
// prefer downloading pre-built packages to speed up the build process
47-
$prefer_pre_built = false;
47+
$prefer_pre_built = true;
4848

4949
// If you want to test your added extensions and libs, add below (comma separated, example `bcmath,openssl`).
5050
$extensions = match (PHP_OS_FAMILY) {

0 commit comments

Comments
 (0)