feat(compiler): 添加项目配置文件支持和编译选项扩展

- 在 CompilerBase 中新增 cxxflags 和 ldflags 属性用于编译选项配置
- 集成 symfony/yaml 依赖以支持 YAML 配置文件解析
- 实现 project.yml 配置文件格式用于定义项目构建参数
- 添加 parseProjectYaml 方法解析 YAML 配置并设置编译标志
- 修改 getFiles 方法以支持目录扫描和 YAML 配置文件处理
- 更新 composer.json 和 composer.lock 文件添加新依赖项
pull/1/head
韩天峰 7 months ago
parent 1fc07ee729
commit 09af173013
  1. 3
      composer.json
  2. 243
      composer.lock
  3. 10
      examples/prime/project.yml
  4. 9
      src/Php/CompilerBase.php
  5. 51
      src/Php/Translator.php

@ -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",

243
composer.lock generated

@ -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",

@ -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

@ -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;
}
}
}

@ -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;
}

Loading…
Cancel
Save