From 9f7a7a5703c41f32af8608239e138ed27005d023 Mon Sep 17 00:00:00 2001 From: crazywhalecc Date: Wed, 23 Jul 2025 11:52:25 +0800 Subject: [PATCH] Add packing placeholder --- src/SPC/builder/LibraryBase.php | 22 +++++++++++++- src/SPC/builder/unix/library/icu.php | 1 + src/SPC/builder/unix/library/libevent.php | 1 + src/SPC/command/dev/PackLibCommand.php | 35 +++++++++++++++++++++++ src/globals/functions.php | 10 +++++++ 5 files changed, 68 insertions(+), 1 deletion(-) diff --git a/src/SPC/builder/LibraryBase.php b/src/SPC/builder/LibraryBase.php index b97b5929..441a389c 100644 --- a/src/SPC/builder/LibraryBase.php +++ b/src/SPC/builder/LibraryBase.php @@ -350,7 +350,27 @@ abstract class LibraryBase protected function install(): void { - // do something after extracting pre-built files, default do nothing. overwrite this method to do something + // replace placeholders if BUILD_ROOT_PATH/.spc-extract-placeholder.json exists + $placeholder_file = BUILD_ROOT_PATH . '/.spc-extract-placeholder.json'; + if (!file_exists($placeholder_file)) { + return; + } + $placeholder = json_decode(file_get_contents($placeholder_file), true); + if (!is_array($placeholder)) { + throw new RuntimeException('Invalid placeholder file: ' . $placeholder_file); + } + $placeholder = get_pack_placehoder(); + // replace placeholders in BUILD_ROOT_PATH + foreach ($placeholder as $item) { + $filepath = BUILD_ROOT_PATH . "/{$item}"; + FileSystem::replaceFileStr( + $filepath, + array_values($placeholder), + array_keys($placeholder), + ); + } + // remove placeholder file + unlink($placeholder_file); } /** diff --git a/src/SPC/builder/unix/library/icu.php b/src/SPC/builder/unix/library/icu.php index d1aa8dc1..6f2e8611 100644 --- a/src/SPC/builder/unix/library/icu.php +++ b/src/SPC/builder/unix/library/icu.php @@ -17,6 +17,7 @@ trait icu protected function install(): void { + parent::install(); $icu_config = BUILD_ROOT_PATH . '/bin/icu-config'; FileSystem::replaceFileStr($icu_config, '{BUILD_ROOT_PATH}', BUILD_ROOT_PATH); } diff --git a/src/SPC/builder/unix/library/libevent.php b/src/SPC/builder/unix/library/libevent.php index 55889d1d..ab4838e3 100644 --- a/src/SPC/builder/unix/library/libevent.php +++ b/src/SPC/builder/unix/library/libevent.php @@ -68,6 +68,7 @@ trait libevent protected function install(): void { + parent::install(); FileSystem::replaceFileStr( BUILD_LIB_PATH . '/cmake/libevent/LibeventTargets-static.cmake', '{BUILD_ROOT_PATH}', diff --git a/src/SPC/command/dev/PackLibCommand.php b/src/SPC/command/dev/PackLibCommand.php index 6440588c..7b6aec07 100644 --- a/src/SPC/command/dev/PackLibCommand.php +++ b/src/SPC/command/dev/PackLibCommand.php @@ -51,6 +51,10 @@ class PackLibCommand extends BuildCommand } } + $origin_files = []; + // get pack placehoder defines + $placehoder = get_pack_placehoder(); + foreach ($builder->getLibs() as $lib) { if ($lib->getName() !== $lib_name) { // other dependencies: install or build, both ok @@ -73,6 +77,27 @@ class PackLibCommand extends BuildCommand // After build: load buildroot/ directory, and calculate increase files $after_buildroot = FileSystem::scanDirFiles(BUILD_ROOT_PATH, relative: true); $increase_files = array_diff($after_buildroot, $before_buildroot); + + // patch pkg-config and la files with absolute path + foreach ($increase_files as $file) { + if (str_ends_with($file, '.pc') || str_ends_with($file, '.la')) { + $content = FileSystem::readFile(BUILD_ROOT_PATH . '/' . $file); + $origin_files[$file] = $content; + // replace relative paths with absolute paths + $content = str_replace( + array_keys($placehoder), + array_values($placehoder), + $content + ); + FileSystem::writeFile(BUILD_ROOT_PATH . '/' . $file, $content); + } + } + + // add .spc-extract-placeholder.json in BUILD_ROOT_PATH + $placeholder_file = BUILD_ROOT_PATH . '/.spc-extract-placeholder.json'; + file_put_contents($placeholder_file, json_encode(array_keys($origin_files), JSON_PRETTY_PRINT)); + $increase_files[] = '.spc-extract-placeholder.json'; + // every file mapped with BUILD_ROOT_PATH // get BUILD_ROOT_PATH last dir part $buildroot_part = basename(BUILD_ROOT_PATH); @@ -94,6 +119,16 @@ class PackLibCommand extends BuildCommand $filename = WORKING_DIR . '/dist/' . $filename; f_passthru("tar {$tar_option} {$filename} -T " . WORKING_DIR . '/packlib_files.txt'); logger()->info('Pack library ' . $lib->getName() . ' to ' . $filename . ' complete.'); + + // remove temp files + unlink($placeholder_file); + } + } + + foreach ($origin_files as $file => $content) { + // restore original files + if (file_exists(BUILD_ROOT_PATH . '/' . $file)) { + FileSystem::writeFile(BUILD_ROOT_PATH . '/' . $file, $content); } } diff --git a/src/globals/functions.php b/src/globals/functions.php index 998b2d1c..28f059ac 100644 --- a/src/globals/functions.php +++ b/src/globals/functions.php @@ -232,3 +232,13 @@ function ac_with_args(string $arg_name, bool $use_value = false): array { return $use_value ? ["--with-{$arg_name}=yes", "--with-{$arg_name}=no"] : ["--with-{$arg_name}", "--without-{$arg_name}"]; } + +function get_pack_placehoder(): array +{ + return [ + BUILD_LIB_PATH => '@build_lib_path@', + BUILD_BIN_PATH => '@build_bin_path@', + BUILD_INCLUDE_PATH => '@build_include_path@', + BUILD_ROOT_PATH => '@build_root_path@', + ]; +}