From 09af173013b36864301984205f13a70001471e54 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Tue, 27 Jan 2026 18:31:05 +0800 Subject: [PATCH] =?UTF-8?q?feat(compiler):=20=E6=B7=BB=E5=8A=A0=E9=A1=B9?= =?UTF-8?q?=E7=9B=AE=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=92=8C=E7=BC=96=E8=AF=91=E9=80=89=E9=A1=B9=E6=89=A9=E5=B1=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 CompilerBase 中新增 cxxflags 和 ldflags 属性用于编译选项配置 - 集成 symfony/yaml 依赖以支持 YAML 配置文件解析 - 实现 project.yml 配置文件格式用于定义项目构建参数 - 添加 parseProjectYaml 方法解析 YAML 配置并设置编译标志 - 修改 getFiles 方法以支持目录扫描和 YAML 配置文件处理 - 更新 composer.json 和 composer.lock 文件添加新依赖项 --- composer.json | 3 +- composer.lock | 243 ++++++++++++++++++++++++------------- examples/prime/project.yml | 10 ++ src/Php/CompilerBase.php | 9 ++ src/Php/Translator.php | 51 +++++++- 5 files changed, 226 insertions(+), 90 deletions(-) create mode 100644 examples/prime/project.yml diff --git a/composer.json b/composer.json index e5ede0a3..9b443689 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,8 @@ "nikic/php-parser": "5.6.1", "league/climate": "^3.10", "marcj/topsort": "^2.0", - "symfony/var-dumper": "^8.0" + "symfony/var-dumper": "^8.0", + "symfony/yaml": "^8.0" }, "require-dev": { "phpunit/phpunit": "^10.4", diff --git a/composer.lock b/composer.lock index 8ffa02a5..626bf9b1 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "4982ed71f89f83b090b223c2a581f719", + "content-hash": "9cc0c6bd977c8959d0cd9596d3962b37", "packages": [ { "name": "league/climate", @@ -297,6 +297,89 @@ }, "time": "2020-12-15T21:32:01+00:00" }, + { + "name": "symfony/polyfill-ctype", + "version": "v1.33.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-ctype.git", + "reference": "a3cc8b044a6ea513310cbd48ef7333b384945638" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/a3cc8b044a6ea513310cbd48ef7333b384945638", + "reference": "a3cc8b044a6ea513310cbd48ef7333b384945638", + "shasum": "" + }, + "require": { + "php": ">=7.2" + }, + "provide": { + "ext-ctype": "*" + }, + "suggest": { + "ext-ctype": "For best performance" + }, + "type": "library", + "extra": { + "thanks": { + "url": "https://github.com/symfony/polyfill", + "name": "symfony/polyfill" + } + }, + "autoload": { + "files": [ + "bootstrap.php" + ], + "psr-4": { + "Symfony\\Polyfill\\Ctype\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Gert de Pagter", + "email": "BackEndTea@gmail.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill for ctype functions", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "ctype", + "polyfill", + "portable" + ], + "support": { + "source": "https://github.com/symfony/polyfill-ctype/tree/v1.33.0" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://github.com/nicolas-grekas", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2024-09-09T11:45:10+00:00" + }, { "name": "symfony/polyfill-mbstring", "version": "v1.33.0", @@ -468,6 +551,81 @@ } ], "time": "2026-01-01T23:07:29+00:00" + }, + { + "name": "symfony/yaml", + "version": "v8.0.1", + "source": { + "type": "git", + "url": "https://github.com/symfony/yaml.git", + "reference": "7a1a90ba1df6e821a6b53c4cabdc32a56cabfb14" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/yaml/zipball/7a1a90ba1df6e821a6b53c4cabdc32a56cabfb14", + "reference": "7a1a90ba1df6e821a6b53c4cabdc32a56cabfb14", + "shasum": "" + }, + "require": { + "php": ">=8.4", + "symfony/polyfill-ctype": "^1.8" + }, + "conflict": { + "symfony/console": "<7.4" + }, + "require-dev": { + "symfony/console": "^7.4|^8.0" + }, + "bin": [ + "Resources/bin/yaml-lint" + ], + "type": "library", + "autoload": { + "psr-4": { + "Symfony\\Component\\Yaml\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Loads and dumps YAML files", + "homepage": "https://symfony.com", + "support": { + "source": "https://github.com/symfony/yaml/tree/v8.0.1" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://github.com/nicolas-grekas", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2025-12-04T18:17:06+00:00" } ], "packages-dev": [ @@ -3686,89 +3844,6 @@ ], "time": "2025-11-12T15:55:31+00:00" }, - { - "name": "symfony/polyfill-ctype", - "version": "v1.33.0", - "source": { - "type": "git", - "url": "https://github.com/symfony/polyfill-ctype.git", - "reference": "a3cc8b044a6ea513310cbd48ef7333b384945638" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/a3cc8b044a6ea513310cbd48ef7333b384945638", - "reference": "a3cc8b044a6ea513310cbd48ef7333b384945638", - "shasum": "" - }, - "require": { - "php": ">=7.2" - }, - "provide": { - "ext-ctype": "*" - }, - "suggest": { - "ext-ctype": "For best performance" - }, - "type": "library", - "extra": { - "thanks": { - "url": "https://github.com/symfony/polyfill", - "name": "symfony/polyfill" - } - }, - "autoload": { - "files": [ - "bootstrap.php" - ], - "psr-4": { - "Symfony\\Polyfill\\Ctype\\": "" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Gert de Pagter", - "email": "BackEndTea@gmail.com" - }, - { - "name": "Symfony Community", - "homepage": "https://symfony.com/contributors" - } - ], - "description": "Symfony polyfill for ctype functions", - "homepage": "https://symfony.com", - "keywords": [ - "compatibility", - "ctype", - "polyfill", - "portable" - ], - "support": { - "source": "https://github.com/symfony/polyfill-ctype/tree/v1.33.0" - }, - "funding": [ - { - "url": "https://symfony.com/sponsor", - "type": "custom" - }, - { - "url": "https://github.com/fabpot", - "type": "github" - }, - { - "url": "https://github.com/nicolas-grekas", - "type": "github" - }, - { - "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", - "type": "tidelift" - } - ], - "time": "2024-09-09T11:45:10+00:00" - }, { "name": "symfony/polyfill-intl-grapheme", "version": "v1.33.0", diff --git a/examples/prime/project.yml b/examples/prime/project.yml new file mode 100644 index 00000000..0a4a3d11 --- /dev/null +++ b/examples/prime/project.yml @@ -0,0 +1,10 @@ +name: prime2 +version: 0.0.1 +cxxflags: | + -std=c++17 + -I/usr/include/python3.10 + -Wall +ldflags: | + -L/usr/lib/x86_64-linux-gnu/ + -lssl + diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 7e85bbbd..a01977eb 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -79,6 +79,8 @@ class CompilerBase extends \PhpAot\Core\Translator protected array $redoAfterDeclare = []; protected int $optimizeLevel = 0; protected string $buildMode = 'bin'; + protected string $cxxflags = ''; + protected string $ldflags = ''; protected int $floatPrecision = 17; protected bool $debugInfo = true; protected bool $noLiteralStrings = false; @@ -1254,6 +1256,13 @@ class CompilerBase extends \PhpAot\Core\Translator if ($link) { $cmd .= ' ' . $this->parseLdflags(); $cmd .= ' ' . $this->parseLibs(); + if ($this->ldflags) { + $cmd .= ' ' . $this->ldflags; + } + } else { + if ($this->cxxflags) { + $cmd .= ' ' . $this->cxxflags; + } } } diff --git a/src/Php/Translator.php b/src/Php/Translator.php index 1f6911d7..7957865a 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -3,6 +3,7 @@ namespace PhpAot\Php; use MJS\TopSort\Implementations\StringSort; +use Symfony\Component\Yaml\Yaml; use PhpParser\Modifiers; use PhpParser\Node; use PhpParser\Node\Stmt\Foreach_; @@ -158,6 +159,39 @@ class Translator extends Preprocessor $this->targetName = $name; } + protected function getFilesFromDir(string $path): array + { + $scanner = new FileScanner($path); + return $scanner->scan(); + } + + protected function parseProjectYaml(string $path): array + { + $cfg = Yaml::parseFile($path); + + if (!empty($cfg['sources'])) { + $dirs = $cfg['sources']; + $list = []; + foreach ($dirs as $dir) { + $tmp = $this->getFilesFromDir($dir); + $list = array_merge($list, $tmp); + } + } else { + $dir = dirname($path); + $list = $this->getFilesFromDir($dir); + } + if (!empty($cfg['cxxflags'])) { + $this->cxxflags = str_replace("\n", " ", $cfg['cxxflags']); + } + if (!empty($cfg['ldflags'])) { + $this->ldflags = str_replace("\n", " ", $cfg['ldflags']); + } + if (!empty($cfg['name'])) { + $this->setTargetName($cfg['name']); + } + return $list; + } + public function getFiles(string $path): array { $realpath = realpath($path); @@ -167,14 +201,21 @@ class Translator extends Preprocessor $path = $realpath; if (is_dir($path)) { - $scanner = new FileScanner($path); - $list = $scanner->scan(); + $list = $this->getFilesFromDir($path); $targetName = basename($path); + $this->setTargetName($targetName); } else { - $list = [$path]; - $targetName = FileScanner::getFileName($path); + $ext = pathinfo($path, PATHINFO_EXTENSION); + if ($ext === 'yml') { + $list = $this->parseProjectYaml($path); + } elseif ($ext === 'php') { + $list = [$path]; + $targetName = FileScanner::getFileName($path); + $this->setTargetName($targetName); + } else { + $this->error('Unsupported file type: ' . $path); + } } - $this->setTargetName($targetName); return $list; }