Skip to content

Commit 1bc7bc3

Browse files
committed
Addd embed docs
1 parent 8b9b707 commit 1bc7bc3

File tree

2 files changed

+145
-0
lines changed

2 files changed

+145
-0
lines changed

docs/en/guide/manual-build.md

+73
Original file line numberDiff line numberDiff line change
@@ -553,3 +553,76 @@ If you need to build multiple times locally, the following method can save you t
553553
- 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.
554554
- 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.
555555
- 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.
556+
557+
## embed usage
558+
559+
If you want to embed static-php into other C language programs, you can use `--build-embed` to build an embed version of PHP.
560+
561+
```bash
562+
bin/spc build {your extensions} --build-embed --debug
563+
```
564+
565+
Under normal circumstances, PHP embed will generate `php-config` after compilation.
566+
For static-php, we provide `spc-config` to obtain the parameters during compilation.
567+
In addition, when using embed SAPI (libphp.a), you need to use the same compiler as libphp, otherwise there will be a link error.
568+
569+
Here is the basic usage of spc-config:
570+
571+
```bash
572+
# output all flags and options
573+
bin/spc spc-config curl,zlib,phar,openssl
574+
575+
# output libs
576+
bin/spc spc-config curl,zlib,phar,openssl --libs
577+
578+
# output includes
579+
bin/spc spc-config curl,zlib,phar,openssl --includes
580+
```
581+
582+
By default, static-php uses the following compilers on different systems:
583+
584+
- macOS: `clang`
585+
- Linux (Alpine Linux): `gcc`
586+
- Linux (glibc based distros, x86_64): `/usr/local/musl/bin/x86_64-linux-musl-gcc`
587+
- Linux (glibc based distros, aarch64): `/usr/local/musl/bin/aarch64-linux-musl-gcc`
588+
- FreeBSD: `clang`
589+
590+
Here is an example of using embed SAPI:
591+
592+
```c
593+
// embed.c
594+
#include <sapi/embed/php_embed.h>
595+
596+
int main(int argc,char **argv){
597+
598+
PHP_EMBED_START_BLOCK(argc,argv)
599+
600+
zend_file_handle file_handle;
601+
602+
zend_stream_init_filename(&file_handle,"embed.php");
603+
604+
if(php_execute_script(&file_handle) == FAILURE){
605+
php_printf("Failed to execute PHP script.\n");
606+
}
607+
608+
PHP_EMBED_END_BLOCK()
609+
return 0;
610+
}
611+
```
612+
613+
614+
```php
615+
<?php
616+
// embed.php
617+
echo "Hello world!\n";
618+
```
619+
620+
```bash
621+
# compile in debian/ubuntu x86_64
622+
/usr/local/musl/bin/x86_64-linux-musl-gcc embed.c $(bin/spc spc-config bcmath,zlib) -static -o embed
623+
# compile in macOS/FreeBSD
624+
clang embed.c $(bin/spc spc-config bcmath,zlib) -o embed
625+
626+
./embed
627+
# out: Hello world!
628+
```

docs/zh/guide/manual-build.md

+72
Original file line numberDiff line numberDiff line change
@@ -493,3 +493,75 @@ static-php-cli 开放的方法非常多,文档中无法一一列举,但只
493493
- 如果你想重新构建一次,但不重新下载源码,可以先 `rm -rf buildroot source` 删除编译目录和源码目录,然后重新构建。
494494
- 如果你想更新某个依赖的版本,可以使用 `bin/spc del-download <source-name>` 删除指定的源码,然后使用 `download <source-name>` 重新下载。
495495
- 如果你想更新所有依赖的版本,可以使用 `bin/spc download --clean` 删除所有下载的源码,然后重新下载。
496+
497+
## embed 使用
498+
499+
如果你想将 static-php 嵌入到其他 C 语言程序中,可以使用 `--build-embed` 构建一个 embed 版本的 PHP。
500+
501+
```bash
502+
bin/spc build {your extensions} --build-embed --debug
503+
```
504+
505+
在通常的情况下,PHP embed 编译后会生成 `php-config`。对于 static-php,我们提供了 `spc-config`,用于获取编译时的参数。
506+
另外,在使用 embed SAPI(libphp.a)时,你需要使用和编译 libphp 相同的编译器,否则会出现链接错误。
507+
508+
下面是 spc-config 的基本用法:
509+
510+
```bash
511+
# output all flags and options
512+
bin/spc spc-config curl,zlib,phar,openssl
513+
514+
# output libs
515+
bin/spc spc-config curl,zlib,phar,openssl --libs
516+
517+
# output includes
518+
bin/spc spc-config curl,zlib,phar,openssl --includes
519+
```
520+
521+
默认情况下,static-php 在不同系统使用的编译器分别是:
522+
523+
- macOS: `clang`
524+
- Linux (Alpine Linux): `gcc`
525+
- Linux (glibc based distros, x86_64): `/usr/local/musl/bin/x86_64-linux-musl-gcc`
526+
- Linux (glibc based distros, aarch64): `/usr/local/musl/bin/aarch64-linux-musl-gcc`
527+
- FreeBSD: `clang`
528+
529+
下面是一个使用 embed SAPI 的例子:
530+
531+
```c
532+
// embed.c
533+
#include <sapi/embed/php_embed.h>
534+
535+
int main(int argc,char **argv){
536+
537+
PHP_EMBED_START_BLOCK(argc,argv)
538+
539+
zend_file_handle file_handle;
540+
541+
zend_stream_init_filename(&file_handle,"embed.php");
542+
543+
if(php_execute_script(&file_handle) == FAILURE){
544+
php_printf("Failed to execute PHP script.\n");
545+
}
546+
547+
PHP_EMBED_END_BLOCK()
548+
return 0;
549+
}
550+
```
551+
552+
553+
```php
554+
<?php
555+
// embed.php
556+
echo "Hello world!\n";
557+
```
558+
559+
```bash
560+
# compile in debian/ubuntu x86_64
561+
/usr/local/musl/bin/x86_64-linux-musl-gcc embed.c $(bin/spc spc-config bcmath,zlib) -static -o embed
562+
# compile in macOS/FreeBSD
563+
clang embed.c $(bin/spc spc-config bcmath,zlib) -o embed
564+
565+
./embed
566+
# out: Hello world!
567+
```

0 commit comments

Comments
 (0)