Skip to content

Commit 9190335

Browse files
authored
Merge pull request #810 from crazywhalecc/refactor/spc-target
refactor: replace SPC_LIBC with SPC_TARGET and update related logic
2 parents 840e09a + 68e500e commit 9190335

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+469
-137
lines changed

bin/spc-gnu-docker

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ fi
6464
# Detect docker env is setup
6565
if ! $DOCKER_EXECUTABLE images | grep -q cwcc-spc-gnu-$SPC_USE_ARCH-$SPC_DOCKER_VERSION; then
6666
echo "Docker container does not exist. Building docker image ..."
67-
$DOCKER_EXECUTABLE buildx build $PLATFORM_ARG --no-cache -t cwcc-spc-gnu-$SPC_USE_ARCH-$SPC_DOCKER_VERSION -f- . <<EOF
67+
$DOCKER_EXECUTABLE buildx build $PLATFORM_ARG -t cwcc-spc-gnu-$SPC_USE_ARCH-$SPC_DOCKER_VERSION -f- . <<EOF
6868
FROM centos:7
6969
RUN sed -i 's/mirror.centos.org/vault.centos.org/g' /etc/yum.repos.d/*.repo && \
7070
sed -i 's/^#.*baseurl=http/baseurl=http/g' /etc/yum.repos.d/*.repo && \
@@ -96,7 +96,7 @@ RUN curl -fsSL -o patchelf.tgz https://github.com/NixOS/patchelf/releases/downlo
9696
tar -xzf patchelf.tgz -C /patchelf --strip-components=1 && \
9797
cp /patchelf/bin/patchelf /usr/bin/
9898
99-
RUN curl -o cmake.tgz -fsSL https://github.com/Kitware/CMake/releases/download/v3.31.4/cmake-3.31.4-linux-$SPC_USE_ARCH.tar.gz && \
99+
RUN curl -o cmake.tgz -#fSL https://github.com/Kitware/CMake/releases/download/v3.31.4/cmake-3.31.4-linux-$SPC_USE_ARCH.tar.gz && \
100100
mkdir /cmake && \
101101
tar -xzf cmake.tgz -C /cmake --strip-components 1
102102
@@ -107,8 +107,8 @@ ADD ./bin/setup-runtime /app/bin/setup-runtime
107107
ADD ./bin/spc /app/bin/spc
108108
RUN /app/bin/setup-runtime
109109
RUN /app/bin/php /app/bin/composer install --no-dev
110-
ENV PATH="/app/bin:/cmake/bin:$PATH"
111110
ENV SPC_LIBC=glibc
111+
ENV PATH="/app/bin:/cmake/bin:/opt/rh/devtoolset-10/root/usr/bin:\$PATH"
112112
113113
ADD ./config/env.ini /app/config/env.ini
114114
RUN CC=gcc bin/spc doctor --auto-fix --debug
@@ -128,7 +128,6 @@ RUN curl -o automake.tgz -fsSL https://ftp.gnu.org/gnu/automake/automake-1.17.ta
128128
make && \
129129
make install && \
130130
ln -sf /usr/local/bin/automake /usr/bin/automake
131-
132131
EOF
133132
fi
134133

@@ -154,11 +153,7 @@ if [ -f "$(pwd)/craft.yml" ]; then
154153
fi
155154

156155
# Apply env in temp env file
157-
echo 'CC=/opt/rh/devtoolset-10/root/usr/bin/gcc' > /tmp/spc-gnu-docker.env
158-
echo 'CXX=/opt/rh/devtoolset-10/root/usr/bin/g++' >> /tmp/spc-gnu-docker.env
159-
echo 'AR=/opt/rh/devtoolset-10/root/usr/bin/ar' >> /tmp/spc-gnu-docker.env
160-
echo 'LD=/opt/rh/devtoolset-10/root/usr/bin/ld' >> /tmp/spc-gnu-docker.env
161-
echo 'SPC_DEFAULT_C_FLAGS=-fPIC' >> /tmp/spc-gnu-docker.env
156+
echo 'SPC_DEFAULT_C_FLAGS=-fPIC' > /tmp/spc-gnu-docker.env
162157
echo 'SPC_LIBC=glibc' >> /tmp/spc-gnu-docker.env
163158
echo 'SPC_CMD_VAR_PHP_MAKE_EXTRA_LDFLAGS_PROGRAM="-Wl,-O1 -pie"' >> /tmp/spc-gnu-docker.env
164159
echo 'SPC_CMD_VAR_PHP_MAKE_EXTRA_LIBS="-ldl -lpthread -lm -lresolv -lutil -lrt"' >> /tmp/spc-gnu-docker.env

config/env.ini

Lines changed: 16 additions & 0 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=native-windows
5860
; php-sdk-binary-tools path
5961
PHP_SDK_PATH="${WORKING_DIR}\php-sdk-binary-tools"
6062
; upx executable path
@@ -63,6 +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]
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+
6679
; include PATH for musl libc.
6780
SPC_LIBC=musl
6881
; compiler environments
@@ -109,6 +122,9 @@ SPC_CMD_VAR_PHP_MAKE_EXTRA_LDFLAGS=""
109122
SPC_CMD_VAR_PHP_MAKE_EXTRA_LDFLAGS_PROGRAM="-all-static -Wl,-O1 -pie"
110123

111124
[macos]
125+
; build target: macho or macho (possibly we could support macho-universal in the future)
126+
; Currently we do not support universal and cross-compilation for macOS.
127+
SPC_TARGET=native-macos
112128
; compiler environments
113129
CC=clang
114130
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

phpunit.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
>
55
<php>
66
<env name="SPC_IGNORE_BAD_HASH" value="yes" force="true" />
7+
<env name="SPC_SKIP_TOOLCHAIN_CHECK" value="yes" force="true" />
78
</php>
89
</phpunit>

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.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use SPC\store\Config;
1111
use SPC\store\FileSystem;
1212
use SPC\util\SPCConfigUtil;
13+
use SPC\util\SPCTarget;
1314

1415
class Extension
1516
{
@@ -532,6 +533,11 @@ protected function getStaticAndSharedLibs(): array
532533
$sharedLibString .= '-l' . $lib . ' ';
533534
}
534535
}
536+
// move static libstdc++ to shared if we are on non-full-static build target
537+
if (!SPCTarget::isStatic() && in_array(SPCTarget::getLibc(), SPCTarget::LIBC_LIST)) {
538+
$staticLibString .= ' -lstdc++';
539+
$sharedLibString = str_replace('-lstdc++', '', $sharedLibString);
540+
}
535541
return [trim($staticLibString), trim($sharedLibString)];
536542
}
537543

src/SPC/builder/extension/imagick.php

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,9 @@
1010
#[CustomExt('imagick')]
1111
class imagick extends Extension
1212
{
13-
public function patchBeforeMake(): bool
14-
{
15-
if (PHP_OS_FAMILY !== 'Linux') {
16-
return false;
17-
}
18-
if (getenv('SPC_LIBC') === 'glibc' && str_contains(getenv('CC'), 'devtoolset-10')) {
19-
return false;
20-
}
21-
// imagick with calls omp_pause_all, which requires openmp, on non-musl we build imagick without openmp
22-
$extra_libs = trim(getenv('SPC_EXTRA_LIBS') . ' -lgomp');
23-
f_putenv('SPC_EXTRA_LIBS=' . $extra_libs);
24-
return true;
25-
}
26-
2713
public function getUnixConfigureArg(bool $shared = false): string
2814
{
29-
$disable_omp = !(getenv('SPC_LIBC') === 'glibc' && str_contains(getenv('CC'), 'devtoolset-10')) ? '' : ' ac_cv_func_omp_pause_resource_all=no';
15+
$disable_omp = ' ac_cv_func_omp_pause_resource_all=no';
3016
return '--with-imagick=' . ($shared ? 'shared,' : '') . BUILD_ROOT_PATH . $disable_omp;
3117
}
32-
33-
protected function getStaticAndSharedLibs(): array
34-
{
35-
// on centos 7, it will use the symbol _ZTINSt6thread6_StateE, which is not defined in system libstdc++.so.6
36-
[$static, $shared] = parent::getStaticAndSharedLibs();
37-
if (getenv('SPC_LIBC') === 'glibc' && str_contains(getenv('CC'), 'devtoolset-10')) {
38-
$static .= ' -lstdc++';
39-
$shared = str_replace('-lstdc++', '', $shared);
40-
}
41-
return [$static, $shared];
42-
}
4318
}

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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,12 @@ public static function getSupportedDistros(): array
188188
/**
189189
* Get libc version string from ldd
190190
*/
191-
public static function getLibcVersionIfExists(): ?string
191+
public static function getLibcVersionIfExists(string $libc): ?string
192192
{
193193
if (self::$libc_version !== null) {
194194
return self::$libc_version;
195195
}
196-
if (PHP_OS_FAMILY === 'Linux' && getenv('SPC_LIBC') === 'glibc') {
196+
if ($libc === 'glibc') {
197197
$result = shell()->execWithResult('ldd --version', false);
198198
if ($result[0] !== 0) {
199199
return null;
@@ -208,7 +208,7 @@ public static function getLibcVersionIfExists(): ?string
208208
}
209209
return null;
210210
}
211-
if (PHP_OS_FAMILY === 'Linux' && getenv('SPC_LIBC') === 'musl') {
211+
if ($libc === 'musl') {
212212
if (self::isMuslDist()) {
213213
$result = shell()->execWithResult('ldd 2>&1', false);
214214
} else {

0 commit comments

Comments
 (0)