Skip to content

Commit 79ab649

Browse files
authored
fix CustomPackage handling (#897)
2 parents 3198cc4 + 191d345 commit 79ab649

File tree

12 files changed

+47
-59
lines changed

12 files changed

+47
-59
lines changed

src/SPC/builder/BuilderBase.php

Lines changed: 2 additions & 2 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\store\LockFile;
13+
use SPC\store\pkg\GoXcaddy;
1314
use SPC\store\SourceManager;
1415
use SPC\store\SourcePatcher;
1516
use SPC\util\AttributeMapper;
@@ -507,8 +508,7 @@ public function checkBeforeBuildPHP(int $rule): void
507508
throw new WrongUsageException('FrankenPHP SAPI is only available on Linux and macOS!');
508509
}
509510
// frankenphp needs package go-xcaddy installed
510-
$pkg_dir = PKG_ROOT_PATH . '/go-xcaddy-' . arch2gnu(php_uname('m')) . '-' . osfamily2shortname();
511-
if (!file_exists("{$pkg_dir}/bin/go") || !file_exists("{$pkg_dir}/bin/xcaddy")) {
511+
if (!GoXcaddy::isInstalled()) {
512512
global $argv;
513513
throw new WrongUsageException("FrankenPHP SAPI requires the go-xcaddy package, please install it first: {$argv[0]} install-pkg go-xcaddy");
514514
}

src/SPC/builder/extension/dba.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class dba extends Extension
1212
{
1313
public function getUnixConfigureArg(bool $shared = false): string
1414
{
15-
$qdbm = $this->builder->getLib('qdbm') ? (' --with-qdbm=' . ($shared ? 'shared,' : '') . BUILD_ROOT_PATH) : '';
15+
$qdbm = $this->builder->getLib('qdbm') ? (' --with-qdbm=' . BUILD_ROOT_PATH) : '';
1616
return '--enable-dba' . ($shared ? '=shared' : '') . $qdbm;
1717
}
1818

src/SPC/builder/linux/LinuxBuilder.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,16 +147,15 @@ public function buildPHP(int $build_target = BUILD_TARGET_NONE): void
147147
}
148148
$this->buildEmbed();
149149
}
150-
// build dynamic extensions if needed, must happen before building FrankenPHP to make sure we export all necessary, undefined symbols
150+
if ($enableFrankenphp) {
151+
logger()->info('building frankenphp');
152+
$this->buildFrankenphp();
153+
}
151154
$shared_extensions = array_map('trim', array_filter(explode(',', $this->getOption('build-shared'))));
152155
if (!empty($shared_extensions)) {
153156
logger()->info('Building shared extensions ...');
154157
$this->buildSharedExts();
155158
}
156-
if ($enableFrankenphp) {
157-
logger()->info('building frankenphp');
158-
$this->buildFrankenphp();
159-
}
160159
}
161160

162161
public function testPHP(int $build_target = BUILD_TARGET_NONE)
@@ -326,7 +325,7 @@ protected function buildEmbed(): void
326325
$target = "{$libDir}/{$realLibName}";
327326
if (file_exists($target)) {
328327
[, $output] = shell()->execWithResult('readelf -d ' . escapeshellarg($target));
329-
$output = join("\n", $output);
328+
$output = implode("\n", $output);
330329
if (preg_match('/SONAME.*\[(.+)]/', $output, $sonameMatch)) {
331330
$currentSoname = $sonameMatch[1];
332331
if ($currentSoname !== basename($target)) {

src/SPC/builder/macos/MacOSBuilder.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,15 +161,15 @@ public function buildPHP(int $build_target = BUILD_TARGET_NONE): void
161161
}
162162
$this->buildEmbed();
163163
}
164+
if ($enableFrankenphp) {
165+
logger()->info('building frankenphp');
166+
$this->buildFrankenphp();
167+
}
164168
$shared_extensions = array_map('trim', array_filter(explode(',', $this->getOption('build-shared'))));
165169
if (!empty($shared_extensions)) {
166170
logger()->info('Building shared extensions ...');
167171
$this->buildSharedExts();
168172
}
169-
if ($enableFrankenphp) {
170-
logger()->info('building frankenphp');
171-
$this->buildFrankenphp();
172-
}
173173
}
174174

175175
public function testPHP(int $build_target = BUILD_TARGET_NONE)

src/SPC/builder/unix/UnixBuilderBase.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ protected function patchPhpScripts(): void
267267

268268
protected function buildFrankenphp(): void
269269
{
270-
GlobalEnvManager::addPathIfNotExists(GoXcaddy::getEnvironment()['PATH']);
270+
GlobalEnvManager::addPathIfNotExists(GoXcaddy::getPath());
271271
$nobrotli = $this->getLib('brotli') === null ? ',nobrotli' : '';
272272
$nowatcher = $this->getLib('watcher') === null ? ',nowatcher' : '';
273273
$xcaddyModules = getenv('SPC_CMD_VAR_FRANKENPHP_XCADDY_MODULES');
@@ -311,22 +311,17 @@ protected function buildFrankenphp(): void
311311
$cflags .= ' -Wno-error=missing-profile';
312312
$libs .= ' -lgcov';
313313
}
314-
$env = [
314+
$env = [...[
315315
'CGO_ENABLED' => '1',
316316
'CGO_CFLAGS' => clean_spaces($cflags),
317317
'CGO_LDFLAGS' => "{$this->arch_ld_flags} {$staticFlags} {$config['ldflags']} {$libs}",
318318
'XCADDY_GO_BUILD_FLAGS' => '-buildmode=pie ' .
319319
'-ldflags \"-linkmode=external ' . $extLdFlags . ' ' . $debugFlags .
320320
'-X \'github.com/caddyserver/caddy/v2.CustomVersion=FrankenPHP ' .
321-
"{$frankenPhpVersion} PHP {$libphpVersion} Caddy'\\\" " .
321+
"v{$frankenPhpVersion} PHP {$libphpVersion} Caddy'\\\" " .
322322
"-tags={$muslTags}nobadger,nomysql,nopgx{$nobrotli}{$nowatcher}",
323323
'LD_LIBRARY_PATH' => BUILD_LIB_PATH,
324-
];
325-
foreach (GoXcaddy::getEnvironment() as $key => $value) {
326-
if ($key !== 'PATH') {
327-
$env[$key] = $value;
328-
}
329-
}
324+
], ...GoXcaddy::getEnvironment()];
330325
shell()->cd(BUILD_BIN_PATH)
331326
->setEnv($env)
332327
->exec("xcaddy build --output frankenphp {$xcaddyModules}");

src/SPC/command/CraftCommand.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
namespace SPC\command;
66

77
use SPC\exception\ValidationException;
8+
use SPC\store\pkg\GoXcaddy;
9+
use SPC\store\pkg\Zig;
810
use SPC\toolchain\ToolchainManager;
911
use SPC\toolchain\ZigToolchain;
1012
use SPC\util\ConfigValidator;
@@ -63,15 +65,15 @@ public function handle(): int
6365
}
6466
}
6567
// install go and xcaddy for frankenphp
66-
if (in_array('frankenphp', $craft['sapi'])) {
68+
if (in_array('frankenphp', $craft['sapi']) && !GoXcaddy::isInstalled()) {
6769
$retcode = $this->runCommand('install-pkg', 'go-xcaddy');
6870
if ($retcode !== 0) {
6971
$this->output->writeln('<error>craft go-xcaddy failed</error>');
7072
return static::FAILURE;
7173
}
7274
}
7375
// install zig if requested
74-
if (ToolchainManager::getToolchainClass() === ZigToolchain::class) {
76+
if (ToolchainManager::getToolchainClass() === ZigToolchain::class && !Zig::isInstalled()) {
7577
$retcode = $this->runCommand('install-pkg', 'zig');
7678
if ($retcode !== 0) {
7779
$this->output->writeln('<error>craft zig failed</error>');

src/SPC/doctor/item/LinuxToolCheckList.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ class LinuxToolCheckList
5353
'base-devel' => 'automake',
5454
'gettext-devel' => 'gettextize',
5555
'gettext-dev' => 'gettextize',
56-
'perl-IPC-Cmd' => '/usr/share/doc/perl-IPC-Cmd',
56+
'perl-IPC-Cmd' => '/usr/share/perl5/vendor_perl/IPC/Cmd.pm',
57+
'perl-Time-Piece' => '/usr/lib64/perl5/Time/Piece.pm',
5758
];
5859

5960
/** @noinspection PhpUnused */
@@ -65,7 +66,7 @@ public function checkCliTools(): ?CheckResult
6566
$required = match ($distro['dist']) {
6667
'alpine' => self::TOOLS_ALPINE,
6768
'redhat' => self::TOOLS_RHEL,
68-
'centos' => array_merge(self::TOOLS_RHEL, ['perl-IPC-Cmd']),
69+
'centos' => array_merge(self::TOOLS_RHEL, ['perl-IPC-Cmd', 'perl-Time-Piece']),
6970
'arch' => self::TOOLS_ARCH,
7071
default => self::TOOLS_DEBIAN,
7172
};

src/SPC/store/FileSystem.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ public static function convertPath(string $path): string
274274
public static function convertWinPathToMinGW(string $path): string
275275
{
276276
if (preg_match('/^[A-Za-z]:/', $path)) {
277-
$path = '/' . strtolower(substr($path, 0, 1)) . '/' . str_replace('\\', '/', substr($path, 2));
277+
$path = '/' . strtolower($path[0]) . '/' . str_replace('\\', '/', substr($path, 2));
278278
}
279279
return $path;
280280
}
@@ -314,8 +314,13 @@ public static function scanDirFiles(string $dir, bool $recursive = true, bool|st
314314
$sub_file = self::convertPath($dir . '/' . $v);
315315
if (is_dir($sub_file) && $recursive) {
316316
# 如果是 目录 且 递推 , 则递推添加下级文件
317-
$list = array_merge($list, self::scanDirFiles($sub_file, $recursive, $relative));
318-
} elseif (is_file($sub_file) || is_dir($sub_file) && !$recursive && $include_dir) {
317+
$sub_list = self::scanDirFiles($sub_file, $recursive, $relative);
318+
if (is_array($sub_list)) {
319+
foreach ($sub_list as $item) {
320+
$list[] = $item;
321+
}
322+
}
323+
} elseif (is_file($sub_file) || (is_dir($sub_file) && !$recursive && $include_dir)) {
319324
# 如果是 文件 或 (是 目录 且 不递推 且 包含目录)
320325
if (is_string($relative) && mb_strpos($sub_file, $relative) === 0) {
321326
$list[] = ltrim(mb_substr($sub_file, mb_strlen($relative)), '/\\');
@@ -440,7 +445,7 @@ public static function createDir(string $path): void
440445
public static function writeFile(string $path, mixed $content, ...$args): bool|int|string
441446
{
442447
$dir = pathinfo(self::convertPath($path), PATHINFO_DIRNAME);
443-
if (!is_dir($dir) && !mkdir($dir, 0755, true)) {
448+
if (!is_dir($dir) && !mkdir($dir, 0755, true) && !is_dir($dir)) {
444449
throw new FileSystemException('Write file failed, cannot create parent directory: ' . $dir);
445450
}
446451
return file_put_contents($path, $content, ...$args);

src/SPC/store/pkg/CustomPackage.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,14 @@ abstract public function fetch(string $name, bool $force = false, ?array $config
3030

3131
/**
3232
* Get the environment variables this package needs to be usable.
33-
* PATH needs to be appended, rather than replaced.
3433
*/
3534
abstract public static function getEnvironment(): array;
3635

36+
/**
37+
* Get the PATH required to use this package.
38+
*/
39+
abstract public static function getPath(): ?string;
40+
3741
abstract public static function isInstalled(): bool;
3842

3943
/**

src/SPC/store/pkg/GoXcaddy.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,22 +90,17 @@ public function extract(string $name): void
9090

9191
public static function getEnvironment(): array
9292
{
93-
$arch = arch2gnu(php_uname('m'));
94-
$os = match (PHP_OS_FAMILY) {
95-
'Windows' => 'win',
96-
'Darwin' => 'macos',
97-
'BSD' => 'freebsd',
98-
default => 'linux',
99-
};
100-
101-
$packageName = "go-xcaddy-{$arch}-{$os}";
93+
$packageName = 'go-xcaddy';
10294
$pkgroot = PKG_ROOT_PATH;
103-
10495
return [
105-
'PATH' => "{$pkgroot}/{$packageName}/bin",
10696
'GOROOT' => "{$pkgroot}/{$packageName}",
10797
'GOBIN' => "{$pkgroot}/{$packageName}/bin",
10898
'GOPATH' => "{$pkgroot}/go",
10999
];
110100
}
101+
102+
public static function getPath(): ?string
103+
{
104+
return PKG_ROOT_PATH . '/go-xcaddy/bin';
105+
}
111106
}

0 commit comments

Comments
 (0)