mirror of
https://github.com/crazywhalecc/static-php-cli.git
synced 2026-07-02 14:25:41 +08:00
Compare commits
5 Commits
3c438914c2
...
4fb6d8cafd
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4fb6d8cafd | ||
|
|
3a5ccb7f8d | ||
|
|
e7c1345dcb | ||
|
|
e4b37b9f9a | ||
|
|
f06736315d |
58
README-en.md
58
README-en.md
@@ -1,4 +1,5 @@
|
||||
# static-php-cli
|
||||
|
||||
Compile A Statically Linked PHP With Swoole and other Extensions.
|
||||
|
||||
Compile a purely static PHP binary file with various extensions to make PHP-cli applications more portable!
|
||||
@@ -31,14 +32,48 @@ After stable release for this project, a single phar and single binary for this
|
||||
|
||||
And currently you may need to clone this branch and edit GitHub Action to build.
|
||||
|
||||
### Compilation
|
||||
|
||||
```bash
|
||||
chmod +x spc
|
||||
# fetch all libraries
|
||||
./spc fetch --all
|
||||
# with bcmath,openssl,swoole extension, build both CLI and phpmicro SAPI
|
||||
./spc build "bcmath,openssl,swoole" --build-all
|
||||
# with bcmath,openssl,tokenizer,sqlite3,pdo_sqlite,ftp,curl extension, build both CLI and phpmicro SAPI
|
||||
./spc build bcmath,openssl,tokenizer,sqlite3,pdo_sqlite,ftp,curl --build-all
|
||||
```
|
||||
|
||||
### php-cli Usage
|
||||
|
||||
When using the parameter `--build-all` or not adding the `--build-micro` parameter,
|
||||
the final compilation result will output a binary file named `./php`,
|
||||
which can be distributed and used directly.
|
||||
This file will be located in the directory `source/php-src/sapi/cli/`, simply copy it out for use.
|
||||
|
||||
```bash
|
||||
./php -v
|
||||
./php -m
|
||||
./php your_code.php
|
||||
```
|
||||
|
||||
### micro.sfx Usage
|
||||
|
||||
When using the parameter `--build-all` or `--build-micro`,
|
||||
the final compilation result will output a file named `./micro.sfx`,
|
||||
which needs to be used with your PHP source code like `code.php`.
|
||||
This file will be located in the directory `source/php-src/sapi/micro/`, simply copy it out for use.
|
||||
|
||||
Prepare your project source code, which can be a single PHP file or a Phar file, for use.
|
||||
|
||||
```bash
|
||||
echo "<?php echo 'Hello world' . PHP_EOL;" > code.php
|
||||
cat micro.sfx code.php > single-app && chmod +x single-app
|
||||
./single-app
|
||||
|
||||
# If packing a PHAR file, simply replace code.php with the Phar file path.
|
||||
```
|
||||
|
||||
> In some cases, PHAR files may not run in a micro environment.
|
||||
|
||||
## Current Status
|
||||
|
||||
- [X] Basic CLI framework (by symfony/console)
|
||||
@@ -52,7 +87,24 @@ chmod +x spc
|
||||
|
||||
[Support Extension List](/ext-support.md)
|
||||
|
||||
## Open-Source LICENSE
|
||||
## Contribution
|
||||
|
||||
Currently, there are only a few supported extensions.
|
||||
If the extension you need is missing, you can create an issue.
|
||||
If you are familiar with this project, you are also welcome to initiate a pull request.
|
||||
|
||||
The basic principles for contributing are as follows:
|
||||
|
||||
- This project uses php-cs-fixer and phpstan as code formatting tools. Before contributing, please run `composer analyze` and `composer cs-fix` on the updated code.
|
||||
- If other open source libraries are involved, the corresponding licenses should be provided.
|
||||
Also, configuration files should be sorted using the command `sort-config` after modification.
|
||||
For more information about sorting commands, see the documentation.
|
||||
- Naming conventions should be followed, such as using the extension name registered in PHP for the extension name itself,
|
||||
and external library names should follow the project's own naming conventions. For internal logic functions, class names, variables, etc.,
|
||||
camelCase and underscore formats should be followed, and mixing within the same module is prohibited.
|
||||
- When compiling external libraries and creating patches, compatibility with different operating systems should be considered.
|
||||
|
||||
## Open-Source License
|
||||
|
||||
This project is based on the tradition of using the MIT License for old versions,
|
||||
while the new version references source code from some other projects.
|
||||
|
||||
46
README.md
46
README.md
@@ -1,4 +1,5 @@
|
||||
# static-php-cli
|
||||
|
||||
Compile A Statically Linked PHP With Swoole and other Extensions. [English README](README-en.md)
|
||||
|
||||
编译纯静态的 PHP Binary 二进制文件,带有各种扩展,让 PHP-cli 应用变得更便携!
|
||||
@@ -34,14 +35,44 @@ Compile A Statically Linked PHP With Swoole and other Extensions. [English READM
|
||||
|
||||
未来会提供一个直接可使用的 phar 包和一个 phpmicro 打包的二进制文件,你可以直接从 Release 中获取并使用:
|
||||
|
||||
### 编译
|
||||
|
||||
```bash
|
||||
chmod +x spc
|
||||
# 拉取所有依赖库
|
||||
./spc fetch --all
|
||||
# 构建包含 bcmath,openssl,swoole 扩展的 php-cli 和 micro.sfx
|
||||
./spc build "bcmath,openssl,swoole" --build-all
|
||||
# 构建包含 bcmath,openssl,tokenizer,sqlite3,pdo_sqlite,ftp,curl 扩展的 php-cli 和 micro.sfx
|
||||
./spc build "bcmath,openssl,tokenizer,sqlite3,pdo_sqlite,ftp,curl" --build-all
|
||||
```
|
||||
|
||||
### 使用 php-cli
|
||||
|
||||
采用参数 `--build-all` 或不添加 `--build-micro` 参数时,最后编译结果会输出一个 `./php` 的二进制文件,此文件可分发、可直接使用。
|
||||
该文件编译后会存放在 `source/php-src/sapi/cli/` 目录中,拷贝出来即可。
|
||||
|
||||
```bash
|
||||
./php -v
|
||||
./php -m
|
||||
./php your_code.php
|
||||
```
|
||||
|
||||
### 使用 micro.sfx
|
||||
|
||||
采用项目参数 `--build-all` 或 `--build-micro` 时,最后编译结果会输出一个 `./micro.sfx` 的文件,此文件需要配合你的 PHP 源码使用。
|
||||
该文件编译后会存放在 `source/php-src/sapi/micro/` 目录中,拷贝出来即可。
|
||||
|
||||
使用时应准备好你的项目源码文件,可以是单个 PHP 文件,也可以是 Phar 文件。
|
||||
|
||||
```bash
|
||||
echo "<?php echo 'Hello world' . PHP_EOL;" > code.php
|
||||
cat micro.sfx code.php > single-app && chmod +x single-app
|
||||
./single-app
|
||||
|
||||
# 如果打包 PHAR 文件,仅需把 code.php 更换为 phar 文件路径即可
|
||||
```
|
||||
|
||||
> 有些情况下的 phar 文件可能无法在 micro 环境下运行。
|
||||
|
||||
## 项目支持情况(WIP)
|
||||
|
||||
- [X] 基础结构编写(采用 symfony/console`)
|
||||
@@ -55,6 +86,17 @@ chmod +x spc
|
||||
|
||||
[扩展支持列表](/ext-support.md)
|
||||
|
||||
## 贡献
|
||||
|
||||
目前支持的扩展较少,如果缺少你需要的扩展,可发起 Issue。如果你对本项目较熟悉,也欢迎为本项目发起 Pull Request。
|
||||
|
||||
贡献基本原则如下:
|
||||
|
||||
- 项目采用了 php-cs-fixer、phpstan 作为代码规范工具,贡献前请对更新的代码执行 `composer analyze` 和 `composer cs-fix`。
|
||||
- 涉及到其他开源库的部分应提供对应库的协议,同时对配置文件在修改后采用命令 `sort-config` 排序。有关排序的命令,见文档。
|
||||
- 应遵循命名规范,例如扩展名称应采取 PHP 内注册的扩展名本身,外部库名应遵循项目本身的名称,内部逻辑的函数、类名、变量等应遵循驼峰、下划线等格式,禁止同一模块混用。
|
||||
- 涉及编译外部库的命令和 Patch 时应注意兼容不同操作系统。
|
||||
|
||||
## 开源协议
|
||||
|
||||
本项目依据旧版本惯例采用 MIT License 开源,新版本采用了部分项目的源代码做参考,特别感谢:
|
||||
|
||||
@@ -1,4 +1,13 @@
|
||||
{
|
||||
"name": "crazywhalecc/static-php-cli",
|
||||
"description": "Build single static PHP binary, with PHP project together, with popular extensions included.",
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
"name": "jerry",
|
||||
"email": "admin@zhamao.me"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": ">= 8.0",
|
||||
"ext-tokenizer": "*",
|
||||
@@ -23,8 +32,6 @@
|
||||
"src/globals/functions.php"
|
||||
]
|
||||
},
|
||||
"extra": {
|
||||
},
|
||||
"bin": [
|
||||
"bin/spc"
|
||||
],
|
||||
|
||||
@@ -298,6 +298,9 @@
|
||||
"swow": {
|
||||
"type": "external",
|
||||
"source": "swow",
|
||||
"lib-depends": [
|
||||
"libuv"
|
||||
],
|
||||
"lib-suggests": [
|
||||
"openssl",
|
||||
"curl"
|
||||
|
||||
@@ -254,6 +254,15 @@
|
||||
"cunit"
|
||||
]
|
||||
},
|
||||
"libuv": {
|
||||
"source": "libuv",
|
||||
"static-libs-unix": [
|
||||
"libuv.a"
|
||||
],
|
||||
"headers": [
|
||||
"uv.h"
|
||||
]
|
||||
},
|
||||
"onig": {
|
||||
"source": "onig",
|
||||
"static-libs-unix": [
|
||||
|
||||
@@ -150,6 +150,14 @@
|
||||
"path": "COPYING"
|
||||
}
|
||||
},
|
||||
"libuv": {
|
||||
"type": "ghtar",
|
||||
"repo": "libuv/libuv",
|
||||
"license": {
|
||||
"type": "file",
|
||||
"path": "LICENSE"
|
||||
}
|
||||
},
|
||||
"onig": {
|
||||
"type": "ghrel",
|
||||
"repo": "kkos/oniguruma",
|
||||
@@ -209,6 +217,7 @@
|
||||
"path": "php-src/ext/swow-src",
|
||||
"rev": "ci",
|
||||
"url": "https://github.com/swow/swow",
|
||||
"patch": "patchSwow",
|
||||
"license": {
|
||||
"type": "file",
|
||||
"path": "LICENSE"
|
||||
|
||||
@@ -270,6 +270,7 @@ class Extension
|
||||
case 'swow':
|
||||
$arg .= $this->builder->getLib('openssl') ? ' --enable-swow-ssl' : ' --disable-swow-ssl';
|
||||
$arg .= $this->builder->getLib('curl') ? ' --enable-swow-curl' : ' --disable-swow-curl';
|
||||
$arg .= ' SWOW_UV_CFLAGS=-I"' . BUILD_INCLUDE_PATH . '" ';
|
||||
break;
|
||||
case 'swoole':
|
||||
if ($this->builder->getLib('openssl')) {
|
||||
|
||||
29
src/SPC/builder/macos/library/libuv.php
Normal file
29
src/SPC/builder/macos/library/libuv.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace SPC\builder\macos\library;
|
||||
|
||||
/**
|
||||
* is a template library class for unix
|
||||
*/
|
||||
class libuv extends MacOSLibraryBase
|
||||
{
|
||||
public const NAME = 'libuv';
|
||||
|
||||
protected function build()
|
||||
{
|
||||
[,,$destdir] = SEPARATED_PATH;
|
||||
|
||||
f_passthru(
|
||||
$this->builder->set_x . ' && ' .
|
||||
"cd {$this->source_dir} && " .
|
||||
"{$this->builder->configure_env} ./configure " .
|
||||
'--enable-static --disable-shared ' .
|
||||
'--prefix= && ' . // use prefix=/
|
||||
'make clean && ' .
|
||||
"make -j{$this->builder->concurrency} && " .
|
||||
'make install DESTDIR=' . $destdir
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -229,7 +229,7 @@ class Downloader
|
||||
public static function fetchSource(string $name, array $source): void
|
||||
{
|
||||
// 避免重复 fetch
|
||||
if (!isset($source['path']) && !is_dir(FileSystem::convertPath(DOWNLOAD_PATH . "/{$name}")) || isset($source['path']) && is_dir(FileSystem::convertPath(SOURCE_PATH . "/{$source['path']}"))) {
|
||||
if (!isset($source['path']) && is_dir(FileSystem::convertPath(DOWNLOAD_PATH . "/{$name}")) || isset($source['path']) && is_dir(FileSystem::convertPath(SOURCE_PATH . "/{$source['path']}"))) {
|
||||
logger()->notice("{$name} source already extracted");
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user