Compare commits

...

11 Commits
2.0.0 ... 2.0.1

Author SHA1 Message Date
crazywhalecc
5f8641f417 update README 2024-01-10 11:13:47 +08:00
crazywhalecc
17b69ec1e9 update README 2024-01-10 11:11:45 +08:00
Jerry Ma
8de942c274 Adjust function to protected (#315)
* add `--with-added-patch` command

* add BuilderProvider::getBuilder() function

* cs fix

* add builder() and patch_point() global functions

* bump version to 2.0.1

* adjust function prefix

* adjust function prefix
2024-01-10 11:10:40 +08:00
crazywhalecc
a7d5a48b48 bump version to 2.0.1 2024-01-09 11:51:11 +08:00
crazywhalecc
fdc00301c0 add builder() and patch_point() global functions 2024-01-09 11:51:11 +08:00
crazywhalecc
7620d5900e cs fix 2024-01-09 11:51:11 +08:00
crazywhalecc
e973fe743e add BuilderProvider::getBuilder() function 2024-01-09 11:51:11 +08:00
crazywhalecc
149e844d59 add --with-added-patch command 2024-01-09 11:51:11 +08:00
crazywhalecc
12ea3218e8 fix --from-zip not working bug 2024-01-08 23:36:19 +08:00
crazywhalecc
f9e7af1c9a add deepin support for doctor 2024-01-08 23:36:08 +08:00
Jerry Ma
8d2f6baaa2 Update README.md 2024-01-07 12:54:48 +08:00
16 changed files with 361 additions and 220 deletions

View File

@@ -1,32 +1,46 @@
# static-php-cli # static-php-cli
Build single static PHP binary, with PHP project together, with popular extensions included. [![Version](https://img.shields.io/packagist/v/crazywhalecc/static-php-cli?include_prereleases&label=Release&style=flat-square)]()
🌐 **[中文](README-zh.md)** | **[English](README.md)**
编译纯静态的 PHP Binary 二进制文件,带有各种扩展,让 PHP-cli 应用变得更便携cli SAPI
<img width="600" alt="截屏2023-05-02 15 53 13" src="https://user-images.githubusercontent.com/20330940/235610282-23e58d68-bd35-4092-8465-171cff2d5ba8.png">
同时可以使用 micro 二进制文件,将 PHP 源码和 PHP 二进制构建为一个文件分发micro SAPI
<img width="600" alt="截屏2023-05-02 15 52 33" src="https://user-images.githubusercontent.com/20330940/235610318-2ef4e3f1-278b-4ca4-99f4-b38120efc395.png">
> 该 SAPI 源自 [dixyes/phpmicro](https://github.com/dixyes/phpmicro) 的 [Fork 仓库](https://github.com/static-php/phpmicro)。
[![Version](https://img.shields.io/badge/Version-2.0.0-green.svg?style=flat-square)]()
[![License](https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square)]()
[![](https://img.shields.io/github/actions/workflow/status/crazywhalecc/static-php-cli/tests.yml?branch=main&label=Build%20Test&style=flat-square)](https://github.com/crazywhalecc/static-php-cli/actions/workflows/tests.yml) [![](https://img.shields.io/github/actions/workflow/status/crazywhalecc/static-php-cli/tests.yml?branch=main&label=Build%20Test&style=flat-square)](https://github.com/crazywhalecc/static-php-cli/actions/workflows/tests.yml)
[![](https://img.shields.io/badge/Extension%20Counter-75+-yellow.svg?style=flat-square)]() [![License](https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square)]()
[![](https://img.shields.io/github/search/crazywhalecc/static-php-cli/TODO?label=TODO%20Counter&style=flat-square)]() [![](https://img.shields.io/badge/Extension%20Counter-75+-yellow.svg?style=flat-square)]([https://static-php.dev/](https://static-php.dev/en/guide/extensions.html))
[![](https://dcbadge.vercel.app/api/server/RNpegEYW?style=flat-square&compact=true&theme=default-inverted)](https://discord.gg/RNpegEYW)
> 项目名称是 static-php-cli,但其实支持 cli、fpm、micro 和 embed SAPI 😎 **static-php-cli**是一个用于静态编译、构建 PHP 解释器的工具,支持众多流行扩展。
目前 static-php-cli 支持 `cli``fpm``embed``micro` SAPI。
**static-php-cli**也支持将 PHP 代码和 PHP 运行时打包为一个文件并运行。
- [README - English](./README.md)
- [README - 中文](./README-zh.md)
## 特性
static-php-cli简称 `spc`)有许多特性:
- :handbag: 构建独立的单文件 PHP 解释器,无需任何依赖
- :hamburger: 构建 **[phpmicro](https://github.com/dixyes/phpmicro)** 自执行二进制(将 PHP 代码和 PHP 解释器打包为一个文件)
- :pill: 提供一键检查和修复编译环境的 Doctor 模块
- :zap: 支持多个系统:`Linux``macOS``FreeBSD`、[`Windows (WIP)`](https://github.com/crazywhalecc/static-php-cli/pull/301)
- :wrench: 高度自定义的代码 patch 功能
- :books: 自带编译依赖管理
- 📦 提供由自身编译的独立 `spc` 二进制(使用 spc 和 [box](https://github.com/box-project/box) 构建)
- :fire: 支持大量 [扩展](https://static-php.dev/zh/guide/extensions.html)
**静态 php-cli:**
<img width="700" alt="out1" src="https://github.com/crazywhalecc/static-php-cli/assets/20330940/01a2e60f-13b0-4242-a645-f7afa4936396">
**使用 phpmicro 打包 PHP 代码:**
<img width="700" alt="out2" src="https://github.com/crazywhalecc/static-php-cli/assets/20330940/46b7128d-fb72-4169-957e-48564c3ff3e2">
## 文档 ## 文档
目前 README 编写了基本用法。有关 static-php-cli 所有的功能,请点击这里查看文档:<https://static-php.dev>。 目前 README 编写了基本用法。有关 static-php-cli 所有的功能,请点击这里查看文档:<https://static-php.dev>。
## 自托管直接下载 ## 直接下载
如果你不想自行编译 PHP可以从本项目现有的示例 Action 下载 Artifact也可以从自托管的服务器下载。 如果你不想自行编译 PHP可以从本项目现有的示例 Action 下载 Artifact也可以从自托管的服务器下载。
@@ -38,6 +52,10 @@ Build single static PHP binary, with PHP project together, with popular extensio
### 编译环境需求 ### 编译环境需求
- PHP >= 8.1spc 自身是用 PHP 写的)
- 扩展:`mbstring,pcntl,posix,tokenizer,phar`
- 系统安装了 `curl``git`
是的,本项目采用 PHP 编写,编译前需要一个 PHP 环境,比较滑稽。 是的,本项目采用 PHP 编写,编译前需要一个 PHP 环境,比较滑稽。
但本项目默认可通过自身构建的 micro 和 static-php 二进制运行,其他只需要包含 mbstring、pcntl 扩展和 PHP 版本大于等于 8.1 即可。 但本项目默认可通过自身构建的 micro 和 static-php 二进制运行,其他只需要包含 mbstring、pcntl 扩展和 PHP 版本大于等于 8.1 即可。
@@ -50,11 +68,9 @@ Build single static PHP binary, with PHP project together, with popular extensio
| Windows | | | | Windows | | |
| FreeBSD | :computer: | :computer: | | FreeBSD | :computer: | :computer: |
> macOS-arm64 因 GitHub 暂未提供 arm runner如果要构建 arm 二进制,可以使用手动构建。
目前支持编译的 PHP 版本为:`7.3``7.4``8.0``8.1``8.2``8.3` 目前支持编译的 PHP 版本为:`7.3``7.4``8.0``8.1``8.2``8.3`
### 支持的扩展情况 ### 支持的扩展
请先根据下方扩展列表选择你要编译的扩展。 请先根据下方扩展列表选择你要编译的扩展。
@@ -63,7 +79,7 @@ Build single static PHP binary, with PHP project together, with popular extensio
> 如果这里没有你需要的扩展,可以提交 Issue。 > 如果这里没有你需要的扩展,可以提交 Issue。
### 使用 Actions 构建 ### 在线构建(使用 GitHub Actions
使用 GitHub Action 可以方便地构建一个静态编译的 PHP同时可以自行定义要编译的扩展。 使用 GitHub Action 可以方便地构建一个静态编译的 PHP同时可以自行定义要编译的扩展。
@@ -74,72 +90,77 @@ Build single static PHP binary, with PHP project together, with popular extensio
如果你选择了 `debug`,则会在构建时输出所有日志,包括编译的日志,以供排查错误。 如果你选择了 `debug`,则会在构建时输出所有日志,包括编译的日志,以供排查错误。
### 手动构建(使用 SPC 二进制) ### 本地构建(使用 spc 二进制)
项目提供了一个 static-php-cli 的二进制文件,你可以直接下载对应平台的二进制文件,然后使用它来构建静态的 PHP。目前 `spc` 二进制支持的平台有 Linux 和 macOS 项目提供了 static-php-cli 的二进制文件`spc`
您可以使用 `spc` 二进制文件,无需安装任何运行时(用起来就像 golang 程序)。
目前,`spc` 二进制文件提供的平台有 Linux 和 macOS。
下面是从 GitHub Action 下载的方法 使用以下命令从自托管服务器下载
1. 进入 [GitHub Action](https://github.com/crazywhalecc/static-php-cli/actions/workflows/release-build.yml)。
2. 选择一个最新的构建任务,进入后选择 `Artifacts`,下载对应平台的二进制文件。
3. 解压 `.zip` 文件。解压后,为其添加执行权限:`chmod +x ./spc`
你也可以从自托管的服务器下载二进制文件:[进入](https://dl.static-php.dev/static-php-cli/spc-bin/nightly/)。
### 手动构建(使用源码)
先克隆本项目:
```bash ```bash
# Download from self-hosted nightly builds (sync with main branch)
# For Linux x86_64
curl -o spc https://dl.static-php.dev/static-php-cli/spc-bin/nightly/spc-linux-x86_64
# For Linux aarch64
curl -o spc https://dl.static-php.dev/static-php-cli/spc-bin/nightly/spc-linux-aarch64
# macOS x86_64 (Intel)
curl -o spc https://dl.static-php.dev/static-php-cli/spc-bin/nightly/spc-macos-x86_64
# macOS aarch64 (Apple)
curl -o spc https://dl.static-php.dev/static-php-cli/spc-bin/nightly/spc-macos-aarch64
# add x perm
chmod +x ./spc
./spc --version
```
自托管 `spc` 由 GitHub Actions 构建,你也可以从 Actions 直接下载:[此处](https://github.com/crazywhalecc/static-php-cli/actions/workflows/release-build.yml)。
### 本地构建(使用 git 源码)
```bash
# clone 仓库即可
git clone https://github.com/crazywhalecc/static-php-cli.git git clone https://github.com/crazywhalecc/static-php-cli.git
``` ```
如果你本机没有安装 PHP你需要先使用包管理例如 brew、apt、yum、apk 等)安装 php 如果您的系统上尚未安装 php我们建议你使用内置的 setup-runtime 自动安装 PHP 和 Composer
你也可以通过 `bin/setup-runtime` 命令下载静态编译好的 php-cli 和 Composer。下载的 php 和 Composer 将保存为 `bin/php``bin/composer`
```bash ```bash
cd static-php-cli cd static-php-cli
chmod +x bin/setup-runtime chmod +x bin/setup-runtime
./bin/setup-runtime # it will download static php (from self-hosted server) and composer (from getcomposer)
bin/setup-runtime
# 使用独立的 php 运行 static-php-cli # initialize composer deps
./bin/php bin/spc bin/composer install
# chmod
# 使用 composer
./bin/php bin/composer
# 初始化本项目
cd static-php-cli
composer update
chmod +x bin/spc chmod +x bin/spc
bin/spc --version
``` ```
### 使用 static-php-cli 命令行程序 ### 开始构建 PHP
下面是使用 static-php-cli 编译静态 php 和 micro 的基础用法: 下面是使用 static-php-cli 的基础用法:
> 如果你使用的是打包好的 `spc` 二进制,你需要将下列命令的 `bin/spc` 替换为 `./spc`。 > 如果你使用的是打包好的 `spc` 二进制,你需要将下列命令的 `./bin/spc` 替换为 `./spc`。
```bash ```bash
# 检查环境依赖,并根据提示的命令安装缺失的编译工具 # 检查环境依赖,并根据尝试自动安装缺失的编译工具
./bin/spc doctor ./bin/spc doctor --auto-fix
# 拉取所有依赖库 # 拉取所有依赖库
./bin/spc fetch --all ./bin/spc download --all
# 只拉取编译指定扩展需要的所有依赖 # 只拉取编译指定扩展需要的所有依赖(推荐)
./bin/spc download --for-extensions=openssl,pcntl,mbstring,pdo_sqlite ./bin/spc download --for-extensions=openssl,pcntl,mbstring,pdo_sqlite
# 下载编译不同版本的 PHP (--with-php=x.y推荐 7.3 ~ 8.3)
./bin/spc download --for-extensions=openssl,curl,mbstring --with-php=8.1
# 构建包含 bcmath,openssl,tokenizer,sqlite3,pdo_sqlite,ftp,curl 扩展的 php-cli 和 micro.sfx # 构建包含 bcmath,openssl,tokenizer,sqlite3,pdo_sqlite,ftp,curl 扩展的 php-cli 和 micro.sfx
./bin/spc build "bcmath,openssl,tokenizer,sqlite3,pdo_sqlite,ftp,curl" --build-cli --build-micro ./bin/spc build "bcmath,openssl,tokenizer,sqlite3,pdo_sqlite,ftp,curl" --build-cli --build-micro
# 编译线程安全版本 (--enable-zts)
./bin/spc build curl,phar --enable-zts --build-cli
``` ```
你也可以使用参数 `--with-php=x.y` 来指定下载的 PHP 版本,目前支持 7.3 ~ 8.3 其中,目前支持构建 climicrofpm 和 embed使用以下参数的一个或多个来指定编译的 SAPI
```bash
# 优先考虑使用 >= 8.0 的 PHP 版本,因为 phpmicro 不支持在 PHP7 中构建
./bin/spc download --with-php=8.2 --all
```
其中,目前支持构建 climicrofpm 三种静态二进制,使用以下参数的一个或多个来指定编译的 SAPI
- `--build-cli`:构建 cli 二进制 - `--build-cli`:构建 cli 二进制
- `--build-micro`:构建 phpmicro 自执行二进制 - `--build-micro`:构建 phpmicro 自执行二进制
@@ -151,17 +172,9 @@ chmod +x bin/spc
```bash ```bash
./bin/spc build openssl,pcntl,mbstring --debug --build-all ./bin/spc build openssl,pcntl,mbstring --debug --build-all
./bin/spc fetch --all --debug ./bin/spc download --all --debug
``` ```
此外,默认编译的 PHP 为 NTS 版本。如需编译线程安全版本ZTS只需添加参数 `--enable-zts` 即可。
```bash
./bin/spc build openssl,pcntl --build-all --enable-zts
```
同时,你也可以使用参数 `--no-strip` 来关闭裁剪,关闭裁剪后可以使用 gdb 等工具调试,但这样会让静态二进制体积变大。
## 不同 SAPI 的使用 ## 不同 SAPI 的使用
### 使用 cli ### 使用 cli

174
README.md
View File

@@ -1,28 +1,42 @@
# static-php-cli # static-php-cli
Build single static PHP binary, with PHP project together, with popular extensions included. [![Version](https://img.shields.io/packagist/v/crazywhalecc/static-php-cli?include_prereleases&label=Release&style=flat-square)]()
🌐 **[中文](README-zh.md)** | **[English](README.md)**
> 2.0 Release is coming soon, windows support will be added in v2.1.
The project name is static-php-cli, but it actually supports cli, fpm, micro and embed SAPI 😎
Compile a purely static php-cli binary file with various extensions to make PHP applications more portable! (cli SAPI)
<img width="600" alt="2023-05-02 15 53 13" src="https://user-images.githubusercontent.com/20330940/235610282-23e58d68-bd35-4092-8465-171cff2d5ba8.png">
You can also use the micro binary file to combine php binary and php source code into one for distribution! (micro SAPI)
<img width="600" alt="2023-05-02 15 52 33" src="https://user-images.githubusercontent.com/20330940/235610318-2ef4e3f1-278b-4ca4-99f4-b38120efc395.png">
> This SAPI feature is from the [Fork](https://github.com/static-php/phpmicro) of [dixyes/phpmicro](https://github.com/dixyes/phpmicro).
[![Version](https://img.shields.io/badge/Version-2.0.0-green.svg?style=flat-square)]()
[![License](https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square)]()
[![](https://img.shields.io/github/actions/workflow/status/crazywhalecc/static-php-cli/tests.yml?branch=main&label=Build%20Test&style=flat-square)](https://github.com/crazywhalecc/static-php-cli/actions/workflows/tests.yml) [![](https://img.shields.io/github/actions/workflow/status/crazywhalecc/static-php-cli/tests.yml?branch=main&label=Build%20Test&style=flat-square)](https://github.com/crazywhalecc/static-php-cli/actions/workflows/tests.yml)
[![License](https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square)]()
[![](https://img.shields.io/badge/Extension%20Counter-75+-yellow.svg?style=flat-square)]([https://static-php.dev/](https://static-php.dev/en/guide/extensions.html)) [![](https://img.shields.io/badge/Extension%20Counter-75+-yellow.svg?style=flat-square)]([https://static-php.dev/](https://static-php.dev/en/guide/extensions.html))
[![](https://img.shields.io/github/search/crazywhalecc/static-php-cli/TODO?label=TODO%20Counter&style=flat-square)]() [![](https://dcbadge.vercel.app/api/server/RNpegEYW?style=flat-square&compact=true&theme=default-inverted)](https://discord.gg/RNpegEYW)
**static-php-cli** is a powerful tool designed for building static, standalone PHP runtime
with popular extensions.
Static PHP built by **static-php-cli** supports `cli`, `fpm`, `embed` and `micro` SAPI.
**static-php-cli** also has the ability to package PHP projects
along with the PHP interpreter into one single executable file.
- [README - English](./README.md)
- [README - 中文](./README-zh.md)
## Features
static-php-cli (you can call it `spc`) has a lot of features:
- :handbag: Build single-file php executable, without any dependencies
- :hamburger: Build **[phpmicro](https://github.com/dixyes/phpmicro)** self-extracted executable (glue php binary and php source code into one file)
- :pill: Automatic build environment checker (Doctor module)
- :zap: `Linux`, `macOS`, `FreeBSD`, [`Windows (WIP)`](https://github.com/crazywhalecc/static-php-cli/pull/301) support
- :wrench: Configurable source code patches
- :books: Build dependency management
- 📦 Provide `spc` own standalone executable (built by spc and [box](https://github.com/box-project/box))
- :fire: Support many popular [extensions](https://static-php.dev/en/guide/extensions.html)
**Single-file standalone php-cli:**
<img width="700" alt="out1" src="https://github.com/crazywhalecc/static-php-cli/assets/20330940/01a2e60f-13b0-4242-a645-f7afa4936396">
**Combine PHP code with PHP interpreter using phpmicro:**
<img width="700" alt="out2" src="https://github.com/crazywhalecc/static-php-cli/assets/20330940/46b7128d-fb72-4169-957e-48564c3ff3e2">
## Documentation ## Documentation
@@ -31,7 +45,7 @@ see <https://static-php.dev> .
## Direct Download ## Direct Download
If you don't want to compile yourself, you can download example pre-compiled artifact from [Actions](https://github.com/static-php/static-php-cli-hosted/actions/workflows/build-php-common.yml), or from self-hosted server. If you don't want to build or want to test first, you can download example pre-compiled artifact from [Actions](https://github.com/static-php/static-php-cli-hosted/actions/workflows/build-php-bulk.yml), or from self-hosted server.
Below are several precompiled static-php binaries with different extension combinations, Below are several precompiled static-php binaries with different extension combinations,
which can be downloaded directly according to your needs. which can be downloaded directly according to your needs.
@@ -40,14 +54,18 @@ which can be downloaded directly according to your needs.
- [Extension-Combination - bulk](https://dl.static-php.dev/static-php-cli/bulk/): `bulk` contains [50+](https://dl.static-php.dev/static-php-cli/bulk/README.txt) extensions and is about 70MB in size. - [Extension-Combination - bulk](https://dl.static-php.dev/static-php-cli/bulk/): `bulk` contains [50+](https://dl.static-php.dev/static-php-cli/bulk/README.txt) extensions and is about 70MB in size.
- [Extension-Combination - minimal](https://dl.static-php.dev/static-php-cli/minimal/): `minimal` contains [5](https://dl.static-php.dev/static-php-cli/minimal/README.txt) extensions and is about 6MB in size. - [Extension-Combination - minimal](https://dl.static-php.dev/static-php-cli/minimal/): `minimal` contains [5](https://dl.static-php.dev/static-php-cli/minimal/README.txt) extensions and is about 6MB in size.
## Use static-php-cli to build PHP ## Build
### Compilation Requirements ### Compilation Requirements
Yes, this project is written in PHP, pretty funny. - PHP >= 8.1 (static-php-cli is written in PHP)
- Extension: `mbstring,pcntl,posix,tokenizer,phar`
- Supported OS with `curl` and `git` installed
You can say I made a PHP builder written in PHP, pretty funny.
But static-php-cli runtime only requires an environment above PHP 8.1 and `mbstring`, `pcntl` extension. But static-php-cli runtime only requires an environment above PHP 8.1 and `mbstring`, `pcntl` extension.
Here is the architecture support status, where :octocat: represents support for GitHub Action builds, Here is the supported OS and arch, where :octocat: represents support for GitHub Action builds,
:computer: represents support for local manual builds, and blank represents not currently supported. :computer: represents support for local manual builds, and blank represents not currently supported.
| | x86_64 | aarch64 | | | x86_64 | aarch64 |
@@ -57,8 +75,6 @@ Here is the architecture support status, where :octocat: represents support for
| Windows | | | | Windows | | |
| FreeBSD | :computer: | :computer: | | FreeBSD | :computer: | :computer: |
> macOS-arm64 is not supported for GitHub Actions, if you are going to build on arm, you can build it manually on your own machine.
Currently supported PHP versions for compilation are: `7.3`, `7.4`, `8.0`, `8.1`, `8.2`, `8.3`. Currently supported PHP versions for compilation are: `7.3`, `7.4`, `8.0`, `8.1`, `8.2`, `8.3`.
### Supported Extensions ### Supported Extensions
@@ -72,7 +88,7 @@ Please first select the extension you want to compile based on the extension lis
Here is the current planned roadmap for extension support: [#152](https://github.com/crazywhalecc/static-php-cli/issues/152) . Here is the current planned roadmap for extension support: [#152](https://github.com/crazywhalecc/static-php-cli/issues/152) .
### GitHub Actions Build ### Build Online (using GitHub Actions)
Use GitHub Action to easily build a statically compiled PHP, Use GitHub Action to easily build a statically compiled PHP,
and at the same time define the extensions to be compiled by yourself. and at the same time define the extensions to be compiled by yourself.
@@ -84,80 +100,77 @@ and at the same time define the extensions to be compiled by yourself.
If you enable `debug`, all logs will be output at build time, including compiled logs, for troubleshooting. If you enable `debug`, all logs will be output at build time, including compiled logs, for troubleshooting.
- When using ubuntu-latest, it will build linux-x86_64 binary. ### Build Locally (using SPC binary)
- When using macos-latest, it will build macOS-x86_64 binary.
### Manual build (using SPC binary) This project provides a binary file of static-php-cli: `spc`.
You can use `spc` binary instead of installing any runtime like golang app.
This project provides a binary file of static-php-cli.
You can directly download the binary file of the corresponding platform and then use it to build static PHP.
Currently, the platforms supported by `spc` binary are Linux and macOS. Currently, the platforms supported by `spc` binary are Linux and macOS.
Here's how to download from GitHub Actions: Download from self-hosted nightly builds using commands below:
1. Enter [GitHub Actions](https://github.com/crazywhalecc/static-php-cli/actions/workflows/release-build.yml) or [self-hosted nightly builds](https://dl.static-php.dev/static-php-cli/spc-bin/nightly/).
2. If you download from GHA, select the latest build task, select `Artifacts`, and download the binary file of the corresponding platform.
3. If you download from GHA, unzip the `.zip` file. After decompressing, add execution permissions to it: `chmod +x ./spc`.
4. If you download from self-hosted server, download `spc-$os-$arch` file and just use it (don't forget `chmod +x`).
> SPC single-file binary is built by phpmicro and box, and it doesn't need to install PHP. Just treat `spc` as a standalone executable.
### Manual build (using source code)
Clone repo first:
```bash ```bash
# Download from self-hosted nightly builds (sync with main branch)
# For Linux x86_64
curl -o spc https://dl.static-php.dev/static-php-cli/spc-bin/nightly/spc-linux-x86_64
# For Linux aarch64
curl -o spc https://dl.static-php.dev/static-php-cli/spc-bin/nightly/spc-linux-aarch64
# macOS x86_64 (Intel)
curl -o spc https://dl.static-php.dev/static-php-cli/spc-bin/nightly/spc-macos-x86_64
# macOS aarch64 (Apple)
curl -o spc https://dl.static-php.dev/static-php-cli/spc-bin/nightly/spc-macos-aarch64
# add x perm
chmod +x ./spc
./spc --version
```
Self-hosted `spc` is built by GitHub Actions, you can also download from Actions artifacts [here](https://github.com/crazywhalecc/static-php-cli/actions/workflows/release-build.yml).
### Build Locally (using git source)
```bash
# just clone me!
git clone https://github.com/crazywhalecc/static-php-cli.git git clone https://github.com/crazywhalecc/static-php-cli.git
``` ```
If you have not installed php on your system, you can use package management to install PHP (such as brew, apt, yum, apk etc.). If you have not installed php on your system, we recommend that you use the built-in setup-runtime to install PHP and Composer automatically.
And you can also download single-file php binary and composer using command `bin/setup-runtime`.
The PHP runtime for static-php-cli itself will be downloaded at `bin/php`, and composer is at `bin/composer`.
```bash ```bash
cd static-php-cli cd static-php-cli
chmod +x bin/setup-runtime chmod +x bin/setup-runtime
# It will download php-cli from self-hosted server and composer from getcomposer.org # it will download static php (from self-hosted server) and composer (from getcomposer)
./bin/setup-runtime bin/setup-runtime
# initialize composer deps
# Use this php runtime to run static-php-cli compiler bin/composer install
./bin/php bin/spc # chmod
# Use composer
./bin/php bin/composer
# Initialize this project
cd static-php-cli
composer update
chmod +x bin/spc chmod +x bin/spc
bin/spc --version
``` ```
### Use static-php-cli ### Start Building PHP
Basic usage for building php and micro with some extensions: Basic usage for building php with some extensions:
> If you are using the packaged `spc` binary, you need to replace `bin/spc` with `./spc` in the following commands. > If you are using the packaged `spc` binary, you need to replace `bin/spc` with `./spc` in the following commands.
```bash ```bash
# Check system tool dependencies, fix them if possible # Check system tool dependencies, auto-fix them if possible
./bin/spc doctor ./bin/spc doctor --auto-fix
# fetch all libraries # fetch all libraries
./bin/spc download --all ./bin/spc download --all
# only fetch necessary sources by needed extensions # only fetch necessary sources by needed extensions (recommended)
./bin/spc download --for-extensions=openssl,pcntl,mbstring,pdo_sqlite ./bin/spc download --for-extensions=openssl,pcntl,mbstring,pdo_sqlite
# download different PHP version (--with-php=x.y, recommend 7.3 ~ 8.3)
./bin/spc download --for-extensions=openssl,curl,mbstring --with-php=8.1
# with bcmath,openssl,tokenizer,sqlite3,pdo_sqlite,ftp,curl extension, build both CLI and phpmicro SAPI # with bcmath,openssl,tokenizer,sqlite3,pdo_sqlite,ftp,curl extension, build both CLI and phpmicro SAPI
./bin/spc build bcmath,openssl,tokenizer,sqlite3,pdo_sqlite,ftp,curl --build-cli --build-micro ./bin/spc build bcmath,openssl,tokenizer,sqlite3,pdo_sqlite,ftp,curl --build-cli --build-micro
# build thread-safe (ZTS) version (--enable-zts)
./bin/spc build curl,phar --enable-zts --build-cli
``` ```
You can also use the parameter `--with-php=x.y` to specify the downloaded PHP version, currently supports 7.4 ~ 8.3: Now we support `cli`, `micro`, `fpm` and `embed` SAPI. You can use one or more of the following parameters to specify the compiled SAPI:
```bash
# Using PHP >= 8.0 is recommended, because PHP7 cannot use phpmicro
./bin/spc fetch --with-php=8.2 --all
```
Now we support `cli`, `micro`, `fpm`, you can use one or more of the following parameters to specify the compiled SAPI:
- `--build-cli`: build static cli executable - `--build-cli`: build static cli executable
- `--build-micro`: build static phpmicro self-extracted executable - `--build-micro`: build static phpmicro self-extracted executable
@@ -169,17 +182,9 @@ If anything goes wrong, use `--debug` option to display full terminal output:
```bash ```bash
./bin/spc build openssl,pcntl,mbstring --debug --build-all ./bin/spc build openssl,pcntl,mbstring --debug --build-all
./bin/spc fetch --all --debug ./bin/spc download --all --debug
``` ```
In addition, we build NTS (non-thread-safe) by default. If you are going to build ZTS version, just add `--enable-zts` option.
```bash
./bin/spc build openssl,pcntl --build-all --enable-zts
```
Adding option `--no-strip` can produce binaries with debug symbols, in order to debug (using gdb). Disabling strip will increase the size of static binary.
## Different SAPI Usage ## Different SAPI Usage
### Use cli ### Use cli
@@ -288,4 +293,3 @@ and they all have their own open source licenses.
Please use the `bin/spc dump-license` command to export the open source licenses used in the project after compilation, Please use the `bin/spc dump-license` command to export the open source licenses used in the project after compilation,
and comply with the corresponding project's LICENSE. and comply with the corresponding project's LICENSE.

View File

@@ -23,7 +23,7 @@ use Symfony\Component\Console\Command\ListCommand;
*/ */
final class ConsoleApplication extends Application final class ConsoleApplication extends Application
{ {
public const VERSION = '2.0.0'; public const VERSION = '2.0.1';
public function __construct() public function __construct()
{ {

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace SPC\builder; namespace SPC\builder;
use SPC\exception\ExceptionHandler;
use SPC\exception\FileSystemException; use SPC\exception\FileSystemException;
use SPC\exception\RuntimeException; use SPC\exception\RuntimeException;
use SPC\exception\WrongUsageException; use SPC\exception\WrongUsageException;
@@ -30,6 +31,9 @@ abstract class BuilderBase
/** @var array<string, mixed> compile options */ /** @var array<string, mixed> compile options */
protected array $options = []; protected array $options = [];
/** @var string patch point name */
protected string $patch_point = '';
/** /**
* Build libraries * Build libraries
* *
@@ -37,6 +41,7 @@ abstract class BuilderBase
* @throws FileSystemException * @throws FileSystemException
* @throws RuntimeException * @throws RuntimeException
* @throws WrongUsageException * @throws WrongUsageException
* @internal
*/ */
public function buildLibs(array $sorted_libraries): void public function buildLibs(array $sorted_libraries): void
{ {
@@ -78,9 +83,14 @@ abstract class BuilderBase
$lib->calcDependency(); $lib->calcDependency();
} }
// patch point
$this->emitPatchPoint('before-libs-extract');
// extract sources // extract sources
SourceExtractor::initSource(libs: $sorted_libraries); SourceExtractor::initSource(libs: $sorted_libraries);
$this->emitPatchPoint('after-libs-extract');
// build all libs // build all libs
foreach ($this->libs as $lib) { foreach ($this->libs as $lib) {
match ($lib->tryBuild($this->getOption('rebuild', false))) { match ($lib->tryBuild($this->getOption('rebuild', false))) {
@@ -172,6 +182,8 @@ abstract class BuilderBase
/** /**
* Set libs only mode. * Set libs only mode.
*
* @internal
*/ */
public function setLibsOnly(bool $status = true): void public function setLibsOnly(bool $status = true): void
{ {
@@ -185,15 +197,22 @@ abstract class BuilderBase
* @throws RuntimeException * @throws RuntimeException
* @throws \ReflectionException * @throws \ReflectionException
* @throws WrongUsageException * @throws WrongUsageException
* @internal
*/ */
public function proveExts(array $extensions): void public function proveExts(array $extensions): void
{ {
CustomExt::loadCustomExt(); CustomExt::loadCustomExt();
$this->emitPatchPoint('before-php-extract');
SourceExtractor::initSource(sources: ['php-src']); SourceExtractor::initSource(sources: ['php-src']);
$this->emitPatchPoint('after-php-extract');
if ($this->getPHPVersionID() >= 80000) { if ($this->getPHPVersionID() >= 80000) {
$this->emitPatchPoint('before-micro-extract');
SourceExtractor::initSource(sources: ['micro']); SourceExtractor::initSource(sources: ['micro']);
$this->emitPatchPoint('after-micro-extract');
} }
$this->emitPatchPoint('before-exts-extract');
SourceExtractor::initSource(exts: $extensions); SourceExtractor::initSource(exts: $extensions);
$this->emitPatchPoint('after-exts-extract');
foreach ($extensions as $extension) { foreach ($extensions as $extension) {
$class = CustomExt::getExtClass($extension); $class = CustomExt::getExtClass($extension);
$ext = new $class($extension, $this); $ext = new $class($extension, $this);
@@ -342,6 +361,39 @@ abstract class BuilderBase
return implode(' ', $env); return implode(' ', $env);
} }
/**
* Get builder patch point name.
*/
public function getPatchPoint(): string
{
return $this->patch_point;
}
public function emitPatchPoint(string $point_name): void
{
$this->patch_point = $point_name;
if (($patches = $this->getOption('with-added-patch', [])) === []) {
return;
}
foreach ($patches as $patch) {
try {
if (!file_exists($patch)) {
throw new RuntimeException("Additional patch script file {$patch} not found!");
}
logger()->debug('Running additional patch script: ' . $patch);
require $patch;
} catch (\Throwable $e) {
logger()->critical('Patch script ' . $patch . ' failed to run.');
if ($this->getOption('debug')) {
ExceptionHandler::getInstance()->handle($e);
} else {
logger()->critical('Please check with --debug option to see more details.');
}
}
}
}
/** /**
* Check if all libs are downloaded. * Check if all libs are downloaded.
* If not, throw exception. * If not, throw exception.

View File

@@ -17,6 +17,8 @@ use Symfony\Component\Console\Input\InputInterface;
*/ */
class BuilderProvider class BuilderProvider
{ {
private static ?BuilderBase $builder = null;
/** /**
* @throws FileSystemException * @throws FileSystemException
* @throws RuntimeException * @throws RuntimeException
@@ -24,7 +26,7 @@ class BuilderProvider
*/ */
public static function makeBuilderByInput(InputInterface $input): BuilderBase public static function makeBuilderByInput(InputInterface $input): BuilderBase
{ {
return match (PHP_OS_FAMILY) { self::$builder = match (PHP_OS_FAMILY) {
// 'Windows' => new WindowsBuilder( // 'Windows' => new WindowsBuilder(
// binary_sdk_dir: $input->getOption('with-sdk-binary-dir'), // binary_sdk_dir: $input->getOption('with-sdk-binary-dir'),
// vs_ver: $input->getOption('vs-ver'), // vs_ver: $input->getOption('vs-ver'),
@@ -35,5 +37,17 @@ class BuilderProvider
'BSD' => new BSDBuilder($input->getOptions()), 'BSD' => new BSDBuilder($input->getOptions()),
default => throw new WrongUsageException('Current OS "' . PHP_OS_FAMILY . '" is not supported yet'), default => throw new WrongUsageException('Current OS "' . PHP_OS_FAMILY . '" is not supported yet'),
}; };
return self::$builder;
}
/**
* @throws WrongUsageException
*/
public static function getBuilder(): BuilderBase
{
if (self::$builder === null) {
throw new WrongUsageException('Builder has not been initialized');
}
return self::$builder;
} }
} }

View File

@@ -140,7 +140,9 @@ abstract class LibraryBase
if (!$this->patched && $this->patchBeforeBuild()) { if (!$this->patched && $this->patchBeforeBuild()) {
file_put_contents($this->source_dir . '/.spc.patched', 'PATCHED!!!'); file_put_contents($this->source_dir . '/.spc.patched', 'PATCHED!!!');
} }
$this->getBuilder()->emitPatchPoint('before-library[ ' . static::NAME . ']-build');
$this->build(); $this->build();
$this->getBuilder()->emitPatchPoint('after-library[ ' . static::NAME . ']-build');
return BUILD_STATUS_OK; return BUILD_STATUS_OK;
} }

View File

@@ -81,10 +81,12 @@ class BSDBuilder extends BuilderBase
} }
$this->setOption('extra-libs', $extra_libs); $this->setOption('extra-libs', $extra_libs);
$this->emitPatchPoint('before-php-buildconf');
SourcePatcher::patchBeforeBuildconf($this); SourcePatcher::patchBeforeBuildconf($this);
shell()->cd(SOURCE_PATH . '/php-src')->exec('./buildconf --force'); shell()->cd(SOURCE_PATH . '/php-src')->exec('./buildconf --force');
$this->emitPatchPoint('before-php-configure');
SourcePatcher::patchBeforeConfigure($this); SourcePatcher::patchBeforeConfigure($this);
$json_74 = $this->getPHPVersionID() < 80000 ? '--enable-json ' : ''; $json_74 = $this->getPHPVersionID() < 80000 ? '--enable-json ' : '';
@@ -115,6 +117,7 @@ class BSDBuilder extends BuilderBase
$this->makeExtensionArgs() $this->makeExtensionArgs()
); );
$this->emitPatchPoint('before-php-make');
SourcePatcher::patchBeforeMake($this); SourcePatcher::patchBeforeMake($this);
$this->cleanMake(); $this->cleanMake();
@@ -140,6 +143,7 @@ class BSDBuilder extends BuilderBase
} }
if (php_uname('m') === $this->getOption('arch')) { if (php_uname('m') === $this->getOption('arch')) {
$this->emitPatchPoint('before-sanity-check');
$this->sanityCheck($build_target); $this->sanityCheck($build_target);
} }
} }
@@ -150,7 +154,7 @@ class BSDBuilder extends BuilderBase
* @throws RuntimeException * @throws RuntimeException
* @throws FileSystemException * @throws FileSystemException
*/ */
public function buildCli(): void protected function buildCli(): void
{ {
$vars = SystemUtil::makeEnvVarString([ $vars = SystemUtil::makeEnvVarString([
'EXTRA_CFLAGS' => '-g -Os', // with debug information, but optimize for size 'EXTRA_CFLAGS' => '-g -Os', // with debug information, but optimize for size
@@ -173,7 +177,7 @@ class BSDBuilder extends BuilderBase
* @throws RuntimeException * @throws RuntimeException
* @throws WrongUsageException * @throws WrongUsageException
*/ */
public function buildMicro(): void protected function buildMicro(): void
{ {
if ($this->getPHPVersionID() < 80000) { if ($this->getPHPVersionID() < 80000) {
throw new WrongUsageException('phpmicro only support PHP >= 8.0!'); throw new WrongUsageException('phpmicro only support PHP >= 8.0!');
@@ -211,7 +215,7 @@ class BSDBuilder extends BuilderBase
* @throws RuntimeException * @throws RuntimeException
* @throws FileSystemException * @throws FileSystemException
*/ */
public function buildFpm(): void protected function buildFpm(): void
{ {
$vars = SystemUtil::makeEnvVarString([ $vars = SystemUtil::makeEnvVarString([
'EXTRA_CFLAGS' => '-g -Os', // with debug information, but optimize for size 'EXTRA_CFLAGS' => '-g -Os', // with debug information, but optimize for size
@@ -231,7 +235,7 @@ class BSDBuilder extends BuilderBase
* *
* @throws RuntimeException * @throws RuntimeException
*/ */
public function buildEmbed(): void protected function buildEmbed(): void
{ {
$vars = SystemUtil::makeEnvVarString([ $vars = SystemUtil::makeEnvVarString([
'EXTRA_CFLAGS' => '-g -Os', // with debug information, but optimize for size 'EXTRA_CFLAGS' => '-g -Os', // with debug information, but optimize for size

View File

@@ -147,10 +147,13 @@ class LinuxBuilder extends BuilderBase
'LDFLAGS' => '-L' . BUILD_LIB_PATH, 'LDFLAGS' => '-L' . BUILD_LIB_PATH,
'LIBS' => '-ldl -lpthread', 'LIBS' => '-ldl -lpthread',
]); ]);
$this->emitPatchPoint('before-php-buildconf');
SourcePatcher::patchBeforeBuildconf($this); SourcePatcher::patchBeforeBuildconf($this);
shell()->cd(SOURCE_PATH . '/php-src')->exec('./buildconf --force'); shell()->cd(SOURCE_PATH . '/php-src')->exec('./buildconf --force');
$this->emitPatchPoint('before-php-configure');
SourcePatcher::patchBeforeConfigure($this); SourcePatcher::patchBeforeConfigure($this);
$phpVersionID = $this->getPHPVersionID(); $phpVersionID = $this->getPHPVersionID();
@@ -193,6 +196,7 @@ class LinuxBuilder extends BuilderBase
' ' . $envs_build_php . ' ' ' ' . $envs_build_php . ' '
); );
$this->emitPatchPoint('before-php-make');
SourcePatcher::patchBeforeMake($this); SourcePatcher::patchBeforeMake($this);
$this->cleanMake(); $this->cleanMake();
@@ -218,6 +222,7 @@ class LinuxBuilder extends BuilderBase
} }
if (php_uname('m') === $this->getOption('arch')) { if (php_uname('m') === $this->getOption('arch')) {
$this->emitPatchPoint('before-sanity-check');
$this->sanityCheck($build_target); $this->sanityCheck($build_target);
} }
} }
@@ -228,7 +233,7 @@ class LinuxBuilder extends BuilderBase
* @throws RuntimeException * @throws RuntimeException
* @throws FileSystemException * @throws FileSystemException
*/ */
public function buildCli(): void protected function buildCli(): void
{ {
$vars = SystemUtil::makeEnvVarString($this->getBuildVars()); $vars = SystemUtil::makeEnvVarString($this->getBuildVars());
shell()->cd(SOURCE_PATH . '/php-src') shell()->cd(SOURCE_PATH . '/php-src')
@@ -249,7 +254,7 @@ class LinuxBuilder extends BuilderBase
* @throws RuntimeException * @throws RuntimeException
* @throws WrongUsageException * @throws WrongUsageException
*/ */
public function buildMicro(): void protected function buildMicro(): void
{ {
if ($this->getPHPVersionID() < 80000) { if ($this->getPHPVersionID() < 80000) {
throw new WrongUsageException('phpmicro only support PHP >= 8.0!'); throw new WrongUsageException('phpmicro only support PHP >= 8.0!');
@@ -283,7 +288,7 @@ class LinuxBuilder extends BuilderBase
* @throws FileSystemException * @throws FileSystemException
* @throws RuntimeException * @throws RuntimeException
*/ */
public function buildFpm(): void protected function buildFpm(): void
{ {
$vars = SystemUtil::makeEnvVarString($this->getBuildVars()); $vars = SystemUtil::makeEnvVarString($this->getBuildVars());
shell()->cd(SOURCE_PATH . '/php-src') shell()->cd(SOURCE_PATH . '/php-src')
@@ -302,7 +307,7 @@ class LinuxBuilder extends BuilderBase
* *
* @throws RuntimeException * @throws RuntimeException
*/ */
public function buildEmbed(): void protected function buildEmbed(): void
{ {
$vars = SystemUtil::makeEnvVarString($this->getBuildVars()); $vars = SystemUtil::makeEnvVarString($this->getBuildVars());

View File

@@ -209,4 +209,21 @@ class SystemUtil
} }
return $ret; return $ret;
} }
/**
* Get fully-supported linux distros.
*
* @return string[] List of supported Linux distro name for doctor
*/
public static function getSupportedDistros(): array
{
return [
// debian-like
'debian', 'ubuntu', 'Deepin',
// rhel-like
'redhat',
// alpine
'alpine',
];
}
} }

View File

@@ -141,10 +141,12 @@ class MacOSBuilder extends BuilderBase
} }
$this->setOption('extra-libs', $extra_libs); $this->setOption('extra-libs', $extra_libs);
$this->emitPatchPoint('before-php-buildconf');
SourcePatcher::patchBeforeBuildconf($this); SourcePatcher::patchBeforeBuildconf($this);
shell()->cd(SOURCE_PATH . '/php-src')->exec('./buildconf --force'); shell()->cd(SOURCE_PATH . '/php-src')->exec('./buildconf --force');
$this->emitPatchPoint('before-php-configure');
SourcePatcher::patchBeforeConfigure($this); SourcePatcher::patchBeforeConfigure($this);
$json_74 = $this->getPHPVersionID() < 80000 ? '--enable-json ' : ''; $json_74 = $this->getPHPVersionID() < 80000 ? '--enable-json ' : '';
@@ -190,6 +192,7 @@ class MacOSBuilder extends BuilderBase
$envs_build_php $envs_build_php
); );
$this->emitPatchPoint('before-php-make');
SourcePatcher::patchBeforeMake($this); SourcePatcher::patchBeforeMake($this);
$this->cleanMake(); $this->cleanMake();
@@ -215,6 +218,7 @@ class MacOSBuilder extends BuilderBase
} }
if (php_uname('m') === $this->getOption('arch')) { if (php_uname('m') === $this->getOption('arch')) {
$this->emitPatchPoint('before-sanity-check');
$this->sanityCheck($build_target); $this->sanityCheck($build_target);
} }
} }
@@ -225,7 +229,7 @@ class MacOSBuilder extends BuilderBase
* @throws RuntimeException * @throws RuntimeException
* @throws FileSystemException * @throws FileSystemException
*/ */
public function buildCli(): void protected function buildCli(): void
{ {
$vars = SystemUtil::makeEnvVarString($this->getBuildVars()); $vars = SystemUtil::makeEnvVarString($this->getBuildVars());
@@ -244,7 +248,7 @@ class MacOSBuilder extends BuilderBase
* @throws RuntimeException * @throws RuntimeException
* @throws WrongUsageException * @throws WrongUsageException
*/ */
public function buildMicro(): void protected function buildMicro(): void
{ {
if ($this->getPHPVersionID() < 80000) { if ($this->getPHPVersionID() < 80000) {
throw new WrongUsageException('phpmicro only support PHP >= 8.0!'); throw new WrongUsageException('phpmicro only support PHP >= 8.0!');
@@ -280,7 +284,7 @@ class MacOSBuilder extends BuilderBase
* @throws RuntimeException * @throws RuntimeException
* @throws FileSystemException * @throws FileSystemException
*/ */
public function buildFpm(): void protected function buildFpm(): void
{ {
$vars = SystemUtil::makeEnvVarString($this->getBuildVars()); $vars = SystemUtil::makeEnvVarString($this->getBuildVars());
@@ -297,7 +301,7 @@ class MacOSBuilder extends BuilderBase
* *
* @throws RuntimeException * @throws RuntimeException
*/ */
public function buildEmbed(): void protected function buildEmbed(): void
{ {
$vars = SystemUtil::makeEnvVarString($this->getBuildVars()); $vars = SystemUtil::makeEnvVarString($this->getBuildVars());

View File

@@ -49,12 +49,49 @@ trait UnixBuilderTrait
return array_map(fn ($x) => realpath(BUILD_LIB_PATH . "/{$x}"), $libFiles); return array_map(fn ($x) => realpath(BUILD_LIB_PATH . "/{$x}"), $libFiles);
} }
/**
* Return generic cmake options when configuring cmake projects
*/
public function makeCmakeArgs(): string
{
$extra = $this instanceof LinuxBuilder ? '-DCMAKE_C_COMPILER=' . getenv('CC') . ' ' : '';
return $extra .
'-DCMAKE_BUILD_TYPE=Release ' .
'-DCMAKE_INSTALL_PREFIX=/ ' .
'-DCMAKE_INSTALL_BINDIR=/bin ' .
'-DCMAKE_INSTALL_LIBDIR=/lib ' .
'-DCMAKE_INSTALL_INCLUDEDIR=/include ' .
"-DCMAKE_TOOLCHAIN_FILE={$this->cmake_toolchain_file}";
}
/**
* Generate configure flags
*/
public function makeAutoconfFlags(int $flag = AUTOCONF_ALL): string
{
$extra = '';
// TODO: add auto pkg-config support
if (($flag & AUTOCONF_LIBS) === AUTOCONF_LIBS) {
$extra .= 'LIBS="' . BUILD_LIB_PATH . '" ';
}
if (($flag & AUTOCONF_CFLAGS) === AUTOCONF_CFLAGS) {
$extra .= 'CFLAGS="-I' . BUILD_INCLUDE_PATH . '" ';
}
if (($flag & AUTOCONF_CPPFLAGS) === AUTOCONF_CPPFLAGS) {
$extra .= 'CPPFLAGS="-I' . BUILD_INCLUDE_PATH . '" ';
}
if (($flag & AUTOCONF_LDFLAGS) === AUTOCONF_LDFLAGS) {
$extra .= 'LDFLAGS="-L' . BUILD_LIB_PATH . '" ';
}
return $extra;
}
/** /**
* Sanity check after build complete * Sanity check after build complete
* *
* @throws RuntimeException * @throws RuntimeException
*/ */
public function sanityCheck(int $build_target): void protected function sanityCheck(int $build_target): void
{ {
// sanity check for php-cli // sanity check for php-cli
if (($build_target & BUILD_TARGET_CLI) === BUILD_TARGET_CLI) { if (($build_target & BUILD_TARGET_CLI) === BUILD_TARGET_CLI) {
@@ -95,7 +132,7 @@ trait UnixBuilderTrait
* @throws RuntimeException * @throws RuntimeException
* @throws FileSystemException * @throws FileSystemException
*/ */
public function deployBinary(int $type): bool protected function deployBinary(int $type): bool
{ {
$src = match ($type) { $src = match ($type) {
BUILD_TARGET_CLI => SOURCE_PATH . '/php-src/sapi/cli/php', BUILD_TARGET_CLI => SOURCE_PATH . '/php-src/sapi/cli/php',
@@ -114,46 +151,9 @@ trait UnixBuilderTrait
* *
* @throws RuntimeException * @throws RuntimeException
*/ */
public function cleanMake(): void protected function cleanMake(): void
{ {
logger()->info('cleaning up'); logger()->info('cleaning up');
shell()->cd(SOURCE_PATH . '/php-src')->exec('make clean'); shell()->cd(SOURCE_PATH . '/php-src')->exec('make clean');
} }
/**
* Return generic cmake options when configuring cmake projects
*/
public function makeCmakeArgs(): string
{
$extra = $this instanceof LinuxBuilder ? '-DCMAKE_C_COMPILER=' . getenv('CC') . ' ' : '';
return $extra .
'-DCMAKE_BUILD_TYPE=Release ' .
'-DCMAKE_INSTALL_PREFIX=/ ' .
'-DCMAKE_INSTALL_BINDIR=/bin ' .
'-DCMAKE_INSTALL_LIBDIR=/lib ' .
'-DCMAKE_INSTALL_INCLUDEDIR=/include ' .
"-DCMAKE_TOOLCHAIN_FILE={$this->cmake_toolchain_file}";
}
/**
* Generate configure flags
*/
public function makeAutoconfFlags(int $flag = AUTOCONF_ALL): string
{
$extra = '';
// TODO: add auto pkg-config support
if (($flag & AUTOCONF_LIBS) === AUTOCONF_LIBS) {
$extra .= 'LIBS="' . BUILD_LIB_PATH . '" ';
}
if (($flag & AUTOCONF_CFLAGS) === AUTOCONF_CFLAGS) {
$extra .= 'CFLAGS="-I' . BUILD_INCLUDE_PATH . '" ';
}
if (($flag & AUTOCONF_CPPFLAGS) === AUTOCONF_CPPFLAGS) {
$extra .= 'CPPFLAGS="-I' . BUILD_INCLUDE_PATH . '" ';
}
if (($flag & AUTOCONF_LDFLAGS) === AUTOCONF_LDFLAGS) {
$extra .= 'LDFLAGS="-L' . BUILD_LIB_PATH . '" ';
}
return $extra;
}
} }

View File

@@ -35,6 +35,7 @@ class BuildCliCommand extends BuildCommand
$this->addOption('with-micro-fake-cli', null, null, 'Enable phpmicro fake cli'); $this->addOption('with-micro-fake-cli', null, null, 'Enable phpmicro fake cli');
$this->addOption('with-suggested-libs', 'L', null, 'Build with suggested libs for selected exts and libs'); $this->addOption('with-suggested-libs', 'L', null, 'Build with suggested libs for selected exts and libs');
$this->addOption('with-suggested-exts', 'E', null, 'Build with suggested extensions for selected exts'); $this->addOption('with-suggested-exts', 'E', null, 'Build with suggested extensions for selected exts');
$this->addOption('with-added-patch', 'P', InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Inject patch script outside');
} }
public function handle(): int public function handle(): int

View File

@@ -188,7 +188,8 @@ class DownloadCommand extends BaseCommand
// create downloads // create downloads
try { try {
if (PHP_OS_FAMILY !== 'Windows') { if (PHP_OS_FAMILY !== 'Windows') {
f_passthru('mkdir ' . DOWNLOAD_PATH . ' && cd ' . DOWNLOAD_PATH . ' && unzip ' . escapeshellarg($path)); $abs_path = realpath($path);
f_passthru('mkdir ' . DOWNLOAD_PATH . ' && cd ' . DOWNLOAD_PATH . ' && unzip ' . escapeshellarg($abs_path));
} }
// Windows TODO // Windows TODO

View File

@@ -62,6 +62,7 @@ class LinuxToolCheckList
'ubuntu', 'ubuntu',
'alpine', 'alpine',
'redhat', 'redhat',
'Deepin',
'debian' => CheckResult::fail(implode(', ', $missing) . ' not installed on your system', 'install-linux-tools', [$distro, $missing]), 'debian' => CheckResult::fail(implode(', ', $missing) . ' not installed on your system', 'install-linux-tools', [$distro, $missing]),
default => CheckResult::fail(implode(', ', $missing) . ' not installed on your system'), default => CheckResult::fail(implode(', ', $missing) . ' not installed on your system'),
}; };
@@ -70,7 +71,7 @@ class LinuxToolCheckList
} }
/** @noinspection PhpUnused */ /** @noinspection PhpUnused */
#[AsCheckItem('if necessary packages are installed', limit_os: 'Linux')] #[AsCheckItem('if necessary linux headers are installed', limit_os: 'Linux')]
public function checkSystemOSPackages(): ?CheckResult public function checkSystemOSPackages(): ?CheckResult
{ {
if (SystemUtil::isMuslDist()) { if (SystemUtil::isMuslDist()) {
@@ -90,7 +91,7 @@ class LinuxToolCheckList
public function fixBuildTools(array $distro, array $missing): bool public function fixBuildTools(array $distro, array $missing): bool
{ {
$install_cmd = match ($distro['dist']) { $install_cmd = match ($distro['dist']) {
'ubuntu', 'debian' => 'apt-get install -y', 'ubuntu', 'debian', 'Deepin' => 'apt-get install -y',
'alpine' => 'apk add', 'alpine' => 'apk add',
'redhat' => 'dnf install -y', 'redhat' => 'dnf install -y',
default => throw new RuntimeException('Current linux distro does not have an auto-install script for musl packages yet.'), default => throw new RuntimeException('Current linux distro does not have an auto-install script for musl packages yet.'),
@@ -101,7 +102,7 @@ class LinuxToolCheckList
logger()->warning('Current user is not root, using sudo for running command'); logger()->warning('Current user is not root, using sudo for running command');
} }
try { try {
$is_debian = in_array($distro['dist'], ['debian', 'ubuntu']); $is_debian = in_array($distro['dist'], ['debian', 'ubuntu', 'Deepin']);
$to_install = $is_debian ? str_replace('xz', 'xz-utils', $missing) : $missing; $to_install = $is_debian ? str_replace('xz', 'xz-utils', $missing) : $missing;
// debian, alpine libtool -> libtoolize // debian, alpine libtool -> libtoolize
$to_install = str_replace('libtoolize', 'libtool', $to_install); $to_install = str_replace('libtoolize', 'libtool', $to_install);

View File

@@ -13,13 +13,14 @@ class OSCheckList
{ {
use UnixSystemUtilTrait; use UnixSystemUtilTrait;
#[AsCheckItem('if current OS are supported', level: 999)] #[AsCheckItem('if current OS are supported', level: 1000)]
public function checkOS(): ?CheckResult public function checkOS(): ?CheckResult
{ {
if (!in_array(PHP_OS_FAMILY, ['Darwin', 'Linux', 'BSD', 'Windows'])) { if (!in_array(PHP_OS_FAMILY, ['Darwin', 'Linux', 'BSD', 'Windows'])) {
return CheckResult::fail('Current OS is not supported: ' . PHP_OS_FAMILY); return CheckResult::fail('Current OS is not supported: ' . PHP_OS_FAMILY);
} }
$distro = PHP_OS_FAMILY === 'Linux' ? (' ' . SystemUtil::getOSRelease()['dist']) : ''; $distro = PHP_OS_FAMILY === 'Linux' ? (' ' . SystemUtil::getOSRelease()['dist']) : '';
return CheckResult::ok(PHP_OS_FAMILY . ' ' . php_uname('m') . $distro . ', supported'); $known_distro = PHP_OS_FAMILY === 'Linux' && in_array(SystemUtil::getOSRelease()['dist'], SystemUtil::getSupportedDistros());
return CheckResult::ok(PHP_OS_FAMILY . ' ' . php_uname('m') . $distro . ', supported' . ($known_distro ? '' : ' (but not tested on this distro)'));
} }
} }

View File

@@ -3,6 +3,8 @@
declare(strict_types=1); declare(strict_types=1);
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use SPC\builder\BuilderBase;
use SPC\builder\BuilderProvider;
use SPC\exception\RuntimeException; use SPC\exception\RuntimeException;
use SPC\exception\WrongUsageException; use SPC\exception\WrongUsageException;
use SPC\util\UnixShell; use SPC\util\UnixShell;
@@ -127,3 +129,23 @@ function cmd(?bool $debug = null): WindowsCmd
{ {
return new WindowsCmd($debug); return new WindowsCmd($debug);
} }
/**
* Get current builder.
*
* @throws WrongUsageException
*/
function builder(): BuilderBase
{
return BuilderProvider::getBuilder();
}
/**
* Get current patch point.
*
* @throws WrongUsageException
*/
function patch_point(): string
{
return BuilderProvider::getBuilder()->getPatchPoint();
}