Skip to content

Commit

Permalink
Addd embed docs
Browse files Browse the repository at this point in the history
  • Loading branch information
crazywhalecc committed Dec 13, 2024
1 parent c42b98b commit 2e56f44
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 0 deletions.
73 changes: 73 additions & 0 deletions docs/en/guide/manual-build.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <source-name>` to delete the specified source code, and then use `download <source-name>` 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 <sapi/embed/php_embed.h>

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
<?php
// embed.php
echo "Hello world!\n";
```

```bash
# compile in debian/ubuntu x86_64
/usr/local/musl/bin/x86_64-linux-musl-gcc embed.c $(bin/spc spc-config bcmath,zlib) -static -o embed
# compile in macOS/FreeBSD
clang embed.c $(bin/spc spc-config bcmath,zlib) -o embed

./embed
# out: Hello world!
```
72 changes: 72 additions & 0 deletions docs/zh/guide/manual-build.md
Original file line number Diff line number Diff line change
Expand Up @@ -493,3 +493,75 @@ static-php-cli 开放的方法非常多,文档中无法一一列举,但只
- 如果你想重新构建一次,但不重新下载源码,可以先 `rm -rf buildroot source` 删除编译目录和源码目录,然后重新构建。
- 如果你想更新某个依赖的版本,可以使用 `bin/spc del-download <source-name>` 删除指定的源码,然后使用 `download <source-name>` 重新下载。
- 如果你想更新所有依赖的版本,可以使用 `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 <sapi/embed/php_embed.h>

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
<?php
// embed.php
echo "Hello world!\n";
```

```bash
# compile in debian/ubuntu x86_64
/usr/local/musl/bin/x86_64-linux-musl-gcc embed.c $(bin/spc spc-config bcmath,zlib) -static -o embed
# compile in macOS/FreeBSD
clang embed.c $(bin/spc spc-config bcmath,zlib) -o embed

./embed
# out: Hello world!
```

0 comments on commit 2e56f44

Please sign in to comment.