mirror of
https://github.com/crazywhalecc/static-php-cli.git
synced 2026-07-05 15:55:39 +08:00
Add DirDiff utility and enhance package build process
- Introduced DirDiff class for tracking directory file changes. - Updated ConsoleApplication to use addCommand for build targets. - Enhanced PackageBuilder with methods for deploying binaries and extracting debug info. - Improved package installation logic to support shared extensions. - Added readline extension with patching for static builds.
This commit is contained in:
@@ -35,10 +35,11 @@ class ConsoleApplication extends Application
|
||||
// only add target that contains artifact.source
|
||||
if ($package->hasStage('build')) {
|
||||
logger()->debug("Registering build target command for package: {$name}");
|
||||
$this->add(new BuildTargetCommand($name));
|
||||
$this->addCommand(new BuildTargetCommand($name));
|
||||
}
|
||||
}
|
||||
|
||||
// add core commands
|
||||
$this->addCommands([
|
||||
new DownloadCommand(),
|
||||
new DoctorCommand(),
|
||||
|
||||
@@ -9,9 +9,11 @@ use StaticPHP\DI\ApplicationContext;
|
||||
use StaticPHP\Exception\SPCInternalException;
|
||||
use StaticPHP\Exception\WrongUsageException;
|
||||
use StaticPHP\Runtime\Shell\Shell;
|
||||
use StaticPHP\Runtime\SystemTarget;
|
||||
use StaticPHP\Util\FileSystem;
|
||||
use StaticPHP\Util\GlobalEnvManager;
|
||||
use StaticPHP\Util\InteractiveTerm;
|
||||
use StaticPHP\Util\System\LinuxUtil;
|
||||
|
||||
class PackageBuilder
|
||||
{
|
||||
@@ -85,6 +87,92 @@ class PackageBuilder
|
||||
return $this->options[$key] ?? $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deploy the binary file from src to dst.
|
||||
*/
|
||||
public function deployBinary(string $src, string $dst, bool $executable = true): string
|
||||
{
|
||||
logger()->debug("Deploying binary from {$src} to {$dst}");
|
||||
|
||||
// file must exists
|
||||
if (!file_exists($src)) {
|
||||
throw new SPCInternalException("Deploy failed. Cannot find file: {$src}");
|
||||
}
|
||||
// dst dir must exists
|
||||
FileSystem::createDir(dirname($dst));
|
||||
|
||||
// ignore copy to self
|
||||
if (realpath($src) !== realpath($dst)) {
|
||||
shell()->exec('cp ' . escapeshellarg($src) . ' ' . escapeshellarg($dst));
|
||||
}
|
||||
|
||||
// file exist
|
||||
if (!file_exists($dst)) {
|
||||
throw new SPCInternalException("Deploy failed. Cannot find file after copy: {$dst}");
|
||||
}
|
||||
|
||||
// extract debug info
|
||||
$this->extractDebugInfo($dst);
|
||||
|
||||
// strip
|
||||
if (!$this->getOption('no-strip')) {
|
||||
$this->stripBinary($dst);
|
||||
}
|
||||
|
||||
// UPX for linux
|
||||
$upx_option = $this->getOption('with-upx-pack');
|
||||
if ($upx_option && SystemTarget::getTargetOS() === 'Linux' && $executable) {
|
||||
if ($this->getOption('no-strip')) {
|
||||
logger()->warning('UPX compression is not recommended when --no-strip is enabled.');
|
||||
}
|
||||
logger()->info("Compressing {$dst} with UPX");
|
||||
shell()->exec(getenv('UPX_EXEC') . " --best {$dst}");
|
||||
}
|
||||
|
||||
return $dst;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract debug information from binary file.
|
||||
*
|
||||
* @param string $binary_path the path to the binary file, including executables, shared libraries, etc
|
||||
*/
|
||||
public function extractDebugInfo(string $binary_path): string
|
||||
{
|
||||
$target_dir = BUILD_ROOT_PATH . '/debug';
|
||||
FileSystem::createDir($target_dir);
|
||||
$basename = basename($binary_path);
|
||||
$debug_file = "{$target_dir}/{$basename}" . (SystemTarget::getTargetOS() === 'Darwin' ? '.dwarf' : '.debug');
|
||||
if (SystemTarget::getTargetOS() === 'Darwin') {
|
||||
shell()->exec("dsymutil -f {$binary_path} -o {$debug_file}");
|
||||
} elseif (SystemTarget::getTargetOS() === 'Linux') {
|
||||
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}");
|
||||
} else {
|
||||
shell()
|
||||
->exec("objcopy --only-keep-debug {$binary_path} {$debug_file}")
|
||||
->exec("objcopy --add-gnu-debuglink={$debug_file} {$binary_path}");
|
||||
}
|
||||
} else {
|
||||
throw new SPCInternalException('extractDebugInfo is only supported on Linux and macOS');
|
||||
}
|
||||
return $debug_file;
|
||||
}
|
||||
|
||||
/**
|
||||
* Strip unneeded symbols from binary file.
|
||||
*/
|
||||
public function stripBinary(string $binary_path): void
|
||||
{
|
||||
shell()->exec(match (SystemTarget::getTargetOS()) {
|
||||
'Darwin' => "strip -S {$binary_path}",
|
||||
'Linux' => "strip --strip-unneeded {$binary_path}",
|
||||
default => throw new SPCInternalException('stripBinary is only supported on Linux and macOS'),
|
||||
});
|
||||
}
|
||||
|
||||
private function installLicense(Package $package, array $license): void
|
||||
{
|
||||
$dir = BUILD_ROOT_PATH . '/source-licenses/' . $package->getName();
|
||||
|
||||
@@ -195,15 +195,20 @@ class PackageInstaller
|
||||
|
||||
/**
|
||||
* Get all resolved packages.
|
||||
* You can filter by package type class if needed.
|
||||
*
|
||||
* @return array<string, Package>
|
||||
* @template T
|
||||
* @param class-string<T> $package_type Filter by package type
|
||||
* @return array<T>
|
||||
*/
|
||||
public function getResolvedPackages(): array
|
||||
public function getResolvedPackages(mixed $package_type = Package::class): array
|
||||
{
|
||||
return $this->packages;
|
||||
return array_filter($this->packages, function (Package $pkg) use ($package_type): bool {
|
||||
return $pkg instanceof $package_type;
|
||||
});
|
||||
}
|
||||
|
||||
public function isPackageBeingResolved(string $package_name): bool
|
||||
public function isPackageResolved(string $package_name): bool
|
||||
{
|
||||
return isset($this->packages[$package_name]);
|
||||
}
|
||||
|
||||
@@ -232,7 +232,7 @@ class PackageLoader
|
||||
$installer = ApplicationContext::get(PackageInstaller::class);
|
||||
$stages = self::$before_stages[$package_name][$stage] ?? [];
|
||||
foreach ($stages as [$callback, $only_when_package_resolved]) {
|
||||
if ($only_when_package_resolved !== null && !$installer->isPackageBeingResolved($only_when_package_resolved)) {
|
||||
if ($only_when_package_resolved !== null && !$installer->isPackageResolved($only_when_package_resolved)) {
|
||||
continue;
|
||||
}
|
||||
yield $callback;
|
||||
@@ -246,7 +246,7 @@ class PackageLoader
|
||||
$stages = self::$after_stage[$package_name][$stage] ?? [];
|
||||
$result = [];
|
||||
foreach ($stages as [$callback, $only_when_package_resolved]) {
|
||||
if ($only_when_package_resolved !== null && !$installer->isPackageBeingResolved($only_when_package_resolved)) {
|
||||
if ($only_when_package_resolved !== null && !$installer->isPackageResolved($only_when_package_resolved)) {
|
||||
continue;
|
||||
}
|
||||
$result[] = $callback;
|
||||
|
||||
@@ -107,4 +107,9 @@ class PhpExtensionPackage extends Package
|
||||
{
|
||||
return $this->build_with_php;
|
||||
}
|
||||
|
||||
public function buildSharedExtension(): void
|
||||
{
|
||||
// TODO: build common shared extensions code here...
|
||||
}
|
||||
}
|
||||
|
||||
@@ -252,6 +252,12 @@ class Registry
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return full path, resolving relative paths against a base path.
|
||||
*
|
||||
* @param string $path Input path (relative or absolute)
|
||||
* @param string $relative_path_base Base path for relative paths
|
||||
*/
|
||||
private static function fullpath(string $path, string $relative_path_base): string
|
||||
{
|
||||
if (FileSystem::isRelativePath($path)) {
|
||||
|
||||
95
src/StaticPHP/Util/DirDiff.php
Normal file
95
src/StaticPHP/Util/DirDiff.php
Normal file
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace StaticPHP\Util;
|
||||
|
||||
/**
|
||||
* A util class to diff directory file increments.
|
||||
*/
|
||||
class DirDiff
|
||||
{
|
||||
protected array $before = [];
|
||||
|
||||
protected array $before_file_hashes = [];
|
||||
|
||||
public function __construct(protected string $dir, protected bool $track_content_changes = false)
|
||||
{
|
||||
$this->reset();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the baseline to current state.
|
||||
*/
|
||||
public function reset(): void
|
||||
{
|
||||
$this->before = FileSystem::scanDirFiles($this->dir, relative: true) ?: [];
|
||||
|
||||
if ($this->track_content_changes) {
|
||||
$this->before_file_hashes = [];
|
||||
foreach ($this->before as $file) {
|
||||
$this->before_file_hashes[$file] = md5_file($this->dir . DIRECTORY_SEPARATOR . $file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of incremented files.
|
||||
*
|
||||
* @param bool $relative Return relative paths or absolute paths
|
||||
* @return array<string> List of incremented files
|
||||
*/
|
||||
public function getIncrementFiles(bool $relative = false): array
|
||||
{
|
||||
$after = FileSystem::scanDirFiles($this->dir, relative: true) ?: [];
|
||||
$diff = array_diff($after, $this->before);
|
||||
if ($relative) {
|
||||
return $diff;
|
||||
}
|
||||
return array_map(fn ($f) => $this->dir . DIRECTORY_SEPARATOR . $f, $diff);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of changed files (including new files).
|
||||
*
|
||||
* @param bool $relative Return relative paths or absolute paths
|
||||
* @param bool $include_new_files Include new files as changed files
|
||||
* @return array<string> List of changed files
|
||||
*/
|
||||
public function getChangedFiles(bool $relative = false, bool $include_new_files = true): array
|
||||
{
|
||||
$after = FileSystem::scanDirFiles($this->dir, relative: true) ?: [];
|
||||
$changed = [];
|
||||
foreach ($after as $file) {
|
||||
if (isset($this->before_file_hashes[$file])) {
|
||||
$after_hash = md5_file($this->dir . DIRECTORY_SEPARATOR . $file);
|
||||
if ($after_hash !== $this->before_file_hashes[$file]) {
|
||||
$changed[] = $file;
|
||||
}
|
||||
} elseif ($include_new_files) {
|
||||
// New file, consider as changed
|
||||
$changed[] = $file;
|
||||
}
|
||||
}
|
||||
if ($relative) {
|
||||
return $changed;
|
||||
}
|
||||
return array_map(fn ($f) => $this->dir . DIRECTORY_SEPARATOR . $f, $changed);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of removed files.
|
||||
*
|
||||
* @param bool $relative Return relative paths or absolute paths
|
||||
* @return array<string> List of removed files
|
||||
*/
|
||||
public function getRemovedFiles(bool $relative = false): array
|
||||
{
|
||||
$after = FileSystem::scanDirFiles($this->dir, relative: true) ?: [];
|
||||
$removed = array_diff($this->before, $after);
|
||||
if ($relative) {
|
||||
return $removed;
|
||||
}
|
||||
return array_map(fn ($f) => $this->dir . DIRECTORY_SEPARATOR . $f, $removed);
|
||||
}
|
||||
}
|
||||
@@ -159,4 +159,39 @@ class SourcePatcher
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Patch micro SAPI to support compressed phar loading from the current executable.
|
||||
*
|
||||
* @param int $version_id PHP version ID
|
||||
*/
|
||||
public static function patchMicroPhar(int $version_id): void
|
||||
{
|
||||
FileSystem::backupFile(SOURCE_PATH . '/php-src/ext/phar/phar.c');
|
||||
FileSystem::replaceFileStr(
|
||||
SOURCE_PATH . '/php-src/ext/phar/phar.c',
|
||||
'static zend_op_array *phar_compile_file',
|
||||
"char *micro_get_filename(void);\n\nstatic zend_op_array *phar_compile_file"
|
||||
);
|
||||
if ($version_id < 80100) {
|
||||
// PHP 8.0.x
|
||||
FileSystem::replaceFileStr(
|
||||
SOURCE_PATH . '/php-src/ext/phar/phar.c',
|
||||
'if (strstr(file_handle->filename, ".phar") && !strstr(file_handle->filename, "://")) {',
|
||||
'if ((strstr(file_handle->filename, micro_get_filename()) || strstr(file_handle->filename, ".phar")) && !strstr(file_handle->filename, "://")) {'
|
||||
);
|
||||
} else {
|
||||
// PHP >= 8.1
|
||||
FileSystem::replaceFileStr(
|
||||
SOURCE_PATH . '/php-src/ext/phar/phar.c',
|
||||
'if (strstr(ZSTR_VAL(file_handle->filename), ".phar") && !strstr(ZSTR_VAL(file_handle->filename), "://")) {',
|
||||
'if ((strstr(ZSTR_VAL(file_handle->filename), micro_get_filename()) || strstr(ZSTR_VAL(file_handle->filename), ".phar")) && !strstr(ZSTR_VAL(file_handle->filename), "://")) {'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public static function unpatchMicroPhar(): void
|
||||
{
|
||||
FileSystem::restoreBackupFile(SOURCE_PATH . '/php-src/ext/phar/phar.c');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user