Skip to content

Commit 9f7a7a5

Browse files
committed
Add packing placeholder
1 parent 147fd39 commit 9f7a7a5

File tree

5 files changed

+68
-1
lines changed

5 files changed

+68
-1
lines changed

src/SPC/builder/LibraryBase.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,27 @@ abstract protected function build();
350350

351351
protected function install(): void
352352
{
353-
// do something after extracting pre-built files, default do nothing. overwrite this method to do something
353+
// replace placeholders if BUILD_ROOT_PATH/.spc-extract-placeholder.json exists
354+
$placeholder_file = BUILD_ROOT_PATH . '/.spc-extract-placeholder.json';
355+
if (!file_exists($placeholder_file)) {
356+
return;
357+
}
358+
$placeholder = json_decode(file_get_contents($placeholder_file), true);
359+
if (!is_array($placeholder)) {
360+
throw new RuntimeException('Invalid placeholder file: ' . $placeholder_file);
361+
}
362+
$placeholder = get_pack_placehoder();
363+
// replace placeholders in BUILD_ROOT_PATH
364+
foreach ($placeholder as $item) {
365+
$filepath = BUILD_ROOT_PATH . "/{$item}";
366+
FileSystem::replaceFileStr(
367+
$filepath,
368+
array_values($placeholder),
369+
array_keys($placeholder),
370+
);
371+
}
372+
// remove placeholder file
373+
unlink($placeholder_file);
354374
}
355375

356376
/**

src/SPC/builder/unix/library/icu.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public function beforePack(): void
1717

1818
protected function install(): void
1919
{
20+
parent::install();
2021
$icu_config = BUILD_ROOT_PATH . '/bin/icu-config';
2122
FileSystem::replaceFileStr($icu_config, '{BUILD_ROOT_PATH}', BUILD_ROOT_PATH);
2223
}

src/SPC/builder/unix/library/libevent.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ protected function build(): void
6868

6969
protected function install(): void
7070
{
71+
parent::install();
7172
FileSystem::replaceFileStr(
7273
BUILD_LIB_PATH . '/cmake/libevent/LibeventTargets-static.cmake',
7374
'{BUILD_ROOT_PATH}',

src/SPC/command/dev/PackLibCommand.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ public function handle(): int
5151
}
5252
}
5353

54+
$origin_files = [];
55+
// get pack placehoder defines
56+
$placehoder = get_pack_placehoder();
57+
5458
foreach ($builder->getLibs() as $lib) {
5559
if ($lib->getName() !== $lib_name) {
5660
// other dependencies: install or build, both ok
@@ -73,6 +77,27 @@ public function handle(): int
7377
// After build: load buildroot/ directory, and calculate increase files
7478
$after_buildroot = FileSystem::scanDirFiles(BUILD_ROOT_PATH, relative: true);
7579
$increase_files = array_diff($after_buildroot, $before_buildroot);
80+
81+
// patch pkg-config and la files with absolute path
82+
foreach ($increase_files as $file) {
83+
if (str_ends_with($file, '.pc') || str_ends_with($file, '.la')) {
84+
$content = FileSystem::readFile(BUILD_ROOT_PATH . '/' . $file);
85+
$origin_files[$file] = $content;
86+
// replace relative paths with absolute paths
87+
$content = str_replace(
88+
array_keys($placehoder),
89+
array_values($placehoder),
90+
$content
91+
);
92+
FileSystem::writeFile(BUILD_ROOT_PATH . '/' . $file, $content);
93+
}
94+
}
95+
96+
// add .spc-extract-placeholder.json in BUILD_ROOT_PATH
97+
$placeholder_file = BUILD_ROOT_PATH . '/.spc-extract-placeholder.json';
98+
file_put_contents($placeholder_file, json_encode(array_keys($origin_files), JSON_PRETTY_PRINT));
99+
$increase_files[] = '.spc-extract-placeholder.json';
100+
76101
// every file mapped with BUILD_ROOT_PATH
77102
// get BUILD_ROOT_PATH last dir part
78103
$buildroot_part = basename(BUILD_ROOT_PATH);
@@ -94,6 +119,16 @@ public function handle(): int
94119
$filename = WORKING_DIR . '/dist/' . $filename;
95120
f_passthru("tar {$tar_option} {$filename} -T " . WORKING_DIR . '/packlib_files.txt');
96121
logger()->info('Pack library ' . $lib->getName() . ' to ' . $filename . ' complete.');
122+
123+
// remove temp files
124+
unlink($placeholder_file);
125+
}
126+
}
127+
128+
foreach ($origin_files as $file => $content) {
129+
// restore original files
130+
if (file_exists(BUILD_ROOT_PATH . '/' . $file)) {
131+
FileSystem::writeFile(BUILD_ROOT_PATH . '/' . $file, $content);
97132
}
98133
}
99134

src/globals/functions.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,13 @@ function ac_with_args(string $arg_name, bool $use_value = false): array
232232
{
233233
return $use_value ? ["--with-{$arg_name}=yes", "--with-{$arg_name}=no"] : ["--with-{$arg_name}", "--without-{$arg_name}"];
234234
}
235+
236+
function get_pack_placehoder(): array
237+
{
238+
return [
239+
BUILD_LIB_PATH => '@build_lib_path@',
240+
BUILD_BIN_PATH => '@build_bin_path@',
241+
BUILD_INCLUDE_PATH => '@build_include_path@',
242+
BUILD_ROOT_PATH => '@build_root_path@',
243+
];
244+
}

0 commit comments

Comments
 (0)