From 2e56f441feb2a2d34c7972787a4985e5e693a214 Mon Sep 17 00:00:00 2001 From: crazywhalecc Date: Fri, 13 Dec 2024 14:37:27 +0800 Subject: [PATCH] Addd embed docs --- docs/en/guide/manual-build.md | 73 +++++++++++++++++++++++++++++++++++ docs/zh/guide/manual-build.md | 72 ++++++++++++++++++++++++++++++++++ 2 files changed, 145 insertions(+) diff --git a/docs/en/guide/manual-build.md b/docs/en/guide/manual-build.md index 74d75118..a0b02b08 100644 --- a/docs/en/guide/manual-build.md +++ b/docs/en/guide/manual-build.md @@ -553,3 +553,76 @@ If you need to build multiple times locally, the following method can save you t - If you want to rebuild once, but do not re-download the source code, you can first `rm -rf buildroot source` to delete the compilation directory and source code directory, and then rebuild. - If you want to update a version of a dependency, you can use `bin/spc del-download ` to delete the specified source code, and then use `download ` to download it again. - If you want to update all dependent versions, you can use `bin/spc download --clean` to delete all downloaded sources, and then download them again. + +## embed usage + +If you want to embed static-php into other C language programs, you can use `--build-embed` to build an embed version of PHP. + +```bash +bin/spc build {your extensions} --build-embed --debug +``` + +Under normal circumstances, PHP embed will generate `php-config` after compilation. +For static-php, we provide `spc-config` to obtain the parameters during compilation. +In addition, when using embed SAPI (libphp.a), you need to use the same compiler as libphp, otherwise there will be a link error. + +Here is the basic usage of spc-config: + +```bash +# output all flags and options +bin/spc spc-config curl,zlib,phar,openssl + +# output libs +bin/spc spc-config curl,zlib,phar,openssl --libs + +# output includes +bin/spc spc-config curl,zlib,phar,openssl --includes +``` + +By default, static-php uses the following compilers on different systems: + +- macOS: `clang` +- Linux (Alpine Linux): `gcc` +- Linux (glibc based distros, x86_64): `/usr/local/musl/bin/x86_64-linux-musl-gcc` +- Linux (glibc based distros, aarch64): `/usr/local/musl/bin/aarch64-linux-musl-gcc` +- FreeBSD: `clang` + +Here is an example of using embed SAPI: + +```c +// embed.c +#include + +int main(int argc,char **argv){ + + PHP_EMBED_START_BLOCK(argc,argv) + + zend_file_handle file_handle; + + zend_stream_init_filename(&file_handle,"embed.php"); + + if(php_execute_script(&file_handle) == FAILURE){ + php_printf("Failed to execute PHP script.\n"); + } + + PHP_EMBED_END_BLOCK() + return 0; +} +``` + + +```php +` 删除指定的源码,然后使用 `download ` 重新下载。 - 如果你想更新所有依赖的版本,可以使用 `bin/spc download --clean` 删除所有下载的源码,然后重新下载。 + +## embed 使用 + +如果你想将 static-php 嵌入到其他 C 语言程序中,可以使用 `--build-embed` 构建一个 embed 版本的 PHP。 + +```bash +bin/spc build {your extensions} --build-embed --debug +``` + +在通常的情况下,PHP embed 编译后会生成 `php-config`。对于 static-php,我们提供了 `spc-config`,用于获取编译时的参数。 +另外,在使用 embed SAPI(libphp.a)时,你需要使用和编译 libphp 相同的编译器,否则会出现链接错误。 + +下面是 spc-config 的基本用法: + +```bash +# output all flags and options +bin/spc spc-config curl,zlib,phar,openssl + +# output libs +bin/spc spc-config curl,zlib,phar,openssl --libs + +# output includes +bin/spc spc-config curl,zlib,phar,openssl --includes +``` + +默认情况下,static-php 在不同系统使用的编译器分别是: + +- macOS: `clang` +- Linux (Alpine Linux): `gcc` +- Linux (glibc based distros, x86_64): `/usr/local/musl/bin/x86_64-linux-musl-gcc` +- Linux (glibc based distros, aarch64): `/usr/local/musl/bin/aarch64-linux-musl-gcc` +- FreeBSD: `clang` + +下面是一个使用 embed SAPI 的例子: + +```c +// embed.c +#include + +int main(int argc,char **argv){ + + PHP_EMBED_START_BLOCK(argc,argv) + + zend_file_handle file_handle; + + zend_stream_init_filename(&file_handle,"embed.php"); + + if(php_execute_script(&file_handle) == FAILURE){ + php_printf("Failed to execute PHP script.\n"); + } + + PHP_EMBED_END_BLOCK() + return 0; +} +``` + + +```php +