Skip to content

Commit 12aadf1

Browse files
committed
refactor: replace SPC_LIBC with SPC_TARGET and update related logic
1 parent b04ffad commit 12aadf1

35 files changed

+420
-85
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ jobs:
105105
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
106106

107107
- name: "Run PHPUnit Tests"
108-
run: SPC_LIBC=glibc vendor/bin/phpunit tests/ --no-coverage
108+
run: SPC_TARGET=glibc vendor/bin/phpunit tests/ --no-coverage
109109

110110
define-matrix:
111111
name: "Define Matrix"

bin/build-static-frankenphp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ ADD ./bin/spc /app/bin/spc
9292
RUN /app/bin/setup-runtime
9393
RUN /app/bin/php /app/bin/composer install --no-dev --classmap-authoritative
9494
ENV PATH="/app/bin:/cmake/bin:/usr/local/go/bin:$PATH"
95-
ENV SPC_LIBC=glibc
95+
ENV SPC_TARGET=glibc
9696
9797
ADD ./config/env.ini /app/config/env.ini
9898
RUN bin/spc doctor --auto-fix --debug
@@ -146,7 +146,7 @@ echo 'CXX=/opt/rh/devtoolset-10/root/usr/bin/g++' >> /tmp/spc-gnu-docker.env
146146
echo 'AR=/opt/rh/devtoolset-10/root/usr/bin/ar' >> /tmp/spc-gnu-docker.env
147147
echo 'LD=/opt/rh/devtoolset-10/root/usr/bin/ld' >> /tmp/spc-gnu-docker.env
148148
echo 'SPC_DEFAULT_C_FLAGS=-fPIE -fPIC' >> /tmp/spc-gnu-docker.env
149-
echo 'SPC_LIBC=glibc' >> /tmp/spc-gnu-docker.env
149+
echo 'SPC_TARGET=glibc' >> /tmp/spc-gnu-docker.env
150150
echo 'SPC_CMD_VAR_PHP_MAKE_EXTRA_LDFLAGS_PROGRAM="-Wl,-O1 -pie"' >> /tmp/spc-gnu-docker.env
151151
echo 'SPC_CMD_VAR_PHP_MAKE_EXTRA_LIBS="-ldl -lpthread -lm -lresolv -lutil -lrt"' >> /tmp/spc-gnu-docker.env
152152

bin/spc-gnu-docker

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ ADD ./bin/spc /app/bin/spc
108108
RUN /app/bin/setup-runtime
109109
RUN /app/bin/php /app/bin/composer install --no-dev
110110
ENV PATH="/app/bin:/cmake/bin:$PATH"
111-
ENV SPC_LIBC=glibc
111+
ENV SPC_TARGET=glibc
112112
113113
ADD ./config/env.ini /app/config/env.ini
114114
RUN CC=gcc bin/spc doctor --auto-fix --debug
@@ -159,7 +159,7 @@ echo 'CXX=/opt/rh/devtoolset-10/root/usr/bin/g++' >> /tmp/spc-gnu-docker.env
159159
echo 'AR=/opt/rh/devtoolset-10/root/usr/bin/ar' >> /tmp/spc-gnu-docker.env
160160
echo 'LD=/opt/rh/devtoolset-10/root/usr/bin/ld' >> /tmp/spc-gnu-docker.env
161161
echo 'SPC_DEFAULT_C_FLAGS=-fPIC' >> /tmp/spc-gnu-docker.env
162-
echo 'SPC_LIBC=glibc' >> /tmp/spc-gnu-docker.env
162+
echo 'SPC_TARGET=glibc' >> /tmp/spc-gnu-docker.env
163163
echo 'SPC_CMD_VAR_PHP_MAKE_EXTRA_LDFLAGS_PROGRAM="-Wl,-O1 -pie"' >> /tmp/spc-gnu-docker.env
164164
echo 'SPC_CMD_VAR_PHP_MAKE_EXTRA_LIBS="-ldl -lpthread -lm -lresolv -lutil -lrt"' >> /tmp/spc-gnu-docker.env
165165

config/env.ini

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ SPC_CMD_VAR_FRANKENPHP_XCADDY_MODULES="--with github.com/dunglas/frankenphp/cadd
5555
; EXTENSION_DIR=
5656

5757
[windows]
58+
; build target: win7-static
59+
SPC_TARGET=msvc-static
5860
; php-sdk-binary-tools path
5961
PHP_SDK_PATH="${WORKING_DIR}\php-sdk-binary-tools"
6062
; upx executable path
@@ -63,8 +65,17 @@ UPX_EXEC="${PKG_ROOT_PATH}\bin\upx.exe"
6365
SPC_MICRO_PATCHES=static_extensions_win32,cli_checks,disable_huge_page,vcruntime140,win32,zend_stream,cli_static
6466

6567
[linux]
66-
; include PATH for musl libc.
67-
SPC_LIBC=musl
68+
; Linux can use different build toolchain, but the toolchain can not be changed in this file:
69+
; - musl (default): used for general linux distros, can build `musl-static` target only.
70+
; - zig (WIP): used for general linux distros, can build `musl` and `glibc` targets.
71+
; - musl-native: used for alpine linux, can build `musl-static` and `musl`(WIP) target.
72+
; - gnu-native (assume): used for general linux distros, can build `glibc` target only and have portability issues.
73+
74+
; build target:
75+
; - musl-static (default): pure static linking, using musl-libc, can run on any linux distro.
76+
; - musl: static linking with dynamic linking to musl-libc, can run on musl-based linux distro.
77+
; - glibc: static linking with dynamic linking to glibc, can run on glibc-based linux distro.
78+
SPC_TARGET=musl-static
6879
; compiler environments
6980
CC=${SPC_LINUX_DEFAULT_CC}
7081
CXX=${SPC_LINUX_DEFAULT_CXX}
@@ -109,6 +120,9 @@ SPC_CMD_VAR_PHP_MAKE_EXTRA_LDFLAGS=""
109120
SPC_CMD_VAR_PHP_MAKE_EXTRA_LDFLAGS_PROGRAM="-all-static -Wl,-O1 -pie"
110121

111122
[macos]
123+
; build target: macho or macho (possibly we could support macho-universal in the future)
124+
; Currently we do not support universal and cross-compilation for macOS.
125+
SPC_TARGET=macho
112126
; compiler environments
113127
CC=clang
114128
CXX=clang++

docs/en/guide/extension-notes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ and this extension cannot be compiled into php by static linking, so it cannot b
8282

8383
## xdebug
8484

85-
1. Xdebug is only buildable as a shared extension. On Linux, you need to use static-php-cli with SPC_LIBC=glibc.
85+
1. Xdebug is only buildable as a shared extension. You need to use a build target other than `musl-static` for SPC_TARGET.
8686
2. When using Linux/glibc or macOS, you can compile Xdebug as a shared extension using --build-shared="xdebug".
8787
The compiled `./php` binary can be configured and run by specifying the INI, eg `./php -d 'zend_extension=/path/to/xdebug.so' your-code.php`.
8888

docs/zh/guide/extension-notes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ bin/spc build gd --with-libs=freetype,libjpeg,libavif,libwebp --build-cli
7676

7777
## xdebug
7878

79-
1. Xdebug 只能作为共享扩展进行构建。在 Linux 上,您需要使用 static-php-cli 并设置 SPC_LIBC=glibc
79+
1. Xdebug 只能作为共享扩展进行构建。您需要使用除了 `musl-static` 外的其他 `SPC_TARGET` 构建目标
8080
2. 使用 Linux/glibc 或 macOS 时,您可以使用 `--build-shared=xdebug` 将 Xdebug 编译为共享扩展。
8181
编译后的 `./php` 二进制文件可以通过指定 INI 文件进行配置和运行,例如 `./php -d 'zend_extension=/path/to/xdebug.so' your-code.php`
8282

src/SPC/ConsoleApplication.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use SPC\command\CraftCommand;
1010
use SPC\command\DeleteDownloadCommand;
1111
use SPC\command\dev\AllExtCommand;
12+
use SPC\command\dev\EnvCommand;
1213
use SPC\command\dev\ExtVerCommand;
1314
use SPC\command\dev\GenerateExtDepDocsCommand;
1415
use SPC\command\dev\GenerateExtDocCommand;
@@ -70,6 +71,7 @@ public function __construct()
7071
new GenerateExtDepDocsCommand(),
7172
new GenerateLibDepDocsCommand(),
7273
new PackLibCommand(),
74+
new EnvCommand(),
7375
]
7476
);
7577
}

src/SPC/builder/extension/imagick.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use SPC\builder\Extension;
88
use SPC\util\CustomExt;
9+
use SPC\util\SPCTarget;
910

1011
#[CustomExt('imagick')]
1112
class imagick extends Extension
@@ -15,7 +16,7 @@ public function patchBeforeMake(): bool
1516
if (PHP_OS_FAMILY !== 'Linux') {
1617
return false;
1718
}
18-
if (getenv('SPC_LIBC') === 'glibc' && str_contains(getenv('CC'), 'devtoolset-10')) {
19+
if (SPCTarget::isTarget(SPCTarget::GLIBC)) {
1920
return false;
2021
}
2122
// imagick with calls omp_pause_all, which requires openmp, on non-musl we build imagick without openmp
@@ -26,15 +27,15 @@ public function patchBeforeMake(): bool
2627

2728
public function getUnixConfigureArg(bool $shared = false): string
2829
{
29-
$disable_omp = !(getenv('SPC_LIBC') === 'glibc' && str_contains(getenv('CC'), 'devtoolset-10')) ? '' : ' ac_cv_func_omp_pause_resource_all=no';
30+
$disable_omp = SPCTarget::isTarget(SPCTarget::GLIBC) ? ' ac_cv_func_omp_pause_resource_all=no' : '';
3031
return '--with-imagick=' . ($shared ? 'shared,' : '') . BUILD_ROOT_PATH . $disable_omp;
3132
}
3233

3334
protected function getStaticAndSharedLibs(): array
3435
{
3536
// on centos 7, it will use the symbol _ZTINSt6thread6_StateE, which is not defined in system libstdc++.so.6
3637
[$static, $shared] = parent::getStaticAndSharedLibs();
37-
if (getenv('SPC_LIBC') === 'glibc' && str_contains(getenv('CC'), 'devtoolset-10')) {
38+
if (SPCTarget::isTarget(SPCTarget::GLIBC)) {
3839
$static .= ' -lstdc++';
3940
$shared = str_replace('-lstdc++', '', $shared);
4041
}

src/SPC/builder/linux/LinuxBuilder.php

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,8 @@ public function __construct(array $options = [])
2525
{
2626
$this->options = $options;
2727

28-
// check musl-cross make installed if we use musl-cross-make
29-
$arch = arch2gnu(php_uname('m'));
30-
3128
GlobalEnvManager::init();
32-
33-
if (getenv('SPC_LIBC') === 'musl' && !SystemUtil::isMuslDist()) {
34-
$this->setOptionIfNotExist('library_path', "LIBRARY_PATH=\"/usr/local/musl/{$arch}-linux-musl/lib\"");
35-
$this->setOptionIfNotExist('ld_library_path', "LD_LIBRARY_PATH=\"/usr/local/musl/{$arch}-linux-musl/lib\"");
36-
$configure = getenv('SPC_CMD_PREFIX_PHP_CONFIGURE');
37-
$configure = "LD_LIBRARY_PATH=\"/usr/local/musl/{$arch}-linux-musl/lib\" " . $configure;
38-
GlobalEnvManager::putenv("SPC_CMD_PREFIX_PHP_CONFIGURE={$configure}");
39-
40-
if (!file_exists("/usr/local/musl/{$arch}-linux-musl/lib/libc.a")) {
41-
throw new WrongUsageException('You are building with musl-libc target in glibc distro, but musl-toolchain is not installed, please install musl-toolchain first. (You can use `doctor` command to install it)');
42-
}
43-
}
29+
GlobalEnvManager::afterInit();
4430

4531
// concurrency
4632
$this->concurrency = intval(getenv('SPC_CONCURRENCY'));

src/SPC/builder/linux/SystemUtil.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use SPC\builder\traits\UnixSystemUtilTrait;
88
use SPC\exception\RuntimeException;
9+
use SPC\util\SPCTarget;
910

1011
class SystemUtil
1112
{
@@ -193,7 +194,7 @@ public static function getLibcVersionIfExists(): ?string
193194
if (self::$libc_version !== null) {
194195
return self::$libc_version;
195196
}
196-
if (PHP_OS_FAMILY === 'Linux' && getenv('SPC_LIBC') === 'glibc') {
197+
if (SPCTarget::isTarget(SPCTarget::GLIBC)) {
197198
$result = shell()->execWithResult('ldd --version', false);
198199
if ($result[0] !== 0) {
199200
return null;
@@ -208,7 +209,7 @@ public static function getLibcVersionIfExists(): ?string
208209
}
209210
return null;
210211
}
211-
if (PHP_OS_FAMILY === 'Linux' && getenv('SPC_LIBC') === 'musl') {
212+
if (SPCTarget::isTarget(SPCTarget::MUSL_STATIC)) {
212213
if (self::isMuslDist()) {
213214
$result = shell()->execWithResult('ldd 2>&1', false);
214215
} else {

0 commit comments

Comments
 (0)