Skip to content

Commit b142610

Browse files
committed
move to systemutil
1 parent 9a3a536 commit b142610

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

config/env.ini

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,6 @@ SPC_CMD_VAR_PHP_MAKE_EXTRA_LDFLAGS=""
108108
; EXTRA_LDFLAGS_PROGRAM for `make` php
109109
SPC_CMD_VAR_PHP_MAKE_EXTRA_LDFLAGS_PROGRAM="-all-static -pie"
110110

111-
# Zig
112-
ZIG_SHARED_EXTENSION_EXTRA_OBJECTS="/usr/lib/gcc/x86_64-redhat-linux/14/crtbeginS.o /usr/lib/gcc/x86_64-redhat-linux/14/crtendS.o"
113-
114111
[macos]
115112
; compiler environments
116113
CC=clang

src/SPC/builder/Extension.php

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

55
namespace SPC\builder;
66

7+
use SPC\builder\linux\SystemUtil;
78
use SPC\exception\FileSystemException;
89
use SPC\exception\RuntimeException;
910
use SPC\exception\WrongUsageException;
@@ -215,13 +216,13 @@ public function patchBeforeSharedConfigure(): bool
215216
*/
216217
public function patchBeforeSharedMake(): bool
217218
{
218-
if (!str_contains(getenv('CC'), 'zig')) {
219+
$extra = SystemUtil::getExtraRuntimeObjects();
220+
if (!$extra) {
219221
return false;
220222
}
221-
$extra = getenv('ZIG_SHARED_EXTENSION_EXTRA_OBJECTS');
222223
FileSystem::replaceFileRegex(
223224
$this->source_dir . '/Makefile',
224-
"/^(shared_objects_{$this->getName()}\s*=.*)$/m",
225+
"/^(shared_objects_{$this->getName()}\\s*=.*)$/m",
225226
"$1 {$extra}",
226227
);
227228
return true;

src/SPC/builder/linux/SystemUtil.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ class SystemUtil
1313

1414
public static ?string $libc_version = null;
1515

16+
private static ?string $extra_runtime_objects = null;
17+
1618
/** @noinspection PhpMissingBreakStatementInspection */
1719
public static function getOSRelease(): array
1820
{
@@ -226,4 +228,38 @@ public static function getLibcVersionIfExists(): ?string
226228
}
227229
return null;
228230
}
231+
232+
public static function getExtraRuntimeObjects(): string
233+
{
234+
$cc = getenv('CC');
235+
if (!$cc || !str_contains($cc, 'zig')) {
236+
return '';
237+
}
238+
239+
if (self::$extra_runtime_objects !== null) {
240+
return self::$extra_runtime_objects;
241+
}
242+
243+
$paths = ['/usr/lib/gcc', '/usr/local/lib/gcc'];
244+
$objects = ['crtbeginS.o', 'crtendS.o'];
245+
$found = [];
246+
247+
foreach ($objects as $obj) {
248+
$located = null;
249+
foreach ($paths as $base) {
250+
$output = shell_exec("find {$base} -name {$obj} -print -quit 2>/dev/null");
251+
$line = trim((string) $output);
252+
if ($line !== '') {
253+
$located = $line;
254+
break;
255+
}
256+
}
257+
if ($located) {
258+
$found[] = escapeshellarg($located);
259+
}
260+
}
261+
262+
self::$extra_runtime_objects = implode(' ', $found);
263+
return implode(' ', $found);
264+
}
229265
}

0 commit comments

Comments
 (0)