Skip to content

Conversation

krakjoe
Copy link
Member

@krakjoe krakjoe commented Sep 4, 2025

This is a possible approach to deploying embed with some php code embedded for main(), we don't have to require any special deps, or really make significant changes.

Options:

  • --with-embed-main takes path to php/phar file
  • --with-embed-link takes path for symlink creation (phar requires a symlink with the same name as the archive when it was created)

Possible improvements:

  • sigint (cleanup properly)
  • determine link name automagically
  • add ability to set hardcoded ini
  • do less nasty things in config.m4, formalize in makefile
  • set name of executable

Tested widely enough to make sure phar archives work, obvious this is very nix focused, I've no idea how to do this in a windows friendly way.

Example:

--with-embed-main=/path/to/pie.phar --with-embed-link=pie.phar creates:

krakjoe@fiji:/opt/src/php-src$ /opt/bin/php-embed-main build apcu/apcu
#!/usr/bin/env php
🥧 PHP Installer for Extensions (PIE) 1.2.0, from The PHP Foundation
You are running PHP 8.5.0-dev
Target PHP installation: 8.5.0-dev ts, on Linux/OSX/etc x86_64 (from /opt/bin/php)
Found package: apcu/apcu:v5.1.27 which provides ext-apcu
Extracted apcu/apcu:v5.1.27 source to: /home/krakjoe/.config/pie/php8.5_36ea40acb359a0ef916e847e11dafbd9/vendor/apcu/apcu
phpize complete.
Configure complete.
Running make without parallelization - try providing -jN to PIE where N is the number of cores you have.
Build complete: /home/krakjoe/.config/pie/php8.5_36ea40acb359a0ef916e847e11dafbd9/vendor/apcu/apcu/modules/apcu.so

\$(PHP_GLOBAL_OBJS) \$(PHP_SAPI_OBJS) \$(EXTRA_LIBS) \$(ZEND_EXTRA_LIBS) \
-o \$(INSTALL_ROOT)\$(bindir)/php-embed-main"
if test "$PHP_EMBED_LINK" != "no"; then
AC_DEFINE_UNQUOTED([PHP_EMBED_LINK], ["$PHP_EMBED_LINK"], [PHP_EMBED_LINK])
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this is yes/empty we could use basename of PHP_EMBED_MAIN, this will normally be correct.


SG(headers_sent) = 1;
SG(request_info).no_headers = 1;
php_register_variable("PHP_SELF", "-", NULL);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not exactly sure why this is here, it doesn't look correct ... I moved to register_server_variables because that's idiomatic, and allows consumers to control this behavior appropriately


static php_stream *s_in_process = NULL;

static int php_embed_main_streams(void) {
Copy link
Member Author

@krakjoe krakjoe Sep 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lifted from cli, possibly this should be exported API function in main ...

@henderkes
Copy link
Contributor

This is essentially the same as compiling embed=static followed by compiling and linking a simple C program that executes php code against the produced libphp.a, right?

https://github.com/easysoft/phpmicro goes for a different approach to provide the same functionality.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants