Skip to content

Commit 4fc487f

Browse files
authored
Make php-cli functionality available in embed build (#21385)
1 parent 7abd493 commit 4fc487f

12 files changed

Lines changed: 73 additions & 24 deletions

File tree

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ PHP NEWS
1616
. Fixed bug GH-22825 (DOMElement::setAttribute() fails silently when the DTD
1717
declares a default value for the attribute). (iliaal)
1818

19+
- Embed:
20+
. Made php-cli functionality available in embed builds. (henderkes)
21+
1922
- GMP:
2023
. Added gmp_prevprime(). (Weilin Du, David Carlier)
2124
. Fixed GMP power and shift operators to reject GMP right operands outside

UPGRADING.INTERNALS

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ PHP 8.6 INTERNALS UPGRADE NOTES
191191
strings from one or more buffers.
192192
. Added zend_string_ends_with() and related variants.
193193
. Added trait support for internal classes.
194+
. Added do_php_cli().
194195

195196
========================
196197
2. Build system changes
@@ -227,6 +228,10 @@ PHP 8.6 INTERNALS UPGRADE NOTES
227228
. Added a new function CHECK_HEADER() which is intended to be used instead of
228229
the CHECK_HEADER_ADD_INCLUDE().
229230

231+
- Embed:
232+
. The CLI SAPI can not be disabled when building the embed SAPI
233+
(--enable-embed is incompatible with --disable-cli).
234+
230235
========================
231236
3. Module changes
232237
========================
@@ -307,3 +312,6 @@ PHP 8.6 INTERNALS UPGRADE NOTES
307312

308313
- AG and SCNG are now allocated with ts_allocate_tls_id() and live in native
309314
__thread storage on ZTS builds.
315+
316+
- php-cli functionality is now available in embed builds via the do_php_cli()
317+
function.

sapi/cli/cli.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,7 @@ typedef struct php_cli_server_context {
5454
php_cli_mode mode;
5555
} php_cli_server_context;
5656

57+
/* this performs full cli-SAPI boot, loads modules, sets up TSRM and co. */
58+
extern PHP_CLI_API int do_php_cli(int argc, char *argv[]);
59+
5760
#endif /* CLI_H */

sapi/cli/cli_win32.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
#define PHP_CLI_WIN32_NO_CONSOLE 1
22
#include "php_cli.c"
3+
4+
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
5+
{
6+
return do_php_cli(__argc, __argv);
7+
}

sapi/cli/config.m4

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ PHP_ARG_ENABLE([cli],
55
[yes],
66
[no])
77

8+
dnl The embed SAPI requires the CLI sources for do_php_cli().
9+
if test "$PHP_EMBED" != "no" -a "$PHP_CLI" = "no"; then
10+
AC_MSG_ERROR([--enable-embed requires the CLI SAPI, do not use --disable-cli with --enable-embed])
11+
fi
12+
813
if test "$PHP_CLI" != "no"; then
914
AC_CHECK_FUNCS([setproctitle])
1015

@@ -32,9 +37,17 @@ if test "$PHP_CLI" != "no"; then
3237
dnl Select SAPI.
3338
PHP_SELECT_SAPI([cli],
3439
[program],
35-
[php_cli.c php_http_parser.c php_cli_server.c ps_title.c php_cli_process_title.c],
40+
[php_cli_main.c],
3641
[-DZEND_ENABLE_STATIC_TSRMLS_CACHE=1])
3742

43+
dnl Everything except the main() entry point, so that the embed SAPI can link
44+
dnl the same objects into libphp for do_php_cli().
45+
PHP_ADD_SOURCES_X([sapi/cli],
46+
[php_cli.c php_http_parser.c php_cli_server.c ps_title.c php_cli_process_title.c],
47+
[-DZEND_ENABLE_STATIC_TSRMLS_CACHE=1],
48+
[PHP_CLI_SHARED_OBJS])
49+
PHP_CLI_OBJS="$PHP_CLI_OBJS $PHP_CLI_SHARED_OBJS"
50+
3851
AS_CASE([$host_alias],
3952
[*aix*], [
4053
AS_VAR_IF([php_sapi_module], [shared], [

sapi/cli/config.w32

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@
33
ARG_ENABLE('cli', 'Build CLI version of PHP', 'yes');
44
ARG_ENABLE('cli-win32', 'Build console-less CLI version of PHP', 'no');
55

6+
// The embed SAPI requires the CLI sources for do_php_cli().
7+
if (PHP_EMBED != "no" && PHP_CLI != "yes") {
8+
ERROR("--enable-embed requires the CLI SAPI, do not use --disable-cli with --enable-embed");
9+
}
10+
611
if (PHP_CLI == "yes") {
7-
SAPI('cli', 'php_cli.c php_http_parser.c php_cli_server.c', 'php.exe', '/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1');
12+
SAPI('cli', 'php_cli.c php_cli_main.c php_http_parser.c php_cli_server.c', 'php.exe', '/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1');
813
ADD_SOURCES(configure_module_dirname, 'php_cli_process_title.c ps_title.c', 'cli');
914
ADD_FLAG("LIBS_CLI", "ws2_32.lib");
1015
ADD_FLAG("LIBS_CLI", "shell32.lib");

sapi/cli/php_cli.c

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,18 +1173,10 @@ static int do_cli(int argc, char **argv) /* {{{ */
11731173
}
11741174
/* }}} */
11751175

1176-
/* {{{ main */
1177-
#ifdef PHP_CLI_WIN32_NO_CONSOLE
1178-
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
1179-
#else
1180-
int main(int argc, char *argv[])
1181-
#endif
1176+
/* {{{ do_php_cli */
1177+
PHP_CLI_API int do_php_cli(int argc, char *argv[])
11821178
{
11831179
#if defined(PHP_WIN32)
1184-
# ifdef PHP_CLI_WIN32_NO_CONSOLE
1185-
int argc = __argc;
1186-
char **argv = __argv;
1187-
# endif
11881180
int num_args;
11891181
wchar_t **argv_wide;
11901182
char **argv_save = argv;
@@ -1387,6 +1379,6 @@ int main(int argc, char *argv[])
13871379
* exiting.
13881380
*/
13891381
cleanup_ps_args(argv);
1390-
exit(exit_status);
1382+
return exit_status;
13911383
}
13921384
/* }}} */

sapi/cli/php_cli_main.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| Copyright © The PHP Group and Contributors. |
4+
+----------------------------------------------------------------------+
5+
| This source file is subject to the Modified BSD License that is |
6+
| bundled with this package in the file LICENSE, and is available |
7+
| through the World Wide Web at <https://www.php.net/license/>. |
8+
| |
9+
| SPDX-License-Identifier: BSD-3-Clause |
10+
+----------------------------------------------------------------------+
11+
*/
12+
13+
#include "php.h"
14+
#include "cli.h"
15+
16+
int main(int argc, char *argv[])
17+
{
18+
return do_php_cli(argc, argv);
19+
}

sapi/embed/config.m4

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
PHP_ARG_ENABLE([embed],,
2-
[AS_HELP_STRING([[--enable-embed[=TYPE]]],
3-
[Enable building of embedded SAPI library TYPE is either
4-
'shared' or 'static'. [TYPE=shared]])],
5-
[no],
6-
[no])
7-
81
AC_MSG_CHECKING([for embedded SAPI library support])
92

103
if test "$PHP_EMBED" != "no"; then
@@ -33,6 +26,9 @@ if test "$PHP_EMBED" != "no"; then
3326
[php_embed.c],
3427
[-DZEND_ENABLE_STATIC_TSRMLS_CACHE=1])
3528
PHP_INSTALL_HEADERS([sapi/embed], [php_embed.h])
29+
30+
dnl Link the CLI objects into libphp for do_php_cli().
31+
PHP_SAPI_OBJS="$PHP_SAPI_OBJS $PHP_CLI_SHARED_OBJS"
3632
])
3733
else
3834
AC_MSG_RESULT([no])

sapi/embed/config.w32

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,8 @@ var PHP_EMBED_PGO = false;
66

77
if (PHP_EMBED != "no") {
88
SAPI('embed', 'php_embed.c', 'php' + PHP_VERSION + 'embed.lib', '/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1');
9+
ADD_SOURCES("sapi/cli", "php_cli.c php_http_parser.c php_cli_server.c ps_title.c php_cli_process_title.c", "embed", undefined, true);
10+
ADD_FLAG("LIBS_EMBED", "ws2_32.lib");
11+
ADD_FLAG("LIBS_EMBED", "shell32.lib");
912
PHP_INSTALL_HEADERS("sapi/embed", "php_embed.h");
1013
}

0 commit comments

Comments
 (0)