Skip to content

Commit

Permalink
Fix micro upx compress bug (#440)
Browse files Browse the repository at this point in the history
* triple compression for micro with upx

* place debug info first
  • Loading branch information
crazywhalecc authored May 6, 2024
1 parent 954f8f4 commit bde1805
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/SPC/builder/linux/LinuxBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,19 @@ public function buildPHP(int $build_target = BUILD_TARGET_NONE): void
]);

// upx pack and strip for micro
// but always restore Makefile.frag.bak first
if (file_exists(SOURCE_PATH . '/php-src/sapi/micro/Makefile.frag.bak')) {
copy(SOURCE_PATH . '/php-src/sapi/micro/Makefile.frag.bak', SOURCE_PATH . '/php-src/sapi/micro/Makefile.frag');
}
if ($this->getOption('with-upx-pack', false)) {
// judge $(MAKE) micro_2s_objs SFX_FILESIZE=`$(STAT_SIZE) $(SAPI_MICRO_PATH)` count
// if 2, replace src/globals/extra/micro-triple-Makefile.frag file content
if (substr_count(FileSystem::readFile(SOURCE_PATH . '/php-src/sapi/micro/Makefile.frag'), '$(MAKE) micro_2s_objs SFX_FILESIZE=`$(STAT_SIZE) $(SAPI_MICRO_PATH)`') === 2) {
// bak first
copy(SOURCE_PATH . '/php-src/sapi/micro/Makefile.frag', SOURCE_PATH . '/php-src/sapi/micro/Makefile.frag.bak');
// replace Makefile.frag content
FileSystem::writeFile(SOURCE_PATH . '/php-src/sapi/micro/Makefile.frag', FileSystem::readFile(ROOT_DIR . '/src/globals/extra/micro-triple-Makefile.frag'));
}
// with upx pack always need strip
FileSystem::replaceFileRegex(
SOURCE_PATH . '/php-src/sapi/micro/Makefile.frag',
Expand Down
2 changes: 1 addition & 1 deletion src/SPC/command/BuildCliCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ public function handle(): int
if (!empty($not_included)) {
$indent_texts['Extra Exts (' . count($not_included) . ')'] = implode(', ', $not_included);
}
$this->printFormatInfo($indent_texts);
$this->printFormatInfo($this->getDefinedEnvs(), true);
$this->printFormatInfo($indent_texts);
logger()->notice('Build will start after 2s ...');
sleep(2);

Expand Down
51 changes: 51 additions & 0 deletions src/globals/extra/micro-triple-Makefile.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

micro: $(SAPI_MICRO_PATH)

micro_2s_objs:
rm $(MICRO_2STAGE_OBJS) || :
$(MAKE) $(MICRO_2STAGE_OBJS)

EXTRA_MICRO_BUILD_COMMANDS=
POST_MICRO_BUILD_COMMANDS=$(STRIP) $(MICRO_STRIP_FLAGS) $(SAPI_MICRO_PATH)

$(SAPI_MICRO_PATH): $(PHP_GLOBAL_OBJS) $(PHP_BINARY_OBJS) $(PHP_MICRO_OBJS)
$(MAKE) micro_2s_objs SFX_FILESIZE=0xcafebabe
$(BUILD_MICRO)
$(EXTRA_MICRO_BUILD_COMMANDS)
$(POST_MICRO_BUILD_COMMANDS)
$(MAKE) micro_2s_objs SFX_FILESIZE=`$(STAT_SIZE) $(SAPI_MICRO_PATH)`
$(BUILD_MICRO)
$(EXTRA_MICRO_BUILD_COMMANDS)
$(POST_MICRO_BUILD_COMMANDS)
$(MAKE) micro_2s_objs SFX_FILESIZE=`$(STAT_SIZE) $(SAPI_MICRO_PATH)`
$(BUILD_MICRO)
$(EXTRA_MICRO_BUILD_COMMANDS)
$(POST_MICRO_BUILD_COMMANDS)
$(MAKE) micro_2s_objs SFX_FILESIZE=`$(STAT_SIZE) $(SAPI_MICRO_PATH)`
$(BUILD_MICRO)
$(EXTRA_MICRO_BUILD_COMMANDS)
$(POST_MICRO_BUILD_COMMANDS)

MICRO_EXES = sapi/micro/tests/simpleecho.exe sapi/micro/tests/fakecmd.exe

$(MICRO_EXES): $(SAPI_MICRO_PATH)
@binname=$@;\
cat $(SAPI_MICRO_PATH) $${binname%.exe}.php > $@ || {\
rm $@; \
exit 1; \
}
@chmod 0755 $@

MICRO_FAKECMD=sapi/micro/tests/fakecmd.exe

micro_test: $(SAPI_MICRO_PATH) $(MICRO_EXES)
@[ x"hello world" = "x`sapi/micro/tests/simpleecho.exe nonce world`" ] || {\
echo sanity check for micro.sfx failed, the sfx generated may be corrupt. >&2 ;\
exit 1;\
}
@SKIP_IO_CAPTURE_TESTS=yes \
TEST_PHP_EXECUTABLE=$(MICRO_FAKECMD) \
TEST_PHP_SRCDIR=$(top_srcdir) \
CC="$(CC)" \
$(MICRO_FAKECMD) -n $(PHP_TEST_SETTINGS) $(top_srcdir)/run-tests.php -n $(TESTS); \
exit $$?;

0 comments on commit bde1805

Please sign in to comment.