crazywhalecc / static-php-cli

Build standalone PHP binary on Linux, macOS, FreeBSD, Windows, with PHP project together, with popular extensions included.
https://static-php.dev
MIT License
1.34k stars 233 forks source link

Critical error when trying to build all php extensions using --build-micro #468

Closed HallexCosta closed 5 months ago

HallexCosta commented 5 months ago

Version PHP: 8.1 Command used: bin/spc build --build-micro "apcu,bcmath,calendar,ctype,curl,dba,dom,exif,fileinfo,filter,gd,iconv,intl,mbregex,mbstring,mysqli,mysqlnd,opcache,openssl,pcntl,pdo,phar,posix,readline,redis,session,simplexml,sockets,sodium,sqlite3,swoole,swoole-hook-mysql,swoole-hook-pgsql,swoole-hook-sqlite,tokenizer,xml,xmlreader,xmlwriter,xsl,zip,zlib" --debug

Context description: I want to generate a binary file for a hyper application, but I am receiving this critical error that prevents me from using the command bin/spc micro:combine ../bin/hyperf.php

image

If I'm doing something wrong or does anyone know of an article or post explaining how I can generate a binary for a hyperf application, I'll be grateful :)

jingjingxyk commented 5 months ago
  1. 推荐在容器里构建 building in container https://static-php.dev/en/guide/manual-build.html#use-docker

  2. swoole need sapi_module.name=micro more info: https://github.com/swoole/swoole-src/pull/5340/files

HallexCosta commented 5 months ago
  1. 推荐在容器里构建 building in container https://static-php.dev/en/guide/manual-build.html#use-docker
  2. swoole need sapi_module.name=micro more info: https://github.com/swoole/swoole-src/pull/5340/files

@jingjingxyk Thanks for your answer!

Building with container and define sapi_module.name=micro work for me image

although now receive this error when execute the binary from application: image

try get around the problem replacing the Swoole\Constant use in config/autoload/server.php but it didn't work very well 😅

<?php

declare(strict_types=1);

use Hyperf\Server\Event;
use Hyperf\Server\Server;
use Swoole\Constant;

return [
    'mode' => SWOOLE_PROCESS,
    'servers' => [
        [
            'name' => 'http',
            'type' => Server::SERVER_HTTP,
            'host' => '0.0.0.0',
            'port' => 9501,
            'sock_type' => SWOOLE_SOCK_TCP,
            'callbacks' => [
                Event::ON_REQUEST => [Hyperf\HttpServer\Server::class, 'onRequest'],
            ],
            'options' => [
                // Whether to enable request lifecycle event
                'enable_request_lifecycle' => false,
            ],
        ],
    ],
    'settings' => [
        Constant::OPTION_ENABLE_COROUTINE => true,
        Constant::OPTION_WORKER_NUM => swoole_cpu_num(),
        Constant::OPTION_PID_FILE => BASE_PATH . '/runtime/hyperf.pid',
        Constant::OPTION_OPEN_TCP_NODELAY => true,
        Constant::OPTION_MAX_COROUTINE => 100000,
        Constant::OPTION_OPEN_HTTP2_PROTOCOL => true,
        Constant::OPTION_MAX_REQUEST => 100000,
        Constant::OPTION_SOCKET_BUFFER_SIZE => 2 * 1024 * 1024,
        Constant::OPTION_BUFFER_OUTPUT_SIZE => 2 * 1024 * 1024,
    ],
    'callbacks' => [
        Event::ON_WORKER_START => [Hyperf\Framework\Bootstrap\WorkerStartCallback::class, 'onWorkerStart'],
        Event::ON_PIPE_MESSAGE => [Hyperf\Framework\Bootstrap\PipeMessageCallback::class, 'onPipeMessage'],
        Event::ON_WORKER_EXIT => [Hyperf\Framework\Bootstrap\WorkerExitCallback::class, 'onWorkerExit'],
    ],
];

for this:

<?php
declare(strict_types=1);

use Hyperf\Server\Event;
use Hyperf\Server\Server;
use Swoole\Constant;

return [
    'mode' => SWOOLE_PROCESS,
    'servers' => [
        [
            'name' => 'http',
            'type' => Server::SERVER_HTTP,
            'host' => '0.0.0.0',
            'port' => 9501,
            'sock_type' => SWOOLE_SOCK_TCP,
            'callbacks' => [
                Event::ON_REQUEST => [Hyperf\HttpServer\Server::class, 'onRequest'],
            ],
            'options' => [
                // Whether to enable request lifecycle event
                'enable_request_lifecycle' => false,
            ],
        ],
    ],
    'settings' => [
        'enable_coroutine' => true,
        'worker_num' => swoole_cpu_num(),
        'pid_file' => BASE_PATH . '/runtime/hyperf.pid',
        'open_tcp_nodelay' => true,
        'max_coroutine' => 100000,
        'open_http2_protocol' => true,
        'max_request' => 100000,
        'socket_buffer_size' => 2 * 1024 * 1024,
        'buffer_output_size' => 2 * 1024 * 1024
    ],
    'callbacks' => [
        Event::ON_WORKER_START => [Hyperf\Framework\Bootstrap\WorkerStartCallback::class, 'onWorkerStart'],
        Event::ON_PIPE_MESSAGE => [Hyperf\Framework\Bootstrap\PipeMessageCallback::class, 'onPipeMessage'],
        Event::ON_WORKER_EXIT => [Hyperf\Framework\Bootstrap\WorkerExitCallback::class, 'onWorkerExit'],
    ],
];

image after replace Swoole\Constant image

jingjingxyk commented 5 months ago

2. https://github.com/swoole/swoole-src/pull/5340/files

swoole v5.1.2 之前的版本,需要修改swoole 源码,手动添加sapi_module.name=micro ,详见: https://github.com/swoole/swoole-src/blob/16a9122aec63c3b8c80874ec547f7ac39ff58dcf/ext-src/php_swoole.cc#L224

Before version 5.1.2 of SWOOLE, it is necessary to modify the SWOOLE source code and manually add sapi_module. name=micro . info: https://github.com/swoole/swoole-src/blob/16a9122aec63c3b8c80874ec547f7ac39ff58dcf/ext-src/php_swoole.cc#L224

v5.1.2 版本 在 697 行添加如下代码 line number 697 add code example : https://github.com/swoole/swoole-src/pull/5340/files


|| strcmp("micro", sapi_module.name) == 0

v5.1.2: https://github.com/swoole/swoole-src/blob/2dcfef90b3dbcfc08783747ea9abf6ebaf7eedb8/ext-src/php_swoole.cc#L697

HallexCosta commented 5 months ago
  1. https://github.com/swoole/swoole-src/pull/5340/files

swoole v5.1.2 之前的版本,需要修改swoole 源码,手动添加sapi_module.name=micro ,详见: https://github.com/swoole/swoole-src/blob/16a9122aec63c3b8c80874ec547f7ac39ff58dcf/ext-src/php_swoole.cc#L224

Before version 5.1.2 of SWOOLE, it is necessary to modify the SWOOLE source code and manually add sapi_module. name=micro . info: https://github.com/swoole/swoole-src/blob/16a9122aec63c3b8c80874ec547f7ac39ff58dcf/ext-src/php_swoole.cc#L224

v5.1.2 版本 在 697 行添加如下代码 line number 697 add code example : https://github.com/swoole/swoole-src/pull/5340/files

|| strcmp("micro", sapi_module.name) == 0

v5.1.2: https://github.com/swoole/swoole-src/blob/2dcfef90b3dbcfc08783747ea9abf6ebaf7eedb8/ext-src/php_swoole.cc#L697

Hmm, now I understand, this change is made directly in the swoole source code... Thanks for explanation!

Now the server is working! Although the worker die upon receiving a request from the browser. Apparently when I use an HTTP Client it works normally.

I found an open issue in the swoole-src repository with a similar problem. https://github.com/swoole/swoole-src/issues/5324

image

crazywhalecc commented 5 months ago

@HallexCosta

For your first build issue, building different combination of extensions before, you may need to delete buildroot and source dir first. Because some combination has alternative libs, and if you compile them first, and second time build without it, you will get linking error like yours.

Swoole currently does not support micro SAPI officially, you can modify swoole-src like @jingjingxyk said, or build with --with-micro-fake-cli, makes micro pretend to be cli.

I need to re-produce hyperf bug on my own computers. But you can try the latest phpmicro, the author of phpmicro has updated and fixed several bugs: bin/spc del-download micro && bin/spc download micro.

For every different build combination, remember rm -rf buildroot source.

HallexCosta commented 5 months ago

@HallexCosta

For your first build issue, building different combination of extensions before, you may need to delete buildroot and source dir first. Because some combination has alternative libs, and if you compile them first, and second time build without it, you will get linking error like yours.

Swoole currently does not support micro SAPI officially, you can modify swoole-src like @jingjingxyk said, or build with --with-micro-fake-cli, makes micro pretend to be cli.

I need to re-produce hyperf bug on my own computers. But you can try the latest phpmicro, the author of phpmicro has updated and fixed several bugs: bin/spc del-download micro && bin/spc download micro.

For every different build combination, remember rm -rf buildroot source.

Sorry for the delay of the answer.

I tested both solutions and they both seem to have worked fine, I don't know what could have happened but delete buildroot and source, define less compilation extensions like "bcmath,calendar,ctype,curl,dba,dom,exif,filter,fileinfo,iconv,mbstring,mbregex,openssl,pcntl,pdo,swoole,phar,posix,readline,simplexml,sockets, sqlite3,tokenizer,xml,xmlreader,xmlwriter,zip,zlib,sodium", and rebuild using bin/spc build --build-micro --with-micro-fake-cli "$EXTENSIONS" or directly modify the swoole-src now worked 😁.

Now, I'm trying to compile my hyperf application into a .phar file and then build it into a binary file using spc, but I'm having new challenges when building hyperf app on .phar

HallexCosta commented 5 months ago

Another problem I'm facing is when building a binary from a .phar file. Using swoole-cli hyperf.phar start the app works normally, but when building it in static binary and running it the console prints some error messages

Command used for build is the same from above comment: set EXTENSIONS "bcmath,calendar,ctype,curl,dba,dom,exif,filter,fileinfo,iconv,mbstring,mbregex,openssl,pcntl,pdo,swoole,phar,posix,readline,simplexml,sockets,sqlite3,tokenizer,xml,xmlreader,xmlwriter,zip,zlib,sodium" && spc build --build-micro --with-micro-fake-cli "$EXTENSIONS" Comand used to combine: spc micro:combine hyperf.phar -O bin/phar-static-binary -M ../../crazywhalecc/static-php-cli/buildroot/bin/micro.sfx

Message error when execute the binary generated using a .phar file image

The same phar file running using swoole-cli image

crazywhalecc commented 5 months ago

Using the same spc compilation mode, after adding --build-cli, can it be run using buildroot/bin/php your-hyperf.phar start?

jingjingxyk commented 5 months ago

swoole support micro on v5.1.3 https://github.com/swoole/swoole-src/releases/tag/v5.1.3 https://github.com/swoole/swoole-src/pull/5340/files

HallexCosta commented 5 months ago

Using the same spc compilation mode, after adding --build-cli, can it be run using buildroot/bin/php your-hyperf.phar start?

Yes, with build --build-cli and running buildroot/bin/php my-hyperf.phar works image

crazywhalecc commented 5 months ago

This problem is related to phpmicro itself. See #476 . Will fix phpmicro upstream

crazywhalecc commented 5 months ago

phpmicro has fixed phar issue, remember cleaning buildroot and source, and bin/spc del-download micro && bin/spc download micro to download the latest micro repo to build.