From 6b99ba0ece9abae560eeeeda338b4da1ed124ba3 Mon Sep 17 00:00:00 2001 From: DubbleClick Date: Mon, 2 Oct 2023 20:55:23 +0200 Subject: [PATCH] only run necessary musl install steps --- src/SPC/builder/linux/LinuxBuilder.php | 2 +- src/SPC/doctor/item/LinuxMuslCheck.php | 20 +++++++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/SPC/builder/linux/LinuxBuilder.php b/src/SPC/builder/linux/LinuxBuilder.php index 6a652ad3..66ab11d3 100644 --- a/src/SPC/builder/linux/LinuxBuilder.php +++ b/src/SPC/builder/linux/LinuxBuilder.php @@ -330,7 +330,7 @@ public function buildFpm(string $extra_libs, string $use_lld): void public function buildEmbed(string $extra_libs, string $use_lld): void { $vars = SystemUtil::makeEnvVarString([ - 'EXTRA_CFLAGS' => '-g -Os -fno-ident ' . implode(' ', array_map(fn ($x) => "-Xcompiler {$x}", $this->tune_c_flags)), + 'EXTRA_CFLAGS' => '-g -Os -fno-ident -fPIE ' . implode(' ', array_map(fn ($x) => "-Xcompiler {$x}", $this->tune_c_flags)), 'EXTRA_LIBS' => $extra_libs, 'EXTRA_LDFLAGS_PROGRAM' => "{$use_lld} -all-static", ]); diff --git a/src/SPC/doctor/item/LinuxMuslCheck.php b/src/SPC/doctor/item/LinuxMuslCheck.php index c1c8ae6d..f207a486 100644 --- a/src/SPC/doctor/item/LinuxMuslCheck.php +++ b/src/SPC/doctor/item/LinuxMuslCheck.php @@ -52,12 +52,14 @@ public function fixMusl(): bool 'url' => 'https://github.com/richfelker/musl-cross-make', 'rev' => 'master', ]; + $arch = arch2gnu(php_uname('m')) === 'x86_64' ? 'x86_64-linux-musl' : 'aarch64-linux-musl'; + $cross_compile_lib = "/usr/local/musl/{$arch}/lib/libc.a"; + $musl_wrapper_lib = sprintf('/lib/ld-musl-%s.so.1', php_uname('m')); Downloader::downloadSource('musl-cross-make', $musl_cross_compile_source); Downloader::downloadSource('musl-1.2.4', $musl_source); FileSystem::extractSource('musl-1.2.4', DOWNLOAD_PATH . '/musl-1.2.4.tar.gz'); - $arch = arch2gnu(php_uname('m')) === 'x86_64' ? 'x86_64-linux-musl' : 'aarch64-linux-musl'; $install_musl_wrapper_cmd = 'cd ' . DOWNLOAD_PATH . '/musl-cross-make && \ - make install TARGET=' . $arch . ' OUTPUT=/usr/local/musl -j'; + make install TARGET=' . $arch . ' OUTPUT=/usr/local/musl CFLAGS="-fPIE" -j'; $musl_install = 'cd ' . SOURCE_PATH . '/musl-1.2.4 && \ ./configure --enable-wrapper=gcc && \ make -j && make install'; @@ -68,17 +70,21 @@ public function fixMusl(): bool }; $fix_path = 'if [[ ! "$PATH" =~ (^|:)"/usr/local/musl/bin"(:|$) ]]; then echo "export PATH=/usr/local/musl/bin:$PATH" >> ~/.bash_profile fi && \ - source ~/.bash_profile'; + export PATH=/usr/local/musl/bin:$PATH'; $prefix = ''; if (get_current_user() !== 'root') { $prefix = 'sudo '; logger()->warning('Current user is not root, using sudo for running command'); } try { - shell(true) - ->exec($prefix . $musl_install_cmd) - ->exec($install_musl_wrapper_cmd) - ->exec($prefix . $fix_path); + if (!file_exists($musl_wrapper_lib)) { + shell(true)->exec($prefix . $musl_install_cmd); + } + if (!file_exists($cross_compile_lib)) { + shell(true)->exec($prefix . $install_musl_wrapper_cmd); + } + + shell(true)->exec($prefix . $fix_path); return true; } catch (RuntimeException) { return false;