Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/StaticPHP/DI/ApplicationContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,25 @@ public static function has(string $id): bool
return self::getContainer()->has($id);
}

/**
* Resolve $id, returning null if it can't be constructed.
* PHP-DI's has() returns true for any autowirable class even when get()
* would throw on missing scalar args — for "is this resolvable right now"
* semantics use this.
*
* @template T
* @param class-string<T> $id
* @return null|T
*/
public static function tryGet(string $id): mixed
{
try {
return self::getContainer()->get($id);
} catch (\Throwable) {
return null;
}
}

/**
* Set a service in the container.
* Use sparingly - prefer configuration-based definitions.
Expand Down
16 changes: 11 additions & 5 deletions src/StaticPHP/Package/PackageBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use StaticPHP\Exception\WrongUsageException;
use StaticPHP\Runtime\Shell\Shell;
use StaticPHP\Runtime\SystemTarget;
use StaticPHP\Toolchain\Interface\ToolchainInterface;
use StaticPHP\Toolchain\ZigToolchain;
use StaticPHP\Util\FileSystem;
use StaticPHP\Util\GlobalPathTrait;
use StaticPHP\Util\InteractiveTerm;
Expand Down Expand Up @@ -178,14 +180,15 @@ public function extractDebugInfo(string $binary_path): string
if (SystemTarget::getTargetOS() === 'Darwin') {
shell()->exec("dsymutil -f {$binary_path} -o {$debug_file}");
} elseif (SystemTarget::getTargetOS() === 'Linux') {
$objcopy = getenv('OBJCOPY') ?: 'objcopy';
if ($eu_strip = LinuxUtil::findCommand('eu-strip')) {
shell()
->exec("{$eu_strip} -f {$debug_file} {$binary_path}")
->exec("objcopy --add-gnu-debuglink={$debug_file} {$binary_path}");
->exec("{$objcopy} --add-gnu-debuglink={$debug_file} {$binary_path}");
} else {
shell()
->exec("objcopy --only-keep-debug {$binary_path} {$debug_file}")
->exec("objcopy --add-gnu-debuglink={$debug_file} {$binary_path}");
->exec("{$objcopy} --only-keep-debug {$binary_path} {$debug_file}")
->exec("{$objcopy} --add-gnu-debuglink={$debug_file} {$binary_path}");
}
} else {
logger()->debug('extractDebugInfo is only supported on Linux and macOS');
Expand All @@ -199,9 +202,12 @@ public function extractDebugInfo(string $binary_path): string
*/
public function stripBinary(string $binary_path): void
{
$strip = ApplicationContext::tryGet(ToolchainInterface::class) instanceof ZigToolchain
? PKG_ROOT_PATH . '/llvm-tools/bin/llvm-strip'
: 'strip';
shell()->exec(match (SystemTarget::getTargetOS()) {
'Darwin' => "strip -S {$binary_path}",
'Linux' => "strip --strip-unneeded {$binary_path}",
'Darwin' => "{$strip} -S {$binary_path}",
'Linux' => "{$strip} --strip-unneeded {$binary_path}",
'Windows' => 'echo "Skip strip on Windows"', // Windows strip is not available for now
default => throw new SPCInternalException('stripBinary is only supported on Linux and macOS'),
});
Expand Down
Loading