Skip to content

Commit 5c1194e

Browse files
committed
fix custompackage handling
1 parent c330d02 commit 5c1194e

File tree

7 files changed

+29
-43
lines changed

7 files changed

+29
-43
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/unix/UnixBuilderBase.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ protected function patchPhpScripts(): void
266266

267267
protected function buildFrankenphp(): void
268268
{
269-
GlobalEnvManager::addPathIfNotExists(GoXcaddy::getEnvironment()['PATH']);
269+
GlobalEnvManager::addPathIfNotExists(GoXcaddy::getPath());
270270
$nobrotli = $this->getLib('brotli') === null ? ',nobrotli' : '';
271271
$nowatcher = $this->getLib('watcher') === null ? ',nowatcher' : '';
272272
$xcaddyModules = getenv('SPC_CMD_VAR_FRANKENPHP_XCADDY_MODULES');
@@ -310,7 +310,7 @@ protected function buildFrankenphp(): void
310310
$cflags .= ' -Wno-error=missing-profile';
311311
$libs .= ' -lgcov';
312312
}
313-
$env = [
313+
$env = [...[
314314
'CGO_ENABLED' => '1',
315315
'CGO_CFLAGS' => clean_spaces($cflags),
316316
'CGO_LDFLAGS' => "{$this->arch_ld_flags} {$staticFlags} {$config['ldflags']} {$libs}",
@@ -320,12 +320,7 @@ protected function buildFrankenphp(): void
320320
"{$frankenPhpVersion} PHP {$libphpVersion} Caddy'\\\" " .
321321
"-tags={$muslTags}nobadger,nomysql,nopgx{$nobrotli}{$nowatcher}",
322322
'LD_LIBRARY_PATH' => BUILD_LIB_PATH,
323-
];
324-
foreach (GoXcaddy::getEnvironment() as $key => $value) {
325-
if ($key !== 'PATH') {
326-
$env[$key] = $value;
327-
}
328-
}
323+
], ...GoXcaddy::getEnvironment()];
329324
shell()->cd(BUILD_BIN_PATH)
330325
->setEnv($env)
331326
->exec("xcaddy build --output frankenphp {$xcaddyModules}");

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
}

src/SPC/store/pkg/Zig.php

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -129,23 +129,10 @@ public function extract(string $name): void
129129

130130
public static function getEnvironment(): array
131131
{
132-
$arch = arch2gnu(php_uname('m'));
133-
$os = match (PHP_OS_FAMILY) {
134-
'Windows' => 'win',
135-
'Darwin' => 'macos',
136-
'BSD' => 'freebsd',
137-
default => 'linux',
138-
};
139-
140-
$packageName = "zig-{$arch}-{$os}";
141-
$path = PKG_ROOT_PATH . "/{$packageName}";
142-
143-
return [
144-
'PATH' => $path,
145-
];
132+
return [];
146133
}
147134

148-
private static function getPath(): string
135+
public static function getPath(): ?string
149136
{
150137
return PKG_ROOT_PATH . '/zig';
151138
}

src/SPC/toolchain/ZigToolchain.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ public function initEnv(): void
4242

4343
public function afterInit(): void
4444
{
45-
if (!is_dir(Zig::getEnvironment()['PATH'])) {
45+
if (!Zig::isInstalled()) {
4646
throw new EnvironmentException('You are building with zig, but zig is not installed, please install zig first. (You can use `doctor` command to install it)');
4747
}
48-
GlobalEnvManager::addPathIfNotExists(Zig::getEnvironment()['PATH']);
48+
GlobalEnvManager::addPathIfNotExists(Zig::getPath());
4949
f_passthru('ulimit -n 2048'); // zig opens extra file descriptors, so when a lot of extensions are built statically, 1024 is not enough
5050
$cflags = getenv('SPC_DEFAULT_C_FLAGS') ?: '';
5151
$cxxflags = getenv('SPC_DEFAULT_CXX_FLAGS') ?: '';

0 commit comments

Comments
 (0)