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.29k stars 226 forks source link

help with install `swoole\ext-postgresql` #132

Closed kocoten1992 closed 1 year ago

kocoten1992 commented 1 year ago

Hi @crazywhalecc , I'm trying to install https://github.com/swoole/ext-postgresql, but I cannot pass this check:

libpq libraries was not found.
set LIBPQ_CFLAGS and LIBPQ_LIBS environment or
install following package to obtain them:
libpq-dev (for debian and its varients)
postgresql-devel (for rhel varients)
libpq-devel (for newer fedora)
postgresql-libs (for arch and its varients)
postgresql-dev (for alpine)
postgresql (for homebrew)
configure: error: postgresql support needs libpq libraries to build

I was trying to use Docker (alpine linux) to build static cli with swoole_postgresql extension:

./bin/spc build "swoole_postgresql" --build-cli --debug

I already did this:

--- a/config/ext.json
+++ b/config/ext.json
@@ -380,6 +380,19 @@
         ],
         "unix-only": true
     },
+    "swoole_postgresql": {
+        "type": "external",
+        "source": "swoole_postgresql",
+        "arg-type": "custom",
+        "cpp-extension": true,
+        "lib-depends": [
+            "postgresql"
+        ],
+        "ext-depends": [
+            "swoole"
+        ],
+        "unix-only": true
+    },
     "swow": {
         "type": "external",
         "source": "swow",

```diff
--- a/config/source.json
+++ b/config/source.json
@@ -418,6 +418,16 @@
             "path": "LICENSE"
         }
     },
+    "swoole_postgresql": {
+        "type": "git",
+        "path": "php-src/ext/swoole_postgresql",
+        "rev": "master",
+        "url": "https://github.com/swoole/ext-postgresql",
+        "license": {
+            "type": "text",
+            "text": "This source file is subject to version 2.0 of the Apache license,\n that is bundled with this package in the file LICENSE, and is\n available through the world-wide-web at the following url:\n http://www.apache.org/licenses/LICENSE-2.0.html\n If you did not receive a copy of the Apache2.0 license and are unable\n to obtain it through the world-wide-web, please send a note to\n license@swoole.com so we can mail you a copy immediately."
+        }
+    },
     "swow": {
         "type": "git",
         "path": "php-src/ext/swow-src",

But cannot pass libpq check (I already install package as suggestion - but it does not work), do you have any idea?

crazywhalecc commented 1 year ago

I'm not sure if swoole/ext-postgresql will work because it's archived. But for pgsql, it needs patches before ./configure. And you also set "arg-type": "custom", then you need to create a class in src/SPC/builder/extension/, with Attribute #[CustomExt('swoole_postgresql')]. You can refer to the code of pgsql extension:

https://github.com/crazywhalecc/static-php-cli/blob/9c57ed6439d6c836b8ecc60bbebcb69b90376742/src/SPC/builder/extension/pgsql.php#L18

To put it simply, if all libraries that have been successfully compiled cannot be found in the PHP configure stage, most of them can be solved by replacing the parameters with files.

kocoten1992 commented 1 year ago

I dig deeper and I found this https://github.com/swoole/swoole-src/blob/b43bff854d00e77d25e4c5052ec27e3f8dda6fdd/.github/workflows/framework.yml#L34

I try to add --enable-swoole-pgsql directly inside https://github.com/crazywhalecc/static-php-cli/blob/main/src/SPC/builder/extension/swoole.php, it seem to work, what is your thought about add that functionality --enable-swoole-pgsql.

If you agree I'll craft a PR adding --enable-swoole-pgsql, or let me know if you have a better way.

jingjingxyk commented 1 year ago

@kocoten1992 reference

https://github.com/jingjingxyk/swoole-cli/blob/3d833ba24bf2847ad80cf1db10f6bb54411b3847/sapi/src/builder/extension/swoole.php#L10C6-L10C6

https://wiki.swoole.com/#/coroutine_client/init?id=%e5%8d%8f%e7%a8%8b%e5%ae%a2%e6%88%b7%e7%ab%af-lt-docsify-ignore-all-gt

https://wiki.swoole.com/#/coroutine_client/postgresql

use LIBS CPPFLAGS LDFLAGS

https://github.com/crazywhalecc/static-php-cli/issues/86

https://github.com/crazywhalecc/static-php-cli/issues/86#issuecomment-1637335319


CPPFLAGS=$(pkg-config --cflags-only-I --static libpq)                                                                                                                                  
LDFLAGS=$(pkg-config --libs-only-L   --static libpq)                                                                                                                                  
LIBS=$(pkg-config --libs-only-l   --static libpq)
crazywhalecc commented 1 year ago

Just feel free to create PR, and I will check it locally and run some integration tests.

jingjingxyk commented 1 year ago

need add pkg-config

export_variables() {
    # -all-static | -static | -static-libtool-libs
    CPPFLAGS=""
    CFLAGS=""
    LDFLAGS="-static"

    LDFLAGS=""
    LIBS=""
    CPPFLAGS="$CPPFLAGS -I/usr/local/swoole-cli/libiconv/include"
    LDFLAGS="$LDFLAGS -L/usr/local/swoole-cli/libiconv/lib"
    LIBS="$LIBS -liconv"
    CPPFLAGS="$CPPFLAGS -I/usr/local/swoole-cli/bzip2/include"
    LDFLAGS="$LDFLAGS -L/usr/local/swoole-cli/bzip2/lib"
    LIBS="$LIBS -lbz2"
    PACKAGES="libcrypto libssl openssl zlib libxml-2.0 libssh2 libpq icu-i18n icu-io icu-uc readline ncursesw libexslt libxslt libzstd liblz4 liblz4 liblzma"
    CPPFLAGS="$CPPFLAGS $(pkg-config --cflags-only-I --static $PACKAGES ) "
    LDFLAGS="$LDFLAGS $(pkg-config --libs-only-L --static $PACKAGES ) "
    LIBS="$LIBS $(pkg-config --libs-only-l --static $PACKAGES ) -lstdc++"
    export  LIBPQ_CFLAGS="$(pkg-config  --cflags --static libpq)"
    export  LIBPQ_LIBS="$(pkg-config    --libs   --static libpq)"
    export  CPPFLAGS="$CPPFLAGS"
    export  LDFLAGS="$LDFLAGS"
    export  LIBS="$LIBS"

}

https://github.com/jingjingxyk/swoole-cli/blob/2d37258bc0d5ef25cce6d1ff322e361b45ab68ba/sapi/src/template/make.php#L129

or

https://github.com/jingjingxyk/static-php-cli/blob/75cda157a83df98ecb8b5e1259bab1c9779fdff0/src/SPC/builder/linux/LinuxBuilder.php#L174