phpmicro icon indicating copy to clipboard operation
phpmicro copied to clipboard

swoole扩展怎么编译进去

Open xieyuhua opened this issue 4 years ago • 19 comments

swoole扩展怎么编译进去

xieyuhua avatar Feb 06 '21 10:02 xieyuhua

像一般的扩展in-tree构建一样,你可以这么来构建带某个扩展的micro.sfx:

Like common extension in-tree build, you can build micro.sfx with some extension:

# 在php源码目录 / at php source dir
# clone swoole to ext/swoole / clone swoole到ext/swoole
git clone --depth 1 --branch <你喜欢的ref> https://github.com/swoole/swoole-src ext/myext
# 重新生成configure / regenerate configure
./buildconf --force
# congfigure
./configure <args like "--disable-cli --enable-micro"> --enable-<扩展名比如swoole>
# make
make -j`nproc`

注意的是,可能一些非官方扩展对in-tree构建的支持不是很好,你可能需要稍微修改一下它的源代码

Note here, some non-offical extensions may not compatible with in-tree build, you may need to patch its source.

特别的,对于swoole,多个swoole版本在in-tree构建时会以各种方式出错

As for swoole, variant swoole versions may error in in-tree build.

同时 swoole需要c++,这意味着你需要编译glibc或者libcpp,这将大大增加micro的大小

dixyes avatar Feb 06 '21 11:02 dixyes

好的,谢谢, 我得去补充一下这方面的知识,如果其他扩展都能加入进去的话,相对来说,还是方便不少。

xieyuhua avatar Feb 06 '21 11:02 xieyuhua

我试着编译了 swoole 4.6.2

I tried built with swoole 4.6.2

确实,swoole需要一些patch才能in-tree编译,我使用了这些patch:

Indeed, swoole need some patches to in-tree build, the patches I used:

nonghttp.patch

我单独编译并连接了nghttp,因此需要将swoole的nghttp代码删除:

I build and linked in nghttp, so I need remove nghttp codes in swoole:

diff --git a/config.m4 b/config.m4
index 8193d9d69..56de650fb 100644
--- a/config.m4
+++ b/config.m4
@@ -597,7 +597,7 @@ if test "$PHP_SWOOLE" != "no"; then
         thirdparty/hiredis/read.c \
         thirdparty/hiredis/sds.c"

-    swoole_source_file="$swoole_source_file \
+    NOswoole_source_file="$swoole_source_file \
         thirdparty/nghttp2/nghttp2_hd.c \
         thirdparty/nghttp2/nghttp2_rcbuf.c \
         thirdparty/nghttp2/nghttp2_helper.c \

clang.patch

swoole直接修改了clang编译时的CFLAGS,导致php代码无法正常编译, 我也使用了clang

swoole modified CFLAGS instantly when using clang, this cause php codes cannot compile normally

diff --git a/config.m4 b/config.m4
index 8193d9d69..5ccd58891 100644
--- a/config.m4
+++ b/config.m4
@@ -279,10 +279,6 @@ AC_COMPILE_IFELSE([
 )
 AC_MSG_RESULT([$CLANG])

-if test "$CLANG" = "yes"; then
-    CFLAGS="$CFLAGS -std=gnu89"
-fi
-
 AC_CANONICAL_HOST

 if test "$PHP_SWOOLE" != "no"; then

config_h.patch

in-tree编译时不使用config.h而是main/php_config.h,这导致HAVE_CONFIG_H预定义宏无法正常生效

when in-tree building, generated header is main/php_config.h not config.h, this causes HAVE_CONFIG_H macro not work

( copied from my config.m4 for swow...

diff --git a/config.m4 b/config.m4
index 8193d9d69..1a9d1c846 100644
--- a/config.m4
+++ b/config.m4
@@ -286,6 +286,13 @@ fi
 AC_CANONICAL_HOST

 if test "$PHP_SWOOLE" != "no"; then
+dnl solve in-tree build config.h problem
+dnl just make a fake config.h before all things
+PHP_ADD_BUILD_DIR(PHP_EXT_DIR(swoole)"/build", 1)
+cat > PHP_EXT_DIR(swoole)/build/config.h << EOF
+#include "php_config.h"
+EOF
+INCLUDES="-I. -I"PHP_EXT_DIR(swoole)"/build ${INCLUDES}"

     AC_CHECK_LIB(c, accept4, AC_DEFINE(HAVE_ACCEPT4, 1, [have accept4]))
     AC_CHECK_LIB(c, signalfd, AC_DEFINE(HAVE_SIGNALFD, 1, [have signalfd]))
@@ -682,7 +689,7 @@ if test "$PHP_SWOOLE" != "no"; then
         AC_DEFINE(SW_USE_ASM_CONTEXT, 1, [use boost asm context])
     fi

-    PHP_NEW_EXTENSION(swoole, $swoole_source_file, $ext_shared,,$EXTRA_CFLAGS, cxx)
+    PHP_NEW_EXTENSION(swoole, $swoole_source_file, $ext_shared,,"$EXTRA_CFLAGS -DHAVE_CONFIG_H", cxx)

     PHP_ADD_INCLUDE([$ext_srcdir])
     PHP_ADD_INCLUDE([$ext_srcdir/include])

除此之外,swoole的php_swoole.h没有出现在扩展根目录,这导致configure无法正常找到定义,你还需要在扩展根目录放置任意名称(比如"php_swoole_for_configure.h")的头文件,内容如下

Otherwise, swoole's php_swoole.h is not located in extension root dir, this cause configure cannot locate module definations, you need put a header at extension root dir with any name (like "php_swoole_for_configure.h") with content:

extern zend_module_entry swoole_module_entry;
#define phpext_swoole_ptr &swoole_module_entry
文件 SHA256
micro_with_swoole462_php802_x64_linux.zip 10f942b6d2343153d216a7992ac1e57d5864b69f0f52261c5bd2617cdd6e47d0

dixyes avatar Feb 06 '21 13:02 dixyes

[root@SwLocal api]# php api.php Swoole http server is started at http://127.0.0.1:9501 时间:2021-02-07 13:23:42

[root@SwLocal api]# [root@SwLocal api]# [root@SwLocal api]# [root@SwLocal api]# cat micro.sfx api.php >ai [root@SwLocal api]# chmod 0755 ai [root@SwLocal api]# ./ai Fatal error: Uncaught Swoole\Exception: Swoole\Http\Server can only be used in CLI mode in /home/sku-stock/api/ai:4 Stack trace: #0 /home/sku-stock/api/ai(4): Swoole\Server->__construct('0.0.0.0', 9501) #1 {main} thrown in /home/sku-stock/api/ai on line 4 [root@SwLocal api]#

xieyuhua avatar Feb 07 '21 05:02 xieyuhua

看来是做了硬编码的SAPI检查,加了个patch

Seems there's some hardcoden checks, add a patch

diff --git a/ext-src/php_swoole.cc b/ext-src/php_swoole.cc
index d98127733..89ab4380c 100644
--- a/ext-src/php_swoole.cc
+++ b/ext-src/php_swoole.cc
@@ -722,7 +722,7 @@ PHP_MINIT_FUNCTION(swoole) {
     }

     swoole_init();
-    if (strcmp("cli", sapi_module.name) == 0 || strcmp("phpdbg", sapi_module.name) == 0) {
+    if (strcmp("cli", sapi_module.name) == 0 || strcmp("phpdbg", sapi_module.name) == 0 || strcmp("micro", sapi_module.name) == 0 ) {
         SWOOLE_G(cli) = 1;
     }

文件 SHA256
micro_with_swoole462_php802_x64_linux_mod1.zip 627c9164e91afe1ea56d79dd280385f73ac969ac38026ce5db36d78c7afdb003

dixyes avatar Feb 07 '21 05:02 dixyes

谢谢。目前可以了,能搞一个Windows版本的吗?

xieyuhua avatar Feb 07 '21 06:02 xieyuhua

谢谢。目前可以了,能搞一个Windows版本的吗?

Swoole不支持windows,如果需要windows版,考虑Swow替代

dixyes avatar Feb 07 '21 06:02 dixyes

整一个撒,

xieyuhua avatar Feb 08 '21 01:02 xieyuhua

image

xieyuhua avatar Feb 16 '21 10:02 xieyuhua

WARNING swoole_timer_free: timer is not available

xieyuhua avatar Feb 16 '21 10:02 xieyuhua

哥,redis扩展也没有弄好呀

xieyuhua avatar Feb 16 '21 14:02 xieyuhua

试试这个

https://github.com/dixyes/build-my-own-phpmicro/releases/tag/20210218030458-a4f6aaf

最近么的电脑,稍晚点再考虑phpredis里lzf zstd lz4之类的依赖

dixyes avatar Feb 18 '21 03:02 dixyes

Fatal error: Uncaught Error: Call to undefined function Jobs\curl_init() in phar

xieyuhua avatar Feb 18 '21 08:02 xieyuhua

搞不来c++,不然我自己搞了

xieyuhua avatar Feb 18 '21 08:02 xieyuhua

redis好了, Deprecated: Method Swoole\Timer::set() is deprecated Fatal error: Uncaught Error: Call to undefined function Jobs\curl_init() in phar

xieyuhua avatar Feb 19 '21 07:02 xieyuhua

(讲道理我的这俩项目里一行C++都没有

curl整上了,做解析静态库依赖花了点时间。 至于swoole的弃用,你需要适配下swoole的api。

https://github.com/dixyes/build-my-own-phpmicro/releases/tag/20210220061013-a4f6aaf

dixyes avatar Feb 20 '21 06:02 dixyes

怎么设置php.ini里面内容??

xieyuhua avatar Feb 26 '21 06:02 xieyuhua

怎么设置php.ini里面内容??

https://github.com/longyan/phpmicro/wiki/INI-settings

dixyes avatar Feb 26 '21 06:02 dixyes

我按照你的步骤执行

在php源码目录下

patch -p1 api/micro/patches/disable_huge_page.patch patch -p1 api/micro/patches/zend_stream.patch 为啥不动呢,,

到是可以执行后面得步骤,,但是我编译出来的micro.sfx,结果感觉是我当前php环境,并不是我下载得源码php8.0版本得环境,我有点蒙了

xieyuhua avatar Mar 01 '21 07:03 xieyuhua