Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

swoole扩展怎么编译进去 #1

Open
xieyuhua opened this issue Feb 6, 2021 · 19 comments
Open

swoole扩展怎么编译进去 #1

xieyuhua opened this issue Feb 6, 2021 · 19 comments

Comments

@xieyuhua
Copy link

xieyuhua commented Feb 6, 2021

swoole扩展怎么编译进去

@dixyes
Copy link
Collaborator

dixyes commented Feb 6, 2021

像一般的扩展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的大小

@xieyuhua
Copy link
Author

xieyuhua commented Feb 6, 2021

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

@dixyes
Copy link
Collaborator

dixyes commented Feb 6, 2021

我试着编译了 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

@xieyuhua
Copy link
Author

xieyuhua commented Feb 7, 2021

[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]#

@dixyes
Copy link
Collaborator

dixyes commented Feb 7, 2021

看来是做了硬编码的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

@xieyuhua
Copy link
Author

xieyuhua commented Feb 7, 2021

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

@dixyes
Copy link
Collaborator

dixyes commented Feb 7, 2021

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

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

@xieyuhua
Copy link
Author

xieyuhua commented Feb 8, 2021

整一个撒,

@xieyuhua
Copy link
Author

image

@xieyuhua
Copy link
Author

WARNING swoole_timer_free: timer is not available

@xieyuhua
Copy link
Author

哥,redis扩展也没有弄好呀

@dixyes
Copy link
Collaborator

dixyes commented Feb 18, 2021

试试这个

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

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

@xieyuhua
Copy link
Author

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

@xieyuhua
Copy link
Author

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

@xieyuhua
Copy link
Author

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

@dixyes
Copy link
Collaborator

dixyes commented Feb 20, 2021

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

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

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

@xieyuhua
Copy link
Author

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

@dixyes
Copy link
Collaborator

dixyes commented Feb 26, 2021

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

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

@xieyuhua
Copy link
Author

xieyuhua commented Mar 1, 2021

我按照你的步骤执行

在php源码目录下

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants