php-ext-xlswriter icon indicating copy to clipboard operation
php-ext-xlswriter copied to clipboard

Cannot be statically built

Open char101 opened this issue 2 years ago • 6 comments

Hi,

This extension cannot be statically built into php and can only be build using using phpize, because of several factors

  1. $srcdir should be $ext_srcdir, and $builddir should be $ext_builddir
  2. $ext_srcdir and $ext_builddir are defined by PHP_NEW_EXTENSION, before it we need to use PHP_EXT_SRCDIR and PHP_EXT_BUILDDIR macros.
diff --git a/config.m4 b/config.m4
index 0fb2694..d15f43d 100644
--- a/config.m4
+++ b/config.m4
@@ -111,7 +111,7 @@ if test "$PHP_XLSWRITER" != "no"; then
     else
         AC_MSG_RESULT([use the bundled library])
         xls_writer_sources="$xls_writer_sources $libxlsxwriter_sources"
-        PHP_ADD_INCLUDE([$srcdir/library/libxlsxwriter/include])
+        PHP_ADD_INCLUDE([PHP_EXT_SRCDIR/library/libxlsxwriter/include])

         dnl see library/CMakeLists.txt
         LIBOPT="-DNOCRYPT -DNOUNCRYPT"
@@ -148,13 +148,13 @@ if test "$PHP_XLSWRITER" != "no"; then
             AC_MSG_RESULT([use the bundled library])

             xls_writer_sources="$xls_writer_sources $libexpat"
-            PHP_ADD_INCLUDE([$srcdir/library/libexpat/expat/lib])
-            PHP_ADD_BUILD_DIR([$abs_builddir/library/libexpat/expat/lib])
+            PHP_ADD_INCLUDE([PHP_EXT_SRCDIR/library/libexpat/expat/lib])
+            PHP_ADD_BUILD_DIR([PHP_EXT_BUILDDIR/library/libexpat/expat/lib])
             LIBOPT="$LIBOPT -DXML_POOR_ENTROPY"

             xls_writer_sources="$xls_writer_sources $libxlsxio"
-            PHP_ADD_INCLUDE([$srcdir/library/libxlsxio/include])
-            PHP_ADD_BUILD_DIR([$abs_builddir/library/libxlsxio/lib])
+            PHP_ADD_INCLUDE([PHP_EXT_SRCDIR/library/libxlsxio/include])
+            PHP_ADD_BUILD_DIR([PHP_EXT_BUILDDIR/library/libxlsxio/lib])
             LIBOPT="$LIBOPT -DUSE_MINIZIP"

         fi
@@ -175,8 +175,8 @@ if test "$PHP_XLSWRITER" != "no"; then

     PHP_NEW_EXTENSION(xlswriter, $xls_writer_sources, $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1 $LIBOPT)

-    PHP_ADD_INCLUDE([$srcdir])
-    PHP_ADD_INCLUDE([$srcdir/include])
+    PHP_ADD_INCLUDE([$ext_srcdir])
+    PHP_ADD_INCLUDE([$ext_srcdir/include])

     PHP_ADD_BUILD_DIR([$abs_builddir/kernel])
     PHP_ADD_BUILD_DIR([$abs_builddir/library/libxlsxwriter/src])

char101 avatar Jul 29 '21 06:07 char101

Hi @char101

What is the reason for the need for static build? Can PECL meet the demand?

viest avatar Jul 29 '21 14:07 viest

Because it is supported by PHP.

https://www.php.net/manual/en/install.pecl.static.php

In my case I want to compile a static php binary so that it can be deployed along the application.

There is no fix required for this. I only put it here since other people who want to compile the extension statically might find it useful.

If you don't want to support the case of statically linking the extension please close the issue.

char101 avatar Jul 29 '21 14:07 char101

I think this benefit may be small.

Even if compiled statically, the binary file still depends on the external library, and you cannot directly copy the executable file and run it anywhere.

Or you have a way to solve this problem, if you don’t mind, I would love to know your working method, thank you.

viest avatar Jul 29 '21 16:07 viest

The benefit of a static php binary is exactly that it does not depend on any shared library and can be just copied to and run in another machine. In fact I have just compiled a static php executable with this extension embedded statically.

mini:~/compile/php-static/dist-8.0.8$ ls -alh bin
total 28M
drwxr-xr-x    2 char     char        4.0K Jul 29 14:38 .
drwxr-xr-x   10 char     char        4.0K Jul 28 18:54 ..
lrwxrwxrwx    1 char     char           9 Jul 29 14:28 phar -> phar.phar
-rwxr-xr-x    1 char     char       15.0K Jul 29 14:28 phar.phar
-rwxr-xr-x    1 char     char       32.1M Jul 29 14:38 php
-rwxr-xr-x    1 char     char        3.2K Jul 29 14:28 php-config
-rwxr-xr-x    1 char     char        4.5K Jul 29 14:28 phpize

mini:~/compile/php-static/dist-8.0.8$ file bin/php
bin/php: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, stripped

mini:~/compile/php-static/dist-8.0.8$ ./bin/php -i | rg -i xls
Configure Command =>  './configure'  '--prefix=/home/char/compile/php-static/dist-8.0.8' '--enable-re2c-cgoto' '--enable-fpm' '--disable-cgi' '--disable-ipv6' '--disable-phpdbg' '--with-openssl' '--with-pcre-jit' '--with-zlib' '--with-bz2' '--with-curl' '--enable-exif' '--enable-gd' '--with-jpeg' '--with-freetype''--with-ldap' '--enable-mbstring' '--with-mysqli' '--enable-pcntl' '--with-pdo-mysql' '--with-pdo-pgsql' '--with-pgsql' '--enable-soap' '--enable-sockets' '--with-zip' '--without-pear' '--enable-static' '--disable-shared' '--with-event-core' '--enable-redis' '--with-yaml' '--with-xlswriter'
xlswriter
xlswriter support => enabled
bundled libxlsxwriter version => 1.0.0
bundled libxlsxio version => 0.2.27

There is also this repository https://github.com/crazywhalecc/static-php-cli that provides a build script although I don't use it (I use my own build script).

char101 avatar Jul 29 '21 16:07 char101

Wow, this is great, are you interested in making your script public?

viest avatar Jul 30 '21 01:07 viest

It is not a simple script, there are several alpine linux packages that need to be rebuild and there are some extensions that need to be modified.

char101 avatar Jul 30 '21 02:07 char101

@viest xlsWriter是一个非常好的项目,我们在我们的代码里广泛使用了xlsWriter。 swoole-cli 也是一个非常好的项目,便于项目部署和分发。代码上线时只需要部署一个 cli 文件,就包含了php环境以及数以万计的文件。 请问能否对兼容swoole-cli进行支持?

jatyPeng avatar May 13 '23 03:05 jatyPeng

静态编译报错信息:

n file included from main/internal_functions_cli.c:57:
In file included from /work/php-src/ext/xlswriter/php_xlswriter.h:16:
In file included from /work/php-src/ext/xlswriter/include/xlswriter.h:34:
/work/php-src/ext/xlswriter/include/excel.h:48:1: warning: type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int [-Wimplicit-int]
VTIFUL_STARTUP_FUNCTION(excel);
^
int
/work/php-src/ext/xlswriter/include/excel.h:48:25: error: a parameter list without types is only allowed in a function definition
VTIFUL_STARTUP_FUNCTION(excel);
                        ^
In file included from main/internal_functions_cli.c:57:
In file included from /work/php-src/ext/xlswriter/php_xlswriter.h:16:
In file included from /work/php-src/ext/xlswriter/include/xlswriter.h:35:
/work/php-src/ext/xlswriter/include/validation.h:18:1: warning: type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int [-Wimplicit-int]
VTIFUL_STARTUP_FUNCTION(validation);
^
int
/work/php-src/ext/xlswriter/include/validation.h:18:25: error: a parameter list without types is only allowed in a function definition
VTIFUL_STARTUP_FUNCTION(validation);
                        ^
In file included from main/internal_functions_cli.c:57:
In file included from /work/php-src/ext/xlswriter/php_xlswriter.h:16:
In file included from /work/php-src/ext/xlswriter/include/xlswriter.h:36:
/work/php-src/ext/xlswriter/include/exception.h:18:1: warning: type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int [-Wimplicit-int]
VTIFUL_STARTUP_FUNCTION(exception);
^
int
/work/php-src/ext/xlswriter/include/exception.h:18:25: error: a parameter list without types is only allowed in a function definition
VTIFUL_STARTUP_FUNCTION(exception);
                        ^
In file included from main/internal_functions_cli.c:57:
In file included from /work/php-src/ext/xlswriter/php_xlswriter.h:16:
In file included from /work/php-src/ext/xlswriter/include/xlswriter.h:37:
/work/php-src/ext/xlswriter/include/format.h:18:1: warning: type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int [-Wimplicit-int]
VTIFUL_STARTUP_FUNCTION(format);
^
int
/work/php-src/ext/xlswriter/include/format.h:18:25: error: a parameter list without types is only allowed in a function definition
VTIFUL_STARTUP_FUNCTION(format);
                        ^
In file included from main/internal_functions_cli.c:57:
In file included from /work/php-src/ext/xlswriter/php_xlswriter.h:16:
In file included from /work/php-src/ext/xlswriter/include/xlswriter.h:38:
/work/php-src/ext/xlswriter/include/chart.h:18:1: warning: type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int [-Wimplicit-int]
VTIFUL_STARTUP_FUNCTION(chart);
^
int
/work/php-src/ext/xlswriter/include/chart.h:18:25: error: a parameter list without types is only allowed in a function definition
VTIFUL_STARTUP_FUNCTION(chart);
                        ^
In file included from main/internal_functions_cli.c:57:
In file included from /work/php-src/ext/xlswriter/php_xlswriter.h:16:
In file included from /work/php-src/ext/xlswriter/include/xlswriter.h:39:
/work/php-src/ext/xlswriter/include/rich_string.h:18:1: warning: type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int [-Wimplicit-int]
VTIFUL_STARTUP_FUNCTION(rich_string);
^
int
/work/php-src/ext/xlswriter/include/rich_string.h:18:25: error: a parameter list without types is only allowed in a function definition
VTIFUL_STARTUP_FUNCTION(rich_string);
                        ^
In file included from main/internal_functions_cli.c:57:
In file included from /work/php-src/ext/xlswriter/php_xlswriter.h:16:
In file included from /work/php-src/ext/xlswriter/include/xlswriter.h:43:
/work/php-src/ext/xlswriter/library/libxlsxio/include/xlsxio_read.h:92:67: warning: a function declaration without a prototype is deprecated in all versions of C [-Wstrict-prototypes]
DLL_EXPORT_XLSXIO const XLSXIOCHAR* xlsxioread_get_version_string ();
                                                                  ^
                                                                   void
7 warnings and 6 errors generated.
make: *** [Makefile:2104: main/internal_functions_cli.lo] Error 1

jingjingxyk avatar May 13 '23 03:05 jingjingxyk

静态编译命令:

git clone -b php-8.2.4 --depth=1   https://github.com/php/php-src.git
git clone -b v1.5.4 --depth=1 --recursive https://github.com/viest/php-ext-xlswriter.git


mv php-ext-xlswriter xlswriter

mv xlswriter php-src/ext/


cd php-src/

./buildconf --force

export CPPFLAGS=' -I/usr/libiconv/include -I/usr/bzip2/include -I/usr/zlib/include -I/usr/libxml2/include/libxml2 -I/usr/libiconv/include '

export LDFLAGS=' -L/usr/libiconv/lib -L/usr/bzip2/lib -L/usr/zlib/lib -L/usr/libxml2/lib -L/usr/libiconv/lib '

export LIBS=' -liconv -lbz2 -lz -lxml2 -liconv -lm -lstdc++'

./configure --prefix=/tmp/php-8.2.4 \
 --disable-all \
--disable-cgi  \
--disable-phpdbg \
--enable-shared=no \
--enable-static=yes \
--enable-cli  \
--with-iconv=/usr/libiconv \
--with-bz2=/usr/bzip2 \
--enable-filter \
--enable-session \
--enable-tokenizer \
--enable-ctype \
--with-zlib --with-zlib-dir=/usr/zlib \
--enable-posix \
--enable-phar \
--enable-fileinfo \
--enable-xml --enable-simplexml --enable-xmlreader --enable-xmlwriter --enable-dom --with-libxml \
 --with-xlswriter --enable-reader

 export LDFLAGS="$LDFLAGS -all-static"

 make -j $(nproc)  cli

命令来自: https://github.com/jingjingxyk/swoole-cli/tree/build_native_php

jingjingxyk avatar May 13 '23 04:05 jingjingxyk