Skip to content

Commit 958fd0c

Browse files
committed
Merge branch 'main' of https://github.com/crazywhalecc/static-php-cli into grpc
2 parents a44dc0a + 0d0c789 commit 958fd0c

36 files changed

+418
-404
lines changed

.github/workflows/ext-matrix-tests.yml

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,4 @@ jobs:
136136
- name: "Build library: ${{ matrix.library }}"
137137
run: |
138138
SPC_USE_SUDO=yes ./bin/spc doctor --auto-fix
139-
if [ "${{ env.OS }}" = "linux-x86_64" ]; then
140-
./bin/spc install-pkg upx
141-
UPX=--with-upx-pack
142-
elif [ "${{ env.OS }}" = "linux-aarch64" ]; then
143-
./bin/spc install-pkg upx
144-
UPX=--with-upx-pack
145-
fi
146-
./bin/spc build --build-cli --build-micro --build-fpm ${{ matrix.extension }} --debug $UPX --with-suggested-libs --with-suggested-exts
139+
./bin/spc build --build-cli --build-micro --build-fpm ${{ matrix.extension }} --debug --with-suggested-libs --with-suggested-exts

config/env.ini

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ LD=${SPC_LINUX_DEFAULT_LD}
8989
SPC_DEFAULT_C_FLAGS="-fPIC -Os"
9090
SPC_DEFAULT_CXX_FLAGS="-fPIC -Os"
9191
; extra libs for building php executable, used in `make` command for building php (this value may changed by extension build process, space separated)
92-
SPC_EXTRA_LIBS=
92+
SPC_EXTRA_LIBS=""
9393
; upx executable path
9494
UPX_EXEC=${PKG_ROOT_PATH}/bin/upx
9595
; phpmicro patches, for more info, see: https://github.com/easysoft/phpmicro/tree/master/patches
@@ -119,7 +119,7 @@ SPC_CMD_VAR_PHP_MAKE_EXTRA_CFLAGS="-g -fstack-protector-strong -fno-ident -fPIE
119119
; EXTRA_LIBS for `make` php
120120
SPC_CMD_VAR_PHP_MAKE_EXTRA_LIBS="-ldl -lpthread -lm"
121121
; EXTRA_LDFLAGS for `make` php, can use -release to set a soname for libphp.so
122-
SPC_CMD_VAR_PHP_MAKE_EXTRA_LDFLAGS=""
122+
SPC_CMD_VAR_PHP_MAKE_EXTRA_LDFLAGS="-L${BUILD_LIB_PATH}"
123123
; EXTRA_LDFLAGS_PROGRAM for `make` php
124124
SPC_CMD_VAR_PHP_MAKE_EXTRA_LDFLAGS_PROGRAM="-all-static -Wl,-O1 -pie"
125125

@@ -134,7 +134,7 @@ CXX=clang++
134134
SPC_DEFAULT_C_FLAGS="--target=${MAC_ARCH}-apple-darwin -Os"
135135
SPC_DEFAULT_CXX_FLAGS="--target=${MAC_ARCH}-apple-darwin -Os"
136136
; extra libs for building php executable, used in `make` command for building php (this value may changed by extension build process, space separated)
137-
SPC_EXTRA_LIBS=
137+
SPC_EXTRA_LIBS=""
138138
; phpmicro patches, for more info, see: https://github.com/easysoft/phpmicro/tree/master/patches
139139
SPC_MICRO_PATCHES=cli_checks,macos_iconv
140140

config/lib.json

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@
3535
},
3636
"brotli": {
3737
"source": "brotli",
38-
"static-libs-unix": [
39-
"libbrotlidec.a",
40-
"libbrotlienc.a",
41-
"libbrotlicommon.a"
38+
"pkg-configs": [
39+
"libbrotlicommon",
40+
"libbrotlidec",
41+
"libbrotlienc"
4242
],
4343
"static-libs-windows": [
4444
"brotlicommon.lib",
@@ -89,7 +89,8 @@
8989
"nghttp3",
9090
"ngtcp2",
9191
"zstd",
92-
"libcares"
92+
"libcares",
93+
"ldap"
9394
],
9495
"lib-suggests-windows": [
9596
"brotli",
@@ -201,11 +202,10 @@
201202
"icu": {
202203
"source": "icu",
203204
"cpp-library": true,
204-
"static-libs-unix": [
205-
"libicui18n.a",
206-
"libicuio.a",
207-
"libicuuc.a",
208-
"libicudata.a"
205+
"pkg-configs": [
206+
"icu-uc",
207+
"icu-i18n",
208+
"icu-io"
209209
]
210210
},
211211
"icu-static-win": {
@@ -222,10 +222,10 @@
222222
},
223223
"imagemagick": {
224224
"source": "imagemagick",
225-
"static-libs-unix": [
226-
"libMagick++-7.Q16HDRI.a",
227-
"libMagickWand-7.Q16HDRI.a",
228-
"libMagickCore-7.Q16HDRI.a"
225+
"pkg-configs": [
226+
"Magick++-7.Q16HDRI",
227+
"MagickCore-7.Q16HDRI",
228+
"MagickWand-7.Q16HDRI"
229229
],
230230
"lib-depends": [
231231
"zlib",
@@ -268,9 +268,9 @@
268268
},
269269
"ldap": {
270270
"source": "ldap",
271-
"static-libs-unix": [
272-
"libldap.a",
273-
"liblber.a"
271+
"pkg-configs": [
272+
"ldap",
273+
"lber"
274274
],
275275
"lib-depends": [
276276
"openssl",
@@ -423,11 +423,11 @@
423423
},
424424
"libjxl": {
425425
"source": "libjxl",
426-
"static-libs-unix": [
427-
"libjxl.a",
428-
"libjxl_cms.a",
429-
"libjxl_threads.a",
430-
"libhwy.a"
426+
"pkg-configs": [
427+
"libjxl",
428+
"libjxl_cms",
429+
"libjxl_threads",
430+
"libhwy"
431431
],
432432
"lib-depends": [
433433
"brotli",
@@ -557,12 +557,12 @@
557557
},
558558
"libwebp": {
559559
"source": "libwebp",
560-
"static-libs-unix": [
561-
"libwebp.a",
562-
"libwebpdecoder.a",
563-
"libwebpdemux.a",
564-
"libwebpmux.a",
565-
"libsharpyuv.a"
560+
"pkg-configs": [
561+
"libwebp",
562+
"libwebpdecoder",
563+
"libwebpdemux",
564+
"libwebpmux",
565+
"libsharpyuv"
566566
],
567567
"static-libs-windows": [
568568
"libwebp.lib",
@@ -738,8 +738,8 @@
738738
"openssl": {
739739
"source": "openssl",
740740
"static-libs-unix": [
741-
"libssl.a",
742-
"libcrypto.a"
741+
"libcrypto.a",
742+
"libssl.a"
743743
],
744744
"static-libs-windows": [
745745
"libssl.lib",
@@ -754,10 +754,8 @@
754754
},
755755
"postgresql": {
756756
"source": "postgresql",
757-
"static-libs-unix": [
758-
"libpq.a",
759-
"libpgport.a",
760-
"libpgcommon.a"
757+
"pkg-configs": [
758+
"libpq"
761759
],
762760
"lib-depends": [
763761
"libiconv",

src/SPC/builder/BuilderBase.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,17 @@ public function setupLibs(): void
6666
// build all libs
6767
foreach ($this->libs as $lib) {
6868
$starttime = microtime(true);
69-
match ($lib->setup($this->getOption('rebuild', false))) {
69+
$status = $lib->setup($this->getOption('rebuild', false));
70+
match ($status) {
7071
LIB_STATUS_OK => logger()->info('lib [' . $lib::NAME . '] setup success, took ' . round(microtime(true) - $starttime, 2) . ' s'),
7172
LIB_STATUS_ALREADY => logger()->notice('lib [' . $lib::NAME . '] already built'),
7273
LIB_STATUS_BUILD_FAILED => logger()->error('lib [' . $lib::NAME . '] build failed'),
7374
LIB_STATUS_INSTALL_FAILED => logger()->error('lib [' . $lib::NAME . '] install failed'),
7475
default => logger()->warning('lib [' . $lib::NAME . '] build status unknown'),
7576
};
77+
if (in_array($status, [LIB_STATUS_BUILD_FAILED, LIB_STATUS_INSTALL_FAILED])) {
78+
throw new RuntimeException('Library [' . $lib::NAME . '] setup failed.');
79+
}
7680
}
7781
}
7882

src/SPC/builder/LibraryBase.php

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -182,22 +182,8 @@ public function tryInstall(array $lock, bool $force_install = false): int
182182
return LIB_STATUS_INSTALL_FAILED;
183183
}
184184
}
185-
foreach ($this->getStaticLibs() as $name) {
186-
if (!file_exists(BUILD_LIB_PATH . "/{$name}")) {
187-
$this->tryInstall($lock, true);
188-
return LIB_STATUS_OK;
189-
}
190-
}
191-
foreach ($this->getHeaders() as $name) {
192-
if (!file_exists(BUILD_INCLUDE_PATH . "/{$name}")) {
193-
$this->tryInstall($lock, true);
194-
return LIB_STATUS_OK;
195-
}
196-
}
197-
// pkg-config is treated specially. If it is pkg-config, check if the pkg-config binary exists
198-
if (static::NAME === 'pkg-config' && !file_exists(BUILD_ROOT_PATH . '/bin/pkg-config')) {
199-
$this->tryInstall($lock, true);
200-
return LIB_STATUS_OK;
185+
if (!$this->isLibraryInstalled()) {
186+
return $this->tryInstall($lock, true);
201187
}
202188
return LIB_STATUS_ALREADY;
203189
}
@@ -240,28 +226,8 @@ public function tryBuild(bool $force_build = false): int
240226
return LIB_STATUS_OK;
241227
}
242228

243-
// check if these libraries exist, if not, invoke compilation and return the result status
244-
foreach ($this->getStaticLibs() as $name) {
245-
if (!file_exists(BUILD_LIB_PATH . "/{$name}")) {
246-
$this->tryBuild(true);
247-
return LIB_STATUS_OK;
248-
}
249-
}
250-
// header files the same
251-
foreach ($this->getHeaders() as $name) {
252-
if (!file_exists(BUILD_INCLUDE_PATH . "/{$name}")) {
253-
$this->tryBuild(true);
254-
return LIB_STATUS_OK;
255-
}
256-
}
257-
// current library is package and binary file is not exists
258-
if (Config::getLib(static::NAME, 'type', 'lib') === 'package') {
259-
foreach ($this->getBinaryFiles() as $name) {
260-
if (!file_exists(BUILD_BIN_PATH . "/{$name}")) {
261-
$this->tryBuild(true);
262-
return LIB_STATUS_OK;
263-
}
264-
}
229+
if (!$this->isLibraryInstalled()) {
230+
return $this->tryBuild(true);
265231
}
266232
// if all the files exist at this point, skip the compilation process
267233
return LIB_STATUS_ALREADY;
@@ -350,7 +316,27 @@ abstract protected function build();
350316

351317
protected function install(): void
352318
{
353-
// do something after extracting pre-built files, default do nothing. overwrite this method to do something
319+
// replace placeholders if BUILD_ROOT_PATH/.spc-extract-placeholder.json exists
320+
$placeholder_file = BUILD_ROOT_PATH . '/.spc-extract-placeholder.json';
321+
if (!file_exists($placeholder_file)) {
322+
return;
323+
}
324+
$placeholder = json_decode(file_get_contents($placeholder_file), true);
325+
if (!is_array($placeholder)) {
326+
throw new RuntimeException('Invalid placeholder file: ' . $placeholder_file);
327+
}
328+
$placeholder = get_pack_placehoder();
329+
// replace placeholders in BUILD_ROOT_PATH
330+
foreach ($placeholder as $item) {
331+
$filepath = BUILD_ROOT_PATH . "/{$item}";
332+
FileSystem::replaceFileStr(
333+
$filepath,
334+
array_values($placeholder),
335+
array_keys($placeholder),
336+
);
337+
}
338+
// remove placeholder file
339+
unlink($placeholder_file);
354340
}
355341

356342
/**
@@ -397,4 +383,29 @@ protected function installLicense(): void
397383
}
398384
}
399385
}
386+
387+
protected function isLibraryInstalled(): bool
388+
{
389+
foreach (Config::getLib(static::NAME, 'static-libs', []) as $name) {
390+
if (!file_exists(BUILD_LIB_PATH . "/{$name}")) {
391+
return false;
392+
}
393+
}
394+
foreach (Config::getLib(static::NAME, 'headers', []) as $name) {
395+
if (!file_exists(BUILD_INCLUDE_PATH . "/{$name}")) {
396+
return false;
397+
}
398+
}
399+
foreach (Config::getLib(static::NAME, 'pkg-configs', []) as $name) {
400+
if (!file_exists(BUILD_LIB_PATH . "/pkgconfig/{$name}.pc")) {
401+
return false;
402+
}
403+
}
404+
foreach (Config::getLib(static::NAME, 'bin', []) as $name) {
405+
if (!file_exists(BUILD_BIN_PATH . "/{$name}")) {
406+
return false;
407+
}
408+
}
409+
return true;
410+
}
400411
}

src/SPC/builder/freebsd/BSDBuilder.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,6 @@ public function __construct(array $options = [])
6363
*/
6464
public function buildPHP(int $build_target = BUILD_TARGET_NONE): void
6565
{
66-
// ---------- Update extra-libs ----------
67-
$extra_libs = $this->getOption('extra-libs', '');
68-
// add libc++, some extensions or libraries need it (C++ cannot be linked statically)
69-
$extra_libs .= (empty($extra_libs) ? '' : ' ') . ($this->hasCpp() ? '-lc++ ' : '');
70-
if (!$this->getOption('bloat', false)) {
71-
$extra_libs .= (empty($extra_libs) ? '' : ' ') . implode(' ', $this->getAllStaticLibFiles());
72-
} else {
73-
logger()->info('bloat linking');
74-
$extra_libs .= (empty($extra_libs) ? '' : ' ') . implode(' ', array_map(fn ($x) => "-Wl,-force_load,{$x}", array_filter($this->getAllStaticLibFiles())));
75-
}
76-
$this->setOption('extra-libs', $extra_libs);
77-
7866
$this->emitPatchPoint('before-php-buildconf');
7967
SourcePatcher::patchBeforeBuildconf($this);
8068

src/SPC/builder/freebsd/library/curl.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,4 @@ class curl extends BSDLibraryBase
99
use \SPC\builder\unix\library\curl;
1010

1111
public const NAME = 'curl';
12-
13-
public function getStaticLibFiles(string $style = 'autoconf', bool $recursive = true, bool $include_self = true): string
14-
{
15-
$libs = parent::getStaticLibFiles($style, $recursive, $include_self);
16-
if ($this->builder->getLib('openssl')) {
17-
$this->builder->setOption('extra-libs', $this->builder->getOption('extra-libs') . ' /usr/lib/libpthread.a /usr/lib/libdl.a');
18-
}
19-
return $libs;
20-
}
2112
}

src/SPC/builder/linux/LinuxBuilder.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use SPC\store\FileSystem;
1212
use SPC\store\SourcePatcher;
1313
use SPC\util\GlobalEnvManager;
14+
use SPC\util\SPCConfigUtil;
1415

1516
class LinuxBuilder extends UnixBuilderBase
1617
{
@@ -56,17 +57,6 @@ public function __construct(array $options = [])
5657
*/
5758
public function buildPHP(int $build_target = BUILD_TARGET_NONE): void
5859
{
59-
// ---------- Update extra-libs ----------
60-
$extra_libs = getenv('SPC_EXTRA_LIBS') ?: '';
61-
// bloat means force-load all static libraries, even if they are not used
62-
if (!$this->getOption('bloat', false)) {
63-
$extra_libs .= (empty($extra_libs) ? '' : ' ') . implode(' ', $this->getAllStaticLibFiles());
64-
} else {
65-
$extra_libs .= (empty($extra_libs) ? '' : ' ') . implode(' ', array_map(fn ($x) => "-Xcompiler {$x}", array_filter($this->getAllStaticLibFiles())));
66-
}
67-
// add libstdc++, some extensions or libraries need it
68-
$extra_libs .= (empty($extra_libs) ? '' : ' ') . ($this->hasCpp() ? '-lstdc++ ' : '');
69-
f_putenv('SPC_EXTRA_LIBS=' . $extra_libs);
7060
$cflags = $this->arch_c_flags;
7161
f_putenv('CFLAGS=' . $cflags);
7262

@@ -308,9 +298,10 @@ protected function buildEmbed(): void
308298

309299
private function getMakeExtraVars(): array
310300
{
301+
$config = (new SPCConfigUtil($this, ['libs_only_deps' => true, 'absolute_libs' => true]))->config($this->ext_list, $this->lib_list, $this->getOption('with-suggested-exts'), $this->getOption('with-suggested-libs'));
311302
return [
312303
'EXTRA_CFLAGS' => getenv('SPC_CMD_VAR_PHP_MAKE_EXTRA_CFLAGS'),
313-
'EXTRA_LIBS' => getenv('SPC_EXTRA_LIBS') . ' ' . getenv('SPC_CMD_VAR_PHP_MAKE_EXTRA_LIBS'),
304+
'EXTRA_LIBS' => $config['libs'],
314305
'EXTRA_LDFLAGS' => getenv('SPC_CMD_VAR_PHP_MAKE_EXTRA_LDFLAGS'),
315306
'EXTRA_LDFLAGS_PROGRAM' => getenv('SPC_CMD_VAR_PHP_MAKE_EXTRA_LDFLAGS_PROGRAM'),
316307
];

src/SPC/builder/linux/library/curl.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,4 @@ class curl extends LinuxLibraryBase
99
use \SPC\builder\unix\library\curl;
1010

1111
public const NAME = 'curl';
12-
13-
public function getStaticLibFiles(string $style = 'autoconf', bool $recursive = true, bool $include_self = true): string
14-
{
15-
$libs = parent::getStaticLibFiles($style, $recursive, $include_self);
16-
if ($this->builder->getLib('openssl')) {
17-
$libs .= ' -ldl -lpthread';
18-
}
19-
return $libs;
20-
}
2112
}

src/SPC/builder/linux/library/icu.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ protected function build(): void
3838
->exec("make -j{$this->builder->concurrency}")
3939
->exec('make install');
4040

41-
$this->patchPkgconfPrefix(['icu-i18n.pc', 'icu-io.pc', 'icu-uc.pc'], PKGCONF_PATCH_PREFIX);
41+
$this->patchPkgconfPrefix(patch_option: PKGCONF_PATCH_PREFIX);
4242
FileSystem::removeDir(BUILD_LIB_PATH . '/icu');
4343
}
4444
}

0 commit comments

Comments
 (0)