mirror of
https://github.com/crazywhalecc/static-php-cli.git
synced 2026-03-18 04:44:53 +08:00
Merge pull request #830 from crazywhalecc/fix/pack-location-independent
Patch pkg-config and la files with placeholder when packing pre-built content
This commit is contained in:
commit
c715f20fe3
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -67,8 +67,8 @@ class openssl extends LinuxLibraryBase
|
||||
shell()->cd($this->source_dir)->initializeEnv($this)
|
||||
->exec(
|
||||
"{$env} ./Configure no-shared {$extra} " .
|
||||
'--prefix=/ ' .
|
||||
'--libdir=lib ' .
|
||||
'--prefix=' . BUILD_ROOT_PATH . ' ' .
|
||||
'--libdir=' . BUILD_LIB_PATH . ' ' .
|
||||
'--openssldir=/etc/ssl ' .
|
||||
"{$zlib_extra}" .
|
||||
'no-legacy ' .
|
||||
@ -76,17 +76,17 @@ class openssl extends LinuxLibraryBase
|
||||
)
|
||||
->exec('make clean')
|
||||
->exec("make -j{$this->builder->concurrency} CNF_EX_LIBS=\"{$ex_lib}\"")
|
||||
->exec("make install_sw DESTDIR={$destdir}");
|
||||
->exec('make install_sw');
|
||||
$this->patchPkgconfPrefix(['libssl.pc', 'openssl.pc', 'libcrypto.pc']);
|
||||
// patch for openssl 3.3.0+
|
||||
if (!str_contains($file = FileSystem::readFile(BUILD_LIB_PATH . '/pkgconfig/libssl.pc'), 'prefix=')) {
|
||||
FileSystem::writeFile(BUILD_LIB_PATH . '/pkgconfig/libssl.pc', 'prefix=${pcfiledir}/../..' . "\n" . $file);
|
||||
FileSystem::writeFile(BUILD_LIB_PATH . '/pkgconfig/libssl.pc', 'prefix=' . BUILD_ROOT_PATH . "\n" . $file);
|
||||
}
|
||||
if (!str_contains($file = FileSystem::readFile(BUILD_LIB_PATH . '/pkgconfig/openssl.pc'), 'prefix=')) {
|
||||
FileSystem::writeFile(BUILD_LIB_PATH . '/pkgconfig/openssl.pc', 'prefix=${pcfiledir}/../..' . "\n" . $file);
|
||||
FileSystem::writeFile(BUILD_LIB_PATH . '/pkgconfig/openssl.pc', 'prefix=' . BUILD_ROOT_PATH . "\n" . $file);
|
||||
}
|
||||
if (!str_contains($file = FileSystem::readFile(BUILD_LIB_PATH . '/pkgconfig/libcrypto.pc'), 'prefix=')) {
|
||||
FileSystem::writeFile(BUILD_LIB_PATH . '/pkgconfig/libcrypto.pc', 'prefix=${pcfiledir}/../..' . "\n" . $file);
|
||||
FileSystem::writeFile(BUILD_LIB_PATH . '/pkgconfig/libcrypto.pc', 'prefix=' . BUILD_ROOT_PATH . "\n" . $file);
|
||||
}
|
||||
FileSystem::replaceFileRegex(BUILD_LIB_PATH . '/pkgconfig/libcrypto.pc', '/Libs.private:.*/m', 'Libs.private: ${libdir}/libz.a');
|
||||
FileSystem::replaceFileRegex(BUILD_LIB_PATH . '/cmake/OpenSSL/OpenSSLConfig.cmake', '/set\(OPENSSL_LIBCRYPTO_DEPENDENCIES .*\)/m', 'set(OPENSSL_LIBCRYPTO_DEPENDENCIES "${OPENSSL_LIBRARY_DIR}/libz.a")');
|
||||
|
||||
@ -37,8 +37,6 @@ class openssl extends MacOSLibraryBase
|
||||
*/
|
||||
protected function build(): void
|
||||
{
|
||||
[$lib,,$destdir] = SEPARATED_PATH;
|
||||
|
||||
// lib:zlib
|
||||
$extra = '';
|
||||
$ex_lib = '';
|
||||
@ -52,24 +50,24 @@ class openssl extends MacOSLibraryBase
|
||||
shell()->cd($this->source_dir)->initializeEnv($this)
|
||||
->exec(
|
||||
"./Configure no-shared {$extra} " .
|
||||
'--prefix=/ ' . // use prefix=/
|
||||
"--libdir={$lib} " .
|
||||
'--prefix=' . BUILD_ROOT_PATH . ' ' . // use prefix=/
|
||||
'--libdir=lib ' .
|
||||
'--openssldir=/etc/ssl ' .
|
||||
"darwin64-{$arch}-cc"
|
||||
)
|
||||
->exec('make clean')
|
||||
->exec("make -j{$this->builder->concurrency} CNF_EX_LIBS=\"{$ex_lib}\"")
|
||||
->exec("make install_sw DESTDIR={$destdir}");
|
||||
->exec('make install_sw');
|
||||
$this->patchPkgconfPrefix(['libssl.pc', 'openssl.pc', 'libcrypto.pc']);
|
||||
// patch for openssl 3.3.0+
|
||||
if (!str_contains($file = FileSystem::readFile(BUILD_LIB_PATH . '/pkgconfig/libssl.pc'), 'prefix=')) {
|
||||
FileSystem::writeFile(BUILD_LIB_PATH . '/pkgconfig/libssl.pc', 'prefix=${pcfiledir}/../..' . "\n" . $file);
|
||||
FileSystem::writeFile(BUILD_LIB_PATH . '/pkgconfig/libssl.pc', 'prefix=' . BUILD_ROOT_PATH . "\n" . $file);
|
||||
}
|
||||
if (!str_contains($file = FileSystem::readFile(BUILD_LIB_PATH . '/pkgconfig/openssl.pc'), 'prefix=')) {
|
||||
FileSystem::writeFile(BUILD_LIB_PATH . '/pkgconfig/openssl.pc', 'prefix=${pcfiledir}/../..' . "\n" . $file);
|
||||
FileSystem::writeFile(BUILD_LIB_PATH . '/pkgconfig/openssl.pc', 'prefix=' . BUILD_ROOT_PATH . "\n" . $file);
|
||||
}
|
||||
if (!str_contains($file = FileSystem::readFile(BUILD_LIB_PATH . '/pkgconfig/libcrypto.pc'), 'prefix=')) {
|
||||
FileSystem::writeFile(BUILD_LIB_PATH . '/pkgconfig/libcrypto.pc', 'prefix=${pcfiledir}/../..' . "\n" . $file);
|
||||
FileSystem::writeFile(BUILD_LIB_PATH . '/pkgconfig/libcrypto.pc', 'prefix=' . BUILD_ROOT_PATH . "\n" . $file);
|
||||
}
|
||||
FileSystem::replaceFileRegex(BUILD_LIB_PATH . '/pkgconfig/libcrypto.pc', '/Libs.private:.*/m', 'Libs.private: ${libdir}/libz.a');
|
||||
FileSystem::replaceFileRegex(BUILD_LIB_PATH . '/cmake/OpenSSL/OpenSSLConfig.cmake', '/set\(OPENSSL_LIBCRYPTO_DEPENDENCIES .*\)/m', 'set(OPENSSL_LIBCRYPTO_DEPENDENCIES "${OPENSSL_LIBRARY_DIR}/libz.a")');
|
||||
|
||||
@ -75,7 +75,7 @@ trait UnixLibraryTrait
|
||||
logger()->debug('Patching ' . $realpath);
|
||||
// replace prefix
|
||||
$file = FileSystem::readFile($realpath);
|
||||
$file = ($patch_option & PKGCONF_PATCH_PREFIX) === PKGCONF_PATCH_PREFIX ? preg_replace('/^prefix\s*=.*$/m', 'prefix=${pcfiledir}/../..', $file) : $file;
|
||||
$file = ($patch_option & PKGCONF_PATCH_PREFIX) === PKGCONF_PATCH_PREFIX ? preg_replace('/^prefix\s*=.*$/m', 'prefix=' . BUILD_ROOT_PATH, $file) : $file;
|
||||
$file = ($patch_option & PKGCONF_PATCH_EXEC_PREFIX) === PKGCONF_PATCH_EXEC_PREFIX ? preg_replace('/^exec_prefix\s*=.*$/m', 'exec_prefix=${prefix}', $file) : $file;
|
||||
$file = ($patch_option & PKGCONF_PATCH_LIBDIR) === PKGCONF_PATCH_LIBDIR ? preg_replace('/^libdir\s*=.*$/m', 'libdir=${prefix}/lib', $file) : $file;
|
||||
$file = ($patch_option & PKGCONF_PATCH_INCLUDEDIR) === PKGCONF_PATCH_INCLUDEDIR ? preg_replace('/^includedir\s*=.*$/m', 'includedir=${prefix}/include', $file) : $file;
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
@ -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}',
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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@',
|
||||
];
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user