Skip to content

Commit c715f20

Browse files
authored
Merge pull request #830 from crazywhalecc/fix/pack-location-independent
Patch pkg-config and la files with placeholder when packing pre-built content
2 parents a98f72c + e02ce4c commit c715f20

File tree

8 files changed

+81
-16
lines changed

8 files changed

+81
-16
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/linux/library/openssl.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,26 +67,26 @@ public function build(): void
6767
shell()->cd($this->source_dir)->initializeEnv($this)
6868
->exec(
6969
"{$env} ./Configure no-shared {$extra} " .
70-
'--prefix=/ ' .
71-
'--libdir=lib ' .
70+
'--prefix=' . BUILD_ROOT_PATH . ' ' .
71+
'--libdir=' . BUILD_LIB_PATH . ' ' .
7272
'--openssldir=/etc/ssl ' .
7373
"{$zlib_extra}" .
7474
'no-legacy ' .
7575
"linux-{$arch}{$clang_postfix}"
7676
)
7777
->exec('make clean')
7878
->exec("make -j{$this->builder->concurrency} CNF_EX_LIBS=\"{$ex_lib}\"")
79-
->exec("make install_sw DESTDIR={$destdir}");
79+
->exec('make install_sw');
8080
$this->patchPkgconfPrefix(['libssl.pc', 'openssl.pc', 'libcrypto.pc']);
8181
// patch for openssl 3.3.0+
8282
if (!str_contains($file = FileSystem::readFile(BUILD_LIB_PATH . '/pkgconfig/libssl.pc'), 'prefix=')) {
83-
FileSystem::writeFile(BUILD_LIB_PATH . '/pkgconfig/libssl.pc', 'prefix=${pcfiledir}/../..' . "\n" . $file);
83+
FileSystem::writeFile(BUILD_LIB_PATH . '/pkgconfig/libssl.pc', 'prefix=' . BUILD_ROOT_PATH . "\n" . $file);
8484
}
8585
if (!str_contains($file = FileSystem::readFile(BUILD_LIB_PATH . '/pkgconfig/openssl.pc'), 'prefix=')) {
86-
FileSystem::writeFile(BUILD_LIB_PATH . '/pkgconfig/openssl.pc', 'prefix=${pcfiledir}/../..' . "\n" . $file);
86+
FileSystem::writeFile(BUILD_LIB_PATH . '/pkgconfig/openssl.pc', 'prefix=' . BUILD_ROOT_PATH . "\n" . $file);
8787
}
8888
if (!str_contains($file = FileSystem::readFile(BUILD_LIB_PATH . '/pkgconfig/libcrypto.pc'), 'prefix=')) {
89-
FileSystem::writeFile(BUILD_LIB_PATH . '/pkgconfig/libcrypto.pc', 'prefix=${pcfiledir}/../..' . "\n" . $file);
89+
FileSystem::writeFile(BUILD_LIB_PATH . '/pkgconfig/libcrypto.pc', 'prefix=' . BUILD_ROOT_PATH . "\n" . $file);
9090
}
9191
FileSystem::replaceFileRegex(BUILD_LIB_PATH . '/pkgconfig/libcrypto.pc', '/Libs.private:.*/m', 'Libs.private: ${libdir}/libz.a');
9292
FileSystem::replaceFileRegex(BUILD_LIB_PATH . '/cmake/OpenSSL/OpenSSLConfig.cmake', '/set\(OPENSSL_LIBCRYPTO_DEPENDENCIES .*\)/m', 'set(OPENSSL_LIBCRYPTO_DEPENDENCIES "${OPENSSL_LIBRARY_DIR}/libz.a")');

src/SPC/builder/macos/library/openssl.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ class openssl extends MacOSLibraryBase
3737
*/
3838
protected function build(): void
3939
{
40-
[$lib,,$destdir] = SEPARATED_PATH;
41-
4240
// lib:zlib
4341
$extra = '';
4442
$ex_lib = '';
@@ -52,24 +50,24 @@ protected function build(): void
5250
shell()->cd($this->source_dir)->initializeEnv($this)
5351
->exec(
5452
"./Configure no-shared {$extra} " .
55-
'--prefix=/ ' . // use prefix=/
56-
"--libdir={$lib} " .
53+
'--prefix=' . BUILD_ROOT_PATH . ' ' . // use prefix=/
54+
'--libdir=lib ' .
5755
'--openssldir=/etc/ssl ' .
5856
"darwin64-{$arch}-cc"
5957
)
6058
->exec('make clean')
6159
->exec("make -j{$this->builder->concurrency} CNF_EX_LIBS=\"{$ex_lib}\"")
62-
->exec("make install_sw DESTDIR={$destdir}");
60+
->exec('make install_sw');
6361
$this->patchPkgconfPrefix(['libssl.pc', 'openssl.pc', 'libcrypto.pc']);
6462
// patch for openssl 3.3.0+
6563
if (!str_contains($file = FileSystem::readFile(BUILD_LIB_PATH . '/pkgconfig/libssl.pc'), 'prefix=')) {
66-
FileSystem::writeFile(BUILD_LIB_PATH . '/pkgconfig/libssl.pc', 'prefix=${pcfiledir}/../..' . "\n" . $file);
64+
FileSystem::writeFile(BUILD_LIB_PATH . '/pkgconfig/libssl.pc', 'prefix=' . BUILD_ROOT_PATH . "\n" . $file);
6765
}
6866
if (!str_contains($file = FileSystem::readFile(BUILD_LIB_PATH . '/pkgconfig/openssl.pc'), 'prefix=')) {
69-
FileSystem::writeFile(BUILD_LIB_PATH . '/pkgconfig/openssl.pc', 'prefix=${pcfiledir}/../..' . "\n" . $file);
67+
FileSystem::writeFile(BUILD_LIB_PATH . '/pkgconfig/openssl.pc', 'prefix=' . BUILD_ROOT_PATH . "\n" . $file);
7068
}
7169
if (!str_contains($file = FileSystem::readFile(BUILD_LIB_PATH . '/pkgconfig/libcrypto.pc'), 'prefix=')) {
72-
FileSystem::writeFile(BUILD_LIB_PATH . '/pkgconfig/libcrypto.pc', 'prefix=${pcfiledir}/../..' . "\n" . $file);
70+
FileSystem::writeFile(BUILD_LIB_PATH . '/pkgconfig/libcrypto.pc', 'prefix=' . BUILD_ROOT_PATH . "\n" . $file);
7371
}
7472
FileSystem::replaceFileRegex(BUILD_LIB_PATH . '/pkgconfig/libcrypto.pc', '/Libs.private:.*/m', 'Libs.private: ${libdir}/libz.a');
7573
FileSystem::replaceFileRegex(BUILD_LIB_PATH . '/cmake/OpenSSL/OpenSSLConfig.cmake', '/set\(OPENSSL_LIBCRYPTO_DEPENDENCIES .*\)/m', 'set(OPENSSL_LIBCRYPTO_DEPENDENCIES "${OPENSSL_LIBRARY_DIR}/libz.a")');

src/SPC/builder/traits/UnixLibraryTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function patchPkgconfPrefix(array $files, int $patch_option = PKGCONF_PAT
7575
logger()->debug('Patching ' . $realpath);
7676
// replace prefix
7777
$file = FileSystem::readFile($realpath);
78-
$file = ($patch_option & PKGCONF_PATCH_PREFIX) === PKGCONF_PATCH_PREFIX ? preg_replace('/^prefix\s*=.*$/m', 'prefix=${pcfiledir}/../..', $file) : $file;
78+
$file = ($patch_option & PKGCONF_PATCH_PREFIX) === PKGCONF_PATCH_PREFIX ? preg_replace('/^prefix\s*=.*$/m', 'prefix=' . BUILD_ROOT_PATH, $file) : $file;
7979
$file = ($patch_option & PKGCONF_PATCH_EXEC_PREFIX) === PKGCONF_PATCH_EXEC_PREFIX ? preg_replace('/^exec_prefix\s*=.*$/m', 'exec_prefix=${prefix}', $file) : $file;
8080
$file = ($patch_option & PKGCONF_PATCH_LIBDIR) === PKGCONF_PATCH_LIBDIR ? preg_replace('/^libdir\s*=.*$/m', 'libdir=${prefix}/lib', $file) : $file;
8181
$file = ($patch_option & PKGCONF_PATCH_INCLUDEDIR) === PKGCONF_PATCH_INCLUDEDIR ? preg_replace('/^includedir\s*=.*$/m', 'includedir=${prefix}/include', $file) : $file;

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)