Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit dbe7470

Browse files
committed
an approach to embedding main() into sapi/embed from php code
1 parent cb9af8d commit dbe7470

File tree

2 files changed

+99
-3
lines changed

2 files changed

+99
-3
lines changed

‎sapi/embed/config.m4

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
11
PHP_ARG_ENABLE([embed],,
22
[AS_HELP_STRING([[--enable-embed[=TYPE]]],
33
[Enable building of embedded SAPI library TYPE is either
4-
'shared' or 'static'. [TYPE=shared]])],
4+
'shared' or 'static' or 'main'. [TYPE=shared]])],
5+
[no],
6+
[no])
7+
8+
PHP_ARG_WITH([embed-main],,
9+
[AS_HELP_STRING([[--enable-embed-main[=FILE]]],
10+
[Enable building of embedded SAPI as a program with embedded main
11+
[TYPE=no]])],
512
[no],
613
[no])
714

815
AC_MSG_CHECKING([for embedded SAPI library support])
916

10-
if test "$PHP_EMBED" != "no"; then
17+
if test "$PHP_EMBED" != "no" || test "$PHP_EMBED_MAIN" != "no"; then
18+
19+
if test "$PHP_EMBED" == "main"; then
20+
if test "$PHP_EMBED_MAIN" == "no"; then
21+
AC_MSG_ERROR([--with-embed-main is required for --enable-embed=main])
22+
fi
23+
elif test "$PHP_EMBED_MAIN" != "no"; then
24+
PHP_EMBED=main
25+
fi
26+
1127
AS_CASE([$PHP_EMBED],
1228
[yes|shared], [
1329
LIBPHP_CFLAGS="-shared"
@@ -22,6 +38,43 @@ if test "$PHP_EMBED" != "no"; then
2238
PHP_EMBED_TYPE=static
2339
INSTALL_IT="\$(mkinstalldirs) \$(INSTALL_ROOT)\$(orig_libdir); \$(INSTALL) -m 0644 $SAPI_STATIC \$(INSTALL_ROOT)\$(orig_libdir)"
2440
],
41+
[main], [
42+
AC_MSG_RESULT([main])
43+
AC_PATH_PROG([XXD], [xxd])
44+
if test ! -x "$XXD"; then
45+
AC_MSG_ERROR([xxd not found, required for --with-embed-main])
46+
fi
47+
AC_MSG_CHECKING([for access to $PHP_EMBED_MAIN])
48+
if test ! -f "$PHP_EMBED_MAIN"; then
49+
AC_MSG_ERROR([--with-embed-main argument "$PHP_EMBED_MAIN" not found])
50+
else
51+
AC_MSG_RESULT([ok])
52+
fi
53+
54+
AC_MSG_CHECKING([generating sapi/embed/php_embed_main.h])
55+
$XXD -i -n php_embed_main "$PHP_EMBED_MAIN" sapi/embed/php_embed_main.h
56+
if test $? -ne 0; then
57+
AC_MSG_ERROR([failed])
58+
else
59+
AC_MSG_RESULT([ok])
60+
fi
61+
62+
AC_MSG_CHECKING([proceeding to final configure of embed])
63+
LIBPHP_CFLAGS="-static"
64+
PHP_EMBED_TYPE=static
65+
INSTALL_IT="\
66+
\$(mkinstalldirs) \$(INSTALL_ROOT)\$(orig_libdir); \
67+
\$(INSTALL) -m 0644 \$(SAPI_STATIC) \$(INSTALL_ROOT)\$(orig_libdir); \
68+
\$(mkinstalldirs) \$(INSTALL_ROOT)\$(bindir); \
69+
\$(LIBTOOL) --tag=CC --mode=compile \$(CC) \
70+
\$(CFLAGS_CLEAN) \$(EXTRA_CFLAGS) \$(INCLUDES) -DHAVE_EMBED_MAIN \
71+
-c sapi/embed/php_embed.c -o sapi/embed/php_embed_main.lo; \
72+
\$(LIBTOOL) --tag=CC --mode=link \$(CC) \
73+
-export-dynamic \$(EXTRA_LDFLAGS_PROGRAM) \$(LDFLAGS) \$(PHP_RPATHS) \
74+
sapi/embed/php_embed_main.lo \
75+
\$(PHP_GLOBAL_OBJS) \$(PHP_SAPI_OBJS) \$(EXTRA_LIBS) \$(ZEND_EXTRA_LIBS) \
76+
-o \$(INSTALL_ROOT)\$(bindir)/php-embed-main"
77+
],
2578
[PHP_EMBED_TYPE=])
2679

2780
AS_VAR_IF([PHP_EMBED_TYPE],, [AC_MSG_RESULT([no])], [
@@ -31,7 +84,7 @@ if test "$PHP_EMBED" != "no"; then
3184
PHP_SELECT_SAPI([embed],
3285
[$PHP_EMBED_TYPE],
3386
[php_embed.c],
34-
[-DZEND_ENABLE_STATIC_TSRMLS_CACHE=1])
87+
[-DZEND_ENABLE_STATIC_TSRMLS_CACHE=1 $LIBPHP_CFLAGS])
3588
PHP_INSTALL_HEADERS([sapi/embed], [php_embed.h])
3689
])
3790
else

‎sapi/embed/php_embed.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <fcntl.h>
2424
#endif
2525

26+
#ifndef HAVE_EMBED_MAIN
2627
static const char HARDCODED_INI[] =
2728
"html_errors=0\n"
2829
"register_argc_argv=1\n"
@@ -264,3 +265,45 @@ EMBED_SAPI_API void php_embed_shutdown(void)
264265
tsrm_shutdown();
265266
#endif
266267
}
268+
#endif
269+
270+
#ifdef HAVE_EMBED_MAIN
271+
# include "php_embed_main.h"
272+
273+
int main(int argc, char *argv[])
274+
{
275+
int exit_status = 0;
276+
277+
PHP_EMBED_START_BLOCK(argc, argv)
278+
279+
zend_string *_php_embed_main_ =
280+
zend_string_init(
281+
(const char*)
282+
php_embed_main,
283+
php_embed_main_len, 0);
284+
285+
zend_try {
286+
zend_op_array *ops =
287+
zend_compile_string(
288+
_php_embed_main_, "(main)", 0);
289+
290+
zval retval;
291+
ZVAL_UNDEF(&retval);
292+
293+
if (ops) {
294+
zend_execute(ops, &retval);
295+
}
296+
297+
destroy_op_array(ops);
298+
efree_size(ops, sizeof(zend_op_array));
299+
} zend_end_try();
300+
301+
exit_status = EG(exit_status);
302+
303+
zend_string_release(_php_embed_main_);
304+
305+
PHP_EMBED_END_BLOCK()
306+
307+
return exit_status;
308+
}
309+
#endif

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /