Compare commits

...

87 Commits

Author SHA1 Message Date
crazywhalecc
e6c9112575 add option --no-motd 2023-04-23 20:54:51 +08:00
crazywhalecc
f0a987ba3a update README 2023-04-23 20:32:56 +08:00
crazywhalecc
42b2d36319 add fpm 2023-04-23 20:31:58 +08:00
crazywhalecc
5d5ffe8866 fix zlib source 2023-04-23 20:29:59 +08:00
crazywhalecc
8cd45e3f3f update README 2023-04-23 20:29:50 +08:00
crazywhalecc
baf513bd56 change build CI to new builder 2023-04-23 20:29:30 +08:00
Jerry Ma
5333da35cb Update README.md 2023-04-23 13:30:56 +08:00
Jerry Ma
fe85f138df Update README-en.md 2023-04-22 23:59:36 +08:00
crazywhalecc
684149eb50 update setup time 2023-04-22 23:00:30 +08:00
crazywhalecc
8018f2c570 update README, add badge 2023-04-22 22:52:02 +08:00
crazywhalecc
2fdbe6ba8c update README 2023-04-22 22:41:00 +08:00
crazywhalecc
018c76ba41 update README 2023-04-22 22:25:01 +08:00
crazywhalecc
48f40b4304 update README 2023-04-22 22:18:44 +08:00
crazywhalecc
528ad1199a add doctor command 2023-04-22 21:23:12 +08:00
crazywhalecc
4c0d35c723 refactor command as more easily 2023-04-22 17:45:43 +08:00
Jerry Ma
1540af266b Merge pull request #38 from jingjingxyk/dev
添加快速进入编译环境的脚本
2023-04-22 17:24:03 +08:00
crazywhalecc
8fb8b34239 enhancement for prepare-php-runtime: integrate in one file 2023-04-22 17:18:21 +08:00
crazywhalecc
4b7c2fe84f set php 7.4 patch alternative 2023-04-22 16:00:34 +08:00
crazywhalecc
61a76dc0c9 set php 7.4 patch alternative 2023-04-22 15:58:24 +08:00
jingjingxyk
e4de37708b 修改快速运行构建环境脚本 2023-04-22 11:20:36 +08:00
jingjingxyk
ea40d25125 修改快速运行构建环境脚本 2023-04-22 11:12:16 +08:00
jingjingxyk
eacb24a482 修改 linux X86_64 README.md 2023-04-22 02:14:23 +08:00
jingjingxyk
65a32a977f 添加快速进入编译环境脚本 2023-04-22 02:05:26 +08:00
jingjingxyk
40d09bc82e 添加快速构建脚本 2023-04-22 01:32:17 +08:00
crazywhalecc
fa437644f5 update README 2023-04-21 19:10:23 +08:00
crazywhalecc
762f6043c5 update README 2023-04-15 21:44:34 +08:00
crazywhalecc
ab1109df90 update README 2023-04-15 21:29:05 +08:00
crazywhalecc
c40da1a71a update CI 2023-04-15 19:14:15 +08:00
crazywhalecc
05490c1785 update CI 2023-04-15 19:07:08 +08:00
crazywhalecc
103f76e190 update README 2023-04-15 19:05:59 +08:00
crazywhalecc
01bf2ae63b update README 2023-04-15 18:58:29 +08:00
crazywhalecc
03b987c78e update ext-support.md 2023-04-15 18:56:05 +08:00
crazywhalecc
960addc8f2 add license dumper 2023-04-15 18:46:46 +08:00
crazywhalecc
5b09e95e51 fix argument multiple space 2023-04-15 18:46:21 +08:00
crazywhalecc
2bbb66f844 change file system removeDir function 2023-04-15 18:46:02 +08:00
crazywhalecc
4777f86f7e change macos Builder f_passthru 2023-04-15 18:45:34 +08:00
crazywhalecc
f95b3bcd4b change extension to custom 2023-04-15 18:45:11 +08:00
Jerry
6de0d81ea3 Update README-en.md 2023-04-14 13:38:58 +08:00
Jerry
31fee5645d Update README.md 2023-04-14 13:35:44 +08:00
Jerry
651bb747f1 add CI php build verson display 2023-04-14 13:29:43 +08:00
Jerry
52d787c903 Update build.yml 2023-04-12 15:40:49 +08:00
crazywhalecc
0b4253090d remove hash 2023-04-09 14:17:25 +08:00
crazywhalecc
50a1fcfc20 update README 2023-04-09 13:58:09 +08:00
crazywhalecc
e2aa21fbea update README 2023-04-09 13:38:41 +08:00
crazywhalecc
62aeb6dc7b fix gd check 2023-04-09 13:24:52 +08:00
crazywhalecc
a7d500422d add postgresql and custom downloader 2023-04-09 12:12:32 +08:00
crazywhalecc
76e95b8c55 add postgresql and custom downloader 2023-04-09 12:11:14 +08:00
crazywhalecc
9970986a52 fix libpng not executable 2023-04-09 12:10:09 +08:00
crazywhalecc
abfc60511d update README 2023-04-09 10:56:14 +08:00
crazywhalecc
663d0c05c7 update README 2023-04-09 10:41:50 +08:00
crazywhalecc
76c39ef08a update README 2023-04-08 22:35:10 +08:00
crazywhalecc
bcd45e87c8 fix ci 2023-04-08 22:22:52 +08:00
crazywhalecc
5c54434300 fix ci 2023-04-08 22:16:56 +08:00
crazywhalecc
feb20fad83 fix ci 2023-04-08 22:15:48 +08:00
crazywhalecc
bb52b87d2f fix ci 2023-04-08 22:08:20 +08:00
crazywhalecc
40b6980cdc update action and version 2023-04-08 22:05:10 +08:00
crazywhalecc
bbeb399004 fix action 2023-04-08 21:39:51 +08:00
crazywhalecc
bdf99ba9f1 update freetype link 2023-04-08 21:36:39 +08:00
crazywhalecc
9161aff942 update readme 2023-04-08 21:23:30 +08:00
crazywhalecc
298a758166 add linux github ci 2023-04-08 21:20:22 +08:00
crazywhalecc
000ebef28b add inotify 2023-04-08 18:55:23 +08:00
crazywhalecc
54efec60b8 change php download url back to official 2023-04-08 18:46:12 +08:00
crazywhalecc
7f5f15a41d update ext-support.md 2023-04-08 18:45:49 +08:00
crazywhalecc
2766450696 fix openssl with zlib no symbol bug 2023-04-08 18:45:37 +08:00
crazywhalecc
24c6a3a637 adjust curl support, fix curl bug 2023-04-08 18:45:16 +08:00
crazywhalecc
d3b8baa5e7 support swow 2023-04-08 18:44:46 +08:00
crazywhalecc
fd57d40ca9 add extension dependency sort (including suggestion) 2023-04-08 18:42:46 +08:00
crazywhalecc
feae9b59f5 remove libffi support default 2023-04-08 18:36:10 +08:00
crazywhalecc
24ce235ed4 add swow macos support 2023-04-08 12:42:58 +08:00
crazywhalecc
31a02082de sort them 2023-04-08 11:58:24 +08:00
crazywhalecc
f4ebca927b add dist name getter for extension 2023-04-08 11:49:06 +08:00
crazywhalecc
84daa9b151 remove enchant temporarily 2023-04-08 11:48:30 +08:00
Simon Hamp
213a6680dc Add GitHub Action (#35)
* Create build.yml

* Update build.yml

* Update build.yml

* Update FetchSourceCommand.php

Attempt to fix exception reporting error when cURL fails

* Update Downloader.php

Attempt to solve GitHub API requests bailing in the Action runner

* Update Downloader.php

Revert

* Update build.yml

Add env variable

* Update build.yml

* Update build.yml

* Update build.yml

* Update build.yml

* Update build.yml

* Update build.yml

* Update build.yml

* Update build.yml

* Update build.yml

* Update build.yml

* Update build.yml

Revert. It didn't help :(
2023-04-08 10:35:31 +08:00
crazywhalecc
978b59c0dc fix suggested libs dependency order 2023-04-07 22:11:41 +08:00
crazywhalecc
4063b8454b adjust readme 2023-04-03 21:30:32 +08:00
crazywhalecc
29e0269588 add some tests and iconv support 2023-04-03 21:27:50 +08:00
crazywhalecc
1731cc1202 add bz2 test 2023-04-03 20:51:02 +08:00
crazywhalecc
09e5c16570 refactor macos builder 2023-04-03 20:47:24 +08:00
crazywhalecc
3709bcc5e4 update readme 2023-03-31 00:16:40 +08:00
crazywhalecc
b9b686b9cf Merge remote-tracking branch 'origin/refactor' into refactor 2023-03-29 21:39:47 +08:00
crazywhalecc
b0ac50941f add wrong usage exception 2023-03-29 21:39:36 +08:00
jingjingxyk
b8a58b270c 更正 env 所在目录 2023-03-28 23:51:57 +08:00
jingjingxyk
a20cc5bd26 修复 spc 入口 shell 2023-03-28 23:51:57 +08:00
crazywhalecc
d31e1b00e8 update README.md 2023-03-27 00:13:52 +08:00
crazywhalecc
1f11e25593 remove gzip and change deploy file default name 2023-03-27 00:11:00 +08:00
crazywhalecc
6af40cd92d fix micro build and change deploy command args 2023-03-26 23:59:34 +08:00
crazywhalecc
84b1732683 refactor and add more linux extensions and libraries 2023-03-26 22:27:51 +08:00
117 changed files with 3598 additions and 1293 deletions

129
.github/workflows/build.yml vendored Normal file
View File

@@ -0,0 +1,129 @@
name: CI
on:
workflow_dispatch:
inputs:
operating-system:
required: true
description: Compile target OS
type: choice
options:
- ubuntu-latest
- macos-latest
version:
required: true
description: php version to compile
default: '8.2'
type: choice
options:
- '8.2'
- '8.1'
- '8.0'
- '7.4'
build-cli:
description: build cli binary
default: true
type: boolean
build-micro:
description: build phpmicro binary
type: boolean
build-fpm:
description: build fpm binary
type: boolean
extensions:
description: extensions to compile (comma separated)
required: true
type: string
debug:
type: boolean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
jobs:
build:
name: build ${{ inputs.version }} on ${{ inputs.operating-system }}
runs-on: ${{ inputs.operating-system }}
steps:
- uses: actions/checkout@v3
# Install macOS missing packages and mark os suffix
- if: ${{ inputs.operating-system == 'macos-latest' }}
run: |
brew install automake gzip
echo "SPC_BUILD_OS=macos" >> $GITHUB_ENV
# Install Ubuntu missing packages and mark os suffix
- if: ${{ inputs.operating-system == 'ubuntu-latest' }}
run: |
sudo apt install musl-tools -y
echo "SPC_BUILD_OS=linux" >> $GITHUB_ENV
# Cache composer dependencies
- id: cache-composer-deps
uses: actions/cache@v3
with:
path: vendor
key: composer-dependencies
# If there's no Composer cache, install dependencies
- if: steps.cache-composer-deps.outputs.cache-hit != 'true'
run: composer update --no-dev
# Cache downloaded source
- id: cache-download
uses: actions/cache@v3
with:
path: downloads
key: php-${{ inputs.version }}-dependencies
# With or without debug
- if: inputs.debug == true
run: echo "SPC_BUILD_DEBUG=--debug" >> $GITHUB_ENV
# With target select: cli, micro or both
- if: ${{ inputs.build-cli == true }}
run: echo "SPC_BUILD_CLI=--build-cli" >> $GITHUB_ENV
- if: ${{ inputs.build-micro == true }}
run: echo "SPC_BUILD_MICRO=--build-micro" >> $GITHUB_ENV
- if: ${{ inputs.build-fpm == true }}
run: echo "SPC_BUILD_FPM=--build-fpm" >> $GITHUB_ENV
# If there's no dependencies cache, fetch sources, with or without debug
- run: CACHE_API_EXEC=yes ./bin/spc fetch --with-php=${{ inputs.version }} --all ${{ env.SPC_BUILD_DEBUG }}
# Run build command
- run: ./bin/spc build ${{ inputs.extensions }} ${{ env.SPC_BUILD_DEBUG }} ${{ env.SPC_BUILD_CLI }} ${{ env.SPC_BUILD_MICRO }} ${{ env.SPC_BUILD_FPM }}
# Upload cli executable
- if: ${{ inputs.build-cli == true }}
uses: actions/upload-artifact@v3
with:
name: php-${{ inputs.version }}-${{ env.SPC_BUILD_OS }}
path: buildroot/bin/php
# Upload micro self-extracted executable
- if: ${{ inputs.build-micro == true }}
uses: actions/upload-artifact@v3
with:
name: micro-${{ inputs.version }}-${{ env.SPC_BUILD_OS }}
path: buildroot/bin/micro.sfx
# Upload fpm executable
- if: ${{ inputs.build-fpm == true }}
uses: actions/upload-artifact@v3
with:
name: php-fpm-${{ inputs.version }}-${{ env.SPC_BUILD_OS }}
path: buildroot/bin/php-fpm
# Upload extensions metadata
- uses: actions/upload-artifact@v3
with:
name: license-files
path: buildroot/license/
- uses: actions/upload-artifact@v3
with:
name: build-meta
path: |
buildroot/build-extensions.json
buildroot/build-libraries.json

5
.gitignore vendored
View File

@@ -19,3 +19,8 @@ composer.lock
# php cs fixer cache file
.php-cs-fixer.cache
# exclude self-runtime
/bin/*
!/bin/spc
!/bin/setup-runtime

View File

@@ -5,62 +5,144 @@ 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!
You can also use the micro binary file to package PHP source code and binary files into one for distribution!
This function is provided by [dixyes/phpmicro](https://github.com/dixyes/phpmicro).
Note: only support cli SAPI, not support fpm, cgi.
> This branch is new version, if you are looking for old bash version of static-php-cli, see [bash-version](https://github.com/crazywhalecc/static-php-cli/tree/bash-version).
[![Version](https://img.shields.io/badge/Version-2.0--beta1-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/build.yml?branch=refactor&label=Actions%20Build&style=flat-square)](https://github.com/crazywhalecc/static-php-cli/actions/workflows/build.yml)
[![](https://img.shields.io/github/search/crazywhalecc/static-php-cli/TODO?label=TODO%20Counter&style=flat-square)]()
## Compilation Requirements
Yes, this project is written in PHP, pretty funny.
But php-static-cli only requires an environment above PHP 8.0.
But static-php-cli runtime only requires an environment above PHP 8.0 and `tokenizer`, `iconv` extension.
- Linux
- Supported arch: aarch64, amd64
- Supported distributions: alpine, ubuntu, centos
- Requirements: (TODO)
- macOS
- Supported arch: arm64, x86_64
- Requirements: make, bison, flex, pkg-config, git, autoconf, automake, tar, unzip, xz, gzip, bzip2, cmake
- Windows
- Supported arch: x86_64
- Requirements: (TODO)
- PHP
- Supported version: 8.0, 8.1, 8.2
Here is the architecture support status, where `CI` represents support for GitHub Action builds,
`Local` represents support for local builds, and blank represents not currently supported.
## Usage (WIP)
| | x86_64 | aarch64 |
|---------|-----------|---------|
| macOS | CI, Local | Local |
| Linux | CI, Local | Local |
| Windows | | |
After stable release for this project, a single phar and single binary for this tool will be published.
> linux-aarch64 and 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.
And currently you may need to clone this branch and edit GitHub Action to build.
Currently supported PHP versions for compilation are: `7.4`, `8.0`, `8.1`, `8.2`.
### Compilation
## Usage
Please first select the extension you want to compile based on the extension list below.
### Supported Extensions
[Supported Extension List](/ext-support.md)
> If there is no extension you need here, you can submit an issue.
### GitHub Actions Build
Use GitHub Action to easily build a statically compiled PHP and phpmicro,
and at the same time define the extensions to be compiled by yourself.
1. Fork me.
2. Go to the Actions of the project and select `CI`.
3. Select `Run workflow`, fill in the PHP version you want to compile, the target type, and the list of extensions. (extensions comma separated, e.g. `bcmath,curl,mbstring`)
4. After waiting for about a period of time, enter the corresponding task and get `Artifacts`.
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.
- When using macos-latest, it will build macOS-x86_64 binary.
### Manual Build
Clone repo first:
```bash
chmod +x spc
git clone https://github.com/crazywhalecc/static-php-cli.git
```
If you have not installed php on your system, you can download single-file php binary and composer first.
The PHP runtime for static-php-cli itself will be downloaded at `bin/php`, and composer is at `bin/composer`.
```bash
cd static-php-cli
chmod +x bin/setup-runtime
./bin/setup-runtime
# Use this php runtime to run static-php-cli compiler
./bin/php bin/spc
# Use composer
./bin/php bin/composer
```
Basic usage for building php and micro with some extensions:
```bash
cd static-php-cli
composer update
chmod +x bin/spc
# Check system tool dependencies, fix them automatically (only support macOS) (TODO: Linux distro support)
./bin/spc doctor
# fetch all libraries
./spc fetch --all
./bin/spc fetch --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
./bin/spc build bcmath,openssl,tokenizer,sqlite3,pdo_sqlite,ftp,curl --build-cli --build-micro
```
You can also use the parameter `--with-php=x.y` to specify the downloaded PHP version, currently supports 7.4 ~ 8.2:
```bash
# Using PHP >= 8.0 is recommended, because 7.4 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-micro`: build static phpmicro self-extracted executable
- `--build-fpm`: build static fpm binary
- `--build-all`: build all
If anything goes wrong, use `--debug` option to display full terminal output:
```bash
./bin/spc build openssl,pcntl,mbstring --debug --build-all
./bin/spc fetch --all --debug
```
### php-cli Usage
When using the parameter `--build-all` or not adding the `--build-micro` parameter,
> php-cli is a single static binary, you can use it like normal php installed on your system.
When using the parameter `--build-cli` or `--build-all`,
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.
This file will be located in the directory `buildroot/bin/`, copy it out for use.
```bash
./php -v
./php -m
./php your_code.php
cd buildroot/bin/
./php -v # check version
./php -m # check extensions
./php your_code.php # run your php code
./php your_project.phar # run your phar (project archive)
```
### micro.sfx Usage
> phpmicro is a Self-Extracted Executable SAPI module,
> provided by [dixyes/phpmicro](https://github.com/dixyes/phpmicro).
> It can put php runtime and your source code together.
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.
This file will be located in the path `buildroot/bin/micro.sfx`, simply copy it out for use.
Prepare your project source code, which can be a single PHP file or a Phar file, for use.
@@ -69,23 +151,33 @@ 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.
# If packing a PHAR file, replace code.php with the Phar file path.
```
> In some cases, PHAR files may not run in a micro environment.
### php-fpm Usage
When using the parameter `--build-all` or `--build-fpm`,
the final compilation result will output a file named `./php-fpm`,
This file will be located in the path `buildroot/bin/`, simply copy it out for use.
In normal Linux distributions and macOS systems, the package manager will automatically generate a default fpm configuration file after installing php-fpm.
Because php-fpm must specify a configuration file before running, the php-fpm compiled by this project will not have any configuration files, so you need to write `php-fpm.conf` and `pool.conf` configuration files yourself.
Specifying `php-fpm.conf` can use the command parameter `-y`, for example: `./php-fpm -y php-fpm.conf`.
## Current Status
- [X] Basic CLI framework (by symfony/console)
- [ ] Linux support
- [X] Basic CLI framework (by `symfony/console`)
- [X] Linux support
- [X] macOS support
- [X] Exception handler
- [ ] Windows support
- [X] PHP 7.4 support
- [X] fpm support
## Supported Extensions (WIP)
[Support Extension List](/ext-support.md)
More functions and features are coming soon, Bugs and TODOs: https://github.com/crazywhalecc/static-php-cli/issues/32
## Contribution
@@ -107,8 +199,7 @@ The basic principles for contributing are as follows:
## 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.
Special thanks to:
while the new version references source code from some other projects:
- [dixyes/lwmbs](https://github.com/dixyes/lwmbs) (Mulun Permissive License)
- [swoole/swoole-cli](https://github.com/swoole/swoole-cli) (Apache 2.0 LICENSE+SWOOLE-CLI LICENSE)
@@ -119,3 +210,9 @@ and they all have their own open source licenses.
Please use the `dump-license`(TODO) command to export the open source licenses used in the project after compilation,
and comply with the corresponding project's LICENSE.
## Advanced
This project is pure open source project, and some modules are separated for developing.
This section will be improved after refactor version released.

169
README.md
View File

@@ -1,65 +1,139 @@
# static-php-cli
Compile A Statically Linked PHP With Swoole and other Extensions. [English README](README-en.md)
Compile A Statically Linked PHP With Swoole and other Extensions.
If you are using English, see [English README](README-en.md).
编译纯静态的 PHP Binary 二进制文件,带有各种扩展,让 PHP-cli 应用变得更便携!
同时可以使用 micro 二进制文件,将 PHP 源码和 PHP 二进制构建为一个文件分发!
同时可以使用 micro 二进制文件,将 PHP 源码和 PHP 二进制构建为一个文件分发!(由 [dixyes/phpmicro](https://github.com/dixyes/phpmicro) 提供支持)
注:只能编译 CLI 模式,暂不支持 CGI 和 FPM 模式
> 此分支为重构的新版,如果你在找纯 Bash 编写的旧版本,请到 [bash-version 分支](https://github.com/crazywhalecc/static-php-cli/tree/bash-version)
[![License](https://img.shields.io/badge/License-MIT-blue.svg)]()
[![Version](https://img.shields.io/badge/Version-2.0--beta1-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/build.yml?branch=refactor&label=Actions%20Build&style=flat-square)](https://github.com/crazywhalecc/static-php-cli/actions/workflows/build.yml)
[![](https://img.shields.io/github/search/crazywhalecc/static-php-cli/TODO?label=TODO%20Counter&style=flat-square)]()
## 编译环境需求
是的,本项目采用 PHP 编写,编译前需要一个 PHP 环境,比较滑稽。
但本项目默认可通过自身构建的 micro 和 static-php 二进制运行,其他只需要包含 tokenizer 扩展和 PHP 版本大于等于 8.0 即可。
- Linux
- 支持架构: aarch64, amd64
- 支持发行版: alpine, ubuntu, centos
- 依赖工具: make, bison, flex, pkg-config, git, autoconf, automake, tar, unzip, gzip, bzip2, cmake
- macOS
- 支持架构: arm64, x86_64
- 依赖工具: make, bison, flex, pkg-config, git, autoconf, automake, tar, unzip, xz, gzip, bzip2, cmake
- Windows
- 支持架构: x86_64
- 依赖工具: (TODO)
- PHP
- 支持版本: 8.0, 8.1, 8.2
下面是架构支持情况,`CI` 代表支持 GitHub Action 构建,`Local` 代表支持本地构建,空 代表暂不支持。
## 使用WIP
| | x86_64 | aarch64 |
|---------|-----------|---------|
| macOS | CI, Local | Local |
| Linux | CI, Local | Local |
| Windows | | |
> 你正在看的是重构后的 static-php-cli 编译项目,新项目还未完全重构,所以还有大量的扩展没有完成
> 你可以阅读使用 bash 编写的仅为 Linux 系统使用的静态编译脚本和 Docker详见 bash-version 分支。 旧版本未来将会切换为次要版本,提供有限支持。
> linux-aarch64 and macOS-arm64 因 GitHub 暂未提供 arm runner如果要构建 arm 二进制,可以使用手动构建
未来会提供一个直接可使用的 phar 包和一个 phpmicro 打包的二进制文件,你可以直接从 Release 中获取并使用:
目前支持编译的 PHP 版本为:`7.4``8.0``8.1``8.2`
### 编译
## 使用
请先根据下方扩展列表选择你要编译的扩展。
### 支持的扩展情况
[扩展支持列表](/ext-support.md)
> 如果这里没有你需要的扩展,可以提交 Issue。
### 使用 Actions 构建
使用 GitHub Action 可以方便地构建一个静态编译的 PHP 和 phpmicro同时可以自行定义要编译的扩展。
1. Fork 本项目。
2. 进入项目的 Actions选择 CI。
3. 选择 `Run workflow`,填入你要编译的 PHP 版本、目标类型、扩展列表。(扩展列表使用英文逗号分割,例如 `bcmath,curl,mbstring`
4. 等待大约一段时间后,进入对应的任务中,获取 `Artifacts`
如果你选择了 `debug`,则会在构建时输出所有日志,包括编译的日志,以供排查错误。
### 手动构建
先克隆本项目:
```bash
chmod +x spc
git clone https://github.com/crazywhalecc/static-php-cli.git
```
如果你本机没有安装 PHP你可以通过命令下载静态编译好的 php-cli 和 Composer。
下载的 php 和 Composer 将保存为 `bin/php``bin/composer`
```bash
cd static-php-cli
chmod +x bin/setup-runtime
./bin/setup-runtime
# 使用独立的 php 运行 static-php-cli
./bin/php bin/spc
# 使用 composer
./bin/php bin/composer
```
下面是使用 static-php-cli 编译静态 php 和 micro 的基础用法:
```bash
# 克隆本项目
cd static-php-cli
composer update
chmod +x bin/spc
# 检查环境依赖,并根据提示的命令安装缺失的编译工具(目前仅支持 macOSLinux 后续会支持)
./bin/spc doctor
# 拉取所有依赖库
./spc fetch --all
./bin/spc fetch --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
./bin/spc build "bcmath,openssl,tokenizer,sqlite3,pdo_sqlite,ftp,curl" --build-cli --build-micro
```
你也可以使用参数 `--with-php=x.y` 来指定下载的 PHP 版本,目前支持 7.4 ~ 8.2
```bash
# 优先考虑使用 >= 8.0 的 PHP 版本
./bin/spc fetch --with-php=8.2 --all
```
其中,目前支持构建 climicrofpm 三种静态二进制,使用以下参数的一个或多个来指定编译的 SAPI
- `--build-cli`:构建 cli 二进制
- `--build-micro`:构建 phpmicro 自执行二进制
- `--build-fpm`:构建 fpm
- `--build-all`:构建所有
如果出现了任何错误,可以使用 `--debug` 参数来展示完整的输出日志,以供排查错误:
```bash
./bin/spc build openssl,pcntl,mbstring --debug --build-all
./bin/spc fetch --all --debug
```
### 使用 php-cli
采用参数 `--build-all` 或不添加 `--build-micro` 参数时,最后编译结果会输出一个 `./php` 的二进制文件,此文件可分发、可直接使用
该文件编译后会存放在 `source/php-src/sapi/cli/` 目录中,拷贝出来即可。
> php-cli 是一个静态的二进制文件,类似 Go、Rust 语言编译后的单个可移植的二进制文件
采用参数 `--build-cli``--build-all` 参数时,最后编译结果会输出一个 `./php` 的二进制文件,此文件可分发、可直接使用。
该文件编译后会存放在 `buildroot/bin/` 目录中,名称为 `php`,拷贝出来即可。
```bash
./php -v
./php -m
./php your_code.php
cd buildroot/bin/
./php -v # 检查版本
./php -m # 检查编译的扩展
./php your_code.php # 运行代码
./php your_project.phar # 运行打包为 phar 单文件的项目
```
### 使用 micro.sfx
采用项目参数 `--build-all``--build-micro` 时,最后编译结果会输出一个 `./micro.sfx` 的文件,此文件需要配合你的 PHP 源码使用
该文件编译后会存放在 `source/php-src/sapi/micro/` 目录中,拷贝出来即可。
> phpmicro 是一个提供自执行二进制 PHP 的项目,本项目依赖 phpmicro 进行编译自执行二进制。详见 [dixyes/phpmicro](https://github.com/dixyes/phpmicro)
采用项目参数 `--build-micro``--build-all` 时,最后编译结果会输出一个 `./micro.sfx` 的文件,此文件需要配合你的 PHP 源码使用。
该文件编译后会存放在 `buildroot/bin/` 目录中,拷贝出来即可。
使用时应准备好你的项目源码文件,可以是单个 PHP 文件,也可以是 Phar 文件。
@@ -73,18 +147,27 @@ cat micro.sfx code.php > single-app && chmod +x single-app
> 有些情况下的 phar 文件可能无法在 micro 环境下运行。
## 项目支持情况WIP
### 使用 php-fpm
- [X] 基础结构编写(采用 symfony/console`
采用项目参数 `--build-fpm``--build-all` 时,最后编译结果会输出一个 `./php-fpm` 的文件。
该文件存放在 `buildroot/bin/` 目录,拷贝出来即可使用。
在正常的 Linux 发行版和 macOS 系统中,安装 php-fpm 后包管理会自动生成默认的 fpm 配置文件。
因为 php-fpm 必须指定配置文件才可启动,本项目编译的 php-fpm 不会带任何配置文件,所以需自行编写 `php-fpm.conf``pool.conf` 配置文件。
指定 `php-fpm.conf` 可以使用命令参数 `-y`,例如:`./php-fpm -y php-fpm.conf`
## 项目支持情况
- [X] 基础结构编写(采用 `symfony/console`
- [X] 错误处理
- [X] macOS 支持
- [ ] Windows 支持
- [ ] Linux 支持
- [X] Linux 支持
- [X] PHP 7.4 支持
- [X] fpm 支持
## 支持的扩展情况WIP
[扩展支持列表](/ext-support.md)
更多功能和特性正在陆续支持中详见https://github.com/crazywhalecc/static-php-cli/issues/32
## 贡献
@@ -97,12 +180,20 @@ cat micro.sfx code.php > single-app && chmod +x single-app
- 应遵循命名规范,例如扩展名称应采取 PHP 内注册的扩展名本身,外部库名应遵循项目本身的名称,内部逻辑的函数、类名、变量等应遵循驼峰、下划线等格式,禁止同一模块混用。
- 涉及编译外部库的命令和 Patch 时应注意兼容不同操作系统。
另外,添加新扩展的贡献方式,可以参考下方 `进阶`
## 开源协议
本项目依据旧版本惯例采用 MIT License 开源,新版本采用了部分项目的源代码做参考,特别感谢
本项目依据旧版本惯例采用 MIT License 开源,自身的部分代码引用或修改自以下项目
- [dixyes/lwmbs](https://github.com/dixyes/lwmbs)(木兰宽松许可证)
- [swoole/swoole-cli](https://github.com/swoole/swoole-cli)Apache 2.0 LICENSE+SWOOLE-CLI LICENSE
- [swoole/swoole-cli](https://github.com/swoole/swoole-cli)Apache 2.0 LICENSESWOOLE-CLI LICENSE
因本项目的特殊性,使用项目编译过程中会使用很多其他开源项目,例如 curl、protobuf 等,它们都有各自的开源协议。
请在编译完成后,使用命令 `dump-license`(TODO) 导出项目使用项目的开源协议,并遵守对应项目的 LICENSE。
## 进阶
本项目重构分支为模块化编写。
TODO这部分将在基础功能完成后编写完成。

48
bin/setup-runtime Executable file
View File

@@ -0,0 +1,48 @@
#!/usr/bin/env bash
# set error-quit, verbose, non-variable-quit
set -eu
# see what system used
__OS__=$(uname -s)
# see what arch used
__ARCH__=$(uname -m)
# format arch name
case $__ARCH__ in
arm64 | aarch64) __ARCH__=arm64 ;;
x86_64|x64) __ARCH__=x64 ;;
*) ;;
esac
# format uname
case $__OS__ in
Darwin) __OS_FIXED__=macos ;;
Linux) __OS_FIXED__=linux ;;
*) echo "Current OS is not supported" && exit 1 ;;
esac
# set project dir
__DIR__=$(cd "$(dirname "$0")" && pwd)
__PROJECT__=$(cd ${__DIR__}/../ && pwd)
# set download dir
__PHP_RUNTIME_URL__="https://github.com/swoole/swoole-src/releases/download/v4.8.13/swoole-cli-v4.8.13-${__OS_FIXED__}-${__ARCH__}.tar.xz"
__COMPOSER_URL__="https://getcomposer.org/download/latest-stable/composer.phar"
# download static-php binary (currently using swoole-cli temporarily)
test -d ${__PROJECT__}/downloads || mkdir ${__PROJECT__}/downloads
# download static php binary
test -f ${__PROJECT__}/downloads/runtime.tar.xz || { echo "Downloading $__PHP_RUNTIME_URL__ ..." && curl -#fSL -o ${__PROJECT__}/downloads/runtime.tar.xz "$__PHP_RUNTIME_URL__" ; }
test -f ${__DIR__}/php || { tar -xf ${__PROJECT__}/downloads/runtime.tar.xz -C ${__DIR__}/ && mv ${__DIR__}/swoole-cli ${__DIR__}/php && rm ${__DIR__}/LICENSE ; } # (TODO: temporarily use swoole-cli as php)
chmod +x ${__DIR__}/php
# download composer
test -f ${__DIR__}/composer || curl -#fSL -o ${__DIR__}/composer "$__COMPOSER_URL__"
chmod +x ${__DIR__}/composer
# sanity check for php and composer
${__DIR__}/php -v >/dev/null || { echo "Failed to run php" && exit 1; }
${__DIR__}/php ${__DIR__}/composer --version >/dev/null || { echo "Failed to run composer" && exit 1; }
echo "Setup runtime OK!"
echo "runtime bin path needs to add manually by command below:"
echo ""
echo " export PATH=\"${__DIR__}:\$PATH\""
echo ""

View File

@@ -1,4 +1,4 @@
#!php
#!/usr/bin/env php
<?php
require_once __DIR__ . '/../vendor/autoload.php';

View File

@@ -4,7 +4,7 @@
},
"bz2": {
"type": "builtin",
"arg-type": "custom",
"arg-type": "with-prefix",
"lib-depends": [
"bzip2"
]
@@ -17,7 +17,7 @@
},
"curl": {
"type": "builtin",
"arg-type": "with",
"arg-type": "custom",
"lib-depends": [
"curl"
]
@@ -28,24 +28,18 @@
},
"dom": {
"type": "builtin",
"arg-type": "custom",
"arg-type-windows": "with",
"lib-depends": [
"libxml2",
"zlib"
]
},
"enchant": {
"type": "builtin",
"arg-type": "with",
"lib-depends": [
"enchant2"
]
},
"exif": {
"type": "builtin"
},
"ffi": {
"arg-type": "with",
"arg-type": "custom",
"type": "builtin",
"lib-depends": [
"libffi"
@@ -65,11 +59,15 @@
},
"gd": {
"type": "builtin",
"arg-type": "custom",
"arg-type-windows": "with",
"lib-depends": [
"zlib",
"libpng"
],
"ext-depends": [
"zlib"
],
"lib-suggests": [
"gd",
"libavif",
@@ -101,14 +99,14 @@
},
"gmp": {
"type": "builtin",
"arg-type": "none",
"arg-type": "with-prefix",
"lib-depends": [
"gmp"
]
},
"iconv": {
"type": "builtin",
"arg-type": "with",
"arg-type": "with-prefix",
"lib-depends-windows": [
"libiconv"
]
@@ -144,10 +142,15 @@
},
"mbstring": {
"type": "builtin",
"arg-type": "custom",
"lib-depends": [
"onig"
]
},
"mongodb": {
"type": "external",
"source": "mongodb"
},
"mysqli": {
"type": "builtin",
"arg-type": "with",
@@ -164,7 +167,7 @@
},
"openssl": {
"type": "builtin",
"arg-type": "with",
"arg-type": "custom",
"lib-depends": [
"openssl"
]
@@ -207,7 +210,7 @@
},
"phar": {
"type": "builtin",
"lib-suggests": [
"ext-depends": [
"zlib"
]
},
@@ -215,6 +218,10 @@
"type": "builtin",
"unix-only": true
},
"protobuf": {
"type": "external",
"source": "protobuf"
},
"pspell": {
"type": "builtin",
"arg-type": "with",
@@ -235,7 +242,8 @@
},
"redis": {
"type": "external",
"source": "redis"
"source": "redis",
"arg-type": "custom"
},
"session": {
"type": "builtin"
@@ -245,6 +253,7 @@
},
"simplexml": {
"type": "builtin",
"arg-type": "custom",
"arg-type-windows": "with",
"lib-depends": [
"libxml2"
@@ -259,6 +268,7 @@
},
"soap": {
"type": "builtin",
"arg-type": "custom",
"lib-depends": [
"libxml2"
]
@@ -275,7 +285,7 @@
},
"sqlite3": {
"type": "builtin",
"arg-type": "with",
"arg-type": "custom",
"lib-depends": [
"sqlite"
]
@@ -283,9 +293,9 @@
"swoole": {
"type": "external",
"source": "swoole",
"arg-type": "custom",
"lib-depends": [
"openssl",
"curl"
"openssl"
],
"ext-depends": [
"openssl"
@@ -295,17 +305,20 @@
],
"unix-only": true
},
"inotify": {
"type": "external",
"source": "inotify"
},
"swow": {
"type": "external",
"source": "swow",
"lib-depends": [
"libuv"
],
"arg-type": "custom",
"lib-suggests": [
"openssl",
"curl"
],
"ext-suggests": [
"openssl",
"curl"
]
},
@@ -333,6 +346,7 @@
},
"xml": {
"type": "builtin",
"arg-type": "custom",
"arg-type-windows": "with",
"lib-depends": [
"libxml2"
@@ -340,12 +354,14 @@
},
"xmlreader": {
"type": "builtin",
"arg-type": "custom",
"lib-depends": [
"libxml2"
]
},
"xmlwriter": {
"type": "builtin",
"arg-type": "custom",
"lib-depends": [
"libxml2"
]
@@ -360,14 +376,14 @@
"yaml": {
"type": "external",
"source": "yaml",
"arg-type": "with",
"arg-type": "with-prefix",
"lib-depends": [
"libyaml"
]
},
"zip": {
"type": "builtin",
"arg-type": "with",
"arg-type": "custom",
"arg-type-windows": "enable",
"lib-depends": [
"libzip"
@@ -384,6 +400,7 @@
"zstd": {
"type": "external",
"source": "ext-zstd",
"arg-type": "custom",
"lib-depends": [
"zstd"
]

View File

@@ -49,9 +49,7 @@
"brotli",
"nghttp2",
"zstd",
"openssl",
"idn2",
"psl"
"openssl"
],
"lib-suggests-windows": [
"zlib",
@@ -68,6 +66,22 @@
"SystemConfiguration"
]
},
"postgresql": {
"source": "postgresql",
"static-libs-unix": [
"libpg.a"
]
},
"freetype": {
"source": "freetype",
"static-libs-unix": [
"libfreetype.a"
],
"headers-unix": [
"freetype2/freetype/freetype.h",
"freetype2/ft2build.h"
]
},
"gmp": {
"source": "gmp",
"static-libs-unix": [
@@ -98,7 +112,23 @@
"ffitarget.h"
]
},
"libiconv": {
"source": "libiconv",
"static-libs-unix": [
"libiconv.a"
],
"headers": [
"iconv.h",
"libcharset.h",
"localcharset.h"
]
},
"libmcrypt": {
"source": "libmcrypt",
"static-libs-unix": [
"libmcrypt.a"
]
},
"libpng": {
"source": "libpng",
"static-libs-unix": [
@@ -158,7 +188,6 @@
"libiconv"
],
"lib-suggests": [
"icu",
"xz",
"zlib"
],
@@ -169,17 +198,6 @@
"pthreads4w"
]
},
"libiconv": {
"source": "libiconv",
"static-libs-unix": [
"libiconv.a"
],
"headers": [
"iconv.h",
"libcharset.h",
"localcharset.h"
]
},
"libyaml": {
"source": "libyaml",
"static-libs-unix": [
@@ -217,28 +235,12 @@
"openssl"
]
},
"libmcrypt": {
"source": "libmcrypt",
"static-libs-unix": [
"libmcrypt.a"
]
},
"mcrypt": {
"source": "mcrypt",
"static-libs-unix": [
"libmcrypt.a"
]
},
"freetype": {
"source": "freetype",
"static-libs-unix": [
"libfreetype.a"
],
"headers-unix": [
"freetype2/freetype/freetype.h",
"freetype2/ft2build.h"
]
},
"nghttp2": {
"source": "nghttp2",
"static-libs-unix": [
@@ -268,15 +270,6 @@
"cunit"
]
},
"libuv": {
"source": "libuv",
"static-libs-unix": [
"libuv.a"
],
"headers": [
"uv.h"
]
},
"onig": {
"source": "onig",
"static-libs-unix": [
@@ -306,7 +299,7 @@
"headers": [
"openssl"
],
"lib-suggests": [
"lib-depends": [
"zlib"
]
},
@@ -349,6 +342,9 @@
"headers-windows": [
"lzma",
"lzma.h"
],
"lib-depends": [
"libiconv"
]
},
"zlib": {

View File

@@ -37,13 +37,20 @@
},
"freetype": {
"type": "filelist",
"url": "https://download.savannah.gnu.org/releases/freetype/",
"url": "https://download-mirror.savannah.gnu.org/releases/freetype/",
"regex": "/href=\"(?<file>freetype-(?<version>[^\"]+)\\.tar\\.xz)\"/",
"license": {
"type": "file",
"path": "LICENSE.TXT"
}
},
"postgresql": {
"type": "custom",
"license": {
"type": "file",
"path": "COPYRIGHT"
}
},
"gmp": {
"type": "filelist",
"url": "https://gmplib.org/download/gmp/",
@@ -62,9 +69,10 @@
"path": "LICENSE"
}
},
"mcrypt": {
"type": "url",
"url": "https://jaist.dl.sourceforge.net/project/mcrypt/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz",
"libiconv": {
"type": "filelist",
"url": "https://ftp.gnu.org/gnu/libiconv/",
"regex": "/href=\"(?<file>libiconv-(?<version>[^\"]+)\\.tar\\.gz)\"/",
"license": {
"type": "file",
"path": "COPYING"
@@ -105,6 +113,14 @@
"path": "COPYING"
}
},
"libuv": {
"type": "ghtar",
"repo": "libuv/libuv",
"license": {
"type": "file",
"path": "LICENSE"
}
},
"libxml2": {
"type": "url",
"url": "https://gitlab.gnome.org/GNOME/libxml2/-/archive/v2.9.14/libxml2-v2.9.14.tar.gz",
@@ -131,6 +147,14 @@
"path": "LICENSE"
}
},
"mcrypt": {
"type": "url",
"url": "https://jaist.dl.sourceforge.net/project/mcrypt/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz",
"license": {
"type": "file",
"path": "COPYING"
}
},
"micro": {
"type": "git",
"path": "php-src/sapi/micro",
@@ -141,6 +165,16 @@
"path": "LICENSE"
}
},
"mongodb": {
"type": "ghrel",
"repo": "mongodb/mongo-php-driver",
"path": "php-src/ext/mongodb",
"match": "mongodb.+\\.tgz",
"license": {
"type": "file",
"path": "LICENSE"
}
},
"nghttp2": {
"type": "ghrel",
"repo": "nghttp2/nghttp2",
@@ -150,14 +184,6 @@
"path": "COPYING"
}
},
"libuv": {
"type": "ghtar",
"repo": "libuv/libuv",
"license": {
"type": "file",
"path": "LICENSE"
}
},
"onig": {
"type": "ghrel",
"repo": "kkos/oniguruma",
@@ -176,6 +202,26 @@
"path": "LICENSE.txt"
}
},
"protobuf": {
"type": "url",
"url": "http://pecl.php.net/get/protobuf",
"path": "php-src/ext/protobuf",
"filename": "protobuf.tgz",
"license": {
"type": "file",
"path": "LICENSE"
}
},
"inotify": {
"type": "url",
"url": "http://pecl.php.net/get/inotify",
"path": "php-src/ext/inotify",
"filename": "inotify.tgz",
"license": {
"type": "file",
"path": "LICENSE"
}
},
"pthreads4w": {
"type": "git",
"rev": "master",
@@ -185,6 +231,13 @@
"path": "LICENSE"
}
},
"php-src": {
"type": "custom",
"license": {
"type": "file",
"path": "LICENSE"
}
},
"redis": {
"type": "git",
"path": "php-src/ext/redis",
@@ -200,7 +253,7 @@
"url": "https://www.sqlite.org/2023/sqlite-autoconf-3410100.tar.gz",
"license": {
"type": "text",
"path": "The author disclaims copyright to this source code. In place of\na legal notice, here is a blessing:\n\n * May you do good and not evil.\n * May you find forgiveness for yourself and forgive others.\n * May you share freely, never taking more than you give."
"text": "The author disclaims copyright to this source code. In place of\na legal notice, here is a blessing:\n\n * May you do good and not evil.\n * May you find forgiveness for yourself and forgive others.\n * May you share freely, never taking more than you give."
}
},
"swoole": {
@@ -223,15 +276,6 @@
"path": "LICENSE"
}
},
"libiconv":{
"type": "filelist",
"url": "https://ftp.gnu.org/gnu/libiconv/",
"regex": "/href=\"(?<file>libiconv-(?<version>[^\"]+)\\.tar\\.gz)\"/",
"license": {
"type": "text",
"text": "TODO"
}
},
"xz": {
"type": "filelist",
"url": "https://tukaani.org/xz/",
@@ -252,9 +296,9 @@
}
},
"zlib": {
"type": "filelist",
"url": "https://zlib.net/",
"regex": "/href=\"(?<file>zlib-(?<version>[^\"]+)\\.tar\\.gz)\"/i",
"type": "ghrel",
"repo": "madler/zlib",
"match": "zlib.+\\.tar\\.gz",
"license": {
"type": "text",
"text": "(C) 1995-2022 Jean-loup Gailly and Mark Adler\n\nThis software is provided 'as-is', without any express or implied\nwarranty. In no event will the authors be held liable for any damages\narising from the use of this software.\n\nPermission is granted to anyone to use this software for any purpose,\nincluding commercial applications, and to alter it and redistribute it\nfreely, subject to the following restrictions:\n\n1. The origin of this software must not be misrepresented; you must not\n claim that you wrote the original software. If you use this software\n in a product, an acknowledgment in the product documentation would be\n appreciated but is not required.\n2. Altered source versions must be plainly marked as such, and must not be\n misrepresented as being the original software.\n3. This notice may not be removed or altered from any source distribution.\n\nJean-loup Gailly Mark Adler\njloup@gzip.org madler@alumni.caltech.edu"

View File

@@ -1,64 +1,62 @@
# Extension List
> - yes: supported and tested
> - untested: supported but not tested
> - untested: supported, but not tested
> - empty: not supported yet
> - faulty with issue link: not supported yet due to issue
> - no with issue link: not supported yet due to issue
> - partial with issue link: supported but not perfect due to issue
| | Linux | macOS | Windows |
|------------|----------|--------------------------------------------------------------------|---------|
| bcmath | yes | yes | |
| bz2 | yes | untested | |
| calendar | yes | yes | |
| ctype | yes | yes | |
| curl | untested | yes | |
| date | | yes | |
| dba | yes | yes | |
| dom | untested | untested | |
| event | | | |
| exif | | yes | |
| filter | | yes | |
| fileinfo | | | |
| ftp | | yes | |
| gd | | untested | |
| gmp | | untested | |
| hash | yes | yes | |
| iconv | untested | | |
| inotify | | | |
| json | yes | yes | |
| libxml | | yes | |
| mbstring | | yes | |
| mcrypt | | [faulty](https://github.com/crazywhalecc/static-php-cli/issues/32) | |
| mongodb | | | |
| mysqli | | | |
| mysqlnd | | yes | |
| openssl | untested | yes | |
| pcntl | untested | untested | |
| pcre | | yes | |
| pdo | | yes | |
| pdo_mysql | | yes | |
| pdo_sqlite | | yes | |
| pdo_pgsql | | | |
| phar | yes | yes | |
| posix | yes | yes | |
| protobuf | | | |
| readline | | | |
| redis | | yes | |
| Reflection | | yes | |
| session | | yes | |
| shmop | | | |
| simplexml | | untested | |
| soap | | | |
| sockets | | | |
| sqlite3 | | untested | |
| swow | | | |
| swoole | | [faulty](https://github.com/crazywhalecc/static-php-cli/issues/32) | |
| tokenizer | | yes | |
| xml | | yes | |
| xmlreader | | untested | |
| xmlwriter | | untested | |
| zip | | yes | |
| zlib | yes | yes | |
| | Linux | macOS | Windows |
|------------|--------------------------------------------------------------------|---------------------------------------------------------------------|---------|
| bcmath | yes | yes | |
| bz2 | yes | yes | |
| calendar | yes | yes | |
| ctype | yes | yes | |
| curl | yes | yes | |
| date | yes | yes | |
| dba | yes | yes | |
| dom | yes | yes | |
| enchant | | | |
| event | | | |
| exif | yes | yes | |
| filter | yes | yes | |
| fileinfo | yes | | |
| ftp | yes | yes | |
| gd | yes, untested | yes | |
| gettext | | | |
| gmp | yes, untested | yes, untested | |
| iconv | yes | | |
| inotify | yes | yes | |
| mbstring | yes | yes | |
| mcrypt | | [faulty](https://github.com/crazywhalecc/static-php-cli/issues/32) | |
| mongodb | yes, untested | | |
| mysqli | | | |
| mysqlnd | yes | yes | |
| openssl | yes | yes | |
| pcntl | yes, untested | yes | |
| pdo | yes | yes | |
| pdo_mysql | yes | yes | |
| pdo_sqlite | yes | yes | |
| pdo_pgsql | | | |
| phar | yes | yes | |
| posix | yes | yes | |
| protobuf | yes, untested | | |
| readline | | | |
| redis | yes | yes | |
| session | yes | yes | |
| shmop | yes, untested | | |
| simplexml | yes, untested | yes, untested | |
| soap | yes, untested | | |
| sockets | yes | yes | |
| sqlite3 | yes, untested | yes, untested | |
| swow | yes | [faulty](https://github.com/crazywhalecc/static-php-cli/issues/32) | |
| swoole | [faulty](https://github.com/crazywhalecc/static-php-cli/issues/32) | [partial](https://github.com/crazywhalecc/static-php-cli/issues/32) | |
| tokenizer | yes | yes | |
| xml | yes | yes | |
| xmlreader | yes, untested | yes, untested | |
| xmlwriter | yes, untested | yes, untested | |
| zip | yes, untested | yes | |
| zlib | yes | yes | |
## Additional Requirements
@@ -66,6 +64,11 @@
- swoole >= 5.0 requires PHP >= 8.0
- swow requires PHP >= 8.0
## Limitations
- swow and swoole cannot be compiled at the same time.
- openssl needs manual configuration for ssl certificate. (TODO: I will write a wiki about it)
## Bugs
See [#32](https://github.com/crazywhalecc/static-php-cli/issues/32).

View File

@@ -0,0 +1,34 @@
# 快速启动容器环境
> 提供了 debian 11 构建 和 alpine 构建环境
> 任意选一个就可以
## debian 11 构建环境
```bash
# 启动 debian 11 容器环境
sh quickstart/linux/x86_64/run-debian-11-container.sh
# 进入容器
sh quickstart/linux/x86_64/connection-static-php-cli.sh
# 准备构建基础软件
sh quickstart/linux/x86_64/debian-11-init.sh
```
## aline 构建环境
```bash
# 启动 alpine 容器环境
sh quickstart/linux/x86_64/run-alpine-3.16-container.sh
# 进入容器
sh sh quickstart/linux/x86_64/connection-static-php-cli.sh
# 准备构建基础软件
sh quickstart/linux/x86_64/alpine-3.16-init.sh
```

View File

@@ -0,0 +1,14 @@
#!/bin/bash
set -exu
__DIR__=$(
cd "$(dirname "$0")"
pwd
)
test -f /etc/apk/repositories.save || cp /etc/apk/repositories /etc/apk/repositories.save
sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories
apk update
apk add vim alpine-sdk xz autoconf automake linux-headers clang-dev clang lld libtool cmake bison re2c gettext coreutils

View File

@@ -0,0 +1,11 @@
#!/bin/bash
set -exu
__DIR__=$(
cd "$(dirname "$0")"
pwd
)
cd ${__DIR__}
docker exec -it static-php-cli-dev-1 bash

View File

@@ -0,0 +1,18 @@
#!/bin/bash
set -exu
__DIR__=$(
cd "$(dirname "$0")"
pwd
)
sed -i "s@deb.debian.org@mirrors.ustc.edu.cn@g" /etc/apt/sources.list && \
sed -i "s@security.debian.org@mirrors.ustc.edu.cn@g" /etc/apt/sources.list
apt update -y
apt install -y git curl wget ca-certificates
apt install -y xz-utils autoconf automake libclang-13-dev clang lld libtool cmake bison re2c gettext coreutils lzip zip unzip
apt install -y pkg-config bzip2 flex
# apt install build-essential linux-headers-$(uname -r)

View File

@@ -0,0 +1,25 @@
#!/bin/bash
set -exu
__DIR__=$(
cd "$(dirname "$0")"
pwd
)
__PROJECT__=$(
cd ${__DIR__}/../../../
pwd
)
cd ${__PROJECT__}
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
chmod +x bin/spc
./bin/spc fetch --all --debug
./bin/spc list-ext
#./bin/spc build "bcmath,openssl,tokenizer,sqlite3,pdo,pdo_sqlite,ftp,curl" --cc=gcc --cxx=g++ --debug
./bin/spc build "bcmath,openssl,tokenizer,sqlite3,pdo,pdo_sqlite,ftp,curl" --cc=clang --cxx=clang++ --debug

View File

@@ -0,0 +1,25 @@
#!/bin/bash
set -exu
__DIR__=$(
cd "$(dirname "$0")"
pwd
)
__PROJECT__=$(
cd ${__DIR__}/../../../
pwd
)
cd ${__DIR__}
{
docker stop static-php-cli-dev-1
} || {
echo $?
}
cd ${__DIR__}
IMAGE=alpine:3.16
cd ${__DIR__}
docker run --rm --name static-php-cli-dev-1 -d -v ${__PROJECT__}:/work -w /work $IMAGE tail -f /dev/null

View File

@@ -0,0 +1,25 @@
#!/bin/bash
set -exu
__DIR__=$(
cd "$(dirname "$0")"
pwd
)
__PROJECT__=$(
cd ${__DIR__}/../../../
pwd
)
cd ${__DIR__}
{
docker stop static-php-cli-dev-1
} || {
echo $?
}
cd ${__DIR__}
IMAGE=debian:11
cd ${__DIR__}
docker run --rm --name static-php-cli-dev-1 -d -v ${__PROJECT__}:/work -w /work $IMAGE tail -f /dev/null

View File

@@ -0,0 +1,24 @@
#!/bin/bash
set -exu
__DIR__=$(
cd "$(dirname "$0")"
pwd
)
__PROJECT__=$(
cd ${__DIR__}/../../../
pwd
)
cd ${__PROJECT__}
export PATH=${__PROJECT__}/bin/runtime:$PATH
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
chmod +x bin/spc
./bin/spc fetch --all --debug
./bin/spc list-ext
./bin/spc build "bcmath,openssl,tokenizer,sqlite3,pdo,pdo_sqlite,ftp,curl" --cc=clang --cxx=clang++ --debug

View File

@@ -16,7 +16,7 @@ use Symfony\Component\Console\Command\ListCommand;
*/
class ConsoleApplication extends Application
{
public const VERSION = '2.0-alpha1';
public const VERSION = '2.0-beta2';
/**
* @throws \ReflectionException

View File

@@ -8,6 +8,7 @@ use SPC\exception\FileSystemException;
use SPC\exception\RuntimeException;
use SPC\store\Config;
use SPC\store\FileSystem;
use SPC\util\CustomExt;
use SPC\util\DependencyUtil;
abstract class BuilderBase
@@ -30,6 +31,9 @@ abstract class BuilderBase
/** @var array<string, Extension> 要编译的扩展列表 */
protected array $exts = [];
/** @var array<int, string> 要编译的扩展列表(仅名字列表,用于最后生成编译的扩展列表给 micro */
protected array $plain_extensions = [];
/** @var bool 本次编译是否只编译 libs不编译 PHP */
protected bool $libs_only = false;
@@ -149,12 +153,10 @@ abstract class BuilderBase
*/
public function proveExts(array $extensions): void
{
if (defined('BUILD_ALL_STATIC') && BUILD_ALL_STATIC) {
$k = array_search('ffi', $extensions, true);
$k !== false && array_splice($extensions, $k, 1);
}
CustomExt::loadCustomExt();
foreach ($extensions as $extension) {
$ext = new Extension($extension, $this);
$class = CustomExt::getExtClass($extension);
$ext = new $class($extension, $this);
$this->addExt($ext);
}
@@ -162,20 +164,17 @@ abstract class BuilderBase
// 检查下依赖就行了,作用是导入依赖给 Extension 对象,今后可以对库依赖进行选择性处理
$ext->checkDependency();
}
$this->plain_extensions = $extensions;
}
/**
* 开始构建 PHP
* 构建 micro 的规则:
* - BUILD_MICRO_NONE(默认):只编译 cli
* - BUILD_MICRO_ONLY只编译 micro
* - BUILD_MICRO_BOTH同时编译 micro 和 cli
*
* @param int $build_micro_rule 规则
* @param bool $with_clean 是否为新构建?
* @param bool $bloat 保留
* @param int $build_target 规则
* @param bool $bloat 保留
*/
abstract public function buildPHP(int $build_micro_rule = BUILD_MICRO_NONE, bool $with_clean = false, bool $bloat = false);
abstract public function buildPHP(int $build_target = BUILD_TARGET_NONE, bool $bloat = false);
/**
* 生成依赖的扩展编译启用参数
@@ -188,7 +187,7 @@ abstract class BuilderBase
{
$ret = [];
foreach ($this->exts as $ext) {
$ret[] = $ext->getConfigureArg();
$ret[] = trim($ext->getConfigureArg());
}
logger()->info('Using configure: ' . implode(' ', $ret));
return implode(' ', $ret);
@@ -202,6 +201,31 @@ abstract class BuilderBase
return $this->libs_only;
}
/**
* 获取当前即将编译的 PHP 的版本 ID五位数那个
*/
public function getPHPVersionID(): int
{
$file = file_get_contents(SOURCE_PATH . '/php-src/main/php_version.h');
preg_match('/PHP_VERSION_ID (\d+)/', $file, $match);
return intval($match[1]);
}
public function getBuildTypeName(int $type): string
{
$ls = [];
if (($type & BUILD_TARGET_CLI) === BUILD_TARGET_CLI) {
$ls[] = 'cli';
}
if (($type & BUILD_TARGET_MICRO) === BUILD_TARGET_MICRO) {
$ls[] = 'micro';
}
if (($type & BUILD_TARGET_FPM) === BUILD_TARGET_FPM) {
$ls[] = 'fpm';
}
return implode(', ', $ls);
}
/**
* 检查是否存在 lib 库对应的源码,如果不存在,则抛出异常
*

View File

@@ -8,6 +8,7 @@ use SPC\builder\linux\LinuxBuilder;
use SPC\builder\macos\MacOSBuilder;
use SPC\builder\windows\WindowsBuilder;
use SPC\exception\RuntimeException;
use SPC\exception\WrongUsageException;
use Symfony\Component\Console\Input\InputInterface;
/**
@@ -36,7 +37,7 @@ class BuilderProvider
cxx: $input->getOption('cxx'),
arch: $input->getOption('arch'),
),
default => throw new RuntimeException('Current OS "' . PHP_OS_FAMILY . '" is not supported yet'),
default => throw new WrongUsageException('Current OS "' . PHP_OS_FAMILY . '" is not supported yet'),
};
}
}

View File

@@ -6,6 +6,7 @@ namespace SPC\builder;
use SPC\exception\FileSystemException;
use SPC\exception\RuntimeException;
use SPC\exception\WrongUsageException;
use SPC\store\Config;
class Extension
@@ -13,8 +14,9 @@ class Extension
protected array $dependencies = [];
/**
* @throws RuntimeException
* @throws FileSystemException
* @throws RuntimeException
* @throws WrongUsageException
*/
public function __construct(protected string $name, protected BuilderBase $builder)
{
@@ -33,13 +35,14 @@ class Extension
* 获取开启该扩展的 PHP 编译添加的参数
*
* @throws FileSystemException|RuntimeException
* @throws WrongUsageException
*/
public function getConfigureArg(): string
{
$arg = $this->getEnableArg();
switch (PHP_OS_FAMILY) {
case 'Windows':
$arg .= $this->getWindowsConfigureArg();
$arg = $this->getWindowsConfigureArg();
break;
case 'Darwin':
case 'Linux':
@@ -54,15 +57,17 @@ class Extension
*
* @throws FileSystemException
* @throws RuntimeException
* @throws WrongUsageException
*/
public function getEnableArg(): string
{
$_name = str_replace('_', '-', $this->name);
return match ($arg_type = Config::getExt($this->name, 'arg-type', 'enable')) {
'enable' => '--enable-' . $_name,
'with' => '--with-' . $_name,
'enable' => '--enable-' . $_name . ' ',
'with' => '--with-' . $_name . ' ',
'with-prefix' => '--with-' . $_name . '="' . BUILD_ROOT_PATH . '" ',
'none', 'custom' => '',
default => throw new RuntimeException("argType does not accept {$arg_type}, use [enable/with] ."),
default => throw new WrongUsageException("argType does not accept {$arg_type}, use [enable/with/with-prefix] ."),
};
}
@@ -83,6 +88,7 @@ class Extension
*
* @throws RuntimeException
* @throws FileSystemException
* @throws WrongUsageException
*/
public function checkDependency(): static
{
@@ -111,6 +117,25 @@ class Extension
return $this->name;
}
/**
* returns extension dist name
*/
public function getDistName(): string
{
return $this->name;
}
public function getWindowsConfigureArg(): string
{
return '';
// Windows is not supported yet
}
public function getUnixConfigureArg(): string
{
return '';
}
/**
* @throws RuntimeException
*/
@@ -143,145 +168,6 @@ class Extension
}
}
private function getWindowsConfigureArg(): string
{
$arg = '';
switch ($this->name) {
case 'redis':
// $arg = '--enable-redis';
// if ($this->builder->getLib('zstd')) {
// $arg .= ' --enable-redis-zstd --with-libzstd ';
// }
break;
case 'xml':
case 'soap':
case 'xmlreader':
case 'xmlwriter':
case 'dom':
$arg .= ' --with-libxml ';
break;
case 'swow':
if ($this->builder->getLib('openssl')) {
$arg .= ' --enable-swow-ssl';
}
if ($this->builder->getLib('curl')) {
$arg .= ' --enable-swow-curl';
}
break;
}
return $arg;
}
private function getUnixConfigureArg(): string
{
$arg = '';
switch ($this->name) {
/*case 'event':
$arg = ' --with-event-core --with-event-libevent-dir="' . BUILD_ROOT_PATH . '"';
if ($this->builder->getLib('openssl')) {
$arg .= ' --with-event-openssl --with-openssl-dir="' . BUILD_ROOT_PATH . '"';
}
break;*/
case 'mbstring':
$arg = ' --disable-mbregex ONIG_CFLAGS=-I"' . BUILD_ROOT_PATH . '" ' .
'ONIG_LIBS="' . $this->getLibFilesString() . '" ';
break;
case 'gmp':
$arg = ' --with-gmp="' . BUILD_ROOT_PATH . '" ';
break;
case 'sqlite3':
$arg = ' --with-sqlite3="' . BUILD_ROOT_PATH . '" ' .
'SQLITE_CFLAGS=-I"' . BUILD_INCLUDE_PATH . '" ' .
'SQLITE_LIBS="' . $this->getLibFilesString() . '" ';
break;
case 'redis':
$arg = ' --enable-redis --disable-redis-session';
if ($this->builder->getLib('zstd')) {
$arg .= ' --enable-redis-zstd --with-libzstd="' . BUILD_ROOT_PATH . '" ';
}
break;
case 'yaml':
$arg .= ' --with-yaml="' . BUILD_ROOT_PATH . '" ';
break;
case 'zstd':
$arg .= ' --with-libzstd';
break;
case 'bz2':
$arg = ' --with-bz2="' . BUILD_ROOT_PATH . '" ';
break;
case 'openssl':
$arg .= ' ' .
'OPENSSL_CFLAGS=-I"' . BUILD_INCLUDE_PATH . '" ' .
'OPENSSL_LIBS="' . $this->getLibFilesString() . '" ';
break;
case 'curl':
$arg .= ' ' .
'CURL_CFLAGS=-I"' . BUILD_INCLUDE_PATH . '" ' .
'CURL_LIBS="' . $this->getLibFilesString() . '" ';
break;
case 'gd':
if ($this->builder->getLib('freetype')) {
$arg .= ' --with-freetype ' .
'FREETYPE2_CFLAGS=-I"' . BUILD_INCLUDE_PATH . '/freetype2" ' .
'FREETYPE2_LIBS="' . $this->getLibFilesString() . '" ';
}
$arg .= ' ' .
'PNG_CFLAGS=-I"' . BUILD_INCLUDE_PATH . '" ' .
'PNG_LIBS="' . $this->getLibFilesString() . '" ';
break;
// TODO: other libraries
case 'phar':
case 'zlib':
$arg .= ' ' .
'ZLIB_CFLAGS=-I"' . BUILD_INCLUDE_PATH . '" ' .
'ZLIB_LIBS="' . $this->getLibFilesString() . '" ';
break;
case 'xml': // xml may use expat
if ($this->getLibraryDependencies()['expat'] ?? null) {
$arg .= ' --with-expat="' . BUILD_ROOT_PATH . '" ' .
'EXPAT_CFLAGS=-I"' . BUILD_INCLUDE_PATH . '" ' .
'EXPAT_LIBS="' . $this->getLibFilesString() . '" ';
break;
}
// no break
case 'soap':
case 'xmlreader':
case 'xmlwriter':
case 'dom':
$arg .= ' --with-libxml="' . BUILD_ROOT_PATH . '" ' .
'LIBXML_CFLAGS=-I"' . realpath('include/libxml2') . '" ' .
'LIBXML_LIBS="' . $this->getLibFilesString() . '" ';
break;
case 'ffi':
$arg .= ' ' .
'FFI_CFLAGS=-I"' . BUILD_INCLUDE_PATH . '" ' .
'FFI_LIBS="' . $this->getLibFilesString() . '" ';
break;
case 'zip':
$arg .= ' ' .
'LIBZIP_CFLAGS=-I"' . BUILD_INCLUDE_PATH . '" ' .
'LIBZIP_LIBS="' . $this->getLibFilesString() . '" ';
break;
case 'mbregex':
$arg .= ' ' .
'ONIG_CFLAGS=-I"' . BUILD_INCLUDE_PATH . '" ' .
'ONIG_LIBS="' . $this->getLibFilesString() . '" ';
break;
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')) {
$arg .= ' --enable-openssl';
} else {
$arg .= ' --disable-openssl --without-openssl';
}
}
return $arg;
}
private function getLibraryDependencies(bool $recursive = false): array
{
$ret = array_filter($this->dependencies, fn ($x) => $x instanceof LibraryBase);

View File

@@ -133,6 +133,7 @@ abstract class LibraryBase
{
// 传入 true表明直接编译
if ($force_build) {
logger()->info('Building required library [' . static::NAME . ']');
$this->build();
return BUILD_STATUS_OK;
}

View File

@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace SPC\builder\extension;
use SPC\builder\Extension;
use SPC\util\CustomExt;
#[CustomExt('curl')]
class curl extends Extension
{
public function getUnixConfigureArg(): string
{
return '--with-curl CURL_CFLAGS=-I"' . BUILD_INCLUDE_PATH . '" ' .
'CURL_LIBS="' . $this->getLibFilesString() . '"';
}
}

View File

@@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace SPC\builder\extension;
use SPC\builder\Extension;
use SPC\util\CustomExt;
#[CustomExt('enchant')]
class enchant extends Extension
{
public function getUnixConfigureArg(): string
{
$glibs = [
'/Users/jerry/project/git-project/static-php-cli/buildroot/lib/libgio-2.0.a',
'/Users/jerry/project/git-project/static-php-cli/buildroot/lib/libglib-2.0.a',
'/Users/jerry/project/git-project/static-php-cli/buildroot/lib/libgmodule-2.0.a',
'/Users/jerry/project/git-project/static-php-cli/buildroot/lib/libgobject-2.0.a',
'/Users/jerry/project/git-project/static-php-cli/buildroot/lib/libgthread-2.0.a',
'/Users/jerry/project/git-project/static-php-cli/buildroot/lib/libintl.a',
];
$arg = '--with-enchant="' . BUILD_ROOT_PATH . '"';
$arg .= ' ENCHANT2_CFLAGS=-I"' . BUILD_INCLUDE_PATH . '/enchant-2"';
$arg .= ' ENCHANT2_LIBS="' . $this->getLibFilesString() . '"';
$arg .= ' GLIB_CFLAGS=-I"' . BUILD_INCLUDE_PATH . '"';
$arg .= ' GLIB_LIBS="' . implode(' ', $glibs) . '"';
return $arg;
}
}

View File

@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace SPC\builder\extension;
use SPC\builder\Extension;
use SPC\util\CustomExt;
#[CustomExt('ffi')]
class ffi extends Extension
{
public function getUnixConfigureArg(): string
{
return '--with-ffi FFI_CFLAGS=-I"' . BUILD_INCLUDE_PATH . '" ' .
'FFI_LIBS="' . $this->getLibFilesString() . '"';
}
}

View File

@@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
namespace SPC\builder\extension;
use SPC\builder\Extension;
use SPC\util\CustomExt;
#[CustomExt('gd')]
class gd extends Extension
{
public function getUnixConfigureArg(): string
{
$arg = '--enable-gd';
if ($this->builder->getLib('freetype')) {
$arg .= ' --with-freetype ' .
'FREETYPE2_CFLAGS=-I"' . BUILD_INCLUDE_PATH . '/freetype2" ' .
'FREETYPE2_LIBS="' . $this->getLibFilesString() . '"';
}
$arg .= ' PNG_CFLAGS=-I"' . BUILD_INCLUDE_PATH . '" ' .
'PNG_LIBS="' . $this->getLibFilesString() . '"';
return $arg;
}
}

View File

@@ -0,0 +1,17 @@
<?php
declare(strict_types=1);
namespace SPC\builder\extension;
use SPC\builder\Extension;
use SPC\util\CustomExt;
#[CustomExt('mbregex')]
class mbregex extends Extension
{
public function getDistName(): string
{
return 'mbstring';
}
}

View File

@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace SPC\builder\extension;
use SPC\builder\Extension;
use SPC\util\CustomExt;
#[CustomExt('mbstring')]
class mbstring extends Extension
{
public function getUnixConfigureArg(): string
{
$arg = '--enable-mbstring';
if ($this->builder->getExt('mbregex') === null) {
$arg .= ' --disable-mbregex';
}
return $arg . ' ONIG_CFLAGS=-I"' . BUILD_ROOT_PATH . '" ONIG_LIBS="' . $this->getLibFilesString() . '"';
}
}

View File

@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace SPC\builder\extension;
use SPC\builder\Extension;
use SPC\util\CustomExt;
#[CustomExt('openssl')]
class openssl extends Extension
{
public function getUnixConfigureArg(): string
{
return '--with-openssl OPENSSL_CFLAGS=-I"' . BUILD_INCLUDE_PATH . '" ' .
'OPENSSL_LIBS="' . $this->getLibFilesString() . '" ';
}
}

View File

@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace SPC\builder\extension;
use SPC\builder\Extension;
use SPC\util\CustomExt;
#[CustomExt('redis')]
class redis extends Extension
{
public function getUnixConfigureArg(): string
{
$arg = '--enable-redis --disable-redis-session';
if ($this->builder->getLib('zstd')) {
$arg .= ' --enable-redis-zstd --with-libzstd="' . BUILD_ROOT_PATH . '"';
}
return $arg;
}
}

View File

@@ -0,0 +1,19 @@
<?php
declare(strict_types=1);
namespace SPC\builder\extension;
use SPC\builder\Extension;
use SPC\util\CustomExt;
#[CustomExt('sqlite3')]
class sqlite3 extends Extension
{
public function getUnixConfigureArg(): string
{
return '--with-sqlite3="' . BUILD_ROOT_PATH . '" ' .
'SQLITE_CFLAGS=-I"' . BUILD_INCLUDE_PATH . '" ' .
'SQLITE_LIBS="' . $this->getLibFilesString() . '"';
}
}

View File

@@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
namespace SPC\builder\extension;
use SPC\builder\Extension;
use SPC\util\CustomExt;
#[CustomExt('swoole')]
class swoole extends Extension
{
public function getUnixConfigureArg(): string
{
$arg = '--enable-swoole';
if ($this->builder->getLib('openssl')) {
$arg .= ' --enable-openssl';
} else {
$arg .= ' --disable-openssl --without-openssl';
}
// curl hook is buggy for static php
$arg .= ' --disable-swoole-curl';
return $arg;
}
}

View File

@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace SPC\builder\extension;
use SPC\builder\Extension;
use SPC\util\CustomExt;
#[CustomExt('swow')]
class swow extends Extension
{
public function getUnixConfigureArg(): string
{
$arg = '--enable-swow';
$arg .= $this->builder->getLib('openssl') ? ' --enable-swow-ssl' : ' --disable-swow-ssl';
$arg .= $this->builder->getLib('curl') ? ' --enable-swow-curl' : ' --disable-swow-curl';
return $arg;
}
}

View File

@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
namespace SPC\builder\extension;
use SPC\builder\Extension;
use SPC\exception\RuntimeException;
use SPC\util\CustomExt;
#[CustomExt('xml')]
#[CustomExt('soap')]
#[CustomExt('xmlreader')]
#[CustomExt('xmlwriter')]
#[CustomExt('dom')]
#[CustomExt('simplexml')]
class xml extends Extension
{
/**
* @throws RuntimeException
*/
public function getUnixConfigureArg(): string
{
$arg = match ($this->name) {
'xml' => '--enable-xml',
'soap' => '--enable-soap',
'xmlreader' => '--enable-xmlreader',
'xmlwriter' => '--enable-xmlwriter',
'dom' => '--enable-dom',
'simplexml' => '--enable-simplexml',
default => throw new RuntimeException('Not accept non-xml extension'),
};
$arg .= ' --with-libxml="' . BUILD_ROOT_PATH . '" ' .
'LIBXML_CFLAGS=-I"' . realpath('include/libxml2') . '" ' .
'LIBXML_LIBS="' . $this->getLibFilesString() . '" ';
return $arg;
}
}

View File

@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace SPC\builder\extension;
use SPC\builder\Extension;
use SPC\util\CustomExt;
#[CustomExt('zip')]
class zip extends Extension
{
public function getUnixConfigureArg(): string
{
return '--with-zip LIBZIP_CFLAGS=-I"' . BUILD_INCLUDE_PATH . '" ' .
'LIBZIP_LIBS="' . $this->getLibFilesString() . '"';
}
}

View File

@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace SPC\builder\extension;
use SPC\builder\Extension;
use SPC\util\CustomExt;
#[CustomExt('zlib')]
class zlib extends Extension
{
public function getUnixConfigureArg(): string
{
return '--with-zlib ZLIB_CFLAGS=-I"' . BUILD_INCLUDE_PATH . '" ' .
'ZLIB_LIBS="' . $this->getLibFilesString() . '"';
}
}

View File

@@ -0,0 +1,17 @@
<?php
declare(strict_types=1);
namespace SPC\builder\extension;
use SPC\builder\Extension;
use SPC\util\CustomExt;
#[CustomExt('zstd')]
class zstd extends Extension
{
public function getUnixConfigureArg(): string
{
return '--with-libzstd';
}
}

View File

@@ -9,6 +9,7 @@ use SPC\builder\linux\library\LinuxLibraryBase;
use SPC\builder\traits\UnixBuilderTrait;
use SPC\exception\FileSystemException;
use SPC\exception\RuntimeException;
use SPC\exception\WrongUsageException;
use SPC\util\Patcher;
/**
@@ -40,6 +41,7 @@ class LinuxBuilder extends BuilderBase
/**
* @throws RuntimeException
* @throws WrongUsageException
*/
public function __construct(?string $cc = null, ?string $cxx = null, ?string $arch = null)
{
@@ -86,7 +88,7 @@ class LinuxBuilder extends BuilderBase
}
}
if (!empty($missing)) {
throw new RuntimeException('missing system commands: ' . implode(', ', $missing));
throw new WrongUsageException('missing system commands: ' . implode(', ', $missing));
}
// 创立 pkg-config 和放头文件的目录
@@ -119,7 +121,7 @@ class LinuxBuilder extends BuilderBase
* @throws RuntimeException
* @throws FileSystemException
*/
public function buildPHP(int $build_micro_rule = BUILD_MICRO_NONE, bool $with_clean = false, bool $bloat = false)
public function buildPHP(int $build_target = BUILD_TARGET_NONE, bool $with_clean = false, bool $bloat = false)
{
if (!$bloat) {
$extra_libs = implode(' ', $this->getAllStaticLibFiles());
@@ -151,80 +153,67 @@ class LinuxBuilder extends BuilderBase
}
break;
default:
throw new RuntimeException('libc ' . $this->libc . ' is not implemented yet');
throw new WrongUsageException('libc ' . $this->libc . ' is not implemented yet');
}
$envs = "{$envs} CFLAGS='{$cflags}' LIBS='-ldl -lpthread'";
Patcher::patchPHPBeforeConfigure($this);
f_passthru(
$this->set_x . ' && ' .
'cd ' . SOURCE_PATH . '/php-src && ' .
'./buildconf --force'
);
shell()->cd(SOURCE_PATH . '/php-src')->exec('./buildconf --force');
Patcher::patchPHPConfigure($this);
f_passthru(
$this->set_x . ' && ' .
'cd ' . SOURCE_PATH . '/php-src && ' .
'./configure ' .
'--prefix= ' .
'--with-valgrind=no ' .
'--enable-shared=no ' .
'--enable-static=yes ' .
"--host={$this->gnu_arch}-unknown-linux " .
'--disable-all ' .
'--disable-cgi ' .
'--disable-phpdbg ' .
'--enable-cli ' .
'--enable-micro=all-static ' .
($this->zts ? '--enable-zts' : '') . ' ' .
$this->makeExtensionArgs() . ' ' .
$envs
);
shell()->cd(SOURCE_PATH . '/php-src')
->exec(
'./configure ' .
'--prefix= ' .
'--with-valgrind=no ' .
'--enable-shared=no ' .
'--enable-static=yes ' .
"--host={$this->gnu_arch}-unknown-linux " .
'--disable-all ' .
'--disable-cgi ' .
'--disable-phpdbg ' .
'--enable-cli ' .
'--enable-fpm ' .
'--enable-micro=all-static ' .
($this->zts ? '--enable-zts' : '') . ' ' .
$this->makeExtensionArgs() . ' ' .
$envs
);
$extra_libs .= $this->generateExtraLibs();
file_put_contents('/tmp/comment', $this->note_section);
if ($with_clean) {
logger()->info('cleaning up');
f_passthru(
$this->set_x . ' && ' .
'cd ' . SOURCE_PATH . '/php-src && ' .
'make clean'
);
}
// 清理
$this->cleanMake();
if ($bloat) {
logger()->info('bloat linking');
$extra_libs = "-Wl,--whole-archive {$extra_libs} -Wl,--no-whole-archive";
}
switch ($build_micro_rule) {
case BUILD_MICRO_NONE:
logger()->info('building cli');
$this->buildCli($extra_libs, $use_lld);
break;
case BUILD_MICRO_ONLY:
logger()->info('building micro');
$this->buildMicro($extra_libs, $use_lld, $cflags);
break;
case BUILD_MICRO_BOTH:
logger()->info('building cli and micro');
$this->buildCli($extra_libs, $use_lld);
$this->buildMicro($extra_libs, $use_lld, $cflags);
break;
if (($build_target & BUILD_TARGET_CLI) === BUILD_TARGET_CLI) {
logger()->info('building cli');
$this->buildCli($extra_libs, $use_lld);
}
if (($build_target & BUILD_TARGET_FPM) === BUILD_TARGET_FPM) {
logger()->info('building fpm');
$this->buildFpm($extra_libs, $use_lld);
}
if (($build_target & BUILD_TARGET_MICRO) === BUILD_TARGET_MICRO) {
logger()->info('building micro');
$this->buildMicro($extra_libs, $use_lld, $cflags);
}
if (php_uname('m') === $this->arch) {
$this->sanityCheck($build_micro_rule);
$this->sanityCheck($build_target);
}
if ($this->phar_patched) {
f_passthru('cd ' . SOURCE_PATH . '/php-src && patch -p1 -R < sapi/micro/patches/phar.patch');
shell()->cd(SOURCE_PATH . '/php-src')->exec('patch -p1 -R < sapi/micro/patches/phar.patch');
}
}
@@ -233,23 +222,22 @@ class LinuxBuilder extends BuilderBase
*/
public function buildCli(string $extra_libs, string $use_lld): void
{
f_passthru(
$this->set_x . ' && ' .
'cd ' . SOURCE_PATH . '/php-src && ' .
'sed -i "s|//lib|/lib|g" Makefile && ' .
"make -j{$this->concurrency} " .
'EXTRA_CFLAGS="-g -Os -fno-ident ' . implode(' ', array_map(fn ($x) => "-Xcompiler {$x}", $this->tune_c_flags)) . '" ' .
"EXTRA_LIBS=\"{$extra_libs}\" " .
"EXTRA_LDFLAGS_PROGRAM='{$use_lld}" .
' -all-static' .
"' " .
'cli && ' .
'cd sapi/cli && ' .
"{$this->cross_compile_prefix}objcopy --only-keep-debug php php.debug && " .
'elfedit --output-osabi linux php && ' .
"{$this->cross_compile_prefix}strip --strip-all php && " .
"{$this->cross_compile_prefix}objcopy --update-section .comment=/tmp/comment --add-gnu-debuglink=php.debug --remove-section=.note php"
);
shell()->cd(SOURCE_PATH . '/php-src')
->exec('sed -i "s|//lib|/lib|g" Makefile')
->exec(
'make -j' . $this->concurrency .
' EXTRA_CFLAGS="-g -Os -fno-ident ' . implode(' ', array_map(fn ($x) => "-Xcompiler {$x}", $this->tune_c_flags)) . '" ' .
"EXTRA_LIBS=\"{$extra_libs}\" " .
"EXTRA_LDFLAGS_PROGRAM='{$use_lld} -all-static' " .
'cli'
);
shell()->cd(SOURCE_PATH . '/php-src/sapi/cli')
->exec("{$this->cross_compile_prefix}objcopy --only-keep-debug php php.debug")
->exec('elfedit --output-osabi linux php')
->exec("{$this->cross_compile_prefix}strip --strip-all php")
->exec("{$this->cross_compile_prefix}objcopy --update-section .comment=/tmp/comment --add-gnu-debuglink=php.debug --remove-section=.note php");
$this->deployBinary(BUILD_TARGET_CLI);
}
/**
@@ -257,42 +245,55 @@ class LinuxBuilder extends BuilderBase
*/
public function buildMicro(string $extra_libs, string $use_lld, string $cflags): void
{
if ($this->getPHPVersionID() < 80000) {
throw new RuntimeException('phpmicro only support PHP >= 8.0!');
}
if ($this->getExt('phar')) {
$this->phar_patched = true;
try {
f_passthru('cd ' . SOURCE_PATH . '/php-src && patch -p1 < sapi/micro/patches/phar.patch');
shell()->cd(SOURCE_PATH . '/php-src')->exec('patch -p1 < sapi/micro/patches/phar.patch');
} catch (RuntimeException $e) {
logger()->error('failed to patch phat due to patch exit with code ' . $e->getCode());
$this->phar_patched = false;
}
}
$vars = [
'EXTRA_CFLAGS' => quote('-g -Os -fno-ident ' . implode(' ', array_map(fn ($x) => "-Xcompiler {$x}", $this->tune_c_flags))),
'EXTRA_LIBS' => quote($extra_libs),
'EXTRA_LDFLAGS_PROGRAM' => quote("{$cflags} {$use_lld}" . ' -all-static', "'"),
'POST_MICRO_BUILD_COMMANDS' => quote(
"sh -xc '" .
'cd sapi/micro && ' .
"{$this->cross_compile_prefix}objcopy --only-keep-debug micro.sfx micro.sfx.debug && " .
'elfedit --output-osabi linux micro.sfx && ' .
"{$this->cross_compile_prefix}strip --strip-all micro.sfx && " .
"{$this->cross_compile_prefix}objcopy --update-section .comment=/tmp/comment --add-gnu-debuglink=micro.sfx.debug --remove-section=.note micro.sfx'"
),
];
$var_cmdline = '';
foreach ($vars as $k => $v) {
$var_cmdline .= $k . '=' . $v . ' ';
}
shell()->cd(SOURCE_PATH . '/php-src')
->exec('sed -i "s|//lib|/lib|g" Makefile')
->exec(
"make -j{$this->concurrency} " .
'EXTRA_CFLAGS=' . quote('-g -Os -fno-ident ' . implode(' ', array_map(fn ($x) => "-Xcompiler {$x}", $this->tune_c_flags))) . ' ' .
'EXTRA_LIBS=' . quote($extra_libs) . ' ' .
'EXTRA_LDFLAGS_PROGRAM=' . quote("{$cflags} {$use_lld}" . ' -all-static', "'") . ' ' .
'micro'
);
f_passthru(
$this->set_x . ' && ' .
'cd ' . SOURCE_PATH . '/php-src && ' .
'sed -i "s|//lib|/lib|g" Makefile && ' .
"make -j{$this->concurrency} " .
$var_cmdline .
'micro'
);
shell()->cd(SOURCE_PATH . '/php-src/sapi/micro')->exec("{$this->cross_compile_prefix}strip --strip-all micro.sfx");
$this->deployBinary(BUILD_TARGET_MICRO);
}
/**
* @throws RuntimeException
*/
public function buildFpm(string $extra_libs, string $use_lld): void
{
shell()->cd(SOURCE_PATH . '/php-src')
->exec('sed -i "s|//lib|/lib|g" Makefile')
->exec(
'make -j' . $this->concurrency .
' EXTRA_CFLAGS="-g -Os -fno-ident ' . implode(' ', array_map(fn ($x) => "-Xcompiler {$x}", $this->tune_c_flags)) . '" ' .
"EXTRA_LIBS=\"{$extra_libs}\" " .
"EXTRA_LDFLAGS_PROGRAM='{$use_lld} -all-static' " .
'fpm'
);
shell()->cd(SOURCE_PATH . '/php-src/sapi/fpm')
->exec("{$this->cross_compile_prefix}objcopy --only-keep-debug php-fpm php-fpm.debug")
->exec('elfedit --output-osabi linux php-fpm')
->exec("{$this->cross_compile_prefix}strip --strip-all php-fpm")
->exec("{$this->cross_compile_prefix}objcopy --update-section .comment=/tmp/comment --add-gnu-debuglink=php-fpm.debug --remove-section=.note php-fpm");
$this->deployBinary(BUILD_TARGET_FPM);
}
/**

View File

@@ -7,6 +7,7 @@ namespace SPC\builder\linux;
use JetBrains\PhpStorm\ArrayShape;
use SPC\builder\traits\UnixSystemUtilTrait;
use SPC\exception\RuntimeException;
use SPC\exception\WrongUsageException;
class SystemUtil
{
@@ -128,10 +129,10 @@ class SystemUtil
'clang' => match ($arch) {
'x86_64' => '--target=x86_64-unknown-linux',
'arm64', 'aarch64' => '--target=arm64-unknown-linux',
default => throw new RuntimeException('unsupported arch: ' . $arch),
default => throw new WrongUsageException('unsupported arch: ' . $arch),
},
'gcc' => '',
default => throw new RuntimeException('cc compiler ' . $cc . ' is not supported'),
default => throw new WrongUsageException('cc compiler ' . $cc . ' is not supported'),
};
}
@@ -157,8 +158,7 @@ class SystemUtil
public static function checkCCFlag(string $flag, string $cc): string
{
$ret = 0;
f_exec("echo | {$cc} -E -x c - {$flag}", $dummy, $ret);
[$ret] = shell()->execWithResult("echo | {$cc} -E -x c - {$flag}");
if ($ret != 0) {
return '';
}

View File

@@ -42,6 +42,7 @@ abstract class LinuxLibraryBase extends LibraryBase
{
// 传入 true表明直接编译
if ($force_build) {
logger()->info('Building required library [' . static::NAME . ']');
$this->build();
return BUILD_STATUS_OK;
}

View File

@@ -32,28 +32,28 @@ class brotli extends LinuxLibraryBase
public function build()
{
[$lib, $include, $destdir] = SEPARATED_PATH;
f_passthru(
"{$this->builder->set_x} && " .
"cd {$this->source_dir} && " .
'rm -rf build && ' .
'mkdir -p build && ' .
'cd build && ' .
"{$this->builder->configure_env} " . ' cmake ' .
// '--debug-find ' .
'-DCMAKE_BUILD_TYPE=Release ' .
'-DBUILD_SHARED_LIBS=OFF ' .
'-DCMAKE_INSTALL_PREFIX=/ ' .
"-DCMAKE_INSTALL_LIBDIR={$lib} " .
"-DCMAKE_INSTALL_INCLUDEDIR={$include} " .
"-DCMAKE_TOOLCHAIN_FILE={$this->builder->cmake_toolchain_file} " .
'.. && ' .
"cmake --build . -j {$this->builder->concurrency} --target brotlicommon-static && " .
"cmake --build . -j {$this->builder->concurrency} --target brotlidec-static && " .
"cmake --build . -j {$this->builder->concurrency} --target brotlienc-static && " .
'cp libbrotlidec-static.a ' . BUILD_LIB_PATH . ' && ' .
'cp libbrotlienc-static.a ' . BUILD_LIB_PATH . ' && ' .
'cp libbrotlicommon-static.a ' . BUILD_LIB_PATH . ' && ' .
'cp -r ../c/include/brotli ' . BUILD_INCLUDE_PATH
);
// 清理旧的编译文件
shell()->cd($this->source_dir)
->exec('rm -rf build')
->exec('mkdir -p build');
// 使用 cmake 编译
shell()->cd($this->source_dir . '/build')
->exec(
$this->builder->configure_env . ' cmake ' .
'-DCMAKE_BUILD_TYPE=Release ' .
'-DBUILD_SHARED_LIBS=OFF ' .
'-DCMAKE_INSTALL_PREFIX=/ ' .
"-DCMAKE_INSTALL_LIBDIR={$lib} " .
"-DCMAKE_INSTALL_INCLUDEDIR={$include} " .
"-DCMAKE_TOOLCHAIN_FILE={$this->builder->cmake_toolchain_file} " .
'..'
)
->exec("cmake --build . -j {$this->builder->concurrency} --target brotlicommon-static")
->exec("cmake --build . -j {$this->builder->concurrency} --target brotlidec-static")
->exec("cmake --build . -j {$this->builder->concurrency} --target brotlienc-static")
->exec('cp libbrotlidec-static.a ' . BUILD_LIB_PATH)
->exec('cp libbrotlienc-static.a ' . BUILD_LIB_PATH)
->exec('cp libbrotlicommon-static.a ' . BUILD_LIB_PATH)
->exec('cp -r ../c/include/brotli ' . BUILD_INCLUDE_PATH);
}
}

View File

@@ -33,15 +33,11 @@ class bzip2 extends LinuxLibraryBase
*/
public function build()
{
f_passthru(
$this->builder->set_x . ' && ' .
"cd {$this->source_dir} && " .
"make {$this->builder->configure_env} PREFIX='" . BUILD_ROOT_PATH . "' clean" . ' && ' .
"make -j{$this->builder->concurrency} {$this->builder->configure_env} PREFIX='" . BUILD_ROOT_PATH . "' libbz2.a" . ' && ' .
// make install may fail when cross-compiling, so we copy files.
'cp libbz2.a ' . BUILD_LIB_PATH . ' && ' .
'cp bzlib.h ' . BUILD_INCLUDE_PATH
);
// $this->makeFakePkgconfs();
shell()
->cd($this->source_dir)
->exec("make {$this->builder->configure_env} PREFIX='" . BUILD_ROOT_PATH . "' clean")
->exec("make -j{$this->builder->concurrency} {$this->builder->configure_env} PREFIX='" . BUILD_ROOT_PATH . "' libbz2.a")
->exec('cp libbz2.a ' . BUILD_LIB_PATH)
->exec('cp bzlib.h ' . BUILD_INCLUDE_PATH);
}
}

View File

@@ -76,16 +76,13 @@ EOF
$extra = '';
// lib:openssl
$openssl = $this->builder->getLib('openssl');
if ($openssl instanceof LinuxLibraryBase) {
$extra .= '-DCURL_USE_OPENSSL=ON -DCURL_ENABLE_SSL=ON ';
} else {
$extra .= '-DCURL_USE_OPENSSL=OFF -DCURL_ENABLE_SSL=OFF ';
}
$use_openssl = $openssl instanceof LinuxLibraryBase ? 'ON' : 'OFF';
$extra .= "-DCURL_USE_OPENSSL={$use_openssl} -DCURL_ENABLE_SSL={$use_openssl} ";
// lib:zlib
$zlib = $this->builder->getLib('zlib');
if ($zlib instanceof LinuxLibraryBase) {
$extra .= '-DZLIB_LIBRARIES="' . $zlib->getStaticLibFiles(style: 'cmake') . '" ' .
'-DZLIB_INCLUDE_DIRS="' . BUILD_INCLUDE_PATH . '" ';
$extra .= '-DZLIB_LIBRARY="' . $zlib->getStaticLibFiles(style: 'cmake') . '" ' .
'-DZLIB_INCLUDE_DIR="' . BUILD_INCLUDE_PATH . '" ';
}
// lib:libssh2
$libssh2 = $this->builder->getLib('libssh2');
@@ -140,24 +137,24 @@ EOF
[$lib, $include, $destdir] = SEPARATED_PATH;
// compile
f_passthru(
$this->builder->set_x . ' && ' .
"cd {$this->source_dir} && " .
'rm -rf build && ' .
'mkdir -p build && ' .
'cd build && ' .
"{$this->builder->configure_env} " . ' cmake ' .
// '--debug-find ' .
'-DCMAKE_BUILD_TYPE=Release ' .
'-DBUILD_SHARED_LIBS=OFF ' .
$extra .
'-DCMAKE_INSTALL_PREFIX=/ ' .
"-DCMAKE_INSTALL_LIBDIR={$lib} " .
"-DCMAKE_INSTALL_INCLUDEDIR={$include} " .
"-DCMAKE_TOOLCHAIN_FILE={$this->builder->cmake_toolchain_file} " .
'.. && ' .
"make -j{$this->builder->concurrency} && " .
'make install DESTDIR="' . $destdir . '"'
);
shell()
->cd($this->source_dir)
->exec('rm -rf build')
->exec('mkdir -p build')
->cd($this->source_dir . '/build')
->exec("{$this->builder->configure_env} cmake " .
'-DCMAKE_BUILD_TYPE=Release ' .
'-DBUILD_SHARED_LIBS=OFF ' .
'-DBUILD_CURL_EXE=OFF ' .
$extra .
"-DCMAKE_INSTALL_PREFIX={$destdir} " .
"-DCMAKE_INSTALL_LIBDIR={$lib} " .
"-DCMAKE_INSTALL_INCLUDEDIR={$include} " .
"-DCMAKE_TOOLCHAIN_FILE={$this->builder->cmake_toolchain_file} " .
'..')
->exec("make -j{$this->builder->concurrency}")
->exec("make install DESTDIR='{$destdir}'");
shell()->cd(BUILD_LIB_PATH . '/cmake/CURL/')
->exec("sed -ie 's|\"/lib/libcurl.a\"|\"" . BUILD_LIB_PATH . "/libcurl.a\"|g' CURLTargets-release.cmake");
}
}

View File

@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace SPC\builder\linux\library;
/**
* gmp is a template library class for unix
*/
class gmp extends LinuxLibraryBase
{
public const NAME = 'gmp';
protected function build()
{
[,,$destdir] = SEPARATED_PATH;
shell()->cd($this->source_dir)
->exec(
"{$this->builder->configure_env} ./configure " .
'--enable-static --disable-shared ' .
'--prefix='
)
->exec('make clean')
->exec("make -j{$this->builder->concurrency}")
->exec("make install DESTDIR={$destdir}");
}
}

View File

@@ -35,16 +35,15 @@ class libiconv extends LinuxLibraryBase
{
[,,$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
);
shell()->cd($this->source_dir)
->exec(
"{$this->builder->configure_env} ./configure " .
'--enable-static ' .
'--disable-shared ' .
'--prefix='
)
->exec('make clean')
->exec("make -j{$this->builder->concurrency}")
->exec('make install DESTDIR=' . $destdir);
}
}

View File

@@ -0,0 +1,65 @@
<?php
/**
* Copyright (c) 2022 Yun Dou <dixyes@gmail.com>
*
* lwmbs is licensed under Mulan PSL v2. You can use this
* software according to the terms and conditions of the
* Mulan PSL v2. You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS,
* WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
*
* See the Mulan PSL v2 for more details.
*/
declare(strict_types=1);
namespace SPC\builder\linux\library;
use SPC\exception\FileSystemException;
use SPC\exception\RuntimeException;
use SPC\util\Patcher;
class libpng extends LinuxLibraryBase
{
public const NAME = 'libpng';
/**
* @throws RuntimeException
* @throws FileSystemException
*/
public function build()
{
// 不同架构的专属优化
$optimizations = match ($this->builder->arch) {
'x86_64' => '--enable-intel-sse ',
'arm64' => '--enable-arm-neon ',
default => '',
};
// patch configure
Patcher::patchUnixLibpng();
shell()->cd($this->source_dir)
->exec('chmod +x ./configure')
->exec(
"{$this->builder->configure_env} ./configure " .
"--host={$this->builder->gnu_arch}-unknown-linux " .
'--disable-shared ' .
'--enable-static ' .
'--enable-hardware-optimizations ' .
'--with-zlib-prefix=' . BUILD_ROOT_PATH . ' ' .
$optimizations .
'--prefix='
)
->exec('make clean')
->exec("make -j{$this->builder->concurrency} DEFAULT_INCLUDES='-I. -I" . BUILD_INCLUDE_PATH . "' LIBS= libpng16.la")
->exec('make install-libLTLIBRARIES install-data-am DESTDIR=' . BUILD_ROOT_PATH)
->cd(BUILD_LIB_PATH)
->exec('ln -sf libpng16.a libpng.a');
}
}

View File

@@ -0,0 +1,60 @@
<?php
/**
* Copyright (c) 2022 Yun Dou <dixyes@gmail.com>
*
* lwmbs is licensed under Mulan PSL v2. You can use this
* software according to the terms and conditions of the
* Mulan PSL v2. You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS,
* WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
*
* See the Mulan PSL v2 for more details.
*/
declare(strict_types=1);
namespace SPC\builder\linux\library;
use SPC\exception\RuntimeException;
class libssh2 extends LinuxLibraryBase
{
public const NAME = 'libssh2';
/**
* @throws RuntimeException
*/
public function build()
{
// lib:zlib
$enable_zlib = $this->builder->getLib('zlib') !== null ? 'ON' : 'OFF';
[$lib, $include, $destdir] = SEPARATED_PATH;
shell()->cd($this->source_dir)
->exec('rm -rf build')
->exec('mkdir -p build')
->cd($this->source_dir . '/build')
->exec(
"{$this->builder->configure_env} " . ' cmake ' .
// '--debug-find ' .
'-DCMAKE_BUILD_TYPE=Release ' .
'-DBUILD_SHARED_LIBS=OFF ' .
'-DBUILD_EXAMPLES=OFF ' .
'-DBUILD_TESTING=OFF ' .
"-DENABLE_ZLIB_COMPRESSION={$enable_zlib} " .
'-DCMAKE_INSTALL_PREFIX=/ ' .
"-DCMAKE_INSTALL_LIBDIR={$lib} " .
"-DCMAKE_INSTALL_INCLUDEDIR={$include} " .
"-DCMAKE_TOOLCHAIN_FILE={$this->builder->cmake_toolchain_file} " .
'..'
)
->exec("cmake --build . -j {$this->builder->concurrency} --target libssh2")
->exec('make install DESTDIR="' . $destdir . '"');
}
}

View File

@@ -27,34 +27,6 @@ class libxml2 extends LinuxLibraryBase
{
public const NAME = 'libxml2';
protected array $static_libs = ['libxml2.a'];
protected array $headers = [
'libxml2',
];
protected array $pkgconfs = [
'libxml-2.0.pc' => <<<'EOF'
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include
modules=1
Name: libXML
Version: 2.9.14
Description: libXML library version2.
Requires:
Libs: -L${libdir} -lxml2
Cflags: -I${includedir}/libxml2
EOF
];
protected array $dep_names = [
'icu' => true,
'xz' => true,
'zlib' => true,
];
/**
* @throws RuntimeException
*/
@@ -66,39 +38,39 @@ EOF
[$lib, $include, $destdir] = SEPARATED_PATH;
f_passthru(
$this->builder->set_x . ' && ' .
"cd {$this->source_dir} && " .
'rm -rf build && ' .
'mkdir -p build && ' .
'cd build && ' .
"{$this->builder->configure_env} " . ' cmake ' .
// '--debug-find ' .
'-DCMAKE_BUILD_TYPE=Release ' .
'-DBUILD_SHARED_LIBS=OFF ' .
'-DLIBXML2_WITH_ICONV=ON ' .
'-DIconv_IS_BUILT_IN=OFF ' .
"-DLIBXML2_WITH_ZLIB={$enable_zlib} " .
"-DLIBXML2_WITH_ICU={$enable_icu} " .
"-DLIBXML2_WITH_LZMA={$enable_xz} " .
'-DLIBXML2_WITH_PYTHON=OFF ' .
'-DLIBXML2_WITH_PROGRAMS=OFF ' .
'-DLIBXML2_WITH_TESTS=OFF ' .
'-DCMAKE_INSTALL_PREFIX=/ ' .
"-DCMAKE_INSTALL_LIBDIR={$lib} " .
"-DCMAKE_INSTALL_INCLUDEDIR={$include} " .
"-DCMAKE_TOOLCHAIN_FILE={$this->builder->cmake_toolchain_file} " .
'.. && ' .
"cmake --build . -j {$this->builder->concurrency} && " .
'make install DESTDIR="' . $destdir . '"'
);
shell()->cd($this->source_dir)
->exec('rm -rf build')
->exec('mkdir -p build')
->cd($this->source_dir . '/build')
->exec(
"{$this->builder->configure_env} " . ' cmake ' .
// '--debug-find ' .
'-DCMAKE_BUILD_TYPE=Release ' .
'-DBUILD_SHARED_LIBS=OFF ' .
'-DLIBXML2_WITH_ICONV=ON ' .
'-DIconv_IS_BUILT_IN=OFF ' .
"-DLIBXML2_WITH_ZLIB={$enable_zlib} " .
"-DLIBXML2_WITH_ICU={$enable_icu} " .
"-DLIBXML2_WITH_LZMA={$enable_xz} " .
'-DLIBXML2_WITH_PYTHON=OFF ' .
'-DLIBXML2_WITH_PROGRAMS=OFF ' .
'-DLIBXML2_WITH_TESTS=OFF ' .
'-DCMAKE_INSTALL_PREFIX=/ ' .
"-DCMAKE_INSTALL_LIBDIR={$lib} " .
"-DCMAKE_INSTALL_INCLUDEDIR={$include} " .
"-DCMAKE_TOOLCHAIN_FILE={$this->builder->cmake_toolchain_file} " .
'..'
)
->exec("cmake --build . -j {$this->builder->concurrency}")
->exec('make install DESTDIR="' . $destdir . '"');
if (is_dir(BUILD_INCLUDE_PATH . '/libxml2/libxml')) {
if (is_dir(BUILD_INCLUDE_PATH . '/libxml')) {
f_passthru('rm -rf "' . BUILD_INCLUDE_PATH . '/libxml"');
shell()->exec('rm -rf "' . BUILD_INCLUDE_PATH . '/libxml"');
}
$path = FileSystem::convertPath(BUILD_INCLUDE_PATH . '/libxml2/libxml');
$dst_path = FileSystem::convertPath(BUILD_INCLUDE_PATH . '/');
f_passthru('mv "' . $path . '" "' . $dst_path . '"');
shell()->exec('mv "' . $path . '" "' . $dst_path . '"');
}
}
}

View File

@@ -0,0 +1,93 @@
<?php
/**
* Copyright (c) 2022 Yun Dou <dixyes@gmail.com>
*
* lwmbs is licensed under Mulan PSL v2. You can use this
* software according to the terms and conditions of the
* Mulan PSL v2. You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS,
* WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
*
* See the Mulan PSL v2 for more details.
*/
declare(strict_types=1);
namespace SPC\builder\linux\library;
use SPC\exception\RuntimeException;
class libyaml extends LinuxLibraryBase
{
public const NAME = 'libyaml';
/**
* @throws RuntimeException
*/
public function build()
{
// prepare cmake/config.h.in
if (!is_file(SOURCE_PATH . '/libyaml/cmake/config.h.in')) {
f_mkdir(SOURCE_PATH . '/libyaml/cmake');
file_put_contents(
SOURCE_PATH . '/libyaml/cmake/config.h.in',
<<<'EOF'
#define YAML_VERSION_MAJOR @YAML_VERSION_MAJOR@
#define YAML_VERSION_MINOR @YAML_VERSION_MINOR@
#define YAML_VERSION_PATCH @YAML_VERSION_PATCH@
#define YAML_VERSION_STRING "@YAML_VERSION_STRING@"
EOF
);
}
// prepare yamlConfig.cmake.in
if (!is_file(SOURCE_PATH . '/libyaml/yamlConfig.cmake.in')) {
file_put_contents(
SOURCE_PATH . '/libyaml/yamlConfig.cmake.in',
<<<'EOF'
# Config file for the yaml library.
#
# It defines the following variables:
# yaml_LIBRARIES - libraries to link against
@PACKAGE_INIT@
set_and_check(yaml_TARGETS "@PACKAGE_CONFIG_DIR_CONFIG@/yamlTargets.cmake")
if(NOT yaml_TARGETS_IMPORTED)
set(yaml_TARGETS_IMPORTED 1)
include(${yaml_TARGETS})
endif()
set(yaml_LIBRARIES yaml)
EOF
);
}
[$lib, $include, $destdir] = SEPARATED_PATH;
shell()
->cd($this->source_dir)
->exec('rm -rf build')
->exec('mkdir -p build')
->cd($this->source_dir . '/build')
->exec("{$this->builder->configure_env} cmake " .
// '--debug-find ' .
'-DCMAKE_BUILD_TYPE=Release ' .
'-DBUILD_TESTING=OFF ' .
'-DBUILD_SHARED_LIBS=OFF ' .
'-DCMAKE_INSTALL_PREFIX=/ ' .
"-DCMAKE_INSTALL_LIBDIR={$lib} " .
"-DCMAKE_INSTALL_INCLUDEDIR={$include} " .
"-DCMAKE_TOOLCHAIN_FILE={$this->builder->cmake_toolchain_file} " .
'..')
->exec("make -j{$this->builder->concurrency}")
->exec("make install DESTDIR={$destdir}");
}
}

View File

@@ -0,0 +1,106 @@
<?php
/**
* Copyright (c) 2022 Yun Dou <dixyes@gmail.com>
*
* lwmbs is licensed under Mulan PSL v2. You can use this
* software according to the terms and conditions of the
* Mulan PSL v2. You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS,
* WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
*
* See the Mulan PSL v2 for more details.
*/
declare(strict_types=1);
namespace SPC\builder\linux\library;
use SPC\exception\FileSystemException;
use SPC\exception\RuntimeException;
class libzip extends LinuxLibraryBase
{
public const NAME = 'libzip';
/**
* @throws FileSystemException|RuntimeException
*/
public function build()
{
$extra = '';
// lib:zlib
$zlib = $this->builder->getLib('zlib');
if ($zlib instanceof LinuxLibraryBase) {
$extra .= '-DZLIB_LIBRARY="' . $zlib->getStaticLibFiles(style: 'cmake') . '" ' .
'-DZLIB_INCLUDE_DIR=' . BUILD_INCLUDE_PATH . ' ';
}
// lib:bzip2
$libbzip2 = $this->builder->getLib('bzip2');
if ($libbzip2 instanceof LinuxLibraryBase) {
$extra .= '-DENABLE_BZIP2=ON ' .
'-DBZIP2_LIBRARIES="' . $libbzip2->getStaticLibFiles(style: 'cmake') . '" ' .
'-DBZIP2_INCLUDE_DIR=' . BUILD_INCLUDE_PATH . ' ';
} else {
$extra .= '-DENABLE_BZIP2=OFF ';
}
// lib:xz
$xz = $this->builder->getLib('xz');
if ($xz instanceof LinuxLibraryBase) {
$extra .= '-DENABLE_LZMA=ON ' .
'-DLIBLZMA_LIBRARY="' . $xz->getStaticLibFiles(style: 'cmake') . '" ' .
'-DLIBLZMA_INCLUDE_DIR=' . BUILD_INCLUDE_PATH . ' ';
} else {
$extra .= '-DENABLE_LZMA=OFF ';
}
// lib:zstd
$libzstd = $this->builder->getLib('zstd');
if ($libzstd instanceof LinuxLibraryBase) {
$extra .= '-DENABLE_ZSTD=ON ' .
'-DZstd_LIBRARY="' . $libzstd->getStaticLibFiles(style: 'cmake') . '" ' .
'-DZstd_INCLUDE_DIR="' . BUILD_INCLUDE_PATH . '" ';
} else {
$extra .= '-DENABLE_ZSTD=OFF ';
}
// lib:openssl
$libopenssl = $this->builder->getLib('openssl');
if ($libopenssl instanceof LinuxLibraryBase) {
$extra .= '-DENABLE_OPENSSL=ON ' .
'-DOpenSSL_LIBRARY="' . $libopenssl->getStaticLibFiles(style: 'cmake') . '" ' .
'-DOpenSSL_INCLUDE_DIR="' . BUILD_INCLUDE_PATH . '" ';
} else {
$extra .= '-DENABLE_OPENSSL=OFF ';
}
[$lib, $include, $destdir] = SEPARATED_PATH;
shell()
->cd($this->source_dir)
->exec('rm -rf build')
->exec('mkdir -p build')
->cd($this->source_dir . '/build')
->exec(
$this->builder->configure_env . ' cmake ' .
'-DCMAKE_BUILD_TYPE=Release ' .
'-DENABLE_GNUTLS=OFF ' .
'-DENABLE_MBEDTLS=OFF ' .
'-DBUILD_SHARED_LIBS=OFF ' .
'-DBUILD_DOC=OFF ' .
'-DBUILD_EXAMPLES=OFF ' .
'-DBUILD_REGRESS=OFF ' .
'-DBUILD_TOOLS=OFF ' .
$extra .
'-DCMAKE_INSTALL_PREFIX=/ ' .
"-DCMAKE_INSTALL_LIBDIR={$lib} " .
"-DCMAKE_INSTALL_INCLUDEDIR={$include} " .
"-DCMAKE_TOOLCHAIN_FILE={$this->builder->cmake_toolchain_file} " .
'..'
)
->exec("make -j{$this->builder->concurrency}")
->exec('make install DESTDIR=' . $destdir);
}
}

View File

@@ -0,0 +1,102 @@
<?php
/**
* Copyright (c) 2022 Yun Dou <dixyes@gmail.com>
*
* lwmbs is licensed under Mulan PSL v2. You can use this
* software according to the terms and conditions of the
* Mulan PSL v2. You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS,
* WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
*
* See the Mulan PSL v2 for more details.
*/
declare(strict_types=1);
namespace SPC\builder\linux\library;
use SPC\exception\RuntimeException;
class nghttp2 extends LinuxLibraryBase
{
public const NAME = 'nghttp2';
protected array $static_libs = ['libnghttp2.a'];
protected array $headers = ['nghttp2'];
protected array $pkgconfs = [
'libnghttp2.pc' => <<<'EOF'
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include
Name: libnghttp2
Description: HTTP/2 C library
URL: https://github.com/tatsuhiro-t/nghttp2
Version: 1.47.0
Libs: -L${libdir} -lnghttp2
Cflags: -I${includedir}
EOF
];
protected array $dep_names = [
'zlib' => false,
'openssl' => false,
'libxml2' => true,
'libev' => true,
'libcares' => true,
'libngtcp2' => true,
'libnghttp3' => true,
'libbpf' => true,
'libevent-openssl' => true,
'jansson' => true,
'jemalloc' => true,
'systemd' => true,
'cunit' => true,
];
/**
* @throws RuntimeException
*/
public function build()
{
$args = $this->builder->makeAutoconfArgs(static::NAME, [
'zlib' => null,
'openssl' => null,
'libxml2' => null,
'libev' => null,
'libcares' => null,
'libngtcp2' => null,
'libnghttp3' => null,
'libbpf' => null,
'libevent-openssl' => null,
'jansson' => null,
'jemalloc' => null,
'systemd' => null,
'cunit' => null,
]);
[,,$destdir] = SEPARATED_PATH;
shell()->cd($this->source_dir)
->exec(
"{$this->builder->configure_env} ./configure " .
'--enable-static ' .
'--disable-shared ' .
"--host={$this->builder->gnu_arch}-unknown-linux " .
'--enable-lib-only ' .
'--with-boost=no ' .
$args . ' ' .
'--prefix='
)
->exec('make clean')
->exec("make -j{$this->builder->concurrency}")
->exec("make install DESTDIR={$destdir}");
}
}

View File

@@ -0,0 +1,48 @@
<?php
/**
* Copyright (c) 2022 Yun Dou <dixyes@gmail.com>
*
* lwmbs is licensed under Mulan PSL v2. You can use this
* software according to the terms and conditions of the
* Mulan PSL v2. You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS,
* WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
*
* See the Mulan PSL v2 for more details.
*/
declare(strict_types=1);
namespace SPC\builder\linux\library;
use SPC\exception\RuntimeException;
class onig extends LinuxLibraryBase
{
public const NAME = 'onig';
/**
* @throws RuntimeException
*/
public function build()
{
[,,$destdir] = SEPARATED_PATH;
shell()->cd($this->source_dir)
->exec(
"{$this->builder->configure_env} ./configure " .
'--enable-static ' .
'--disable-shared ' .
"--host={$this->builder->arch}-unknown-linux " .
'--prefix='
)
->exec('make clean')
->exec("make -j{$this->builder->concurrency}")
->exec("make install DESTDIR={$destdir}");
}
}

View File

@@ -28,53 +28,6 @@ class openssl extends LinuxLibraryBase
{
public const NAME = 'openssl';
protected array $static_libs = [
'libssl.a',
'libcrypto.a',
];
protected array $headers = ['openssl'];
protected array $pkgconfs = [
'openssl.pc' => <<<'EOF'
exec_prefix=${prefix}
libdir=${prefix}/lib
includedir=${prefix}/include
Name: OpenSSL
Description: Secure Sockets Layer and cryptography libraries and tools
Version: 3.0.3
Requires: libssl libcrypto
EOF,
'libssl.pc' => <<<'EOF'
exec_prefix=${prefix}
libdir=${prefix}/lib
includedir=${prefix}/include
Name: OpenSSL-libssl
Description: Secure Sockets Layer and cryptography libraries
Version: 3.0.3
Requires.private: libcrypto
Libs: -L${libdir} -lssl
Cflags: -I${includedir}
EOF,
'libcrypto.pc' => <<<'EOF'
exec_prefix=${prefix}
libdir=${prefix}/lib
includedir=${prefix}/include
enginesdir=${libdir}/engines-3
Name: OpenSSL-libcrypto
Description: OpenSSL cryptography library
Version: 3.0.3
Libs: -L${libdir} -lcrypto
Libs.private: -lz -ldl -pthread
Cflags: -I${includedir}
EOF,
];
protected array $dep_names = ['zlib' => true];
/**
* @throws RuntimeException
* @throws FileSystemException
@@ -87,7 +40,7 @@ EOF,
$ex_lib = '-ldl -pthread';
$env = $this->builder->pkgconf_env . " CFLAGS='{$this->builder->arch_c_flags}'";
$env .= " CC='{$this->builder->cc} --static -static-libgcc -idirafter " . BUILD_INCLUDE_PATH .
$env .= " CC='{$this->builder->cc} -static -idirafter " . BUILD_INCLUDE_PATH .
' -idirafter /usr/include/ ' .
' -idirafter /usr/include/' . $this->builder->arch . '-linux-gnu/ ' .
"' ";
@@ -107,21 +60,18 @@ EOF,
$clang_postfix = SystemUtil::getCCType($this->builder->cc) === 'clang' ? '-clang' : '';
f_passthru(
$this->builder->set_x . ' && ' .
"cd {$this->source_dir} && " .
"{$this->builder->configure_env} {$env} ./Configure no-shared {$extra} " .
'--prefix=/ ' . // use prefix=/
"--libdir={$lib} " .
'--static -static ' .
"{$zlib_extra}" .
'no-legacy ' .
"linux-{$this->builder->arch}{$clang_postfix} && " .
'make clean && ' .
"make -j{$this->builder->concurrency} CNF_EX_LIBS=\"{$ex_lib}\" && " .
'make install_sw DESTDIR=' . $destdir
// remove liblegacy
// 'ar t lib/libcrypto.a | grep -e \'^liblegacy-\' | xargs ar d lib/libcrypto.a'
);
shell()->cd($this->source_dir)
->exec(
"{$this->builder->configure_env} {$env} ./Configure no-shared {$extra} " .
'--prefix=/ ' .
'--libdir=lib ' .
'-static ' .
"{$zlib_extra}" .
'no-legacy ' .
"linux-{$this->builder->arch}{$clang_postfix}"
)
->exec('make clean')
->exec("make -j{$this->builder->concurrency} CNF_EX_LIBS=\"{$ex_lib}\"")
->exec("make install_sw DESTDIR={$destdir}");
}
}

View File

@@ -0,0 +1,47 @@
<?php
/**
* Copyright (c) 2022 Yun Dou <dixyes@gmail.com>
*
* lwmbs is licensed under Mulan PSL v2. You can use this
* software according to the terms and conditions of the
* Mulan PSL v2. You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS,
* WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
*
* See the Mulan PSL v2 for more details.
*/
declare(strict_types=1);
namespace SPC\builder\linux\library;
use SPC\exception\RuntimeException;
class sqlite extends LinuxLibraryBase
{
public const NAME = 'sqlite';
/**
* @throws RuntimeException
*/
public function build()
{
[,,$destdir] = SEPARATED_PATH;
shell()->cd($this->source_dir)
->exec(
"{$this->builder->configure_env} ./configure " .
'--enable-static ' .
'--disable-shared ' .
'--prefix='
)
->exec('make clean')
->exec("make -j{$this->builder->concurrency}")
->exec("make install DESTDIR={$destdir}");
}
}

View File

@@ -0,0 +1,56 @@
<?php
/**
* Copyright (c) 2022 Yun Dou <dixyes@gmail.com>
*
* lwmbs is licensed under Mulan PSL v2. You can use this
* software according to the terms and conditions of the
* Mulan PSL v2. You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS,
* WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
*
* See the Mulan PSL v2 for more details.
*/
declare(strict_types=1);
namespace SPC\builder\linux\library;
use SPC\exception\RuntimeException;
class xz extends LinuxLibraryBase
{
public const NAME = 'xz';
/**
* @throws RuntimeException
*/
public function build()
{
[,,$destdir] = SEPARATED_PATH;
shell()->cd($this->source_dir)
// ->exec('autoreconf -i --force')
->exec(
"{$this->builder->configure_env} ./configure " .
'--enable-static ' .
'--disable-shared ' .
"--host={$this->builder->gnu_arch}-unknown-linux " .
'--disable-xz ' .
'--disable-xzdec ' .
'--disable-lzmadec ' .
'--disable-lzmainfo ' .
'--disable-scripts ' .
'--disable-doc ' .
'--with-libiconv ' .
'--prefix='
)
->exec('make clean')
->exec("make -j{$this->builder->concurrency}")
->exec("make install DESTDIR={$destdir}");
}
}

View File

@@ -33,15 +33,14 @@ class zlib extends LinuxLibraryBase
{
[,,$destdir] = SEPARATED_PATH;
f_passthru(
$this->builder->set_x . ' && ' .
"cd {$this->source_dir} && " .
"{$this->builder->configure_env} ./configure " .
'--static ' .
'--prefix= && ' . // use prefix=/
'make clean && ' .
"make -j{$this->builder->concurrency} && " .
'make install DESTDIR=' . $destdir
);
shell()->cd($this->source_dir)
->exec(
"{$this->builder->configure_env} ./configure " .
'--static ' .
'--prefix='
)
->exec('make clean')
->exec("make -j{$this->builder->concurrency}")
->exec("make install DESTDIR={$destdir}");
}
}

View File

@@ -0,0 +1,45 @@
<?php
/**
* Copyright (c) 2022 Yun Dou <dixyes@gmail.com>
*
* lwmbs is licensed under Mulan PSL v2. You can use this
* software according to the terms and conditions of the
* Mulan PSL v2. You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS,
* WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
*
* See the Mulan PSL v2 for more details.
*/
declare(strict_types=1);
namespace SPC\builder\linux\library;
use SPC\exception\RuntimeException;
class zstd extends LinuxLibraryBase
{
public const NAME = 'zstd';
/**
* @throws RuntimeException
*/
public function build()
{
shell()->cd($this->source_dir)
->exec("make {$this->builder->configure_env} PREFIX='" . BUILD_ROOT_PATH . "' clean")
->exec(
"make -j{$this->builder->concurrency} " .
"{$this->builder->configure_env} " .
"PREFIX='" . BUILD_ROOT_PATH . "' " .
'-C lib libzstd.a CPPFLAGS_STATLIB=-DZSTD_MULTITHREAD'
)
->exec('cp lib/libzstd.a ' . BUILD_LIB_PATH)
->exec('cp lib/zdict.h lib/zstd_errors.h lib/zstd.h ' . BUILD_INCLUDE_PATH);
}
}

View File

@@ -9,6 +9,7 @@ use SPC\builder\macos\library\MacOSLibraryBase;
use SPC\builder\traits\UnixBuilderTrait;
use SPC\exception\FileSystemException;
use SPC\exception\RuntimeException;
use SPC\exception\WrongUsageException;
use SPC\util\Patcher;
/**
@@ -20,14 +21,15 @@ class MacOSBuilder extends BuilderBase
/** 编译的 Unix 工具集 */
use UnixBuilderTrait;
/** @var string[] MacOS 环境下编译依赖的命令 */
public const REQUIRED_COMMANDS = ['make', 'bison', 'flex', 'pkg-config', 'git', 'autoconf', 'automake', 'tar', 'unzip', 'xz', 'gzip', 'bzip2', 'cmake'];
/** @var bool 标记是否 patch 了 phar */
private bool $phar_patched = false;
/**
* @param null|string $cc C编译器名称如果不传入则默认使用clang
* @param null|string $cxx C++编译器名称如果不传入则默认使用clang++
* @param null|string $arch 当前架构,如果不传入则默认使用当前系统架构
* @throws RuntimeException
* @throws WrongUsageException
*/
public function __construct(?string $cc = null, ?string $cxx = null, ?string $arch = null)
{
@@ -51,16 +53,6 @@ class MacOSBuilder extends BuilderBase
"CC='{$this->cc}' " .
"CXX='{$this->cxx}' " .
"CFLAGS='{$this->arch_c_flags} -Wimplicit-function-declaration'";
// 保存丢失的命令
$missing = [];
foreach (self::REQUIRED_COMMANDS as $cmd) {
if (SystemUtil::findCommand($cmd) === null) {
$missing[] = $cmd;
}
}
if (!empty($missing)) {
throw new RuntimeException('missing system commands: ' . implode(', ', $missing));
}
// 创立 pkg-config 和放头文件的目录
f_mkdir(BUILD_LIB_PATH . '/pkgconfig', recursive: true);
@@ -127,7 +119,7 @@ class MacOSBuilder extends BuilderBase
* @throws RuntimeException
* @throws FileSystemException
*/
public function buildPHP(int $build_micro_rule = BUILD_MICRO_NONE, bool $with_clean = false, bool $bloat = false): void
public function buildPHP(int $build_target = BUILD_TARGET_NONE, bool $bloat = false): void
{
$extra_libs = $this->getFrameworks(true) . ' ' . ($this->getExt('swoole') ? '-lc++ ' : '');
if (!$bloat) {
@@ -146,11 +138,7 @@ class MacOSBuilder extends BuilderBase
// patch before configure
Patcher::patchPHPBeforeConfigure($this);
f_passthru(
$this->set_x . ' && ' .
'cd ' . SOURCE_PATH . '/php-src && ' .
'./buildconf --force'
);
shell()->cd(SOURCE_PATH . '/php-src')->exec('./buildconf --force');
Patcher::patchPHPConfigure($this);
@@ -158,53 +146,43 @@ class MacOSBuilder extends BuilderBase
$extra_libs .= ' -liconv';
}
f_passthru(
$this->set_x . ' && ' .
'cd ' . SOURCE_PATH . '/php-src && ' .
'./configure ' .
'--prefix= ' .
'--with-valgrind=no ' . // 不检测内存泄漏
'--enable-shared=no ' .
'--enable-static=yes ' .
"--host={$this->gnu_arch}-apple-darwin " .
"CFLAGS='{$this->arch_c_flags} -Werror=unknown-warning-option' " .
'--disable-all ' .
'--disable-cgi ' .
'--disable-phpdbg ' .
'--enable-cli ' .
'--enable-micro ' .
($this->zts ? '--enable-zts' : '') . ' ' .
$this->makeExtensionArgs() . ' ' .
$this->configure_env
);
if ($with_clean) {
logger()->info('cleaning up');
f_passthru(
$this->set_x . ' && ' .
'cd ' . SOURCE_PATH . '/php-src && ' .
'make clean'
shell()->cd(SOURCE_PATH . '/php-src')
->exec(
'./configure ' .
'--prefix= ' .
'--with-valgrind=no ' . // 不检测内存泄漏
'--enable-shared=no ' .
'--enable-static=yes ' .
"--host={$this->gnu_arch}-apple-darwin " .
"CFLAGS='{$this->arch_c_flags} -Werror=unknown-warning-option' " .
'--disable-all ' .
'--disable-cgi ' .
'--disable-phpdbg ' .
'--enable-cli ' .
'--enable-fpm ' .
'--enable-micro ' .
($this->zts ? '--enable-zts' : '') . ' ' .
$this->makeExtensionArgs() . ' ' .
$this->configure_env
);
}
switch ($build_micro_rule) {
case BUILD_MICRO_NONE:
logger()->info('building cli');
$this->buildCli($extra_libs);
break;
case BUILD_MICRO_ONLY:
logger()->info('building micro');
$this->buildMicro($extra_libs);
break;
case BUILD_MICRO_BOTH:
logger()->info('building cli and micro');
$this->buildCli($extra_libs);
$this->buildMicro($extra_libs);
break;
$this->cleanMake();
if (($build_target & BUILD_TARGET_CLI) === BUILD_TARGET_CLI) {
logger()->info('building cli');
$this->buildCli($extra_libs);
}
if (($build_target & BUILD_TARGET_FPM) === BUILD_TARGET_FPM) {
logger()->info('building fpm');
$this->buildFpm($extra_libs);
}
if (($build_target & BUILD_TARGET_MICRO) === BUILD_TARGET_MICRO) {
logger()->info('building micro');
$this->buildMicro($extra_libs);
}
if (php_uname('m') === $this->arch) {
$this->sanityCheck($build_micro_rule);
$this->sanityCheck($build_target);
}
if ($this->phar_patched) {
@@ -213,9 +191,24 @@ class MacOSBuilder extends BuilderBase
}
/**
* 构建 phpmicro
* 构建 cli
*
* @throws RuntimeException
* @throws FileSystemException
*/
public function buildCli(string $extra_libs): void
{
shell()->cd(SOURCE_PATH . '/php-src')
->exec("make -j{$this->concurrency} EXTRA_CFLAGS=\"-g -Os -fno-ident\" EXTRA_LIBS=\"{$extra_libs} -lresolv\" cli")
->exec('dsymutil -f sapi/cli/php')
->exec('strip sapi/cli/php');
$this->deployBinary(BUILD_TARGET_CLI);
}
/**
* 构建 phpmicro
*
* @throws FileSystemException|RuntimeException
*/
public function buildMicro(string $extra_libs): void
{
@@ -225,49 +218,31 @@ class MacOSBuilder extends BuilderBase
if ($this->getExt('phar')) {
$this->phar_patched = true;
try {
f_passthru('cd ' . SOURCE_PATH . '/php-src && patch -p1 < sapi/micro/patches/phar.patch');
// TODO: 未来改进一下 patch让除了这种 patch 类型的文件以外可以恢复原文件
shell()->cd(SOURCE_PATH . '/php-src')->exec('patch -p1 < sapi/micro/patches/phar.patch');
} catch (RuntimeException $e) {
logger()->error('failed to patch phat due to patch exit with code ' . $e->getCode());
$this->phar_patched = false;
}
}
f_passthru(
$this->set_x . ' && ' .
'cd ' . SOURCE_PATH . '/php-src && ' .
"make -j{$this->concurrency} " .
'EXTRA_CFLAGS="-g -Os -fno-ident" ' .
"EXTRA_LIBS=\"{$extra_libs} -lresolv\" " .
'STRIP="dsymutil -f " ' .
// TODO: comment things
'micro'
);
shell()->cd(SOURCE_PATH . '/php-src')
->exec("make -j{$this->concurrency} EXTRA_CFLAGS=\"-g -Os -fno-ident\" EXTRA_LIBS=\"{$extra_libs} -lresolv\" STRIP=\"dsymutil -f \" micro");
$this->deployBinary(BUILD_TARGET_MICRO);
}
/**
* 构建 cli
* 构建 fpm
*
* @throws RuntimeException
* @throws FileSystemException
*/
public function buildCli(string $extra_libs): void
public function buildFpm(string $extra_libs): void
{
f_passthru(
$this->set_x . ' && ' .
'cd ' . SOURCE_PATH . '/php-src && ' .
"make -j{$this->concurrency} " .
'EXTRA_CFLAGS="-g -Os -fno-ident" ' . // 生成调试信息、优化编译后的尺寸、禁用标识符(如变量、函数名)缩短
"EXTRA_LIBS=\"{$extra_libs} -lresolv\" " .
// TODO: comment things
'cli &&' .
'dsymutil -f sapi/cli/php &&' .
'strip sapi/cli/php'
);
}
public function getPHPVersionID(): int
{
$file = file_get_contents(SOURCE_PATH . '/php-src/main/php_version.h');
preg_match('/PHP_VERSION_ID (\d+)/', $file, $match);
return intval($match[1]);
shell()->cd(SOURCE_PATH . '/php-src')
->exec("make -j{$this->concurrency} EXTRA_CFLAGS=\"-g -Os -fno-ident\" EXTRA_LIBS=\"{$extra_libs} -lresolv\" fpm")
->exec('dsymutil -f sapi/fpm/php-fpm')
->exec('strip sapi/fpm/php-fpm');
$this->deployBinary(BUILD_TARGET_FPM);
}
}

View File

@@ -6,6 +6,7 @@ namespace SPC\builder\macos;
use SPC\builder\traits\UnixSystemUtilTrait;
use SPC\exception\RuntimeException;
use SPC\exception\WrongUsageException;
class SystemUtil
{
@@ -19,7 +20,7 @@ class SystemUtil
*/
public static function getCpuCount(): int
{
f_exec('sysctl -n hw.ncpu', $output, $ret);
[$ret, $output] = shell()->execWithResult('sysctl -n hw.ncpu');
if ($ret !== 0) {
throw new RuntimeException('Failed to get cpu count');
}
@@ -30,14 +31,15 @@ class SystemUtil
/**
* 获取不同架构对应的 cflags 参数
*
* @throws RuntimeException
* @param string $arch 架构名称
* @throws WrongUsageException
*/
public static function getArchCFlags(string $arch): string
{
return match ($arch) {
'x86_64' => '--target=x86_64-apple-darwin',
'arm64','aarch64' => '--target=arm64-apple-darwin',
default => throw new RuntimeException('unsupported arch: ' . $arch),
default => throw new WrongUsageException('unsupported arch: ' . $arch),
};
}
}

View File

@@ -14,15 +14,8 @@ abstract class MacOSLibraryBase extends LibraryBase
{
use UnixLibraryTrait;
protected array $static_libs;
protected array $headers;
/**
* 依赖的名字及是否可选例如curl => true代表依赖 curl 但可选
*/
protected array $dep_names;
public function __construct(protected MacOSBuilder $builder)
{
parent::__construct();

View File

@@ -27,23 +27,21 @@ class brotli extends MacOSLibraryBase
protected function build()
{
[$lib, $include, $destdir] = SEPARATED_PATH;
f_passthru(
"{$this->builder->set_x} && " .
"cd {$this->source_dir} && " .
'rm -rf build && ' .
'mkdir -p build && ' .
'cd build && ' .
"{$this->builder->configure_env} " . ' cmake ' .
// '--debug-find ' .
'-DCMAKE_BUILD_TYPE=Release ' .
'-DBUILD_SHARED_LIBS=OFF ' .
'-DCMAKE_INSTALL_PREFIX=/ ' .
"-DCMAKE_INSTALL_LIBDIR={$lib} " .
"-DCMAKE_INSTALL_INCLUDEDIR={$include} " .
"-DCMAKE_TOOLCHAIN_FILE={$this->builder->cmake_toolchain_file} " .
'.. && ' .
"cmake --build . -j {$this->builder->concurrency} && " .
'make install DESTDIR="' . $destdir . '"'
);
shell()->cd($this->source_dir)
->exec('rm -rf build')
->exec('mkdir -p build')
->cd($this->source_dir . '/build')
->exec(
"{$this->builder->configure_env} cmake " .
'-DCMAKE_BUILD_TYPE=Release ' .
'-DBUILD_SHARED_LIBS=OFF ' .
'-DCMAKE_INSTALL_PREFIX=/ ' .
"-DCMAKE_INSTALL_LIBDIR={$lib} " .
"-DCMAKE_INSTALL_INCLUDEDIR={$include} " .
"-DCMAKE_TOOLCHAIN_FILE={$this->builder->cmake_toolchain_file} " .
'..'
)
->exec("cmake --build . -j {$this->builder->concurrency}")
->exec("make install DESTDIR={$destdir}");
}
}

View File

@@ -26,14 +26,10 @@ class bzip2 extends MacOSLibraryBase
protected function build()
{
f_passthru(
$this->builder->set_x . ' && ' .
"cd {$this->source_dir} && " .
"make {$this->builder->configure_env} PREFIX='" . BUILD_ROOT_PATH . "' clean" . ' && ' .
"make -j{$this->builder->concurrency} {$this->builder->configure_env} PREFIX='" . BUILD_ROOT_PATH . "' libbz2.a" . ' && ' .
// make install may fail when cross-compiling, so we copy files.
'cp libbz2.a ' . BUILD_LIB_PATH . ' && ' .
'cp bzlib.h ' . BUILD_INCLUDE_PATH
);
shell()->cd($this->source_dir)
->exec("make {$this->builder->configure_env} PREFIX='" . BUILD_ROOT_PATH . "' clean")
->exec("make -j{$this->builder->concurrency} {$this->builder->configure_env} PREFIX='" . BUILD_ROOT_PATH . "' libbz2.a")
->exec('cp libbz2.a ' . BUILD_LIB_PATH)
->exec('cp bzlib.h ' . BUILD_INCLUDE_PATH);
}
}

View File

@@ -95,24 +95,22 @@ class curl extends MacOSLibraryBase
[$lib, $include, $destdir] = SEPARATED_PATH;
// compile
f_passthru(
$this->builder->set_x . ' && ' .
"cd {$this->source_dir} && " .
'rm -rf build && ' .
'mkdir -p build && ' .
'cd build && ' .
"{$this->builder->configure_env} " . ' cmake ' .
// '--debug-find ' .
'-DCMAKE_BUILD_TYPE=Release ' .
'-DBUILD_SHARED_LIBS=OFF ' .
$extra .
'-DCMAKE_INSTALL_PREFIX= ' .
"-DCMAKE_INSTALL_LIBDIR={$lib} " .
"-DCMAKE_INSTALL_INCLUDEDIR={$include} " .
"-DCMAKE_TOOLCHAIN_FILE={$this->builder->cmake_toolchain_file} " .
'.. && ' .
"make -j{$this->builder->concurrency} && " .
'make install DESTDIR="' . $destdir . '"'
);
shell()->cd($this->source_dir)
->exec('rm -rf build')
->exec('mkdir -p build')
->cd($this->source_dir . '/build')
->exec(
"{$this->builder->configure_env} cmake " .
'-DCMAKE_BUILD_TYPE=Release ' .
'-DBUILD_SHARED_LIBS=OFF ' .
$extra .
'-DCMAKE_INSTALL_PREFIX= ' .
"-DCMAKE_INSTALL_LIBDIR={$lib} " .
"-DCMAKE_INSTALL_INCLUDEDIR={$include} " .
"-DCMAKE_TOOLCHAIN_FILE={$this->builder->cmake_toolchain_file} " .
'..'
)
->exec("make -j{$this->builder->concurrency}")
->exec("make install DESTDIR={$destdir}");
}
}

View File

@@ -23,16 +23,14 @@ class freetype extends MacOSLibraryBase
$suggested .= ($this->builder->getLib('brotli') instanceof MacOSLibraryBase) ? ('--with-brotli=' . BUILD_ROOT_PATH) : '--without-brotli';
$suggested .= ' ';
f_passthru(
$this->builder->set_x . ' && ' .
"cd {$this->source_dir} && " .
"{$this->builder->configure_env} ./configure " .
'--enable-static --disable-shared --without-harfbuzz ' .
$suggested .
'--prefix= && ' . // use prefix=/
'make clean && ' .
"make -j{$this->builder->concurrency} && " .
'make install DESTDIR=' . $destdir
);
shell()->cd($this->source_dir)
->exec(
"{$this->builder->configure_env} ./configure " .
'--enable-static --disable-shared --without-harfbuzz --prefix= ' .
$suggested
)
->exec('make clean')
->exec("make -j{$this->builder->concurrency}")
->exec("make install DESTDIR={$destdir}");
}
}

View File

@@ -15,15 +15,14 @@ class gmp extends MacOSLibraryBase
{
[,,$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
);
shell()->cd($this->source_dir)
->exec(
"{$this->builder->configure_env} ./configure " .
'--enable-static --disable-shared ' .
'--prefix='
)
->exec('make clean')
->exec("make -j{$this->builder->concurrency}")
->exec("make install DESTDIR={$destdir}");
}
}

View File

@@ -26,20 +26,19 @@ class libffi extends MacOSLibraryBase
protected function build()
{
[$lib, $include, $destdir] = SEPARATED_PATH;
f_passthru(
$this->builder->set_x . ' && ' .
"cd {$this->source_dir} && " .
"{$this->builder->configure_env} ./configure " .
'--enable-static ' .
'--disable-shared ' .
"--host={$this->builder->arch}-apple-darwin " .
"--target={$this->builder->arch}-apple-darwin " .
'--prefix= ' . // use prefix=/
"--libdir={$lib} && " .
'make clean && ' .
"make -j{$this->builder->concurrency} && " .
"make install DESTDIR={$destdir}"
);
[$lib, , $destdir] = SEPARATED_PATH;
shell()->cd($this->source_dir)
->exec(
"{$this->builder->configure_env} ./configure " .
'--enable-static ' .
'--disable-shared ' .
"--host={$this->builder->arch}-apple-darwin " .
"--target={$this->builder->arch}-apple-darwin " .
'--prefix= ' . // use prefix=/
"--libdir={$lib}"
)
->exec('make clean')
->exec("make -j{$this->builder->concurrency}")
->exec("make install DESTDIR={$destdir}");
}
}

View File

@@ -0,0 +1,47 @@
<?php
/**
* Copyright (c) 2022 Yun Dou <dixyes@gmail.com>
*
* lwmbs is licensed under Mulan PSL v2. You can use this
* software according to the terms and conditions of the
* Mulan PSL v2. You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS,
* WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
*
* See the Mulan PSL v2 for more details.
*/
declare(strict_types=1);
namespace SPC\builder\macos\library;
use SPC\exception\RuntimeException;
class libiconv extends MacOSLibraryBase
{
public const NAME = 'libiconv';
/**
* @throws RuntimeException
*/
public function build()
{
[,,$destdir] = SEPARATED_PATH;
shell()->cd($this->source_dir)
->exec(
"{$this->builder->configure_env} ./configure " .
'--enable-static ' .
'--disable-shared ' .
'--prefix='
)
->exec('make clean')
->exec("make -j{$this->builder->concurrency}")
->exec('make install DESTDIR=' . $destdir);
}
}

View File

@@ -43,23 +43,21 @@ class libpng extends MacOSLibraryBase
// patch configure
Patcher::patchUnixLibpng();
f_passthru(
$this->builder->set_x . ' && ' .
"cd {$this->source_dir} && " .
"{$this->builder->configure_env} " .
'./configure ' .
"--host={$this->builder->gnu_arch}-apple-darwin " .
'--disable-shared ' .
'--enable-static ' .
'--enable-hardware-optimizations ' .
$optimizations .
'--prefix= && ' . // use prefix=/
'make clean && ' .
"make -j{$this->builder->concurrency} DEFAULT_INCLUDES='-I. -I" . BUILD_INCLUDE_PATH . "' LIBS= libpng16.la && " .
'make install-libLTLIBRARIES install-data-am DESTDIR=' . BUILD_ROOT_PATH . ' && ' .
'cd ' . BUILD_LIB_PATH . ' && ' .
'ln -sf libpng16.a libpng.a'
);
shell()->cd($this->source_dir)
->exec('chmod +x ./configure')
->exec(
"{$this->builder->configure_env} ./configure " .
"--host={$this->builder->gnu_arch}-apple-darwin " .
'--disable-shared ' .
'--enable-static ' .
'--enable-hardware-optimizations ' .
$optimizations .
'--prefix='
)
->exec('make clean')
->exec("make -j{$this->builder->concurrency} DEFAULT_INCLUDES='-I. -I" . BUILD_INCLUDE_PATH . "' LIBS= libpng16.la")
->exec('make install-libLTLIBRARIES install-data-am DESTDIR=' . BUILD_ROOT_PATH)
->cd(BUILD_LIB_PATH)
->exec('ln -sf libpng16.a libpng.a');
}
}

View File

@@ -31,26 +31,24 @@ class libssh2 extends MacOSLibraryBase
[$lib, $include, $destdir] = SEPARATED_PATH;
f_passthru(
$this->builder->set_x . ' && ' .
"cd {$this->source_dir} && " .
'rm -rf build && ' .
'mkdir -p build && ' .
'cd build && ' .
"{$this->builder->configure_env} " . ' cmake ' .
// '--debug-find ' .
'-DCMAKE_BUILD_TYPE=Release ' .
'-DBUILD_SHARED_LIBS=OFF ' .
'-DBUILD_EXAMPLES=OFF ' .
'-DBUILD_TESTING=OFF ' .
"-DENABLE_ZLIB_COMPRESSION={$enable_zlib} " .
'-DCMAKE_INSTALL_PREFIX=/ ' .
"-DCMAKE_INSTALL_LIBDIR={$lib} " .
"-DCMAKE_INSTALL_INCLUDEDIR={$include} " .
"-DCMAKE_TOOLCHAIN_FILE={$this->builder->cmake_toolchain_file} " .
'.. && ' .
"cmake --build . -j {$this->builder->concurrency} --target libssh2 && " .
'make install DESTDIR="' . $destdir . '"'
);
shell()->cd($this->source_dir)
->exec('rm -rf build')
->exec('mkdir -p build')
->cd($this->source_dir . '/build')
->exec(
"{$this->builder->configure_env} " . ' cmake ' .
'-DCMAKE_BUILD_TYPE=Release ' .
'-DBUILD_SHARED_LIBS=OFF ' .
'-DBUILD_EXAMPLES=OFF ' .
'-DBUILD_TESTING=OFF ' .
"-DENABLE_ZLIB_COMPRESSION={$enable_zlib} " .
'-DCMAKE_INSTALL_PREFIX=/ ' .
"-DCMAKE_INSTALL_LIBDIR={$lib} " .
"-DCMAKE_INSTALL_INCLUDEDIR={$include} " .
"-DCMAKE_TOOLCHAIN_FILE={$this->builder->cmake_toolchain_file} " .
'..'
)
->exec("cmake --build . -j {$this->builder->concurrency} --target libssh2")
->exec("make install DESTDIR={$destdir}");
}
}

View File

@@ -1,29 +0,0 @@
<?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
);
}
}

View File

@@ -37,30 +37,29 @@ class libxml2 extends MacOSLibraryBase
[$lib, $include, $destdir] = SEPARATED_PATH;
f_passthru(
$this->builder->set_x . ' && ' .
"cd {$this->source_dir} && " .
'rm -rf build && ' .
'mkdir -p build && ' .
'cd build && ' .
"{$this->builder->configure_env} " . ' cmake ' .
// '--debug-find ' .
'-DCMAKE_BUILD_TYPE=Release ' .
'-DBUILD_SHARED_LIBS=OFF ' .
'-DLIBXML2_WITH_ICONV=ON ' .
"-DLIBXML2_WITH_ZLIB={$enable_zlib} " .
"-DLIBXML2_WITH_ICU={$enable_icu} " .
"-DLIBXML2_WITH_LZMA={$enable_xz} " .
'-DLIBXML2_WITH_PYTHON=OFF ' .
'-DLIBXML2_WITH_PROGRAMS=OFF ' .
'-DLIBXML2_WITH_TESTS=OFF ' .
'-DCMAKE_INSTALL_PREFIX=/ ' .
"-DCMAKE_INSTALL_LIBDIR={$lib} " .
"-DCMAKE_INSTALL_INCLUDEDIR={$include} " .
"-DCMAKE_TOOLCHAIN_FILE={$this->builder->cmake_toolchain_file} " .
'.. && ' .
"cmake --build . -j {$this->builder->concurrency} && " .
'make install DESTDIR="' . $destdir . '"'
);
shell()->cd($this->source_dir)
->exec('rm -rf build')
->exec('mkdir -p build')
->cd($this->source_dir . '/build')
->exec(
"{$this->builder->configure_env} " . ' cmake ' .
// '--debug-find ' .
'-DCMAKE_BUILD_TYPE=Release ' .
'-DBUILD_SHARED_LIBS=OFF ' .
'-DLIBXML2_WITH_ICONV=ON ' .
"-DLIBXML2_WITH_ZLIB={$enable_zlib} " .
"-DLIBXML2_WITH_ICU={$enable_icu} " .
"-DLIBXML2_WITH_LZMA={$enable_xz} " .
'-DLIBXML2_WITH_PYTHON=OFF ' .
'-DLIBXML2_WITH_PROGRAMS=OFF ' .
'-DLIBXML2_WITH_TESTS=OFF ' .
'-DCMAKE_INSTALL_PREFIX=/ ' .
"-DCMAKE_INSTALL_LIBDIR={$lib} " .
"-DCMAKE_INSTALL_INCLUDEDIR={$include} " .
"-DCMAKE_TOOLCHAIN_FILE={$this->builder->cmake_toolchain_file} " .
'..'
)
->exec("cmake --build . -j {$this->builder->concurrency}")
->exec("make install DESTDIR={$destdir}");
}
}

View File

@@ -72,24 +72,23 @@ EOF
[$lib, $include, $destdir] = SEPARATED_PATH;
f_passthru(
$this->builder->set_x . ' && ' .
"cd {$this->source_dir} && " .
'rm -rf build && ' .
'mkdir -p build && ' .
'cd build && ' .
"{$this->builder->configure_env} " . ' cmake ' .
// '--debug-find ' .
'-DCMAKE_BUILD_TYPE=Release ' .
'-DBUILD_TESTING=OFF ' .
'-DBUILD_SHARED_LIBS=OFF ' .
'-DCMAKE_INSTALL_PREFIX=/ ' .
"-DCMAKE_INSTALL_LIBDIR={$lib} " .
"-DCMAKE_INSTALL_INCLUDEDIR={$include} " .
"-DCMAKE_TOOLCHAIN_FILE={$this->builder->cmake_toolchain_file} " .
'.. && ' .
"make -j{$this->builder->concurrency} && " .
'make install DESTDIR=' . $destdir
);
shell()->cd($this->source_dir)
->exec('rm -rf build')
->exec('mkdir -p build')
->cd($this->source_dir . '/build')
->exec(
"{$this->builder->configure_env} " . ' cmake ' .
// '--debug-find ' .
'-DCMAKE_BUILD_TYPE=Release ' .
'-DBUILD_TESTING=OFF ' .
'-DBUILD_SHARED_LIBS=OFF ' .
'-DCMAKE_INSTALL_PREFIX=/ ' .
"-DCMAKE_INSTALL_LIBDIR={$lib} " .
"-DCMAKE_INSTALL_INCLUDEDIR={$include} " .
"-DCMAKE_TOOLCHAIN_FILE={$this->builder->cmake_toolchain_file} " .
'..'
)
->exec("make -j{$this->builder->concurrency}")
->exec("make install DESTDIR={$destdir}");
}
}

View File

@@ -72,30 +72,29 @@ class libzip extends MacOSLibraryBase
[$lib, $include, $destdir] = SEPARATED_PATH;
f_passthru(
$this->builder->set_x . ' && ' .
"cd {$this->source_dir} && " .
'rm -rf build && ' .
'mkdir -p build && ' .
'cd build && ' .
"{$this->builder->configure_env} " . ' cmake ' .
// '--debug-find ' .
'-DCMAKE_BUILD_TYPE=Release ' .
'-DENABLE_GNUTLS=OFF ' .
'-DENABLE_MBEDTLS=OFF ' .
'-DBUILD_SHARED_LIBS=OFF ' .
'-DBUILD_DOC=OFF ' .
'-DBUILD_EXAMPLES=OFF ' .
'-DBUILD_REGRESS=OFF ' .
'-DBUILD_TOOLS=OFF ' .
$extra .
'-DCMAKE_INSTALL_PREFIX=/ ' .
"-DCMAKE_INSTALL_LIBDIR={$lib} " .
"-DCMAKE_INSTALL_INCLUDEDIR={$include} " .
"-DCMAKE_TOOLCHAIN_FILE={$this->builder->cmake_toolchain_file} " .
'.. && ' .
"make -j{$this->builder->concurrency} && " .
'make install DESTDIR=' . $destdir
);
shell()->cd($this->source_dir)
->exec('rm -rf build')
->exec('mkdir -p build')
->cd($this->source_dir . '/build')
->exec(
"{$this->builder->configure_env} " . ' cmake ' .
// '--debug-find ' .
'-DCMAKE_BUILD_TYPE=Release ' .
'-DENABLE_GNUTLS=OFF ' .
'-DENABLE_MBEDTLS=OFF ' .
'-DBUILD_SHARED_LIBS=OFF ' .
'-DBUILD_DOC=OFF ' .
'-DBUILD_EXAMPLES=OFF ' .
'-DBUILD_REGRESS=OFF ' .
'-DBUILD_TOOLS=OFF ' .
$extra .
'-DCMAKE_INSTALL_PREFIX=/ ' .
"-DCMAKE_INSTALL_LIBDIR={$lib} " .
"-DCMAKE_INSTALL_INCLUDEDIR={$include} " .
"-DCMAKE_TOOLCHAIN_FILE={$this->builder->cmake_toolchain_file} " .
'..'
)
->exec("make -j{$this->builder->concurrency}")
->exec("make install DESTDIR={$destdir}");
}
}

View File

@@ -44,20 +44,19 @@ class nghttp2 extends MacOSLibraryBase
[,,$destdir] = SEPARATED_PATH;
f_passthru(
$this->builder->set_x . ' && ' .
"cd {$this->source_dir} && " .
"{$this->builder->configure_env} " . ' ./configure ' .
'--enable-static ' .
'--disable-shared ' .
"--host={$this->builder->gnu_arch}-apple-darwin " .
'--enable-lib-only ' .
'--with-boost=no ' .
$args . ' ' .
'--prefix= && ' . // use prefix=/
'make clean && ' .
"make -j{$this->builder->concurrency} && " .
"make install DESTDIR={$destdir}"
);
shell()->cd($this->source_dir)
->exec(
"{$this->builder->configure_env} " . ' ./configure ' .
'--enable-static ' .
'--disable-shared ' .
"--host={$this->builder->gnu_arch}-apple-darwin " .
'--enable-lib-only ' .
'--with-boost=no ' .
$args . ' ' .
'--prefix='
)
->exec('make clean')
->exec("make -j{$this->builder->concurrency}")
->exec("make install DESTDIR={$destdir}");
}
}

View File

@@ -28,17 +28,16 @@ class onig extends MacOSLibraryBase
{
[,,$destdir] = SEPARATED_PATH;
f_passthru(
$this->builder->set_x . ' && ' .
"cd {$this->source_dir} && " .
"{$this->builder->configure_env} " . ' ./configure ' .
'--enable-static ' .
'--disable-shared ' .
"--host={$this->builder->arch}-apple-darwin " .
'--prefix= && ' . // use prefix=/
'make clean && ' .
"make -j{$this->builder->concurrency} && " .
'make install DESTDIR=' . $destdir
);
shell()->cd($this->source_dir)
->exec(
"{$this->builder->configure_env} " . ' ./configure ' .
'--enable-static ' .
'--disable-shared ' .
"--host={$this->builder->arch}-apple-darwin " .
'--prefix='
)
->exec('make clean')
->exec("make -j{$this->builder->concurrency}")
->exec("make install DESTDIR={$destdir}");
}
}

View File

@@ -37,16 +37,15 @@ class openssl extends MacOSLibraryBase
$ex_lib = trim($zlib->getStaticLibFiles() . ' ' . $ex_lib);
}
f_passthru(
$this->builder->set_x . ' && ' .
"cd {$this->source_dir} && " .
"{$this->builder->configure_env} ./Configure no-shared {$extra} " .
'--prefix=/ ' . // use prefix=/
"--libdir={$lib} " .
" darwin64-{$this->builder->arch}-cc && " .
'make clean && ' .
"make -j{$this->builder->concurrency} CNF_EX_LIBS=\"{$ex_lib}\" && " .
'make install_sw DESTDIR=' . $destdir
);
shell()->cd($this->source_dir)
->exec(
"{$this->builder->configure_env} ./Configure no-shared {$extra} " .
'--prefix=/ ' . // use prefix=/
"--libdir={$lib} " .
" darwin64-{$this->builder->arch}-cc"
)
->exec('make clean')
->exec("make -j{$this->builder->concurrency} CNF_EX_LIBS=\"{$ex_lib}\"")
->exec("make install_sw DESTDIR={$destdir}");
}
}

View File

@@ -11,16 +11,10 @@ class sqlite extends MacOSLibraryBase
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
);
shell()->cd($this->source_dir)
->exec("{$this->builder->configure_env} ./configure --enable-static --disable-shared --prefix=")
->exec('make clean')
->exec("make -j{$this->builder->concurrency}")
->exec("make install DESTDIR={$destdir}");
}
}

View File

@@ -28,25 +28,24 @@ class xz extends MacOSLibraryBase
{
[,,$destdir] = SEPARATED_PATH;
f_passthru(
$this->builder->set_x . ' && ' .
"cd {$this->source_dir} && " .
'autoreconf -i --force && ' .
"{$this->builder->configure_env} ./configure " .
'--enable-static ' .
'--disable-shared ' .
"--host={$this->builder->gnu_arch}-apple-darwin " .
'--disable-xz ' .
'--disable-xzdec ' .
'--disable-lzmadec ' .
'--disable-lzmainfo ' .
'--disable-scripts ' .
'--disable-doc ' .
'--with-libiconv ' .
'--prefix= && ' . // use prefix=/
'make clean && ' .
"make -j{$this->builder->concurrency} && " .
'make install DESTDIR=' . $destdir
);
shell()->cd($this->source_dir)
->exec('autoreconf -i --force')
->exec(
"{$this->builder->configure_env} ./configure " .
'--enable-static ' .
'--disable-shared ' .
"--host={$this->builder->gnu_arch}-apple-darwin " .
'--disable-xz ' .
'--disable-xzdec ' .
'--disable-lzmadec ' .
'--disable-lzmainfo ' .
'--disable-scripts ' .
'--disable-doc ' .
'--with-libiconv ' .
'--prefix='
)
->exec('make clean')
->exec("make -j{$this->builder->concurrency}")
->exec("make install DESTDIR={$destdir}");
}
}

View File

@@ -28,15 +28,10 @@ class zlib extends MacOSLibraryBase
{
[,,$destdir] = SEPARATED_PATH;
f_passthru(
$this->builder->set_x . ' && ' .
"cd {$this->source_dir} && " .
"{$this->builder->configure_env} ./configure " .
'--static ' .
'--prefix= && ' . // use prefix=/
'make clean && ' .
"make -j{$this->builder->concurrency} && " .
'make install DESTDIR=' . $destdir
);
shell()->cd($this->source_dir)
->exec("{$this->builder->configure_env} ./configure --static --prefix=")
->exec('make clean')
->exec("make -j{$this->builder->concurrency}")
->exec("make install DESTDIR={$destdir}");
}
}

View File

@@ -26,16 +26,15 @@ class zstd extends MacOSLibraryBase
protected function build()
{
f_passthru(
$this->builder->set_x . ' && ' .
"cd {$this->source_dir} && " .
"make {$this->builder->configure_env} PREFIX='" . BUILD_ROOT_PATH . "' clean" . ' && ' .
"make -j{$this->builder->concurrency} " .
"{$this->builder->configure_env} " .
"PREFIX='" . BUILD_ROOT_PATH . "' " .
'-C lib libzstd.a CPPFLAGS_STATLIB=-DZSTD_MULTITHREAD && ' .
'cp lib/libzstd.a ' . BUILD_LIB_PATH . ' && ' .
'cp lib/zdict.h lib/zstd_errors.h lib/zstd.h ' . BUILD_INCLUDE_PATH
);
shell()->cd($this->source_dir)
->exec("make {$this->builder->configure_env} PREFIX='" . BUILD_ROOT_PATH . "' clean")
->exec(
"make -j{$this->builder->concurrency} " .
"{$this->builder->configure_env} " .
"PREFIX='" . BUILD_ROOT_PATH . "' " .
'-C lib libzstd.a CPPFLAGS_STATLIB=-DZSTD_MULTITHREAD'
)
->exec('cp lib/libzstd.a ' . BUILD_LIB_PATH)
->exec('cp lib/zdict.h lib/zstd_errors.h lib/zstd.h ' . BUILD_INCLUDE_PATH);
}
}

View File

@@ -1,13 +0,0 @@
<?php
declare(strict_types=1);
namespace SPC\builder\traits;
/**
* 仅供 Command 使用,如果使用了该 Trait则在执行对应命令时不打印 motd
*/
trait NoMotdTrait
{
protected bool $no_motd = true;
}

View File

@@ -4,7 +4,9 @@ declare(strict_types=1);
namespace SPC\builder\traits;
use SPC\exception\FileSystemException;
use SPC\exception\RuntimeException;
use SPC\store\FileSystem;
trait UnixBuilderTrait
{
@@ -54,36 +56,36 @@ trait UnixBuilderTrait
}
/**
* Sanity check after build complete
*
* @throws RuntimeException
*/
public function sanityCheck(int $build_micro_rule): void
public function sanityCheck(int $build_target): void
{
logger()->info('running sanity check');
if ($build_micro_rule !== BUILD_MICRO_ONLY) {
f_exec(
$this->set_x . ' && ' .
SOURCE_PATH . '/php-src/sapi/cli/php -r "echo \"hello\";"',
$output,
$ret
);
// sanity check for php-cli
if (($build_target & BUILD_TARGET_CLI) === BUILD_TARGET_CLI) {
logger()->info('running cli sanity check');
[$ret, $output] = shell()->execWithResult(BUILD_ROOT_PATH . '/bin/php -r "echo \"hello\";"');
if ($ret !== 0 || trim(implode('', $output)) !== 'hello') {
throw new RuntimeException('cli failed sanity check');
}
foreach ($this->exts as $ext) {
logger()->debug('checking ext: ' . $ext->getName());
logger()->debug('testing ext: ' . $ext->getName());
[$ret] = shell()->execWithResult(BUILD_ROOT_PATH . '/bin/php --ri ' . $ext->getDistName(), false);
if ($ret !== 0) {
throw new RuntimeException('extension ' . $ext->getName() . ' failed compile check');
}
if (file_exists(ROOT_DIR . '/src/globals/tests/' . $ext->getName() . '.php')) {
f_exec(
$this->set_x . ' && ' . SOURCE_PATH . '/php-src/sapi/cli/php ' . ROOT_DIR . '/src/globals/tests/' . $ext->getName() . '.php',
$output,
$ret
);
[$ret] = shell()->execWithResult(BUILD_ROOT_PATH . '/bin/php ' . ROOT_DIR . '/src/globals/tests/' . $ext->getName() . '.php');
if ($ret !== 0) {
throw new RuntimeException('extension ' . $ext->getName() . ' failed sanity check');
}
}
}
}
if ($build_micro_rule !== BUILD_MICRO_NONE) {
// sanity check for phpmicro
if (($build_target & BUILD_TARGET_MICRO) === BUILD_TARGET_MICRO) {
if (file_exists(SOURCE_PATH . '/hello.exe')) {
@unlink(SOURCE_PATH . '/hello.exe');
}
@@ -93,10 +95,42 @@ trait UnixBuilderTrait
'<?php echo "hello";'
);
chmod(SOURCE_PATH . '/hello.exe', 0755);
f_exec(SOURCE_PATH . '/hello.exe', $output2, $ret);
[$ret, $output2] = shell()->execWithResult(SOURCE_PATH . '/hello.exe');
if ($ret !== 0 || trim($out = implode('', $output2)) !== 'hello') {
throw new RuntimeException('micro failed sanity check, ret[' . $ret . '], out[' . ($out ?? 'NULL') . ']');
}
}
}
/**
* 将编译好的二进制文件发布到 buildroot
*
* @param int $type 发布类型
* @throws RuntimeException
* @throws FileSystemException
*/
public function deployBinary(int $type): bool
{
$src = match ($type) {
BUILD_TARGET_CLI => SOURCE_PATH . '/php-src/sapi/cli/php',
BUILD_TARGET_MICRO => SOURCE_PATH . '/php-src/sapi/micro/micro.sfx',
BUILD_TARGET_FPM => SOURCE_PATH . '/php-src/sapi/fpm/php-fpm',
default => throw new RuntimeException('Deployment does not accept type ' . $type),
};
logger()->info('Deploying ' . $this->getBuildTypeName($type) . ' file');
FileSystem::createDir(BUILD_ROOT_PATH . '/bin');
shell()->exec('cp ' . escapeshellarg($src) . ' ' . escapeshellarg(BUILD_ROOT_PATH . '/bin/'));
return true;
}
/**
* 清理编译好的文件
*
* @throws RuntimeException
*/
public function cleanMake(): void
{
logger()->info('cleaning up');
shell()->cd(SOURCE_PATH . '/php-src')->exec('make clean');
}
}

View File

@@ -4,6 +4,8 @@ declare(strict_types=1);
namespace SPC\builder\traits;
use SPC\store\FileSystem;
/**
* Unix 系统的工具函数 Trait适用于 Linux、macOS
*/
@@ -48,7 +50,7 @@ CMAKE;
if (PHP_OS_FAMILY === 'Linux') {
$toolchain .= "\nSET(CMAKE_AR \"ar\")";
}
file_put_contents(SOURCE_PATH . '/toolchain.cmake', $toolchain);
FileSystem::writeFile(SOURCE_PATH . '/toolchain.cmake', $toolchain);
return realpath(SOURCE_PATH . '/toolchain.cmake');
}

View File

@@ -13,14 +13,32 @@ use ZM\Logger\ConsoleLogger;
abstract class BaseCommand extends Command
{
protected bool $no_motd = false;
/**
* 输入
*/
protected InputInterface $input;
/**
* 输出
*
* 一般来说同样会是 ConsoleOutputInterface
*/
protected OutputInterface $output;
public function __construct(string $name = null)
{
parent::__construct($name);
$this->addOption('debug', null, null, 'Enable debug mode');
$this->addOption('no-motd', null, null, 'Disable motd');
}
public function initialize(InputInterface $input, OutputInterface $output)
{
if ($input->getOption('no-motd')) {
$this->no_motd = true;
}
// 注册全局错误处理器
set_error_handler(static function ($error_no, $error_msg, $error_file, $error_line) {
$tips = [
@@ -46,7 +64,7 @@ abstract class BaseCommand extends Command
define('DEBUG_MODE', true);
}
$version = ConsoleApplication::VERSION;
if (!isset($this->no_motd)) {
if (!$this->no_motd) {
echo " _ _ _ _
___| |_ __ _| |_(_) ___ _ __ | |__ _ __
/ __| __/ _` | __| |/ __|____| '_ \\| '_ \\| '_ \\
@@ -56,4 +74,44 @@ abstract class BaseCommand extends Command
";
}
}
abstract public function handle(): int;
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->input = $input;
$this->output = $output;
if ($this->shouldExecute()) {
try {
return $this->handle();
} catch (\Throwable $e) {
$msg = explode("\n", $e->getMessage());
foreach ($msg as $v) {
logger()->error($v);
}
return self::FAILURE;
}
}
return self::SUCCESS;
}
protected function getOption(string $name): mixed
{
return $this->input->getOption($name);
}
protected function getArgument(string $name): mixed
{
return $this->input->getArgument($name);
}
/**
* 是否应该执行
*
* @return bool 返回 true 以继续执行,返回 false 以中断执行
*/
protected function shouldExecute(): bool
{
return true;
}
}

View File

@@ -6,53 +6,60 @@ namespace SPC\command;
use SPC\builder\BuilderProvider;
use SPC\exception\ExceptionHandler;
use SPC\exception\WrongUsageException;
use SPC\util\DependencyUtil;
use SPC\util\LicenseDumper;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use ZM\Logger\ConsoleColor;
/** @noinspection PhpUnused */
#[AsCommand('build', 'build CLI binary')]
class BuildCliCommand extends BuildCommand
{
protected static $defaultName = 'build';
public function configure()
{
$this->setDescription('Build CLI binary');
$this->addArgument('extensions', InputArgument::REQUIRED, 'The extensions will be compiled, comma separated');
$this->addOption('with-libs', null, InputOption::VALUE_REQUIRED, 'add additional libraries, comma separated', '');
$this->addOption('build-micro', null, null, 'build micro only');
$this->addOption('build-all', null, null, 'build both cli and micro');
$this->addOption('build-micro', null, null, 'build micro');
$this->addOption('build-cli', null, null, 'build cli');
$this->addOption('build-fpm', null, null, 'build fpm');
$this->addOption('build-all', null, null, 'build cli, micro, fpm');
}
public function execute(InputInterface $input, OutputInterface $output): int
public function handle(): int
{
// 从参数中获取要编译的 libraries并转换为数组
$libraries = array_map('trim', array_filter(explode(',', $input->getOption('with-libs'))));
$libraries = array_map('trim', array_filter(explode(',', $this->getOption('with-libs'))));
// 从参数中获取要编译的 extensions并转换为数组
$extensions = array_map('trim', array_filter(explode(',', $input->getArgument('extensions'))));
$extensions = array_map('trim', array_filter(explode(',', $this->getArgument('extensions'))));
define('BUILD_ALL_STATIC', true);
if ($input->getOption('build-all')) {
$rule = BUILD_MICRO_BOTH;
logger()->info('Builder will build php-cli and phpmicro SAPI');
} elseif ($input->getOption('build-micro')) {
$rule = BUILD_MICRO_ONLY;
logger()->info('Builder will build phpmicro SAPI');
} else {
$rule = BUILD_MICRO_NONE;
logger()->info('Builder will build php-cli SAPI');
$rule = BUILD_TARGET_NONE;
$rule = $rule | ($this->getOption('build-cli') ? BUILD_TARGET_CLI : BUILD_TARGET_NONE);
$rule = $rule | ($this->getOption('build-micro') ? BUILD_TARGET_MICRO : BUILD_TARGET_NONE);
$rule = $rule | ($this->getOption('build-fpm') ? BUILD_TARGET_FPM : BUILD_TARGET_NONE);
$rule = $rule | ($this->getOption('build-all') ? BUILD_TARGET_ALL : BUILD_TARGET_NONE);
if ($rule === BUILD_TARGET_NONE) {
$this->output->writeln('<error>Please add at least one build target!</error>');
$this->output->writeln("<comment>\t--build-cli\tBuild php-cli SAPI</comment>");
$this->output->writeln("<comment>\t--build-micro\tBuild phpmicro SAPI</comment>");
$this->output->writeln("<comment>\t--build-fpm\tBuild php-fpm SAPI</comment>");
$this->output->writeln("<comment>\t--build-all\tBuild all SAPI: cli, micro, fpm</comment>");
return 1;
}
try {
// 构建对象
$builder = BuilderProvider::makeBuilderByInput($input);
$builder = BuilderProvider::makeBuilderByInput($this->input);
// 根据提供的扩展列表获取依赖库列表并编译
[$extensions, $libraries, $not_included] = DependencyUtil::getExtLibsByDeps($extensions, $libraries);
logger()->info('Enabled extensions: ' . implode(', ', $extensions));
logger()->info('Required libraries: ' . implode(', ', $libraries));
/* @phpstan-ignore-next-line */
logger()->info('Build target: ' . ConsoleColor::yellow($builder->getBuildTypeName($rule)));
/* @phpstan-ignore-next-line */
logger()->info('Enabled extensions: ' . ConsoleColor::yellow(implode(', ', $extensions)));
/* @phpstan-ignore-next-line */
logger()->info('Required libraries: ' . ConsoleColor::yellow(implode(', ', $libraries)));
if (!empty($not_included)) {
logger()->warning('some extensions will be enabled due to dependencies: ' . implode(',', $not_included));
}
@@ -62,19 +69,32 @@ class BuildCliCommand extends BuildCommand
// 执行扩展检测
$builder->proveExts($extensions);
// 构建
$builder->buildPHP($rule, $input->getOption('with-clean'), $input->getOption('bloat'));
$builder->buildPHP($rule, $this->getOption('bloat'));
// 统计时间
$time = round(microtime(true) - START_TIME, 3);
logger()->info('Build complete, used ' . $time . ' s !');
if ($rule !== BUILD_MICRO_ONLY) {
logger()->info('Static php binary path: ' . SOURCE_PATH . '/php-src/sapi/cli/php');
if (($rule & BUILD_TARGET_CLI) === BUILD_TARGET_CLI) {
logger()->info('Static php binary path: ' . BUILD_ROOT_PATH . '/bin/php');
}
if ($rule !== BUILD_MICRO_NONE) {
logger()->info('phpmicro binary path: ' . SOURCE_PATH . '/php-src/sapi/micro/micro.sfx');
if (($rule & BUILD_TARGET_MICRO) === BUILD_TARGET_MICRO) {
logger()->info('phpmicro binary path: ' . BUILD_ROOT_PATH . '/bin/micro.sfx');
}
if (($rule & BUILD_TARGET_FPM) === BUILD_TARGET_FPM) {
logger()->info('Static php-fpm binary path: ' . BUILD_ROOT_PATH . '/bin/php-fpm');
}
// 导出相关元数据
file_put_contents(BUILD_ROOT_PATH . '/build-extensions.json', json_encode($extensions, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
file_put_contents(BUILD_ROOT_PATH . '/build-libraries.json', json_encode($libraries, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
// 导出 LICENSE
$dumper = new LicenseDumper();
$dumper->addExts($extensions)->addLibs($libraries)->addSources(['php-src'])->dump(BUILD_ROOT_PATH . '/license');
logger()->info('License path: ' . BUILD_ROOT_PATH . '/license/');
return 0;
} catch (WrongUsageException $e) {
logger()->critical($e->getMessage());
return 1;
} catch (\Throwable $e) {
if ($input->getOption('debug')) {
if ($this->getOption('debug')) {
ExceptionHandler::getInstance()->handle($e);
} else {
logger()->critical('Build failed with ' . get_class($e) . ': ' . $e->getMessage());

View File

@@ -6,20 +6,17 @@ namespace SPC\command;
use SPC\builder\BuilderProvider;
use SPC\exception\ExceptionHandler;
use SPC\exception\FileSystemException;
use SPC\exception\RuntimeException;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/** @noinspection PhpUnused */
#[AsCommand('build:libs', 'Build dependencies')]
class BuildLibsCommand extends BuildCommand
{
protected static $defaultName = 'build:libs';
public function configure()
{
$this->setDescription('Build dependencies');
$this->addArgument('libraries', InputArgument::REQUIRED, 'The libraries will be compiled, comma separated');
$this->addOption('clean', null, null, 'Clean old download cache and source before fetch');
$this->addOption('all', 'A', null, 'Build all libs that static-php-cli needed');
@@ -36,15 +33,14 @@ class BuildLibsCommand extends BuildCommand
/**
* @throws RuntimeException
* @throws FileSystemException
*/
public function execute(InputInterface $input, OutputInterface $output): int
public function handle(): int
{
// 从参数中获取要编译的 libraries并转换为数组
$libraries = array_map('trim', array_filter(explode(',', $input->getArgument('libraries'))));
$libraries = array_map('trim', array_filter(explode(',', $this->getArgument('libraries'))));
// 删除旧资源
if ($input->getOption('clean')) {
if ($this->getOption('clean')) {
logger()->warning('You are doing some operations that not recoverable: removing directories below');
logger()->warning(BUILD_ROOT_PATH);
logger()->warning('I will remove these dir after you press [Enter] !');
@@ -59,7 +55,7 @@ class BuildLibsCommand extends BuildCommand
try {
// 构建对象
$builder = BuilderProvider::makeBuilderByInput($input);
$builder = BuilderProvider::makeBuilderByInput($this->input);
// 只编译 library 的情况下,标记
$builder->setLibsOnly();
// 编译和检查库完整
@@ -69,7 +65,7 @@ class BuildLibsCommand extends BuildCommand
logger()->info('Build libs complete, used ' . $time . ' s !');
return 0;
} catch (\Throwable $e) {
if ($input->getOption('debug')) {
if ($this->getOption('debug')) {
ExceptionHandler::getInstance()->handle($e);
} else {
logger()->critical('Build failed with ' . get_class($e) . ': ' . $e->getMessage());

View File

@@ -7,42 +7,45 @@ namespace SPC\command;
use CliHelper\Tools\ArgFixer;
use CliHelper\Tools\DataProvider;
use CliHelper\Tools\SeekableArrayIterator;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/** @noinspection PhpUnused */
#[AsCommand('deploy', 'Deploy static-php-cli self to an .phar application')]
class DeployCommand extends BaseCommand
{
protected static $defaultName = 'deploy-self';
public function configure()
{
$this->setDescription('Deploy static-php-cli self to an .phar application');
$this->addArgument('target', InputArgument::OPTIONAL, 'The file or directory to pack.');
$this->addOption('auto-phar-fix', null, InputOption::VALUE_NONE, 'Automatically fix ini option.');
$this->addOption('overwrite', 'W', InputOption::VALUE_NONE, 'Overwrite existing files.');
$this->addOption('disable-gzip', 'z', InputOption::VALUE_NONE, 'disable gzip archive mode');
}
public function execute(InputInterface $input, OutputInterface $output): int
public function handle(): int
{
// 第一阶段流程如果没有写path将会提示输入要打包的path
$prompt = new ArgFixer($input, $output);
$prompt = new ArgFixer($this->input, $this->output);
// 首先得确认是不是关闭了readonly模式
if (ini_get('phar.readonly') == 1) {
if ($input->getOption('auto-phar-fix')) {
if ($this->getOption('auto-phar-fix')) {
$ask = true;
} else {
$ask = $prompt->requireBool('<comment>pack command needs "phar.readonly" = "Off" !</comment>' . PHP_EOL . 'If you want to automatically set it and continue, just Enter', true);
}
$output->writeln('<info>Now running command in child process.</info>');
if ($ask) {
global $argv;
passthru(PHP_BINARY . ' -d phar.readonly=0 ' . implode(' ', $argv), $retcode);
exit($retcode);
$args = array_merge(['-d', 'phar.readonly=0'], $_SERVER['argv']);
if (function_exists('pcntl_exec')) {
$this->output->writeln('<info>Changing to phar.readonly=0 mode ...</info>');
if (pcntl_exec(PHP_BINARY, $args) === false) {
throw new \PharException('切换到读写模式失败,请检查环境。');
}
} else {
$this->output->writeln('<info>Now running command in child process.</info>');
passthru(PHP_BINARY . ' -d phar.readonly=0 ' . implode(' ', $argv), $retcode);
exit($retcode);
}
}
}
// 获取路径
@@ -54,9 +57,9 @@ class DeployCommand extends BaseCommand
$phar_path = '/tmp/' . $phar_path;
}
if (file_exists($phar_path)) {
$ask = $input->getOption('overwrite') ? true : $prompt->requireBool('<comment>The file "' . $phar_path . '" already exists, do you want to overwrite it?</comment>' . PHP_EOL . 'If you want to, just Enter');
$ask = $this->getOption('overwrite') ? true : $prompt->requireBool('<comment>The file "' . $phar_path . '" already exists, do you want to overwrite it?</comment>' . PHP_EOL . 'If you want to, just Enter');
if (!$ask) {
$output->writeln('<comment>User canceled.</comment>');
$this->output->writeln('<comment>User canceled.</comment>');
return 1;
}
@unlink($phar_path);
@@ -67,7 +70,7 @@ class DeployCommand extends BaseCommand
$all = DataProvider::scanDirFiles($path, true, true);
$all = array_filter($all, function ($x) {
$dirs = preg_match('/(^(bin|config|src|vendor)\\/|^(composer\\.json|README\\.md|source\\.json|LICENSE|README-en\\.md)$)/', $x);
$dirs = preg_match('/(^(config|src|vendor)\\/|^(composer\\.json|README\\.md|source\\.json|LICENSE|README-en\\.md)$)/', $x);
return !($dirs !== 1);
});
sort($all);
@@ -76,9 +79,12 @@ class DeployCommand extends BaseCommand
$map[$v] = $path . '/' . $v;
}
$output->writeln('<info>Start packing files...</info>');
$this->output->writeln('<info>Start packing files...</info>');
try {
$phar->buildFromIterator(new SeekableArrayIterator($map, new ProgressBar($output)));
foreach ($this->progress()->iterate($map) as $file => $origin_file) {
$phar->addFromString($file, php_strip_whitespace($origin_file));
}
// $phar->buildFromIterator(new SeekableArrayIterator($map, new ProgressBar($output)));
$phar->addFromString(
'.phar-entry.php',
str_replace(
@@ -90,27 +96,36 @@ class DeployCommand extends BaseCommand
$stub = '.phar-entry.php';
$phar->setStub($phar->createDefaultStub($stub));
} catch (\Throwable $e) {
$output->writeln($e);
$this->output->writeln($e);
return 1;
}
$phar->addFromString('.prod', 'true');
if (!$input->getOption('disable-gzip')) {
$phar->compressFiles(\Phar::GZ);
}
$phar->stopBuffering();
$output->writeln(PHP_EOL . 'Done! Phar file is generated at "' . $phar_path . '".');
$this->output->writeln(PHP_EOL . 'Done! Phar file is generated at "' . $phar_path . '".');
if (file_exists(SOURCE_PATH . '/php-src/sapi/micro/micro.sfx')) {
$output->writeln('Detected you have already compiled micro binary, I will make executable now for you!');
$this->output->writeln('Detected you have already compiled micro binary, I will make executable now for you!');
file_put_contents(
$phar_path . '.exe',
pathinfo($phar_path, PATHINFO_DIRNAME) . '/spc',
file_get_contents(SOURCE_PATH . '/php-src/sapi/micro/micro.sfx') .
file_get_contents($phar_path)
);
chmod($phar_path . '.exe', 0755);
$output->writeln('<info>Static: ' . $phar_path . '.exe</info>');
chmod(pathinfo($phar_path, PATHINFO_DIRNAME) . '/spc', 0755);
$this->output->writeln('<info>Binary Executable: ' . pathinfo($phar_path, PATHINFO_DIRNAME) . '/spc</info>');
}
chmod($phar_path, 0755);
$output->writeln('<info>Phar: ' . $phar_path . '</info>');
$this->output->writeln('<info>Phar Executable: ' . $phar_path . '</info>');
return 0;
}
private function progress(int $max = 0): ProgressBar
{
$progress = new ProgressBar($this->output, $max);
$progress->setBarCharacter('<fg=green>⚬</>');
$progress->setEmptyBarCharacter('<fg=red>⚬</>');
$progress->setProgressCharacter('<fg=green>➤</>');
$progress->setFormat(
"%current%/%max% [%bar%] %percent:3s%%\n🪅 %estimated:-20s% %memory:20s%" . PHP_EOL
);
return $progress;
}
}

View File

@@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
namespace SPC\command;
use SPC\doctor\CheckListHandler;
use Symfony\Component\Console\Attribute\AsCommand;
#[AsCommand('doctor', 'Diagnose whether the current environment can compile normally')]
class DoctorCommand extends BaseCommand
{
public function handle(): int
{
try {
$checker = new CheckListHandler($this->input, $this->output);
$checker->runCheck(FIX_POLICY_PROMPT);
$this->output->writeln('<info>Doctor check complete !</info>');
} catch (\Throwable $e) {
$this->output->writeln('<error>' . $e->getMessage() . '</error>');
return 1;
}
return 0;
}
}

View File

@@ -4,26 +4,70 @@ declare(strict_types=1);
namespace SPC\command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use SPC\exception\FileSystemException;
use SPC\exception\RuntimeException;
use SPC\exception\WrongUsageException;
use SPC\util\DependencyUtil;
use SPC\util\LicenseDumper;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputOption;
/**
* 修改 config 后对其 kv 进行排序的操作
*/
#[AsCommand('dump-license', 'Dump licenses for required libraries')]
class DumpLicenseCommand extends BaseCommand
{
protected static $defaultName = 'dump-license';
public function configure()
{
$this->setDescription('Dump licenses for required libraries');
$this->addArgument('config-name', InputArgument::REQUIRED, 'Your config to be sorted, you can sort "lib", "source" and "ext".');
$this->addOption('by-extensions', null, InputOption::VALUE_REQUIRED, 'Dump by extensions and related libraries', null);
$this->addOption('without-php', null, InputOption::VALUE_NONE, 'Dump without php-src');
$this->addOption('by-libs', null, InputOption::VALUE_REQUIRED, 'Dump by libraries', null);
$this->addOption('by-sources', null, InputOption::VALUE_REQUIRED, 'Dump by original sources (source.json)', null);
$this->addOption('dump-dir', null, InputOption::VALUE_REQUIRED, 'Change dump directory', BUILD_ROOT_PATH . '/license');
}
public function execute(InputInterface $input, OutputInterface $output): int
/**
* @throws WrongUsageException
* @throws FileSystemException
* @throws RuntimeException
*/
public function handle(): int
{
$output->writeln('<info>not implemented</info>');
$dumper = new LicenseDumper();
if ($this->getOption('by-extensions') !== null) {
// 从参数中获取要编译的 extensions并转换为数组
$extensions = array_map('trim', array_filter(explode(',', $this->getOption('by-extensions'))));
// 根据提供的扩展列表获取依赖库列表并编译
[$extensions, $libraries, $not_included] = DependencyUtil::getExtLibsByDeps($extensions);
$dumper->addExts($extensions);
$dumper->addLibs($libraries);
if (!$this->getOption('without-php')) {
$dumper->addSources(['php-src']);
}
$dumper->dump($this->getOption('dump-dir'));
$this->output->writeln('Dump license with extensions: ' . implode(', ', $extensions));
$this->output->writeln('Dump license with libraries: ' . implode(', ', $libraries));
$this->output->writeln('Dump license with' . ($this->getOption('without-php') ? 'out' : '') . ' php-src');
$this->output->writeln('Dump target dir: ' . $this->getOption('dump-dir'));
return 0;
}
if ($this->getOption('by-libs') !== null) {
$libraries = array_map('trim', array_filter(explode(',', $this->getOption('by-libs'))));
$libraries = DependencyUtil::getLibsByDeps($libraries);
$dumper->addLibs($libraries);
$dumper->dump($this->getOption('dump-dir'));
$this->output->writeln('Dump target dir: ' . $this->getOption('dump-dir'));
return 0;
}
if ($this->getOption('by-sources') !== null) {
$sources = array_map('trim', array_filter(explode(',', $this->getOption('by-sources'))));
$dumper->addSources($sources);
$dumper->dump($this->getOption('dump-dir'));
$this->output->writeln('Dump target dir: ' . $this->getOption('dump-dir'));
return 0;
}
$this->output->writeln('You must use one of "--by-extensions=", "--by-libs=", "--by-sources=" to dump');
return 1;
}
}

View File

@@ -12,23 +12,21 @@ use SPC\exception\RuntimeException;
use SPC\store\Config;
use SPC\store\Downloader;
use SPC\util\Patcher;
use SPC\util\Util;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/** @noinspection PhpUnused */
#[AsCommand('fetch', 'Fetch required sources')]
class FetchSourceCommand extends BaseCommand
{
protected static $defaultName = 'fetch';
protected string $php_major_ver;
protected InputInterface $input;
public function configure()
{
$this->setDescription('Fetch required sources');
$this->addArgument('extensions', InputArgument::REQUIRED, 'The extensions will be compiled, comma separated');
$this->addArgument('libraries', InputArgument::REQUIRED, 'The libraries will be compiled, comma separated');
$this->addOption('hash', null, null, 'Hash only');
@@ -49,12 +47,12 @@ class FetchSourceCommand extends BaseCommand
parent::initialize($input, $output);
}
public function execute(InputInterface $input, OutputInterface $output): int
public function handle(): int
{
$this->input = $input;
try {
// 匹配版本
$ver = $this->php_major_ver = $input->getOption('with-php') ?? '8.1';
$ver = $this->php_major_ver = $this->getOption('with-php') ?? '8.1';
define('SPC_BUILD_PHP_VERSION', $ver);
preg_match('/^\d+\.\d+$/', $ver, $matches);
if (!$matches) {
logger()->error("bad version arg: {$ver}, x.y required!");
@@ -62,7 +60,7 @@ class FetchSourceCommand extends BaseCommand
}
// 删除旧资源
if ($input->getOption('clean')) {
if ($this->getOption('clean')) {
logger()->warning('You are doing some operations that not recoverable: removing directories below');
logger()->warning(SOURCE_PATH);
logger()->warning(DOWNLOAD_PATH);
@@ -83,7 +81,7 @@ class FetchSourceCommand extends BaseCommand
}
// 使用浅克隆可以减少调用 git 命令下载资源时的存储空间占用
if ($input->getOption('shallow-clone')) {
if ($this->getOption('shallow-clone')) {
define('GIT_SHALLOW_CLONE', true);
}
@@ -91,7 +89,7 @@ class FetchSourceCommand extends BaseCommand
Config::getSource('openssl');
// 是否启用openssl11
if ($input->getOption('with-openssl11')) {
if ($this->getOption('with-openssl11')) {
logger()->debug('Using openssl 1.1');
// 手动修改配置
Config::$source['openssl']['regex'] = '/href="(?<file>openssl-(?<version>1.[^"]+)\.tar\.gz)\"/';
@@ -101,7 +99,7 @@ class FetchSourceCommand extends BaseCommand
$chosen_sources = ['micro'];
// 从参数中获取要编译的 libraries并转换为数组
$libraries = array_map('trim', array_filter(explode(',', $input->getArgument('libraries'))));
$libraries = array_map('trim', array_filter(explode(',', $this->getArgument('libraries'))));
if ($libraries) {
foreach ($libraries as $lib) {
// 从 lib 的 config 中找到对应 source 资源名称,组成一个 lib 的 source 列表
@@ -113,7 +111,7 @@ class FetchSourceCommand extends BaseCommand
}
// 从参数中获取要编译的 extensions并转换为数组
$extensions = array_map('trim', array_filter(explode(',', $input->getArgument('extensions'))));
$extensions = array_map('trim', array_filter(explode(',', $this->getArgument('extensions'))));
if ($extensions) {
foreach ($extensions as $lib) {
if (Config::getExt($lib, 'type') !== 'builtin') {
@@ -131,9 +129,9 @@ class FetchSourceCommand extends BaseCommand
$chosen_sources = array_unique($chosen_sources);
// 是否只hash不下载资源
if ($input->getOption('hash')) {
if ($this->getOption('hash')) {
$hash = $this->doHash($chosen_sources);
$output->writeln($hash);
$this->output->writeln($hash);
return 0;
}
@@ -142,9 +140,7 @@ class FetchSourceCommand extends BaseCommand
f_mkdir(DOWNLOAD_PATH);
// 下载 PHP
logger()->info('Fetching PHP source');
Downloader::fetchSource('php-src', Downloader::getLatestPHPInfo($ver));
array_unshift($chosen_sources, 'php-src');
// 下载别的依赖资源
$cnt = count($chosen_sources);
$ni = 0;
@@ -167,10 +163,10 @@ class FetchSourceCommand extends BaseCommand
return 0;
} catch (\Throwable $e) {
// 不开 debug 模式就不要再显示复杂的调试栈信息了
if ($input->getOption('debug')) {
if ($this->getOption('debug')) {
ExceptionHandler::getInstance()->handle($e);
} else {
logger()->emergency($e->getMessage() . ', previous message: ' . $e->getPrevious()->getMessage());
logger()->emergency($e->getMessage() . ', previous message: ' . $e->getPrevious()?->getMessage());
}
return 1;
}
@@ -215,11 +211,11 @@ class FetchSourceCommand extends BaseCommand
private function doPatch(): void
{
// swow 需要软链接内部的文件夹才能正常编译
if (!file_exists(SOURCE_PATH . '/php-src/ext/swow')) {
if (!file_exists(SOURCE_PATH . '/php-src/ext/swow') && Util::getPHPVersionID() >= 80000) {
Patcher::patchSwow();
// patch 一些 PHP 的资源,以便编译
Patcher::patchMicroThings();
}
// patch 一些 PHP 的资源,以便编译
Patcher::patchPHPDepFiles();
// openssl 3 需要 patch 额外的东西
if (!$this->input->getOption('with-openssl11') && $this->php_major_ver === '8.0') {

View File

@@ -4,23 +4,19 @@ declare(strict_types=1);
namespace SPC\command;
use SPC\builder\traits\NoMotdTrait;
use SPC\exception\FileSystemException;
use SPC\store\Config;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Attribute\AsCommand;
#[AsCommand('list-ext', 'List supported extensions')]
class ListExtCommand extends BaseCommand
{
use NoMotdTrait;
protected bool $no_motd = true;
protected static $defaultName = 'list-ext';
public function configure()
{
$this->setDescription('List supported extensions');
}
public function execute(InputInterface $input, OutputInterface $output)
/**
* @throws FileSystemException
*/
public function handle(): int
{
foreach (Config::getExts() as $ext => $meta) {
echo $ext . PHP_EOL;

View File

@@ -8,20 +8,17 @@ use SPC\exception\FileSystemException;
use SPC\exception\ValidationException;
use SPC\store\FileSystem;
use SPC\util\ConfigValidator;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* 修改 config 后对其 kv 进行排序的操作
*/
#[AsCommand('sort-config', 'After config edited, sort it by alphabet')]
class SortConfigCommand extends BaseCommand
{
protected static $defaultName = 'sort-config';
public function configure()
{
$this->setDescription('After config edited, sort it by alphabet');
$this->addArgument('config-name', InputArgument::REQUIRED, 'Your config to be sorted, you can sort "lib", "source" and "ext".');
}
@@ -29,9 +26,9 @@ class SortConfigCommand extends BaseCommand
* @throws ValidationException
* @throws FileSystemException
*/
public function execute(InputInterface $input, OutputInterface $output): int
public function handle(): int
{
switch ($name = $input->getArgument('config-name')) {
switch ($name = $this->getArgument('config-name')) {
case 'lib':
$file = json_decode(FileSystem::readFile(ROOT_DIR . '/config/lib.json'), true);
ConfigValidator::validateLibs($file);
@@ -51,10 +48,10 @@ class SortConfigCommand extends BaseCommand
file_put_contents(ROOT_DIR . '/config/ext.json', json_encode($file, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
break;
default:
$output->writeln("<error>invalid config name: {$name}</error>");
$this->output->writeln("<error>invalid config name: {$name}</error>");
return 1;
}
$output->writeln('<info>sort success</info>');
$this->output->writeln('<info>sort success</info>');
return 0;
}
}

View File

@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace SPC\doctor;
#[\Attribute(\Attribute::TARGET_METHOD)]
class AsCheckItem
{
public mixed $callback = null;
public function __construct(
public string $item_name,
public ?string $limit_os = null,
public int $level = 100
) {
}
}

View File

@@ -0,0 +1,13 @@
<?php
declare(strict_types=1);
namespace SPC\doctor;
#[\Attribute(\Attribute::TARGET_METHOD)]
class AsFixItem
{
public function __construct(public string $name)
{
}
}

View File

@@ -0,0 +1,128 @@
<?php
declare(strict_types=1);
namespace SPC\doctor;
use SPC\exception\FileSystemException;
use SPC\exception\RuntimeException;
use SPC\store\FileSystem;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
class CheckListHandler
{
/** @var AsCheckItem[] */
private array $check_list = [];
private array $fix_map = [];
/**
* @throws \ReflectionException
* @throws FileSystemException
* @throws RuntimeException
*/
public function __construct(private InputInterface $input, private OutputInterface $output)
{
$this->loadCheckList();
}
/**
* @throws RuntimeException
*/
public function runCheck(int $fix_policy = FIX_POLICY_DIE): void
{
foreach ($this->check_list as $item) {
if ($item->limit_os !== null && $item->limit_os !== PHP_OS_FAMILY) {
continue;
}
$this->output->write('Checking <comment>' . $item->item_name . '</comment> ... ');
$result = call_user_func($item->callback);
if ($result === null) {
$this->output->writeln('skipped');
} elseif ($result instanceof CheckResult) {
if ($result->isOK()) {
$this->output->writeln('ok');
continue;
}
// Failed
$this->output->writeln('<error>' . $result->getMessage() . '</error>');
switch ($fix_policy) {
case FIX_POLICY_DIE:
throw new RuntimeException('Some check items can not be fixed !');
case FIX_POLICY_PROMPT:
if ($result->getFixItem() !== '') {
$helper = new QuestionHelper();
$question = new ConfirmationQuestion('Do you want to fix it? [Y/n] ', true);
if ($helper->ask($this->input, $this->output, $question)) {
$this->emitFix($result);
} else {
throw new RuntimeException('You cancelled fix');
}
} else {
throw new RuntimeException('Some check items can not be fixed !');
}
break;
case FIX_POLICY_AUTOFIX:
if ($result->getFixItem() !== '') {
$this->output->writeln('Automatically fixing ' . $result->getFixItem() . ' ...');
$this->emitFix($result);
} else {
throw new RuntimeException('Some check items can not be fixed !');
}
break;
}
}
}
}
/**
* Load Doctor check item list
*
* @throws \ReflectionException
* @throws RuntimeException
* @throws FileSystemException
*/
private function loadCheckList(): void
{
foreach (FileSystem::getClassesPsr4(__DIR__ . '/item', 'SPC\\doctor\\item') as $class) {
$ref = new \ReflectionClass($class);
foreach ($ref->getMethods() as $method) {
$attr = $method->getAttributes(AsCheckItem::class);
if (isset($attr[0])) {
/** @var AsCheckItem $instance */
$instance = $attr[0]->newInstance();
$instance->callback = [new $class(), $method->getName()];
$this->check_list[] = $instance;
continue;
}
$attr = $method->getAttributes(AsFixItem::class);
if (isset($attr[0])) {
/** @var AsFixItem $instance */
$instance = $attr[0]->newInstance();
// Redundant fix item
if (isset($this->fix_map[$instance->name])) {
throw new RuntimeException('Redundant doctor fix item: ' . $instance->name);
}
$this->fix_map[$instance->name] = [new $class(), $method->getName()];
}
}
}
// sort check list by level
usort($this->check_list, fn ($a, $b) => $a->level > $b->level ? -1 : ($a->level == $b->level ? 0 : 1));
}
private function emitFix(CheckResult $result)
{
$fix = $this->fix_map[$result->getFixItem()];
$fix_result = call_user_func($fix, ...$result->getFixParams());
if ($fix_result) {
$this->output->writeln('<info>Fix done</info>');
} else {
$this->output->writeln('<error>Fix failed</error>');
throw new RuntimeException('Some check item are not fixed');
}
}
}

View File

@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
namespace SPC\doctor;
class CheckResult
{
public function __construct(private string $message = '', private string $fix_item = '', private array $fix_params = [])
{
}
public static function fail(string $message, string $fix_item = '', array $fix_params = []): CheckResult
{
return new static($message, $fix_item, $fix_params);
}
public static function ok(): CheckResult
{
return new static();
}
public function getMessage(): string
{
return $this->message;
}
public function getFixItem(): string
{
return $this->fix_item;
}
public function getFixParams(): array
{
return $this->fix_params;
}
public function isOK(): bool
{
return empty($this->message);
}
}

View File

@@ -0,0 +1,84 @@
<?php
declare(strict_types=1);
namespace SPC\doctor\item;
use SPC\builder\traits\UnixSystemUtilTrait;
use SPC\doctor\AsCheckItem;
use SPC\doctor\AsFixItem;
use SPC\doctor\CheckResult;
use SPC\exception\RuntimeException;
class MacOSToolCheckList
{
use UnixSystemUtilTrait;
/** @var string[] MacOS 环境下编译依赖的命令 */
public const REQUIRED_COMMANDS = [
'curl',
'make',
'bison',
'flex',
'pkg-config',
'git',
'autoconf',
'automake',
'tar',
'unzip',
'xz',
'gzip',
'bzip2',
'gotop',
'cmake',
];
#[AsCheckItem('if homebrew has installed', limit_os: 'Darwin', level: 998)]
public function checkBrew(): ?CheckResult
{
// 检查 homebrew 是否已经安装
if ($this->findCommand('brew') === null) {
return CheckResult::fail('Homebrew is not installed', 'brew');
}
return CheckResult::ok();
}
#[AsCheckItem('if necessary tools are installed', limit_os: 'Darwin')]
public function checkCliTools(): ?CheckResult
{
$missing = [];
foreach (self::REQUIRED_COMMANDS as $cmd) {
if ($this->findCommand($cmd) === null) {
$missing[] = $cmd;
}
}
if (!empty($missing)) {
return CheckResult::fail('missing system commands: ' . implode(', ', $missing), 'build-tools', [$missing]);
}
return CheckResult::ok();
}
#[AsFixItem('brew')]
public function fixBrew(): bool
{
try {
shell(true)->exec('/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"');
} catch (RuntimeException) {
return false;
}
return true;
}
#[AsFixItem('build-tools')]
public function fixBuildTools(array $missing): bool
{
foreach ($missing as $cmd) {
try {
shell(true)->exec('brew install ' . escapeshellarg($cmd));
} catch (RuntimeException) {
return false;
}
}
return true;
}
}

View File

@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
namespace SPC\doctor\item;
use SPC\builder\traits\UnixSystemUtilTrait;
use SPC\doctor\AsCheckItem;
use SPC\doctor\CheckResult;
class OSCheckList
{
use UnixSystemUtilTrait;
#[AsCheckItem('if current os are supported', level: 999)]
public function checkOS(): ?CheckResult
{
if (!in_array(PHP_OS_FAMILY, ['Darwin', 'Linux'])) {
return CheckResult::fail('Current OS is not supported');
}
return CheckResult::ok();
}
}

View File

@@ -0,0 +1,9 @@
<?php
declare(strict_types=1);
namespace SPC\exception;
class WrongUsageException extends \Exception
{
}

Some files were not shown because too many files have changed in this diff Show More