55namespace SPC \builder \linux ;
66
77use SPC \builder \unix \UnixBuilderBase ;
8- use SPC \exception \PatchException ;
98use SPC \exception \WrongUsageException ;
109use SPC \store \Config ;
1110use SPC \store \FileSystem ;
@@ -194,14 +193,6 @@ protected function buildCli(): void
194193 SourcePatcher::patchFile ('musl_static_readline.patch ' , SOURCE_PATH . '/php-src ' , true );
195194 }
196195
197- if (!$ this ->getOption ('no-strip ' , false )) {
198- shell ()->cd (SOURCE_PATH . '/php-src/sapi/cli ' )->exec ('strip --strip-unneeded php ' );
199- }
200- if ($ this ->getOption ('with-upx-pack ' )) {
201- shell ()->cd (SOURCE_PATH . '/php-src/sapi/cli ' )
202- ->exec (getenv ('UPX_EXEC ' ) . ' --best php ' );
203- }
204-
205196 $ this ->deployBinary (BUILD_TARGET_CLI );
206197 }
207198
@@ -213,14 +204,6 @@ protected function buildCgi(): void
213204 ->exec ('sed -i "s|//lib|/lib|g" Makefile ' )
214205 ->exec ("make {$ concurrency } {$ vars } cgi " );
215206
216- if (!$ this ->getOption ('no-strip ' , false )) {
217- shell ()->cd (SOURCE_PATH . '/php-src/sapi/cgi ' )->exec ('strip --strip-unneeded php-cgi ' );
218- }
219- if ($ this ->getOption ('with-upx-pack ' )) {
220- shell ()->cd (SOURCE_PATH . '/php-src/sapi/cgi ' )
221- ->exec (getenv ('UPX_EXEC ' ) . ' --best php-cgi ' );
222- }
223-
224207 $ this ->deployBinary (BUILD_TARGET_CGI );
225208 }
226209
@@ -232,29 +215,29 @@ protected function buildMicro(): void
232215 if ($ this ->getPHPVersionID () < 80000 ) {
233216 throw new WrongUsageException ('phpmicro only support PHP >= 8.0! ' );
234217 }
235- if ($ this ->getExt ('phar ' )) {
236- $ this ->phar_patched = true ;
237- SourcePatcher::patchMicroPhar ($ this ->getPHPVersionID ());
238- }
239-
240- $ enable_fake_cli = $ this ->getOption ('with-micro-fake-cli ' , false ) ? ' -DPHP_MICRO_FAKE_CLI ' : '' ;
241- $ vars = $ this ->getMakeExtraVars ();
242-
243- // patch fake cli for micro
244- $ vars ['EXTRA_CFLAGS ' ] .= $ enable_fake_cli ;
245- $ vars = SystemUtil::makeEnvVarString ($ vars );
246- $ concurrency = getenv ('SPC_CONCURRENCY ' ) ? '-j ' . getenv ('SPC_CONCURRENCY ' ) : '' ;
218+ try {
219+ if ($ this ->getExt ('phar ' )) {
220+ $ this ->phar_patched = true ;
221+ SourcePatcher::patchMicroPhar ($ this ->getPHPVersionID ());
222+ }
247223
248- shell ()->cd (SOURCE_PATH . '/php-src ' )
249- ->exec ('sed -i "s|//lib|/lib|g" Makefile ' )
250- ->exec ("make {$ concurrency } {$ vars } micro " );
224+ $ enable_fake_cli = $ this ->getOption ('with-micro-fake-cli ' , false ) ? ' -DPHP_MICRO_FAKE_CLI ' : '' ;
225+ $ vars = $ this ->getMakeExtraVars ();
251226
252- $ this ->processMicroUPX ();
227+ // patch fake cli for micro
228+ $ vars ['EXTRA_CFLAGS ' ] .= $ enable_fake_cli ;
229+ $ vars = SystemUtil::makeEnvVarString ($ vars );
230+ $ concurrency = getenv ('SPC_CONCURRENCY ' ) ? '-j ' . getenv ('SPC_CONCURRENCY ' ) : '' ;
253231
254- $ this ->deployBinary (BUILD_TARGET_MICRO );
232+ shell ()->cd (SOURCE_PATH . '/php-src ' )
233+ ->exec ('sed -i "s|//lib|/lib|g" Makefile ' )
234+ ->exec ("make {$ concurrency } {$ vars } micro " );
255235
256- if ($ this ->phar_patched ) {
257- SourcePatcher::unpatchMicroPhar ();
236+ $ this ->deployBinary (BUILD_TARGET_MICRO );
237+ } finally {
238+ if ($ this ->phar_patched ) {
239+ SourcePatcher::unpatchMicroPhar ();
240+ }
258241 }
259242 }
260243
@@ -269,13 +252,6 @@ protected function buildFpm(): void
269252 ->exec ('sed -i "s|//lib|/lib|g" Makefile ' )
270253 ->exec ("make {$ concurrency } {$ vars } fpm " );
271254
272- if (!$ this ->getOption ('no-strip ' , false )) {
273- shell ()->cd (SOURCE_PATH . '/php-src/sapi/fpm ' )->exec ('strip --strip-unneeded php-fpm ' );
274- }
275- if ($ this ->getOption ('with-upx-pack ' )) {
276- shell ()->cd (SOURCE_PATH . '/php-src/sapi/fpm ' )
277- ->exec (getenv ('UPX_EXEC ' ) . ' --best php-fpm ' );
278- }
279255 $ this ->deployBinary (BUILD_TARGET_FPM );
280256 }
281257
@@ -390,31 +366,4 @@ private function getMakeExtraVars(): array
390366 'EXTRA_LDFLAGS_PROGRAM ' => "-L {$ lib } {$ static } -pie " ,
391367 ]);
392368 }
393-
394- /**
395- * Strip micro.sfx for Linux.
396- * The micro.sfx does not support UPX directly, but we can remove UPX-info segment to adapt.
397- * This will also make micro.sfx with upx-packed more like a malware fore antivirus :(
398- */
399- private function processMicroUPX (): void
400- {
401- if (version_compare ($ this ->getMicroVersion (), '0.2.0 ' ) >= 0 && !$ this ->getOption ('no-strip ' , false )) {
402- shell ()->exec ('strip --strip-unneeded ' . SOURCE_PATH . '/php-src/sapi/micro/micro.sfx ' );
403-
404- if ($ this ->getOption ('with-upx-pack ' )) {
405- // strip first
406- shell ()->exec (getenv ('UPX_EXEC ' ) . ' --best ' . SOURCE_PATH . '/php-src/sapi/micro/micro.sfx ' );
407- // cut binary with readelf
408- [$ ret , $ out ] = shell ()->execWithResult ('readelf -l ' . SOURCE_PATH . '/php-src/sapi/micro/micro.sfx | awk \'/LOAD|GNU_STACK/ {getline; print $1, $2, $3, $4, $6, $7} \'' );
409- $ out [1 ] = explode (' ' , $ out [1 ]);
410- $ offset = $ out [1 ][0 ];
411- if ($ ret !== 0 || !str_starts_with ($ offset , '0x ' )) {
412- throw new PatchException ('phpmicro UPX patcher ' , 'Cannot find offset in readelf output ' );
413- }
414- $ offset = hexdec ($ offset );
415- // remove upx extra wastes
416- file_put_contents (SOURCE_PATH . '/php-src/sapi/micro/micro.sfx ' , substr (file_get_contents (SOURCE_PATH . '/php-src/sapi/micro/micro.sfx ' ), 0 , $ offset ));
417- }
418- }
419- }
420369}
0 commit comments